Archived
text: the tool-call arguments probe parses under bounds
bad_arguments names the offending tool_calls[].function.arguments when a refused conversation reaches it -- and it probed each string with the plain Json::parse. The HTTP edge depth-caps these strings (its comment is explicit that the strings could otherwise overflow a worker's stack), but nothing capped the VALUE COUNT: a client whose conversation had already failed the template -- exactly the path this refusal exists on -- could hand the probe a few million flat values, and the probe grew a DOM no one would ever read, once per request, inside the body's 512 MiB. Offline callers (fleet, diagnose) reach the same path with no edge at all. The probe is parse_bounded now, at the same depth the edge enforces and a values budget a tool call cannot approach; over, too deep, and not JSON each name themselves in the refusal, and the honest case still refuses in the template's own words. chat_test pins all three bounds. Found by the 2.1.0 review pass (axis: hostile sockets).
This commit is contained in:
1 parent
392746da6b
commit
f4981154fc
2 files changed
+56
-4
No files matched your search
+17
-4
@@ -355,6 +355,14 @@ static bool alias_reasoning(const ojson& messages, ojson* out) {
|
||||
* "messages[i].tool_calls[j].function.arguments is not JSON: <why>", or empty when every call's
|
||||
* arguments parse. The template refuses such a conversation with the parser's words alone, which reads as
|
||||
* the template's fault; this names the call. */
|
||||
/* What one `arguments` string may be: the same value floor a request body gets and the depth the
|
||||
* server's edge already caps these strings at (oai.h's kJsonMaxDepth; core/text cannot include
|
||||
* the server header, so the number is mirrored -- a request that passed the edge must pass here
|
||||
* too). The edge caps depth only, so without these bounds a client could hand the parse below a
|
||||
* few hundred MB of flat JSON and watch it grow a DOM the request itself never needed. */
|
||||
constexpr int64_t kArgumentsValueBudget = 1LL << 20;
|
||||
constexpr int kArgumentsMaxDepth = 1024;
|
||||
|
||||
static std::string bad_arguments(const ojson& messages) {
|
||||
if (!messages.is_array()) return "";
|
||||
for (size_t i = 0; i < messages.size(); ++i) {
|
||||
@@ -367,11 +375,16 @@ static std::string bad_arguments(const ojson& messages) {
|
||||
if (!tc.is_object() || fn == tc.end() || !fn->is_object()) continue;
|
||||
const auto args = fn->find("arguments");
|
||||
if (args == fn->end() || !args->is_string()) continue;
|
||||
try {
|
||||
(void)ojson::parse(args->get_ref<const std::string&>());
|
||||
} catch (const std::exception& e) {
|
||||
bool over = false, deep = false;
|
||||
const ojson probe = ojson::parse_bounded(args->get_ref<const std::string&>(),
|
||||
kArgumentsValueBudget, kArgumentsMaxDepth,
|
||||
&over, &deep);
|
||||
if (probe.is_discarded() || over || deep) {
|
||||
const char* why = over ? " carries far more values than a tool call takes"
|
||||
: deep ? " nests far deeper than a tool call nests"
|
||||
: " is not JSON";
|
||||
return "messages[" + std::to_string(i) + "].tool_calls[" + std::to_string(j) +
|
||||
"].function.arguments is not JSON: " + e.what();
|
||||
"].function.arguments" + why;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -229,6 +229,45 @@ TEST(a_concatenated_string_equals_its_text) {
|
||||
|
||||
/* Tool-call arguments sent as an object reach a template that only takes them as text as their
|
||||
* compact JSON, as when the request sent that text. */
|
||||
TEST(a_refused_request_cannot_grow_an_unbounded_dom_in_its_tool_call_arguments) {
|
||||
/* bad_arguments runs on the !served path -- template refused, merge refused -- and probes the
|
||||
* arguments strings as JSON to name the offender. Probing with the unbounded parser let a
|
||||
* client that had already failed the template hand the probe a few million flat values to
|
||||
* grow into a DOM nothing would read. The probe is bounded now; these bounds reject the
|
||||
* oversized and over-deep strings as the refusal they name, and a real call is unaffected. */
|
||||
ChatTemplate t;
|
||||
CHECK_OK(t.load(
|
||||
"{%- for m in messages %}"
|
||||
"{%- if m.role == 'system' and not loop.first %}"
|
||||
"{{- raise_exception('System message must be at the beginning.') }}{%- endif %}"
|
||||
"{%- if m.role == 'user' and m.content == 'boom' %}{{- raise_exception('no boom') }}{%- endif %}"
|
||||
"<{{ m.role }}>{{ m.content }}</{{ m.role }}>"
|
||||
"{%- endfor %}", "", ""));
|
||||
ChatOptions opt;
|
||||
opt.quiet = true;
|
||||
auto with_args = [](const std::string& args) {
|
||||
return json::parse(R"([{"role":"system","content":"s1"},{"role":"system","content":"s2"},)"
|
||||
R"({"role":"user","content":"boom"},)"
|
||||
R"({"role":"assistant","content":"","tool_calls":[{"id":"c","type":"function",)"
|
||||
R"("function":{"name":"f","arguments":)" + json(args).dump() + R"(}}]}])");
|
||||
};
|
||||
ChatPrompt huge_p;
|
||||
std::string many = "[";
|
||||
for (int v = 0; v < 1100000; ++v) many += v ? ",1" : "1";
|
||||
many += "]";
|
||||
const int huge_rc = t.apply(with_args(many), json(), opt, &huge_p);
|
||||
CHECK(huge_rc < 0);
|
||||
CHECK(huge_p.error.find("carries far more values") != std::string::npos);
|
||||
ChatPrompt deep_p;
|
||||
CHECK(t.apply(with_args(std::string(1200, '[') + std::string(1200, ']')), json(), opt, &deep_p) < 0);
|
||||
CHECK(deep_p.error.find("nests far deeper") != std::string::npos);
|
||||
/* An honest refusal of an honest call still comes back in the template's own words. */
|
||||
ChatPrompt ok_p;
|
||||
CHECK(t.apply(with_args("{\"z\":1}"), json(), opt, &ok_p) < 0);
|
||||
CHECK(ok_p.error.find("chat template refused") != std::string::npos);
|
||||
CHECK(ok_p.error.find("function.arguments") == std::string::npos);
|
||||
}
|
||||
|
||||
TEST(object_arguments_reach_a_string_only_template_as_compact_json) {
|
||||
ChatTemplate t;
|
||||
REQUIRE(t.load(
|
||||
|
||||
Reference in new issue
Block a user