Privacy engineering

Handle Uploaded Files Outside the Web Application’s Trust Boundary

Treat uploaded files as untrusted inputs throughout receipt, processing, storage and download, with explicit states and bounded access.

An uploaded file should remain an untrusted input until the application has applied the checks required for its intended use. Receiving bytes is not the same as approving a document for processing or making it available to another person. Model those steps separately so a failed check, interrupted worker or mistaken storage setting cannot silently skip the intended boundary.

The OWASP File Upload Cheat Sheet recommends layered protections, including limiting accepted types and sizes, generating storage names, checking authorization and keeping uploaded files outside the webroot where possible. It cautions against relying on the client-supplied content type alone. The lifecycle below is an editorial design exercise for a fictional document portal, not a claim that one checklist secures every file format or deployment.

Define the smallest useful upload feature

Start with what the user needs to accomplish. A portal that accepts a profile photograph has different requirements from a service that imports spreadsheets. Supporting every file type because a library can receive it expands the review without necessarily improving the feature.

Write an acceptance contract in user-facing and implementation terms. The user-facing description explains which files are supported and the limits they will encounter. The implementation description identifies how the application determines eligibility and what happens when it cannot establish a result. Keep the two consistent so the interface does not promise support the processing path cannot deliver.

For the fictional portal, assume the feature stores a document for authorized project members. It does not need to execute document content or publish arbitrary files on the main application origin. Those exclusions simplify the design because they remove behaviors that are not part of the intended task.

Separate receipt from approval

Use explicit states for the upload lifecycle. A file can be received, awaiting checks, accepted for a defined use, rejected or awaiting cleanup. The actual names matter less than the behavior attached to them. A file should not become downloadable merely because an object now exists in storage.

Record which component is allowed to change each state. A browser can report that an upload finished, but the application should not treat that report as proof that server-side checks passed. A processing worker should record its result through a controlled path rather than leaving the web layer to infer success from the presence of an output file.

Think about interrupted transitions. If the worker stops after storing a result but before updating the database, the next attempt needs a defined reconciliation behavior. If the same completion message arrives twice, the second message should not create an uncontrolled second publication. A state model gives reviewers a place to reason about those cases.

Keep storage identity under application control

The original filename can be useful display information, but it should not automatically determine where the server writes the file. Assign a storage reference through the application and keep the display name as separately handled metadata. This makes the relationship between a user-provided label and an internal object explicit.

Use normal library and platform facilities for path handling rather than inventing a string manipulation scheme. Review the final storage destination and permissions in the deployment environment. A safe-looking name in a unit test does not establish that a production mount or web-server rule exposes only the intended directory.

Include the original display name in the review of rendered output and logs. A filename is still input supplied by another party. The application should present it as text and avoid turning it into executable markup, an unrestricted path or a diagnostic record that reveals unnecessary personal information.

Give processors a narrow job

A document converter or image processor has a different role from the main application. Identify the files it can read, the output it can write and the external access it actually requires. A processor should not receive broad application credentials simply because it runs in the same environment.

Bound the work. Consider input size, processing time, output size and the number of concurrent jobs. A small compressed input can require more work after expansion, so the limits should reflect the operation rather than only the initial HTTP request size. Choose actual values using the application’s requirements and the processor’s documented behavior.

Plan for a processor that cannot reach a conclusion. A timeout or unavailable scanning service should lead to a visible pending or failed state according to the approved policy. It should not quietly become acceptance because the happy path expects every job to finish quickly.

Make download authorization a separate decision

An accepted file still needs a rule for who may retrieve it. In the project portal, that rule might depend on current project membership and the document’s state. The upload permission and the download permission should be reviewed independently because they answer different questions.

Follow every delivery path. The normal application route, a preview image, an export bundle and a direct storage link may expose the same content through different mechanisms. A correct check on one route does not prove the others follow the same policy. List the paths in the feature’s access matrix.

If the design uses time-limited links, document what those links permit and the intended lifetime. Do not describe an unguessable URL as equivalent to a complete authorization policy. The team needs to understand how a link is issued, where it may be copied and what happens after membership or document state changes.

Test the lifecycle with harmless fixtures

Create a set of disposable fixtures for the supported format and the rejection cases the application claims to handle. Include an ordinary valid file, an unsupported type, an oversized file, a misleading display name and an interrupted upload. Use fixtures appropriate to the test environment rather than collecting dangerous files from unknown sources.

Check both state and access after each case. A rejected file should not be available through a preview or a direct storage path. A pending file should remain in the intended restricted location. An accepted file should be retrievable only by the actors allowed under the policy.

Test condition State question Access question
Upload completes normally Did the required checks complete? Can the intended member retrieve it?
Validation rejects the input Is rejection recorded clearly? Are all delivery paths closed?
Processing stops unexpectedly Is the unfinished job visible? Does the file remain restricted?
Membership is removed Is the document state unchanged? Does retrieval follow the current policy?

The matrix helps the team separate processing correctness from access correctness. Both matter, and one can fail while the other appears healthy.

Track derivatives and abandoned objects

A single upload can create thumbnails, extracted text, converted documents and temporary working files. Give those derivatives a traceable relationship to the original record. Otherwise, removing the original may leave copies that nobody can identify confidently.

Define the lifecycle for an upload that never becomes part of a completed task. An abandoned object is different from a rejected object and may need a different reconciliation path. The retention decision should come from the appropriate owner, then become an implementable rule with a test. Our guide to usable retention schedules explains that translation.

Record cleanup failures separately from legitimate holds. A storage listing that contains old files is not enough to explain whether they are intentionally retained, awaiting work or orphaned. The application needs a way to distinguish those conditions without relying on a maintainer’s memory.

Keep evidence without copying the document again

Operational events should help locate a failed upload or processing attempt. They rarely need the complete document contents. Use a job reference, state transition, bounded result category and the information needed to route the issue. Review error handlers because processors may include filenames or extracted content in their error output.

A support workflow should use the application’s normal access path when inspecting a document is authorized and necessary. Avoid making shared logs or chat attachments an alternative document repository. The logging guide offers a concrete way to test what leaves the application through diagnostics.

Before release, ask a reviewer to trace one valid upload and one rejected upload from receipt to cleanup. They should be able to explain where the bytes reside, which state permits processing and which rule permits retrieval. That traceable lifecycle is a stronger basis for operating the feature than a single statement that files are “validated.”