❤️
SecurityHeaders.nl

X-Content-Type-Options

The X-Content-Type-Options HTTP response header is a security feature that stops a browser from trying to MIME-sniff the content type and forces it to stick with the declared Content-Type.

What is MIME-sniffing?

MIME-sniffing is a feature implemented by some browsers (historically Internet Explorer and Chrome) that attempts to determine the file type of a resource by examining its content, rather than relying solely on the Content-Type header provided by the server.

While this feature was intended to correct server misconfigurations, it introduced a significant security vulnerability. For example, if a user uploads a malicious HTML file pretending to be a .jpg image, the server might serve it with Content-Type: image/jpeg. However, a browser that MIME-sniffs the file might realize it contains HTML and execute the malicious scripts within it, leading to a Cross-Site Scripting (XSS) attack.

The Fix

X-Content-Type-Options: nosniff

This is the only valid value for this header. When a browser receives this directive, it will refuse to interpret files as a different MIME type than what is declared by the server.

For example, if the server says a file is an image, the browser will strictly treat it as an image, effectively neutralizing attacks where executable code is disguised as benign file types.

Implementation

Because there are virtually no downsides to implementing this header, it is highly recommended to set X-Content-Type-Options: nosniff globally for all responses originating from your web application.