Skip to content
quarry View Ticket
Login

View Ticket

2026-09-24
00:55 • Ticket [3b0939bad2] Adopt a strict CSP: datastar CSP mode, nonces, and the untrusted-expression audit status still Closed with 1 other change artifact: a264b19a53 user: zeitgeist
2026-08-31
20:33 • Closed ticket [3b0939bad2]. artifact: 53bb18256e user: ai-local
20:32 • Ticket [3b0939bad2]: 1 change artifact: 40974d4886 user: ai-local
17:12 • New ticket [3b0939bad2]. artifact: 2b2db7e735 user: ai-local

Adopt a strict CSP: datastar CSP mode, nonces, and the untrusted-expression audit

Closed3b0939bad2dd72e9fd91a979d4d2f931ae6fe7a4Created 91.3 days agoUpdated 67.9 days ago
Description2026-08-31 17:12:06
quarry sets NO page CSP today. The only Content-Security-Policy in the tree is on attachment downloads (default-src none; sandbox), which is right for uploaded files and says nothing about the app.

WHAT BLOCKS A STRICT ONE. Not the data-on:* attributes — those are data attributes, not inline handlers, and script-src does not govern them. The blocker is unsafe-eval: datastar evaluates expressions with the Function() constructor. 1.0.3 (now vendored, e13cb746) adds CSP mode, which applies a per-response nonce at expression-compile time instead, allowing script-src self nonce-... with no unsafe-eval.

quarry is well placed: one script tag, zero inline script bodies, already pinned by a test. Two genuine inline handlers remain on the clone-URL widget — onfocus=this.select() and onclick=this.previousElementSibling.select() — which ARE unsafe-inline and must move to data-on:*.

Work: (1) move those two handlers; (2) generate a cryptographically secure nonce per full-page response and put it on <html> as data-nonce; (3) set Content-Security-Policy with script-src self nonce; (4) decide the rest of the policy — img-src, style-src now that the sheet is an external asset, connect-src for SSE.

THE AUDIT, which is the real work. The datastar docs are blunt: CSP mode does NOT make expressions safe with untrusted content — it applies a nonce, it does not sanitize. quarry renders commit messages, ticket titles and forum posts from MIRRORED upstreams (sqlite, tcl, fossil), so any place an untrusted string is interpolated into a data-* expression is an injection whether or not CSP mode is on. The rule is to pass user values through signals and keep expressions static. Audit every data-* expression for interpolated content and pin the rule with a test.

Adjacent and cheap: the JS assets are Cache-Control max-age=86400 with no content hash, so they revalidate daily. The stylesheet is already hashed+immutable (eb4793ee); giving the scripts the same URL shape removes that round trip and is worth more than the 1.3KB brotli would save on the bundle.

Acceptance: a strict CSP is served on every page; no unsafe-eval, no unsafe-inline; the interactive surfaces (file windowing, ticket selects, composer preview, live timeline) all still work in a browser; a test asserts the header is present and that no data-* expression interpolates request or repository content.

2026-08-31 20:33:28

Landed across ef352402 (policy, nonces, handlers, hashed scripts), ce8dcd1a (url-safe nonce) and the alphabet regression test.

All 14 inline handlers gone: 11 identical onchange=this.form.submit() became data-on:change with requestSubmit (validates, unlike submit), two selection handlers became expressions, and the copy-hash button moved to a delegated assets/copy.js. Scripts now carry content-hashed immutable URLs rather than revalidating daily.

Policy served on every page: default-src self; script-src self nonce-...; style-src self; base-uri none; frame-ancestors none. No unsafe-eval, no unsafe-inline.

THE AUDIT found the expressions already safe, for a reason worth pinning: html/template strips a data- prefix before classifying an attribute, so data-on:drop reads as on* and gets the JAVASCRIPT escaper. A hostile board status renders as u0027 rather than ' — which matters because an HTML-escaped quote decodes back to a quote before any script reads the attribute. Board statuses come from .quarry/tickets.json, upstream content on a mirror. Pinned by TestExpressionsGetJavaScriptEscaping.

ONE REAL TRAP, found only in a browser: the nonce was standard base64, whose + is escaped to + inside data-nonce. The browser decodes it before datastar reads it, so the two values disagreed by one round of encoding and every injected script was rejected — SILENTLY, with no console violation. The page just stopped being interactive. Fixed with url-safe base64 and pinned by TestNonceNeedsNoHTMLEscaping.

Verified in a browser under the enforced policy: file windowing (2,617 to 5,166 nodes on scroll), ticket select save-on-change, composer live preview. No console errors.