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.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
feat(ui): open the /rpc allowlist to the read-only query surface
Martin Vogeldab11a7d3173
diff --git a/src/ui/http_server.c b/src/ui/http_server.cindex 18589d8d8..540a97cb3 100644--- a/src/ui/http_server.c+++ b/src/ui/http_server.c@@ -1709,6 +1709,50 @@ static yyjson_val *json_unique_member(yyjson_val *object, const char *name) {return found;}+/* Tools the browser may call through /rpc: the read-only query surface and+ * nothing that mutates the index. Writes (index_repository, delete_project,+ * ingest_traces, manage_adr updates) stay on the dedicated /api routes or the+ * MCP transport, where the daemon's mutation guard applies. CBM Atlas is the+ * human's window on the graph; this list is what that window may see. */+static const char *const UI_RPC_READ_TOOLS[] = {+ "list_projects", "get_code_snippet", "get_graph_schema", "search_graph",+ "search_code", "trace_path", "trace_call_path", "get_architecture",+ "query_graph", "detect_changes", "check_index_coverage", "index_status",+};++static bool rpc_tool_is_read_only(const char *name) {+ for (size_t i = 0; i < sizeof(UI_RPC_READ_TOOLS) / sizeof(UI_RPC_READ_TOOLS[0]); i++) {+ if (strcmp(name, UI_RPC_READ_TOOLS[i]) == 0)+ return true;+ }+ return false;+}++/* manage_adr is read-only only in its "get" (the default) and "sections"+ * modes; anything else writes the document and is refused here. A duplicated+ * or non-string "mode" is ambiguous and refused the same way. */+static bool rpc_manage_adr_is_read(yyjson_val *params) {+ yyjson_val *arguments = json_unique_member(params, "arguments");+ if (!arguments)+ return true; /* no arguments → default mode "get" */+ if (!yyjson_is_obj(arguments))+ return false;+ bool mode_seen = false;+ size_t index, maximum;+ yyjson_val *key, *value;+ yyjson_obj_foreach(arguments, index, maximum, key, value) {+ if (strcmp(yyjson_get_str(key), "mode") != 0)+ continue;+ if (mode_seen || !yyjson_is_str(value))+ return false;+ mode_seen = true;+ const char *mode_text = yyjson_get_str(value);+ if (strcmp(mode_text, "get") != 0 && strcmp(mode_text, "sections") != 0)+ return false;+ }+ return true;+}+static bool rpc_is_allowed_for_ui(const char *body, size_t body_len) {yyjson_doc *document = yyjson_read(body, body_len, 0);if (!document)@@ -1719,9 +1763,13 @@ static bool rpc_is_allowed_for_ui(const char *body, size_t body_len) {yyjson_val *name = json_unique_member(params, "name");const char *method_text = yyjson_is_str(method) ? yyjson_get_str(method) : NULL;const char *name_text = yyjson_is_str(name) ? yyjson_get_str(name) : NULL;- bool allowed =- method_text && strcmp(method_text, "tools/call") == 0 && name_text &&- (strcmp(name_text, "list_projects") == 0 || strcmp(name_text, "get_code_snippet") == 0);+ bool allowed = false;+ if (method_text && strcmp(method_text, "tools/call") == 0 && name_text) {+ if (rpc_tool_is_read_only(name_text))+ allowed = true;+ else if (strcmp(name_text, "manage_adr") == 0)+ allowed = rpc_manage_adr_is_read(params);+ }yyjson_doc_free(document);return allowed;}diff --git a/tests/test_httpd.c b/tests/test_httpd.cindex 0d2f82402..18a1a5b00 100644--- a/tests/test_httpd.c+++ b/tests/test_httpd.c@@ -1086,22 +1086,36 @@ TEST(ui_server_mutations_require_json_content_type) {TEST(ui_server_rpc_allows_only_ui_read_tools) {th_server_t ts;ASSERT_EQ(th_server_start(&ts), 0);- const char *body = "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\","- "\"params\":{\"name\":\"list_projects\",\"arguments\":{}}}";+ /* CBM Atlas reads the graph through /rpc: every read-only query tool must+ * pass the allowlist (a 403 on get_graph_schema is #1663). HTTP 200 means+ * "allowed and dispatched" — tool-level argument errors still ride inside+ * a 200 JSON-RPC envelope. */+ static const char *allowed_tools[] = {+ "list_projects", "get_code_snippet", "get_graph_schema", "search_graph",+ "search_code", "trace_path", "trace_call_path", "get_architecture",+ "query_graph", "detect_changes", "check_index_coverage", "index_status",+ };char req[1024];- snprintf(req, sizeof(req),- "POST /rpc HTTP/1.1\r\n"- "Content-Type: application/json\r\n"- "Content-Length: %d\r\n\r\n%s",- (int)strlen(body), body);char resp[8192];- int n = th_http(cbm_http_server_port(ts.srv), req, resp, sizeof(resp));- ASSERT_GT(n, 0);- ASSERT_EQ(th_status(resp), 200);- ASSERT_NOT_NULL(strstr(resp, "\"jsonrpc\""));+ int n;+ for (size_t i = 0; i < sizeof(allowed_tools) / sizeof(allowed_tools[0]); i++) {+ char body[512];+ snprintf(body, sizeof(body),+ "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"%s\",\"arguments\":{}}}",+ allowed_tools[i]);+ snprintf(req, sizeof(req),+ "POST /rpc HTTP/1.1\r\n"+ "Content-Type: application/json\r\n"+ "Content-Length: %d\r\n\r\n%s",+ (int)strlen(body), body);+ n = th_http(cbm_http_server_port(ts.srv), req, resp, sizeof(resp));+ ASSERT_GT(n, 0);+ ASSERT_EQ(th_status(resp), 200);+ ASSERT_NOT_NULL(strstr(resp, "\"jsonrpc\""));+ }- static const char *blocked_tools[] = {"delete_project", "manage_adr", "ingest_traces",- "index_repository"};+ static const char *blocked_tools[] = {"delete_project", "ingest_traces", "index_repository"};for (size_t i = 0; i < sizeof(blocked_tools) / sizeof(blocked_tools[0]); i++) {char blocked_body[512];snprintf(blocked_body, sizeof(blocked_body),@@ -1117,6 +1131,34 @@ TEST(ui_server_rpc_allows_only_ui_read_tools) {ASSERT_EQ(th_status(resp), 403);}+ /* manage_adr: the read modes pass, everything that writes is refused. */+ static const struct {+ const char *arguments;+ int expected_status;+ } adr_cases[] = {+ {"{}", 200}, /* no mode → default "get" */+ {"{\"mode\":\"get\"}", 200}, /* explicit read */+ {"{\"mode\":\"sections\"}", 200}, /* header listing */+ {"{\"mode\":\"update\"}", 403}, /* write */+ {"{\"mode\":\"store\"}", 403}, /* legacy write alias */+ {"{\"mode\":1}", 403}, /* non-string mode */+ {"{\"mode\":\"get\",\"mode\":\"update\"}", 403}, /* smuggled duplicate */+ };+ for (size_t i = 0; i < sizeof(adr_cases) / sizeof(adr_cases[0]); i++) {+ char adr_body[512];+ snprintf(adr_body, sizeof(adr_body),+ "{\"jsonrpc\":\"2.0\",\"id\":5,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"manage_adr\",\"arguments\":%s}}",+ adr_cases[i].arguments);+ snprintf(req, sizeof(req),+ "POST /rpc HTTP/1.1\r\nContent-Type: application/json\r\n"+ "Content-Length: %zu\r\n\r\n%s",+ strlen(adr_body), adr_body);+ n = th_http(cbm_http_server_port(ts.srv), req, resp, sizeof(resp));+ ASSERT_GT(n, 0);+ ASSERT_EQ(th_status(resp), adr_cases[i].expected_status);+ }+const char *initialize = "{\"jsonrpc\":\"2.0\",\"id\":3,\"method\":\"initialize\",""\"params\":{}}";snprintf(req, sizeof(req),