Why it matters
- Without the header, browsers may examine a response's content to guess its type instead of trusting its
Content-Type: this is MIME type sniffing. - With
X-Content-Type-Options: nosniff, the browser takes theContent-Typeas sent. MDN's example: atext/plainresponse that holds HTML markup is not read as HTML. - The browser also refuses a stylesheet whose type is not
text/css, and a script whose type is not a JavaScript type: a file on your site sent as text, such as one a visitor uploaded, cannot be loaded as a script. - OWASP recommends the header, with the right
Content-Typethroughout the site.
How to fix
Send the header on every response:
http
X-Content-Type-Options: nosniff- Apache (mod_headers):
Header always set X-Content-Type-Options "nosniff" - nginx:
add_header X-Content-Type-Options nosniff always; - In nginx, a
locationblock with anadd_headerof its own does not inherit those of theserverblock, so repeat the header there. - Cloudflare: a Response Header Transform Rule that sets the header.
- Check each file's
Content-Typetoo:text/cssfor stylesheets andtext/javascriptfor scripts. With the header, the browser refuses a stylesheet or a script sent with another type.
How we detect
- The rule applies to HTML pages that answer 2xx on a public site. Local development hosts, such as
localhost, private addresses and names ending in.test, are left out, as for the HTTPS rule: the header protects a public site's visitors. - We read the
X-Content-Type-Optionsheaders as browsers do (the Fetch standard's "determine nosniff"): all of them make one list, split at commas, and the first value must benosniff, whatever its case. Any other first value is ignored. - We read the page's own response. The header matters as much on scripts and stylesheets, which this rule does not fetch.