Skip to content

Tutorial · Security · Beginner · 3 min read

How to fix X-Content-Type-Options (nosniff)

Add the X-Content-Type-Options: nosniff response header on Nginx, Apache, Cloudflare or Express, verify MIME types, and clear the matching Zenith audit finding.

Fix in one line: configure your web server, framework or CDN to send the following HTTP response header. Make sure HTML, JavaScript and CSS files also have accurate Content-Type values.

Example
X-Content-Type-Options: nosniff

Why Zenith flagged it

Zenith reports “Missing or ineffective X-Content-Type-Options” when a response omits the header or its first value is not nosniff. Browsers use MIME types to decide how to handle resources. For script and stylesheet requests, nosniff directs the browser to check the declared type and block unsuitable MIME types. It also limits MIME sniffing in other contexts, including navigation. This header belongs in the HTTP response, not an HTML <meta> tag.

Step 1 — Configure your server

Nginx: add this directive inside the relevant server or location block. The always parameter also covers error responses. Watch for nested location rules that change add_header inheritance.

Example
add_header X-Content-Type-Options "nosniff" always;

Test with sudo nginx -t. If valid, reload with sudo systemctl reload nginx.

Apache: enable mod_headers, then add this to the relevant virtual host or a permitted .htaccess file.

Example
Header always set X-Content-Type-Options "nosniff"

Reload Apache after changing the server configuration. If another layer already sets this header, reconcile that configuration instead of creating duplicate values.

Cloudflare: for compatible static-assets deployments, place the following in your _headers file and redeploy. Responses generated by Worker code need their headers set in the Worker itself.

Example
/*
  X-Content-Type-Options: nosniff
Example
export default {
  async fetch(request) {
    const upstream = await fetch(request);
    const response = new Response(upstream.body, upstream);
    response.headers.set("X-Content-Type-Options", "nosniff");
    return response;
  },
};

The Worker example is illustrative: integrate it into your existing routes instead of replacing your app. A static _headers file does not automatically modify Worker-generated responses.

Node.js / Express: if you use Helmet, the nosniff header is normally enabled by default. Otherwise, add a response-header middleware before routes and static-file handlers, then redeploy or restart.

Example
// Option 1: Helmet
import helmet from "helmet";
app.use(helmet());

// Option 2: without Helmet (use instead of option 1)
app.use((req, res, next) => {
  res.setHeader("X-Content-Type-Options", "nosniff");
  next();
});

Step 2 — Check your Content-Type headers

Enabling nosniff can reveal existing MIME-type mistakes. Verify that the server declares the correct type for every resource, especially scripts and stylesheets.

  • HTML: text/html
  • CSS: text/css
  • JavaScript: text/javascript
  • JSON: application/json

Step 3 — Verify the fix

  1. Open the affected URL and Developer Tools → Network, then reload.
  2. Select the document request and inspect its response headers.
  3. Confirm X-Content-Type-Options: nosniff is present.
  4. Check script and stylesheet responses for accurate Content-Type values, and watch the console for MIME errors.
  5. Check other affected routes and assets, including error responses where appropriate.
  6. Run a fresh Zenith audit to verify that the matching finding disappears for responses where nosniff is observed.
Example
curl -I https://example.com/affected-page

Replace the example address with your actual page. Some servers respond differently to HEAD and GET; when in doubt, check the browser's real network response. Historical Zenith reports keep their original findings.

Common mistakes

  • Adding an HTML <meta> tag instead of an HTTP response header.
  • Using sniff, false or another arbitrary value instead of nosniff.
  • Configuring only the homepage while other routes or assets lack the header.
  • Ignoring broken scripts or styles caused by incorrect Content-Type values.
  • Adding duplicate or conflicting headers at the server, application and CDN layers. Prefer one unambiguous nosniff value.

What this fix does not prove

This header improves one specific browser-facing control. It does not prove that uploads are safe, cross-site scripting is impossible, or every page has been security-tested. Continue to validate uploads, serve user-controlled files safely, and review other findings.

Further reading

  • MDN: https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Headers/X-Content-Type-Options
  • Fetch standard: https://fetch.spec.whatwg.org/#x-content-type-options-header
  • OWASP Secure Headers Project: https://secure-headers-project.owasp.org/response-headers/
  • Zenith's broader security-headers guide: https://zenith.me.uk/learn/security-headers
  • Expected outcome: on a fresh scan, Zenith no longer reports security.no-xcto for responses where it observes an effective nosniff value.

Glossary

MIME type
A media type declared by the Content-Type header, used by browsers to process a resource.
nosniff
A response-header value that enables stricter MIME checks for scripts and styles and limits sniffing in other contexts.
HTTP response header
A name/value field sent by a server or edge service along with its response.