File cpp-httplib-CVE-2025-46728.patch of Package cpp-httplib.18891
Index: cpp-httplib-0.12.5/httplib.h
===================================================================
--- cpp-httplib-0.12.5.orig/httplib.h
+++ cpp-httplib-0.12.5/httplib.h
@@ -113,6 +113,10 @@
#define CPPHTTPLIB_LISTEN_BACKLOG 5
#endif
+#ifndef CPPHTTPLIB_MAX_LINE_LENGTH
+#define CPPHTTPLIB_MAX_LINE_LENGTH 32768
+#endif
+
/*
* Headers
*/
@@ -2365,6 +2369,11 @@ inline bool stream_line_reader::getline(
glowable_buffer_.clear();
for (size_t i = 0;; i++) {
+ if (size() >= CPPHTTPLIB_MAX_LINE_LENGTH) {
+ // Treat exceptionally long lines as an error to
+ // prevent infinite loops/memory exhaustion
+ return false;
+ }
char byte;
auto n = strm_.read(&byte, 1);
Index: cpp-httplib-0.12.5/test/test.cc
===================================================================
--- cpp-httplib-0.12.5.orig/test/test.cc
+++ cpp-httplib-0.12.5/test/test.cc
@@ -33,6 +33,9 @@ const int PORT = 1234;
const string LONG_QUERY_VALUE = string(25000, '@');
const string LONG_QUERY_URL = "/long-query-value?key=" + LONG_QUERY_VALUE;
+const string TOO_LONG_QUERY_VALUE = string(35000, '@');
+const string TOO_LONG_QUERY_URL = "/too-long-query-value?key=" + TOO_LONG_QUERY_VALUE;
+
const std::string JSON_DATA = "{\"hello\":\"world\"}";
const string LARGE_DATA = string(1024 * 1024 * 100, '@'); // 100MB
@@ -1950,6 +1953,11 @@ protected:
EXPECT_EQ(LONG_QUERY_URL, req.target);
EXPECT_EQ(LONG_QUERY_VALUE, req.get_param_value("key"));
})
+ .Get("/too-long-query-value",
+ [&](const Request &req, Response & /*res*/) {
+ EXPECT_EQ(TOO_LONG_QUERY_URL, req.target);
+ EXPECT_EQ(TOO_LONG_QUERY_VALUE, req.get_param_value("key"));
+ })
.Get("/array-param",
[&](const Request &req, Response & /*res*/) {
EXPECT_EQ(3u, req.get_param_value_count("array"));
@@ -2607,6 +2615,13 @@ TEST_F(ServerTest, LongQueryValue) {
EXPECT_EQ(414, res->status);
}
+TEST_F(ServerTest, TooLongQueryValue) {
+ auto res = cli_.Get(TOO_LONG_QUERY_URL.c_str());
+
+ ASSERT_FALSE(res);
+ EXPECT_EQ(Error::Read, res.error());
+}
+
TEST_F(ServerTest, TooLongHeader) {
Request req;
req.method = "GET";