From 9f285b72950a6b907e63565310d197d97e2fc06f Mon Sep 17 00:00:00 2001 From: Jean-Marie Gaillourdet Date: Mon, 16 Nov 2026 18:33:43 +0100 Subject: [PATCH] Reject cross-site and DNS-rebinding requests to the API 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 --- cmd/gebed/main.go | 4 ++- internal/server/guard.go | 64 +++++++++++++++++++++++++++++++++++ internal/server/guard_test.go | 61 +++++++++++++++++++++++++++++++++ 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 internal/server/guard.go create mode 100644 internal/server/guard_test.go diff --git a/cmd/gebed/main.go b/cmd/gebed/main.go index abe51b7..b9fccfd 100644 --- a/cmd/gebed/main.go +++ b/cmd/gebed/main.go @@ -85,7 +85,9 @@ func main() { log.Printf("gebed on http://%s", *addr) srv := server.New(reg, store.Exec, themes, net, u.Username) srv.Warm() - log.Fatal(http.ListenAndServe(*addr, srv)) + // Guard refuses cross-site writes and DNS-rebinding hosts, so a web page + // the user visits cannot drive the loopback API. + log.Fatal(http.ListenAndServe(*addr, server.Guard(*addr, srv))) } // installSkill implements `gebed skill install [-to DIR]`. diff --git a/internal/server/guard.go b/internal/server/guard.go new file mode 100644 index 0000000..701dca6 --- /dev/null +++ b/internal/server/guard.go @@ -0,0 +1,64 @@ +package server + +import ( + "net" + "net/http" +) + +// Guard wraps h so a web page the user merely visits cannot drive gebed. +// +// Two checks, outermost first: +// +// - A Host allowlist. gebed listens on loopback, so a request whose Host is +// not a loopback name (or the address gebed was told to listen on) is a +// DNS-rebinding attempt and is refused. This is what CrossOriginProtection +// does not cover, and it is what lets a rebinding domain read every bead. +// +// - Go's http.CrossOriginProtection, which rejects unsafe-method +// (POST/PUT/PATCH/DELETE) cross-origin requests using Sec-Fetch-Site and +// Origin. Safe methods pass, so the read API is unaffected; the write +// endpoints under /api/.../ops and DELETE /api/repos/{name} are covered. +// +// listenAddr is the value passed to -addr. +func Guard(listenAddr string, h http.Handler) http.Handler { + h = http.NewCrossOriginProtection().Handler(h) + allowed := loopbackHost(listenAddr) + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + if !allowed(r.Host) { + http.Error(w, "forbidden host", http.StatusForbidden) + return + } + h.ServeHTTP(w, r) + }) +} + +// loopbackHost reports whether a request's Host header may reach gebed: a +// loopback name, or the host:port gebed listens on. A wildcard bind +// (0.0.0.0, ::, or no host) adds nothing, so only loopback names are accepted +// then; expose gebed deliberately and it needs its own authentication anyway. +func loopbackHost(listenAddr string) func(string) bool { + allow := map[string]struct{}{"localhost": {}, "127.0.0.1": {}, "::1": {}} + var port string + if h, p, err := net.SplitHostPort(listenAddr); err == nil { + port = p + switch h { + case "", "0.0.0.0", "::": + default: + allow[h] = struct{}{} + } + } + return func(host string) bool { + if host == "" { + return false + } + h, p, err := net.SplitHostPort(host) + if err != nil { // Host carried no port + h, p = host, "" + } + if port != "" && p != "" && p != port { + return false + } + _, ok := allow[h] + return ok + } +} diff --git a/internal/server/guard_test.go b/internal/server/guard_test.go new file mode 100644 index 0000000..7eff3be --- /dev/null +++ b/internal/server/guard_test.go @@ -0,0 +1,61 @@ +package server + +import ( + "net/http" + "net/http/httptest" + "testing" +) + +func TestGuard(t *testing.T) { + reached := false + inner := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { reached = true }) + h := Guard("127.0.0.1:7878", inner) + + cases := []struct { + name string + method string + host string + headers map[string]string + want int // want status; 0 means the inner handler must run (200) + }{ + {"loopback GET reaches inner", "GET", "127.0.0.1:7878", nil, 0}, + {"localhost GET reaches inner", "GET", "localhost:7878", nil, 0}, + {"same-origin POST reaches inner", "POST", "127.0.0.1:7878", + map[string]string{"Origin": "http://127.0.0.1:7878", "Sec-Fetch-Site": "same-origin"}, 0}, + {"non-browser POST reaches inner", "POST", "127.0.0.1:7878", nil, 0}, + {"cross-origin POST blocked", "POST", "127.0.0.1:7878", + map[string]string{"Origin": "http://evil.example", "Sec-Fetch-Site": "cross-site"}, http.StatusForbidden}, + {"cross-origin POST without Sec-Fetch-Site blocked", "POST", "127.0.0.1:7878", + map[string]string{"Origin": "http://evil.example"}, http.StatusForbidden}, + {"rebinding Host blocked", "GET", "evil.example", nil, http.StatusForbidden}, + {"rebinding Host with port blocked", "POST", "evil.example:7878", + map[string]string{"Origin": "http://evil.example:7878", "Sec-Fetch-Site": "same-origin"}, http.StatusForbidden}, + {"wrong port blocked", "GET", "127.0.0.1:9999", nil, http.StatusForbidden}, + } + + for _, c := range cases { + t.Run(c.name, func(t *testing.T) { + reached = false + r := httptest.NewRequest(c.method, "/api/repos", nil) + r.Host = c.host + for k, v := range c.headers { + r.Header.Set(k, v) + } + w := httptest.NewRecorder() + h.ServeHTTP(w, r) + + if c.want == 0 { + if !reached { + t.Fatalf("inner handler not reached; status %d", w.Code) + } + return + } + if reached { + t.Fatalf("inner handler ran but the request should have been refused") + } + if w.Code != c.want { + t.Fatalf("status = %d, want %d", w.Code, c.want) + } + }) + } +}