Pattern: File upload
Note for AI agents: This page is baseline guidance, not an exact prescription. Read it, tell your owner what applies to their project, and get their approval before you change anything. Your project's own rules take precedence. If something important for this project is missing here, suggest it to your owner.
For agents
- Offer as a suggestion. Limits are examples.
- Core ideas: check who is uploading and whether they may, and limit the rate, before accepting a single byte; send large files straight to storage with a short-lived signed address; decide the type from the bytes; clean up files that never got attached.
- Related: server layers, photo and video ratios, rate limits.
1. Problem
Uploads let strangers put data on your disk. Typical failures: writing the file before the permission check, trusting the Content-Type header or file name, accepting any size, running the server out of disk, and leaving abandoned files behind.
2. Options
| Method | Upside | Downside |
|---|---|---|
| Upload through your server | Full control; simple for small files | Server carries all bandwidth and memory; slow for large files |
| Direct upload to storage with a signed, time-limited address (recommended for large files) | Server never carries the bytes; scales | More moving parts; validation after the upload |
| Multipart upload for very large files | Resumable, parallel | Cleanup of unfinished parts needed |
The OWASP File Upload Cheat Sheet is the main reference; the AWS S3 documentation covers presigned URLs and multipart uploads (similar features exist in other object stores).
3. Recommended design
- Order for upload endpoints: identity → permission → rate limit → receive the file. An unauthorized request writes nothing to disk. OWASP: allow only authorized users to upload.
- Direct upload: the app asks your API for an upload permit; the API checks identity, permission and limits, then returns a signed address valid for a short time (example: 10 minutes) for one object key chosen by the server. The client uploads straight to storage, then tells the API it finished; the API validates.
- Type from the bytes: validate the file type from its signature, do not trust the
Content-Typeheader (it can be spoofed) or the extension (OWASP). Allow only the types the feature needs. - Size by type: a limit per type (example: images 10 MB, video limit derived from duration). OWASP: set a file size limit; also a file name length limit.
- Names: generate the storage name yourself; never use the user’s file name as a path.
- Images: decode and re-encode to an allowed format, removing metadata, as OWASP describes. Rewriting does not make an untrusted file safe by itself; keep image libraries updated and sandboxed where possible.
- Video: validate container and duration without re-encoding on the upload server (keeps CPU cost low; practitioner experience), process in a separate worker.
- Storage location: store uploaded files outside the web root or on a separate storage service (OWASP).
- Orphan scan: a scheduled job finds files with no database reference older than a short time (example: 24 hours) and removes them. Clean up unfinished multipart uploads too.
- Deletion errors are not swallowed: if removing a file fails, record it and retry; do not report success.
- Log upload events with ids and sizes, not content.
4. Scenarios and edge cases
- File renamed to
.jpgbut is an executable: rejected by signature check. - Client lies about size: the signed permit carries a size limit; the API re-checks after upload.
- Upload never completed: the unfinished object is removed by the orphan job.
- Duplicate upload after a network drop: the same permit key is reused or the API ignores a second completion (see retries).
- Very large video on mobile data: warn and allow resume.
5. Common mistakes
- Saving to disk before checking permission.
- Trusting
Content-Typeor the extension. - Using the user-supplied file name in a path.
- No size limit or one limit for everything.
- Public write access on the storage bucket.
- Silent failures when deleting files.
6. How to audit
- What: unauthorized uploads write nothing.
- How to check: send an upload with no token and with an invalid token; list storage.
- Evidence: 401/403 and an unchanged listing.
- What: the type check uses the bytes.
- How to check: upload a text file renamed to
.png. - Evidence: rejection.
- How to check: upload a text file renamed to
- What: size limits are enforced.
- How to check: upload a file over the limit.
- Evidence: rejection at the stated limit.
- What: orphans are removed.
- How to check: request a permit, never upload or complete; wait for the job.
- Evidence: object absent after the job.
Sources
- File Upload Cheat Sheet — OWASP Cheat Sheet Series
- S3: presigned URLs — AWS
- S3: multipart upload — AWS
- Application Security Verification Standard (ASVS) — OWASP
- Logging Cheat Sheet — OWASP Cheat Sheet Series