Dev.to Security 🔐 Cybersecurity 👁 0 📖 9 min read

X-Content-Type-Options: nosniff for website owners: what it stops, what it needs, and how to check it

Every file your website sends to a browser comes with a label that says what kind of file it is. That label is the Content-Type header, and its value is a MIME type such as text/html for a web page or text/css for a styl

Every file your website sends to a browser comes with a label that says what kind of file it is. That label is the Content-Type header, and its value is a MIME type such as text/html for a web page or text/css for a stylesheet. The X-Content-Type-Options header, set to the single value nosniff, tells the browser to trust that label instead of guessing. MDN describes it as a response header that indicates the MIME types advertised in the Content-Type headers should be respected and not changed, so the browser does not perform MIME type sniffing.

In this guide

  1. What X-Content-Type-Options: nosniff actually does
  2. Why nosniff only works if your Content-Type labels are right
  3. Where wrong labels come from: server defaults and outdated types
  4. How to check the headers your site sends today
  5. What can break when the header is switched on
  6. Limits of this header

What X-Content-Type-Options: nosniff actually does

Why would a browser guess at all? Sniffing means the browser looks at the content itself and decides what it probably is, even if the server said something else. MDN's Content-Type reference warns that the declared value may be ignored if browsers perform MIME sniffing, and that the way to prevent this is to set X-Content-Type-Options to nosniff. OWASP explains the security problem plainly: sniffing can turn a non-executable MIME type into an executable one, which OWASP calls a MIME confusion attack.

The header has two separate effects, and it helps to keep them apart. The first is request blocking. When a page asks for a script or a stylesheet, the browser blocks the response if its MIME type does not match what is expected: a JavaScript MIME type for scripts, or text/css for stylesheets. The second effect applies to everything else, including whole HTML pages: sniffing is turned off, and the browser uses the declared Content-Type as it is.

The practical value for a business site shows up wherever users can supply content, such as uploads, attachments or profile files. MDN gives the example of a server that sends a file as text/plain along with nosniff: the browser will not treat it as HTML, even if the file contains HTML markup. MDN states that this prevents cross-site scripting attacks where user-uploaded content is executed as an HTML document even though the server said it was plain text or some other type. MDN also notes that the directive stops a browser from treating a response as text/html when the Content-Type header is absent or names a non-HTML type.

Why nosniff only works if your Content-Type labels are right

nosniff is a promise to the browser: "believe my labels." That promise is only safe if the labels are correct. MDN notes that site security testers usually expect two things together: the X-Content-Type-Options header set, and the Content-Type header correctly set for all resources. OWASP's recommendation pairs them the same way, telling site owners to set the Content-Type header correctly throughout the site alongside nosniff.

What happens when a label is wrong? OWASP warns that if Content-Type is not set correctly, a resource such as an image may be interpreted as HTML, which makes cross-site scripting possible. OWASP adds an important nuance for prioritising: a wrong Content-Type would constitute a vulnerability only if the content is intended to be rendered by the browser and the resource is untrusted, meaning provided or modified by a user. For a site owner, this points attention at the places where visitors or customers can put files or text onto your domain, rather than at every static file equally.

The character set matters for HTML pages too. OWASP's recommended example is Content-Type: text/html; charset=UTF-8, and it notes that the charset attribute is necessary to prevent XSS in HTML pages. So a page labelled just text/html, without a charset, is worth raising with whoever maintains your site.

Correct labels also matter beyond attacks. MDN explains that if a server reports an incorrect MIME type, including a default type for unknown content, the browser has no way of knowing what the author intended, which may cause unexpected behaviour. MDN also frames guessing as a loss of control: if the browser ignores the reported MIME type, the people running the site no longer control how their content is processed. Its Content-Type reference adds that serving JavaScript as text/javascript and CSS as text/css helps the browser handle them more securely and with better performance.

Where wrong labels come from: server defaults and outdated types

Most site owners never type a Content-Type header by hand; the web server or the application picks it. That is where problems creep in. MDN states that server default configurations vary widely and set different default MIME types for files with no defined content type. As concrete examples, MDN says modern versions of Apache report none for files with unknown content types, while Nginx reports text/plain if no default content type is defined.

The list of file types also drifts out of date. MDN notes that as new content types appear, administrators may fail to add the new MIME types to their server's configuration. If your site serves a newer file format, its label depends on whether someone added that type.

JavaScript deserves a specific mention, because nosniff blocks scripts whose type does not match a JavaScript MIME type. MDN lists many older JavaScript MIME types you may still see and says that, although browsers may support some or all of them, you should only use text/javascript for JavaScript files.

