File upload vulnerability: when a form accepts more than a picture
A file upload vulnerability is a weakness in how an application receives, checks, stores or serves files that users send. The textbook case is unrestricted file upload: the application takes a file of a dangerous type and then processes it, so an attacker can place a script, a crafted document or a hostile page on your server. It is one of the web attacks where the damage depends almost entirely on what happens after the upload request is accepted.
Quick facts
| Item | Detail |
|---|---|
| Also called | Unrestricted file upload, dangerous file upload |
| Weakness | CWE-434: Unrestricted Upload of File with Dangerous Type, listed under the alternate term “Unrestricted File Upload” |
| Main guidance | OWASP File Upload Cheat Sheet |
| Typical outcomes | A script that runs on the server, an overwritten file, a stored page that attacks visitors, an exhausted disk, a parser exploited by a crafted file |
| Where the fix lives | In the application and on the server: allow-list, generated names, storage outside the webroot, size limits |
| Does a WAF stop it? | Partly. It sees the upload request, not what the application does with the file afterwards |
What the weakness is
The MITRE entry for CWE-434 describes the problem as a product that allows the upload or transfer of file types that it then automatically processes. Two parts matter. The type is dangerous for the place it lands, and the system acts on the file without a person deciding to. A server that stores a PDF in a folder nobody can reach has not made this mistake. A server that stores a script in a folder it executes has.
“Unrestricted” is a little misleading. Most vulnerable forms do have a check; it is just one that can be walked around. A form that only inspects the file name in the browser, or that trusts the declared type, is unrestricted in the sense that counts.
MITRE maps the weakness to OWASP Top 10 2025 category A06, insecure design. That fits: the missing piece is usually a decision about what an upload may become, not a coding slip.
What can go wrong
The OWASP File Upload Cheat Sheet lists the harms a careless upload feature invites. They fall into a few groups.
- Code on the server. A file with an extension the server treats as executable, such as an alternative PHP extension, or a server configuration file such as
.htaccessorweb.config, can change how the server behaves. A script left in a reachable folder becomes a web shell, and from there the attacker has what command injection or remote code execution would have given. - Attacks through the parser. A crafted file can exploit the library that reads it. OWASP names ImageTragick and XXE as examples.
- Overwriting. If the stored name comes from the user, an upload can replace an existing file on the system.
- Harm to other users. When uploaded files are publicly retrievable, active content in them can reach visitors as XSS or CSRF. Public retrieval can also turn your domain into a host for illegal, offensive or copyrighted material.
- Exhausted resources. ZIP bombs, XML bombs and plain oversized files fill storage and hurt availability. Many requests for large stored files can do the same to bandwidth.
- Phishing. A careers form that accepts attachments can carry hostile documents to staff.
Why simple checks fail
Nearly everything in an upload request is chosen by the sender. The multipart format that browsers use for file forms is defined in RFC 7578. It says a part may carry a file name and a content type, and that a part without a content type defaults to text/plain. It also tells receivers to check the file name, change it to fit local conventions if needed, and discard any directory path information. None of that is a promise that the values are honest. Exported files raise a related problem: CSV injection. An XML upload can also exhaust the parser: XML bomb.
The file name and extension
Extension checks go wrong in familiar ways. The cheat sheet warns about double extensions, null bytes, case changes, alternative extensions that servers still execute, and NTFS alternate data streams, which is why it advises rejecting names that contain a colon and validating after the name has been decoded. MITRE adds three rules of thumb: allow only one extension in a file name, compare extensions case-insensitively on servers whose file names are, and prefer a very small allow-list over a list of forbidden types. A block-list of bad extensions loses to the first one nobody thought of; OWASP says block-lists are weak on their own.
The declared type and the content
The Content-Type header comes from the client and can be spoofed. Checking the first bytes of the file, its signature, is better, but OWASP says to use it alongside other checks and never alone, and MITRE notes that content checks by themselves do not settle things because content can hide executable code. No single test proves a file harmless, so storage and serving decisions carry as much weight as validation.
How to prevent it
The controls below come from the OWASP File Upload Cheat Sheet and from the mitigations listed under CWE-434. Together they aim at one outcome: nothing a user uploads can be run, and nothing is stored under a name the user chose.
- Allow-list extensions. Accept only the types the feature needs for its business purpose, and repeat every client-side check on the server.
- Check type and signature together. Do not trust the Content-Type header. Compare it with the file signature, and do not rely on either alone.
- Generate the stored name. Use a random identifier such as a UUID. If the original name must be kept, keep it as data and limit its length and characters.
- Store outside the webroot, or elsewhere. OWASP prefers a different host. Failing that, a location outside the webroot. If files must live inside it, give write-only permissions and put access control on reads. Serve through a handler that maps an ID to the stored file.
- Limit size. Set upload size limits, and measure after decompression for archives.
- Require an identity. Allow uploads only from authenticated, authorised users.
- Scan the content. Run an antivirus or sandbox where available. For document types such as PDF and DOCX, OWASP also describes content disarm and reconstruction.
- Give the folder least privilege. Per-directory configuration overrides should be off there (Apache
AllowOverride Noneis OWASP’s example), and the process that writes files should not be able to run them.
Images and archives
For images, OWASP suggests decoding and re-encoding into an allowed format, which also strips metadata. It adds that this does not guarantee all malicious content is gone, so the imaging library should be current, limited to the formats you need, and run with resource limits and sandboxing. For archives the advice is blunter: OWASP does not recommend accepting ZIP files because of the number of attack vectors, and if you do extract them, the sizes must be computed safely.
A separate host for user files
OWASP prefers storing uploads on a different host. Read alongside its warning about active content in publicly retrievable files, the benefit is twofold: executable content stays away from the server that runs your code, and a hostile page uploaded by one user does not run on the origin your other users are signed in to. The second half is our reading of the advice, not OWASP’s wording.
Where a WAF helps and where it does not
A web application firewall reads HTTP requests, and a file upload is an HTTP request with a body in a known shape. That gives it real but narrow work: it can look at the envelope of an upload, and it has no view of what happens to the file once the application takes it.
What request-side filtering can do
The open-source OWASP Core Rule Set shows what these checks look like in practice. It is a public rule set, not a statement about the rule list in any particular product, but its documented rules are a fair picture of the category.
- Size. CRS has rules for an uploaded file that is too large (920400) and for the total size of all files in a request (920410). The limits come from settings,
max_file_sizeandcombined_file_sizes, and the example configuration ships them commented out, so the default is unlimited until someone sets a value. - Content types. Rule 920420 refuses a request whose Content-Type is not on an allow-list. The default list includes multipart/form-data, so an ordinary upload form passes; the check matters for types you never intended to accept. The configuration file also warns that allowing types that do not activate a body processor can open bypasses.
- Script file names. Rule 933110 inspects uploaded file names for PHP script extensions. Rule 933111 is a stricter variant that matches a PHP extension followed by another dot anywhere in the name, which is the double-extension case; its own comment gives
index.php.tmpas the example. The name is checked as sent, so it only helps against names that announce themselves. - Malformed multipart bodies. Rules 920120 and 920121 flag attempted multipart/form-data bypasses in file names and file content.
One mix-up is worth avoiding. The CRS setting restricted_extensions (rule 920440) looks at the extension of the requested URL to guard against exposing development and configuration files. It does not look at the name of a file being uploaded.
The Bridge WAF modes apply to whatever such a rule decides. In monitor mode a match is recorded and the upload still goes through; in block mode the request is refused; off inspects nothing. Upload endpoints are the place to start in monitor, because a legitimate large file or an unusual document type can trip a size or type check, and the activity log will show which uploads would have been refused. Real users caught that way are handled as false positives, with a narrow exception rather than a loosened rule.
What it cannot do
A WAF does not know where the application writes the file, so it cannot tell a script saved in a safe folder from one saved beside your pages. It does not know whether the name is regenerated. It cannot decide that this user may not replace that document, which is an authorisation question for the application, much like IDOR. A file can be a valid image with a harmful payload for a vulnerable library, and no header says so. When an application accepts a file as text inside a JSON body, there is no file part for a multipart rule to inspect. And a file already on disk, through an earlier flaw, is invisible to it.
So the WAF is an outer layer: it refuses the obvious, limits the size, and leaves a record. The decisions in the previous section stay with your application and server.
Reviewing your own upload feature
Ask five things of each upload form, and write the answers down. Which extensions does the server accept, and is that enforced on the server or only in the browser? Who chose the stored name? Is the storage location reachable by URL, and does the server execute anything there? What is the largest file, and what happens when an archive is unpacked? Can an anonymous visitor upload, and can one user overwrite another’s file?
If the answers point to a script already sitting in an upload folder, treat it as an incident and follow the steps on the web shell page. Uploads that reach files outside the intended folder through the file name are a different flaw, covered under path traversal. For the wider catalogue of weaknesses, see the OWASP Top 10.
Questions
What is a file upload vulnerability?
A file upload vulnerability is a flaw in how an application accepts, checks, stores or serves uploaded files. When the checks are weak, an attacker can store a file the server then runs, parses or hands to other users, which can lead to a web shell, a stored page that attacks visitors, or a full server takeover.
What is unrestricted file upload?
Unrestricted file upload is another name for CWE-434, Unrestricted Upload of File with Dangerous Type. The weakness exists when a product accepts a file type that it then processes automatically, without limiting which types may be uploaded.
Can a WAF stop file upload attacks?
Partly. A WAF can cap the size of an upload, refuse request content types and file names you do not accept, and record the attempt. It cannot see where the application stores the file, whether it renames it, or what a later image or archive library does with the content.
Is checking the file extension enough?
No. Use an allow-list of extensions as the first check, but not the only one. The Content-Type header and the file name both come from the client, and OWASP advises checking the file itself, generating the stored name, and keeping uploads out of a location the server executes.
Where should uploaded files be stored?
On a different host if you can, otherwise outside the webroot, with the application serving them through a handler that maps an ID to the stored file. That way a file the server would execute is never reachable at a URL of its own.