server: bound the last unbounded parses of untrusted text

Three ways in, each ending in a parse that bounds neither what it
builds nor how deep it nests:

videos.cpp read its multipart form fields with the raw parser, so a
512 MiB field of brackets died at the tree's teardown; the same file's
JSON path was bounded, so the form path now shares its gate, and a
field over budget or over depth reads as a bad parse (a form test pins
both refusals and a well-formed field).

Anthropic's buffered body parsed the model's tool-call arguments
directly; the streaming path already scans them with json_complete,
whose depth cap keeps a cooperating model from stack-killing the
worker, and the buffered body now passes the same gate, reading too
deep as an empty input.

The image decoders checked the container header's dimensions against
the pixel cap but converted whatever the bitstream actually decoded;
a container whose SPS disagrees with its header slipped through. Both
now re-check the decoded frame, as the video path already does. The
re-check is inside RAD_HAVE_FFMPEG and compiles with every FFmpeg
build; the media-less container proves the two server changes.
This commit is contained in:
fl0l0u authored and StillDeadcode committed 2026-10-31 07:50:43 +01:00
1 parent 546f0c2047
commit d54f3b9e6e
5 files changed
+77 -4

No files matched your search

+12
View File
@@ -327,6 +327,12 @@ int decode_image(const uint8_t* data, size_t n, int64_t max_pixels, Frame* out,
SwsContext* sws = nullptr;
bool got = false;
const int s = decode_frames(in, [&](int64_t, const AVFrame* f) -> int {
/* The decoded frame, not the header's size, is what gets converted: a container whose
* bitstream disagrees with its declared dimensions must not slip past the cap. */
if ((int64_t)f->width * f->height > max_pixels) {
*why = "the decoded image is larger than the server's pixel limit";
return RAD_E_INVAL;
}
const int c = to_rgb(f, &sws, out, why);
if (c < 0) return c;
got = true;
@@ -355,6 +361,12 @@ int decode_image_rgba(const uint8_t* data, size_t n, int64_t max_pixels, FrameRG
SwsContext* sws = nullptr;
bool got = false;
const int s = decode_frames(in, [&](int64_t, const AVFrame* f) -> int {
/* Re-check the decoded frame against the cap, as decode_image and the video path do:
* the bitstream, not the header, decides what the converter allocates. */
if ((int64_t)f->width * f->height > max_pixels) {
*why = "the decoded image is larger than the server's pixel limit";
return RAD_E_INVAL;
}
const int c = to_rgba(f, &sws, out, why);
if (c < 0) return c;
got = true;
+9 -2
View File
@@ -3989,8 +3989,15 @@ std::string anthropic_message_body(const OaiRequest& r, const ChoiceOut& c, cons
if (!r.continued.empty() && text.rfind(r.continued, 0) == 0) text.erase(0, r.continued.size());
if (!text.empty()) blocks.push_back(json{ { "type", "text" }, { "text", text } });
for (const ToolCall& tc : c.msg.tool_calls) {
json in = json::parse(tc.arguments, nullptr, false);
if (in.is_discarded() || !in.is_object()) in = json::object();
/* The same bound the streaming path applies: `arguments` is model output, and
* json_complete's depth-capped scan must run before nlohmann's recursion and its
* teardown, neither of which bounds depth on its own. Too deep or incomplete reads as
* an empty input, as a discarded parse did. */
json in = json::object();
if (json_complete(tc.arguments)) {
json v = json::parse(tc.arguments, nullptr, false);
if (!v.is_discarded() && v.is_object()) in = std::move(v);
}
blocks.push_back(json{ { "type", "tool_use" }, { "id", tc.id }, { "name", tc.name },
{ "input", in } });
}
+6 -2
View File
@@ -320,8 +320,12 @@ HttpResponse Server::handle_video_create(const HttpRequest& req) {
if (p.name == "target" || p.name == "conditions" ||
((p.name == "loras" || p.name == "lora") && !p.data.empty() &&
(p.data[0] == '[' || p.data[0] == '{'))) {
v = json::parse(p.data, nullptr, false);
if (v.is_discarded())
/* Bounded like every other untrusted parse: a form field under the body cap can
* be brackets alone, and the deep tree's teardown would take the process with
* it. Over-budget or over-deep reads as a bad parse. */
bool over = false, deep = false;
v = parse_json_bounded(p.data, 0, &over, &deep);
if (v.is_discarded() || over || deep)
return error_response(err_bad(p.name, "expected JSON"), req.peer);
}
body[p.name] = v;
+22
View File
@@ -3912,6 +3912,28 @@ TEST(messages_return_a_tool_call_as_a_tool_use_block) {
CHECK(f.tmpl.last_n_tools == 1);
}
/* `arguments` is model output, and the buffered body parses it behind the depth-capped scan
* the streaming path applies: neither the recursive parse nor the tree's teardown bounds depth
* on its own, and a model made to cooperate can name a tool and nest fifty thousand brackets.
* Too deep reads as an empty input, as a discarded parse did; a shallow one rides whole. */
TEST(messages_empty_a_tool_use_input_too_deep_to_parse) {
OaiRequest r;
r.id = "msg_test";
r.model = "test-model";
ChoiceOut c;
UsageOut u;
c.msg.tool_calls.push_back({ "call_1", "get_weather",
"{\"city\":" + std::string(50000, '[') +
std::string(50000, ']') + "}" });
const json j = json::parse(anthropic_message_body(r, c, u));
CHECK_EQ(j["content"][0]["type"].get<std::string>(), std::string("tool_use"));
CHECK_EQ(j["content"][0]["name"].get<std::string>(), std::string("get_weather"));
CHECK_EQ(j["content"][0]["input"].dump(), std::string("{}"));
c.msg.tool_calls[0].arguments = "{\"city\":\"Paris\"}";
const json k = json::parse(anthropic_message_body(r, c, u));
CHECK_EQ(k["content"][0]["input"].dump(), std::string(R"({"city":"Paris"})"));
}
TEST(messages_turns_a_tool_result_back_into_a_tool_message) {
Fixture f;
f.sched.reply = "sunny";
+28
View File
@@ -368,6 +368,34 @@ TEST(a_form_creates_a_video_too) {
CHECK_EQ(v.f->srv->router().dispatch(q).status, 400);
}
/* A form field that carries JSON is parsed under a value budget and a depth cap, like every
* other untrusted text: a field of brackets alone sits well inside the body cap and would take
* the worker down at the deep tree's teardown. Over-deep and over-budget read as a bad parse. */
TEST(a_form_field_of_brackets_is_refused_rather_than_parsed) {
Videos v;
const std::string deep = std::string(4096, '[');
for (const char* field : { "target", "conditions", "loras" }) {
HttpRequest q;
q.method = "POST";
q.path = "/v1/videos";
q.form = { FormPart{ "prompt", "", "", "a boat" }, FormPart{ field, "", "", deep } };
CHECK_EQ(v.f->srv->router().dispatch(q).status, 400);
}
/* Over the value budget but shallow, on the same gate. */
std::string wide = "[";
for (int i = 0; i < (1 << 20) + 2; ++i) wide += (i ? "," : "") + std::string("1");
wide += "]";
HttpRequest q;
q.method = "POST";
q.path = "/v1/videos";
q.form = { FormPart{ "prompt", "", "", "a boat" }, FormPart{ "target", "", "", wide } };
CHECK_EQ(v.f->srv->router().dispatch(q).status, 400);
/* And a well-formed one still parses: it reaches the schema, not the parse refusal. */
q.form = { FormPart{ "prompt", "", "", "a boat" },
FormPart{ "target", "", "", "{\"short_edge\":256}" } };
CHECK_EQ(v.f->srv->router().dispatch(q).status, 200);
}
/* The job's life: queued at once, completed with progress 100 and an expiry, the MP4 and its
* first frame served; listing it, deleting it, and a 404 after. */
TEST(a_video_job_completes_and_serves_its_file) {