Where does the fix live? It depends on your platform, so this is something to confirm with your host or developer rather than guess. MDN's guidance is that whatever server you use, the goal is a response header named Content-Type, followed by a colon and a space, followed by the MIME type. For Nginx, MDN notes there is no .htaccess equivalent, so all changes go into the main configuration file. For sites built with a server-side framework, MDN says the method depends on that tool and points to its documentation.

How to check the headers your site sends today

This check is a look, not a change: you, as the site owner, view responses in your own browser and leave the server as it is. You are checking the Content-Type label on each file, and separately whether the site sends X-Content-Type-Options: nosniff.

In Chrome, MDN describes the label check this way: load the file, open View > Developer > Developer Tools and choose the Network tab. Reload the page and select the resource you want to inspect. Under the headers, look for Content-Type, which reports the content type of that resource.

In Firefox, MDN says to load the file and go to Tools > Page Info to see the content type of the page. You can also open Tools > Web Developer > Network and reload the page; the list shows every resource the page loaded, and clicking any one shows all its information, including its Content-Type header. Menu names can differ between browser versions, so if you cannot find these menus, the result of your check is unconfirmed and you should ask your developer to pull the headers instead.

What to look at: check more than the home page. Look at a script file, a stylesheet, an image, and above all anything users can upload or download. Scripts should show a JavaScript type, stylesheets text/css, and HTML pages text/html with a charset. If a page uses a meta tag to declare its type, MDN notes that the standards say such a meta element should be ignored when a Content-Type header is present, so the header is what counts.

For the nosniff header itself, MDN points to HTTP Observatory as a way to test a website's security configuration, including this header. The value you are looking for is the one in MDN's syntax: X-Content-Type-Options: nosniff, which blocks a stylesheet not served as text/css and a script not served as a JavaScript MIME type. Running the scan before and after any change gives you a simple record of whether the header is present.

You can also check your website for vulnerabilities from the outside, then confirm the Content-Type and nosniff headers yourself in the browser, as described above.

What can break when the header is switched on

The request-blocking effect is the one that can surprise you. With nosniff in place, a stylesheet that is not served as text/css, or a script that is not served as a JavaScript MIME type, is blocked by the browser. A site that has quietly relied on browsers guessing may lose its styling or some functionality the moment the header is added.

That is why the order matters: confirm the labels before anyone switches the header on. Using the check above, look at the scripts and stylesheets your pages load, including files from plugins or themes. Anything that shows text/plain, a missing type or an unexpected type is a file that would be blocked. MDN's guidance on JavaScript is to use text/javascript, and OWASP's guidance is to set Content-Type correctly throughout the site, so fixing labels is the real work. The exact setting that enables the header depends on your server or platform and is not described by the sources used for this article, so this article gives no configuration steps for it. Instead, hand your list of mislabelled files to your developer or host, ask them which configuration they will change to enable the header, and ask how they would undo that change if pages break.

After they report the change as live, repeat the label check on the same files, run HTTP Observatory again to see whether the header is now reported, and confirm the pages still look and behave as before. If something stops working, the blocked resource is the first place to look, but its cause is not established until you have looked at what type it was actually served with.

Limits of this header

nosniff is a narrow control. It makes the browser honour the labels your server sends; it does not decide whether those labels are right. Treat it as a layer next to careful handling of user uploads, a correctly set character set on HTML pages, and the other security headers your site uses. This article covers the header's behaviour and how to observe it in a browser; it does not cover server-specific configuration syntax, which you should take from your host's or framework's own documentation.

A website vulnerability scanner that looks from the outside can help you confirm whether nosniff is present, but it does not tell you whether every file is labelled correctly. That check is the Network tab above.

Questions and answers

What does X-Content-Type-Options: nosniff do?

The X-Content-Type-Options header, set to the single value nosniff, tells the browser to trust that label instead of guessing.

Is nosniff enough on its own?

That promise is only safe if the labels are correct.

How do I check if my site sends correct Content-Type headers?

Under the headers, look for Content-Type, which reports the content type of that resource.

Can turning on nosniff break my site?

A site that has quietly relied on browsers guessing may lose its styling or some functionality the moment the header is added.

Which MIME type should JavaScript files use?

you should only use text/javascript for JavaScript files

Open your browser's developer tools on your own site today and note which scripts, stylesheets and uploads show the labels you expect. If you want a second look at the security headers a site you own sends, you can run a free website security scan.

Sources

  1. MDN: X-Content-Type-Options
  2. MDN: Content-Type header
  3. OWASP HTTP Security Headers Cheat Sheet
  4. MDN: Configuring server MIME types

The original version of this article is on the HackerMe Security blog.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.