diff --git a/core/mm/media.cpp b/core/mm/media.cpp index 586a2173..17559449 100644 --- a/core/mm/media.cpp +++ b/core/mm/media.cpp @@ -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; diff --git a/core/server/oai.cpp b/core/server/oai.cpp index 061fbfd9..47f3fd7f 100644 --- a/core/server/oai.cpp +++ b/core/server/oai.cpp @@ -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 } }); } diff --git a/core/server/videos.cpp b/core/server/videos.cpp index 53ac4396..eceee3bd 100644 --- a/core/server/videos.cpp +++ b/core/server/videos.cpp @@ -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; diff --git a/tests/server_test.cpp b/tests/server_test.cpp index 077a9f8a..8bdeb682 100644 --- a/tests/server_test.cpp +++ b/tests/server_test.cpp @@ -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("tool_use")); + CHECK_EQ(j["content"][0]["name"].get(), 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"; diff --git a/tests/videos_test.cpp b/tests/videos_test.cpp index 4420553a..6083870f 100644 --- a/tests/videos_test.cpp +++ b/tests/videos_test.cpp @@ -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) {