Lesson 8 of 14 · 20m
File Uploads: Magic Bytes, Polyglots, and Serving Back Safely
Content-Type is a claim the client makes — sniffing, storage isolation, and presigned uploads that keep bytes off your API.
Content-Type: image/png in a multipart upload is a claim the client makes, not a fact about the bytes. The client also picked the filename, and your stack will parse multipart/form-data — a format with boundary delimiters, per-part headers, and decades of parser edge cases — before your handler even runs. File upload is the one endpoint where you should assume every byte is adversarial, because it is.
Trust nothing the client declares
- Sniff, don't believe. The
Content-Typepart header and the.pngextension are both attacker input. Check the magic bytes instead —libmagic/file --mime-type, or a library that parses the format header properly.\x89PNG\r\n\x1a\nat offset 0 is a stronger statement than any header. - Polyglots are real files. A file that's a valid GIF and a valid JAR (the GIFAR trick, demonstrated back in 2008) or a valid image with HTML appended will pass naive sniffing and still render as active content in the wrong context. Polyglots are why "sniffed OK" is necessary but not sufficient — the next section is what finishes the job.
- Filenames are hostile strings.
../../etc/cron.d/x,a.png\0.jpg, and filenames containing CRLF end up inContent-Dispositionheaders and filesystem paths. Strip to a safe charset, or better — never use the client's name server-side at all; store under your own generated key and keep the original name as display metadata.
The delivery side is the other half
Where the bytes get served from matters as much as what they are. Serving user uploads from your app domain means an uploaded SVG — which is XML that executes JavaScript — is a stored XSS payload on your own origin. The mitigations, in order of effort:
Content-Disposition: attachment— the browser downloads instead of rendering. Kills inline-executed HTML/SVG dead.X-Content-Type-Options: nosniff— stops the browser second-guessing yourContent-Typeand rendering "text/plain" as HTML.- A separate download hostname —
dl.example.com, cookie-free — so even active content that executes can't reach your app origin's credentials. - A sandbox CSP on the download path for formats you must render inline.
Move the bytes off your API
Receiving multi-megabyte uploads through your application server is an availability problem you built yourself — memory pressure, slow-client ties, proxy body-size limits to negotiate. The operationally correct pattern is a presigned URL: your API returns a short-lived S3 PutObject presign, the client uploads directly to object storage, and a callback or event notifies you when it lands. Your API servers never touch the bytes; your authorization check happens at URL-mint time where it belongs.
Scan and size-limit regardless of the path. Cap bytes at the transport layer (your proxy's client_max_body_size and your handler both) and enforce it at presign time — a presigned URL without a size cap is just outsourced upload with no limit at all. Decompression bombs — the 42 KB zip that inflates to petabytes — are the argument for scanning limits on anything you'll unzip or image-process later; ImageTragick (CVE-2016-3714) made the same point about image pipelines: a "processing" library is itself attack surface, so run it locked down, in a container with no network and a memory ceiling, or delegate to sharp/libvips under the same constraints. ClamAV for malware scanning is table stakes for anything you'll later serve to other users.
Try this
Design avatar upload for your API: the presign flow, the size cap, the sniffing step, where it gets stored, how it's served back. Then write the exploit: the file you'd send to break each layer you just designed. If your answer is "the library handles it," name the library and its last CVE.
Pitfalls
- Trusting
Content-Typeor extension — sniff magic bytes; treat both as attacker input. - Serving uploads from the app origin — SVG-as-XSS; separate hostname,
attachment,nosniff. - Client filenames in
Content-Disposition— CRLF injection and path traversal in one string. - Uncapped presigned uploads — the cap moves to the presign or it doesn't exist.
- Unbounded decode/decompress — bombs live downstream of the parser, not in it.
Was this lesson helpful?
Keep reading
Related articles from the SudoSecurity blog.
Building a Production REST API With Node.js and Express
Build a REST API in Node.js and Express the way production systems need: input validation, JWT auth, refresh token rotation, status codes, rate limiting, idempotency keys, and centralized error handling.
GraphQL Over HTTP Is Finally a Spec. Your Server Probably Still Says 200.
The GraphQL over HTTP specification shipped its first edition on November 12, ending the decade of '200 Not OK.' Validation errors get 422, partial results get a new 294 status, and a dedicated media type tells clients the body is safe to parse.
Strings Are Lying to You: Unicode Length, Case and Normalization in PHP, Python and JavaScript
One family emoji is 18 bytes, 8 JavaScript code units, 5 Python characters and 1 thing a human sees. I ran the same Unicode tests through PHP 8.6, Python 3.15 and Node 26: each gave a different answer for length, case and truncation, a KELVIN SIGN lowercases into a real username, and PHP 8.6's...
PHP and WordPress Patched a Day Apart. Your Front End Decides Which PHP Bugs Reach You.
PHP 8.5.13, 8.4.28, 8.3.37 and 8.2.36 fix 18 CVEs, seven rated 9.2, and WordPress 7.1.5 fixes 20 more issues. Most of the PHP bugs only fire under specific settings, so I checked the official Docker images for each condition. The images were patched the same day, and the patch quietly turns off...