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 62 additions and 58 deletions

No files matched your search

+1 -1
View File
@@ -4,7 +4,7 @@ langs = ["en", "ja"]
[site]
title = "cpp-httplib"
version = "0.69.0"
version = "0.70.0"
hostname = "https://yhirose.github.io"
base_path = "/cpp-httplib"
footer_message = "© 2026 Yuji Hirose. All rights reserved."
+24 -29
View File
@@ -8,8 +8,8 @@
#ifndef CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_HTTPLIB_H
#define CPPHTTPLIB_VERSION "0.69.0"
#define CPPHTTPLIB_VERSION_NUM "0x004500"
#define CPPHTTPLIB_VERSION "0.70.0"
#define CPPHTTPLIB_VERSION_NUM "0x004600"
#ifdef _WIN32
#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0A00
@@ -4057,7 +4057,8 @@ private:
PollerWaker waker_;
// fds_[0] is the waker's, always armed; tokens_[i] belongs to fds_[i].
// fds_[0] is the waker's, which is always armed; tokens_[i] belongs to
// fds_[i].
std::vector<struct pollfd> fds_;
std::vector<Token> tokens_;
std::unordered_map<socket_t, size_t> index_;
@@ -4110,11 +4111,6 @@ private:
// The best poller for this platform, or nullptr when it cannot be created.
std::unique_ptr<Poller> make_poller();
// The Poller::wait() timeout until `deadline`: -1 for time_point::max(),
// otherwise whole milliseconds, rounded up and capped at INT_MAX.
int poll_timeout_until(std::chrono::steady_clock::time_point deadline,
std::chrono::steady_clock::time_point now);
class BufferStream final : public Stream {
public:
BufferStream() = default;
@@ -7459,7 +7455,7 @@ inline void drain_and_close_socket(socket_t sock,
const auto n = read_socket(sock, buf, sizeof(buf), CPPHTTPLIB_RECV_FLAGS);
if (n <= 0) { break; }
total += static_cast<size_t>(n);
} while (total < size_t(1024u * 1024u) && usec_left() > 0); // bound #2
} while (usec_left() > 0 && total < size_t(1024u * 1024u)); // bound #2
shutdown_socket(sock);
close_socket(sock);
@@ -8342,18 +8338,6 @@ inline std::unique_ptr<Poller> make_poller() {
return PollPoller::create();
}
inline int poll_timeout_until(std::chrono::steady_clock::time_point deadline,
std::chrono::steady_clock::time_point now) {
using namespace std::chrono;
if (deadline == (steady_clock::time_point::max)()) { return -1; }
if (deadline <= now) { return 0; }
const auto left = deadline - now;
auto msec = duration_cast<milliseconds>(left);
if (msec < left) { msec += milliseconds(1); }
const auto cap = milliseconds((std::numeric_limits<int>::max)());
return static_cast<int>((std::min)(msec, cap).count());
}
inline bool bind_ip_address(socket_t sock, const std::string &host) {
struct addrinfo hints;
struct addrinfo *result;
@@ -16774,10 +16758,16 @@ inline bool Server::parking_loop(TaskQueue &task_queue) {
poller.arm(listen_sock, listen_token) ? never : now + check_interval;
}
auto until =
(std::min)({st.next_deadline(), listen_rearm_at, next_idle,
st.backlog.empty() ? never : now + check_interval});
poller.wait(detail::poll_timeout_until(until, now), ready);
// At most a minute, which keeps the timeout below within an int.
auto until = (std::min)({st.next_deadline(), listen_rearm_at, next_idle,
st.backlog.empty() ? never : now + check_interval,
now + std::chrono::seconds(60)});
// Rounded up, so that the wait does not end just short of the deadline.
auto timeout = std::chrono::duration_cast<std::chrono::milliseconds>(
until - now + std::chrono::microseconds(999));
poller.wait(static_cast<int>((std::max)(timeout.count(),
std::chrono::milliseconds::rep(0))),
ready);
if (svr_sock_ != listen_sock) { break; }
now = Clock::now();
@@ -17473,6 +17463,14 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf,
constexpr size_t kDecompressionBufferSize = 8192;
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) {
auto n = detail::read_body_content(stream_, body_reader_, compressed_buf,
sizeof(compressed_buf));
@@ -17484,10 +17482,7 @@ inline ssize_t ClientImpl::StreamHandle::read_with_decompression(char *buf,
compressed_buf, static_cast<size_t>(n),
[&](const char *data, size_t data_len) {
decompress_buffer_.append(data, data_len);
auto limit = body_reader_.payload_max_length;
exceeded =
limit > 0 &&
decompressed_bytes_read_ + decompress_buffer_.size() > limit;
exceeded = limit > 0 && counted + decompress_buffer_.size() > limit;
return !exceeded;
});
+37 -28
View File
@@ -16978,6 +16978,43 @@ TEST(ClientVulnerabilityTest, ZipBombWithoutContentLength) {
}
#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) {
httplib::Client cli("www.google.com", 1234);
ASSERT_EQ("www.google.com", cli.host());
@@ -34774,20 +34811,6 @@ TEST_P(PollerTest, WaitHonoursItsTimeout) {
EXPECT_LT(elapsed, std::chrono::seconds(1));
}
TEST_P(PollerTest, NegativeTimeoutWaitsUntilReady) {
TestSocketPair sp;
ASSERT_TRUE(sp.ok());
ASSERT_TRUE(poller_->arm(sp.reader(), 3));
std::thread writer([&] {
std::this_thread::sleep_for(std::chrono::milliseconds(20));
sp.make_readable();
});
auto ready = wait_for(-1);
writer.join();
EXPECT_EQ(Tokens{3}, ready);
}
TEST_P(PollerTest, OnlyTheReadyDescriptorsAreDelivered) {
std::vector<std::unique_ptr<TestSocketPair>> sps;
for (detail::Poller::Token i = 0; i < 8; i++) {
@@ -34906,20 +34929,6 @@ TEST(PollerFactoryTest, MakePollerWorks) {
EXPECT_TRUE(poller->wait(1000, ready));
}
TEST(PollTimeoutTest, LastsUntilTheDeadline) {
using namespace std::chrono;
const auto now = steady_clock::now();
auto timeout = [&](steady_clock::time_point deadline) {
return detail::poll_timeout_until(deadline, now);
};
EXPECT_EQ(-1, timeout((steady_clock::time_point::max)()));
EXPECT_EQ(0, timeout(now - milliseconds(5)));
EXPECT_EQ(1, timeout(now + nanoseconds(1)));
EXPECT_EQ(1, timeout(now + milliseconds(1)));
EXPECT_EQ(2, timeout(now + microseconds(1001)));
EXPECT_EQ((std::numeric_limits<int>::max)(), timeout(now + hours(1000)));
}
// ---------------------------------------------------------------------------
// Keep-alive parking
// ---------------------------------------------------------------------------