Modern web applications often deliver a wide range of file types, from JavaScript libraries and CSS stylesheets to JSON or XML data. Browsers, in an attempt to be "helpful," sometimes try to guess or "sniff" a resource's actual content type—even if the server has specified a particular MIME type. This behavior can introduce significant security vulnerabilities. The X-Content-Type-Options header protects against such risks by instructing the browser to strictly adhere to declared MIME types rather than making assumptions.
The X-Content-Type-Options header is an HTTP response header that prevents browsers from overriding or "sniffing" declared MIME types. It typically works in conjunction with the server-generated Content-Type header, ensuring files are handled strictly according to their stated MIME type.
X-Content-Type-Options: nosniff
Without X-Content-Type-Options, a browser might detect a file's MIME type by reading its contents—even if the server-specified MIME type says otherwise. For instance, if you mistakenly set a JavaScript file's MIME type to text/plain, some browsers might still try to run it as script if they "sniff" recognizable JavaScript patterns. With X-Content-Type-Options: nosniff, the browser simply trusts the MIME type declared by the server and halts execution if there's a mismatch.
In addition to preventing MIME type sniffing for scripts and stylesheets, this header can also activate Cross-Origin Read Blocking (CORB) for content such as JSON, HTML, and XML. That means if a web page tries to fetch a potentially sensitive file across domains, the browser may proactively block or sanitize the response to minimize data leaks.
Attackers may attempt to supply a malicious file disguised as a common resource. Without the nosniff directive, the browser could interpret that file as an executable script.
When the MIME type is improperly identified, users might inadvertently download and run harmful files that appear benign.
If CORB or similar protective mechanisms are not triggered, cross-origin requests could potentially reveal sensitive JSON or XML content.
a2enmod headers
systemctl restart apache2
<IfModule mod_headers.c>
Header set X-Content-Type-Options "nosniff"
</IfModule>
systemctl reload apache2
add_header X-Content-Type-Options nosniff;
nginx -t
systemctl reload nginx
Launch IIS Manager via the Start menu by searching for "Internet Information Services (IIS) Manager."
Name: X-Content-Type-Options
Value: nosniff
Learn about other HTTP security headers to strengthen your website's security: