Skip to content

Commit 0488acf

Browse files
authored
Merge pull request DeusData#888 from DeusData/distill/887-traversal-depth-caps
fix: cap client-driven traversal depth (var-length Cypher + MCP tools)
2 parents 582c124 + 98e98c3 commit 0488acf

6 files changed

Lines changed: 204 additions & 3 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#include "cypher/cypher.h"
99
#include "store/store.h"
1010
#include "foundation/platform.h"
11+
#include "foundation/limits.h"
12+
#include "foundation/log.h"
1113

1214
enum {
1315
CYP_BUF_16 = 16,
@@ -22,7 +24,6 @@ enum {
2224
CYP_MAX_VARS = 16, /* max Cypher variables in a query */
2325
CYP_MAX_EDGE_VARS = 8, /* max edge variables */
2426
CYP_GROWTH_10 = 10, /* binding growth factor */
25-
CYP_MAX_DEPTH = 10, /* max variable-length path depth */
2627
CYP_CHAR_IDX1 = 1, /* second character index (e.g. op[1]) */
2728
CYP_EBUF_MASK = 7,
2829
CYP_NODE_COLS = 4, /* columns per node var: name, qn, label, file */
@@ -2886,7 +2887,20 @@ static void expand_var_length(cbm_store_t *store, cbm_rel_pattern_t *rel,
28862887
cbm_node_pattern_t *target_node, binding_t *b, cbm_node_t *src,
28872888
const char *to_var, binding_t *new_bindings, int *new_count,
28882889
int max_new, int *match_count) {
2889-
int max_depth = rel->max_hops > 0 ? rel->max_hops : CYP_MAX_DEPTH;
2890+
/* Clamp BOTH the explicit (`*1..N`) and unbounded (`*`, `*..m`) forms to the
2891+
* engine ceiling: an explicit N above the cap was previously honoured
2892+
* verbatim, driving cbm_store_bfs to an unbounded hop count (#887). WARN on
2893+
* clamp — never a silent truncation. */
2894+
int depth_cap = cbm_cypher_max_depth();
2895+
int max_depth = rel->max_hops > 0 ? rel->max_hops : depth_cap;
2896+
if (max_depth > depth_cap) {
2897+
char req_buf[16];
2898+
char cap_buf[16];
2899+
snprintf(req_buf, sizeof(req_buf), "%d", max_depth);
2900+
snprintf(cap_buf, sizeof(cap_buf), "%d", depth_cap);
2901+
cbm_log_warn("cypher.depth_capped", "requested", req_buf, "cap", cap_buf);
2902+
max_depth = depth_cap;
2903+
}
28902904
cbm_traverse_result_t tr = {0};
28912905
const char *dir = rel->direction ? rel->direction : "outbound";
28922906
cbm_store_bfs(store, src->id, dir, rel->types, rel->type_count, max_depth, CBM_PERCENT, &tr);

‎src/foundation/limits.c‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "foundation/limits.h"
55

66
#include <errno.h>
7+
#include <limits.h>
78
#include <stdlib.h>
89

910
long cbm_max_file_bytes(void) {
@@ -24,3 +25,32 @@ long cbm_max_file_bytes(void) {
2425
}
2526
return default_cap;
2627
}
28+
29+
/* Shared env-int parser: a positive integer in [1, INT_MAX], else the fallback.
30+
* Read fresh each call (see cbm_max_file_bytes rationale — cheap, test-friendly,
31+
* no stale memoized copy across runs). */
32+
static int env_positive_int(const char *name, int fallback) {
33+
const char *raw = getenv(name);
34+
if (raw && raw[0]) {
35+
errno = 0;
36+
char *end = NULL;
37+
long v = strtol(raw, &end, 10);
38+
if (errno == 0 && end != raw && *end == '\0' && v > 0 && v <= INT_MAX) {
39+
return (int)v;
40+
}
41+
/* Unparseable / non-positive / out-of-range → safe default. */
42+
}
43+
return fallback;
44+
}
45+
46+
int cbm_cypher_max_depth(void) {
47+
/* 10 — generous for a code call/def graph; an explicit `*1..N` above this is
48+
* WARN-capped, never an unbounded (cyclic-graph DoS) traversal. */
49+
return env_positive_int("CBM_CYPHER_MAX_DEPTH", 10);
50+
}
51+
52+
int cbm_mcp_max_depth(void) {
53+
/* 15 — ceiling for client-driven MCP graph traversals (trace_call_path,
54+
* detect_changes); the caller's `depth` is WARN-clamped to this. */
55+
return env_positive_int("CBM_MCP_MAX_DEPTH", 15);
56+
}

‎src/foundation/limits.h‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,17 @@ typedef enum {
3030
* leaking across runs. */
3131
long cbm_max_file_bytes(void);
3232

33+
/* Maximum variable-length path depth for the Cypher engine (the `*min..max`
34+
* hop ceiling). BOTH the explicit (`*1..N`) and unbounded (`*`, `*..m`) forms
35+
* are clamped to this, so `[:CALLS*1..1000000]` degrades to a WARN-and-cap
36+
* rather than an unbounded (cyclic-graph DoS) traversal. Override with
37+
* CBM_CYPHER_MAX_DEPTH (a positive integer). Default 10. */
38+
int cbm_cypher_max_depth(void);
39+
40+
/* Maximum traversal depth for client-driven MCP graph tools (trace_call_path,
41+
* detect_changes): the client `depth` argument is WARN-clamped to this so an
42+
* arbitrarily large value cannot drive an unbounded BFS over the shared store.
43+
* Override with CBM_MCP_MAX_DEPTH (a positive integer). Default 15. */
44+
int cbm_mcp_max_depth(void);
45+
3346
#endif /* CBM_LIMITS_H */

‎src/mcp/mcp.c‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ enum {
1414
MCP_TIMEOUT_MS = 1000,
1515
MCP_HALF_SEC_US = 500000,
1616
MCP_MAX_ROWS = 100,
17-
MCP_MAX_DEPTH = 15,
1817
MCP_COL_2 = 2,
1918
MCP_COL_3 = 3,
2019
MCP_COL_4 = 4,
@@ -54,6 +53,7 @@ enum {
5453
#include "foundation/compat_fs.h"
5554
#include "foundation/compat_thread.h"
5655
#include "foundation/log.h"
56+
#include "foundation/limits.h"
5757
#include "mcp/index_supervisor.h"
5858
#include "foundation/str_util.h"
5959
#include "foundation/dump_verify.h"
@@ -2933,6 +2933,22 @@ static void bfs_union_same_name(cbm_store_t *store, const cbm_node_t *nodes, int
29332933
}
29342934
}
29352935

2936+
/* Clamp a client-supplied traversal depth to the MCP ceiling (cbm_mcp_max_depth),
2937+
* WARN-logging when it does so — never a silent truncation (#887). An unclamped
2938+
* `depth` would drive the shared cbm_store_bfs to an arbitrary hop count. */
2939+
static int clamp_mcp_depth(int depth, const char *tool) {
2940+
int cap = cbm_mcp_max_depth();
2941+
if (depth > cap) {
2942+
char req_buf[16];
2943+
char cap_buf[16];
2944+
snprintf(req_buf, sizeof(req_buf), "%d", depth);
2945+
snprintf(cap_buf, sizeof(cap_buf), "%d", cap);
2946+
cbm_log_warn("mcp.depth_capped", "tool", tool, "requested", req_buf, "cap", cap_buf);
2947+
return cap;
2948+
}
2949+
return depth;
2950+
}
2951+
29362952
static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
29372953
char *func_name = cbm_mcp_get_string_arg(args, "function_name");
29382954
char *project = get_project_arg(args);
@@ -2941,6 +2957,7 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
29412957
char *mode = cbm_mcp_get_string_arg(args, "mode");
29422958
char *param_name = cbm_mcp_get_string_arg(args, "parameter_name");
29432959
int depth = cbm_mcp_get_int_arg(args, "depth", MCP_DEFAULT_DEPTH);
2960+
depth = clamp_mcp_depth(depth, "trace_call_path");
29442961
bool risk_labels = cbm_mcp_get_bool_arg(args, "risk_labels");
29452962
bool include_tests = cbm_mcp_get_bool_arg(args, "include_tests");
29462963

@@ -5201,6 +5218,7 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
52015218
char *since = cbm_mcp_get_string_arg(args, "since");
52025219
char *scope = cbm_mcp_get_string_arg(args, "scope");
52035220
int depth = cbm_mcp_get_int_arg(args, "depth", MCP_DEFAULT_BFS_DEPTH);
5221+
depth = clamp_mcp_depth(depth, "detect_changes");
52045222

52055223
/* scope: "files" = just changed files, "symbols" = files + symbols (default) */
52065224
bool want_symbols = !scope || strcmp(scope, "symbols") == 0 || strcmp(scope, "impact") == 0;

‎tests/test_cypher.c‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "test_framework.h"
88
#include <cypher/cypher.h>
99
#include <store/store.h>
10+
#include <stdio.h>
1011
#include <string.h>
1112
#include <stdlib.h>
1213

@@ -834,6 +835,73 @@ TEST(cypher_exec_variable_length) {
834835
PASS();
835836
}
836837

838+
/* Reproduce-first (#887): an EXPLICIT variable-length upper bound must still be
839+
* capped at the engine ceiling (cbm_cypher_max_depth(), default 10). On
840+
* origin/main, expand_var_length honoured an explicit `*1..N` verbatim (only the
841+
* unbounded `*` / `*..m` forms were capped), so `[:CALLS*1..N]` passed N straight
842+
* to cbm_store_bfs — an unbounded traversal (a DoS on cyclic graphs). RED before
843+
* the clamp: a *1..12 walk over a 13-node chain
844+
* returns all 12 hops (N01..N12). GREEN after: it stops at the depth-10 ceiling
845+
* (N01..N10); N11/N12 are never emitted. max_rows=64 keeps the binding-expansion
846+
* cap (bind_cap*10) well above the hop count, so DEPTH — not the binding cap — is
847+
* the bound under test. */
848+
TEST(cypher_exec_var_length_explicit_bound_capped) {
849+
cbm_store_t *s = cbm_store_open_memory();
850+
cbm_store_upsert_project(s, "test", "/tmp/test");
851+
852+
/* Linear chain N00 -CALLS-> N01 -> ... -> N12 (13 nodes, one node per hop). */
853+
int64_t ids[13];
854+
for (int i = 0; i < 13; i++) {
855+
char name[8];
856+
char qn[24];
857+
snprintf(name, sizeof(name), "N%02d", i);
858+
snprintf(qn, sizeof(qn), "test.N%02d", i);
859+
cbm_node_t n = {.project = "test",
860+
.label = "Function",
861+
.name = name,
862+
.qualified_name = qn,
863+
.file_path = "chain.go"};
864+
ids[i] = cbm_store_upsert_node(s, &n);
865+
}
866+
for (int i = 0; i < 12; i++) {
867+
cbm_edge_t e = {
868+
.project = "test", .source_id = ids[i], .target_id = ids[i + 1], .type = "CALLS"};
869+
cbm_store_insert_edge(s, &e);
870+
}
871+
872+
cbm_cypher_result_t r = {0};
873+
int rc = cbm_cypher_execute(s,
874+
"MATCH (a:Function {name: \"N00\"})-[:CALLS*1..12]->"
875+
"(x:Function) RETURN x.name",
876+
"test", 64, &r);
877+
ASSERT_EQ(rc, 0);
878+
879+
/* Capped at 10 hops → exactly N01..N10; N11/N12 are beyond the ceiling. */
880+
ASSERT_EQ(r.row_count, 10);
881+
bool saw_n10 = false;
882+
bool saw_n11 = false;
883+
bool saw_n12 = false;
884+
for (int i = 0; i < r.row_count; i++) {
885+
const char *v = r.rows[i][0];
886+
if (v && strcmp(v, "N10") == 0) {
887+
saw_n10 = true;
888+
}
889+
if (v && strcmp(v, "N11") == 0) {
890+
saw_n11 = true;
891+
}
892+
if (v && strcmp(v, "N12") == 0) {
893+
saw_n12 = true;
894+
}
895+
}
896+
ASSERT_TRUE(saw_n10); /* within the ceiling — proves the traversal really ran */
897+
ASSERT_FALSE(saw_n11); /* clamped away */
898+
ASSERT_FALSE(saw_n12);
899+
900+
cbm_cypher_result_free(&r);
901+
cbm_store_close(s);
902+
PASS();
903+
}
904+
837905
TEST(cypher_exec_defines_edge) {
838906
cbm_store_t *s = setup_cypher_store();
839907
cbm_cypher_result_t r = {0};
@@ -2581,6 +2649,7 @@ SUITE(cypher) {
25812649
RUN_TEST(cypher_exec_limit);
25822650
RUN_TEST(cypher_exec_order_by);
25832651
RUN_TEST(cypher_exec_variable_length);
2652+
RUN_TEST(cypher_exec_var_length_explicit_bound_capped);
25842653
RUN_TEST(cypher_exec_defines_edge);
25852654
RUN_TEST(cypher_exec_no_results);
25862655
RUN_TEST(cypher_exec_where_numeric);

‎tests/test_mcp.c‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,62 @@ TEST(tool_trace_call_path_prefers_definition) {
10011001
PASS();
10021002
}
10031003

1004+
/* Reproduce-first (#887): the client-supplied `depth` on trace_call_path must be
1005+
* clamped to the MCP ceiling (cbm_mcp_max_depth(), default 15). On origin/main
1006+
* an MCP_MAX_DEPTH=15 constant was defined but never applied — `depth` flowed
1007+
* straight into bfs_union_same_name, so an unbounded value drives the shared
1008+
* cbm_store_bfs to arbitrary depth. Over an 18-node call chain, depth=1000
1009+
* reaches n16/n17 (RED); with the clamp the walk stops at hop 15, so n15 is
1010+
* reached but n16 is not (GREEN). Quoted tokens ("n15"/"n16") match only the
1011+
* node-name field, never the qualified_name (preceded by '.'), so the boundary
1012+
* check is exact. */
1013+
TEST(tool_trace_call_path_depth_clamped) {
1014+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
1015+
cbm_store_t *st = cbm_mcp_server_store(srv);
1016+
const char *proj = "depth-proj";
1017+
cbm_mcp_server_set_project(srv, proj);
1018+
cbm_store_upsert_project(st, proj, "/tmp/depth");
1019+
1020+
/* Linear call chain n00 -CALLS-> n01 -> ... -> n17 (18 nodes). */
1021+
int64_t ids[18];
1022+
for (int i = 0; i < 18; i++) {
1023+
char name[8];
1024+
char qn[32];
1025+
snprintf(name, sizeof(name), "n%02d", i);
1026+
snprintf(qn, sizeof(qn), "depth-proj.n%02d", i);
1027+
cbm_node_t n = {.project = proj,
1028+
.label = "Function",
1029+
.name = name,
1030+
.qualified_name = qn,
1031+
.file_path = "chain.c",
1032+
.start_line = 1,
1033+
.end_line = 2};
1034+
ids[i] = cbm_store_upsert_node(st, &n);
1035+
}
1036+
for (int i = 0; i < 17; i++) {
1037+
cbm_edge_t e = {
1038+
.project = proj, .source_id = ids[i], .target_id = ids[i + 1], .type = "CALLS"};
1039+
cbm_store_insert_edge(st, &e);
1040+
}
1041+
1042+
char *resp = cbm_mcp_server_handle(
1043+
srv, "{\"jsonrpc\":\"2.0\",\"id\":71,\"method\":\"tools/call\","
1044+
"\"params\":{\"name\":\"trace_call_path\",\"arguments\":{\"function_name\":\"n00\","
1045+
"\"project\":\"depth-proj\",\"direction\":\"outbound\",\"depth\":1000}}}");
1046+
ASSERT_NOT_NULL(resp);
1047+
char *inner = extract_text_content(resp);
1048+
ASSERT_NOT_NULL(inner);
1049+
1050+
/* Reached within the ceiling (proves the traversal ran) but clamped at 15. */
1051+
ASSERT_NOT_NULL(strstr(inner, "\"n15\""));
1052+
ASSERT_NULL(strstr(inner, "\"n16\""));
1053+
1054+
free(inner);
1055+
free(resp);
1056+
cbm_mcp_server_free(srv);
1057+
PASS();
1058+
}
1059+
10041060
/* Reproduce-first (#650, distilled): two GENUINELY-DIFFERENT same-named functions
10051061
* whose bodies differ in length score differently, so the old exact-tie check did
10061062
* not flag them ambiguous — and bfs_union_same_name (#546) then merged the caller
@@ -4711,6 +4767,7 @@ SUITE(mcp) {
47114767
RUN_TEST(tool_trace_missing_function_name);
47124768
RUN_TEST(tool_trace_call_path_ambiguous);
47134769
RUN_TEST(tool_trace_call_path_prefers_definition);
4770+
RUN_TEST(tool_trace_call_path_depth_clamped);
47144771
RUN_TEST(tool_trace_call_path_distinct_defs_not_over_unioned);
47154772
RUN_TEST(tool_trace_call_path_dts_stub_unions_with_impl);
47164773
RUN_TEST(tool_delete_project_not_found);

0 commit comments

Comments
 (0)