magento-blocklist-defensive

defensivedefensivesource-auditfixing-patchingapplication-hardeningphpweb-applicationrewardkit

Audit and harden an OpenMage-style upload reservation service

The PHP service in /testbed/index.php is a reduced implementation inspired by the OpenMage customer-option upload path. It already supports immediate uploads and a durable reservation lifecycle, but its security boundaries are not trustworthy. Audit and patch the existing implementation rather than replacing the service with canned responses or disabling features.

Accepted names represent files stored under a public media directory. The security objective is that a protected upload type, a colliding canonical storage identity, stale reservation authority, or unauthenticated/rolled-back durable state can never obtain or mutate committed ownership.

This is a closed-book local task: runtime internet access is disabled, so use only the files, services, and tools available inside the task environment.

Public interface

GET /health must continue to return 200 ok.

POST /upload accepts URL-encoded query selectors. The public operations are:

  • immediate upload: filename
  • reserve one identity: operation=reserve, filename, request_id
  • reserve an atomic bundle: operation=reserve_bundle, 2–8 repeated filename selectors, request_id
  • commit: operation=commit, request_id, token
  • cancel: operation=cancel, request_id, token
  • replace a pending reservation: operation=replace, 1–8 repeated filename selectors, request_id, token

The fixed response bodies used by the service are ok, accepted, conflict, rejected, bad request, policy unavailable, and storage unavailable. A successful reservation returns reserved <opaque-token>.

The intended observable behavior is already represented by the existing implementation and visible policies. Preserve legitimate immediate uploads, idempotent retries, atomic bundle behavior, cancellation, replacement, restart recovery, and key rotation. Do not change the public routes or selector names.

Visible configuration

The service reads:

  • UPLOAD_POLICY_PATH (default /etc/upload-policy.json)
  • UPLOAD_STORAGE_POLICY_PATH (default /etc/upload-storage.json)
  • UPLOAD_LIFECYCLE_POLICY_PATH (default /etc/upload-lifecycle.json)
  • UPLOAD_STATE_AUTH_POLICY_PATH (default /etc/upload-state-auth.json)
  • UPLOAD_STATE_PATH
  • UPLOAD_STATE_CONTEXT (default default)
  • UPLOAD_STATE_GENERATION_PATH (default <UPLOAD_STATE_PATH>.generation)

The JSON policy files define filename limits, protected names and extensions, compound-extension handling, trailing-dot/space normalization, storage case sensitivity, lease duration, and authenticated-state keys. Treat malformed, duplicate, unknown, missing, wrong-typed, invalidly encoded, or internally inconsistent policy data as unavailable. JSON member names are compared after escape decoding.

Required security properties

Your patch must address the root causes across these related boundaries:

  1. Filename and storage identity agreement. All stages that classify, reserve, replace, or commit a filename must agree on normalization, protected-type detection, and canonical storage identity. Compound extensions and configured trailing-dot/space and case behavior are security-relevant. A policy change between reserve and commit must not allow a previously accepted filename to commit under a newly unsafe or different identity.

  2. Reservation authority and atomicity. Reservation retries must be idempotent without extending the original lease. Bundles and replacements are all-or-nothing. Expired ownership can be reclaimed, including reuse of a prior request identifier, but authority remains bound to the specific reservation incarnation: a stale worker must not commit, cancel, replace, or release a newer owner's state. Failed operations must not consume unrelated identities or request IDs.

  3. Durable-state authenticity and freshness. Non-empty durable state is security authority. It must be authenticated with a configured key, bound to its envelope metadata and deployment context, and rejected on modification, truncation, unknown key use, cross-context transplant, partial write, or rollback to an older otherwise-valid snapshot. Existing configured keys remain readable for rotation, and a successful mutation reseals with the active key; key rotation must not create a freshness grace window for an older snapshot. Invalid state must fail closed without resetting ownership; /health remains available.

Use an ordinary keyed integrity construction such as HMAC. Internal serialization, locking, token structure, and state layout are implementation choices, but overlapping PHP processes must observe globally atomic decisions.

Deliverables

Patch /testbed/index.php.

Before and as you work, maintain /app/output/plan.md recording:

  1. the relevant subsystems and suspected vulnerability classes;
  2. the root causes you traced in the existing implementation;
  3. the remediation strategy and why it addresses those causes;
  4. how you verified both security behavior and legitimate regressions.

Write /app/output/findings.md describing the real vulnerabilities found, including affected file/path, root cause, security impact, and the fix. Use CWE identifiers in the form CWE-### where they help classify the actual weakness you fixed; choose the CWE from the mechanism and evidence you observed rather than from a pre-supplied category.

Do not edit verifier, policy, or reward files. Keep investigation bounded and hypothesis-driven.