Course contentsShow
← Security
Lesson 949 of 3,05220. Web Security - File Upload and ProcessingFree lesson

MIME Type Confusion Attacks

How Content-Type header manipulation and MIME sniffing can bypass validation that relies solely on client-provided MIME types.

MIME Type Confusion Attacks

What you'll learn: How attackers manipulate Content-Type headers and exploit MIME sniffing to bypass upload validation that trusts client-provided file type information.


The Illusion of Trust

When you upload a file, your browser sends a Content-Type header declaring what type of file it is (like image/jpeg or text/plain). Many applications naively trust this client-provided information to validate uploads, assuming "if the header says it's an image, it must be safe."

The problem: Attackers control this header completely and can lie about it.

How MIME Type Confusion Works

Imagine a bouncer at a club who only checks what guests claim to be wearing, without actually looking. An attacker could say "I'm wearing formal attire" while dressed in rags—and get in.

Similarly, an attacker can:

  1. Upload a malicious PHP web shell
  2. Set the Content-Type: image/jpeg header in the HTTP request
  3. Bypass validation that only checks the MIME type
  4. The server saves the .php file and executes it later

MIME Sniffing: The Double-Edged Sword

Browsers and some servers perform MIME sniffing—trying to detect the "real" file type by examining content, not just the extension or header. While intended to help users, this can backfire:

  • You upload a file named image.jpg
  • It contains <?php system($_GET['cmd']); ?> at the start
  • The server checks the extension (.jpg) and allows it
  • But the web server or browser sniffs the content and executes it as PHP

Why This Matters

Relying solely on client-controlled MIME types is like accepting someone's verbal ID instead of checking their passport. The Content-Type header is trivial to forge, and MIME sniffing can reinterpret files in dangerous ways.

Key Takeaway: Never trust client-provided MIME types alone—validate file content server-side using magic byte signatures, and prevent execution through proper storage, permissions, and X-Content-Type-Options: nosniff headers.