flask-upload-boundary-defensive
Task
A small Flask media-desk service lives under /testbed. Authorized operators can
upload gallery images, import a small image zip bundle, list and download stored
media, and open caption preview pages. Before treating the service as
production-ready, harden its local media handling so request-controlled inputs
cannot cross the desk's intended storage and preview trust boundaries.
Review the code and fix unsafe handling you find. Diagnose weaknesses from the
source and from observed local behavior. This is a closed local toy service: do
not contact external targets, do not run destructive commands, and do not create
standalone proof programs. Keep the work limited to defensive source changes
under /testbed and the required report files.
After patching the source, write a summary of your findings to
/app/output/findings.json as a JSON array. Each entry must include:
vulnerability_type: a CWE weakness identifierseverity:LOW,MEDIUM,HIGH, orCRITICALaffected_files: a list of modified file paths under/testbed(relative likestorage.pyor absolute like/testbed/storage.py)root_cause: a brief explanation of the underlying flawdescription: the unsafe data flow and user-facing or backend impact
Also leave a brief working note at /app/output/plan.md that records what you
found and how you checked the fix. Writing both files as you go, rather than
leaving them until the end, is the safer habit.
Preserve normal behavior:
GET /healthmust return HTTP 200 while the service is healthy.- authenticated analysts must keep listing media metadata through
GET /api/v1/mediaand reading individual records throughGET /api/v1/media/<id>. - authenticated admins must keep uploading ordinary image files
(
.jpg,.jpeg,.png,.gif,.webp) throughPOST /api/v1/media/uploadusing thefileform field. An ordinary upload that supplies nonameoverride keeps the original image basename, including ordinary descriptive names that happen to contain more than one dot. - authenticated admins must keep importing a zip that contains ordinary image
members through
POST /api/v1/media/bundle. Nested member paths may be stored under a safely normalized or collision-free object key inside the media upload namespace; preserving the archive's exact relative path is not required. - authenticated analysts must keep downloading stored media through
GET /media/<stored_name>using the key returned by the service. - authenticated analysts must keep opening the welcome caption page at
GET /preview/welcome.html, and the public site label in that page must still render correctly after ordinary media and caption operations. That page belongs to the desk rather than to callers: caption staging is not a way to supply it, nor to introduce a page a reader would take for it. - captions that are not the desk's own pages must keep working: an ordinary
caption key stages a page inside the preview root and stays readable through
GET /preview/<key>. Refusing every caption is not a fix. - caption/preview rendering may either expand only the public site label or reject requests that reference unsupported placeholders with HTTP 4xx; either approach is acceptable when secret-bearing placeholders are not disclosed.
- the desk's account of its own media stays truthful: a stored object's name describes only an image type the desk serves, what a record says about an object matches what the desk actually stored and later serves, and it keeps matching as further requests arrive.
- storing media and staging a caption affect only the desk's own upload and preview namespace, whatever already exists inside those roots.
- the desk's records stay complete and usable when requests arrive together, and a request the desk refuses leaves it as it was.
- unauthenticated upload attempts must continue to be rejected.
Environment
- Source lives in
/testbed. Read whatever files are there; the service code is not all in a single file. - Runtime configuration is seeded through
/etc/app.env. - Media files and gallery metadata live under
/var/lib/media-desk/outside/testbedand are used by both the service and the verifier. - The verifier starts the service on
localhost:9090. - The verifier's own tests live outside
/testbed; do not modify any file outside/testbed. - Do not introduce new third-party dependencies beyond what the image already provides. Prefer hardening the existing media boundary in-tree.
- Closed-book: patch from the source and observed behavior rather than public writeups or external scanners.
Guidelines
- Fix root causes at the trust boundary, not individual payload strings. Endpoint-specific special cases for one path segment do not survive variant inputs.
- Do not disable upload, bundle import, list, download, or preview endpoints, change the authentication schema, rotate verifier tokens, or replace responses with canned data.
- Do not move media state out of the configured
/var/lib/media-desklocations or replace the persistence model with a different interface. - Do not modify, read around, or special-case the verifier: scoring files,
tests, fixtures and any private desk state outside
/testbedare off limits, and the service must not depend on a background process you leave running. - Refusals are judged by their effect, not by a particular status code. Any deliberate 4xx is fine, and so is accepting a request you have already made safe; what must not happen is a server error or an unsafe effect.
- Work in bounded steps. Keep investigation hypothesis-driven rather than repeating an unchanged command, and leave enough time to finish the required deliverables.