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:
fl0l0u authored and StillDeadcode committed 2026-10-31 07:50:43 +01:00
1 parent ec53bce5dc
commit 5f7244da4d
8 files changed
+268 -12

No files matched your search

+32 -2
View File
@@ -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;