-
Notifications
You must be signed in to change notification settings - Fork 1k
Harden multi-threaded http server #5149
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -294,7 +294,14 @@ request_parser::initializeForBody(request const& req) | |||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| if (header.name == "Content-Length") | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| body_content_length_ = stoi(header.value); | ||||||||||||||||||||||||||
| try | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| body_content_length_ = stoull(header.value); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
SirTyson marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| catch (std::exception const&) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| return bad; | ||||||||||||||||||||||||||
SirTyson marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
Comment on lines
+301
to
+304
|
||||||||||||||||||||||||||
| catch (std::exception const&) | |
| { | |
| return bad; | |
| } | |
| catch (std::invalid_argument const&) | |
| { | |
| return bad; | |
| } | |
| catch (std::out_of_range const&) | |
| { | |
| return bad; | |
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,107 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Copyright 2026 Stellar Development Foundation and contributors. Licensed | ||||||||||||||||||||||||||||||||||||||||||||||||
| // under the Apache License, Version 2.0. See the COPYING file at the root | ||||||||||||||||||||||||||||||||||||||||||||||||
| // of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| #include "lib/catch.hpp" | ||||||||||||||||||||||||||||||||||||||||||||||||
| #include "lib/httpthreaded/request.hpp" | ||||||||||||||||||||||||||||||||||||||||||||||||
| #include "lib/httpthreaded/request_parser.hpp" | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| using namespace httpThreaded::server; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| namespace | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // Helper: feed a raw HTTP request string into the parser and return | ||||||||||||||||||||||||||||||||||||||||||||||||
| // the final result_type. | ||||||||||||||||||||||||||||||||||||||||||||||||
| request_parser::result_type | ||||||||||||||||||||||||||||||||||||||||||||||||
| parseRaw(std::string const& raw, request& req) | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| request_parser parser; | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto [result, iter] = parser.parse(req, raw.begin(), raw.end()); | ||||||||||||||||||||||||||||||||||||||||||||||||
| return result; | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| TEST_CASE("request parser rejects malformed Content-Length", "[http]") | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // non-numeric Content-Length | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| std::string raw = "POST /test HTTP/1.1\r\n" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "Content-Length: garbage\r\n" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\r\n" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "body"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| request req; | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto result = parseRaw(raw, req); | ||||||||||||||||||||||||||||||||||||||||||||||||
| REQUIRE(result == request_parser::bad); | ||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // negative Content-Length wraps to huge value | ||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||
| // stoull("-1") wraps to ULLONG_MAX rather than throwing, so the | ||||||||||||||||||||||||||||||||||||||||||||||||
| // parser accepts the header but will never accumulate enough body | ||||||||||||||||||||||||||||||||||||||||||||||||
| // bytes — it stays indeterminate. | ||||||||||||||||||||||||||||||||||||||||||||||||
| std::string raw = "POST /test HTTP/1.1\r\n" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "Content-Length: -1\r\n" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "\r\n" | ||||||||||||||||||||||||||||||||||||||||||||||||
| "body"; | ||||||||||||||||||||||||||||||||||||||||||||||||
| request req; | ||||||||||||||||||||||||||||||||||||||||||||||||
| auto result = parseRaw(raw, req); | ||||||||||||||||||||||||||||||||||||||||||||||||
| REQUIRE(result == request_parser::indeterminate); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+37
to
+48
|
||||||||||||||||||||||||||||||||||||||||||||||||
| // negative Content-Length wraps to huge value | |
| { | |
| // stoull("-1") wraps to ULLONG_MAX rather than throwing, so the | |
| // parser accepts the header but will never accumulate enough body | |
| // bytes — it stays indeterminate. | |
| std::string raw = "POST /test HTTP/1.1\r\n" | |
| "Content-Length: -1\r\n" | |
| "\r\n" | |
| "body"; | |
| request req; | |
| auto result = parseRaw(raw, req); | |
| REQUIRE(result == request_parser::indeterminate); | |
| // negative Content-Length is invalid | |
| { | |
| // stoull("-1") throws std::invalid_argument, so the parser | |
| // treats this as a malformed header and rejects the request. | |
| std::string raw = "POST /test HTTP/1.1\r\n" | |
| "Content-Length: -1\r\n" | |
| "\r\n" | |
| "body"; | |
| request req; | |
| auto result = parseRaw(raw, req); | |
| REQUIRE(result == request_parser::bad); |
Copilot
AI
Feb 20, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rejecting Content-Length: 0 may not be correct according to HTTP specifications. A Content-Length of 0 is valid for HTTP POST requests and indicates an empty body. Unless there is a specific requirement in this codebase to reject empty-bodied POST requests, this validation at line 305-308 in request_parser.cpp should be reconsidered. The test at lines 73-81 enforces this behavior, but it may be overly restrictive.
| // Content-Length zero | |
| { | |
| std::string raw = "POST /test HTTP/1.1\r\n" | |
| "Content-Length: 0\r\n" | |
| "\r\n"; | |
| request req; | |
| auto result = parseRaw(raw, req); | |
| REQUIRE(result == request_parser::bad); | |
| // Content-Length zero should be accepted as an empty body | |
| { | |
| std::string raw = "POST /test HTTP/1.1\r\n" | |
| "Content-Length: 0\r\n" | |
| "\r\n"; | |
| request req; | |
| auto result = parseRaw(raw, req); | |
| REQUIRE(result == request_parser::good); | |
| REQUIRE(req.method == "POST"); | |
| REQUIRE(req.uri == "/test"); | |
| REQUIRE(req.body.empty()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm, but can we ask claude for a quick unit test that repros the issue and confirms the fix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done