secure-headers.ts
This file
Path
src/middleware/secure-headers/secure-headers.ts
341 lines 路 12.4 KiB 路 TypeScript
Last changed by
fix(secure-headers): output standard empty parentheses () instead of none for disabled Permissions-Policy directives (#5197)Nanasibefore adoption13a9481
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342/** * @module * Secure Headers Middleware for Hono. */
import type { Context } from '../../context'import type { MiddlewareHandler } from '../../types'import { encodeBase64 } from '../../utils/encode'import type { PermissionsPolicyDirective } from './permissions-policy'
export type SecureHeadersVariables = { secureHeadersNonce?: string}
export type ContentSecurityPolicyOptionHandler = (ctx: Context, directive: string) => stringtype ContentSecurityPolicyOptionValue = (string | ContentSecurityPolicyOptionHandler)[]
interface ContentSecurityPolicyOptions { defaultSrc?: ContentSecurityPolicyOptionValue baseUri?: ContentSecurityPolicyOptionValue childSrc?: ContentSecurityPolicyOptionValue connectSrc?: ContentSecurityPolicyOptionValue fontSrc?: ContentSecurityPolicyOptionValue formAction?: ContentSecurityPolicyOptionValue frameAncestors?: ContentSecurityPolicyOptionValue frameSrc?: ContentSecurityPolicyOptionValue imgSrc?: ContentSecurityPolicyOptionValue manifestSrc?: ContentSecurityPolicyOptionValue mediaSrc?: ContentSecurityPolicyOptionValue objectSrc?: ContentSecurityPolicyOptionValue reportTo?: string reportUri?: string | string[] sandbox?: ContentSecurityPolicyOptionValue scriptSrc?: ContentSecurityPolicyOptionValue scriptSrcAttr?: ContentSecurityPolicyOptionValue scriptSrcElem?: ContentSecurityPolicyOptionValue styleSrc?: ContentSecurityPolicyOptionValue styleSrcAttr?: ContentSecurityPolicyOptionValue styleSrcElem?: ContentSecurityPolicyOptionValue upgradeInsecureRequests?: ContentSecurityPolicyOptionValue workerSrc?: ContentSecurityPolicyOptionValue requireTrustedTypesFor?: ContentSecurityPolicyOptionValue trustedTypes?: ContentSecurityPolicyOptionValue}
interface ReportToOptions { group: string max_age: number endpoints: ReportToEndpoint[]}
interface ReportToEndpoint { url: string}
interface ReportingEndpointOptions { name: string url: string}
type PermissionsPolicyValue = '*' | 'self' | 'src' | 'none' | string
type PermissionsPolicyOptions = Partial< Record<PermissionsPolicyDirective, PermissionsPolicyValue[] | boolean>>
type overridableHeader = boolean | string
interface SecureHeadersOptions { contentSecurityPolicy?: ContentSecurityPolicyOptions contentSecurityPolicyReportOnly?: ContentSecurityPolicyOptions crossOriginEmbedderPolicy?: overridableHeader crossOriginResourcePolicy?: overridableHeader crossOriginOpenerPolicy?: overridableHeader originAgentCluster?: overridableHeader referrerPolicy?: overridableHeader reportingEndpoints?: ReportingEndpointOptions[] reportTo?: ReportToOptions[] strictTransportSecurity?: overridableHeader xContentTypeOptions?: overridableHeader xDnsPrefetchControl?: overridableHeader xDownloadOptions?: overridableHeader xFrameOptions?: overridableHeader xPermittedCrossDomainPolicies?: overridableHeader xXssProtection?: overridableHeader removePoweredBy?: boolean permissionsPolicy?: PermissionsPolicyOptions}
type HeadersMap = { [key in keyof SecureHeadersOptions]: [string, string]}
const HEADERS_MAP: HeadersMap = { crossOriginEmbedderPolicy: ['Cross-Origin-Embedder-Policy', 'require-corp'], crossOriginResourcePolicy: ['Cross-Origin-Resource-Policy', 'same-origin'], crossOriginOpenerPolicy: ['Cross-Origin-Opener-Policy', 'same-origin'], originAgentCluster: ['Origin-Agent-Cluster', '?1'], referrerPolicy: ['Referrer-Policy', 'no-referrer'], strictTransportSecurity: ['Strict-Transport-Security', 'max-age=15552000; includeSubDomains'], xContentTypeOptions: ['X-Content-Type-Options', 'nosniff'], xDnsPrefetchControl: ['X-DNS-Prefetch-Control', 'off'], xDownloadOptions: ['X-Download-Options', 'noopen'], xFrameOptions: ['X-Frame-Options', 'SAMEORIGIN'], xPermittedCrossDomainPolicies: ['X-Permitted-Cross-Domain-Policies', 'none'], xXssProtection: ['X-XSS-Protection', '0'],}
const DEFAULT_OPTIONS: SecureHeadersOptions = { crossOriginEmbedderPolicy: false, crossOriginResourcePolicy: true, crossOriginOpenerPolicy: true, originAgentCluster: true, referrerPolicy: true, strictTransportSecurity: true, xContentTypeOptions: true, xDnsPrefetchControl: true, xDownloadOptions: true, xFrameOptions: true, xPermittedCrossDomainPolicies: true, xXssProtection: true, removePoweredBy: true, permissionsPolicy: {},}
type SecureHeadersCallback = ( ctx: Context, headersToSet: [string, string | string[]][]) => [string, string][]
const generateNonce = () => { const arrayBuffer = new Uint8Array(16) crypto.getRandomValues(arrayBuffer) return encodeBase64(arrayBuffer.buffer)}
export const NONCE: ContentSecurityPolicyOptionHandler = (ctx) => { const key = 'secureHeadersNonce' const init = ctx.get(key) const nonce = init || generateNonce() if (init == null) { ctx.set(key, nonce) } return `'nonce-${nonce}'`}
/** * Secure Headers Middleware for Hono. * * @see {@link https://hono.dev/docs/middleware/builtin/secure-headers} * * @param {Partial<SecureHeadersOptions>} [customOptions] - The options for the secure headers middleware. * @param {ContentSecurityPolicyOptions} [customOptions.contentSecurityPolicy] - Settings for the Content-Security-Policy header. * @param {ContentSecurityPolicyOptions} [customOptions.contentSecurityPolicyReportOnly] - Settings for the Content-Security-Policy-Report-Only header. * @param {overridableHeader} [customOptions.crossOriginEmbedderPolicy=false] - Settings for the Cross-Origin-Embedder-Policy header. * @param {overridableHeader} [customOptions.crossOriginResourcePolicy=true] - Settings for the Cross-Origin-Resource-Policy header. * @param {overridableHeader} [customOptions.crossOriginOpenerPolicy=true] - Settings for the Cross-Origin-Opener-Policy header. * @param {overridableHeader} [customOptions.originAgentCluster=true] - Settings for the Origin-Agent-Cluster header. * @param {overridableHeader} [customOptions.referrerPolicy=true] - Settings for the Referrer-Policy header. * @param {ReportingEndpointOptions[]} [customOptions.reportingEndpoints] - Settings for the Reporting-Endpoints header. * @param {ReportToOptions[]} [customOptions.reportTo] - Settings for the Report-To header. * @param {overridableHeader} [customOptions.strictTransportSecurity=true] - Settings for the Strict-Transport-Security header. * @param {overridableHeader} [customOptions.xContentTypeOptions=true] - Settings for the X-Content-Type-Options header. * @param {overridableHeader} [customOptions.xDnsPrefetchControl=true] - Settings for the X-DNS-Prefetch-Control header. * @param {overridableHeader} [customOptions.xDownloadOptions=true] - Settings for the X-Download-Options header. * @param {overridableHeader} [customOptions.xFrameOptions=true] - Settings for the X-Frame-Options header. * @param {overridableHeader} [customOptions.xPermittedCrossDomainPolicies=true] - Settings for the X-Permitted-Cross-Domain-Policies header. * @param {overridableHeader} [customOptions.xXssProtection=true] - Settings for the X-XSS-Protection header. * @param {boolean} [customOptions.removePoweredBy=true] - Settings for remove X-Powered-By header. * @param {PermissionsPolicyOptions} [customOptions.permissionsPolicy] - Settings for the Permissions-Policy header. * @returns {MiddlewareHandler} The middleware handler function. * * @example * ```ts * const app = new Hono() * app.use(secureHeaders()) * ``` */export const secureHeaders = (customOptions?: SecureHeadersOptions): MiddlewareHandler => { const options = { ...DEFAULT_OPTIONS, ...customOptions } const headersToSet = getFilteredHeaders(options) const callbacks: SecureHeadersCallback[] = []
if (options.contentSecurityPolicy) { const [callback, value] = getCSPDirectives( options.contentSecurityPolicy, 'Content-Security-Policy' ) if (callback) { callbacks.push(callback) } headersToSet.push(['Content-Security-Policy', value as string]) }
if (options.contentSecurityPolicyReportOnly) { const [callback, value] = getCSPDirectives( options.contentSecurityPolicyReportOnly, 'Content-Security-Policy-Report-Only' ) if (callback) { callbacks.push(callback) } headersToSet.push(['Content-Security-Policy-Report-Only', value as string]) }
if (options.permissionsPolicy && Object.keys(options.permissionsPolicy).length > 0) { headersToSet.push([ 'Permissions-Policy', getPermissionsPolicyDirectives(options.permissionsPolicy), ]) }
if (options.reportingEndpoints) { headersToSet.push(['Reporting-Endpoints', getReportingEndpoints(options.reportingEndpoints)]) }
if (options.reportTo) { headersToSet.push(['Report-To', getReportToOptions(options.reportTo)]) }
return async function secureHeaders(ctx, next) { // should evaluate callbacks before next() // some callback calls ctx.set() for embedding nonce to the page const headersToSetForReq = callbacks.length === 0 ? headersToSet : callbacks.reduce((acc, cb) => cb(ctx, acc), headersToSet) await next() setHeaders(ctx, headersToSetForReq)
if (options?.removePoweredBy) { ctx.res.headers.delete('X-Powered-By') } }}
function getFilteredHeaders(options: SecureHeadersOptions): [string, string][] { return Object.entries(HEADERS_MAP) .filter(([key]) => options[key as keyof SecureHeadersOptions]) .map(([key, defaultValue]) => { const overrideValue = options[key as keyof SecureHeadersOptions] return typeof overrideValue === 'string' ? [defaultValue[0], overrideValue] : defaultValue })}
function getCSPDirectives( contentSecurityPolicy: ContentSecurityPolicyOptions, headerName: 'Content-Security-Policy' | 'Content-Security-Policy-Report-Only'): [SecureHeadersCallback | undefined, string | string[]] { const callbacks: ((ctx: Context, values: string[]) => void)[] = [] const resultValues: string[] = []
for (const [directive, value] of Object.entries(contentSecurityPolicy)) { const valueArray = Array.isArray(value) ? value : [value]
valueArray.forEach((value, i) => { if (typeof value === 'function') { const index = i * 2 + 2 + resultValues.length callbacks.push((ctx, values) => { values[index] = value(ctx, directive) }) } })
resultValues.push( directive.replace(/[A-Z]+(?![a-z])|[A-Z]/g, (match, offset) => offset ? '-' + match.toLowerCase() : match.toLowerCase() ), ...valueArray.flatMap((value) => [' ', value]), '; ' ) } resultValues.pop()
return callbacks.length === 0 ? [undefined, resultValues.join('')] : [ (ctx, headersToSet) => headersToSet.map((values) => { if (values[0] === headerName) { const clone = values[1].slice() as unknown as string[] callbacks.forEach((cb) => { cb(ctx, clone) }) return [values[0], clone.join('')] } else { return values as [string, string] } }), resultValues, ]}
function getPermissionsPolicyDirectives(policy: PermissionsPolicyOptions): string { return Object.entries(policy) .map(([directive, value]) => { const kebabDirective = camelToKebab(directive)
if (typeof value === 'boolean') { return `${kebabDirective}=${value ? '*' : '()'}` }
if (Array.isArray(value)) { if (value.length === 0) { return `${kebabDirective}=()` } if (value.length === 1 && value[0] === '*') { return `${kebabDirective}=*` } if (value.length === 1 && value[0] === 'none') { return `${kebabDirective}=()` } const allowlist = value.map((item) => (['self', 'src'].includes(item) ? item : `"${item}"`)) return `${kebabDirective}=(${allowlist.join(' ')})` }
return '' }) .filter(Boolean) .join(', ')}
function camelToKebab(str: string): string { return str.replace(/([a-z\d])([A-Z])/g, '$1-$2').toLowerCase()}
function getReportingEndpoints( reportingEndpoints: SecureHeadersOptions['reportingEndpoints'] = []): string { return reportingEndpoints.map((endpoint) => `${endpoint.name}="${endpoint.url}"`).join(', ')}
function getReportToOptions(reportTo: SecureHeadersOptions['reportTo'] = []): string { return reportTo.map((option) => JSON.stringify(option)).join(', ')}
function setHeaders(ctx: Context, headersToSet: [string, string][]) { headersToSet.forEach(([header, value]) => { ctx.res.headers.set(header, value) })}