From e7e47b7f1d897f8f9965021ae4042025404c3551 Mon Sep 17 00:00:00 2001 From: lotherk Date: Mon, 23 Nov 2026 00:56:58 +0000 Subject: [PATCH] json: a depth limit, so a nested body cannot kill the process The parser recurses once per container and had no limit. The OpenAI gateway and the A2A server hand it a caller's body of up to 4 MiB, so a request of 200k nested arrays was a remote crash: verified against a running gateway, the process died and the client got an empty reply. 200k arrays are a 400 KB body, well inside what both servers accept. J_MAX_DEPTH = 64 is far past any real document (a tool schema nests four or five deep) and far short of a stack frame budget. A deeper document is now a parse error, which both servers already answer with 400. Proof in tests/test_fuzz.c (test_json_depth): 200k nested arrays crash without the limit and parse as NULL with it; an ordinary document still parses. make check: all tests passed. make check-asan: all 45 test binaries clean. --- tests/test_fuzz.c | 51 +++++++++++++++++++++++++++++++++++++++++++++ vendor/libmcp/mcp.h | 44 +++++++++++++++++++++++++++++++------- 2 files changed, 87 insertions(+), 8 deletions(-) diff --git a/tests/test_fuzz.c b/tests/test_fuzz.c index 6d928c9..ddf10f5 100644 --- a/tests/test_fuzz.c +++ b/tests/test_fuzz.c @@ -106,6 +106,56 @@ check_cursor(const char *html, size_t limit) sp_buf_free(&piece); } +/* + * A parser that recurses without a limit is a crash reachable from the + * network: the OpenAI gateway and the A2A server hand it a request body of up + * to 4 MiB, and 200k nested arrays overflowed the stack and killed the + * process. A real document nests four or five deep, so a limit far above that + * costs nothing and takes the crash away. + */ +static void +test_json_depth(void) +{ + size_t n = 200000, i; + char *deep = malloc(2 * n + 1); + mcp_json *v = NULL; + + CHECK(deep != NULL); + if (deep == NULL) + return; + for (i = 0; i < n; i++) + deep[i] = '['; + for (i = 0; i < n; i++) + deep[n + i] = ']'; + deep[2 * n] = '\0'; + CHECK(mcp_parse(deep) == NULL); + CHECK(mcp_parse_exact(deep) == NULL); + free(deep); + + /* a document of an ordinary shape still parses */ + v = mcp_parse("{\"a\":[1,{\"b\":[2,3]}],\"c\":\"d\"}"); + CHECK(v != NULL); + CHECK(v != NULL && mcp_arg_num(mcp_arg(v, "c"), NULL, -1) == -1); + if (v != NULL) + mcp_json_free(v); + + /* and one just inside the limit */ + { + char ok[160]; + size_t k = 60; + + for (i = 0; i < k; i++) + ok[i] = '['; + for (i = 0; i < k; i++) + ok[k + i] = ']'; + ok[2 * k] = '\0'; + v = mcp_parse(ok); + CHECK(v != NULL); + if (v != NULL) + mcp_json_free(v); + } +} + int main(void) { @@ -115,6 +165,7 @@ main(void) CHECK(sp_buf_init(&out) == 0); CHECK(sp_buf_init(&err) == 0); + test_json_depth(); for (i = 0; i < 4000; i++) { struct sp_sse *sse; diff --git a/vendor/libmcp/mcp.h b/vendor/libmcp/mcp.h index a90c238..aab1cc8 100644 --- a/vendor/libmcp/mcp.h +++ b/vendor/libmcp/mcp.h @@ -302,7 +302,22 @@ static const char *skip_ws(const char *p) return p; } -static const char *jparse(const char *p, mcp_json **out); +/* + * How deep the parser will go. The parser is recursive, so a body of nested + * arrays is a body of stack frames: 100k of them overflowed the stack and + * killed the process. An OpenAI gateway and an A2A server parse what a caller + * sends, so this is reachable from the network. 64 is far past any real + * document (a tool schema nests four or five deep) and far short of a crash. + */ +#define J_MAX_DEPTH 64 + +static const char *jparse_d(const char *p, mcp_json **out, int depth); + +static const char * +jparse(const char *p, mcp_json **out) +{ + return jparse_d(p, out, 0); +} static char *parse_string(const char **pp) { @@ -365,10 +380,16 @@ static char *parse_string(const char **pp) return b.p; } -static const char *jparse_array(const char *p, mcp_json **out) +static const char * +jparse_array(const char *p, mcp_json **out, int depth) { mcp_json *v = jnew(MCP_ARR); + if (depth >= J_MAX_DEPTH) { + jfree(v); + return NULL; + } + p = skip_ws(p + 1); if (*p == ']') { *out = v; @@ -377,7 +398,7 @@ static const char *jparse_array(const char *p, mcp_json **out) for (;;) { mcp_json *item = NULL; - p = jparse(p, &item); + p = jparse_d(p, &item, depth + 1); if (p == NULL) { jfree(v); return NULL; @@ -402,10 +423,16 @@ static const char *jparse_array(const char *p, mcp_json **out) } } -static const char *jparse_object(const char *p, mcp_json **out) +static const char * +jparse_object(const char *p, mcp_json **out, int depth) { mcp_json *v = jnew(MCP_OBJ); + if (depth >= J_MAX_DEPTH) { + jfree(v); + return NULL; + } + p = skip_ws(p + 1); if (*p == '}') { *out = v; @@ -427,7 +454,7 @@ static const char *jparse_object(const char *p, mcp_json **out) jfree(v); return NULL; } - p = jparse(skip_ws(p + 1), &val); + p = jparse_d(skip_ws(p + 1), &val, depth + 1); if (p == NULL) { free(key); jfree(v); @@ -458,14 +485,15 @@ static const char *jparse_object(const char *p, mcp_json **out) } } -static const char *jparse(const char *p, mcp_json **out) +static const char * +jparse_d(const char *p, mcp_json **out, int depth) { p = skip_ws(p); switch (*p) { case '{': - return jparse_object(p, out); + return jparse_object(p, out, depth); case '[': - return jparse_array(p, out); + return jparse_array(p, out, depth); case '"': { mcp_json *v = jnew(MCP_STR);