X-XSS-Protection Middleware
This package provides an Express middleware specifically designed to disable the `X-XSS-Protection` HTTP header by setting its value to `0`. This header, once intended to mitigate cross-site scripting (XSS) attacks, has been largely deprecated by browser vendors due to its propensity to introduce new security vulnerabilities rather than solve them. It is part of the Helmet.js project, a collection of middlewares for securing Express apps. The current stable version is 2.0.0. The package maintains a low release cadence, primarily updating for Node.js compatibility or critical bug fixes, as its core functionality (disabling a header) is stable and intentionally minimal. Its key differentiator is its explicit recommendation and implementation for removing a problematic legacy security feature, contrasting with older practices that advocated its use. This library is a targeted solution for modern web security practices, where XSS mitigation is handled by Content Security Policy (CSP) and robust input sanitization instead of this unreliable header.
Common errors
-
TypeError: xXssProtection is not a function
cause Attempting to call a named import as a function when the package exports a default function, or calling `xXssProtection` when using a namespace import without accessing `.default`.fixUse `import xXssProtection from 'x-xss-protection'` for ESM or `const xXssProtection = require('x-xss-protection')` for CommonJS. -
ReferenceError: xXssProtection is not defined
cause Forgetting to import or require the package before attempting to use the `xXssProtection` function.fixAdd `const xXssProtection = require('x-xss-protection')` or `import xXssProtection from 'x-xss-protection'` at the top of your file.
Warnings
- breaking Using the `X-XSS-Protection` header (even with `1; mode=block`) is generally insecure and deprecated by modern browsers, as it can introduce new vulnerabilities like Content Security Policy (CSP) bypasses or arbitrary script execution.
- gotcha This middleware specifically sets the `X-XSS-Protection` header to `0`, effectively disabling it. It does not provide any options to enable or configure its behavior to `1; mode=block` or similar legacy settings.
- gotcha This module's functionality (disabling the `X-XSS-Protection` header) is now the default behavior in Helmet.js versions 6 and above. If you are using a modern version of Helmet, this specific middleware is likely redundant.
Install
-
npm install x-xss-protection -
yarn add x-xss-protection -
pnpm add x-xss-protection
Imports
- Default exported middleware function (ESM)
import { xXssProtection } from 'x-xss-protection';import xXssProtection from 'x-xss-protection';
- Default exported middleware function (CommonJS)
const { xXssProtection } = require('x-xss-protection');const xXssProtection = require('x-xss-protection'); - Namespace import (ESM)
import * as xXssProtection from 'x-xss-protection';
Quickstart
const express = require('express');
const xXssProtection = require('x-xss-protection');
const app = express();
// Set "X-XSS-Protection: 0"
app.use(xXssProtection());
app.get('/', (req, res) => {
res.send('X-XSS-Protection header is set to 0');
});
const PORT = process.env.PORT ?? 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
});