Repository navigation
fix(viewer): only answer requests addressed to the viewer itself - #10
Conversation
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]>
|
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.
The fix: browsers label every request with where it came from ( 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/dataOn 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
left a comment
There was a problem hiding this comment.
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 beforerejectRequest; a malformed Host throws and Bun answers with its dev error page (stack + local paths). Wrap in try/catch → 400. → #12page.htmlrenderstsandsuperseded_byintoinnerHTMLwithoutesc()(pre-existing here; #9's fix removes the main route for planting hostile values in those fields, but the reader should still escape). → #11OPENCODE_VIEWER_ALLOWED_HOSTSbeyond 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.
|
🎉 This PR is included in version 0.2.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Follow-up to what I sent on Discord.
Problem
bun memory-viewbinds to 127.0.0.1, which keeps other machines out but not other websites open in the same browser:Content-TypeorOrigin. A cross-sitetext/plainPOST 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.Hostheader 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 (
rejectRequestinsrc/viewer/server.ts):127.0.0.1,localhost,::1orOPENCODE_VIEWER_HOST, on the viewer's port; anything else gets 403.OPENCODE_VIEWER_ALLOWED_HOSTS=name1,name2adds names for a viewer deliberately exposed under another hostname or LAN address.application/json(415 otherwise), and if the browser sends anOrigin, 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,localhostaccepted. Three of these fail onmain. I also checked against a live server with curl: 415 / 403 / 403 for the three attacks, 200 for the page.bun testpasses (161 pass, 4 skip) andtsc --noEmitis clean. No platform-specific code; LF line endings.🤖 Generated with Claude Code