Skip to content

Reject cross-site and DNS-rebinding requests to the API - #2

Open
jm-g wants to merge 1 commit into
vic:mainfrom
jm-g:fix/cross-origin-protection
Open

jm-g wants to merge 1 commit into
vic:mainfrom
jm-g:fix/cross-origin-protection

Conversation

@jm-g

@jm-g jm-g commented Nov 16, 2026

Copy link
Copy Markdown

Fixes #1.

The HTTP API performed no Origin or Host check, and POST /api/repos/{name}/ops decoded a JSON body regardless of Content-Type. Since gebed listens on loopback, any web page the user visited while gebed was running could issue a cross-origin "simple request" (text/plain, no preflight) that created or changed beads — and, via addComment with an @name, inject a message into a live Claude Code session (an agent with shell access). DNS rebinding additionally defeated the missing Host check and exposed reads (all beads, comments, and /api/sessions).

Change

A single Guard wrapper applied in main.go, outermost check first:

  • Host allowlist — only loopback names (localhost, 127.0.0.1, ::1) and the host:port from -addr. A request with a rebinding Host (e.g. evil.example) is refused. This is what closes DNS rebinding; CrossOriginProtection does not look at Host.
  • http.CrossOriginProtection (Go 1.25) — rejects unsafe-method (POST/PUT/PATCH/DELETE) cross-origin requests via Sec-Fetch-Site/Origin. Safe methods pass, so the read API is unchanged; the write endpoints are covered.

Non-browser clients (no Origin, no Sec-Fetch-Site) and same-origin requests still pass, so gebed mcp and the UI are unaffected.

A wildcard bind (0.0.0.0/::) adds no host to the allowlist, so only loopback names are accepted then; exposing gebed deliberately is out of scope for this change and would need its own authentication.

Tests

internal/server/guard_test.go covers: loopback GET, localhost GET, same-origin POST and non-browser POST reaching the handler; cross-origin POST (with and without Sec-Fetch-Site), rebinding Host, and wrong port refused. go test ./... and go vet ./... pass.

Verified end-to-end

Against the built binary on the default port:

normal GET (loopback Host):   200
cross-origin POST (Origin evil.example): 403   (no bead created)
rebinding (Host evil.example):           403

🤖 Generated with Claude Code

The HTTP API had no Origin or Host check, and POST /api/repos/{name}/ops
decoded a JSON body regardless of Content-Type. Any web page the user
visited while gebed ran could therefore issue a cross-origin "simple
request" (text/plain, no preflight) that created or changed beads, and a
comment with @name is delivered into the live Claude Code session of that
name, so the page could inject a prompt into an agent with shell access.

- Guard wraps the handler with http.CrossOriginProtection (Go 1.25), which
  rejects unsafe-method cross-origin requests; safe reads are unaffected
- plus a Host allowlist limited to loopback names and the -addr host, which
  closes DNS rebinding (CrossOriginProtection does not inspect Host)
- test covers same-origin and non-browser requests passing, cross-origin
  writes and rebinding hosts refused

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.

API has no Origin/Host check: any website can write to gebed and reach Claude sessions

1 participant