Skip to content

Commit ed1b6af

Browse files
authored
Fix crash caused by header field regex complexity (#457)
1 parent 08fc708 commit ed1b6af

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

httplib.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1847,7 +1847,7 @@ inline bool read_headers(Stream &strm, Headers &headers) {
18471847
// the left or right side of the header value:
18481848
// - https://stackoverflow.com/questions/50179659/
18491849
// - https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html
1850-
static const std::regex re(R"(([^:]+):[\t ]*(.+))");
1850+
static const std::regex re(R"(([^:]+):[\t ]*([^\t ].*))");
18511851

18521852
std::cmatch m;
18531853
if (std::regex_match(line_reader.ptr(), end, m, re)) {

test/test.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,6 +2333,19 @@ TEST(ServerRequestParsingTest, ReadHeadersRegexComplexity2) {
23332333
"&&&%%%");
23342334
}
23352335

2336+
TEST(ServerRequestParsingTest, ExcessiveWhitespaceInUnparseableHeaderLine) {
2337+
// Make sure this doesn't crash the server.
2338+
// In a previous version of the header line regex, the "\r" rendered the line
2339+
// unparseable and the regex engine repeatedly backtracked, trying to look for
2340+
// a new position where the leading white space ended and the field value
2341+
// began.
2342+
// The crash occurs with libc++ but not libstdc++.
2343+
test_raw_request("GET /hi HTTP/1.1\r\n"
2344+
"a:" + std::string(2000, ' ') + '\r' + std::string(20, 'z') +
2345+
"\r\n"
2346+
"\r\n");
2347+
}
2348+
23362349
TEST(ServerRequestParsingTest, InvalidFirstChunkLengthInRequest) {
23372350
std::string out;
23382351

0 commit comments

Comments
 (0)