X-Content-Type-Options Header Explained
X-Content-Type-Options: nosniff is an HTTP response header that prevents browsers from MIME-type sniffing. Without it, browsers may interpret a file as a different content type than declared, allowing attackers to disguise executable scripts as images or other harmless file types.
Recommended Value
X-Content-Type-Options: nosniff
What Each Directive Does
| Directive | Purpose |
|---|---|
nosniff | The only valid value — tells the browser to strictly follow the declared Content-Type and never guess |
What Happens Without This Header
Without nosniff, an attacker can upload a file with a .jpg extension but containing JavaScript. If the server serves it with the wrong Content-Type or without one, the browser might sniff the actual content and execute it as JavaScript, bypassing XSS protections.
How to Implement
# Nginx
add_header X-Content-Type-Options "nosniff" always;
# Apache
Header always set X-Content-Type-Options "nosniff"
# Express.js
app.use((req, res, next) => {
res.setHeader('X-Content-Type-Options', 'nosniff');
next();
});
Testing and Report-Only
X-Content-Type-Options has no report-only mode. It is safe to deploy immediately since it only enforces correct MIME type handling.
Related Questions
Scan your system prompt with LochBot — free, client-side, no data sent anywhere.