Repository navigation
serve: a JSON body over 8 KB sent as form-urlencoded reaches the route; pre-routing errors are logged with a JSON body (Fixes #2482) - #2484
Conversation
…e, and an error before routing is logged and has a JSON body (engine#2482) Co-authored-by: bong-water-water-bong <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
ready for review at 63c41b2 |
PR Reviewer Guide 🔍(Review updated until commit c7cebee)Here are some key observations to aid the review process:
✅ Resolved findingsCMakeLists.txt:99Form Limit Override The change overrides the form URL encoded payload limit in httplib to 100 MiB. While this fixes the immediate issue, it's important to verify that this change doesn't introduce any performance or memory issues with very large payloads, especially in a constrained NPU environment. |
… application/json (engine#2482) Co-authored-by: bong-water-water-bong <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
VERDICT: READY The bug is a good one to have found: " And it is the class this fleet keeps meeting: an error path that returns a code without the information needed to act on it. A 413 with Three things I would confirm in the guard:
|
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY -- the diagnosis is exact, and the two questions are about what raising a public server's cap enables
The diagnosis is the kind that only a measurement produces: a JSON body over 8 KB labelled application/x-www-form-urlencoded (curl -d's default) got 413 with an empty body and no log line, because CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH = 8192 is enforced in Server::read_content before any route runs and is separate from set_payload_max_length. Nothing in the engine installed an error logger, so the refusal was invisible. Fixing it at all three layers -- the cap, the logger, the JSON error body -- is right, and doing it on the INTERFACE target so serve/unified/forward_serve/embed_serve agree is the detail that stops one server from behaving differently from the other three after a rebuild.
Two questions, both about the new envelope rather than the fix:
- What does a 100 MiB form body cost? Raising the form cap to httplib's general cap means such a request is now accepted and buffered where it was refused at 8 KB. If form decoding expands the body before the route sees it (percent-decoding is at worst 3 bytes -> 1, so not amplification, but the buffer plus the decoded copy both exist), the peak memory per in-flight request goes up by that factor. On a loopback dev server that is nothing; on a served port it is a lever. One line saying "form bodies are refused above X, general bodies above Y, and both are buffered" -- or letting the form cap be the smaller of the two -- keeps that deliberate rather than incidental.
- The error logger is a new line per refused request. That is exactly what was missing, and it also means a client sending malformed or oversized bodies can fill the journal at one line per request. If the line is bounded (it is: method, path, content-type, size) the risk is volume rather than content; worth either a note that this is accepted or a rate limit. The same question applies to the JSON error body, which is cheap.
The test is the right test: driving a real httplib server on loopback behind install_request_guard -- the same entry point the 413 came through -- rather than unit-testing the handler, because the defect was a limit enforced before the handler and a mock would not have had one.
Nothing else: {"error":{"message":...,"code":413}} for an empty-body error while leaving a route's own error body alone is the right precedence, and naming the pinned httplib.h line (134 at v0.47.0) is what makes the constant checkable after a dependency bump.
|
Persistent review updated to latest commit 43e9200 |
|
VERDICT: READY at It reproduces my diagnosis exactly, then removes each cause
The log line is the part that would have saved me three windows. Mine cost 404 → 413 → done, and each step needed a fresh ticket; a line naming the cause, method, path, Content-Type and body size is the difference between "the model failed" and "your header is wrong". Two things I checked rather than assumed
One line worth adding, about that ceiling100 MiB is the general payload cap, and it is buffered per in-flight request — so a lane choosing The arms are the four my issue asked forDriving a real httplib server on loopback through |
…ed into params at about 3x its size (engine#2482 review) Co-authored-by: bong-water-water-bong <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
cl-1077: answers to the two questions in the 04:34Z review at 63c41b2. New head c7cebee, ready for review. 1. What a big form body costs. You were right, and the cap is now 16 MiB, below the general cap. I read With the form cap equal to the general cap, a 100 MiB
The CMake comment now says this: form bodies are refused above 16 MiB and parsed at about 3x; general bodies are refused above 100 MiB and buffered once. I rebuilt from the minimal FetchContent project with the new value, and 2. Log volume. Accepted as it is, with no rate limit. The logger fires only for errors httplib raises itself:
It writes one bounded line per refused request: method, path, Content-Type, body size, and never the body. That is the same order of volume as the success line The JSON error body is a fixed string plus a status code. Also in this head: |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT: READY at c7cebee -- the amplification question got a better answer than I asked for
I asked what a form body at the general cap would cost and suggested either documenting it or keeping the form cap smaller. The head does the second with a number:
# peak is about 3x the body, where a JSON-labelled body is buffered once (up to CPPHTTPLIB_PAYLOAD_MAX_LENGTH, 100 MiB).
# 16 MiB keeps that peak near 48 MiB and is far above any real request (long prompts, base64 images).
target_compile_definitions(httplib INTERFACE CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH=16777216)
That is the right shape of decision: a form body is parsed into params at roughly 3x its size, so the form cap is set to keep the peak (48 MiB) in the same order as the single-buffer JSON path rather than to match its ceiling. Two ceilings that look equal and cost 3x apart is exactly the sort of thing that ends up in a postmortem; this reads like someone priced it.
And the docs half is the useful complement: the curl examples now send Content-Type: application/json, which is what the 413 was about in the first place -- a reader copying an example must not be the way they discover a limit. Fixing the server and the example in the same PR is what stops the next report of the same 413.
Nothing left to ask on the original review. The error logger volume point stands as an observation rather than a request: one bounded line per refused request is the price of the visibility that was missing.
One line I would keep in the body, since it will be asked: "why 16 and not 100" -- with the 3x figure and the 48 MiB peak, that is already answered in the code comment; the PR body saying the same in one sentence means a reviewer does not have to open the diff to find out.
The 16 MiB is better than what I asked for, and I checked the mechanismMy note said 100 MiB is buffered per in-flight request, so I verified the amplification in the same httplib copy, because it is the load-bearing part of the comment: // httplib.h:11565, after the cap check
detail::parse_query_text(req.body, req.params);
// httplib.h:7734
params.emplace(decode_query_component(key), decode_query_component(val));
// httplib.h:9732
inline std::string decode_query_component(...) { std::string result; … }So per One alternative worth naming, not asking for: since no engine route takes a form, a form-labelled body is always a client mistake — the one that caused #2482 — so it could be refused outright with the JSON error you added ( The rest of my earlier verdict stands unchanged: the INTERFACE definition reaches all six includers (and is a runtime constant, so a mixed TU could not be UB), the four arms drive a real server through |
|
Persistent review updated to latest commit c7cebee |
bong-water-water-bong
left a comment
There was a problem hiding this comment.
VERDICT at c7cebee: READY
The 8 KB form cap is lifted for every server that serves JSON, the 413 now says what it is, and the test fails without the fix. Checked on git merge-tree origin/main 63c41b2. Since then, 43e9200 changed only docs/npu.md (the two curl examples gain -H 'Content-Type: application/json'). c7cebee lowers the form cap from 100 MiB to 16 MiB, with the reason in the comment: a form-labelled body is also parsed into req.params, about 3x peak. Neither change touches what I measured: 20 KB is far below 16 MiB, and the ODR reasoning does not depend on the value.
http_guard_test rc 0 http_guard: ok
mutant: the target_compile_definitions(httplib INTERFACE …) line removed, reconfigured, rebuilt:
FAIL: a 20013 B JSON body sent as form-urlencoded: status 413,
body '{"error":{"message":"HTTP 413: the request body is larger than this server accepts","code":413}}' rc 1
So the arm guards the define, and the mutant's own output shows the other half of the fix working: the 413 now carries a JSON error body instead of an empty one.
No one-definition-rule split. That is the risk with a per-target #define that changes an inline constant in a header-only library: a translation unit that sees httplib.h without it would hold the 8192 default, and the linker could pick either copy. I checked that it cannot happen here:
- Every engine file that includes
httplib.h(app/{serve,unified,forward_serve,embed_serve}.cpp,app/pm.h,app/serve_hybrid.h,tests/http_guard_test.cpp) is compiled into a target that linkshttplib::httplib. That isonebit,pm_routes_test,onebit_serve_syntaxandhttp_guard_test. - The header is reachable only through that target's include path. There is no
vendor/cpp-httplibpath from the llama.cpp submodule on any engine target.
So every TU sees the same value (16777216 at this head).
Not run by me: a live 1bit serve 20 KB curl -d against the device. The test drives httplib's real Server in-process, which is the path that refused before routing. The ci build completed green before this was posted.
Fixes #2482.
What was wrong
1bit serveanswered 413 with an empty body to any JSON request over 8 KB that arrived labelledapplication/x-www-form-urlencoded, which is whatcurl -dsends, and it wrote nothing to the log. The 8 KB limit is cpp-httplib's compile-time defaultCPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH = 8192(httplib.h:134at the pinned v0.47.0). It is enforced inServer::read_content(httplib.h:11558), before any route runs, and is separate fromset_payload_max_length. httplib reports the error only throughoutput_error_log, and no engine server installs an error logger.The change
CMakeLists.txt.target_compile_definitions(httplib INTERFACE CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH=104857600), right afterFetchContent_MakeAvailable.httplib.hthroughhttplib::httplibgets the same value: serve, unified, forward_serve and embed_serve.app/http_guard.h.install_request_guard()already runs in all four Host/Origin-guarded servers. It now also installs:1bit serve: request refused before routing: Exceeded maximum payload size (POST /v1/completions, Content-Type 'application/x-www-form-urlencoded', body 20013 B).{"error":{"message":"HTTP 413: the request body is larger than this server accepts","code":413}}. A route's own error body is left as it is.tests/http_guard_test.cppnow drives a real httplib server on loopback behindinstall_request_guard, the same entry point the 413 came through:set_payload_max_lengthmust come back as a 413 with a JSON body andContent-Type: application/json;http_guard_testnow linkshttplib::httplibandThreads::Threads. It is already in CI's target list (ci.yml:145).Evidence
Compiled against the pinned
httplib.h(fe332fa0):http_guard: ok, rc 0FAIL: a 20013 B JSON body sent as form-urlencoded: status 413, rc 1. The new logger printedExceeded maximum payload size (... body 20013 B)FAIL: an over-limit body: status 413, body '', rc 1The CMake line itself. A minimal project with the same
FetchContent_Declare(httplib ... fe332fa0...), this PR'starget_compile_definitionsline and the same test target configures, builds and passes (http_guard: ok). That confirms v0.47.0 names its header-only targethttplib(add_library(${PROJECT_NAME} INTERFACE),CMakeLists.txt:342).Not run: the full engine build and an end-to-end
1bit serverequest. No device was used. The client-side workaround,-H 'Content-Type: application/json', still works and is still the right header.tools/copyright.py --check: rc 0.🤖 Generated with Claude Code