Design a safe file-type detector
Design the component that decides what an uploaded file really is: magic-byte signatures at fixed offsets, refusal of ambiguous and polyglot files, markup sniffing so browsers cannot reinterpret an image as HTML, structural end checks, and a verdict the rest of the upload pipeline can trust.
01Understand the problem
- Identify file types from content using signatures at fixed offsets
- Detect polyglots and files that a browser would sniff as HTML
- Return one unambiguous verdict or refuse
- Serve the result so browsers cannot reinterpret it (
nosniff,Content-Disposition, separate origin)
What they probe: why the extension and the client's Content-Type are worthless as evidence, what a magic number is and where it sits, what a polyglot is (GIFAR, PDF plus zip, JPEG with HTML in a comment), what browsers do with a file served without a type, and why SVG is not an image for security purposes. Where candidates slip: trusting file.type from the browser, checking signatures anywhere in the file, accepting SVG as an image, and serving uploads from the main origin.
The name and Content-Type of an upload are written by the client, so they are claims, not facts. The bytes are the only evidence. Most binary formats begin with a magic number: PNG with 89 50 4E 47 0D 0A 1A 0A, PDF with %PDF-, zip with PK\x03\x04. A detector reads the first few kilobytes and matches signatures at fixed offsets. The hard part is what to do when a file matches two things, or looks like a valid image and also like HTML.
detect(head, tail)returns exactly one kind, orunknownorambiguous.- Signatures are anchored at their specified offset, never searched anywhere in the file.
- Any file whose first kilobyte contains HTML-like markup is flagged, whatever its signature.
- Formats with a defined end marker (PNG IEND) are checked for trailing data.
- The policy layer, not the detector, decides which kinds are allowed for a feature.
| Kind | Signature | Offset |
|---|---|---|
| PNG | 89 50 4E 47 0D 0A 1A 0A | 0 |
| JPEG | FF D8 FF | 0 |
| GIF | GIF87a or GIF89a | 0 |
| WebP | RIFF then WEBP | 0 and 8 |
%PDF- | 0 (readers tolerate it later in the first 1024 bytes; we do not) | |
| ZIP (also DOCX, XLSX, JAR, APK) | 50 4B 03 04 | 0 |
| MP4 / ISO BMFF | ftyp | 4 |
This is an advanced module
Problems: parsing and sandboxing components is part of the Pro pass. 4 more sections — the deep dives, the final design, follow-ups and the test — unlock with it.
Already a cyart.io or studio.cyart.io member? Your pass code is in your Studio account.