The TiffinBox BuildOne system, every chapter
0 / 0

Workbench / The TiffinBox Build

S28 · V2·28

Lock the Sinks

Volume 2, chapter 28. Escaping has protected TiffinBox's pages since stage 12; this stage adds the layers that work when escaping fails. Trusted Types with no policies at all refuses every string written to innerHTML, insertAdjacentHTML, a script's text or setTimeout, in all three views, and the suite proves it. Violations are reported to a size-limited endpoint (with report-uri only, because adding report-to made Chromium stop sending report-uri reports and its Reporting API delivered nothing in 90 s). The Angular page gets its own policy with a fresh style nonce per response, so its component styles work again under a strict CSP. Cross-origin form posts are refused by Origin when an old browser sends no Fetch Metadata. And the accessibility tests now run under the production policy instead of bypassing it.

6 parts · ~5 min to read
Updated Suggest a correction

Stage 28: lock the sinks

The brief

What you'll build.

  • the policy (Server.csp): object-src 'none', require-trusted-types-for 'script', trusted-types 'none' and report-uri /csp-reports added to stage 12's baseline;
  • POST /csp-reports: accepts reports (8 KB limit), logs one line each, untrusted input;
  • a per-response style nonce for the Angular page: Pages.themed generates 16 random bytes, puts them in that response's own CSP (replacing the default, never added to it) and in <meta property="csp-nonce">; main.ts provides it as Angular's CSP_NONCE; component styles are allowed again;
  • an Origin check on state-changing requests when Fetch Metadata is absent;
  • tests: test_security.py (each sink refused and reported, the nonce new each time and on the Angular page only, cross-origin posts refused); axe served as a parser-inserted test script so it runs under the real policy; a PagesTest for the headers.

Why a real company needs this. Every large application eventually ships an escaping mistake: a new developer, a third-party component, an innocent-looking innerHTML. Companies that survive these quietly have layers behind escaping that turn a vulnerability into a blocked action and a report. Trusted Types is the strongest of these, and the hardest to retrofit, so it's best adopted while the codebase is small.

Why now. Chapter 28 is frontend security: XSS, CSP, Trusted Types, CSRF, framing, the supply chain and identity. The build has banned sinks in its own code since stage 15 and kept a strict CSP since stage 12; making the browser enforce it is the next step, and stage 23's banned Angular styles were waiting for nonces.

What you'll have at the end.

  • the browser refusing every string-to-HTML and string-to-script write on every page, with reports;
  • nonces where they're needed and nowhere else;
  • ADR-0030: the browser security baseline.

Theory to own

  1. What Trusted Types does. Which operations become errors, and what's a policy? (The chapter's lab, Experiment 2; Companion, Gaps 1.)
  2. Nonces. What makes a nonce safe, and what doesn't it protect against? (Companion, Gaps 3.)
  3. Reporting. How do violations reach you, and how can that fail? (Companion, Gaps 2.)
  4. CSRF defences without login. What stops another site's form from changing state here? (Companion, drill D3.)
  5. Testing under the real policy. Why must security tests not bypass the CSP? (Companion, drill D1.)

Build it

Step 1: Trusted Types, and the suite as the judge

Add object-src 'none', require-trusted-types-for 'script' and trusted-types 'none' to the policy, rebuild, and run the whole suite: every test fails on any console error, and a Trusted Types refusal is one. Then write tests that attack each sink deliberately.

Checkpoint. All existing tests pass under the new policy; each sink is refused.

Step 2: reports

Add POST /csp-reports before the routes that need state: read the body, refuse over 8 KB with 413, flatten it to one printable line and log it. Add report-uri /csp-reports. Try report-to csp with a Reporting-Endpoints header as well, and test delivery by violating Trusted Types and waiting for the log line.

Checkpoint. A violation produces a log line within seconds.

Step 3: a nonce for the Angular page

In Pages.themed, when the page loads /angular-kitchen.js, generate a nonce from a SecureRandom, write it into a <meta property="csp-nonce" nonce="…"> and into that response's own Content-Security-Policy with style-src 'self' 'nonce-…'. In Server.write, a response's own CSP replaces the default instead of being added (two policies are both enforced). In main.ts, read the meta element's nonce property and provide it as CSP_NONCE. Give Ticket a real component style, and remove the boundary rule against component styles.

Checkpoint. The style applies; the nonce differs on every load; no other page has one.

Step 4: tests that run under the real policy

Replace the axe tests' bypass_csp context: route the page so its HTML arrives with one more line, <script src="/__test__/axe.js">, and serve axe at that path from the test. Write the cross-origin test: post the theme form with Sec-Fetch-Site: cross-site, with only Origin: https://evil.example, with this server's Origin, and with neither; and post 9,000 bytes to the report endpoint.

Checkpoint. axe runs with no CSP bypass anywhere in the suite; the Origin-only post is refused.

Senior and architect lens

1. Escaping is the first layer, not the only one. The build had no known injection; the point of Trusted Types is the one nobody knows about yet. Adopted now, it cost one test-harness change; adopted after years of innerHTML, it costs an inventory and many fixes.

2. ADR-0030: the browser security baseline. Write it with these points:

  • Policy: same-origin scripts and styles only; no inline script; object-src 'none', base-uri 'none', form-action 'self', frame-ancestors 'none'; Trusted Types required with no policies; a style nonce only where a page needs one, fresh per response, never cached.
  • Reports: report-uri to a size-limited endpoint; report-to after stage 32 proves delivery; reports treated as personal data.
  • Requests: Fetch Metadata, then Origin, on every state-changing request; SameSite=Lax, HttpOnly cookies.
  • Code rules: no sinks, no dangerouslySetInnerHTML, no bypassSecurityTrust* (boundaries.mjs); a sanitiser, if ever needed, through a named policy listed in the CSP.
  • Tests: the suite runs under the production policy; security tests attack each layer.

3. Test the policy, not around it. Bypassing the CSP made the tests easier and blind. The fix took one route rule.

4. Each defence has a failure mode worth testing. Reports that never arrive, nonces that leak through caches, headers missing in old browsers: each was found by testing the defence, not the attack.

5. Security and performance meet at caching. Nonced pages can't be shared-cached; stage 31 must keep that true while caching everything else.

What breaks first, and when it's addressed:

Pressure What breaks Where it's addressed
the site runs on plain HTTP cookies can't be Secure or __Host-; traffic is readable stage 31 (HTTPS, HSTS)
a CDN in front of the pages nonced pages could be cached stage 31 (cache rules exclude them)
reports only reach a log file nobody sees a spike stage 32 (reports into RUM, with alerts)
no authentication for kitchen staff anyone who reaches the server can act as a kitchen Volume 3 (staff login, sessions)
dependencies are pinned but not audited a compromised package would ship stage 30 (workspace with lockfile review and audit in CI)

Evidence

  • The suite passes under the production policy, with no bypass_csp anywhere; test_security.py passes (7 tests).
  • The report-uri and report-to delivery results are recorded in the README and in Server.csp's comment.
  • Two loads of the Angular page show different nonces; the plain page has none; the component style applies.
  • The Origin-only cross-origin post is refused (403); 34 server tests pass.
  • ADR-0030 is committed.

Carry forward

  • Server.csp is the one place the policy is written; stage 31 adds HSTS and the HTTPS-only cookie attributes beside it.
  • /csp-reports feeds stage 32's monitoring.
  • The parser-inserted test script is how any test tool is loaded from now on.
  • The Origin check protects stage 29's queued commands when they're replayed after going offline.