Merge claude/security-b-content-state-cc308e: sizes before work, one link rule, no stand-in locale, strict switches, write-only secrets cannot be queried
AGENT.md conflict: the 2FA rows from A, the stored-secrets row from B (its check answers 400 QueryError, not 403). Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
import type { SecurityHeader } from './buildSecurityHeaders.js'
|
||||
|
||||
import { assertBooleanSwitch, assertOneOf } from '../shared/switches.js'
|
||||
|
||||
export type CspDirective =
|
||||
| 'base-uri'
|
||||
| 'child-src'
|
||||
@@ -76,6 +78,26 @@ export type BuildCspArgs = {
|
||||
export const DEFAULT_CSP_REPORT_GROUP = 'csp-endpoint'
|
||||
|
||||
/** Header name for the mode — the request header Next reads the nonce from is the same. */
|
||||
/**
|
||||
* A CSP option the policy cannot read fails at start (IAWL idea 21): `dev: 'false'` used to add 'unsafe-eval' in
|
||||
* production, and a string in `extra` / `override` was spread into single characters (a lone `*` is a wildcard).
|
||||
*/
|
||||
function assertCspArgs(args: BuildCspArgs): void {
|
||||
assertBooleanSwitch('buildCsp.dev', args.dev)
|
||||
assertBooleanSwitch('buildCsp.strictDynamic', (args as { strictDynamic?: unknown }).strictDynamic)
|
||||
assertOneOf('buildCsp.mode', args.mode, ['enforce', 'report-only'])
|
||||
for (const [key, list] of [['extra', args.extra], ['override', args.override]] as const) {
|
||||
if (list === undefined) {continue}
|
||||
if (!list || typeof list !== 'object' || Array.isArray(list)) {throw new Error(`[iapl] buildCsp.${key} must be an object of directive → array of sources.`)}
|
||||
for (const [dir, values] of Object.entries(list as Record<string, unknown>)) {
|
||||
if (values === undefined || (key === 'override' && values === null)) {continue}
|
||||
if (!Array.isArray(values) || values.some((v) => typeof v !== 'string')) {
|
||||
throw new Error(`[iapl] buildCsp.${key}['${dir}'] must be an array of sources, e.g. ['https://cdn.example.com'] (received ${JSON.stringify(values)}).`)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function cspHeaderKey(mode: BuildCspArgs['mode'] = 'enforce'): string {
|
||||
return mode === 'report-only' ? 'Content-Security-Policy-Report-Only' : 'Content-Security-Policy'
|
||||
}
|
||||
@@ -106,6 +128,7 @@ export function cspHeaderKey(mode: BuildCspArgs['mode'] = 'enforce'): string {
|
||||
* missing sources via `extra`, THEN switch to 'enforce'. See docs/security.md.
|
||||
*/
|
||||
export function buildCsp(args: BuildCspArgs = {}): { key: string; value: string } {
|
||||
assertCspArgs(args)
|
||||
const {
|
||||
analytics,
|
||||
clarity,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { BuildCspArgs } from './buildCsp.js'
|
||||
|
||||
import { isSiteNoindex } from '../seo/siteNoindex.js'
|
||||
import { assertBooleanSwitch, assertNumberSwitch, assertOneOf } from '../shared/switches.js'
|
||||
import { buildCspHeaders } from './buildCsp.js'
|
||||
|
||||
/**
|
||||
@@ -82,6 +83,18 @@ export type BuildSecurityHeadersArgs = {
|
||||
* }
|
||||
*/
|
||||
export function buildSecurityHeaders(args: BuildSecurityHeadersArgs = {}): SecurityHeader[] {
|
||||
// A value the browser would not understand fails at start instead of being sent (IAWL idea 21): `hsts: null`
|
||||
// dropped HSTS, `hstsMaxAge: 'abc'` and `frameOptions: 'false'` produced headers browsers ignore.
|
||||
for (const name of ['hsts', 'hstsIncludeSubDomains', 'hstsPreload'] as const) {assertBooleanSwitch(`buildSecurityHeaders.${name}`, args[name])}
|
||||
assertNumberSwitch('buildSecurityHeaders.hstsMaxAge', args.hstsMaxAge, { min: 0 })
|
||||
if (args.frameOptions !== null) {assertOneOf('buildSecurityHeaders.frameOptions', args.frameOptions, ['DENY', 'SAMEORIGIN'])}
|
||||
if (args.coop !== false) {assertOneOf('buildSecurityHeaders.coop', args.coop, ['same-origin', 'same-origin-allow-popups', 'noopener-allow-popups', 'unsafe-none'])}
|
||||
for (const name of ['permissionsPolicy', 'referrerPolicy'] as const) {
|
||||
const v = args[name] as unknown
|
||||
if (v !== undefined && v !== null && (typeof v !== 'string' || !v.trim() || v === 'false')) {
|
||||
throw new Error(`[iapl] buildSecurityHeaders.${name} must be a header value or null (received ${JSON.stringify(v)}).`)
|
||||
}
|
||||
}
|
||||
const {
|
||||
additional = [],
|
||||
coop = 'same-origin',
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import { readCappedBody } from '../shared/readCappedBody.js'
|
||||
|
||||
/** One CSP violation, normalised from either report format. */
|
||||
export type CspViolation = {
|
||||
blockedUri?: string
|
||||
@@ -92,15 +94,14 @@ export function createCspReportRoute(args: CreateCspReportRouteArgs = {}): (requ
|
||||
} = args
|
||||
|
||||
return async (request: Request) => {
|
||||
const length = Number(request.headers.get('content-length') ?? 0)
|
||||
if (length > maxBytes) {return new Response(null, { status: 413 })}
|
||||
let text: string
|
||||
// read only up to the cap, whether or not the browser declared the length (a chunked body has none)
|
||||
let text: null | string
|
||||
try {
|
||||
text = await request.text()
|
||||
text = await readCappedBody(request, maxBytes)
|
||||
} catch {
|
||||
return new Response(null, { status: 400 })
|
||||
}
|
||||
if (text.length > maxBytes) {return new Response(null, { status: 413 })}
|
||||
if (text === null) {return new Response(null, { status: 413 })}
|
||||
|
||||
let body: unknown
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user