From 12735a62dcd61571571de30c71f299dac4694af2 Mon Sep 17 00:00:00 2001 From: davetha Date: Mon, 19 Oct 2026 04:08:28 +0000 Subject: [PATCH] json-schema: identity escapes of punctuation are read as the character Patterns written in JavaScript escape punctuation that needs no escape: "\/" in a path, "\-" in a class. zod's email pattern carries "[A-Za-z0-9_'+\-]", so any tool validating an email with zod sent a schema that was refused outside a class ("Unsupported regex escape") or, inside one, produced a grammar that failed to load. Outside a class such an escape is now the bare character; regex syntax characters and '"' keep their backslash. Inside a class it is written \xHH, so an escaped '-' or '^' cannot read as a range or a negation. Found by running 203 patterns harvested from installed agent packages through rad-gbnf-check. vendor/patches/0019, vendor/UPSTREAM; gbnf_test cases. --- tests/gbnf_test.cpp | 12 ++++ vendor/UPSTREAM | 12 ++++ .../llama/common/json-schema-to-grammar.cpp | 26 ++++++++- ...hema-identity-escapes-of-punctuation.patch | 58 +++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 vendor/patches/0019-json-schema-identity-escapes-of-punctuation.patch diff --git a/tests/gbnf_test.cpp b/tests/gbnf_test.cpp index 60aa78d1..9cc84c52 100644 --- a/tests/gbnf_test.cpp +++ b/tests/gbnf_test.cpp @@ -1718,6 +1718,18 @@ TEST(json_schema_grammars_admit_exactly_their_instances) { { R"({"type":"string","pattern":"^a\\sb$"})", { R"("a b")", R"("a\tb")" }, { R"("ab")" } }, { R"({"type":"string","pattern":"^[\\s\\S]{2}$"})", { R"("ab")", R"("a ")" }, { R"("a")" } }, { R"({"type":"string","pattern":"^\\D$"})", { R"("x")" }, { R"("7")" } }, + /* identity escapes of punctuation (0019): zod's email pattern sends "\-" inside a class, and + * JavaScript-written patterns escape "/". An escaped '-' in a class is not a range. */ + { R"({"type":"string","pattern":"^a\\/b$"})", { R"("a/b")" }, { R"("ab")" } }, + { R"({"type":"string","pattern":"^a\\-b$"})", { R"("a-b")" }, { R"("ab")" } }, + { R"({"type":"string","pattern":"^[a\\-z]$"})", { R"("a")", R"("-")", R"("z")" }, { R"("b")" } }, + { R"({"type":"string","pattern":"^[a\\^]$"})", { R"("^")", R"("a")" }, { R"("b")" } }, + { R"({"type":"string","pattern":"^[\\^a]$"})", { R"("^")", R"("a")" }, { R"("b")" } }, + { R"({"type":"string","pattern":"^[\\--z]$"})", { R"("-")", R"("a")" }, { R"("+")" } }, + { R"({"type":"string","pattern":"^[\\.]$"})", { R"(".")" }, { R"("a")" } }, + { R"({"type":"string","pattern":"a\\-b"})", { R"("xa-by")" }, { R"("ab")" } }, + { R"({"type":"string","pattern":"^(?:[A-Za-z0-9_'+\\-]+\\.)*[A-Za-z0-9_'+\\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\\-]*\\.)+[A-Za-z]{2,}$"})", + { R"("a.b@c.io")", R"("x-y@d-e.org")" }, { R"("a@b")", R"("a b@c.io")" } }, { R"({"type":"array","items":{"type":"integer"},"minItems":1,"maxItems":3})", { "[1]", "[1,2,3]", "[-4,0]" }, { "[]", "[1,2,3,4]", R"(["a"])" } }, { R"({"type":"object","properties":{"a":{"type":"integer"},"b":{"type":"string"}},)" diff --git a/vendor/UPSTREAM b/vendor/UPSTREAM index 0e21d000..20076c4c 100644 --- a/vendor/UPSTREAM +++ b/vendor/UPSTREAM @@ -103,6 +103,7 @@ vendor/llama/common/regex-partial.{h,cpp} <- llama.cpp common/* patches/0016-chat-analyse-a-template-once.patch patches/0017-json-schema-a-pattern-without-anchors-searches.patch patches/0018-json-schema-shorthand-classes-are-written-out.patch + patches/0019-json-schema-identity-escapes-of-punctuation.patch On 0003: a `pattern` in a `response_format` reaches _visit_pattern() as client data, and three of its paths do not survive that. `front()`/`back()` run on an empty string; `seq.back()` runs @@ -524,3 +525,14 @@ PATCH SERIES \u0031 that decodes to a digit. sample_test's "cannot spell" case moves from \d to \b, which is still refused; gbnf_test covers \S on strings and array items, \d dates and decimals, [\w-], \s with an escaped tab, [\s\S] and \D, and the refused [a\S]. + + patches/0019-json-schema-identity-escapes-of-punctuation.patch + + Patterns written in JavaScript escape punctuation that needs no escape: "\/" in a path, + "\-" in a class (zod's email pattern carries both "[A-Za-z0-9_'+\-]" and "[A-Za-z0-9\-]"). + Outside a class these were refused ("Unsupported regex escape"); inside one they were copied + raw and the grammar failed to load. Outside a class such an escape is now the bare + character -- regex syntax characters and '"' keep their backslash and go the old way -- + and inside a class it is written \xHH, so an escaped '-' or '^' cannot read as a range or + a negation. gbnf_test covers "\/" and "\-" in literals, "\-" "\^" "\." in classes and as + a range endpoint, an unanchored "a\-b", and zod's email pattern. diff --git a/vendor/llama/common/json-schema-to-grammar.cpp b/vendor/llama/common/json-schema-to-grammar.cpp index 46648333..92561c49 100644 --- a/vendor/llama/common/json-schema-to-grammar.cpp +++ b/vendor/llama/common/json-schema-to-grammar.cpp @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -336,7 +337,12 @@ static std::string anchor_pattern(const std::string & p) { // carry escaped, and admit any JSON escape instead, so a match never runs past the closing quote; // \s adds the escapes \t \n \f \r for the same reason. In a class a positive shorthand becomes its // ranges and [\s\S] and its kin become any text; a negated one mixed into a class has no spelling, -// so false is returned and the pattern refused. +// so false is returned and the pattern refused. Identity escapes of punctuation, which JavaScript +// writes freely ("\/" in a path, "\-" in a class), become characters GBNF can read. +static bool is_identity_punct(char c) { + return c > ' ' && c < 0x7F && c != '\\' && !std::isalnum(static_cast(c)); +} + static bool expand_shorthands(const std::string & p, std::string & out) { static const std::string sp = " \\u00A0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF"; static const std::string esc = "\\\\(?:[\"\\\\/bfnrt]|u[0-9A-Fa-f]{4})"; @@ -354,7 +360,16 @@ static bool expand_shorthands(const std::string & p, std::string & out) { case 'D': out += negated("0-9"); break; case 'W': out += negated("A-Za-z0-9_"); break; case 'S': out += negated(sp); break; - default: out += c; out += n; break; + default: + // An identity escape of punctuation GBNF has no escape for ("\/", "\-") is the + // bare character; regex syntax characters and '"' keep their backslash. + if (is_identity_punct(n) && std::string("^$.[](){}|*+?\"").find(n) == std::string::npos) { + out += n; + } else { + out += c; + out += n; + } + break; } continue; } @@ -387,6 +402,13 @@ static bool expand_shorthands(const std::string & p, std::string & out) { else if (n == 'w') cls += "A-Za-z0-9_"; else if (n == 's') cls += sp; else if (n == 'D' || n == 'W' || n == 'S') return false; + else if (is_identity_punct(n) && n != '"' && n != '[' && n != ']') { + // "\-" "\." "\^" and the like: GBNF has no such escape, and bare they could + // read as a range or a negation, so they are written as \xHH. + cls += "\\x"; + cls += "0123456789ABCDEF"[(n >> 4) & 0xF]; + cls += "0123456789ABCDEF"[n & 0xF]; + } else { cls += '\\'; cls += n; } } else { cls += body[k]; diff --git a/vendor/patches/0019-json-schema-identity-escapes-of-punctuation.patch b/vendor/patches/0019-json-schema-identity-escapes-of-punctuation.patch new file mode 100644 index 00000000..24a75154 --- /dev/null +++ b/vendor/patches/0019-json-schema-identity-escapes-of-punctuation.patch @@ -0,0 +1,58 @@ +diff --git a/common/json-schema-to-grammar.cpp b/common/json-schema-to-grammar.cpp +index 4664833..92561c4 100644 +--- a/common/json-schema-to-grammar.cpp ++++ b/common/json-schema-to-grammar.cpp +@@ -4,6 +4,7 @@ + #include + + #include ++#include + #include + #include + #include +@@ -336,7 +337,12 @@ static std::string anchor_pattern(const std::string & p) { + // carry escaped, and admit any JSON escape instead, so a match never runs past the closing quote; + // \s adds the escapes \t \n \f \r for the same reason. In a class a positive shorthand becomes its + // ranges and [\s\S] and its kin become any text; a negated one mixed into a class has no spelling, +-// so false is returned and the pattern refused. ++// so false is returned and the pattern refused. Identity escapes of punctuation, which JavaScript ++// writes freely ("\/" in a path, "\-" in a class), become characters GBNF can read. ++static bool is_identity_punct(char c) { ++ return c > ' ' && c < 0x7F && c != '\\' && !std::isalnum(static_cast(c)); ++} ++ + static bool expand_shorthands(const std::string & p, std::string & out) { + static const std::string sp = " \\u00A0\\u1680\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000\\uFEFF"; + static const std::string esc = "\\\\(?:[\"\\\\/bfnrt]|u[0-9A-Fa-f]{4})"; +@@ -354,7 +360,16 @@ static bool expand_shorthands(const std::string & p, std::string & out) { + case 'D': out += negated("0-9"); break; + case 'W': out += negated("A-Za-z0-9_"); break; + case 'S': out += negated(sp); break; +- default: out += c; out += n; break; ++ default: ++ // An identity escape of punctuation GBNF has no escape for ("\/", "\-") is the ++ // bare character; regex syntax characters and '"' keep their backslash. ++ if (is_identity_punct(n) && std::string("^$.[](){}|*+?\"").find(n) == std::string::npos) { ++ out += n; ++ } else { ++ out += c; ++ out += n; ++ } ++ break; + } + continue; + } +@@ -387,6 +402,13 @@ static bool expand_shorthands(const std::string & p, std::string & out) { + else if (n == 'w') cls += "A-Za-z0-9_"; + else if (n == 's') cls += sp; + else if (n == 'D' || n == 'W' || n == 'S') return false; ++ else if (is_identity_punct(n) && n != '"' && n != '[' && n != ']') { ++ // "\-" "\." "\^" and the like: GBNF has no such escape, and bare they could ++ // read as a range or a negation, so they are written as \xHH. ++ cls += "\\x"; ++ cls += "0123456789ABCDEF"[(n >> 4) & 0xF]; ++ cls += "0123456789ABCDEF"[n & 0xF]; ++ } + else { cls += '\\'; cls += n; } + } else { + cls += body[k];