Skip to main content

Import

Usage

Options

The secureHeaders 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

The NONCE 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-origin
  • Cross-Origin-Opener-Policy: same-origin
  • Origin-Agent-Cluster: ?1
  • Referrer-Policy: no-referrer
  • Strict-Transport-Security: max-age=15552000; includeSubDomains
  • X-Content-Type-Options: nosniff
  • X-DNS-Prefetch-Control: off
  • X-Download-Options: noopen
  • X-Frame-Options: SAMEORIGIN
  • X-Permitted-Cross-Domain-Policies: none
  • X-XSS-Protection: 0
  • Removes X-Powered-By header

CSP Directives

Available Content Security Policy directives:
  • defaultSrc
  • baseUri
  • childSrc
  • connectSrc
  • fontSrc
  • formAction
  • frameAncestors
  • frameSrc
  • imgSrc
  • manifestSrc
  • mediaSrc
  • objectSrc
  • reportTo
  • reportUri
  • sandbox
  • scriptSrc
  • scriptSrcAttr
  • scriptSrcElem
  • styleSrc
  • styleSrcAttr
  • styleSrcElem
  • upgradeInsecureRequests
  • workerSrc
  • requireTrustedTypesFor
  • trustedTypes

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