Import
Usage
Options
ThesecureHeaders middleware accepts an optional SecureHeadersOptions object:
Settings for the Content-Security-Policy header. Configure directives like
defaultSrc, scriptSrc, styleSrc, etc.Settings for the Content-Security-Policy-Report-Only header. Same structure as
contentSecurityPolicy but for report-only mode.Settings for the Cross-Origin-Embedder-Policy header. Default value:
"require-corp"Settings for the Cross-Origin-Resource-Policy header. Default value:
"same-origin"Settings for the Cross-Origin-Opener-Policy header. Default value:
"same-origin"Settings for the Origin-Agent-Cluster header. Default value:
"?1"Settings for the Referrer-Policy header. Default value:
"no-referrer"Settings for the Reporting-Endpoints header. Array of objects with
name and url properties.Settings for the Report-To header. Array of objects with
group, max_age, and endpoints properties.Settings for the Strict-Transport-Security header. Default value:
"max-age=15552000; includeSubDomains"Settings for the X-Content-Type-Options header. Default value:
"nosniff"Settings for the X-DNS-Prefetch-Control header. Default value:
"off"Settings for the X-Download-Options header. Default value:
"noopen"Settings for the X-Frame-Options header. Default value:
"SAMEORIGIN"Settings for the X-Permitted-Cross-Domain-Policies header. Default value:
"none"Settings for the X-XSS-Protection header. Default value:
"0"Whether to remove the X-Powered-By header.
Settings for the Permissions-Policy header. Object mapping permission directives to their values.
Signature
NONCE Helper
TheNONCE helper generates a cryptographically secure nonce for CSP:
Examples
Basic usage with defaults
Custom Content Security Policy
Using NONCE for scripts
Custom Strict Transport Security
Disable specific headers
Permissions Policy
Report-Only CSP
With Reporting Endpoints
Default Values
When called without options,secureHeaders() sets these headers:
Cross-Origin-Resource-Policy: same-originCross-Origin-Opener-Policy: same-originOrigin-Agent-Cluster: ?1Referrer-Policy: no-referrerStrict-Transport-Security: max-age=15552000; includeSubDomainsX-Content-Type-Options: nosniffX-DNS-Prefetch-Control: offX-Download-Options: noopenX-Frame-Options: SAMEORIGINX-Permitted-Cross-Domain-Policies: noneX-XSS-Protection: 0- Removes
X-Powered-Byheader
CSP Directives
Available Content Security Policy directives:defaultSrcbaseUrichildSrcconnectSrcfontSrcformActionframeAncestorsframeSrcimgSrcmanifestSrcmediaSrcobjectSrcreportToreportUrisandboxscriptSrcscriptSrcAttrscriptSrcElemstyleSrcstyleSrcAttrstyleSrcElemupgradeInsecureRequestsworkerSrcrequireTrustedTypesFortrustedTypes
Behavior
- Sets security headers after handler execution
- Headers can be overridden by providing string values
- Headers can be disabled by setting to
false - CSP directives support dynamic values via callback functions
- NONCE helper generates unique nonce per request and stores in context
- Removes X-Powered-By header by default
- Supports both Content-Security-Policy and Content-Security-Policy-Report-Only