Security audit fixes for 1.18.0: Markdown negotiation private + Vary: Accept (F3), public lists capped (F4), operator sort names refused (F5), CSP report log normalised and capped (F1), buildUrlField and the reference socials (F2), document locks for staff only

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 16:56:09 +01:00
co-authored by Claude Opus 5.5
parent d2f19095dc
commit e9f9947eb7
20 changed files with 677 additions and 49 deletions
+79 -6
View File
@@ -1,3 +1,4 @@
import { clientIp, consumeRateLimit } from '../shared/rateLimit.js'
import { readCappedBody } from '../shared/readCappedBody.js'
/** One CSP violation, normalised from either report format. */
@@ -15,12 +16,79 @@ export type CspViolation = {
export type CreateCspReportRouteArgs = {
/** Largest accepted body in bytes; bigger requests get 413. Default 16 KB. */
maxBytes?: number
/** Most violations handled from one request (a body may hold many). Default 10; the rest are dropped. */
maxReports?: number
/**
* What to do with each violation. Default: one `console.warn` line per
* violation (Coolify keeps container logs — read them there). Plug in
* payload.logger, Sentry or a database write here.
* violation (Coolify keeps container logs — read them there), made by
* {@link cspLogLine}: origins and paths only, no control characters, every value
* capped — a report cannot write a line of its own into the log. Your own
* `onReport` gets the values exactly as the browser sent them: escape them
* before you log them. Plug in payload.logger, Sentry or a database write here.
*/
onReport?: (violation: CspViolation) => Promise<void> | void
/**
* Requests per client (`clientIp`, IPv6 per /64) and window that are handled; over it the route still answers 204
* and handles nothing. Default `{ max: 60, windowMs: 60_000 }`; `false` = no limit.
*/
rateLimit?: { max?: number; windowMs?: number } | false
}
/** Longest value {@link cspLogLine} writes. */
export const CSP_LOG_VALUE_MAX = 128
/** Control characters (new lines among them) and the Unicode line separators: never in a log line. */
// eslint-disable-next-line no-control-regex
const CONTROL = /[\u0000-\u001f\u007f-\u009f\u2028\u2029]/g
const clean = (value: string | undefined, max = CSP_LOG_VALUE_MAX): string => {
if (!value) {return '?'}
const text = value.replace(CONTROL, ' ').trim()
return text.length > max ? `${text.slice(0, max)}…` : text || '?'
}
/** `https://cdn.x/a.js?q` → `https://cdn.x`; a keyword (`inline`, `eval`, `data`) stays as it is. */
const originOf = (value: string | undefined): string | undefined => {
if (!value) {return undefined}
try {
const url = new URL(value)
return url.origin === 'null' ? `${url.protocol}` : url.origin
} catch {
return value
}
}
/** `https://site.pl/pl/o-nas?x=1#y` → `/pl/o-nas` (no query: it may hold personal data). */
const pathOf = (value: string | undefined): string | undefined => {
if (!value) {return undefined}
try {
return new URL(value).pathname
} catch {
return value.split(/[?#]/)[0]
}
}
/** `https://cdn.x/a.js?q` → `https://cdn.x/a.js`: where the script lives, without its query. */
const scriptOf = (value: string): string => {
try {
const url = new URL(value)
return url.origin === 'null' ? url.protocol : `${url.origin}${url.pathname}`
} catch {
return value.split(/[?#]/)[0]
}
}
/**
* The default log line of one violation: the blocked resource's origin, the page's path, the script's origin and path
* with its line — control characters removed, every value at most {@link CSP_LOG_VALUE_MAX} characters (security
* audit 2026-12, F1). Use it in your own `onReport` for the same line.
*/
export function cspLogLine(v: CspViolation): string {
const source = v.sourceFile ? scriptOf(v.sourceFile) : ''
return (
`[csp] ${clean(v.disposition ?? 'report', 16)} ${clean(v.effectiveDirective, 32)} blocked=${clean(originOf(v.blockedUri))} ` +
`page=${clean(pathOf(v.documentUri))}${source ? ` src=${clean(source)}:${typeof v.lineNumber === 'number' ? v.lineNumber : ''}` : ''}`
)
}
type Raw = Record<string, unknown>
@@ -86,14 +154,19 @@ export function parseCspReports(body: unknown): CspViolation[] {
export function createCspReportRoute(args: CreateCspReportRouteArgs = {}): (request: Request) => Promise<Response> {
const {
maxBytes = 16 * 1024,
maxReports = 10,
onReport = (v: CspViolation) => {
console.warn(
`[csp] ${v.disposition ?? 'report'} ${v.effectiveDirective ?? '?'} blocked=${v.blockedUri ?? '?'} page=${v.documentUri ?? '?'}${v.sourceFile ? ` src=${v.sourceFile}:${v.lineNumber ?? ''}` : ''}`,
)
console.warn(cspLogLine(v))
},
rateLimit = {},
} = args
return async (request: Request) => {
if (rateLimit) {
const { ok } = await consumeRateLimit({ key: clientIp(request), max: rateLimit.max ?? 60, scope: 'csp-report', windowMs: rateLimit.windowMs ?? 60_000 })
// the browser does not read the answer: 204 either way, nothing handled
if (!ok) {return new Response(null, { status: 204 })}
}
// read only up to the cap, whether or not the browser declared the length (a chunked body has none)
let text: null | string
try {
@@ -110,7 +183,7 @@ export function createCspReportRoute(args: CreateCspReportRouteArgs = {}): (requ
return new Response(null, { status: 400 })
}
for (const violation of parseCspReports(body).slice(0, 50)) {
for (const violation of parseCspReports(body).slice(0, Math.max(0, maxReports))) {
try {
await onReport(violation)
} catch {