Helmet Security Headers
Set Content-Security-Policy, HSTS, X-Frame-Options, Referrer-Policy and more on every response with helmet() from @axiljs/security. All defaults and options.
On this page10
helmet() sets a group of HTTP response headers that tell browsers to turn on their built-in protections against cross-site scripting, clickjacking, MIME sniffing, protocol downgrade and referrer leaks. It has no dependencies and every header can be changed or turned off.
1import { Application } from '@axiljs/core'
2import { helmet } from '@axiljs/security'
3
4const app = new Application()
5app.use(helmet())Register it as global middleware, before your routes, so every response gets the headers.
Default headers
With no options, helmet() sets:
| Header | Default value | Protects against |
|---|---|---|
Content-Security-Policy | See "Default Content-Security-Policy" below | XSS and injected resources |
X-Frame-Options | DENY | Clickjacking (framing your pages) |
Strict-Transport-Security | max-age=15552000; includeSubDomains | HTTPS downgrade (180 days) |
X-Content-Type-Options | nosniff | MIME-type sniffing |
X-XSS-Protection | 1; mode=block | Reflected XSS in legacy browsers |
Referrer-Policy | no-referrer | Leaking URLs to other sites |
Permissions-Policy | camera=(), microphone=(), geolocation=(), payment=() | Pages or embeds using those browser APIs |
It also removes any X-Powered-By header already on the response, so the server stack is not advertised.
Default Content-Security-Policy
default-src 'self'; base-uri 'self'; font-src 'self' https: data:; form-action 'self';
frame-ancestors 'self'; img-src 'self' data: https:; object-src 'none'; script-src 'self';
script-src-attr 'none'; style-src 'self' https: 'unsafe-inline'; upgrade-insecure-requests(Sent as a single line.) Scripts load only from your own origin, inline event handlers are blocked, plugins are disabled, and http: sub-resources are upgraded to https:.
Options
| Option | Type | Default | Description |
|---|---|---|---|
contentSecurityPolicy | boolean | string | true | true sends the default policy, a string replaces it, false omits the header |
frameOptions | 'DENY' | 'SAMEORIGIN' | false | 'DENY' | X-Frame-Options value, or false to omit it |
hsts | boolean | { maxAge?: number; includeSubDomains?: boolean } | true | false omits it. The object form defaults to maxAge: 15552000 and includeSubDomains: true |
noSniff | boolean | true | Send X-Content-Type-Options: nosniff |
xssFilter | boolean | true | Send X-XSS-Protection: 1; mode=block |
referrerPolicy | string | false | 'no-referrer' | Referrer-Policy value, or false to omit it |
permissionsPolicy | boolean | true | Send the fixed Permissions-Policy value above. Its value cannot be customised |
hidePoweredBy | boolean | true | Remove X-Powered-By |
Common configurations
A JSON API
An API that only returns JSON can use the strictest policy:
1app.use(helmet({
2 contentSecurityPolicy: "default-src 'none'; frame-ancestors 'none'",
3 referrerPolicy: 'no-referrer'
4}))Pages that load third-party scripts
Pass the full policy as a string. It replaces the default entirely, so restate every directive you need:
1app.use(helmet({
2 contentSecurityPolicy: [
3 "default-src 'self'",
4 "script-src 'self' https://cdn.jsdelivr.net",
5 "img-src 'self' data: https:",
6 "style-src 'self' 'unsafe-inline'",
7 "connect-src 'self' https://api.example.com",
8 "frame-ancestors 'none'"
9 ].join('; ')
10}))Allow framing by your own site
app.use(helmet({ frameOptions: 'SAMEORIGIN' }))The default CSP already contains frame-ancestors 'self', which modern browsers use instead of X-Frame-Options when both are present.
Longer HSTS for preload
app.use(helmet({ hsts: { maxAge: 63072000, includeSubDomains: true } })) // 2 yearsThere is no preload option. Browsers ignore Strict-Transport-Security on plain http: responses, so it does no harm in local development.
Turning headers off
1app.use(helmet({
2 contentSecurityPolicy: false, // set by your CDN or reverse proxy instead
3 xssFilter: false,
4 permissionsPolicy: false
5}))Note
helmet() sets headers before your handler runs. A handler that sets the same header afterwards with res.header() overrides it for that response, which is the way to loosen a policy on a single route. hidePoweredBy only removes an X-Powered-By header that exists when the middleware runs.
Related
Was this page helpful?