Example 1
Example 1
import { HttpSecurityPlugin, corsMiddleware } from '@setu-ts/http-security-plugin'; app.register(HttpSecurityPlugin({ cors: { origin: 'https://example.com', credentials: true }, csrf: { trustedOrigins: ['https://example.com'] }, })); // Per-route use of standalone factories: app.router.get('/api', { middleware: [corsMiddleware({ origin: 'https://other.com' })], handler: (ctx) => ctx.response.json({ ok: true }), });
CORS middleware factory.
CSRF middleware factory.
HttpSecurityPlugin factory.
IP security middleware factory.
Request size middleware factory.
Security headers middleware factory.
Options for Content-Security-Policy header.
-
connectSrc: string
Connect source directive.
-
defaultSrc: string
Default source directive.
-
fontSrc: string
Font source directive.
-
frameSrc: string
Frame source directive.
-
imgSrc: string
Image source directive.
-
mediaSrc: string
Media source directive.
-
objectSrc: string
Object source directive.
-
reportUri: string
Report URI for CSP violations.
-
scriptSrc: string
Script source directive.
-
styleSrc: string
Style source directive.
Options for CORS middleware.
-
allowedHeaders: readonly string[]
Allowed request headers for the preflight
Access-Control-Allow-Headersresponse. -
credentials: boolean
When
true, emitAccess-Control-Allow-Credentials: true. -
enabled: boolean
Enable/disable CORS. Defaults to
truewhen present. -
exposedHeaders: readonly string[]
Exposed response headers for
Access-Control-Expose-Headers. -
maxAge: number
Max age (seconds) for preflight cache.
-
methods: readonly string[]
Allowed methods for preflight
Allow-Methodsheader. -
origin: boolean | string | readonly string[] | CorsOriginMatcher
Origin configuration:
Options for CSRF middleware.
-
customHeader: string
When set, unsafe methods must carry this custom header or the request is rejected with 403. Simple form submits cannot set custom headers without a preflight, making this a CSRF defense.
-
enabled: boolean
Enable/disable CSRF protection. Defaults to
truewhen present. -
trustedOrigins: readonly string[]
Additional trusted origins (scheme+host) beyond the request's own origin. The request's own origin (derived from
request.url) is always implicitly trusted. Default:[].
Options for HttpSecurityPlugin.
-
cors: CorsOptions
CORS configuration. Presence enables CORS; absent means inactive.
-
csrf: CsrfOptions
CSRF configuration. Presence enables CSRF; absent means inactive.
-
headers: SecurityHeadersOptions
Security headers configuration. Omitted → default secure header set.
{ enabled: false }→ off. Sub-fields override individual headers. -
ipSecurity: IpSecurityOptions
IP security configuration. Presence enables IP resolution; absent means inactive.
-
requestSize: RequestSizeOptions
Request-size configuration. Presence enables size limiting; absent means inactive.
Options for IP security middleware.
-
enabled: boolean
Enable/disable IP resolution. Defaults to
truewhen present. -
ipHeader: string
The header name to read when
trustProxyistrue. Default:X-Forwarded-For. -
proxyHops: number
The number of proxies in front of this application, when they cannot be addressed by IP (a managed load balancer on a rotating address). The nth entry FROM THE RIGHT is the client:
1skips the immediate peer's contribution,2skips two, and so on. -
trustProxy: boolean
When
true, read the client IP from the proxy header instead ofrequest.ip. Requires a trusted reverse proxy. Default:false. -
trustedProxies: readonly string[]
Addresses of the proxies in front of this application, as literal addresses or CIDR blocks. When supplied, the header is walked RIGHT to LEFT and the first entry that is not one of these is the client.
Options for request-size middleware.
-
enabled: boolean
Enable/disable request size limiting. Defaults to
truewhen present. -
maxBodySize: number
Maximum body size in bytes. Default: 1_048_576 (1 MiB).
Options for security headers middleware.
-
contentSecurityPolicy: ContentSecurityPolicyOptions | false
Content-Security-Policy configuration. Set to
falseto omit entirely.undefinedkeeps the default (no CSP by default since it breaks apps). -
enabled: boolean
Enable/disable all security headers. Defaults to
true. -
permissionsPolicy: string | false
Permissions-Policy value. Set to
falseto omit.undefineduses default (none by default). -
referrerPolicy: string | false
Referrer-Policy value. Set to
falseto omit.undefineduses default (no-referrer). -
strictTransportSecurity: StrictTransportSecurityOptions | false
Strict-Transport-Security configuration. Set to
falseto omit.undefineduses defaults. -
xContentTypeOptions: string | false
X-Content-Type-Options value. Set to
falseto omit.undefineduses default (nosniff). -
xFrameOptions: string | false
X-Frame-Options value. Set to
falseto omit.undefineduses default (DENY).
Options for Strict-Transport-Security header.
-
includeSubDomains: boolean
Include subdomains. Default: true.
-
maxAge: number
Max age in seconds. Default: 31536000 (1 year).
-
preload: boolean
Preload directive.
Origin matcher function for dynamic CORS decisions.
Usage
import * as HTTP_transport_security_plugin__CORS__security_headers__CSRF__request_size__IP_security__Middleware_only_plugin_with_no_capability_token_or_service__ from "http-security-plugin/src/index.ts";