mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Do not reserve a response body by its claimed length
The client reserved what Content-Length claimed, up to payload_max_length, before any of the body arrived, and without a limit nothing bounded it. Reserve at most 1 MiB up front and let a longer body grow as it is read.
This commit is contained in:
1 parent
b270564c18
commit
672255619b
2 files changed
+31
-7
No files matched your search
@@ -16599,13 +16599,13 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
|
||||
output_error_log(error, &req);
|
||||
return false;
|
||||
}
|
||||
// Cap the reservation by payload_max_length_ to avoid OOM when a
|
||||
// hostile or malformed server sends an enormous Content-Length.
|
||||
// The actual body read below is bounded by payload_max_length_,
|
||||
// so reserving more than that is never useful.
|
||||
auto reserve_len = static_cast<size_t>(len);
|
||||
if (payload_max_length_ > 0 && reserve_len > payload_max_length_) {
|
||||
reserve_len = payload_max_length_;
|
||||
// Content-Length is only the server's claim, so reserve no more than
|
||||
// payload_max_length_ allows, nor more than 1 MiB before any of the
|
||||
// body has arrived. A longer body grows as it is read.
|
||||
auto reserve_len =
|
||||
(std::min)(static_cast<size_t>(len), size_t(1024 * 1024));
|
||||
if (payload_max_length_ > 0) {
|
||||
reserve_len = (std::min)(reserve_len, payload_max_length_);
|
||||
}
|
||||
res.body.reserve(reserve_len);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user