If an attacker can place a file of their choosing somewhere your server will execute it, the application is over. That is why upload features attract so much attention during a test, and why the usual defences deserve scrutiny.
Why the common checks fail
Extension deny-lists. Blocking .php invites .php5, .phtml, .phar, and whatever else the handler configuration happens to execute. You are now maintaining a list against a target you do not control.
Content-Type header. Supplied by the client. Changing it is one line in an intercepting proxy.
Magic byte checks alone. Better, and still bypassable: prepend valid image header bytes to a script and many checks pass while the server still executes the remainder.
Checking only the last extension. Depending on server configuration, shell.php.jpg or shell.jpg.php may still be handled by the script interpreter.
Every one of these tries to decide whether content is dangerous. The reliable approach removes the server's ability to execute it at all.
The defence that holds
Store uploads outside the web root. If the file cannot be requested by URL, executing it requires a second vulnerability. Serve files back through a script that reads from storage and sets headers, rather than by direct path.
Better still, put them in object storage on a separate domain with no application runtime attached.
Never use the client-supplied filename. Generate a new identifier, keep the original name only as a display label in the database. This removes path traversal, null-byte tricks and encoding games in one step.
Use an allow-list of extensions, validated against the final extension, combined with content inspection.
Re-encode images. Process every uploaded image through an imaging library and save the output. Appended payloads do not survive the round trip and metadata that may carry injection attempts is stripped.
Disable execution in the upload directory at the web server level, as defence in depth for when something else is misconfigured.
The attacks that are not webshells
Treating uploads purely as a remote code execution risk misses several practical issues.
Stored cross-site scripting via SVG. SVG is XML and can contain script. Served inline from your domain, an uploaded SVG executes in your origin. Either reject SVG, sanitise it properly, or serve it with headers that prevent inline rendering.
XXE through document formats. Office documents and SVGs are parsed XML. If your parser resolves external entities, an upload becomes server-side file disclosure.
Path traversal through the filename. A filename of ../../config/app.php overwrites application files where the name is used unmodified.
Denial of service. Decompression bombs, enormous images that exhaust memory on resize, and unlimited file sizes filling the disk.
Malware distribution. Your domain hosting an attacker's payload damages reputation and can get the site blocked, even though the application itself was never compromised.
Access control on retrieval
Frequently forgotten. Uploads commonly land at predictable or sequential paths with no authorisation check on download. Anyone who can guess a path reads other customers' documents, and the only thing protecting them is obscurity.
Authorise every retrieval against the requesting user, exactly as you would any other record.
A practical checklist
- Stored outside the web root or on a separate storage domain.
- Generated filename; original kept only as a label.
- Allow-list of extensions plus content inspection.
- Images re-encoded, SVG handled deliberately.
- Size limits enforced server-side.
- Execution disabled in the storage location.
- Authorisation enforced on download, not just upload.
- Files scanned where the content will be shared with other users.
0 comments
Leave a comment