Skip to content

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-Type part header and the .png extension 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\n at 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 in Content-Disposition headers 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:

  1. Content-Disposition: attachment — the browser downloads instead of rendering. Kills inline-executed HTML/SVG dead.
  2. X-Content-Type-Options: nosniff — stops the browser second-guessing your Content-Type and rendering "text/plain" as HTML.
  3. A separate download hostname — dl.example.com, cookie-free — so even active content that executes can't reach your app origin's credentials.
  4. 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-Type or 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?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.