File uploads: check what customers really send you
Short answer: Never trust the file name or the type the browser reports when someone uploads a file to your site or app. Both can be faked. Decide which file types you actually need, check the extension against that allowlist, read the first bytes of the file to confirm its real format, give it a new random name, store it away from your web root, and serve it back with the correct Content-Type plus the X-Content-Type-Options: nosniff header. For images, re-encoding them removes a whole class of tricks.
Why upload forms are a favourite target
Plenty of business sites accept files: CVs on a careers page, photos of damage on a repair request, proof of purchase for a warranty claim, artwork for a custom order. Each upload is a file written by a stranger onto your system.
If your checks are weak, an attacker can upload a script disguised as an image and, if the server runs it or a browser renders it, take over pages or the server itself. Even without an attacker, staff can be caught out by a "PDF" from a customer that is really something else.
OWASP's File Upload Cheat Sheet, a widely used security reference, is blunt about the starting point: validate the file type and do not trust the Content-Type header, because it can be spoofed.
Layer one: an allowlist of extensions
Start by deciding what your form genuinely needs. A careers page might need PDF and DOCX; a photo upload needs JPEG, PNG and perhaps HEIC from iPhones. Everything else is rejected.
OWASP recommends an allowlist approach and lists bypasses a weak filter falls for:
- Double extensions such as invoice.jpg.php, which slip past a check that only looks for ".jpg" somewhere in the name.
- Null bytes such as file.php%00.jpg, where some systems truncate the name at the null and keep ".php".
- Case tricks such as .pHp, which defeat case-sensitive comparisons.
Check the final extension only, after lowercasing, against your list.
Layer two: check the file's signature
Most file formats begin with a fixed sequence of bytes, often called a signature or "magic bytes". A PNG starts with a specific eight-byte signature defined in the PNG specification; a PDF begins with the characters "%PDF". OWASP's cheat sheet recommends file signature validation in conjunction with content-type validation.
In practice you use a well-maintained library for your language that reads the first bytes and reports the detected type, then compare it with the extension. If someone uploads "photo.jpg" whose bytes say it is an executable or HTML document, reject it. Signature checks are not perfect, since a file can be valid in two formats at once, which is why the next layers matter.
Layer three: rename and store safely
OWASP advises that the application should generate the file name, for example as a UUID, rather than keeping the uploaded one. Original names can contain path characters, scripts or simply clash with other files. If you need the original name for display, store it as data in your database, after validation.
The cheat sheet also recommends storing uploads on a different server, or at least outside the web root, and serving public files through a handler that maps an ID to the stored file. That way an uploaded file can never be requested directly as a script on your main domain. Object storage, with files served from a separate domain, achieves the same separation.
Other items on OWASP's list worth copying:
- Set a file size limit and a file name length limit.
- Only allow authorised users to upload when the feature allows it.
- Run files through antivirus or a sandbox where available.
- For document types such as PDF and DOCX, consider Content Disarm and Reconstruct, which rebuilds files without active content.
- Protect the upload form against cross-site request forgery.
Layer four: re-encode images
For images, a strong extra step is to decode the upload and save a fresh copy, for example resizing it to the sizes your site uses. The new file contains only pixel data your image library wrote, so hidden scripts or odd metadata are dropped. It also strips location data from customer photos and gives you consistent dimensions. Reject the upload if the library cannot decode it.
Layer five: serve files back safely
When files are downloaded or displayed, the browser decides how to treat them partly from the Content-Type header. Set it from your validated type, not from what the uploader claimed. For anything that should be downloaded rather than shown, use a Content-Disposition header of "attachment".
Add X-Content-Type-Options: nosniff. MDN explains that this header tells browsers to follow the declared MIME type rather than guessing, and that for script and style requests the browser blocks the response if the type does not match what is expected. Without it, a browser may "sniff" an uploaded file, decide it looks like HTML and render it on your domain.
A worked example
Consider a hypothetical Brisbane auto-glass repairer whose quote form let customers attach photos of chipped windscreens. The original form only checked that the file name contained ".jpg" and saved uploads in a public folder under their original names.
The rebuilt version accepts JPEG, PNG and HEIC only, checks the final extension and the file signature, rejects anything over 15 MB, renames each file to a random ID, stores it in private object storage and re-encodes every image to a standard size. Staff view the photos through a page that requires login, served with the correct type and nosniff. Customers notice no difference, except that the upload is faster because photos are resized.
FAQ
Is checking the extension in the browser enough?
No. Browser checks improve the experience, but anyone can bypass them. All validation must also happen on the server.
Should I accept ZIP files?
Only if you truly need them. Archives can hide many files and expand to huge sizes, so they need extra checks.
Does a CDN or form plugin handle this for me?
Some do part of it. Check what they validate, where files are stored and which headers they are served with.
If your quote, job or careers forms accept files and you are not sure how safely, tell us what customers upload.