Unrestricted file upload
| Class | Injection |
|---|---|
| Also known as | Unrestricted upload of file with dangerous type; file upload vulnerability |
| CWE | CWE-434: Unrestricted Upload of File with Dangerous Type |
| OWASP Top 10 | A06:2025 Insecure Design |
| Typical severity | Critical when the file can run on the server; Medium to High otherwise |
| Affects | Avatar, attachment and import features; form plugins; any route that writes a request's file to disk or storage |
A profile form sends a picture to the server as one part of a multipart/form-data request. That part carries two values the browser fills in and the sender can change at will: a file name, such as filename="avatar.jpg", and a declared MIME type, such as Content-Type: image/jpeg.1 An unrestricted file upload is an upload feature that trusts those values, or checks nothing, and so stores a file of a type the application never meant to accept. If the file lands in a folder the web server executes, a request for its address runs it.
The CWE entry files the weakness under "dangerous type" and calls the short name imprecise, because it also reads as a missing limit on the size or number of files.2 Both faults turn up on the same routes. An uploaded file can run on the server, replace another file, run as a script in other visitors' browsers, or fill the disk.1
How an upload becomes code
A web server serving a static file reads the extension from the path and looks it up in a table that maps extensions to types. An image is sent back as it is. A type the server is set up to execute, such as .php under Apache with AddType application/x-httpd-php .php, is run first and its output sent instead.1 PHP and ASP files run this way even when the file system gives them no execute permission, because the web server interprets them itself.2
So an avatar route that saves shell.php into /uploads/ under the web root has created a Web shell: every request to /uploads/shell.php runs the attacker's code with the server's permissions. Saving the file starts nothing; the attacker's follow-up request for its address does.1
Checks that fail
| Check | How it is passed |
|---|---|
| Declared Content-Type | The sender writes image/jpeg on a PHP file; the header is input like any other.3 |
| Blocklist of extensions | Other extensions the server also executes, such as .php5, .phtml or .shtml; mixed case such as .pHp.13 |
| "Ends in .jpg" | Names that two programs read differently: shell.php.jpg, shell.asp;.jpg, shell.php%00.jpg, and on Windows shell.asp:.jpg, which NTFS stores as an .asp file.13 |
| Stripping ".php" once | shell.p.phphp becomes shell.php after one pass.1 |
| First bytes match an image | A real JPEG, which starts FF D8 FF, with code hidden in its metadata (a polyglot file).1 |
| Check after saving | The file sits in a served folder for the milliseconds a scanner takes, and a request in that window runs it.1 |
An uploaded .htaccess or web.config skips the extension question entirely: where the server honours per-folder settings, the file can tell it to execute a harmless-looking extension in that folder.3
Other ways an upload does harm
A file name taken from the request decides where the file is written. The same name as an existing file replaces it, and a name with ../ climbs out of the folder, which is Path traversal.1 An uploaded archive that the app unpacks raises the same question for the name of every file inside it (Zip Slip).
An HTML page or an SVG image with a <script> tag, served from the app's own origin, runs in the browser of anyone who opens it: stored Cross-site scripting.1 Browsers can also guess a file's type from its content, called MIME sniffing; the X-Content-Type-Options: nosniff header, one of the HTTP security headers, turns that guessing off, so a file sent as text/plain is not rendered as HTML.4
Files that are parsed after upload expose the parser. XML-based office documents can carry XML external entity (XXE) payloads, and image libraries have had their own flaws; a ZIP bomb (a small archive that unpacks to a far larger size) or one huge file fills the disk, a form of Denial-of-service attack.3 Multer, the upload library maintained by the Express project, sets no file size limit and no file count limit unless the code gives one.5
Contact Form 7, 2020
Contact Form 7, a WordPress form plugin with over five million active installations, cleaned the names of files attached to forms before saving them.6 In version 5.3.1 and older, a form submitter could pass special characters in the name that the cleaning missed, and upload a file that then ran as a script on the host.7 The fix, version 5.3.2 on 17 December 2020, removed control, separator and other special characters from file names.7 The flaw is CVE-2020-35489, scored with no login and no user interaction required.8 The researcher who reported it withheld a proof of concept and had seen no attacks at the time.6
Safer design
- Allow a short list of extensions the feature needs, for example PNG and JPEG for avatars, and reject everything else.1
- Generate the stored name on the server, for example a UUID, and never reuse the sender's name as a path. Multer gives a random name with no extension when no
filenamefunction is set.5 - Store files outside the web root, on another host, or in object storage, and serve them through a route that maps an id to the file.3
- Set a size limit and a file count limit, and measure archives after unpacking.3
- Decode and re-encode images, which drops metadata; this reduces hidden content without removing all of it.3
- Serve user files with
Content-Disposition: attachment, which makes the browser download them, and withnosniff.94 - Accept uploads only from signed-in users, and only on the routes meant for them.3 Multer's own README warns against mounting it globally, because files would then be accepted on routes nobody planned for.5
// Express with multer: one route, random names, size and count limits
const upload = multer({
storage: multer.diskStorage({ destination: "/srv/data/avatars" }), // outside the web root
limits: { fileSize: 2 * 1024 * 1024, files: 1 },
// file.mimetype is the type the request declares, so the sender sets it
fileFilter: (req, file, cb) => cb(null, ["image/png", "image/jpeg"].includes(file.mimetype)),
});
app.post("/avatar", requireLogin, upload.single("avatar"), saveAvatar);In Supabase Storage, a bucket created with allowedMimeTypes and fileSizeLimit rejects uploads outside those settings.10 Whether a bucket should be public at all is covered in Public cloud storage bucket.
await supabase.storage.createBucket("avatars", {
public: false,
allowedMimeTypes: ["image/png", "image/jpeg"],
fileSizeLimit: "2MB",
});Prompt for a coding agent
Find every route or server function in this project that accepts an uploaded file. For each one: allow only the file extensions the feature needs; replace the stored file name with a random id generated on the server and keep the original name only as data; save files outside any folder the web server serves, or in private object storage; set a maximum file size and a maximum number of files; make sure only signed-in users reach the route; and serve stored files with Content-Disposition: attachment and X-Content-Type-Options: nosniff. If the upload library is mounted globally, move it onto the upload routes only. List each file and line you changed, and any upload you were unsure about.
In SecFlash
SecFlash reports uploads saved under the name the browser sent, with no check of their type, as SF-SEC-794 on the Security page. Archives unpacked without checking their entry names show as SF-SEC-741, and a missing X-Content-Type-Options header as SF-SEC-106, both on the same page.
See also
- Web shell
- Path traversal
- Zip Slip
- Cross-site scripting
- Input validation
- HTTP security headers
- Category: Injection
References
- a b c d e f g h i j k l PortSwigger. "File upload vulnerabilities". Web Security Academy.
- a b MITRE (CWE 5.0). "CWE-434: Unrestricted Upload of File with Dangerous Type". Common Weakness Enumeration.
- a b c d e f g h i "File Upload Cheat Sheet". OWASP Cheat Sheet Series.
- a b "X-Content-Type-Options header". MDN Web Docs. Mozilla.
- a b c Express.js project. "Multer" (README). GitHub.
- a b Jinson Varghese Behanan, Astra Security (December 2020). "Unrestricted File Upload in Contact Form 7". jinsonvarghese.com.
- a b Contact Form 7 (17 December 2020). "Contact Form 7 5.3.2". contactform7.com.
- back OSV (published 17 December 2020). "CVE-2020-35489". Open Source Vulnerabilities database.
- back "Content-Disposition header". MDN Web Docs. Mozilla.
- back "Creating buckets": Restricting uploads. Supabase Docs. Supabase.
External links
- File Upload Cheat Sheet, OWASP Cheat Sheet Series