Letting users upload files safely: a checklist for app teams
Review file upload restrictions, validation, storage and access controls before accepting images or documents from users.
- Validate everything
- Allow only needed extensions, validate the file type, and never trust the Content-Type header.
- Store away from the webroot
- OWASP's priority order: a different host, then outside the webroot, then inside with write-only permissions.
- Scanning is extra
- An additional check, not permission to run an arbitrary file. No size-limit figures: the source gives none.
All wording checked against the OWASP File Upload Cheat Sheet linked in Sources.
Profile photos, CVs, documents, videos: file upload is common and risky. OWASP's File Upload Cheat Sheet says the app must be able to fend off bogus and malicious files. We read it in full on 15 November 2026 and not the CWE, vendor or Java code references. We summarise it as a checklist and give no code.
What can go wrong
OWASP lists:
- A file that exploits a bug in the parser or processor (it names ImageTrick and XXE), is used for phishing (a careers form), fills storage (ZIP bombs, XML bombs, huge files), overwrites an existing file, or carries active content such as XSS that harms other users if the file can be retrieved publicly.
- If files are publicly retrievable: exposure of other files, denial of service through many cheap requests with large responses, and you becoming a host for illegal, offensive or dangerous content such as personal or copyrighted data.
OWASP says there is no silver bullet: use several techniques together.
The sheet's summary principles
Allow only needed extensions; apply input validation before checking the extension; validate the file type and do not trust the Content-Type header; replace the filename with an application-generated one; limit filename length and characters; limit file size; allow only authorised users to upload; store files on a different server or at least outside the webroot; serve public files through a handler that maps an ID to a file; scan with antivirus or a sandbox where available; apply Content Disarm and Reconstruct (CDR) for types like PDF and DOCX where applicable; keep libraries configured and updated; protect the upload from CSRF.
Validating the file
- Extensions: check after decoding the filename and watch for known tricks: double extensions (.jpg.php), null bytes, case changes (.pHp), alternative extensions that map to the same interpreter (such as .phtml or .php5), Windows alternate data streams (reject any filename with a colon), and badly tested regular expressions. An allowlist is the strong option, for example one agreed image type, or docx and pdf for CVs. OWASP says a blocklist alone is weak.
- Content-Type: supplied by the user and trivial to spoof. Useful for catching honest mistakes, preferably against an allowlist, but not for security.
- File signature: checking the file's signature against the expected type helps but is common to bypass, so not on its own.
- Filenames: generating a random name (a UUID) is essential; if user filenames are needed, set a maximum length, restrict to a small character set, block leading periods, sequential periods (traversal) and leading hyphens or spaces. OWASP also warns about Windows 8.3 short-name aliases that can bypass a check on the long name, which a server-generated filename avoids.
- Content: for images, decode and re-encode to an allowed format and strip unneeded metadata, noting this does not guarantee all malicious content is gone and the image library itself handles untrusted input, so use an up-to-date library with limited formats, resource limits and sandboxing. ZIP files are not recommended because they can contain anything. Let users report illegal content and let copyright owners report abuse. If you have the resources, review files manually in a sandbox before public release, and be careful about data leakage when using public scanning services such as VirusTotal.
Double extensions
.jpg.php
Null bytes
Decode the filename before checking the extension.
Case changes
.pHp
Alternative extensions
.phtml, .php5 - same interpreter, different name.
Alternate data streams
Reject any filename with a colon.
Bad regexes
Badly tested regular expressions miss tricks. An allowlist is the strong option.
Where to store files, in order of OWASP's priority
- On a different host.
- Outside the webroot with administrative access only.
- Inside the webroot with write-only permissions and proper read controls. Storing files in a database is possible but OWASP advises it only when a DBA is on the team and it proves an improvement, since it brings performance, backup and SQL injection concerns. Also, attackers may upload server configuration files (such as .htaccess or web.config); as a second layer, disable per-directory configuration overrides on upload directories.
- On a different host.
- Outside the webroot, with administrative access only.
- Inside the webroot with write-only permissions and proper read controls.
- In a database - possible, but OWASP advises it only when a DBA is on the team and it proves an improvement.
Who and how much
- Require authentication so you can set limits, and authorisation so users can reach only their own files.
- Use least-privilege filesystem permissions and do not execute user uploads as application code. Scanning is an additional check, not permission to run an arbitrary file.
- Set upload size limits; if files are extracted, enforce resource and expanded-size limits during extraction, not only after decompression has exhausted resources. Use safe extraction paths and a reviewed archive-processing library. Set request limits on downloads to protect against denial of service.
Questions for your team
- Which exact file types does the business need, and nothing else?
- Is the stored filename ours or the user's?
- Where do files live, and can the web server run anything from that location?
- What limits exist on size, count and download rate?
- Do we scan, and what do we do about illegal content reports?
Related: input validation basics, CSRF prevention basics, API rate limiting and resource limits, XSS prevention.
Limits
One OWASP page, read in full. Referenced articles (CWE-434, Microsoft, Java code) unread. No figures for size limits are given by the source, and we give none. Legal duties about hosted content depend on your place and product; ask counsel.
By Omnistu.