Skip to content

feat(http-web): serve the built editor with its response headers on every answer - #786

Merged
ajianaz merged 1 commit into
developfrom
feat/serve-editor
Dec 5, 2026
Merged

ajianaz merged 1 commit into
developfrom
feat/serve-editor

Conversation

@ajianaz

@ajianaz ajianaz commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

What

ADR-0022 slice 13b, serve the editor: a new crate meldframe-http-web serves the files of apps/web/dist with a Content-Security-Policy, X-Content-Type-Options: nosniff, Referrer-Policy: no-referrer and, on https, Strict-Transport-Security on every answer it makes, and refuses every path that leaves the directory. The starting policy of the ADR was checked against the real build in Chromium. Refs the 13b entry of the Plan in docs/adr/0022-*.md; E2.1 stays unticked.

Why

The editor and the API must share one origin (the __Host- cookie, the exact Origin check), and agent-written text is shown in the editor, so a script that ran on that origin could call the approvals route as the person. The mitigations the ADR names are the policy, a lint that forbids {@html, and escaping; this slice builds the first two and measures the policy against the real page instead of assuming it.

Size: one logical change, 38 files, about 3,900 lines of which the tests and the browser check are most (owner decision of 4 Dec 2026: no numeric limit). It cannot be split without a crate whose headers are not tested on the real page.

Changes

  • crates/integrations/meldframe-http-web (kind bin; deps axum, tokio, futures-util, meldframe-text for the hidden-character set). Site::open(dir, Headers), Site::router() (one fallback, for fallback_service behind the API routes), headers::layer (the same stamp on any router), the serve example. One layer sets the four headers after the response is made, replacing a handler's value, so no path forgets them: 200, 206, 304, 400, 404, 405, 414, 416, 500 and HEAD.
  • Paths: each segment is decoded strictly and refused (400) for ., .., empty, a hidden character, \, :, an encoded /, or bytes that are not UTF-8; a dotfile and a directory are 404; over 1,024 bytes or 16 segments is 414; then the path is canonicalised and must be inside the canonical directory by components (so a sibling dist-secret is outside), pass no dotfile, and be a regular file by fstat of the handle that is read.
  • Files: types from the requested name (.wasm as application/wasm), immutable only for Vite's assets/<name>-<8 char hash>.<ext>, no-cache for the rest, no-store for errors, strong ETag and 304, one bytes= range with If-Range and 416, HEAD, 64 KiB reads. Error bodies have the API's shape (docs/error-codes.md, "Static file codes").
  • apps/web: the canvas imports pixi.js/unsafe-eval, and the inline theme script of index.html is the file public/theme-init.js. These two are the only changes the policy needed; the policy itself is unchanged.
  • packages/web-tools/scripts/html-sinks*.mjs and three fixtures: the {@html lint (also dangerouslySetInnerHTML, innerHTML and outerHTML assignments, insertAdjacentHTML, document.write) over every product file of apps/web and the packages; its allow-list is empty and only goes down.
  • packages/web-e2e/scripts/csp-*.mjs: the browser check, run by hand (not in CI).
  • Docs: the Built note of slice 13b in the ADR (and its CSP paragraph now says it was verified), docs/epics.md status, docs/architecture.md, docs/error-codes.md, three entries in AGENTS.md with what is not covered.
  • Ruled on where the ADR was silent: no fallback to index.html (the editor's pages are query strings of /); HSTS max-age=31536000 only; form-action left out of the policy on purpose (Chrome applies it to the redirect after a form post, and the sign-in post redirects to Gatehouse); compression, OPTIONS and TLS are the proxy's.
  • Not wired into apps/server: slice 13a owns main.rs. It mounts Site::open(dir, Headers::new(https))?.router() as the fallback_service and must not add a second, different Content-Security-Policy to API answers (two headers are both enforced).

No relaxation of the starting policy was needed, so none is written here. What it was found not to need, measured: img-src for blob: or data: (Pixi fetches a texture and decodes it with createImageBitmap, covered by connect-src 'self'), worker-src (no worker starts), style-src (styles are set through the CSSOM, which is not a violation, and the built CSS is a file).

Testing

The first run of the real page under the starting policy failed (the canvas could not start and the theme script was refused), which is how the two page changes were found; the crate tests assert each behaviour listed above and the mutants below show they can fail.

  • cargo test -p meldframe-http-web: 76 tests (18 unit, 13 files, 10 headers, 9 methods, 11 ranges, 15 traversal), real listener and a raw HTTP client so paths reach the server as written. Corpus of 72 paths on GET and HEAD, every method, long and deep paths, symlinks (to a file, a directory, .., /, a sibling with the same prefix, a loop, a dangling link, a link into a dotfile, a build opened through a link), every header on every kind of answer on http and https.
  • Hand mutants, each run against the whole crate test suite: header dropped on the error path, on a 404, on a 405, a handler's policy kept, .. allowed, an encoded slash, a backslash, a control character, containment removed, containment by text prefix, no canonicalisation, the dotfile check in the path and in the resolved path, directories served, everything immutable, a range off by one, the method check removed, a 304 for any tag: all 18 killed (run before the hidden-character test moved to meldframe_text::is_hidden_text; the corpus has U+202E, U+200B and U+FEFF as encoded bytes). One survived and is equivalent: the handler sending a body for HEAD (axum's router and hyper drop it; the test through the router alone cannot tell either). A {@html added to a package file fails the lint test.
  • npm test in apps/web: 137 files, 1,780 tests, including html-sinks.test.mjs (13, with three deliberate-violation fixtures), csp-lib.test.mjs (19) and the rewritten index-html.test.ts (6: no inline script, handler or style, no global leak).
  • Browser check, node packages/web-e2e/scripts/csp-e2e.mjs (Playwright's Chromium, software GL; real meldframe-server, the crate's serve example on the real dist, a front proxy that makes one origin): 119 checks, 0 failed. 113 under the starting policy (the editor drawn with a frame, a rectangle, a text and an image from an uploaded asset, pixel probes; wasm served as application/wasm and immutable; Inter as font/woff2; the stored theme applied before the first paint; the gestures on the Svelte page and on the React copies, every dialog, menu, the palette, the shortcut sheet, tooltips, the Activity tab, Import, Manage and Export variables with a download and a copy, a text edit, the image picker; the design pages; the error page) with zero securitypolicyviolation events, CSP console lines, blocked requests and workers, and every response of the static server carrying the headers. Eighteen attempts the policy must stop (inline script, handler, new Function, javascript:, foreign fetch, WebSocket, image, font, stylesheet, object, frame and base, data: and blob: images, inline style element and attribute, a worker from a blob: URL, a framing page) are run as page code under the same headers and each is reported under the directive it breaks; a style set through the CSSOM reports nothing. Negative phase: three tightened policies (no wasm-unsafe-eval, connect-src 'none', default-src 'none') each produce a violation and the editor does not come up. Run by hand with --csp and a policy without wasm-unsafe-eval, the positive phases fail (9 passed, 4 failed, exit 1).
  • Gates: cargo fmt --all --check, cargo clippy -p meldframe-http-web --all-targets -- -D warnings, cargo xtask deps-check, mm-check, size-check --base origin/develop, ui-literals, licences --check --main-only; in apps/web typecheck, lint, boundaries, frozen, test, build, ui-literals, licences: all pass. git log origin/develop..HEAD --format=%B | sh scripts/ci/message-patterns.sh prints nothing.

Not run: cargo deny check (cargo-deny is not installed here; the lock gains only the new crate, no new third-party crate, and licences --check passes), cargo clippy --workspace and cargo test --workspace (the crate and its dependants in the lock are untouched; CI runs them), sh scripts/ci/selftest.sh (no script changed).

Not covered: Firefox and Safari; a real TLS connection (HSTS is read off the header); the policy behind a reverse proxy; the sign-in redirect (slice 11b does not exist); a request hyper refuses before the service sees it (a space or a backslash in the request line gets a bare 400 without the headers, so the reverse proxy must set them too); the window between the check of the resolved path and the open (a build directory that someone can write is out of scope); compression, Last-Modified, several ranges; some gesture selectors of the browser check were found by reading markup, so a renamed control fails the check as "not found" and the script is fixed, not read as a violation; the Rune web client.

Cora (agent, local): not independently reviewed. The deterministic pass cora review --base origin/develop --no-llm is clean on the whole diff (its one finding, a hardcoded-secret rule on a per-run random token in csp-e2e.mjs, is a false positive and was avoided by moving the token into a function). The implementing agent cannot start an independent reviewer and cora mcp needs one to judge, so there is no independent verdict, and a self-review of the Rust source found no defect.


…very answer

A new crate, meldframe-http-web, serves apps/web/dist: a router of one fallback with a layer that sets the Content-Security-Policy, nosniff, Referrer-Policy and, on https, HSTS on every answer it makes, a path rule that never leaves the directory (strict per-segment decoding, canonicalisation, containment by components, no dotfiles, regular files only), content types, cache rules, ETag, one byte range and HEAD.

The starting policy was checked against the real build in Chromium and needed no relaxation. Two changes to the page were required: the canvas imports pixi.js/unsafe-eval (Pixi builds shader code with new Function) and the theme script is a file. A browser check (csp-e2e.mjs) drives the editor, its dialogs and menus on the Svelte and React copies, tries eighteen things the policy must stop and shows that tightened policies fail. A lint test forbids raw HTML sinks in every front-end package. The crate is not wired into apps/server (slice 13a).

Signed-off-by: Anaz S. Aji <12648688+ajianaz@users.noreply.github.com>
@ajianaz
ajianaz merged commit 4cf76de into develop Dec 5, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant