跳转到内容
简体中文

Security headers

此内容尚不支持你的语言。

securityHeaders() adds a sensible set of security response headers. Every header is individually overridable, false leaves one out, and a header the handler already set always wins.

import { securityHeaders, withMiddleware } from 'actor-ts/http';
const routes = withMiddleware(securityHeaders(), appRoutes);

Defaults: X-Content-Type-Options: nosniff, X-Frame-Options: DENY, Referrer-Policy: no-referrer, Cross-Origin-Opener-Policy: same-origin, Cross-Origin-Resource-Policy: same-origin, and X-XSS-Protection: 0 (the legacy filter is buggy — off is safest). COEP, Permissions-Policy, and HSTS are opt-in.

Builder methodDefault
withContentTypeOptions(flag?)nosniff on
withFrameOptions('DENY' | 'SAMEORIGIN' | false)'DENY'
withReferrerPolicy(value | false)'no-referrer'
withCrossOriginOpenerPolicy(v | false)'same-origin'
withCrossOriginResourcePolicy(v | false)'same-origin'
withCrossOriginEmbedderPolicy(v | false)false (breaks embeds; opt-in)
withPermissionsPolicy(map | false)false
withHsts(options | false)false (opt-in)
const options = SecurityHeadersOptions.create()
.withFrameOptions('SAMEORIGIN')
.withHsts({ maxAge: 31_536_000, preload: true });

CSP is deliberately not part of the bundle — it is too app-specific; configure it with contentSecurityPolicy.

HSTS on its own — strictTransportSecurity

Section titled “HSTS on its own — strictTransportSecurity”
import { strictTransportSecurity, HstsOptions, withMiddleware } from 'actor-ts/http';
const hsts = strictTransportSecurity(
HstsOptions.create().withMaxAge(31_536_000).withIncludeSubDomains().withPreload(),
);

Defaults: max-age 180 days + includeSubDomains. The header is set unconditionally — a browser ignores it over plain HTTP, so a dev server is a harmless no-op, and framework servers usually sit behind a TLS-terminating proxy anyway.

  • CSP — Content-Security-Policy.
  • CORS — cross-origin requests.
  • Security — the full recommended stack.