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.

fix(ui): index job carries the resolved root, not the request spelling

Martin Vogel46a8f57f18e5
diff --git a/src/ui/http_server.c b/src/ui/http_server.c
index 66f1affb8..7403897e7 100644
--- a/src/ui/http_server.c
+++ b/src/ui/http_server.c
@@ -160,6 +160,10 @@ static void handle_ui_config(cbm_http_conn_t *c, const cbm_http_req_t *req) {
/* ── Server state ─────────────────────────────────────────────── */
#define MAX_INDEX_JOBS 4
+/* Capacity of the buffer POST /api/index resolves the root into, and of the
+ * job field that buffer is copied to: the job carries the resolved string
+ * whole, so the path handed to the daemon is the one the route checked. */
+#define INDEX_JOB_ROOT_CAP 4096
enum {
HTTP_RUN_IDLE = 0,
@@ -170,7 +174,7 @@ enum {
typedef struct {
cbm_http_server_t *server;
- char root_path[1024];
+ char root_path[INDEX_JOB_ROOT_CAP]; /* resolved form, see handle_index_start */
char project_name[256];
atomic_int status; /* 0=idle, 1=running, 2=done, 3=error */
char error_msg[256];
@@ -1182,7 +1186,7 @@ static void handle_index_start(cbm_http_server_t *server, cbm_http_conn_t *c,
* it accepted roots the MCP path refused — an operator's boundary held on one
* entry point and not the other. Canonicalize first: the policy is defined
* over resolved paths, and a symlink would otherwise launder the verdict. */
- char canonical_root[4096];
+ char canonical_root[INDEX_JOB_ROOT_CAP];
char boundary_err[1024];
if (!cbm_canonical_path(rpath, canonical_root, sizeof(canonical_root))) {
yyjson_doc_free(doc);
@@ -1198,6 +1202,15 @@ static void handle_index_start(cbm_http_server_t *server, cbm_http_conn_t *c,
cbm_http_replyf(c, 403, g_cors_json, "{\"error\":\"%s\"}", escaped);
return;
}
+ /* The job carries exactly the string the checks above ran on. The field
+ * has the resolved buffer's capacity, so a resolved path always fits; the
+ * check keeps a resized field from ever handing the worker a cut-off path
+ * instead of the one that was checked. */
+ if (strlen(canonical_root) >= INDEX_JOB_ROOT_CAP) {
+ yyjson_doc_free(doc);
+ cbm_http_replyf(c, 400, g_cors_json, "{\"error\":\"root_path too long\"}");
+ return;
+ }
/* Find free job slot */
int slot = -1;
@@ -1226,7 +1239,7 @@ static void handle_index_start(cbm_http_server_t *server, cbm_http_conn_t *c,
job->thread_started = false;
}
job->server = server;
- snprintf(job->root_path, sizeof(job->root_path), "%s", rpath);
+ snprintf(job->root_path, sizeof(job->root_path), "%s", canonical_root);
snprintf(job->project_name, sizeof(job->project_name), "%s", project_name);
job->error_msg[0] = '\0';
atomic_store(&job->status, 1);
@@ -1258,14 +1271,15 @@ static void handle_index_status(cbm_http_server_t *server, cbm_http_conn_t *c) {
if (pos > 1)
http_appendf(buf, sizeof(buf), &pos, ",");
const char *ss = st == 1 ? "indexing" : st == 2 ? "done" : "error";
- /* root_path comes from POST /api/index and is up to 1023 bytes, so four
- * occupied slots exceed this buffer. http_appendf pins pos to
- * sizeof(buf) on truncation, so the separator and the close have to go
- * through it as well rather than indexing raw. Both fields are free-form,
- * so escape them — a quote in a path would otherwise end its JSON string
- * early. */
- /* Escaping can double each byte: root_path is 1024, error_msg 256. */
- char esc_path[2048];
+ /* root_path is the resolved root POST /api/index stored, up to
+ * INDEX_JOB_ROOT_CAP - 1 bytes, so one long path already exceeds this
+ * buffer. http_appendf pins pos to sizeof(buf) on truncation, so the
+ * separator and the close have to go through it as well rather than
+ * indexing raw. Both fields are free-form, so escape them — a quote in a
+ * path would otherwise end its JSON string early. */
+ /* Escaping can double each byte: root_path is INDEX_JOB_ROOT_CAP,
+ * error_msg 256. */
+ char esc_path[2 * INDEX_JOB_ROOT_CAP];
char esc_error[512];
cbm_json_escape(esc_path, (int)sizeof(esc_path), server->index_jobs[i].root_path);
cbm_json_escape(esc_error, (int)sizeof(esc_error),
diff --git a/tests/test_httpd.c b/tests/test_httpd.c
index 322130c33..ee024a4d6 100644
--- a/tests/test_httpd.c
+++ b/tests/test_httpd.c
@@ -18,6 +18,7 @@
#include "../src/foundation/compat_thread.h"
#include "../src/foundation/log.h"
#include "../src/foundation/platform.h"
+#include "../src/foundation/str_util.h" /* cbm_json_escape: expected index-status fragment */
#include "../src/cli/cli.h"
#include "../src/daemon/host_internal.h"
#include "../src/git/git_context.h" /* #798 follow-up: live-socket git-resolve repro */
@@ -29,6 +30,7 @@
#include <store/store.h>
#include <watcher/watcher.h>
+#include <limits.h> /* PATH_MAX: which platforms resolve a root past 1 KB */
#include <stdio.h>
#include <stdatomic.h>
#include <stdlib.h>
@@ -627,7 +629,7 @@ static int th_server_start_with_watcher(th_server_t *ts, cbm_watcher_t *watcher)
typedef struct {
atomic_int calls;
- char root_path[512];
+ char root_path[4096]; /* the route resolves into a 4 KB buffer; record it whole */
char project_name[256];
} th_ui_index_executor_t;
@@ -974,6 +976,10 @@ TEST(ui_server_process_kill_route_is_unavailable) {
TEST(ui_server_routes_indexing_through_joinable_daemon_executor) {
char *root = th_mktempdir("cbm_httpd_daemon_index");
ASSERT_NOT_NULL(root);
+ /* The executor receives the root in resolved form (the string the route
+ * checked), which on macOS differs from the /tmp spelling. */
+ char resolved_root[4096];
+ ASSERT_TRUE(cbm_canonical_path(root, resolved_root, sizeof(resolved_root)));
th_ui_index_executor_t executor = {0};
atomic_init(&executor.calls, 0);
th_server_t ts;
@@ -998,7 +1004,7 @@ TEST(ui_server_routes_indexing_through_joinable_daemon_executor) {
ASSERT_GT(response_length, 0);
ASSERT_EQ(th_status(response), 202);
ASSERT_TRUE(called);
- ASSERT_STR_EQ(executor.root_path, root);
+ ASSERT_STR_EQ(executor.root_path, resolved_root);
ASSERT_STR_EQ(executor.project_name, "ui-project");
th_cleanup(root);
PASS();
@@ -1048,6 +1054,227 @@ TEST(ui_server_free_never_joins_active_index_worker) {
PASS();
}
+/* ── POST /api/index carries the resolved root ────────────────── */
+
+/* The route resolves the request's root_path, runs the workspace check on the
+ * resolved form, and the job must then carry exactly that string: what was
+ * checked is what the daemon receives. These cases drive the real endpoint
+ * with the recording executor and read every place the stored root surfaces
+ * (the executor argument, the 202 reply, /api/index-status). */
+
+typedef struct {
+ bool was_set;
+ char value[4096];
+} th_allowed_root_saved_t;
+
+/* Set (or, with NULL, clear) CBM_ALLOWED_ROOT and remember the previous value
+ * so the test can put it back before asserting — a failed assertion returns
+ * early, and a boundary left behind would leak into every later test. */
+static void th_allowed_root_set(th_allowed_root_saved_t *saved, const char *value) {
+ const char *current = getenv("CBM_ALLOWED_ROOT");
+ saved->was_set = current != NULL;
+ snprintf(saved->value, sizeof(saved->value), "%s", current ? current : "");
+ if (value) {
+ cbm_setenv("CBM_ALLOWED_ROOT", value, 1);
+ } else {
+ cbm_unsetenv("CBM_ALLOWED_ROOT");
+ }
+}
+
+static void th_allowed_root_restore(const th_allowed_root_saved_t *saved) {
+ if (saved->was_set) {
+ cbm_setenv("CBM_ALLOWED_ROOT", saved->value, 1);
+ } else {
+ cbm_unsetenv("CBM_ALLOWED_ROOT");
+ }
+}
+
+typedef struct {
+ th_ui_index_executor_t executor;
+ int status; /* HTTP status of the POST, -1 when no reply came */
+ bool executor_called; /* only waited for after a 202 */
+ char response[8192]; /* the raw POST reply */
+ char index_status[16384]; /* GET /api/index-status, read after the POST */
+} th_ui_index_probe_t;
+
+/* Response body: everything after the header block, or "" when absent. */
+static const char *th_response_body(const char *response) {
+ const char *body = strstr(response, "\r\n\r\n");
+ return body ? body + 4 : "";
+}
+
+/* Boot a server with the recording executor, POST one /api/index request,
+ * wait for the worker only when the route accepted the job, read
+ * /api/index-status and stop the server. Everything observed lands in the
+ * probe so the caller asserts after teardown. */
+static void th_ui_index_probe(th_ui_index_probe_t *probe, const char *root_path,
+ const char *project_name) {
+ memset(probe, 0, sizeof(*probe));
+ atomic_init(&probe->executor.calls, 0);
+ probe->status = -1;
+ th_server_t ts;
+ ts.srv = cbm_http_server_new(0);
+ if (!ts.srv) {
+ return;
+ }
+ cbm_http_server_set_index_executor(ts.srv, th_ui_index_executor, &probe->executor);
+ if (th_server_thread_start(&ts.tid, ts.srv) != 0) {
+ (void)cbm_http_server_free(ts.srv);
+ return;
+ }
+ int port = cbm_http_server_port(ts.srv);
+ char body[8192];
+ snprintf(body, sizeof(body), "{\"root_path\":\"%s\",\"project_name\":\"%s\"}", root_path,
+ project_name);
+ char request[8192 + 256];
+ snprintf(request, sizeof(request),
+ "POST /api/index HTTP/1.1\r\nContent-Type: application/json\r\n"
+ "Content-Length: %zu\r\n\r\n%s",
+ strlen(body), body);
+ if (th_http(port, request, probe->response, sizeof(probe->response)) > 0) {
+ probe->status = th_status(probe->response);
+ }
+ if (probe->status == 202) {
+ probe->executor_called = th_wait_atomic_int(&probe->executor.calls, 1, 2000);
+ }
+ char status_request[128];
+ snprintf(status_request, sizeof(status_request),
+ "GET /api/index-status HTTP/1.1\r\nHost: 127.0.0.1:%d\r\n\r\n", port);
+ (void)th_http(port, status_request, probe->index_status, sizeof(probe->index_status));
+ th_server_stop(&ts);
+}
+
+TEST(ui_server_index_start_refuses_root_outside_allowed) {
+ char *tmp = th_mktempdir("cbm_httpd_index_bound");
+ ASSERT_NOT_NULL(tmp);
+ char base[256];
+ snprintf(base, sizeof(base), "%s", tmp);
+ char allowed[512];
+ char outside[512];
+ snprintf(allowed, sizeof(allowed), "%s/allowed", base);
+ snprintf(outside, sizeof(outside), "%s/outside", base);
+ ASSERT_EQ(th_mkdir_p(allowed), 0);
+ ASSERT_EQ(th_mkdir_p(outside), 0);
+ /* The boundary is declared in resolved form, as allow-root records it. */
+ char canonical_base[4096];
+ ASSERT_TRUE(cbm_canonical_path(base, canonical_base, sizeof(canonical_base)));
+ char allowed_root[4096 + 16];
+ snprintf(allowed_root, sizeof(allowed_root), "%s/allowed", canonical_base);
+ /* Spelled under the allowed root, resolving beside it: the verdict has to
+ * come from the resolved form. */
+ char request_path[600];
+ snprintf(request_path, sizeof(request_path), "%s/allowed/../outside", base);
+
+ th_allowed_root_saved_t saved;
+ th_allowed_root_set(&saved, allowed_root);
+ th_ui_index_probe_t probe;
+ th_ui_index_probe(&probe, request_path, "bound");
+ th_allowed_root_restore(&saved);
+ th_cleanup(base);
+
+ ASSERT_EQ(probe.status, 403);
+ ASSERT_NOT_NULL(strstr(probe.response, "outside the allowed root"));
+ ASSERT_EQ(atomic_load(&probe.executor.calls), 0);
+ ASSERT_STR_EQ(th_response_body(probe.index_status), "[]");
+ PASS();
+}
+
+TEST(ui_server_index_start_stores_resolved_root) {
+ char *tmp = th_mktempdir("cbm_httpd_index_root");
+ ASSERT_NOT_NULL(tmp);
+ char base[256];
+ snprintf(base, sizeof(base), "%s", tmp);
+ char inner[512];
+ snprintf(inner, sizeof(inner), "%s/allowed/inner", base);
+ ASSERT_EQ(th_mkdir_p(inner), 0);
+ char canonical_base[4096];
+ ASSERT_TRUE(cbm_canonical_path(base, canonical_base, sizeof(canonical_base)));
+ char allowed_root[4096 + 16];
+ snprintf(allowed_root, sizeof(allowed_root), "%s/allowed", canonical_base);
+ /* The request names the allowed root through a ".." segment, so its
+ * spelling is not the resolved form (on macOS /tmp is itself a link to
+ * /private/tmp, so the prefix differs as well). */
+ char request_path[600];
+ snprintf(request_path, sizeof(request_path), "%s/allowed/inner/..", base);
+ ASSERT_TRUE(strcmp(request_path, allowed_root) != 0);
+
+ th_allowed_root_saved_t saved;
+ th_allowed_root_set(&saved, allowed_root);
+ th_ui_index_probe_t probe;
+ th_ui_index_probe(&probe, request_path, "resolved");
+ th_allowed_root_restore(&saved);
+ th_cleanup(base);
+
+ ASSERT_EQ(probe.status, 202);
+ ASSERT_TRUE(probe.executor_called);
+ /* The daemon receives the string the check ran on, not the request
+ * spelling. */
+ ASSERT_STR_EQ(probe.executor.root_path, allowed_root);
+ ASSERT_STR_EQ(probe.executor.project_name, "resolved");
+ /* The 202 reply and the status listing report that same stored root. */
+ char expected[8192 + 64];
+ snprintf(expected, sizeof(expected), "\"path\":\"%s\"}", allowed_root);
+ ASSERT_NOT_NULL(strstr(probe.response, expected));
+ char escaped[8192];
+ cbm_json_escape(escaped, (int)sizeof(escaped), allowed_root);
+ snprintf(expected, sizeof(expected), "\"path\":\"%s\",\"error\"", escaped);
+ ASSERT_NOT_NULL(strstr(probe.index_status, expected));
+ PASS();
+}
+
+TEST(ui_server_index_start_long_root_not_truncated) {
+ char *tmp = th_mktempdir("cbm_httpd_index_long");
+ ASSERT_NOT_NULL(tmp);
+ char base[256];
+ snprintf(base, sizeof(base), "%s", tmp);
+ /* A root well past 1 KB, the capacity the job used to hold, and well
+ * inside the 4 KB the route resolves; 200-byte components keep every
+ * component under NAME_MAX. */
+ char component[201];
+ memset(component, 'd', sizeof(component) - 1);
+ component[sizeof(component) - 1] = '\0';
+ char deep[2048];
+ int n = snprintf(deep, sizeof(deep), "%s", base);
+ while (n < 1400) {
+ n += snprintf(deep + n, sizeof(deep) - (size_t)n, "/%s", component);
+ }
+ bool created = th_mkdir_p(deep) == 0;
+ char canonical[4096];
+ bool resolved = created && cbm_canonical_path(deep, canonical, sizeof(canonical)) != 0;
+
+ /* No boundary here: the question is the length the job keeps. */
+ th_allowed_root_saved_t saved;
+ th_allowed_root_set(&saved, NULL);
+ th_ui_index_probe_t probe;
+ th_ui_index_probe(&probe, deep, "long");
+ th_allowed_root_restore(&saved);
+ th_cleanup(base);
+
+ if (probe.status == 202) {
+ /* The platform resolved the path, so the worker has to receive it
+ * whole — the same string the route checked. */
+ ASSERT_TRUE(resolved);
+ ASSERT_TRUE(probe.executor_called);
+ ASSERT_TRUE(strlen(probe.executor.root_path) > 1023);
+ ASSERT_STR_EQ(probe.executor.root_path, canonical);
+ } else {
+ /* The platform cannot resolve a path this long (macOS: PATH_MAX is
+ * 1024, so mkdir and stat refuse it before the route sees it); the
+ * route answers 400 and keeps nothing. */
+ ASSERT_EQ(probe.status, 400);
+ ASSERT_EQ(atomic_load(&probe.executor.calls), 0);
+ ASSERT_STR_EQ(th_response_body(probe.index_status), "[]");
+ }
+#if defined(PATH_MAX) && PATH_MAX >= 2048
+ /* Linux resolves it (PATH_MAX 4096), and that is the one platform where a
+ * cut-off could reach the worker; the accepted branch is the only honest
+ * outcome there, so the refusal branch may not stand in for it. */
+ ASSERT_TRUE(created);
+ ASSERT_EQ(probe.status, 202);
+#endif
+ PASS();
+}
+
TEST(ui_server_root_without_embedded_assets_is_not_found) {
/* The frontend is linked into the image, so its availability is decided at
* build time, not warmed at runtime: a binary built without --with-ui has
@@ -2318,8 +2545,8 @@ TEST(ui_server_logs_escape_dense_no_overflow) {
/* The index-status endpoint renders every active job into a fixed 2 KB stack
* buffer. http_appendf clamps its own writes, but the separator and the closing
- * bracket were raw indexes, so two jobs holding ~1 KB root paths (the field is
- * 1024 bytes and the value comes straight from POST /api/index) pushed pos to
+ * bracket were raw indexes, so two jobs holding ~1 KB root paths (the field
+ * was 1024 bytes and now holds the 4 KB resolved root) pushed pos to
* the clamp and the close then wrote past the buffer. Drive it through the real
* endpoint with the index executor stubbed out, in a forked child so the
* overflow surfaces as a killing signal. */
@@ -2458,6 +2685,9 @@ SUITE(httpd) {
RUN_TEST(ui_server_process_kill_route_is_unavailable);
RUN_TEST(ui_server_routes_indexing_through_joinable_daemon_executor);
RUN_TEST(ui_server_free_never_joins_active_index_worker);
+ RUN_TEST(ui_server_index_start_refuses_root_outside_allowed);
+ RUN_TEST(ui_server_index_start_stores_resolved_root);
+ RUN_TEST(ui_server_index_start_long_root_not_truncated);
RUN_TEST(ui_server_root_without_embedded_assets_is_not_found);
RUN_TEST(ui_server_same_origin_request_is_allowed);
RUN_TEST(ui_server_rejects_foreign_and_null_origins);