Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

ui: reject non-loopback Host on the local HTTP server

Martin Vogel707131082583
diff --git a/src/ui/http_server.c b/src/ui/http_server.c
index f23e36f4a..6ef994393 100644
--- a/src/ui/http_server.c
+++ b/src/ui/http_server.c
@@ -1577,11 +1577,32 @@ static void handle_rpc(cbm_http_conn_t *c, const cbm_http_req_t *req, cbm_mcp_se
/* ── Request dispatch ─────────────────────────────────────────── */
+/* True when the Host header names the loopback interface the server binds to
+ * (with or without a port). Anything else means the request reached us under a
+ * name that is not loopback — a rebinding DNS host or a proxy pointed at the
+ * local port — which is the DNS-rebinding / cross-site vector against a
+ * localhost-only service. */
+static bool host_is_loopback(const char *host) {
+ return cbm_http_path_match(host, "localhost") || cbm_http_path_match(host, "localhost:*") ||
+ cbm_http_path_match(host, "127.0.0.1") || cbm_http_path_match(host, "127.0.0.1:*") ||
+ cbm_http_path_match(host, "[::1]") || cbm_http_path_match(host, "[::1]:*");
+}
+
static void dispatch_request(cbm_http_server_t *srv, cbm_http_conn_t *c,
const cbm_http_req_t *req) {
/* Build per-request CORS headers (only reflects localhost origins) */
update_cors(req);
+ /* DNS-rebinding / cross-site guard: the server binds to loopback only, so a
+ * request carrying any non-loopback Host was routed here under a foreign
+ * name (a rebinding DNS record, a proxy) and must be refused before it can
+ * reach a state-changing endpoint. A bare request with no Host header
+ * (HTTP/1.0 local tooling) is still allowed. */
+ if (req->host[0] != '\0' && !host_is_loopback(req->host)) {
+ cbm_http_replyf(c, 403, g_cors, "%s", "{\"error\":\"forbidden host\"}");
+ return;
+ }
+
bool is_get = strcmp(req->method, "GET") == 0;
bool is_post = strcmp(req->method, "POST") == 0;
bool is_delete = strcmp(req->method, "DELETE") == 0;
diff --git a/src/ui/httpd.c b/src/ui/httpd.c
index da5f0146f..7e41b46e4 100644
--- a/src/ui/httpd.c
+++ b/src/ui/httpd.c
@@ -342,6 +342,8 @@ int cbm_http_parse_head(const char *data, size_t len, cbm_http_req_t *req, size_
if (header_name_is(p, nlen, "origin")) {
copy_header_value(colon + 1, eol, req->origin, sizeof(req->origin));
+ } else if (header_name_is(p, nlen, "host")) {
+ copy_header_value(colon + 1, eol, req->host, sizeof(req->host));
} else if (header_name_is(p, nlen, "accept-language")) {
copy_header_value(colon + 1, eol, req->accept_language, sizeof(req->accept_language));
} else if (header_name_is(p, nlen, "transfer-encoding")) {
diff --git a/src/ui/httpd.h b/src/ui/httpd.h
index b6fd48110..d20d9a0f8 100644
--- a/src/ui/httpd.h
+++ b/src/ui/httpd.h
@@ -49,6 +49,7 @@ typedef struct {
char path[2048];
char query[2048];
char origin[256];
+ char host[256];
char accept_language[256];
char *body;
size_t body_len;
diff --git a/tests/test_httpd.c b/tests/test_httpd.c
index d7cb25742..5d4a8a6b1 100644
--- a/tests/test_httpd.c
+++ b/tests/test_httpd.c
@@ -1085,6 +1085,31 @@ TEST(git_context_resolve_no_hang_under_live_ui_sockets) {
#endif
}
+/* The server binds to loopback only. A request carrying a non-loopback Host
+ * header reached it under a foreign name — the DNS-rebinding / cross-site
+ * vector against a localhost service — and must be refused before routing. A
+ * loopback Host (or none) proceeds normally. */
+TEST(ui_server_rejects_non_loopback_host) {
+ th_server_t ts;
+ ASSERT_EQ(th_server_start(&ts), 0);
+ int port = cbm_http_server_port(ts.srv);
+ char resp[4096];
+
+ int n = th_http(port, "GET / HTTP/1.1\r\nHost: evil.example.com\r\n\r\n", resp, sizeof(resp));
+ ASSERT_GT(n, 0);
+ ASSERT_EQ(th_status(resp), 403);
+
+ /* A loopback Host is not rejected (routes on to the normal 404 stub). */
+ char req[128];
+ snprintf(req, sizeof(req), "GET / HTTP/1.1\r\nHost: 127.0.0.1:%d\r\n\r\n", port);
+ n = th_http(port, req, resp, sizeof(resp));
+ ASSERT_GT(n, 0);
+ ASSERT_NEQ(th_status(resp), 403);
+
+ th_server_stop(&ts);
+ PASS();
+}
+
/* ── Suite ────────────────────────────────────────────────────── */
SUITE(httpd) {
@@ -1113,6 +1138,7 @@ SUITE(httpd) {
RUN_TEST(httpd_listen_port_collision_returns_null);
/* Full UI server */
+ RUN_TEST(ui_server_rejects_non_loopback_host);
RUN_TEST(ui_server_unknown_path_404);
RUN_TEST(ui_server_root_serves_stub_404);
RUN_TEST(ui_server_cors_localhost_reflected);