Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -680,6 +688,8 @@ 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)
# 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
Expand Down
36 changes: 36 additions & 0 deletions tests/http_guard_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <cstdio>
#include <string>
#include <thread>

static int failures = 0;

Expand Down Expand Up @@ -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;
}
Loading