Skip to content

ui-server: decide how the app shell rolls out a strict CSP with Trusted Types, starting Report-Only #2607

Open

Description

@schlessera

Context

The app shell has no script policy. Every HTTP response from @schlessera/brain-ui-server that lacks a route CSP gets only Content-Security-Policy: frame-ancestors 'none' and X-Frame-Options: DENY (app.use, packages/ui-server/src/app.ts:715-725). That includes the SPA's index.html when the server serves it via staticRoot (packages/ui-server/src/app.ts:983-1004). The behaviour is specified in docs/http-api.md:314-316. Only the two file routes set real policies (HTML_PREVIEW_CSP, packages/ui-server/src/routes/files.ts:38-39; raw content at :160). Those routes are governed by the HTML preview decision. The hardening record covers origin, session and container boundaries but not script injection.

Known HTML sinks in the client are the three Mermaid SVG injections (dangerouslySetInnerHTML, packages/ui-react/src/components/chat/mermaid-block.tsx:88, :97; packages/ui-react/src/components/chat/mermaid-viewer.tsx:40). Mermaid runs with securityLevel: "strict" (packages/ui-react/src/lib/mermaid.ts:34). Mermaid depends on DOMPurify, which creates its own dompurify policy when Trusted Types exist. What else writes to sinks at runtime (React, maps, the service worker registration, and so on) is unknown until measured. #2079 may replace the Mermaid path with module renderers.

Guide security/security: "Phase 3 core enforcement represents the highest-leverage security work. Specifically, Injection/XSS mitigation through enforced CSP (§3.2) and Trusted Types (§3.3) addresses the largest practical threat". Guide security/trusted-types: "First, roll out using the Content-Security-Policy-Report-Only header."

Support for Trusted Types (MDN browser-compat-data): Chrome Android 83, Samsung 13, Firefox Android 148. A Report-Only header changes no behaviour in any browser.

Scope

A spike whose deliverable is a written decision, docs/decisions/content-security-policy.md, answering:

  1. Which sinks fire. Measure under Content-Security-Policy-Report-Only: require-trusted-types-for 'script'; script-src 'self' 'report-sample'; object-src 'none'; base-uri 'none'. Collect securitypolicyviolation events in the real-browser layout harness while it drives chat (including a Mermaid diagram), the graph, Activity, Files, Settings and voice capture. List each violation with its source file and stack.
  2. Strict script-src shape. The host builds a static index.html, so decide between hashes and a per-request nonce, and what 'strict-dynamic' buys here. Inventory the inline scripts in the host-built shell, as far as brain-kit can know them.
  3. Who owns the header. Options: ui-server for responses it serves (staticRoot), the host for its own static server, or both through an exported policy builder that both can use. Cover the dev proxy too.
  4. Reporting. Decide whether ui-server gets a report endpoint (a new HTTP route, which is a contract addition) or reports go to a host-configured URL. Give the retention and authentication of reports.
  5. Policies. The Trusted Types policy names to allow (for example dompurify, an app policy for Mermaid output, default or none) and the order of rollout from Report-Only to enforced.

The decision ends with the follow-up issues to file. It files no code changes.

Out of scope

  • Shipping any header, policy or report route.
  • Changing the HTML preview or raw-file CSPs.
  • COOP, CORP, HSTS and the other companion headers. The decision may list them as follow-ups.
  • Host deployment specifics.

Acceptance criteria

  • docs/decisions/content-security-policy.md exists, is linked from docs/decisions/README.md, and answers questions 1–5.
  • The sink inventory comes from an actual browser run in the layout harness under the Report-Only policy, not from grep. The PR attaches the collected violation list and the exact command.
  • The decision names the binding records it touches (the preview decision and the hardening record) and states whether any amendment is needed.
  • The follow-up issues are listed with proposed titles, areas and whether each is a contract change (docs/integration-contract/http.md for headers or a report route).
  • bun scripts/check-leakage.ts passes on the new document.

Verification

node scripts/visual.mjs --project=ui-react-layout --filter=<csp-report-only-probe>
bun scripts/check-leakage.ts
bun run test tests/decision-citations.test.ts
bun run lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: commit prefix, and the doc updated in the same commit.
  • This changes a published package's behaviour, so it needs a changeset.

None: the spike adds a decision document and a measurement probe. Shipping a header later is a contract change.

Activity

  1. added
    type: spike
    area: ui-server
    area: ui-react
    priority: p3
    agent-ready
    security
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-reactarea: ui-serverpriority: p3securitytype: spike

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions