Compare commits

...
Author SHA1 Message Date
yhirose 1f59c57662 Release v0.70.0 2026-12-05 11:50:35 -06:00
yhirose d81a932f2a Bound what one stream read inflates when there is no payload limit
With a zero payload limit, StreamHandle::read_with_decompression() held
everything a compressed read inflated to in memory with no bound, and
SSEClient always reads its stream with a zero limit. Keep a single read
within the default payload limit, while the stream as a whole stays
unbounded.
2026-12-05 11:28:46 -06:00
3 changed files with 49 additions and 7 deletions

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@ langs = ["en", "ja"]
[site] [site]
title = "cpp-httplib" title = "cpp-httplib"
version = "0.69.0" version = "0.70.0"
hostname = "https://yhirose.github.io" hostname = "https://yhirose.github.io"
base_path = "/cpp-httplib" base_path = "/cpp-httplib"
footer_message = "© 2026 Yuji Hirose. All rights reserved." footer_message = "© 2026 Yuji Hirose. All rights reserved."
+11 -6
View File
@@ -8,8 +8,8 @@
#ifndef CPPHTTPLIB_HTTPLIB_H #ifndef CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_HTTPLIB_H #define CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_VERSION "0.69.0" #define CPPHTTPLIB_VERSION "0.70.0"
#define CPPHTTPLIB_VERSION_NUM "0x004500" #define CPPHTTPLIB_VERSION_NUM "0x004600"
#ifdef _WIN32 #ifdef _WIN32
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00 #if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
@@ -17463,6 +17463,14 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf,
constexpr size_t kDecompressionBufferSize = 8192; constexpr size_t kDecompressionBufferSize = 8192;
char compressed_buf[kDecompressionBufferSize]; char compressed_buf[kDecompressionBufferSize];
auto limit = body_reader_.payload_max_length;
auto counted = decompressed_bytes_read_;
if (limit == 0) {
// The stream is unbounded, but one read holds all it inflates at once
limit = CPPHTTPLIB_PAYLOAD_MAX_LENGTH;
counted = 0;
}
while (true) { while (true) {
auto n = detail::read_body_content(stream_, body_reader_, compressed_buf, auto n = detail::read_body_content(stream_, body_reader_, compressed_buf,
sizeof(compressed_buf)); sizeof(compressed_buf));
@@ -17474,10 +17482,7 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf,
compressed_buf, static_cast<size_t>(n), compressed_buf, static_cast<size_t>(n),
[&](const char *data, size_t data_len) { [&](const char *data, size_t data_len) {
decompress_buffer_.append(data, data_len); decompress_buffer_.append(data, data_len);
auto limit = body_reader_.payload_max_length; exceeded = limit > 0 && counted + decompress_buffer_.size() > limit;
exceeded =
limit > 0 &&
decompressed_bytes_read_ + decompress_buffer_.size() > limit;
return !exceeded; return !exceeded;
}); });
+37
View File
@@ -16978,6 +16978,43 @@ TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) {
} }
#endif #endif
#ifdef CPPHTTPLIB_BROTLI_SUPPORT
// A zero limit leaves a stream unbounded, but a body of a few bytes must not
// inflate past the default limit in a single read
TEST(ClientVulnerabilityTest, BrotliBombInStreamWithoutPayloadLimit) {
std::string bomb(1024, '\0');
{
std::string body(CPPHTTPLIB_PAYLOAD_MAX_LENGTH + 1024 * 1024, 'A');
auto size = bomb.size();
ASSERT_TRUE(BrotliEncoderCompress(
5, BROTLI_DEFAULT_WINDOW, BROTLI_MODE_GENERIC, body.size(),
reinterpret_cast<const uint8_t *>(body.data()), &size,
reinterpret_cast<uint8_t *>(&bomb[0])));
bomb.resize(size);
}
Server svr;
svr.Get("/", [&](const Request &, Response &res) {
res.set_header("Content-Encoding", "br");
res.set_content(bomb, "text/plain");
});
auto port = svr.bind_to_any_port(HOST);
std::thread t([&] { svr.listen_after_bind(); });
auto se = detail::scope_exit([&] {
svr.stop();
t.join();
});
svr.wait_until_ready();
Client cli(HOST, port);
cli.set_payload_max_length(0);
auto result = stream::Get(cli, "/");
ASSERT_TRUE(result.is_valid());
while (result.next()) {}
EXPECT_EQ(Error::ExceedMaxPayloadSize, result.read_error());
}
#endif
TEST(HostAndPortPropertiesTest, NoSSL) { TEST(HostAndPortPropertiesTest, NoSSL) {
httplib::Client cli("www.google.com", 1234); httplib::Client cli("www.google.com", 1234);
ASSERT_EQ("www.google.com", cli.host()); ASSERT_EQ("www.google.com", cli.host());