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): size the index-status body for the roots the table holds

Martin Vogel7077695f2990
diff --git a/src/ui/http_server.c b/src/ui/http_server.c
index 7403897e7..f9f2ff0c6 100644
--- a/src/ui/http_server.c
+++ b/src/ui/http_server.c
@@ -28,6 +28,7 @@
#endif
/* pipeline.h no longer needed — indexing runs as subprocess */
#include "foundation/log.h"
+#include "foundation/mem_core.h"
#include "foundation/platform.h"
#include "foundation/secure_random.h"
#include "foundation/sha256.h"
@@ -164,6 +165,13 @@ static void handle_ui_config(cbm_http_conn_t *c, const cbm_http_req_t *req) {
* 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
+#define INDEX_JOB_ERROR_CAP 256
+/* GET /api/index-status sizes its body from the table: per entry the framing
+ * below ({"slot":N,"status":"indexing","path":"","error":""} and a separator,
+ * well under 64 bytes) plus both strings at cbm_json_escape's widest expansion,
+ * six bytes for a control character (\u00XX). */
+#define INDEX_STATUS_ENTRY_FRAMING 64
+#define JSON_ESCAPE_MAX_EXPANSION 6
enum {
HTTP_RUN_IDLE = 0,
@@ -177,7 +185,7 @@ typedef struct {
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];
+ char error_msg[INDEX_JOB_ERROR_CAP];
cbm_thread_t thread;
bool thread_started;
atomic_int completed;
@@ -1256,44 +1264,67 @@ static void handle_index_start(cbm_http_server_t *server, cbm_http_conn_t *c,
}
job->thread_started = true;
+ /* The reply names the stored root; escape it as the status listing does,
+ * or a quote or backslash in the path ends the JSON string early. */
+ char esc_path[JSON_ESCAPE_MAX_EXPANSION * INDEX_JOB_ROOT_CAP];
+ cbm_json_escape(esc_path, (int)sizeof(esc_path), job->root_path);
cbm_http_replyf(c, 202, g_cors_json, "{\"status\":\"indexing\",\"slot\":%d,\"path\":\"%s\"}",
- slot, job->root_path);
+ slot, esc_path);
+}
+
+/* Append src JSON-escaped, under http_appendf's clamp contract: once *pos has
+ * reached bufsz nothing more is written. */
+static void http_append_json_escaped(char *buf, size_t bufsz, int *pos, const char *src) {
+ if (*pos < 0) {
+ return;
+ }
+ if ((size_t)*pos >= bufsz) {
+ *pos = (int)bufsz;
+ return;
+ }
+ *pos += cbm_json_escape(buf + *pos, (int)(bufsz - (size_t)*pos), src);
}
/* GET /api/index-status — returns status of all index jobs */
static void handle_index_status(cbm_http_server_t *server, cbm_http_conn_t *c) {
- char buf[2048] = "[";
- int pos = 1;
+ /* Size the body for a full table: every slot's framing plus its root and
+ * error message at the widest escape. A fixed 2 KB stack buffer used to
+ * clamp here, so one long resolved root (the field holds 4 KB) came back
+ * as a cut JSON body with status 200. The append helpers keep clamping,
+ * so this calculation is not the only thing keeping pos in range. */
+ size_t buf_size = 2 + (size_t)MAX_INDEX_JOBS * (INDEX_STATUS_ENTRY_FRAMING +
+ JSON_ESCAPE_MAX_EXPANSION *
+ (INDEX_JOB_ROOT_CAP + INDEX_JOB_ERROR_CAP));
+ char *buf = cbm_alloc(CBM_MEM_CLASS_OTHER, buf_size);
+ if (!buf) {
+ cbm_http_replyf(c, 500, g_cors_json, "{\"error\":\"oom\"}");
+ return;
+ }
+ int pos = 0;
+ http_appendf(buf, buf_size, &pos, "[");
for (int i = 0; i < MAX_INDEX_JOBS; i++) {
int st = atomic_load(&server->index_jobs[i].status);
if (st == 0)
continue;
if (pos > 1)
- http_appendf(buf, sizeof(buf), &pos, ",");
+ http_appendf(buf, buf_size, &pos, ",");
const char *ss = st == 1 ? "indexing" : st == 2 ? "done" : "error";
- /* 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),
- st == 3 ? server->index_jobs[i].error_msg : "");
- http_appendf(buf, sizeof(buf), &pos,
- "{\"slot\":%d,\"status\":\"%s\",\"path\":\"%s\",\"error\":\"%s\"}", i, ss,
- esc_path, esc_error);
- }
- http_appendf(buf, sizeof(buf), &pos, "]");
- if ((size_t)pos >= sizeof(buf)) {
- pos = (int)sizeof(buf) - 1;
+ /* Both fields are free-form, so escape them — a quote in a path would
+ * otherwise end its JSON string early. */
+ http_appendf(buf, buf_size, &pos, "{\"slot\":%d,\"status\":\"%s\",\"path\":\"", i, ss);
+ http_append_json_escaped(buf, buf_size, &pos, server->index_jobs[i].root_path);
+ http_appendf(buf, buf_size, &pos, "\",\"error\":\"");
+ http_append_json_escaped(buf, buf_size, &pos,
+ st == 3 ? server->index_jobs[i].error_msg : "");
+ http_appendf(buf, buf_size, &pos, "\"}");
+ }
+ http_appendf(buf, buf_size, &pos, "]");
+ if ((size_t)pos >= buf_size) {
+ pos = (int)buf_size - 1;
}
buf[pos] = '\0';
- cbm_http_replyf(c, 200, g_cors_json, "%s", buf);
+ cbm_http_reply_buf(c, 200, g_cors_json, buf, (size_t)pos);
+ cbm_free(CBM_MEM_CLASS_OTHER, buf);
}
static void unwatch_project(cbm_http_server_t *srv, const char *name) {
diff --git a/tests/test_httpd.c b/tests/test_httpd.c
index ee024a4d6..d020dc7da 100644
--- a/tests/test_httpd.c
+++ b/tests/test_httpd.c
@@ -29,6 +29,7 @@
#include "ui/http_server.h"
#include <store/store.h>
#include <watcher/watcher.h>
+#include <yyjson/yyjson.h> /* the index replies are asserted as parsed JSON */
#include <limits.h> /* PATH_MAX: which platforms resolve a root past 1 KB */
#include <stdio.h>
@@ -1275,6 +1276,60 @@ TEST(ui_server_index_start_long_root_not_truncated) {
PASS();
}
+/* The 202 reply names the stored root; a quote or backslash in it has to
+ * travel escaped or the reply is not JSON. */
+TEST(ui_server_index_start_reply_escapes_path) {
+ char *tmp = th_mktempdir("cbm_httpd_index_quote");
+ ASSERT_NOT_NULL(tmp);
+ char base[256];
+ snprintf(base, sizeof(base), "%s", tmp);
+ char root[512];
+#ifdef _WIN32
+ /* A quote is not a legal file-name byte on Windows; the separators of the
+ * resolved path already need escaping there. */
+ snprintf(root, sizeof(root), "%s\\plain", base);
+#else
+ snprintf(root, sizeof(root), "%s/q\"uote", base);
+#endif
+ ASSERT_EQ(th_mkdir_p(root), 0);
+ char canonical[4096];
+ ASSERT_TRUE(cbm_canonical_path(root, canonical, sizeof(canonical)));
+ /* The request body is JSON too, so the root goes in escaped. */
+ char request_path[1024];
+ cbm_json_escape(request_path, (int)sizeof(request_path), root);
+
+ th_ui_index_probe_t probe;
+ th_ui_index_probe(&probe, request_path, "quoted");
+ th_cleanup(base);
+
+ bool parsed = false;
+ int slot = -1;
+ char reply_path[4096] = "";
+ const char *body = th_response_body(probe.response);
+ yyjson_doc *doc = yyjson_read(body, strlen(body), 0);
+ if (doc) {
+ yyjson_val *obj = yyjson_doc_get_root(doc);
+ yyjson_val *path = yyjson_obj_get(obj, "path");
+ yyjson_val *slot_val = yyjson_obj_get(obj, "slot");
+ parsed = yyjson_is_obj(obj);
+ if (yyjson_is_str(path)) {
+ snprintf(reply_path, sizeof(reply_path), "%s", yyjson_get_str(path));
+ }
+ if (yyjson_is_int(slot_val)) {
+ slot = yyjson_get_int(slot_val);
+ }
+ yyjson_doc_free(doc);
+ }
+
+ ASSERT_EQ(probe.status, 202);
+ ASSERT_TRUE(probe.executor_called);
+ ASSERT_STR_EQ(probe.executor.root_path, canonical);
+ ASSERT_TRUE(parsed);
+ ASSERT_STR_EQ(reply_path, canonical);
+ ASSERT_TRUE(slot >= 0);
+ 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
@@ -2644,6 +2699,173 @@ TEST(ui_server_index_status_long_paths_no_overflow) {
#endif
}
+/* Grow path in place to exactly target bytes with 'd' components, none
+ * longer than 200 so every component stays under NAME_MAX. */
+static void th_pad_path_to(char *path, size_t cap, int target) {
+ char fill[201];
+ memset(fill, 'd', sizeof(fill) - 1);
+ fill[sizeof(fill) - 1] = '\0';
+ int n = (int)strlen(path);
+ while (n < target && (size_t)n + 2 < cap) {
+ int room = target - n - 1; /* after the '/' */
+ int len = room > 200 ? 200 : room;
+ if (len <= 0) {
+ break;
+ }
+ n += snprintf(path + n, cap - (size_t)n, "/%.*s", len, fill);
+ }
+}
+
+/* Parse an index-status body and compare each entry's "path" with the root
+ * expected for its slot. Results go to out-params so the caller can tear
+ * down before asserting. */
+static void th_index_status_check(const char *body, int expected_count, char expected[][4096],
+ bool *parsed, int *count, bool *paths_whole) {
+ *parsed = false;
+ *count = 0;
+ *paths_whole = false;
+ yyjson_doc *doc = yyjson_read(body, strlen(body), 0);
+ if (!doc) {
+ return;
+ }
+ yyjson_val *arr = yyjson_doc_get_root(doc);
+ if (yyjson_is_arr(arr)) {
+ *parsed = true;
+ *count = (int)yyjson_arr_size(arr);
+ bool whole = *count == expected_count;
+ size_t idx;
+ size_t max_entries;
+ yyjson_val *entry;
+ yyjson_arr_foreach(arr, idx, max_entries, entry) {
+ yyjson_val *slot = yyjson_obj_get(entry, "slot");
+ yyjson_val *path = yyjson_obj_get(entry, "path");
+ int s = yyjson_is_int(slot) ? yyjson_get_int(slot) : -1;
+ whole = whole && s >= 0 && s < expected_count && yyjson_is_str(path) &&
+ strcmp(yyjson_get_str(path), expected[s]) == 0;
+ }
+ *paths_whole = whole;
+ }
+ yyjson_doc_free(doc);
+}
+
+/* The listing is sized for what the job table holds. Every slot takes a root
+ * as long as the platform resolves; the body has to parse as JSON and carry
+ * each root whole, first with one job and then with the table full. Roots
+ * enter the table the way the clamp test's do: real directories through the
+ * route, held open by the blocking executor so every slot stays occupied. */
+TEST(ui_server_index_status_long_roots_render_whole) {
+ char *tmp = th_mktempdir("cbm_status_whole");
+ ASSERT_NOT_NULL(tmp);
+ char base[256];
+ snprintf(base, sizeof(base), "%s", tmp);
+ /* Linux: just under the 4 KB field; the 4 KB request body still wraps it. */
+ int target = 4096 - 160;
+#if defined(_WIN32)
+ /* MAX_PATH: long roots are not characterised there; the listing is still
+ * checked for shape and content with every slot occupied. */
+ target = 200;
+#elif defined(PATH_MAX) && PATH_MAX < 4096
+ /* macOS: the longest root the route can resolve; four still exceed 2 KB. */
+ target = PATH_MAX - 96;
+#endif
+ char deep[MAX_TEST_INDEX_JOBS][4096];
+ char expected[MAX_TEST_INDEX_JOBS][4096];
+ for (int j = 0; j < MAX_TEST_INDEX_JOBS; j++) {
+ snprintf(deep[j], sizeof(deep[j]), "%s/%d", base, j);
+ th_pad_path_to(deep[j], sizeof(deep[j]), target);
+ ASSERT_EQ((int)strlen(deep[j]), target);
+ ASSERT_EQ(th_mkdir_p(deep[j]), 0);
+ ASSERT_TRUE(cbm_canonical_path(deep[j], expected[j], sizeof(expected[j])));
+ }
+
+ th_ui_blocking_index_executor_t executor = {0};
+ atomic_init(&executor.calls, 0);
+ atomic_init(&executor.release, 0);
+ th_server_t ts;
+ ts.srv = cbm_http_server_new(0);
+ ASSERT_NOT_NULL(ts.srv);
+ cbm_http_server_set_index_executor(ts.srv, th_ui_blocking_index_executor, &executor);
+ ASSERT_EQ(th_server_thread_start(&ts.tid, ts.srv), 0);
+ int port = cbm_http_server_port(ts.srv);
+ 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);
+
+ int accepted = 0;
+ bool started = true;
+ bool one_parsed = false;
+ bool one_whole = false;
+ bool all_parsed = false;
+ bool all_whole = false;
+ int one_count = 0;
+ int all_count = 0;
+ size_t one_body_len = 0;
+ size_t all_body_len = 0;
+ char listing[65536];
+ for (int j = 0; j < MAX_TEST_INDEX_JOBS; j++) {
+ char body[4600];
+ snprintf(body, sizeof(body), "{\"root_path\":\"%s\",\"project_name\":\"p%d\"}", deep[j], j);
+ char request[4900];
+ 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);
+ char response[8192];
+ int rn = th_http(port, request, response, sizeof(response));
+ if (rn > 0 && th_status(response) == 202) {
+ accepted++;
+ }
+ /* Wait for the state the listing depends on: this job running. */
+ started = started && th_wait_atomic_int(&executor.calls, j + 1, 5000);
+ if (j == 0 || j == MAX_TEST_INDEX_JOBS - 1) {
+ int n = th_http(port, status_request, listing, sizeof(listing));
+ const char *status_body = n > 0 ? th_response_body(listing) : "";
+ if (j == 0) {
+ one_body_len = strlen(status_body);
+ th_index_status_check(status_body, 1, expected, &one_parsed, &one_count,
+ &one_whole);
+ } else {
+ all_body_len = strlen(status_body);
+ th_index_status_check(status_body, MAX_TEST_INDEX_JOBS, expected, &all_parsed,
+ &all_count, &all_whole);
+ }
+ }
+ }
+ /* Release the workers and tear down; free refuses until each worker has
+ * flagged completion, the same way the active-worker test waits. */
+ atomic_store(&executor.release, 1);
+ cbm_http_server_stop(ts.srv);
+ ASSERT_EQ(cbm_thread_join(&ts.tid), 0);
+ bool freed = false;
+ uint64_t deadline = cbm_now_ms() + 2000;
+ while (!freed && cbm_now_ms() < deadline) {
+ freed = cbm_http_server_free(ts.srv);
+ if (!freed)
+ cbm_usleep(1000);
+ }
+ th_cleanup(base);
+
+ ASSERT_TRUE(freed);
+ ASSERT_EQ(accepted, MAX_TEST_INDEX_JOBS);
+ ASSERT_TRUE(started);
+ if (!one_parsed) {
+ char m[128];
+ snprintf(m, sizeof(m), "one-job index-status body of %zu bytes is not JSON", one_body_len);
+ FAIL(m);
+ }
+ ASSERT_EQ(one_count, 1);
+ ASSERT_TRUE(one_whole);
+ if (!all_parsed) {
+ char m[128];
+ snprintf(m, sizeof(m), "full-table index-status body of %zu bytes is not JSON",
+ all_body_len);
+ FAIL(m);
+ }
+ ASSERT_EQ(all_count, MAX_TEST_INDEX_JOBS);
+ ASSERT_TRUE(all_whole);
+ PASS();
+}
+
/* ── Suite ────────────────────────────────────────────────────── */
SUITE(httpd) {
@@ -2651,6 +2873,7 @@ SUITE(httpd) {
RUN_TEST(ui_server_browse_wide_dir_no_overflow);
RUN_TEST(ui_server_logs_escape_dense_no_overflow);
RUN_TEST(ui_server_index_status_long_paths_no_overflow);
+ RUN_TEST(ui_server_index_status_long_roots_render_whole);
/* Parser / helpers */
RUN_TEST(httpd_parse_simple_get);
RUN_TEST(httpd_parse_security_headers_and_rejects_duplicates);
@@ -2688,6 +2911,7 @@ SUITE(httpd) {
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_index_start_reply_escapes_path);
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);