Files
codebase-memory-mcp/src/foundation/limits.h
Martin Vogel 98e98c3208 fix: cap client-driven traversal depth (var-length Cypher + MCP tools)
Explicit variable-length Cypher bounds ([:CALLS*1..N]) and the client
`depth` on trace_call_path / detect_changes were passed straight into
cbm_store_bfs, so an arbitrarily large value drove an unbounded traversal
(a DoS on cyclic graphs). The MCP_MAX_DEPTH ceiling was defined but never
applied, and CYP_MAX_DEPTH capped only the unbounded var-length forms.

Add env-configurable ceilings cbm_cypher_max_depth() (CBM_CYPHER_MAX_DEPTH,
default 10) and cbm_mcp_max_depth() (CBM_MCP_MAX_DEPTH, default 15), clamp
both entry points to them, and WARN on clamp -- never a silent truncation.
Remove the now-dead CYP_MAX_DEPTH / MCP_MAX_DEPTH constants.

Reproduce-first: cypher_exec_var_length_explicit_bound_capped (12 -> 10 rows)
and tool_trace_call_path_depth_clamped (depth 1000 -> hop 15).

Closes #887.

Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
2026-07-05 16:13:00 +02:00

47 lines
2.3 KiB
C

/*
* limits.h — Generous, env-configurable safety limits (Stage 2 / Track B4).
*
* Each knob has a generous default. Hitting a limit degrades to a *reported*
* skip (surfaced via MCP/CLI/logfile), never a silent drop and never an
* unbounded read (unbounded just trades a crash for an OOM/hang). Every limit
* is env-overridable so an operator can tune it per-repo without a rebuild.
*/
#ifndef CBM_LIMITS_H
#define CBM_LIMITS_H
/* Result of an attempted per-file read, so callers can attribute a skip to the
* right phase/reason instead of collapsing every failure into "read failed". */
typedef enum {
CBM_READ_OK = 0, /* file read successfully */
CBM_READ_OPEN_FAIL, /* could not open (missing / permission) */
CBM_READ_EMPTY, /* zero/negative size — benign, nothing to index */
CBM_READ_OVERSIZED, /* size exceeds cbm_max_file_bytes() */
CBM_READ_OOM, /* buffer allocation failed */
} cbm_read_status_t;
/* Maximum size (bytes) of a single source file the indexer will read into
* memory. Files larger than this are skipped-and-reported (phase "oversized"),
* never silently dropped. Override with CBM_MAX_FILE_BYTES (a positive integer
* count of bytes). Default 512 MiB (raised from the historical 100 MB cap).
*
* The env var is read on each call — this is intentional: read_file() calls it
* once per file (negligible), and reading fresh means a test / operator can
* change the cap via setenv without a process restart or a stale memoized copy
* leaking across runs. */
long cbm_max_file_bytes(void);
/* Maximum variable-length path depth for the Cypher engine (the `*min..max`
* hop ceiling). BOTH the explicit (`*1..N`) and unbounded (`*`, `*..m`) forms
* are clamped to this, so `[:CALLS*1..1000000]` degrades to a WARN-and-cap
* rather than an unbounded (cyclic-graph DoS) traversal. Override with
* CBM_CYPHER_MAX_DEPTH (a positive integer). Default 10. */
int cbm_cypher_max_depth(void);
/* Maximum traversal depth for client-driven MCP graph tools (trace_call_path,
* detect_changes): the client `depth` argument is WARN-clamped to this so an
* arbitrarily large value cannot drive an unbounded BFS over the shared store.
* Override with CBM_MCP_MAX_DEPTH (a positive integer). Default 15. */
int cbm_mcp_max_depth(void);
#endif /* CBM_LIMITS_H */