How to Design Safer File Uploads

An upload feature accepts, stores, and often processes files before anyone can use them. Start by defining what the workflow needs to accept and who may see the result. Apply the resulting rules from intake through deletion, including background processing and retrieval.

A file's path to retrievalApply release rules to thumbnails and exports as well as original files.
  1. Receive privately

    Authorize the upload and enforce the intake size limit.

  2. Validate content

    Use a maintained parser and enforce format and processing bounds.

  3. Process in scope

    Give the worker only the permissions needed to create the derivative.

  4. Approve a version

    Record the exact object or derivative that satisfied required checks.

  5. Authorize retrieval

    Check the record permission before issuing private content or a link.

Limit what the feature accepts

A profile photo feature may need only JPEG and PNG, a maximum upload size, and bounds on image dimensions. Enforce those rules on the server and check permission to upload to the intended record. The browser's file picker can guide users toward the expected formats. Treat the filename, extension, and declared media type as client input. Check that the actual content matches a permitted format using a maintained parser or validation library.

Set limits at the intake layer and during processing. For images, bound decoded dimensions and processing resources. For archives that are necessary to the feature, cap expanded size and the number of entries. Decide which errors to show the uploader and keep files awaiting a required scan or other check unavailable for ordinary retrieval.

Keep processing and storage scoped

Give each upload a server-generated storage identifier and keep any original display name separate. Store files in an approved private location outside the public web root or in object storage with public access disabled for the private area. Give services only the permissions their work requires. For example, an upload handler can write a pending object, while a separate worker reads it and writes an approved derivative.

Track pending, approved, and rejected states in the application. For approved images, a constrained worker can decode and re-encode the file and remove metadata the feature does not need. Treat parsing and processing as operations on untrusted input. Bind approval to the specific object version or derivative that passed verification. Apply the documented cleanup rules to originals, derivatives, and rejected objects.

Check permission at retrieval time

Define which files stay private and which can be published through an approved workflow. Before serving private content or issuing a temporary download link, verify the caller's access to the record. Apply the same policy to thumbnails, previews, generated copies, and exports. If the application issues temporary links, define their scope, lifetime, and expected behavior after access is removed. Check that pending files are unavailable through each delivery path.

What the file state permitsTest storage, previews, and links for each state the application supports.
  • Pending

    Required checks are incomplete. Ordinary retrieval remains unavailable.

  • Approved

    The approved version is eligible for retrieval under its access policy.

  • Rejected

    Retrieval stays closed and the cleanup schedule applies.

Verify the workflow before release

Use synthetic files and test accounts in an approved environment. Record the expected result, the enforcing component, and the observed outcome for each path.

Check Expected result
Submit a permitted image within the agreed limits. The intended version becomes available to the authorized user after the required processing.
Submit an oversized file or an unsupported synthetic format. The applicable intake or processing limit rejects it, and the object remains unavailable for retrieval.
Simulate a required scanner being unavailable. The file stays pending or is rejected according to policy, including in previews and direct download flows.
Request another test account's private file and preview. Retrieval of both requires permission and the unauthorized account receives a denial.
Let a disposable test file pass its retention deadline. The workflow handles the original and generated copies according to the documented cleanup policy.

Repeat the applicable checks after changing storage, parser settings, or the upload path. If uploads go directly to private storage, verify that the same validation and release rules apply before the application serves the object.

Review common web application security risks ยท Explore scoped application security reviews

References