Skip to content

Fix check for URI length to prevent incorrect HTTP 414 errors #2046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions httplib.h
Original file line number Diff line number Diff line change
Expand Up @@ -7234,21 +7234,21 @@ Server::process_request(Stream &strm, const std::string &remote_addr,
#endif
#endif

// Check if the request URI doesn't exceed the limit
if (line_reader.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) {
Headers dummy;
detail::read_headers(strm, dummy);
res.status = StatusCode::UriTooLong_414;
return write_response(strm, close_connection, req, res);
}

// Request line and headers
if (!parse_request_line(line_reader.ptr(), req) ||
!detail::read_headers(strm, req.headers)) {
res.status = StatusCode::BadRequest_400;
return write_response(strm, close_connection, req, res);
}

// Check if the request URI doesn't exceed the limit
if (req.target.size() > CPPHTTPLIB_REQUEST_URI_MAX_LENGTH) {
Headers dummy;
detail::read_headers(strm, dummy);
res.status = StatusCode::UriTooLong_414;
return write_response(strm, close_connection, req, res);
}

if (req.get_header_value("Connection") == "close") {
connection_closed = true;
}
Expand Down
13 changes: 12 additions & 1 deletion test/test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3541,7 +3541,7 @@ TEST_F(ServerTest, LongRequest) {

TEST_F(ServerTest, TooLongRequest) {
std::string request;
for (size_t i = 0; i < 545; i++) {
for (size_t i = 0; i < 546; i++) {
request += "/TooLongRequest";
}
request += "_NG";
Expand All @@ -3552,6 +3552,17 @@ TEST_F(ServerTest, TooLongRequest) {
EXPECT_EQ(StatusCode::UriTooLong_414, res->status);
}

TEST_F(ServerTest, AlmostTooLongRequest) {
// test for #2046 - URI length check shouldn't include other content on req line
// URI is max URI length, minus 14 other chars in req line (GET, space, leading /, space, HTTP/1.1)
std::string request = "/" + string(CPPHTTPLIB_REQUEST_URI_MAX_LENGTH - 14, 'A');

auto res = cli_.Get(request.c_str());

ASSERT_TRUE(res);
EXPECT_EQ(StatusCode::NotFound_404, res->status);
}

TEST_F(ServerTest, LongHeader) {
Request req;
req.method = "GET";
Expand Down