Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Reject non-loopback Host headers on the dashboard and MCP HTTP servers

Ahmed Abdullah0ea07f481e8b
diff --git a/cli/beacon/cmd/mcp.go b/cli/beacon/cmd/mcp.go
index 316596b4..b3838691 100644
--- a/cli/beacon/cmd/mcp.go
+++ b/cli/beacon/cmd/mcp.go
@@ -78,7 +78,7 @@ func runMCPServe(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
httpServer := &http.Server{
Addr: mcpOpts.addr,
- Handler: server.HTTPHandler(),
+ Handler: dashboard.RequireLoopbackHost(server.HTTPHandler()),
ReadHeaderTimeout: 5 * time.Second,
BaseContext: func(_ net.Listener) context.Context { return ctx },
}
diff --git a/cli/beacon/internal/endpoint/dashboard/server.go b/cli/beacon/internal/endpoint/dashboard/server.go
index 34743e56..3bd4db24 100644
--- a/cli/beacon/internal/endpoint/dashboard/server.go
+++ b/cli/beacon/internal/endpoint/dashboard/server.go
@@ -343,7 +343,7 @@ func ListenAndServe(opts Options) error {
}
server := &http.Server{
Addr: opts.Addr,
- Handler: handler,
+ Handler: RequireLoopbackHost(handler),
ReadHeaderTimeout: 5 * time.Second,
}
return server.ListenAndServe()
@@ -361,6 +361,28 @@ func ValidateLoopbackAddr(addr string) error {
return nil
}
+// RequireLoopbackHost rejects requests whose Host header is not a loopback name.
+//
+// Binding to loopback keeps other machines out, but not a web page that DNS-rebinds its own
+// hostname to 127.0.0.1: the browser then treats this server as same-origin with that page and
+// lets it read every response. The Host header still carries the page's hostname, so it is the
+// one place the request can be told apart from a local client.
+func RequireLoopbackHost(next http.Handler) http.Handler {
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ host := r.Host
+ if h, _, err := net.SplitHostPort(host); err == nil {
+ host = h
+ }
+ host = strings.Trim(host, "[]")
+ ip := net.ParseIP(host)
+ if !strings.EqualFold(host, "localhost") && (ip == nil || !ip.IsLoopback()) {
+ writeError(w, http.StatusForbidden, fmt.Errorf("request host must be a loopback address"))
+ return
+ }
+ next.ServeHTTP(w, r)
+ })
+}
+
func URL(addr string) string {
if addr == "" {
addr = DefaultAddr
diff --git a/cli/beacon/internal/endpoint/dashboard/server_test.go b/cli/beacon/internal/endpoint/dashboard/server_test.go
index bd40d4c1..1cadd535 100644
--- a/cli/beacon/internal/endpoint/dashboard/server_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/server_test.go
@@ -658,3 +658,29 @@ func TestStaticDashboardPagesServe(t *testing.T) {
})
}
}
+
+func TestRequireLoopbackHostRejectsRebindingHosts(t *testing.T) {
+ handler := RequireLoopbackHost(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.WriteHeader(http.StatusOK)
+ }))
+ for host, want := range map[string]int{
+ "127.0.0.1:8765": http.StatusOK,
+ "127.0.0.1": http.StatusOK,
+ "localhost:8765": http.StatusOK,
+ "LOCALHOST:8765": http.StatusOK,
+ "[::1]:8766": http.StatusOK,
+ "[::1]": http.StatusOK,
+ "attacker.example:8765": http.StatusForbidden,
+ "localhost.attacker.example": http.StatusForbidden,
+ "192.168.1.20:8765": http.StatusForbidden,
+ "": http.StatusForbidden,
+ } {
+ req := httptest.NewRequest(http.MethodGet, "/api/events", nil)
+ req.Host = host
+ rec := httptest.NewRecorder()
+ handler.ServeHTTP(rec, req)
+ if rec.Code != want {
+ t.Errorf("Host %q: status = %d, want %d", host, rec.Code, want)
+ }
+ }
+}