File Upload Security: Block Malicious Uploads
File upload endpoints are the most common way attackers plant web shells. Validate type, size, content and storage location, and uploads stop being your weakest link.
File upload security means controlling what a user can upload so that a supposedly harmless avatar, resume or attachment cannot become a web shell, stored XSS payload or virus distributor. The core rule: never trust the filename, the extension, or the client-declared content type — validate everything server-side and store files where they cannot execute. Done right, uploads become a routine feature; done wrong, they are the single most common initial-access vector we see on compromised sites.
Why Uploads Are the Classic Attack Vector
An uploaded file gives an attacker two things no other input does: persistence and execution. A PHP file renamed to avatar.jpg.php, a .phtml in a writable directory, or an HTML file with script (stored XSS via upload) each hand the attacker long-term control. The filename is fully attacker-controlled, so any validation based on the name alone — including the extension — can be spoofed with tricks like double extensions (shell.php.jpg), null bytes, or case games (.PHP, .PhP).
The Vulnerability Classes to Block
- Remote code execution — an executable file (php, phtml, jsp, aspx, cgi) stored in a web-served directory
- Stored XSS — an HTML/SVG file uploaded and later served inline from your origin executes script in your users' sessions
- Path traversal — a filename like
../../public/index.phpoverwrites a live file - Denial of service — a 10 GB "image" or a decompression bomb (a small zip/pxm that expands to gigabytes)
- Malware hosting — your domain becomes a trusted-looking distributor of malicious downloads
Step 1: Validate by Content, Not Just Extension
Read the actual bytes. For images, re-encode them server-side with GD or Imagick — this strips embedded payloads and metadata tricks in one pass:
$info = getimagesize($tmpPath);
if ($info === false) { abort(422, 'Not a real image'); }
$img = imagecreatefromstring(file_get_contents($tmpPath)); // re-encode
imagejpeg($img, $storagePath, 85); // store only the re-encoded copy
For documents, use a dedicated scanner (ClamAV) and a strict allow-list of types you actually accept. Blocklist approaches fail because new dangerous extensions appear faster than lists update.
Step 2: Generate Your Own Filenames
Discard the user's filename entirely. Store files under a server-generated name — post/2026/10/a7f3c9…jpg — and keep the original name only in the database as display metadata. This kills path traversal, double extensions and null-byte tricks in one move, because attacker-controlled bytes never reach the filesystem path.
Step 3: Store Outside the Web Root, Serve Through a Controller
Uploads must never live in a directory the web server executes or serves directly:
- Store in
storage/app/uploads/(notpublic/) - Serve via a route that reads the file, forces
Content-Typefrom your own mapping, and addsContent-Disposition: attachmentorX-Content-Type-Options: nosniff - For user-facing images, serve from object storage or a separate static domain so an XSS-in-SVG cannot touch your session cookies
If files must be public, disable script execution in that directory (Nginx: location /uploads/ { add_header X-Content-Type-Options nosniff; location ~* \.(php|phtml|phar)$ { deny all; } }).
Step 4: Enforce Size Limits Early and Often
Cap request body size at the server (client_max_body_size 10m;), cap per-file size in the application, and cap total storage per user. For archives, check both compressed and uncompressed sizes before extracting, and refuse ratio bombs (anything above ~100:1 is suspicious).
Step 5: Log, Alert and Age Out
Record every upload (user, hash, size, type verdict) so an incident can be traced. Scan stored files periodically — malware signatures improve after the fact. Delete unclaimed uploads on a schedule; abandoned upload directories are where web shells hide for years.
How 2MNY Tests Upload Endpoints
Our security scan probes upload handling safely: we check whether executable extensions are accepted, whether uploaded files are served inline from the origin, whether directory execution is disabled, and whether path traversal in filenames is rejected — all with harmless probe files. Full methodology in our evidence-verified findings article.
Frequently Asked Questions
Q: Is validating the file extension enough to prevent malicious uploads?
A: No. Extensions are client-controlled metadata — trivially spoofed with double extensions, case variations and null bytes. Validate the actual content (magic bytes, re-encoding), generate your own storage filename, and store outside any executable directory. Extension checks are at most a first filter.
Q: Why is re-encoding images the recommended defense?
A: Re-encoding through GD or Imagick produces a new image containing only pixel data — every embedded payload, EXIF trick or polyglot construction is discarded because it is not pixel data. It also normalizes the format, so a .jpg that was actually a PHP file fails at decode time.
Q: Should uploaded files be served from the main domain?
A: Preferably not. Serve user content from a separate domain or object storage (for example cdn.example.com) with nosniff and no cookies. Then even a successful HTML/SVG upload cannot execute script in your main site's origin — the same-origin policy does the isolation for you.
Q: How do I safely allow zip uploads?
A: Check the uncompressed size and file count before extraction (refuse > 100:1 compression ratios), extract with paths sanitized against traversal, scan the extracted files, and store the result outside the web root. Archive bombs are resource attacks as much as code attacks.