Archived
server: bound what an unauthenticated socket can make the transport hold
Three ways a client spent the server's patience or memory without ever being admitted: The gate's 401 answers before any handler sees the request, so its body sits unread on a keep-alive socket, and httplib's anti-smuggling drain will wait for it: one byte per read timeout paces a whole worker, at the pacing client's chosen rate. The refusal now closes the connection instead, says so on the response, and buries the body with the socket -- which is also what makes draining a refusal pointless. A drain that does have a purpose -- a framed body on a request the router answered without reading it -- inherits the stream's 30 s read wait per packet gap and no total bound at all. It keeps both waits now: five seconds per gap (the stream's own set_read_timeout, the same knob the WebSocket upgrade uses) and five in total (a deadline the discard provider checks), then the connection dies and the timeout is restored for whatever the connection still has coming. And bodies. The per-request cap bounds one body and no sum, every byte of a body is buffered before parse, admission or any accounting sees it, and a few hundred concurrent large POSTs is an OOM that reaches the engine before any refusal does. read_and_bridge now holds every buffered byte against a transport-wide budget and answers 429 with Retry-After to the newcomer the budget has no room for; the reservation releases on every exit, including the peer vanishing mid-body. The budget defaults to eight bodies at the per-request cap and moves through ServerOptions::max_inflight_body_bytes. Vendor: the two httplib hunks are recorded as patches/0022, and UPSTREAM's "no file content differs" for the pair is replaced with what the split actually is and what is now edited in it. transport_test: a_refusal_closes_the_connection, a_stalled_drain_dies_on_its_deadline, the_inflight_body_budget. ASan+UBSan: transport/wire/lifecycle/server/config/diag/dashboard, 7/7.
This commit is contained in:
1 parent
ec53bce5dc
commit
5f7244da4d
8 files changed
+268
-12
No files matched your search
Vendored
+44
-6
@@ -143,11 +143,15 @@ vendor/cpp-httplib/httplib.h
|
||||
vendor/cpp-httplib/httplib.cpp
|
||||
vendor/cpp-httplib/LICENSE <- llama.cpp vendor/cpp-httplib/cpp-httplib/*
|
||||
|
||||
Header-only JSON and a split-source HTTP library, both unmodified. The only difference from
|
||||
upstream is the PATH: llama.cpp nests them one directory deeper (vendor/nlohmann/nlohmann/)
|
||||
and puts the outer directory on the include path. Flattening lets `<nlohmann/json.hpp>` and
|
||||
`<cpp-httplib/httplib.h>` resolve with vendor/ alone on the path, which is the one include
|
||||
directory core/CMakeLists.txt already adds. No file content differs.
|
||||
Header-only JSON, byte for byte as lifted, and the HTTP library at cpp-httplib v0.46.1 SPLIT from
|
||||
upstream's single ~20,400-line header into a .h/.cpp pair, so one translation unit pays for it.
|
||||
Apart from the split the httplib pair matched upstream exactly when it was lifted; the split itself
|
||||
drops two decorative `// ----` separator comment lines, moves `inline` from definitions to
|
||||
declarations, and adds the .cpp's `#include "httplib.h"` and namespace brace. The only difference
|
||||
from upstream for both is the PATH: llama.cpp nests them one directory deeper
|
||||
(vendor/nlohmann/nlohmann/) and puts the outer directory on the include path. Flattening lets
|
||||
`<nlohmann/json.hpp>` and `<cpp-httplib/httplib.h>` resolve with vendor/ alone on the path, which
|
||||
is the one include directory core/CMakeLists.txt already adds.
|
||||
|
||||
cpp-httplib's own CMakeLists.txt is NOT carried. vendor/CMakeLists.txt builds httplib.cpp
|
||||
itself, because upstream's version reaches for llama.cpp's license and compile-flag helpers
|
||||
@@ -156,7 +160,19 @@ vendor/cpp-httplib/LICENSE <- llama.cpp vendor/cpp-httplib/cpp
|
||||
vendor/CMakeLists.txt sets, so a future disagreement between them would have been invisible --
|
||||
the copy nobody builds is the copy nobody checks.
|
||||
|
||||
LOCAL CHANGES: none.
|
||||
Upstream is several majors ahead of v0.46.1 now — 0.46.1 is what llama.cpp carried at the lifted
|
||||
commit. The lift is owed; when it happens, the patch below must be re-checked against the newer
|
||||
library's own keep-alive refusal and exception handling (its serve_guarded makes the provider-side
|
||||
catch core/server/http.cpp carries redundant), not just re-applied.
|
||||
|
||||
LOCAL CHANGES: patches/0022-httplib-a-refusal-closes-the-socket-and-the-drain-keeps-a-deadline.patch
|
||||
|
||||
On 0022: the API-key gate answers 401 before a handler runs, so the refused request's body sits
|
||||
unread on the keep-alive socket and upstream's anti-smuggling drain reads it byte by byte — one
|
||||
byte per socket timeout paces a whole worker, and the attacker chooses the pace. 0022 hands every
|
||||
such refusal a closed connection (the unread bytes die with it, which is also what makes the drain
|
||||
moot) and gives a drain that does run a 5 s budget — per packet gap and in total — after which the
|
||||
connection closes too.
|
||||
|
||||
|
||||
REWRITTEN INTO core/ INSTEAD
|
||||
@@ -564,3 +580,25 @@ PATCH SERIES
|
||||
with `m.content.0.type`, and since the probe swallows the throw the template came back as
|
||||
tools=0 tool_calls=0, as 0006 describes: every request with `tools` was refused.
|
||||
tests/chat_test.cpp (a_dotted_integer_is_an_index_as_in_jinja2).
|
||||
|
||||
patches/0022-httplib-a-refusal-closes-the-socket-and-the-drain-keeps-a-deadline.patch
|
||||
|
||||
vendor/cpp-httplib/, not vendor/llama/: applies from vendor/cpp-httplib/ with `patch -p1`.
|
||||
|
||||
Upstream's tail of process_request() drains an unconsumed framed body to keep it from being
|
||||
read back as the next request on a keep-alive connection. The drain has two ways to be
|
||||
weaponed against this server. A request the API-key gate refused (401 answered before any
|
||||
handler saw it) has a whole body sitting unread, and the drain will wait for it: the stream's
|
||||
read wait (30 s here) bounds each packet gap, not the drain, so a client that dribbles one
|
||||
byte per timeout holds that worker for as long as it likes, on a socket that was never going to
|
||||
be used again. And the same dribble works on any 404 with a body — the drain there has a
|
||||
purpose, but no bound.
|
||||
|
||||
The patch closes the connection on every 401 that left a framed body unread — `Connection:
|
||||
close` on the refusal itself, and the socket shut after it — and gives every drain that does
|
||||
run a 5 s budget, bounded twice: the stream's read wait shortened for the drain (the WebSocket
|
||||
upgrade already used set_read_timeout for exactly this) and a steady_clock deadline in the
|
||||
discard provider for the total. Past either, the connection is
|
||||
closed and the unread bytes die with it. The socket's own read timeout is restored after, so
|
||||
the next keep-alive request is unaffected. Tests: transport_test
|
||||
(a_refusal_closes_the_connection, the_inflight_body_budget).
|
||||
Vendored
+32
-2
@@ -8453,6 +8453,20 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
: StatusCode::PartialContent_206;
|
||||
}
|
||||
|
||||
// LOCAL CHANGE (radiance): a request the API-key gate refused (401
|
||||
// answered before any handler saw it) has no further use for this
|
||||
// connection, and must not keep one: its unread body would otherwise be
|
||||
// drained on the keep-alive socket, which hands the refusing client a
|
||||
// way to pace this worker — one byte per socket timeout, indefinitely.
|
||||
// Say close on the response itself and end the connection; the unread
|
||||
// bytes die with it, which is also what makes the drain below moot for
|
||||
// the refusal.
|
||||
if (res.status == StatusCode::Unauthorized_401 && !req.body_consumed_ &&
|
||||
detail::has_framed_body(req)) {
|
||||
close_connection = true;
|
||||
connection_closed = true;
|
||||
}
|
||||
|
||||
// Serve file content by using a content provider
|
||||
auto file_open_error = false;
|
||||
if (!res.file_content_path_.empty()) {
|
||||
@@ -8499,14 +8513,30 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
|
||||
// Drain any unconsumed framed body to prevent request smuggling on
|
||||
// keep-alive. Without framing there is no body to drain — reading would
|
||||
// consume the next request (issue #2450).
|
||||
// consume the next request (issue #2450). A 401 refusal never gets here
|
||||
// with a live connection (see the LOCAL CHANGE above).
|
||||
//
|
||||
// LOCAL CHANGE (radiance): the drain is bounded by a deadline, twice
|
||||
// over — per packet gap (the stream's read wait shortened to 5 s for the
|
||||
// drain: the connection's own 30 s bounds a gap, not the drain, and a
|
||||
// client dribbling one byte per timeout would pace this worker
|
||||
// indefinitely) and in total (the provider stops once 5 s have passed).
|
||||
// Past either, the connection is closed and the unread bytes die with it.
|
||||
if (!req.body_consumed_ && detail::has_framed_body(req)) {
|
||||
constexpr time_t kDrainBudgetSec = 5;
|
||||
strm.set_read_timeout(kDrainBudgetSec, 0);
|
||||
const auto drain_until = std::chrono::steady_clock::now() +
|
||||
std::chrono::seconds(kDrainBudgetSec);
|
||||
int dummy_status;
|
||||
if (!detail::read_content(
|
||||
strm, req, payload_max_length_, dummy_status, nullptr,
|
||||
[](const char *, size_t, size_t, size_t) { return true; }, false)) {
|
||||
[drain_until](const char *, size_t, size_t, size_t) {
|
||||
return std::chrono::steady_clock::now() < drain_until;
|
||||
},
|
||||
false)) {
|
||||
connection_closed = true;
|
||||
}
|
||||
strm.set_read_timeout(read_timeout_sec_, read_timeout_usec_);
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
Vendored
+58
@@ -0,0 +1,58 @@
|
||||
diff --git a/httplib.cpp b/httplib.cpp
|
||||
index b9d05d9..fe98b5f 100644
|
||||
--- a/httplib.cpp
|
||||
+++ b/httplib.cpp
|
||||
@@ -8453,6 +8453,20 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
: StatusCode::PartialContent_206;
|
||||
}
|
||||
|
||||
+ // LOCAL CHANGE (radiance): a request the API-key gate refused (401
|
||||
+ // answered before any handler saw it) has no further use for this
|
||||
+ // connection, and must not keep one: its unread body would otherwise be
|
||||
+ // drained on the keep-alive socket, which hands the refusing client a
|
||||
+ // way to pace this worker — one byte per socket timeout, indefinitely.
|
||||
+ // Say close on the response itself and end the connection; the unread
|
||||
+ // bytes die with it, which is also what makes the drain below moot for
|
||||
+ // the refusal.
|
||||
+ if (res.status == StatusCode::Unauthorized_401 && !req.body_consumed_ &&
|
||||
+ detail::has_framed_body(req)) {
|
||||
+ close_connection = true;
|
||||
+ connection_closed = true;
|
||||
+ }
|
||||
+
|
||||
// Serve file content by using a content provider
|
||||
auto file_open_error = false;
|
||||
if (!res.file_content_path_.empty()) {
|
||||
@@ -8499,14 +8513,30 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
|
||||
|
||||
// Drain any unconsumed framed body to prevent request smuggling on
|
||||
// keep-alive. Without framing there is no body to drain — reading would
|
||||
- // consume the next request (issue #2450).
|
||||
+ // consume the next request (issue #2450). A 401 refusal never gets here
|
||||
+ // with a live connection (see the LOCAL CHANGE above).
|
||||
+ //
|
||||
+ // LOCAL CHANGE (radiance): the drain is bounded by a deadline, twice
|
||||
+ // over — per packet gap (the stream's read wait shortened to 5 s for the
|
||||
+ // drain: the connection's own 30 s bounds a gap, not the drain, and a
|
||||
+ // client dribbling one byte per timeout would pace this worker
|
||||
+ // indefinitely) and in total (the provider stops once 5 s have passed).
|
||||
+ // Past either, the connection is closed and the unread bytes die with it.
|
||||
if (!req.body_consumed_ && detail::has_framed_body(req)) {
|
||||
+ constexpr time_t kDrainBudgetSec = 5;
|
||||
+ strm.set_read_timeout(kDrainBudgetSec, 0);
|
||||
+ const auto drain_until = std::chrono::steady_clock::now() +
|
||||
+ std::chrono::seconds(kDrainBudgetSec);
|
||||
int dummy_status;
|
||||
if (!detail::read_content(
|
||||
strm, req, payload_max_length_, dummy_status, nullptr,
|
||||
- [](const char *, size_t, size_t, size_t) { return true; }, false)) {
|
||||
+ [drain_until](const char *, size_t, size_t, size_t) {
|
||||
+ return std::chrono::steady_clock::now() < drain_until;
|
||||
+ },
|
||||
+ false)) {
|
||||
connection_closed = true;
|
||||
}
|
||||
+ strm.set_read_timeout(read_timeout_sec_, read_timeout_usec_);
|
||||
}
|
||||
|
||||
return ret;
|
||||
Reference in new issue
Block a user