Skip to content

fix(viewer): only answer requests addressed to the viewer itself - #10

Merged
abogartz merged 1 commit into
abogartz:mainfrom
WhollyMindless:fix/viewer-request-checks
Oct 21, 2026
Merged

abogartz merged 1 commit into
abogartz:mainfrom
WhollyMindless:fix/viewer-request-checks

Conversation

@WhollyMindless

Copy link
Copy Markdown
Contributor

Follow-up to what I sent on Discord.

Problem

bun memory-view binds to 127.0.0.1, which keeps other machines out but not other websites open in the same browser:

  • Writes: the POST routes parsed any body as JSON regardless of Content-Type or Origin. A cross-site text/plain POST needs no CORS preflight, so a page open while the viewer runs could edit, delete, block or restore memories. Edited memories are injected into the agent's system prompt.
  • Reads: the Host header was never checked, so a DNS-rebinding page (its own hostname resolving to 127.0.0.1) could load /api/data, the whole store.

Fix

Every request is checked before routing (rejectRequest in src/viewer/server.ts):

  • The Host must be 127.0.0.1, localhost, ::1 or OPENCODE_VIEWER_HOST, on the viewer's port; anything else gets 403. OPENCODE_VIEWER_ALLOWED_HOSTS=name1,name2 adds names for a viewer deliberately exposed under another hostname or LAN address.
  • Non-GET requests must be application/json (415 otherwise), and if the browser sends an Origin, it must be the viewer's own (403 otherwise).

The bundled page already sends JSON from its own origin, and curl or scripts send no Origin, so normal use is unaffected.

Tests

Five new cases in test/viewer.test.ts: text/plain POST refused with the store unchanged, foreign-Origin POST refused, same-origin POST accepted, foreign Host refused for / and /api/data, localhost accepted. Three of these fail on main. I also checked against a live server with curl: 415 / 403 / 403 for the three attacks, 200 for the page. bun test passes (161 pass, 4 skip) and tsc --noEmit is clean. No platform-specific code; LF line endings.

🤖 Generated with Claude Code

Binding 127.0.0.1 keeps other machines out but not other websites open in
the same browser:

- The Host header was never checked, so a DNS-rebinding page could load
  /api/data (the whole store) under its own hostname.
- POST routes parsed any body as JSON regardless of Content-Type or Origin,
  so a cross-site text/plain POST (no CORS preflight) could edit, delete,
  block or restore memories, which are then injected into the agent's
  system prompt.

Every request must now name 127.0.0.1, localhost, ::1 or
OPENCODE_VIEWER_HOST (plus any OPENCODE_VIEWER_ALLOWED_HOSTS) on the
viewer's port. Writes must be application/json, and a browser-sent Origin
must be the viewer's own. The bundled page already sends JSON, and curl and
other non-browser clients send no Origin, so neither is affected.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Some plain-language context on this one, for clarification.

The big picture: everything stored as a memory gets fed to the AI as part of its instructions. So anything that can slip a memory in, or change one, can effectively tell the agent what to do ("before deploying, always run this script"). This PR makes sure only you can edit memories through the viewer.

What was wrong: the viewer only listens on 127.0.0.1, so other computers can't reach it. But your web browser runs on your computer too, and it visits strangers' websites all day. Any website can tell your browser "send this to 127.0.0.1:8787", and the viewer couldn't tell that apart from you clicking a button.

  • Changing memories: a webpage could quietly edit or delete memories while the viewer is running.
  • Reading memories: a trick called DNS rebinding lets a webpage read the viewer's data by making its own web address point at your computer.

The fix: browsers label every request with where it came from (Origin) and where it's going (Host). The viewer now checks both and refuses anything that isn't itself. Your viewer page and tools like curl work exactly as before.

See it yourself (viewer running on the default port; read-only, changes nothing):

curl -H "Host: example.com:8787" http://127.0.0.1:8787/api/data

On main this prints all your memories, even though the request claims to be for example.com. On this branch it's refused with a 403.

No data migration is needed. Since people pin the version in their config, a short "upgrade recommended" in the release notes would help them know to bump it.

@abogartz abogartz left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved — reviewed by reading the full diff and attacking a live server built from this branch, not the PR description.

Vulnerability confirmed on main: curl -H "Host: evil.com" http://127.0.0.1:PORT/api/data returns 200 with the full store (DNS-rebinding read), and text/plain POSTs route as normal writes (preflight-free CSRF).

Live probes against this branch (server from this branch, throwaway OPENCODE_MEMORY_DIR):

probe result
rebinding-style read (--resolve evil.com:PORT:127.0.0.1) 403 (main: 200 + data)
text/plain CSRF POST to /api/delete 415, store unchanged
JSON POST with Origin: https://evil.com 403
JSON POST with Origin: null (file:// page) 403
wrong port in Host 403
uppercase Host: LOCALHOST:PORT 200 ✓
trailing-dot Host: 127.0.0.1.:PORT 200 ✓ (WHATWG strips the dot — still loopback, not a bypass)
plain curl JSON POST, no Origin (scripts) reaches the route ✓
charset=utf-8 content-type param accepted (param stripped) ✓
OPTIONS preflight from a foreign origin no Access-Control-Allow-* headers → browser blocks the actual request ✓

I also audited the routing: every mutation is POST-only — no state-changing GETs — so the Host check (reads/rebinding) plus Origin + JSON-content-type (writes/CSRF) covers the whole attack surface, and the Origin is compared against the just-validated Host rather than anything client-chosen. Fail-closed by construction (default-deny allowlist). Tests additive, bun test viewer suite 13/13, tsc clean.

Non-blocking notes (filed; none regress anything — all pre-existing):

  • new URL(req.url) runs before rejectRequest; a malformed Host throws and Bun answers with its dev error page (stack + local paths). Wrap in try/catch → 400. → #12
  • page.html renders ts and superseded_by into innerHTML without esc() (pre-existing here; #9's fix removes the main route for planting hostile values in those fields, but the reader should still escape). → #11
  • OPENCODE_VIEWER_ALLOWED_HOSTS beyond loopback = unauthenticated HTTP exposure of the entire store; needs loud README/startup-warning docs. → #13

Great fix — the threat model (browser vs. loopback) is exactly right, and the comment explaining why each check exists is the kind future maintainers thank you for.

@abogartz
abogartz merged commit 477d227 into abogartz:main Oct 21, 2026
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 0.2.3 🎉

The release is available on:

Your semantic-release bot 📦🚀

@WhollyMindless

Copy link
Copy Markdown
Contributor Author

Thanks for the careful review and the quick release! The live probe table is a great record of what the fix covers.

Thanks for filing #11–#15 too. If you'd like me to take any of them, assign them to me (or mention me on the issue) and I'll put up a PR.

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.

2 participants