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:

  1. False rejections for legitimate users: On a Windows workstation where Microsoft Excel is installed as the default handler for .csv files, the Windows Registry often maps .csv to application/vnd.ms-excel instead of RFC 4180's text/csv. An upload endpoint that strictly checks file.mimetype === 'text/csv' rejects valid CSV uploads from Windows Excel users.
  2. Security bypasses for malicious uploads: Any client can send Content-Type: image/png while 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

ExtensionOfficial IANA MIME typeNon-standard / Legacy variants seen in the wildRecommended backend handling
.csvtext/csvapplication/vnd.ms-excel, text/plain, application/csvAccept UTF-8 text + verify delimiter structure
.jsonapplication/jsontext/json (unregistered), text/plainSend application/json; never send text/json
.jstext/javascriptapplication/javascript (obsoleted by RFC 9239)Use text/javascript per IETF RFC 9239
.mp3audio/mpegaudio/mp3, audio/x-mp3Serve as audio/mpeg; accept audio/mp3 on upload
.wavaudio/vnd.waveaudio/wav, audio/x-wav, audio/waveCheck RIFF....WAVE bytes; serve as audio/wav
.aacaudio/aacaudio/x-aac, audio/mp4 (confused with .m4a)Check FF F1 ADTS header vs. ftypM4A
.xmlapplication/xmltext/xml (RFC 7303 makes both aliases)Both are valid under RFC 7303
.yamlapplication/yamltext/yaml, application/x-yaml (pre-RFC 9512)RFC 9512 (2024) standardized application/yaml
.sqliteapplication/vnd.sqlite3application/x-sqlite3, application/octet-streamVerify 16-byte SQLite format 3\0 header
.rarapplication/vnd.rarapplication/x-rar-compressedVerify 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.

Related guides and test tools

Frequently asked questions

Why did RFC 9239 deprecate application/javascript in favor of text/javascript?
For years, HTML specifications recommended text/javascript while IANA listed application/javascript as current. RFC 9239 aligned the IANA registry with actual browser behavior by marking text/javascript as the sole standard MIME type and obsoleting application/javascript.
Does YAML finally have an official IANA MIME type?
Yes. In February 2024, IETF RFC 9512 officially registered application/yaml with IANA.