Skip to content

fix(viewer): guarded URL parse + nosniff/CSP (closes #12) - #27

Merged
abogartz merged 3 commits into
mainfrom
fix/viewer-guard-order
Oct 21, 2026
Merged

abogartz merged 3 commits into
mainfrom
fix/viewer-guard-order

Conversation

@abogartz

Copy link
Copy Markdown
Owner

Closes #12. Review finding F5, reproduced on main 286d2ba: Host: exa mple → 500 + Bun dev error page (stack, source excerpt, absolute paths of the plugin) because new URL(req.url) throws before rejectRequest gets to answer. The new a malformed Host fails closed with 400 test drives the real socket with the same wire-level repro and fails on main.

Two changes in src/viewer/server.ts:

  1. The URL parse gets its own try/catch → 400 malformed request URL, placed ahead of every other step.
  2. x-content-type-options: nosniff + CSP on every response (default-src 'none', inline script/style and connect-src 'self' only — exactly what the viewer page uses). This is the header half of the viewer: escape ts and superseded_by cells in page.html (stored-XSS defense-in-depth) #11 defense: even if the unescaped ts/superseded_by cells (PR to follow) were exploited, the payload gains no exfil channel, no navigation, no framing.

Full gate on this branch: 196 tests / 0 fail, tsc --noEmit clean. Stacks cleanly with the restore-containment PR (both touch server.ts, disjoint regions).

… CSP

new URL(req.url) ran BEFORE rejectRequest: a malformed Host header threw
and Bun answered with its dev error page — stack, source excerpts and
absolute plugin paths, pre-auth to any local process (issue #12). The
parse now has its own 400, ahead of every other step.

Also adds defense-in-depth headers on all responses: x-content-type-options:
nosniff, and a CSP that keeps a stored-XSS payload (issue #11) from
exfiltrating or framing even if an injection ever lands (the page needs
only inline script/style and same-origin fetch).

Reproduced pre-fix on main 286d2ba: Host 'exa mple' -> 500 with the
DevErrorPage body; now 400 with only the error JSON.
@abogartz
abogartz merged commit 889ce21 into main Oct 21, 2026
2 checks passed
@abogartz-opencode-self-improvement

Copy link
Copy Markdown

🎉 This PR is included in version 0.3.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

abogartz added a commit to WhollyMindless/opencode-self-improvement that referenced this pull request Oct 21, 2026
main merged abogartz#27 (guarded URL parse + nosniff/CSP) while this PR was
open, resolving the same throw. Kept:
- main's 400 message and comment in the try/catch (this PR's raw-socket
  test asserts toContain("malformed request"), which main's message satisfies);
- this PR's serve() error handler — defense-in-depth beyond abogartz#27: ANY
  uncaught handler throw now answers plain JSON 500 instead of Bun's
  dev error page, stack to the terminal only;
- this PR's raw-socket test (Hosts fetch() refuses to send) ALONGSIDE
  main's fetch-based repro and the CSP/header tests.

Combined suite: 206 pass / 4 skip / 0 fail; tsc clean; biome lint exit 0.
@abogartz
abogartz deleted the fix/viewer-guard-order branch October 22, 2026 00:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

viewer: malformed Host header throws before the guard — Bun dev error page discloses stack/paths

1 participant