mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-17 10:59:39 +00:00
Fix OSS-Fuzz #508342856: cap Content-Length reservation by payload_max_length_
A malicious or malformed server response with an enormous Content-Length header (e.g. 20000000000) caused the client to call res.body.reserve(len) with the untrusted value, triggering OOM before read_content's payload_max_length_ check could take effect. Cap the pre-reservation at payload_max_length_, since reading more than that is never useful.
This commit is contained in:
1 parent
cae753425e
commit
2d2efe46da
3 files changed
+87
-1
No files matched your search
@@ -13632,7 +13632,15 @@ inline bool ClientImpl::process_request(Stream &strm, Request &req,
|
||||
output_error_log(error, &req);
|
||||
return false;
|
||||
}
|
||||
res.body.reserve(static_cast<size_t>(len));
|
||||
// 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_;
|
||||
}
|
||||
res.body.reserve(reserve_len);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user