vendor: the pattern parser counts its nesting, and refuses past 64

A tool schema's `pattern` reaches the grammar converter as client data,
and the group scanner recursed once per nested '(': a few tens of KB of
brackets inside one JSON string was an uncatchable stack overflow of the
request worker. The body's depth and value bounds never saw it -- the
brackets are string content, not JSON nesting -- and 0003, the patch that
made this same function survive a hostile pattern, hardened the empty and
the looping cases but never the recursing one. It is the one remote crash
in the audit, and one request is the whole attack.

transform() now counts its nesting and records the refusal once, where
every unwinding frame met the same '(' and repeated it; check_errors turns
it into the clean throw the server already answers with a 400. Proven on
the compile host against the real converter: the 20000-group pattern is
refused by name, a 40-group pattern -- far past anything a real schema
nests -- still compiles. The record keeps the addition under 0003's name,
whose footing it stands on.
This commit is contained in:
fl0l0u committed 2026-11-15 20:38:09 +00:00
1 parent b83d53cf19
commit 2fda03ccdd
4 files changed
+46 -2

No files matched your search

+16
View File
@@ -1515,6 +1515,22 @@ TEST(gbnf_a_grammar_whose_masks_need_unbounded_context_is_refused) {
std::fprintf(stderr, " refused: %s\n", err.c_str());
}
/* `pattern` is client data, and the group scanner recurses once per nested '('. Unbounded, a
* few tens of KB of '(' -- one JSON string, so the body's depth and value bounds never see it --
* stack-overflows the request worker past any catch. Nesting past the cap is refused the way
* every other unsupported syntax is, and a pattern no real schema exceeds stays legal. */
TEST(gbnf_a_pattern_nesting_past_the_cap_is_refused_not_fatal) {
const std::string deep = "^" + std::string(20000, '(') + "a" + std::string(20000, ')') + "$";
const std::string schema = std::string(R"({"type":"string","pattern":")") + deep + R"("})";
std::string gbnf, err;
CHECK(grammar_from_json_schema(schema, "root", &gbnf, &err) != 0);
CHECK(err.find("deep") != std::string::npos);
const std::string sane = "^" + std::string(40, '(') + "a" + std::string(40, ')') + "$";
const std::string schema_ok = std::string(R"({"type":"string","pattern":")") + sane + R"("})";
std::string gbnf2, err2;
CHECK_OK(grammar_from_json_schema(schema_ok, "root", &gbnf2, &err2));
}
/* A rule that can never match is refused by name. `r ::= r` passes the left-recursion check (the
* reference is in tail position) but only leads back into itself, and upstream reads the resulting
* empty stack set as admitting every token. A grammar that merely never ends still compiles. */