Helmet

Help secure Express apps by setting HTTP response headers.

import helmet from "helmet";

const app = express();

app.use(helmet());

Helmet sets the following headers by default:

Each header can be configured. For example, here's how you configure the Content-Security-Policy header:

Headers can also be disabled. For example, here's how you disable the Content-Security-Policy and X-Download-Options headers:

Reference

chevron-rightContent-Security-Policyhashtag

Default:

The Content-Security-Policy header mitigates a large number of attacks, such as cross-site scriptingarrow-up-right. See MDN's introductory article on Content Security Policyarrow-up-right.

This header is powerful but likely requires some configuration for your specific app.

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.

These directives are merged into a default policy, which you can disable by setting useDefaults to false.

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.

You can set the Content-Security-Policy-Report-Onlyarrow-up-right instead:

Helmet performs very little validation on your CSP. You should rely on CSP checkers like CSP Evaluatorarrow-up-right instead.

To disable the Content-Security-Policy header:

You can use this as standalone middleware with app.use(helmet.contentSecurityPolicy()).

chevron-rightCross-Origin-Embedder-Policyhashtag

This header is not set by default.

The Cross-Origin-Embedder-Policy header helps control what resources can be loaded cross-origin. See MDN's article on this headerarrow-up-right for more.

You can use this as standalone middleware with app.use(helmet.crossOriginEmbedderPolicy()).

chevron-rightCross-Origin-Opener-Policyhashtag

Default:

The Cross-Origin-Opener-Policy header helps process-isolate your page. For more, see MDN's article on this headerarrow-up-right.

To disable the Cross-Origin-Opener-Policy header:

You can use this as standalone middleware with app.use(helmet.crossOriginOpenerPolicy()).

chevron-rightCross-Origin-Resource-Policyhashtag

Default:

The Cross-Origin-Resource-Policy header blocks others from loading your resources cross-origin in some cases. For more, see "Consider deploying Cross-Origin Resource Policy"arrow-up-right and MDN's article on this headerarrow-up-right.

To disable the Cross-Origin-Resource-Policy header:

You can use this as standalone middleware with app.use(helmet.crossOriginResourcePolicy()).

chevron-rightOrigin-Agent-Clusterhashtag

Default:

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 specarrow-up-right.

This header takes no options and is set by default.

To disable the Origin-Agent-Cluster header:

You can use this as standalone middleware with app.use(helmet.originAgentCluster()).

chevron-rightReferrer-Policyhashtag

Default:

The Referrer-Policy header which controls what information is set in the Referer request headerarrow-up-right. See "Referer header: privacy and security concerns"arrow-up-right and the header's documentationarrow-up-right on MDN for more.

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 policyarrow-up-right. It defaults to no-referrer.

To disable the Referrer-Policy header:

You can use this as standalone middleware with app.use(helmet.referrerPolicy()).

chevron-rightStrict-Transport-Securityhashtag

Default:

The Strict-Transport-Security header tells browsers to prefer HTTPS instead of insecure HTTP. See the documentation on MDNarrow-up-right for more.

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.

preload is a boolean. If true, it adds the preload directive, expressing intent to add your HSTS policy to browsers. See the "Preloading Strict Transport Security" section on MDNarrow-up-right for more. It defaults to false.

To disable the Strict-Transport-Security header:

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 issuearrow-up-right for more discussion.

You can use this as standalone middleware with app.use(helmet.strictTransportSecurity()).

chevron-rightX-Content-Type-Optionshashtag

Default:

The X-Content-Type-Options mitigates MIME type sniffingarrow-up-right which can cause security issues. See documentation for this header on MDNarrow-up-right for more.

This header takes no options and is set by default.

To disable the X-Content-Type-Options header:

You can use this as standalone middleware with app.use(helmet.xContentTypeOptions()).

chevron-rightX-DNS-Prefetch-Controlhashtag

Default:

The X-DNS-Prefetch-Control header helps control DNS prefetching, which can improve user privacy at the expense of performance. See documentation on MDNarrow-up-right for more.

allow is a boolean dictating whether to enable DNS prefetching. It defaults to false.

Examples:

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()).

chevron-rightX-Download-Optionshashtag

Default:

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 MSDNarrow-up-right.

This header takes no options and is set by default.

To disable the X-Download-Options header:

You can use this as standalone middleware with app.use(helmet.xDownloadOptions()).

chevron-rightX-Frame-Optionshashtag

Default:

The legacy X-Frame-Options header to help you mitigate clickjacking attacksarrow-up-right. This header is superseded by the frame-ancestors Content Security Policy directivearrow-up-right but is still useful on old browsers or if no CSP is used. For more, see the documentation on MDNarrow-up-right.

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.arrow-up-right) It defaults to SAMEORIGIN.

Examples:

To disable the X-Frame-Options header:

You can use this as standalone middleware with app.use(helmet.xFrameOptions()).

chevron-rightX-Permitted-Cross-Domain-Policieshashtag

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 OWASParrow-up-right for more.

permittedPolicies is a string that must be "none", "master-only", "by-content-type", or "all". It defaults to "none".

Examples:

To disable the X-Permitted-Cross-Domain-Policies header:

You can use this as standalone middleware with app.use(helmet.xPermittedCrossDomainPolicies()).

chevron-rightX-Powered-Byhashtag

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 discussionarrow-up-right) and is mostly removed to save bandwidth, but may thwart simplistic attackers.

Note: Express has a built-in way to disable the X-Powered-By headerarrow-up-right, which you may wish to use instead.

The removal of this header takes no options. The header is removed by default.

To disable this behavior:

You can use this as standalone middleware with app.use(helmet.xPoweredBy()).

chevron-rightX-XSS-Protectionhashtag

Default:

Helmet disables browsers' buggy cross-site scripting filter by setting the legacy X-XSS-Protection header to 0. See discussion about disabling the header herearrow-up-right and documentation on MDNarrow-up-right.

This header takes no options and is set by default.

To disable the X-XSS-Protection header:

You can use this as standalone middleware with app.use(helmet.xXssProtection()).

Last updated