Import
Usage
Options
ThesecureHeaders middleware accepts an optional SecureHeadersOptions object:
ContentSecurityPolicyOptions
Settings for the Content-Security-Policy header. Configure directives like
defaultSrc, scriptSrc, styleSrc, etc.ContentSecurityPolicyOptions
Settings for the Content-Security-Policy-Report-Only header. Same structure as
contentSecurityPolicy but for report-only mode.boolean | string
default:false
Settings for the Cross-Origin-Embedder-Policy header. Default value:
"require-corp"boolean | string
default:true
Settings for the Cross-Origin-Resource-Policy header. Default value:
"same-origin"boolean | string
default:true
Settings for the Cross-Origin-Opener-Policy header. Default value:
"same-origin"boolean | string
default:true
Settings for the Origin-Agent-Cluster header. Default value:
"?1"boolean | string
default:true
Settings for the Referrer-Policy header. Default value:
"no-referrer"ReportingEndpointOptions[]
Settings for the Reporting-Endpoints header. Array of objects with
name and url properties.ReportToOptions[]
Settings for the Report-To header. Array of objects with
group, max_age, and endpoints properties.boolean | string
default:true
Settings for the Strict-Transport-Security header. Default value:
"max-age=15552000; includeSubDomains"boolean | string
default:true
Settings for the X-Content-Type-Options header. Default value:
"nosniff"boolean | string
default:true
Settings for the X-DNS-Prefetch-Control header. Default value:
"off"boolean | string
default:true
Settings for the X-Download-Options header. Default value:
"noopen"boolean | string
default:true
Settings for the X-Frame-Options header. Default value:
"SAMEORIGIN"boolean | string
default:true
Settings for the X-Permitted-Cross-Domain-Policies header. Default value:
"none"boolean | string
default:true
Settings for the X-XSS-Protection header. Default value:
"0"boolean
default:true
Whether to remove the X-Powered-By header.
PermissionsPolicyOptions
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