Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cmd/gebed/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]`.
Expand Down
64 changes: 64 additions & 0 deletions internal/server/guard.go
Original file line number Diff line number Diff line change
@@ -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
}
}
61 changes: 61 additions & 0 deletions internal/server/guard_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}