Common MIME type mistakes in file uploads, HTTP headers and magic-byte sniffers
Why trusting req.file.mimetype is a security vulnerability, why Windows reports application/vnd.ms-excel for CSV files, and how to fix 10 real-world MIME type bugs.
1. Mistake #1: Trusting the browser's multipart Content-Type header
When a browser submits a multipart/form-data file upload, each part includes a header such as Content-Type: image/jpeg. Backend developers using Express/Multer (req.file.mimetype), FastAPI (UploadFile.content_type), or Spring Boot (MultipartFile.getContentType()) frequently assume the browser inspected the file's binary contents to produce that string. It did not: the browser simply mapped the filename extension (.jpg) using the client operating system's file-association table.
This creates two production failures:
- False rejections for legitimate users: On a Windows workstation where Microsoft Excel is installed as the default handler for
.csvfiles, the Windows Registry often maps.csvtoapplication/vnd.ms-excelinstead of RFC 4180'stext/csv. An upload endpoint that strictly checksfile.mimetype === 'text/csv'rejects valid CSV uploads from Windows Excel users. - Security bypasses for malicious uploads: Any client can send
Content-Type: image/pngwhile uploading HTML/JavaScript or an executable binary. Always validate the actual file bytes using magic-byte signatures (testable with our wrong-extension-png-as-jpg.jpg and wrong-mime-html-in-csv.csv fixtures).
2. Ten frequent IANA vs. real-world MIME type discrepancies
| Extension | Official IANA MIME type | Non-standard / Legacy variants seen in the wild | Recommended backend handling |
|---|---|---|---|
| .csv | text/csv | application/vnd.ms-excel, text/plain, application/csv | Accept UTF-8 text + verify delimiter structure |
| .json | application/json | text/json (unregistered), text/plain | Send application/json; never send text/json |
| .js | text/javascript | application/javascript (obsoleted by RFC 9239) | Use text/javascript per IETF RFC 9239 |
| .mp3 | audio/mpeg | audio/mp3, audio/x-mp3 | Serve as audio/mpeg; accept audio/mp3 on upload |
| .wav | audio/vnd.wave | audio/wav, audio/x-wav, audio/wave | Check RIFF....WAVE bytes; serve as audio/wav |
| .aac | audio/aac | audio/x-aac, audio/mp4 (confused with .m4a) | Check FF F1 ADTS header vs. ftypM4A |
| .xml | application/xml | text/xml (RFC 7303 makes both aliases) | Both are valid under RFC 7303 |
| .yaml | application/yaml | text/yaml, application/x-yaml (pre-RFC 9512) | RFC 9512 (2024) standardized application/yaml |
| .sqlite | application/vnd.sqlite3 | application/x-sqlite3, application/octet-stream | Verify 16-byte SQLite format 3\0 header |
| .rar | application/vnd.rar | application/x-rar-compressed | Verify 52 61 72 21 1A 07 magic bytes |
3. Always send X-Content-Type-Options: nosniff on static and upload servers
Historically, browsers implemented "MIME sniffing": if a server returned a file with Content-Type: text/plain or application/octet-stream, the browser inspected the first 512 bytes and, if it saw <html> or <script>, rendered the file as an active HTML document. Sending the response header X-Content-Type-Options: nosniff (included on every route in SampleTestFiles' /_headers file) instructs browsers never to override the server's declared Content-Type.