@softistx/alxia-secure-headers 0.0.0-stage → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,14 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/clear-site-data.ts", "../src/csp.ts", "../src/nonce.ts", "../src/page.ts", "../src/secure-headers.ts"],
4
+ "sourcesContent": [
5
+ "/**\n * `clearSiteData`: `Clear-Site-Data` on a successful, same-site sign-out, as a\n * middleware for the sign-out path.\n */\nimport {\n\tdefineMiddleware,\n\ttype Empty,\n\ttype Middleware,\n\tmarkFactory,\n\tsettle,\n\twithHeaders,\n} from '@softistx/alxia';\n\nconst TYPES = [\n\t'cache',\n\t'cookies',\n\t'storage',\n\t'executionContexts',\n\t'clientHints',\n\t'prefetchCache',\n\t'prerenderCache',\n\t'*',\n] as const;\n\n/** A value of the `Clear-Site-Data` header, without its quotes. */\nexport type ClearSiteDataType = (typeof TYPES)[number];\n\nexport interface ClearSiteDataOptions {\n\t/** Defaults to `['POST']`: a GET to a sign-out URL clears nothing. */\n\treadonly methods?: readonly string[];\n\t/**\n\t * Also clear on a request another site made. Defaults to `false`: a\n\t * request whose `Sec-Fetch-Site` is not `same-origin` or `none`, or whose\n\t * `Origin` is not the request's own, clears nothing.\n\t */\n\treadonly crossSite?: boolean;\n\t/**\n\t * The origin a request must come from, such as `https://app.example.com`:\n\t * required behind a TLS-terminating proxy, where the request's own URL\n\t * says `http:`, for a browser that sends no `Sec-Fetch-Site`. A getter is\n\t * read at each request. Else the request's own origin.\n\t */\n\treadonly origin?: string | (() => string | undefined);\n}\n\n/**\n * What `clearSiteData()` returns: a middleware, for `app.use`, that adds\n * nothing to the context.\n */\nexport type ClearSiteData = Middleware<Empty, Promise<Response>>;\n\n/** `\"a\", \"b\"`: each name quoted, as the header and the messages write them. */\nfunction quoted(names: readonly string[]): string {\n\treturn names.map((name) => `\"${name}\"`).join(', ');\n}\n\n/**\n * True when the request came from another site, as far as it says.\n * `Sec-Fetch-Site`, which the browser sets and a proxy leaves alone, decides\n * alone when present; else `Origin` is compared with the app's own.\n */\nfunction isCrossSite(\n\trequest: Request,\n\town: string | (() => string | undefined) | undefined,\n): boolean {\n\tconst site = request.headers.get('sec-fetch-site');\n\tif (site !== null) return site !== 'same-origin' && site !== 'none';\n\tconst origin = request.headers.get('origin');\n\tif (origin === null) return false;\n\tconst set = typeof own === 'function' ? own() : own;\n\treturn origin !== (set ?? new URL(request.url).origin);\n}\n\n/**\n * `Clear-Site-Data` on a 2xx or 3xx answer to one of `methods` (`POST` by\n * default). A request clears nothing when it is refused (a 403, a failure) or\n * when another site made it (`Sec-Fetch-Site` other than `same-origin` or\n * `none`; without that header, a foreign `Origin`), unless `crossSite` is `true`; a request\n * with neither header, such as curl's, is cleared. Give it to `use(path, …)` for the sign-out path; React Router's\n * single fetch posts `.data` too, so mount both:\n *\n * ```ts\n * const cleared = clearSiteData(['cache']);\n * app.use('/sign-out', cleared).use('/sign-out.data', cleared);\n * ```\n *\n * Throws a `TypeError` at declaration on an empty `types`, a type that is not\n * one of `ClearSiteDataType`, and an empty `methods` or an empty method name.\n */\nexport function clearSiteData(\n\ttypes: readonly ClearSiteDataType[],\n\toptions: ClearSiteDataOptions = {},\n): ClearSiteData {\n\tif (types.length === 0) {\n\t\tthrow new TypeError(\n\t\t\t'clearSiteData(): no type is given: write at least one, such as [\"cache\"]',\n\t\t);\n\t}\n\tfor (const type of types) {\n\t\tif (!(TYPES as readonly string[]).includes(type)) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`clearSiteData(): \"${type}\" is not a type: write one of ${quoted(TYPES)}`,\n\t\t\t);\n\t\t}\n\t}\n\tconst value = quoted(types);\n\tconst given = options.methods ?? ['POST'];\n\tif (given.length === 0 || given.some((method) => method === '')) {\n\t\tthrow new TypeError(\n\t\t\t'clearSiteData(): methods is empty or holds an empty method: write at least one, such as [\"POST\"], or leave methods out',\n\t\t);\n\t}\n\tconst methods = new Set(given.map((method) => method.toUpperCase()));\n\tconst crossSite = options.crossSite === true;\n\treturn defineMiddleware(async function clearSiteData(ctx, next) {\n\t\tconst response = await settle(ctx, next());\n\t\tif (!methods.has(ctx.request.method)) return response;\n\t\tif (response.status < 200 || response.status >= 400) return response;\n\t\tif (!crossSite && isCrossSite(ctx.request, options.origin)) return response;\n\t\treturn withHeaders(response, (headers) => {\n\t\t\tif (!headers.has('clear-site-data'))\n\t\t\t\theaders.set('clear-site-data', value);\n\t\t});\n\t});\n}\n\nmarkFactory(clearSiteData);\n",
6
+ "/**\n * `csp`: a `Content-Security-Policy` written as directives, checked once at\n * startup, instead of one long string.\n */\n\n/**\n * A directive's value: its sources; `true` for a bare directive\n * (`upgrade-insecure-requests`); `false` to leave it out.\n */\nexport type CspValue = readonly string[] | boolean;\n\n/** The directives a policy usually names, for completion. */\nexport type CspDirectiveName =\n\t| 'default-src'\n\t| 'script-src'\n\t| 'script-src-elem'\n\t| 'script-src-attr'\n\t| 'style-src'\n\t| 'style-src-elem'\n\t| 'style-src-attr'\n\t| 'img-src'\n\t| 'font-src'\n\t| 'connect-src'\n\t| 'media-src'\n\t| 'object-src'\n\t| 'frame-src'\n\t| 'child-src'\n\t| 'worker-src'\n\t| 'manifest-src'\n\t| 'base-uri'\n\t| 'form-action'\n\t| 'frame-ancestors'\n\t| 'upgrade-insecure-requests'\n\t| 'sandbox';\n\n/** A policy's directives: the known names complete, any other name is allowed. */\nexport type CspDirectives = {\n\treadonly [K in CspDirectiveName]?: CspValue | undefined;\n} & {\n\treadonly [name: string]: CspValue | undefined;\n};\n\nconst KEYWORDS = new Set([\n\t'self',\n\t'none',\n\t'unsafe-inline',\n\t'unsafe-eval',\n\t'unsafe-hashes',\n\t'strict-dynamic',\n\t'wasm-unsafe-eval',\n\t'report-sample',\n\t'inline-speculation-rules',\n]);\n\n/** A keyword, or a nonce or hash, written without its quotes. */\nfunction isUnquotedKeyword(source: string): boolean {\n\tconst lower = source.toLowerCase();\n\treturn (\n\t\tKEYWORDS.has(lower) ||\n\t\t/^(nonce|sha256|sha384|sha512)-[a-z0-9+/_-]+=*$/.test(lower)\n\t);\n}\n\n/**\n * The policy, written in insertion order: `name src src; name src`. `NONCE`\n * passes through as it is, for `secureHeaders({ nonce: true })` to replace.\n * Throws a `TypeError` at startup on a policy a browser would misread, such\n * as a keyword written without its quotes (`self` for `'self'`).\n *\n * ```ts\n * secureHeaders({\n * nonce: true,\n * contentSecurityPolicy: csp({\n * 'default-src': [\"'self'\"],\n * 'script-src': [\"'self'\", NONCE],\n * 'upgrade-insecure-requests': true,\n * }),\n * });\n * ```\n */\nexport function csp(directives: CspDirectives): string {\n\tconst written: string[] = [];\n\tfor (const [name, value] of Object.entries(directives)) {\n\t\tif (!/^[a-z][a-z0-9-]*$/.test(name)) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`csp(): \"${name}\" is not a directive name: write lowercase letters, digits and hyphens, starting with a letter, as in \"script-src\"`,\n\t\t\t);\n\t\t}\n\t\tif (value === undefined || value === false) continue;\n\t\tif (value === true) {\n\t\t\twritten.push(name);\n\t\t\tcontinue;\n\t\t}\n\t\tif (value.length === 0) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`csp(): ${name} has no source: write [\"'none'\"] to allow nothing, or false to leave it out`,\n\t\t\t);\n\t\t}\n\t\tfor (const source of value) {\n\t\t\tif (/[;,\\s]/.test(source) || source === '') {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`csp(): ${name} has the source \"${source}\", which is empty or holds \";\", \",\" or a space: a source is one token, so split a list into several sources, and write a comma inside a URL as %2C`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\tfor (const source of value) {\n\t\t\tif (isUnquotedKeyword(source)) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`csp(): ${source} in ${name} is a keyword: write it quoted, '${source}'`,\n\t\t\t\t);\n\t\t\t}\n\t\t}\n\t\twritten.push(`${name} ${value.join(' ')}`);\n\t}\n\tif (written.length === 0) {\n\t\tthrow new TypeError(\n\t\t\t'csp(): no directive is left: give at least one, such as { \"default-src\": [\"\\'none\\'\"] }',\n\t\t);\n\t}\n\treturn written.join('; ');\n}\n",
7
+ "/**\n * The per-request nonce of `secureHeaders({ nonce: true })`: where it goes\n * in the policy, and how one is made.\n */\n\n/**\n * Where the nonce goes in `contentSecurityPolicy`: each occurrence becomes\n * `'nonce-<value>'`, the request's own value. Without it, `nonce: true`\n * adds the nonce to `script-src` and `script-src-elem`.\n *\n * ```ts\n * secureHeaders({\n * nonce: true,\n * contentSecurityPolicy: `default-src 'self'; style-src 'self' ${NONCE}`,\n * });\n * ```\n */\nexport const NONCE = \"'nonce-{alxia}'\";\n\n/** What `secureHeaders({ nonce: true })` adds to the context of the routes after it. */\nexport interface NonceContext {\n\t/**\n\t * This request's nonce, base64, the one its `Content-Security-Policy`\n\t * allows: what a page puts on its `<script nonce>`.\n\t */\n\treadonly nonce: string;\n}\n\n/** The directives the nonce is added to when the policy does not place it. */\nconst SCRIPT_DIRECTIVES = new Set(['script-src', 'script-src-elem']);\n\n/**\n * The policy as a function of the nonce: split where `NONCE` stands, or\n * where it is added to the script directives. Throws when there is\n * nowhere to put it.\n */\nexport function policyWithNonce(policy: string): (nonce: string) => string {\n\tconst placed = policy.includes(NONCE)\n\t\t? policy\n\t\t: policy\n\t\t\t\t.split(';')\n\t\t\t\t.map((directive) => {\n\t\t\t\t\tconst name = directive.trim().split(/\\s+/)[0]?.toLowerCase() ?? '';\n\t\t\t\t\treturn SCRIPT_DIRECTIVES.has(name)\n\t\t\t\t\t\t? `${directive.trimEnd()} ${NONCE}`\n\t\t\t\t\t\t: directive;\n\t\t\t\t})\n\t\t\t\t.join(';');\n\tif (!placed.includes(NONCE)) {\n\t\tthrow new TypeError(\n\t\t\t`secureHeaders: nonce is on, but the content-security-policy has no script-src to add it to: write one, or place NONCE where the nonce goes (the policy is \"${policy}\")`,\n\t\t);\n\t}\n\tconst parts = placed.split(NONCE);\n\treturn (nonce) => parts.join(`'nonce-${nonce}'`);\n}\n\n/** 128 random bits, base64: a fresh nonce. */\nexport function freshNonce(): string {\n\tconst bytes = crypto.getRandomValues(new Uint8Array(16));\n\treturn btoa(String.fromCharCode(...bytes));\n}\n",
8
+ "/**\n * `pageHeaders`: the headers of a server-rendered page that carries a\n * nonce, as one preset over `secureHeaders`.\n */\nimport { markFactory } from '@softistx/alxia';\nimport { type CspDirectives, csp } from './csp';\nimport { NONCE } from './nonce';\nimport {\n\ttype NonceMiddleware,\n\ttype SecureHeadersOptions,\n\tsecureHeaders,\n} from './secure-headers';\n\n/**\n * A strict policy for server-rendered pages with a nonce, one entry per\n * directive, each with why it holds what it holds. A new source goes in the\n * directive that loads it (through `pageHeaders`' `directives`), never in\n * `default-src`, and never `'unsafe-inline'` or `'unsafe-eval'` in `script-src`.\n */\nconst PAGE_POLICY = {\n\t// Anything a directive below does not name: this site only.\n\t'default-src': [\"'self'\"],\n\t// Only the scripts this response's nonce marks (the framework's inline\n\t// ones and its entry module), and whatever they load ('strict-dynamic':\n\t// the route chunks they import). 'self' is a fallback for browsers\n\t// without 'strict-dynamic'; they ignore it when a nonce is present.\n\t'script-src': [\"'self'\", NONCE, \"'strict-dynamic'\"],\n\t// <style> elements: this site's stylesheets and the ones that carry this\n\t// response's nonce (a UI library's nonced style elements, a dev client's).\n\t// No 'unsafe-inline' here: an injected <style> is the CSS attack\n\t// (attribute selectors reading a value out).\n\t'style-src-elem': [\"'self'\", NONCE],\n\t// `style` attributes (custom properties, positions, a UI library's and\n\t// React's own): no nonce covers an attribute, 'unsafe-inline' is the only\n\t// way. It allows no <style> element and no script.\n\t'style-src-attr': [\"'unsafe-inline'\"],\n\t// The icons, sprites and logo. No data: or blob: until a feature needs one.\n\t'img-src': [\"'self'\"],\n\t// Self-hosted fonts.\n\t'font-src': [\"'self'\"],\n\t// The framework's data requests, only to this site. In development, the\n\t// dev server's WebSocket too: 'self' covers ws: on the page's own host.\n\t'connect-src': [\"'self'\"],\n\t// The service worker, served by this site.\n\t'worker-src': [\"'self'\"],\n\t// The web app manifest, a route of this site.\n\t'manifest-src': [\"'self'\"],\n\t// No plugin content.\n\t'object-src': [\"'none'\"],\n\t// No `<base>`: a relative URL always means this site.\n\t'base-uri': [\"'none'\"],\n\t// Forms post to this site only (a submit before hydration included).\n\t'form-action': [\"'self'\"],\n\t// No page frames this one (clickjacking); X-Frame-Options covers older\n\t// browsers.\n\t'frame-ancestors': [\"'none'\"],\n} as const satisfies CspDirectives;\n\n/** `PAGE_POLICY`, frozen with each of its lists. */\nexport const PAGE_DIRECTIVES: typeof PAGE_POLICY = Object.freeze(\n\tObject.fromEntries(\n\t\tObject.entries(PAGE_POLICY).map(([name, sources]) => [\n\t\t\tname,\n\t\t\tObject.freeze(sources),\n\t\t]),\n\t),\n) as typeof PAGE_POLICY;\n\n/** Directives a page policy cannot do without. */\nconst REQUIRED = {\n\t'default-src': 'it covers frame, media and child sources',\n\t'base-uri':\n\t\t'it has no fallback, so an injected <base> would redirect nonced relative scripts',\n\t'object-src': 'it blocks plugin content',\n} as const;\n\nexport interface PageHeadersOptions\n\textends Omit<SecureHeadersOptions, 'contentSecurityPolicy'> {\n\t/**\n\t * Served over HTTPS: sends HSTS and adds `upgrade-insecure-requests`.\n\t * Required, and never read from the environment: `import.meta.env.PROD`\n\t * or your own flag.\n\t */\n\treadonly https: boolean;\n\t/**\n\t * Merged over `PAGE_DIRECTIVES`: a list replaces the preset's, `false`\n\t * removes a directive. An `upgrade-insecure-requests` given here wins\n\t * over `https`.\n\t */\n\treadonly directives?: CspDirectives;\n}\n\n/**\n * `secureHeaders({ nonce: true, … })` for server-rendered pages: the\n * `PAGE_DIRECTIVES` policy with a nonce per request, HSTS and\n * `upgrade-insecure-requests` only with `https`, `Referrer-Policy:\n * strict-origin-when-cross-origin`, and `Cache-Control: private, no-store`\n * (a cache must never store a nonce). Any other option passes through to\n * `secureHeaders`, and wins over the preset's.\n *\n * ```ts\n * app.use(pageHeaders({ https: import.meta.env.PROD }));\n * ```\n *\n * Throws a `TypeError` when `directives` removes `default-src`, `base-uri`\n * or `object-src`, and when the merged `script-src`, or a `script-src-elem`\n * given as a list, lacks `NONCE`: the page would not hydrate.\n */\nexport function pageHeaders(options: PageHeadersOptions): NonceMiddleware {\n\tconst { https, directives: asGiven = {}, ...given } = options;\n\tconst defined = (entries: object) =>\n\t\tObject.fromEntries(\n\t\t\tObject.entries(entries).filter(([, value]) => value !== undefined),\n\t\t);\n\tconst rest = defined(given) as typeof given;\n\tconst directives: CspDirectives = defined(asGiven);\n\tfor (const [name, why] of Object.entries(REQUIRED)) {\n\t\tconst value = directives[name];\n\t\tif (value === false || value === true) {\n\t\t\tthrow new TypeError(\n\t\t\t\t`pageHeaders(): ${name} cannot be removed from a page policy: ${why}, so give it a list of sources`,\n\t\t\t);\n\t\t}\n\t}\n\tconst merged: CspDirectives = {\n\t\t...PAGE_DIRECTIVES,\n\t\t...directives,\n\t\t'upgrade-insecure-requests':\n\t\t\tdirectives['upgrade-insecure-requests'] ?? https,\n\t};\n\tconst scripts = merged['script-src'];\n\tif (!Array.isArray(scripts) || !scripts.includes(NONCE)) {\n\t\tthrow new TypeError(\n\t\t\t\"pageHeaders(): script-src has no NONCE: a page script runs only with the response's nonce, so write script-src with NONCE in directives, such as [\\\"'self'\\\", NONCE, \\\"'strict-dynamic'\\\"]\",\n\t\t);\n\t}\n\tconst elements = merged['script-src-elem'];\n\tif (Array.isArray(elements) && !elements.includes(NONCE)) {\n\t\tthrow new TypeError(\n\t\t\t'pageHeaders(): script-src-elem has no NONCE: script elements follow it instead of script-src, so add NONCE to it, or give false to remove it',\n\t\t);\n\t}\n\treturn secureHeaders({\n\t\t...(https ? {} : { strictTransportSecurity: false }),\n\t\treferrerPolicy: 'strict-origin-when-cross-origin',\n\t\tcacheControl: 'private, no-store',\n\t\t...rest,\n\t\tnonce: true,\n\t\tcontentSecurityPolicy: csp(merged),\n\t});\n}\n\nmarkFactory(pageHeaders);\n",
9
+ "import {\n\tdefineMiddleware,\n\ttype Empty,\n\ttype Middleware,\n\tmarkFactory,\n\ttype Next,\n\tsettle,\n\twithHeaders,\n} from '@softistx/alxia';\nimport { freshNonce, NONCE, type NonceContext, policyWithNonce } from './nonce';\n\n/** A header's value, or `false` to leave it out. An empty value is refused. */\nexport type Setting = string | false;\n\nexport interface SecureHeadersOptions {\n\t/** Defaults to a policy for an API: nothing loads, nothing frames it. */\n\treadonly contentSecurityPolicy?: Setting;\n\treadonly strictTransportSecurity?: Setting;\n\treadonly xContentTypeOptions?: Setting;\n\treadonly xFrameOptions?: Setting;\n\treadonly referrerPolicy?: Setting;\n\treadonly crossOriginOpenerPolicy?: Setting;\n\treadonly crossOriginResourcePolicy?: Setting;\n\treadonly crossOriginEmbedderPolicy?: Setting;\n\treadonly originAgentCluster?: Setting;\n\treadonly xDnsPrefetchControl?: Setting;\n\treadonly xPermittedCrossDomainPolicies?: Setting;\n\t/** Off by default: a policy is the app's to write. */\n\treadonly permissionsPolicy?: Setting;\n\t/**\n\t * Off by default. `'private, no-store'` for pages that carry a nonce or a\n\t * viewer's data: a cache must never store them.\n\t */\n\treadonly cacheControl?: Setting;\n\t/** Whether `X-Powered-By` and `Server` are removed. On by default. */\n\treadonly hidePoweredBy?: boolean;\n}\n\n/**\n * The nonce switch, beside `SecureHeadersOptions` rather than in it, so that\n * options typed by that interface still pick the plugin without a nonce.\n */\ninterface NonceOption {\n\t/**\n\t * A fresh nonce for each request, in `contentSecurityPolicy` and on the\n\t * context as `nonce`. Placed where the policy names `NONCE`, or added to\n\t * its `script-src`. Off by default.\n\t */\n\treadonly nonce?: boolean;\n}\n\n/**\n * What `secureHeaders()` returns: a middleware, for `app.use`, that sets\n * the headers on every response after it.\n */\nexport type SecureHeaders = Middleware<Empty, Promise<Response>>;\n\n/**\n * What `secureHeaders({ nonce: true })` returns: a middleware, for\n * `app.use`, whose routes after it read `nonce`, and which sets the header\n * with the same one.\n */\nexport type NonceMiddleware = Middleware<Empty, Promise<Next<NonceContext>>>;\n\nconst DEFAULTS = {\n\t'content-security-policy':\n\t\t\"default-src 'none'; base-uri 'none'; form-action 'none'; frame-ancestors 'none'\",\n\t'strict-transport-security': 'max-age=31536000; includeSubDomains',\n\t'x-content-type-options': 'nosniff',\n\t'x-frame-options': 'DENY',\n\t'referrer-policy': 'no-referrer',\n\t'cross-origin-opener-policy': 'same-origin',\n\t'cross-origin-resource-policy': 'same-origin',\n\t'cross-origin-embedder-policy': false,\n\t'origin-agent-cluster': '?1',\n\t'x-dns-prefetch-control': 'off',\n\t'x-permitted-cross-domain-policies': 'none',\n\t'permissions-policy': false,\n\t'cache-control': false,\n} as const satisfies Record<string, Setting>;\n\nconst OPTION: Record<keyof typeof DEFAULTS, keyof SecureHeadersOptions> = {\n\t'content-security-policy': 'contentSecurityPolicy',\n\t'strict-transport-security': 'strictTransportSecurity',\n\t'x-content-type-options': 'xContentTypeOptions',\n\t'x-frame-options': 'xFrameOptions',\n\t'referrer-policy': 'referrerPolicy',\n\t'cross-origin-opener-policy': 'crossOriginOpenerPolicy',\n\t'cross-origin-resource-policy': 'crossOriginResourcePolicy',\n\t'cross-origin-embedder-policy': 'crossOriginEmbedderPolicy',\n\t'origin-agent-cluster': 'originAgentCluster',\n\t'x-dns-prefetch-control': 'xDnsPrefetchControl',\n\t'x-permitted-cross-domain-policies': 'xPermittedCrossDomainPolicies',\n\t'permissions-policy': 'permissionsPolicy',\n\t'cache-control': 'cacheControl',\n};\n\n/**\n * Secure headers on every response, as a middleware: each response of the\n * routes after it and of a request no route matches — a 404, an error —\n * gets them. A header a route set itself is kept: a page that needs its\n * own `Content-Security-Policy` — `@softistx/alxia-graphql`'s IDE — sets it.\n *\n * ```ts\n * app.use(secureHeaders({ contentSecurityPolicy: \"default-src 'self'\" }));\n * ```\n *\n * With `nonce: true`, each request gets a nonce of its own, in the policy\n * and on the context of the routes declared after it:\n *\n * ```ts\n * app\n * .use(secureHeaders({ nonce: true, contentSecurityPolicy: \"script-src 'self'\" }))\n * .get('/', ({ nonce, reply }) => reply(200, `<script nonce=\"${nonce}\">…</script>`));\n * ```\n */\nexport function secureHeaders(\n\toptions: SecureHeadersOptions & { readonly nonce: true },\n): NonceMiddleware;\nexport function secureHeaders(\n\toptions?: SecureHeadersOptions & { readonly nonce?: false },\n): SecureHeaders;\nexport function secureHeaders(\n\toptions: SecureHeadersOptions & NonceOption = {},\n): SecureHeaders | NonceMiddleware {\n\tconst headers = fixedHeaders(options);\n\tconst hide = options.hidePoweredBy ?? true;\n\tconst policy = headers.get('content-security-policy');\n\tif (!options.nonce) {\n\t\tif (policy?.includes(NONCE)) {\n\t\t\tthrow new TypeError(\n\t\t\t\t'secureHeaders: contentSecurityPolicy names NONCE, but nonce is off: give nonce: true',\n\t\t\t);\n\t\t}\n\t\tconst set = setter(headers, hide);\n\t\treturn defineMiddleware(async function secureHeaders(ctx, next) {\n\t\t\treturn set(await settle(ctx, next()));\n\t\t});\n\t}\n\tif (policy === undefined) {\n\t\tthrow new TypeError(\n\t\t\t'secureHeaders: nonce is on, but contentSecurityPolicy is false: a nonce is only read through the content-security-policy header',\n\t\t);\n\t}\n\theaders.delete('content-security-policy');\n\tconst withNonce = policyWithNonce(policy);\n\tconst set = setter(headers, hide);\n\treturn defineMiddleware(async function secureHeaders(ctx, next) {\n\t\tconst added: NonceContext = { nonce: freshNonce() };\n\t\tconst response = await settle(ctx, next(added));\n\t\treturn set(response, withNonce(added.nonce)) as typeof response;\n\t});\n}\n\n/** Each header the options leave in, by name, with its value. Throws on an empty one. */\nfunction fixedHeaders(options: SecureHeadersOptions): Map<string, string> {\n\tconst headers = new Map<string, string>();\n\tfor (const [name, fallback] of Object.entries(DEFAULTS) as [\n\t\tkeyof typeof DEFAULTS,\n\t\tSetting,\n\t][]) {\n\t\tconst setting = options[OPTION[name]];\n\t\tif (typeof setting === 'string' && setting.trim() === '') {\n\t\t\t// An empty header says nothing a browser can act on: leave it out.\n\t\t\tthrow new TypeError(\n\t\t\t\t`secureHeaders: ${OPTION[name]} is empty; give false to leave the ${name} header out`,\n\t\t\t);\n\t\t}\n\t\tconst value = setting === undefined ? fallback : setting;\n\t\tif (typeof value === 'string') headers.set(name, value);\n\t}\n\treturn headers;\n}\n\n/** Every header the response lacks, then the policy given per request. */\nfunction setter(headers: ReadonlyMap<string, string>, hide: boolean) {\n\treturn (response: Response, policy?: string) =>\n\t\twithHeaders(response, (current) => {\n\t\t\tfor (const [name, value] of headers) {\n\t\t\t\tif (!current.has(name)) current.set(name, value);\n\t\t\t}\n\t\t\tif (policy !== undefined && !current.has('content-security-policy')) {\n\t\t\t\tcurrent.set('content-security-policy', policy);\n\t\t\t}\n\t\t\tif (hide) {\n\t\t\t\tcurrent.delete('x-powered-by');\n\t\t\t\tcurrent.delete('server');\n\t\t\t}\n\t\t});\n}\n\nmarkFactory(secureHeaders);\n"
10
+ ],
11
+ "mappings": ";AAIA;AAAA;AAAA;AAAA;AAAA;AAAA;AASA,IAAM,QAAQ;AAAA,EACb;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACD;AA8BA,SAAS,MAAM,CAAC,OAAkC;AAAA,EACjD,OAAO,MAAM,IAAI,CAAC,SAAS,IAAI,OAAO,EAAE,KAAK,IAAI;AAAA;AAQlD,SAAS,WAAW,CACnB,SACA,KACU;AAAA,EACV,MAAM,OAAO,QAAQ,QAAQ,IAAI,gBAAgB;AAAA,EACjD,IAAI,SAAS;AAAA,IAAM,OAAO,SAAS,iBAAiB,SAAS;AAAA,EAC7D,MAAM,SAAS,QAAQ,QAAQ,IAAI,QAAQ;AAAA,EAC3C,IAAI,WAAW;AAAA,IAAM,OAAO;AAAA,EAC5B,MAAM,MAAM,OAAO,QAAQ,aAAa,IAAI,IAAI;AAAA,EAChD,OAAO,YAAY,OAAO,IAAI,IAAI,QAAQ,GAAG,EAAE;AAAA;AAmBzC,SAAS,aAAa,CAC5B,OACA,UAAgC,CAAC,GACjB;AAAA,EAChB,IAAI,MAAM,W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1,34 @@
1
+ /**
2
+ * The per-request nonce of `secureHeaders({ nonce: true })`: where it goes
3
+ * in the policy, and how one is made.
4
+ */
5
+ /**
6
+ * Where the nonce goes in `contentSecurityPolicy`: each occurrence becomes
7
+ * `'nonce-<value>'`, the request's own value. Without it, `nonce: true`
8
+ * adds the nonce to `script-src` and `script-src-elem`.
9
+ *
10
+ * ```ts
11
+ * secureHeaders({
12
+ * nonce: true,
13
+ * contentSecurityPolicy: `default-src 'self'; style-src 'self' ${NONCE}`,
14
+ * });
15
+ * ```
16
+ */
17
+ export declare const NONCE = "'nonce-{alxia}'";
18
+ /** What `secureHeaders({ nonce: true })` adds to the context of the routes after it. */
19
+ export interface NonceContext {
20
+ /**
21
+ * This request's nonce, base64, the one its `Content-Security-Policy`
22
+ * allows: what a page puts on its `<script nonce>`.
23
+ */
24
+ readonly nonce: string;
25
+ }
26
+ /**
27
+ * The policy as a function of the nonce: split where `NONCE` stands, or
28
+ * where it is added to the script directives. Throws when there is
29
+ * nowhere to put it.
30
+ */
31
+ export declare function policyWithNonce(policy: string): (nonce: string) => string;
32
+ /** 128 random bits, base64: a fresh nonce. */
33
+ export declare function freshNonce(): string;
34
+ //# sourceMappingURL=nonce.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nonce.d.ts","sourceRoot":"","sources":["../src/nonce.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,KAAK,oBAAoB,CAAC;AAEvC,wFAAwF;AACxF,MAAM,WAAW,YAAY;IAC5B;;;OAGG;IACH,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACvB;AAKD;;;;GAIG;AACH,wBAAgB,eAAe,CAAC,MAAM,EAAE,MAAM,GAAG,CAAC,KAAK,EAAE,MAAM,KAAK,MAAM,CAmBzE;AAED,8CAA8C;AAC9C,wBAAgB,UAAU,IAAI,MAAM,CAGnC"}
package/dist/page.d.ts ADDED
@@ -0,0 +1,58 @@
1
+ import { type CspDirectives } from './csp';
2
+ import { type NonceMiddleware, type SecureHeadersOptions } from './secure-headers';
3
+ /**
4
+ * A strict policy for server-rendered pages with a nonce, one entry per
5
+ * directive, each with why it holds what it holds. A new source goes in the
6
+ * directive that loads it (through `pageHeaders`' `directives`), never in
7
+ * `default-src`, and never `'unsafe-inline'` or `'unsafe-eval'` in `script-src`.
8
+ */
9
+ declare const PAGE_POLICY: {
10
+ readonly 'default-src': readonly ["'self'"];
11
+ readonly 'script-src': readonly ["'self'", "'nonce-{alxia}'", "'strict-dynamic'"];
12
+ readonly 'style-src-elem': readonly ["'self'", "'nonce-{alxia}'"];
13
+ readonly 'style-src-attr': readonly ["'unsafe-inline'"];
14
+ readonly 'img-src': readonly ["'self'"];
15
+ readonly 'font-src': readonly ["'self'"];
16
+ readonly 'connect-src': readonly ["'self'"];
17
+ readonly 'worker-src': readonly ["'self'"];
18
+ readonly 'manifest-src': readonly ["'self'"];
19
+ readonly 'object-src': readonly ["'none'"];
20
+ readonly 'base-uri': readonly ["'none'"];
21
+ readonly 'form-action': readonly ["'self'"];
22
+ readonly 'frame-ancestors': readonly ["'none'"];
23
+ };
24
+ /** `PAGE_POLICY`, frozen with each of its lists. */
25
+ export declare const PAGE_DIRECTIVES: typeof PAGE_POLICY;
26
+ export interface PageHeadersOptions extends Omit<SecureHeadersOptions, 'contentSecurityPolicy'> {
27
+ /**
28
+ * Served over HTTPS: sends HSTS and adds `upgrade-insecure-requests`.
29
+ * Required, and never read from the environment: `import.meta.env.PROD`
30
+ * or your own flag.
31
+ */
32
+ readonly https: boolean;
33
+ /**
34
+ * Merged over `PAGE_DIRECTIVES`: a list replaces the preset's, `false`
35
+ * removes a directive. An `upgrade-insecure-requests` given here wins
36
+ * over `https`.
37
+ */
38
+ readonly directives?: CspDirectives;
39
+ }
40
+ /**
41
+ * `secureHeaders({ nonce: true, … })` for server-rendered pages: the
42
+ * `PAGE_DIRECTIVES` policy with a nonce per request, HSTS and
43
+ * `upgrade-insecure-requests` only with `https`, `Referrer-Policy:
44
+ * strict-origin-when-cross-origin`, and `Cache-Control: private, no-store`
45
+ * (a cache must never store a nonce). Any other option passes through to
46
+ * `secureHeaders`, and wins over the preset's.
47
+ *
48
+ * ```ts
49
+ * app.use(pageHeaders({ https: import.meta.env.PROD }));
50
+ * ```
51
+ *
52
+ * Throws a `TypeError` when `directives` removes `default-src`, `base-uri`
53
+ * or `object-src`, and when the merged `script-src`, or a `script-src-elem`
54
+ * given as a list, lacks `NONCE`: the page would not hydrate.
55
+ */
56
+ export declare function pageHeaders(options: PageHeadersOptions): NonceMiddleware;
57
+ export {};
58
+ //# sourceMappingURL=page.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"page.d.ts","sourceRoot":"","sources":["../src/page.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,KAAK,aAAa,EAAO,MAAM,OAAO,CAAC;AAEhD,OAAO,EACN,KAAK,eAAe,EACpB,KAAK,oBAAoB,EAEzB,MAAM,kBAAkB,CAAC;AAE1B;;;;;GAKG;AACH,QAAA,MAAM,WAAW;;;;;;;;;;;;;;CAqCiB,CAAC;AAEnC,oDAAoD;AACpD,eAAO,MAAM,eAAe,EAAE,OAAO,WAOd,CAAC;AAUxB,MAAM,WAAW,kBAChB,SAAQ,IAAI,CAAC,oBAAoB,EAAE,uBAAuB,CAAC;IAC3D;;;;OAIG;IACH,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;IACxB;;;;OAIG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,aAAa,CAAC;CACpC;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,WAAW,CAAC,OAAO,EAAE,kBAAkB,GAAG,eAAe,CA0CxE"}
@@ -0,0 +1,64 @@
1
+ import { type Empty, type Middleware, type Next } from '@softistx/alxia';
2
+ import { type NonceContext } from './nonce';
3
+ /** A header's value, or `false` to leave it out. An empty value is refused. */
4
+ export type Setting = string | false;
5
+ export interface SecureHeadersOptions {
6
+ /** Defaults to a policy for an API: nothing loads, nothing frames it. */
7
+ readonly contentSecurityPolicy?: Setting;
8
+ readonly strictTransportSecurity?: Setting;
9
+ readonly xContentTypeOptions?: Setting;
10
+ readonly xFrameOptions?: Setting;
11
+ readonly referrerPolicy?: Setting;
12
+ readonly crossOriginOpenerPolicy?: Setting;
13
+ readonly crossOriginResourcePolicy?: Setting;
14
+ readonly crossOriginEmbedderPolicy?: Setting;
15
+ readonly originAgentCluster?: Setting;
16
+ readonly xDnsPrefetchControl?: Setting;
17
+ readonly xPermittedCrossDomainPolicies?: Setting;
18
+ /** Off by default: a policy is the app's to write. */
19
+ readonly permissionsPolicy?: Setting;
20
+ /**
21
+ * Off by default. `'private, no-store'` for pages that carry a nonce or a
22
+ * viewer's data: a cache must never store them.
23
+ */
24
+ readonly cacheControl?: Setting;
25
+ /** Whether `X-Powered-By` and `Server` are removed. On by default. */
26
+ readonly hidePoweredBy?: boolean;
27
+ }
28
+ /**
29
+ * What `secureHeaders()` returns: a middleware, for `app.use`, that sets
30
+ * the headers on every response after it.
31
+ */
32
+ export type SecureHeaders = Middleware<Empty, Promise<Response>>;
33
+ /**
34
+ * What `secureHeaders({ nonce: true })` returns: a middleware, for
35
+ * `app.use`, whose routes after it read `nonce`, and which sets the header
36
+ * with the same one.
37
+ */
38
+ export type NonceMiddleware = Middleware<Empty, Promise<Next<NonceContext>>>;
39
+ /**
40
+ * Secure headers on every response, as a middleware: each response of the
41
+ * routes after it and of a request no route matches — a 404, an error —
42
+ * gets them. A header a route set itself is kept: a page that needs its
43
+ * own `Content-Security-Policy` — `@softistx/alxia-graphql`'s IDE — sets it.
44
+ *
45
+ * ```ts
46
+ * app.use(secureHeaders({ contentSecurityPolicy: "default-src 'self'" }));
47
+ * ```
48
+ *
49
+ * With `nonce: true`, each request gets a nonce of its own, in the policy
50
+ * and on the context of the routes declared after it:
51
+ *
52
+ * ```ts
53
+ * app
54
+ * .use(secureHeaders({ nonce: true, contentSecurityPolicy: "script-src 'self'" }))
55
+ * .get('/', ({ nonce, reply }) => reply(200, `<script nonce="${nonce}">…</script>`));
56
+ * ```
57
+ */
58
+ export declare function secureHeaders(options: SecureHeadersOptions & {
59
+ readonly nonce: true;
60
+ }): NonceMiddleware;
61
+ export declare function secureHeaders(options?: SecureHeadersOptions & {
62
+ readonly nonce?: false;
63
+ }): SecureHeaders;
64
+ //# sourceMappingURL=secure-headers.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"secure-headers.d.ts","sourceRoot":"","sources":["../src/secure-headers.ts"],"names":[],"mappings":"AAAA,OAAO,EAEN,KAAK,KAAK,EACV,KAAK,UAAU,EAEf,KAAK,IAAI,EAGT,MAAM,iBAAiB,CAAC;AACzB,OAAO,EAAqB,KAAK,YAAY,EAAmB,MAAM,SAAS,CAAC;AAEhF,+EAA+E;AAC/E,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG,KAAK,CAAC;AAErC,MAAM,WAAW,oBAAoB;IACpC,yEAAyE;IACzE,QAAQ,CAAC,qBAAqB,CAAC,EAAE,OAAO,CAAC;IACzC,QAAQ,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAC3C,QAAQ,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IACvC,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;IACjC,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,CAAC;IAClC,QAAQ,CAAC,uBAAuB,CAAC,EAAE,OAAO,CAAC;IAC3C,QAAQ,CAAC,yBAAyB,CAAC,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,yBAAyB,CAAC,EAAE,OAAO,CAAC;IAC7C,QAAQ,CAAC,kBAAkB,CAAC,EAAE,OAAO,CAAC;IACtC,QAAQ,CAAC,mBAAmB,CAAC,EAAE,OAAO,CAAC;IACvC,QAAQ,CAAC,6BAA6B,CAAC,EAAE,OAAO,CAAC;IACjD,sDAAsD;IACtD,QAAQ,CAAC,iBAAiB,CAAC,EAAE,OAAO,CAAC;IACrC;;;OAGG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,OAAO,CAAC;IAChC,sEAAsE;IACtE,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAC;CACjC;AAeD;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;AAEjE;;;;GAIG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,CAAC,KAAK,EAAE,OAAO,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;AAmC7E;;;;;;;;;;;;;;;;;;GAkBG;AACH,wBAAgB,aAAa,CAC5B,OAAO,EAAE,oBAAoB,GAAG;IAAE,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAA;CAAE,GACtD,eAAe,CAAC;AACnB,wBAAgB,aAAa,CAC5B,OAAO,CAAC,EAAE,oBAAoB,GAAG;IAAE,QAAQ,CAAC,KAAK,CAAC,EAAE,KAAK,CAAA;CAAE,GACzD,aAAa,CAAC"}
package/docs/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # @softistx/alxia-secure-headers documentation
2
+
3
+ The [package README](../README.md) is the short version. This folder is
4
+ the long one: every option with its default, which responses get the
5
+ headers, and what to do when a browser refuses what they forbid.
6
+
7
+ | Page | Read it when |
8
+ | --- | --- |
9
+ | [Guide](guide.md) | changing or dropping a header, a nonce per request, writing the policy as directives, the page preset, giving a page its own policy, keeping a page out of shared caches, clearing the browser's cache on sign-out, ordering it with other middlewares, or testing the headers |
10
+ | [Troubleshooting](troubleshooting.md) | something went wrong and you have the message: a TypeScript error, a browser refusal, a header that is or is not there |
11
+ | [Roadmap](roadmap.md) | wondering what is coming, and what is not planned |