From fe354303043a059f14361ea2775fd4faa2048e59 Mon Sep 17 00:00:00 2001 From: pi-prs Date: Sun, 15 Nov 2026 01:06:51 -0400 Subject: [PATCH 1/2] serve: a form-labelled body over 8 KiB reaches the handler instead of an empty 413 (engine#2482) cpp-httplib refuses any body over CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH -- 8 KiB by default -- when the request says `Content-Type: application/x-www-form-urlencoded`. `curl -d` sends exactly that header by default, so a JSON body posted the obvious way was refused with 413 and a ZERO-BYTE reply, and the engine logged no line for it: the client saw a rejection with no reason and the server's own log had no record. Measured in engine#2482: 771 B -> 200, ~94 KB -> 413, 0 bytes. Raised to 64 MiB rather than to unlimited: the engine accepts base64 images in a chat request (a 10 MB PNG is ~13 MB encoded), so the ceiling has to clear that, and it stays a ceiling because the limit exists to bound a parse. tests/http_form_limit_test.cpp asserts the limit itself, CPU only -- it starts an httplib::Server on a loopback port and POSTs 771 B and 94 KiB with the form content type, so no model, no device and no `1bit serve` are involved. The arm asserts that the HANDLER ANSWERED rather than merely "not 413", because a 413 is one way to fail and a dropped connection is another. Mutation-checked, which is the only reason to believe it: with the define -> PASS, 0 failures define removed -> ok 771 B / FAIL 94 KiB / FAIL handler-answered, 2 failures, rc=1 --- CMakeLists.txt | 15 +++++++ tests/http_form_limit_test.cpp | 79 ++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 tests/http_form_limit_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 4d9d27dc5..428eddb28 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,6 +82,14 @@ set(HTTPLIB_USE_ZLIB_IF_AVAILABLE OFF CACHE BOOL "" FORCE) set(HTTPLIB_USE_BROTLI_IF_AVAILABLE OFF CACHE BOOL "" FORCE) set(HTTPLIB_USE_ZSTD_IF_AVAILABLE OFF CACHE BOOL "" FORCE) set(HTTPLIB_INSTALL OFF CACHE BOOL "" FORCE) + +# engine#2482: cpp-httplib refuses a body over 8 KiB with 413 and an EMPTY reply when the request says +# `Content-Type: application/x-www-form-urlencoded` -- which is what `curl -d` sends by default, even when +# the bytes are JSON. A 413 is right for a form body and wrong for this one, and the empty body plus no +# server log line makes it indistinguishable from a refusal the engine chose. Raised to 64 MiB, not to +# unlimited: the engine takes base64 images in a chat request (a 10 MB PNG is ~13 MB encoded), so the +# ceiling has to clear that, and it stays a ceiling because the limit exists to bound a parse. +add_compile_definitions(CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH=67108864) FetchContent_Declare(httplib GIT_REPOSITORY https://github.com/yhirose/cpp-httplib.git GIT_TAG fe332fa06bac76a1c6d402c08f414052999347da) # v0.47.0 @@ -681,6 +689,13 @@ add_dependencies(onebit serve_degenerate_test) add_executable(http_guard_test tests/http_guard_test.cpp) target_include_directories(http_guard_test PRIVATE app) add_test(NAME http_guard COMMAND http_guard_test) + +# engine#2482: a form-labelled body over 8 KiB must reach the handler instead of a 413 with an empty +# reply. CPU only -- it starts an httplib::Server on a loopback port and POSTs to it, so no model, no +# device and no `1bit serve`; the compile-time limit is the thing that changed. +add_executable(http_form_limit_test tests/http_form_limit_test.cpp) +target_link_libraries(http_form_limit_test PRIVATE httplib::httplib Threads::Threads) +add_test(NAME http_form_limit COMMAND http_form_limit_test) # The "the device is gone" classifiers of app/serve_watchdog.h: a backend's device-lost stderr line, and the # NPU runtime's failed-runlist message that makes `1bit unified` answer the request and exit non-zero. Header # only, no device, no model. diff --git a/tests/http_form_limit_test.cpp b/tests/http_form_limit_test.cpp new file mode 100644 index 000000000..ac1bee732 --- /dev/null +++ b/tests/http_form_limit_test.cpp @@ -0,0 +1,79 @@ +// Copyright 2026 bong-water-water-bong +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// http_guard_test: which Host and Origin values app/http_guard.h lets through. +// http_form_limit_test: a form-labelled body larger than 8 KiB must reach the handler, not 413. +// +// engine#2482: `curl -d` sends `Content-Type: application/x-www-form-urlencoded` for a JSON body, so +// cpp-httplib parsed it on the form path and refused anything over CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH +// (8 KiB by default) with 413 and an empty reply -- and the engine logged nothing, so the client saw a +// rejection with no reason. This asserts the compile-time limit, which is the thing that changed: it does +// NOT need a model, a device or `1bit serve`. +// +// The arm is deliberately "the handler ran", not "not 413": a 413 is one way to fail and a dropped +// connection is another, and asserting the positive is the only version that cannot pass on a crash. +#include "httplib.h" + +#include +#include +#include + +static int failures = 0; +static void chk(const char * what, bool ok) { + if (!ok) { std::printf("FAIL %s\n", what); failures++; } + else { std::printf("ok %s\n", what); } +} + +int main() { + httplib::Server srv; + bool reached = false; + srv.Post("/v1/completions", [&](const httplib::Request &, httplib::Response & res) { + reached = true; + res.set_content(R"({"ok":true})", "application/json"); + }); + + const int port = srv.bind_to_any_port("127.0.0.1"); + chk("bound a port", port > 0); + if (port <= 0) { std::printf("%d failure(s)\n", failures); return 1; } + + std::thread th([&] { srv.listen_after_bind(); }); + srv.wait_until_ready(); // void in v0.47.0; the POST below is the real readiness check + chk("server reports running", srv.is_running()); + + { + httplib::Client cli("127.0.0.1", port); + cli.set_read_timeout(10, 0); + cli.set_write_timeout(10, 0); + + // 771 B: the shape the issue measured as working on the real server. + const std::string short_body(771, 'x'); + auto r1 = cli.Post("/v1/completions", httplib::Headers{}, short_body, + "application/x-www-form-urlencoded"); + chk("a short form-labelled body reaches the handler", r1 && r1->status == 200); + + // ~94 KiB: the size the issue measured as a 413 with a zero-byte reply. + reached = false; + const std::string long_body(94 * 1024, 'x'); + auto r2 = cli.Post("/v1/completions", httplib::Headers{}, long_body, + "application/x-www-form-urlencoded"); + chk("a 94 KiB form-labelled body reaches the handler", r2 && r2->status == 200); + chk("... and it is the handler that answered", reached); + } + + srv.stop(); + th.join(); + std::printf("%s: %d failure(s)\n", failures == 0 ? "PASS" : "FAIL", failures); + return failures == 0 ? 0 : 1; +} From 76678329157fc75b11b0877cd1eb490421f14e34 Mon Sep 17 00:00:00 2001 From: pi-prs Date: Sun, 15 Nov 2026 01:36:23 -0400 Subject: [PATCH 2/2] serve: form-limit arm joins http_guard_test, because ci.yml builds a named list (engine#2482) The first version added its own executable and ctest row, and CI failed on it in exactly the way tools/ci/ci_targets_check.py predicts: `add_test names 'http_form_limit_test', and ci.yml never builds http_form_limit`. ci.yml builds 30 named targets rather than ALL, so a new add_executable/add_test pair registers a row that is never built -- and ci.yml is a workflow file, which this lane cannot edit. So the arm moves into tests/http_guard_test.cpp, which ci.yml already builds and runs, and which tests the same subject: what the HTTP layer lets through. The row it needed is already there; CMakeLists only gains the two link libraries the socket work needs (httplib, Threads). ci_targets_check: 9 target(s) whose test never reaches ctest under ci.yml (baseline 9), and none new Recorded for the review: `scripts/pre-pr-checks.sh` runs ci_targets_check, and skipping it is how the first version reached CI red. The check builds the target it is told to, not the set CI builds, so a manual cmake+ninja of one target cannot substitute for it. Mutation-checked after the move, with true exit codes: with the define -> http_guard: ok rc=0 define removed -> FAIL 94 KiB form body -> 413 (engine#2482) rc=1 --- CMakeLists.txt | 9 +--- tests/http_form_limit_test.cpp | 79 ---------------------------------- tests/http_guard_test.cpp | 36 ++++++++++++++++ 3 files changed, 38 insertions(+), 86 deletions(-) delete mode 100644 tests/http_form_limit_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 428eddb28..9840cf2f3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -688,14 +688,9 @@ add_dependencies(onebit serve_degenerate_test) add_executable(http_guard_test tests/http_guard_test.cpp) target_include_directories(http_guard_test PRIVATE app) +# engine#2482: this test also drives a loopback httplib::Server, so it links the server and threads. +target_link_libraries(http_guard_test PRIVATE httplib::httplib Threads::Threads) add_test(NAME http_guard COMMAND http_guard_test) - -# engine#2482: a form-labelled body over 8 KiB must reach the handler instead of a 413 with an empty -# reply. CPU only -- it starts an httplib::Server on a loopback port and POSTs to it, so no model, no -# device and no `1bit serve`; the compile-time limit is the thing that changed. -add_executable(http_form_limit_test tests/http_form_limit_test.cpp) -target_link_libraries(http_form_limit_test PRIVATE httplib::httplib Threads::Threads) -add_test(NAME http_form_limit COMMAND http_form_limit_test) # The "the device is gone" classifiers of app/serve_watchdog.h: a backend's device-lost stderr line, and the # NPU runtime's failed-runlist message that makes `1bit unified` answer the request and exit non-zero. Header # only, no device, no model. diff --git a/tests/http_form_limit_test.cpp b/tests/http_form_limit_test.cpp deleted file mode 100644 index ac1bee732..000000000 --- a/tests/http_form_limit_test.cpp +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2026 bong-water-water-bong -// SPDX-License-Identifier: Apache-2.0 -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// http_guard_test: which Host and Origin values app/http_guard.h lets through. -// http_form_limit_test: a form-labelled body larger than 8 KiB must reach the handler, not 413. -// -// engine#2482: `curl -d` sends `Content-Type: application/x-www-form-urlencoded` for a JSON body, so -// cpp-httplib parsed it on the form path and refused anything over CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH -// (8 KiB by default) with 413 and an empty reply -- and the engine logged nothing, so the client saw a -// rejection with no reason. This asserts the compile-time limit, which is the thing that changed: it does -// NOT need a model, a device or `1bit serve`. -// -// The arm is deliberately "the handler ran", not "not 413": a 413 is one way to fail and a dropped -// connection is another, and asserting the positive is the only version that cannot pass on a crash. -#include "httplib.h" - -#include -#include -#include - -static int failures = 0; -static void chk(const char * what, bool ok) { - if (!ok) { std::printf("FAIL %s\n", what); failures++; } - else { std::printf("ok %s\n", what); } -} - -int main() { - httplib::Server srv; - bool reached = false; - srv.Post("/v1/completions", [&](const httplib::Request &, httplib::Response & res) { - reached = true; - res.set_content(R"({"ok":true})", "application/json"); - }); - - const int port = srv.bind_to_any_port("127.0.0.1"); - chk("bound a port", port > 0); - if (port <= 0) { std::printf("%d failure(s)\n", failures); return 1; } - - std::thread th([&] { srv.listen_after_bind(); }); - srv.wait_until_ready(); // void in v0.47.0; the POST below is the real readiness check - chk("server reports running", srv.is_running()); - - { - httplib::Client cli("127.0.0.1", port); - cli.set_read_timeout(10, 0); - cli.set_write_timeout(10, 0); - - // 771 B: the shape the issue measured as working on the real server. - const std::string short_body(771, 'x'); - auto r1 = cli.Post("/v1/completions", httplib::Headers{}, short_body, - "application/x-www-form-urlencoded"); - chk("a short form-labelled body reaches the handler", r1 && r1->status == 200); - - // ~94 KiB: the size the issue measured as a 413 with a zero-byte reply. - reached = false; - const std::string long_body(94 * 1024, 'x'); - auto r2 = cli.Post("/v1/completions", httplib::Headers{}, long_body, - "application/x-www-form-urlencoded"); - chk("a 94 KiB form-labelled body reaches the handler", r2 && r2->status == 200); - chk("... and it is the handler that answered", reached); - } - - srv.stop(); - th.join(); - std::printf("%s: %d failure(s)\n", failures == 0 ? "PASS" : "FAIL", failures); - return failures == 0 ? 0 : 1; -} diff --git a/tests/http_guard_test.cpp b/tests/http_guard_test.cpp index fc93c1b9d..b459c28cf 100644 --- a/tests/http_guard_test.cpp +++ b/tests/http_guard_test.cpp @@ -15,9 +15,11 @@ // http_guard_test: which Host and Origin values app/http_guard.h lets through. #include "http_guard.h" +#include "httplib.h" #include #include +#include static int failures = 0; @@ -57,6 +59,40 @@ int main() { expect(true, "0.0.0.0", "POST", "box.lan:8000", "http://box.lan:8000"); expect(false, "0.0.0.0", "POST", "box.lan:8000", "http://other.lan:8000"); expect(false, "0.0.0.0", "DELETE", "box.lan:8000", "https://evil.example"); + + // engine#2482: what the HTTP layer refuses, not only what the guard refuses. cpp-httplib returns 413 + // with an EMPTY body when a request labelled `application/x-www-form-urlencoded` (which is what + // `curl -d` sends) carries more than CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH -- 8 KiB by + // default. The engine logged nothing, so a legitimate request looked like a deliberate refusal. + // Same subject as the arms above: a request the server should let through. + { + httplib::Server srv; + bool answered = false; + srv.Post("/v1/completions", [&](const httplib::Request &, httplib::Response & res) { + answered = true; + res.set_content(R"({"ok":true})", "application/json"); + }); + const int port = srv.bind_to_any_port("127.0.0.1"); + if (port <= 0) { ++failures; std::printf("FAIL could not bind a loopback port\n"); } + else { + std::thread th([&] { srv.listen_after_bind(); }); + srv.wait_until_ready(); // void in v0.47.0; the POST is the real readiness check + httplib::Client cli("127.0.0.1", port); + cli.set_read_timeout(10, 0); + cli.set_write_timeout(10, 0); + auto short_r = cli.Post("/v1/completions", httplib::Headers{}, std::string(771, 'x'), + "application/x-www-form-urlencoded"); + if (!(short_r && short_r->status == 200)) { ++failures; std::printf("FAIL 771-byte form body did not reach the handler\n"); } + answered = false; + auto long_r = cli.Post("/v1/completions", httplib::Headers{}, std::string(94 * 1024, 'x'), + "application/x-www-form-urlencoded"); + if (!(long_r && long_r->status == 200)) { ++failures; std::printf("FAIL 94 KiB form body -> %d (engine#2482: the 8 KiB form limit)\n", long_r ? long_r->status : -1); } + if (!answered) { ++failures; std::printf("FAIL the 94 KiB request never reached the handler\n"); } + srv.stop(); + th.join(); + } + } + std::printf("%s\n", failures ? "FAILED" : "http_guard: ok"); return failures ? 1 : 0; }