Helmet helps secure Node/Express apps. It sets HTTP response headers such as Content-Security-Policy and Strict-Transport-Security. It aims to be quick to integrate and be low maintenance afterward.
Quick start
import helmet from "helmet";const app = express();app.use(helmet());
This will set 13 HTTP response headers in your app.
The Origin-Agent-Cluster header provides a mechanism to allow web applications to isolate their origins from other processes. Read more about it in the spec.
This header takes no options and is set by default.
maxAge is the number of seconds browsers should remember to prefer HTTPS. If passed a non-integer, the value is rounded down. It defaults to 365 days.
includeSubDomains is a boolean which dictates whether to include the includeSubDomains directive, which makes this policy extend to subdomains. It defaults to true.
The X-DNS-Prefetch-Control header helps control DNS prefetching, which can improve user privacy at the expense of performance. See documentation on MDN for more.
The X-Download-Options header is specific to Internet Explorer 8. It forces potentially-unsafe downloads to be saved, mitigating execution of HTML in your site's context. For more, see this old post on MSDN.
This header takes no options and is set by default.
The X-Permitted-Cross-Domain-Policies header tells some clients (mostly Adobe products) your domain's policy for loading cross-domain content. See the description on OWASP for more.
Default: the X-Powered-By header, if present, is removed.
Helmet removes the X-Powered-By header, which is set by default in Express and some other frameworks. Removing the header offers very limited security benefits (see this discussion) and is mostly removed to save bandwidth, but may thwart simplistic attackers.
This header takes no options and is set by default.
To disable the X-XSS-Protection header:
// This is not recommended.app.use( helmet({ xXssProtection: false, }),
evanhahn
me@evanhahn.com
To configure this header, pass an object with a nested directives object. Each key is a directive name in camel case (such as defaultSrc) or kebab case (such as default-src). Each value is an array (or other iterable) of strings or functions for that directive. If a function appears in the array, it will be called with the request and response objects.
// Sets all of the defaults, but overrides `script-src`// and disables the default `style-src`.app.use( helmet({ contentSecurityPolicy: { directives: { "script-src": ["'self'", "example.com"], "style-src": null, }, }, }),);
You can get the default directives object with helmet.contentSecurityPolicy.getDefaultDirectives(). Here is the default policy (formatted for readability):
The default-src directive can be explicitly disabled by setting its value to helmet.contentSecurityPolicy.dangerouslyDisableDefaultSrc, but this is not recommended.
upgrade-insecure-requests, a directive that causes browsers to upgrade HTTP to HTTPS, is set by default. You may wish to avoid this in development, as you may not be developing with HTTPS. Notably, Safari will upgrade http://localhost to https://localhost, which can cause problems. To work around this, you may wish to disable the upgrade-insecure-requests directive in development. For example:
You can use this as standalone middleware with app.use(helmet.crossOriginResourcePolicy()).
You can use this as standalone middleware with app.use(helmet.originAgentCluster()).
use
(
helmet
());
policy is a string or array of strings representing the policy. If passed as an array, it will be joined with commas, which is useful when setting a fallback policy. It defaults to no-referrer.
You may wish to disable this header for local development, as it can make your browser force redirects from http://localhost to https://localhost, which may not be desirable if you develop multiple apps using localhost. See this issue for more discussion.
You can use this as standalone middleware with app.use(helmet.strictTransportSecurity()).
app.
use
(
helmet({
xContentTypeOptions: false,
}),
);
You can use this as standalone middleware with app.use(helmet.xContentTypeOptions()).
);
// Sets "X-DNS-Prefetch-Control: on"
app.use(
helmet({
xDnsPrefetchControl: { allow: true },
}),
);
To disable the X-DNS-Prefetch-Control header and use the browser's default value:
You can use this as standalone middleware with app.use(helmet.xDnsPrefetchControl()).
You can use this as standalone middleware with app.use(helmet.xDownloadOptions()).
app.
use
(
helmet
());
action is a string that specifies which directive to use—either DENY or SAMEORIGIN. (A legacy directive, ALLOW-FROM, is not supported by Helmet. Read more here.) It defaults to SAMEORIGIN.