Archived
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.
This commit is contained in:
1 parent
dd56e1158d
commit
12735a62dc
4 files changed
+106
-2
No files matched your search
@@ -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"}},)"
|
||||
|
||||
Vendored
+12
@@ -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.
|
||||
+24
-2
@@ -4,6 +4,7 @@
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <map>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
@@ -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<unsigned char>(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];
|
||||
|
||||
@@ -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 <nlohmann/json.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
+#include <cctype>
|
||||
#include <map>
|
||||
#include <regex>
|
||||
#include <sstream>
|
||||
@@ -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<unsigned char>(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];
|
||||
Reference in new issue
Block a user