Branch data Line data Source code
1 : : // Copyright (c) 2020-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <httpserver.h>
6 : : #include <netaddress.h>
7 : : #include <test/fuzz/FuzzedDataProvider.h>
8 : : #include <test/fuzz/fuzz.h>
9 : : #include <test/fuzz/util.h>
10 : : #include <test/util/net.h>
11 : : #include <test/util/time.h>
12 : : #include <util/check.h>
13 : : #include <util/signalinterrupt.h>
14 : : #include <util/strencodings.h>
15 : :
16 : : #include <cstdint>
17 : : #include <memory>
18 : : #include <string>
19 : : #include <string_view>
20 : : #include <vector>
21 : :
22 : : std::string_view RequestMethodString(HTTPRequestMethod m);
23 : :
24 : : using namespace bitcoin_http;
25 : :
26 : : namespace {
27 : :
28 : : //! A fully parsed request must have a body consistent with its framing headers.
29 : 9039 : void CheckBodyMatchesFraming(const HTTPRequest& req)
30 : : {
31 [ - + ]: 9039 : const std::string body{req.ReadBody()};
32 [ + - ]: 9039 : const auto transfer_encoding{req.GetHeader("Transfer-Encoding")};
33 [ + - ]: 9039 : const auto content_length{req.GetHeader("Content-Length")};
34 [ + + - + : 9244 : if (transfer_encoding && ToLower(*transfer_encoding) == "chunked") {
+ - + + +
+ ]
35 [ - + - + ]: 51 : assert(body.size() <= MAX_BODY_SIZE);
36 [ + + ]: 8988 : } else if (content_length) {
37 [ - + ]: 122 : const auto parsed_length{ToIntegral<uint64_t>(*content_length)};
38 [ - + ]: 122 : assert(parsed_length);
39 [ - + - + ]: 122 : assert(body.size() == *parsed_length);
40 : : } else {
41 [ - + ]: 8866 : assert(body.empty());
42 : : }
43 : 9039 : }
44 : :
45 : : //! Parse the whole buffer in a single pass, as one I/O cycle would.
46 : 604 : void SingleShotParse(const std::string& http_buffer, FuzzedDataProvider& provider)
47 : : {
48 : 604 : using util::LineReader;
49 : :
50 : 604 : HTTPRequest http_request;
51 [ - + + - ]: 604 : LineReader reader(http_buffer, MAX_HEADERS_SIZE);
52 : 604 : try {
53 [ + + + + ]: 604 : if (http_request.Load(reader) != HTTPRequest::State::Complete) return;
54 [ - + ]: 209 : } catch (const std::runtime_error&) {
55 : 209 : return;
56 : 209 : }
57 : :
58 [ + - ]: 305 : const HTTPRequestMethod request_method = http_request.GetRequestMethod();
59 [ + - ]: 305 : (void)RequestMethodString(request_method);
60 [ - + ]: 610 : (void)http_request.GetURI();
61 [ + - ]: 305 : (void)http_request.GetHeader("Host");
62 [ + - ]: 305 : std::string header = provider.ConsumeRandomLengthString(16);
63 [ - + + - ]: 305 : (void)http_request.GetHeader(header);
64 [ + - + - ]: 915 : (void)http_request.WriteHeader(std::string(header), provider.ConsumeRandomLengthString(16));
65 [ - + + - ]: 305 : (void)http_request.GetHeader(header);
66 [ + - ]: 305 : CheckBodyMatchesFraming(http_request);
67 : 604 : }
68 : :
69 : : //! Expose HTTPRemoteClient with the receive buffer.
70 : 1208 : class FuzzClient : public HTTPRemoteClient
71 : : {
72 : : public:
73 [ + - + - ]: 1208 : FuzzClient() : HTTPRemoteClient{/*id=*/0, /*addr=*/CService(), /*socket=*/std::make_unique<ZeroSock>()} {}
74 [ + - ]: 360657 : void Receive(std::string_view s) { MutateRecvBuffer().append(s); }
75 : : };
76 : :
77 : 686520 : int StateRank(HTTPRequest::State s)
78 : : {
79 [ + + - + : 686520 : switch (s) {
- + ]
80 : : case HTTPRequest::State::Init: return 0;
81 : 369294 : case HTTPRequest::State::NeedsHeaders: return 1;
82 : 57877 : case HTTPRequest::State::NeedsBody: return 2;
83 : 0 : case HTTPRequest::State::Complete: return 3;
84 : 770 : case HTTPRequest::State::Error: return 4;
85 : : }
86 : 0 : AssertUnreachable();
87 : : }
88 : :
89 : : struct ParsedRequest {
90 : 4367 : HTTPRequestMethod method;
91 : 4367 : std::string uri;
92 : 4367 : std::string host;
93 : 4367 : std::string body;
94 [ + - + - : 4367 : bool operator==(const ParsedRequest&) const = default;
+ - - + ]
95 : : };
96 : :
97 : 1208 : struct RunResult {
98 : : std::vector<ParsedRequest> requests;
99 : : bool errored{false};
100 : : std::string remainder;
101 : : HTTPRequest::State last_state{HTTPRequest::State::Init};
102 : : };
103 : :
104 : : //! Take every request the client can parse right now, checking what must hold
105 : : //! at each I/O cycle boundary.
106 : 361261 : void Drain(const std::shared_ptr<FuzzClient>& client, RunResult& out)
107 : : {
108 : 378729 : while (true) {
109 : : // A failed request is inert: further reads consume nothing.
110 [ + + ]: 369995 : if (const HTTPRequest* cur{client->GetRequest()};
111 [ + + + + ]: 369995 : cur && cur->GetState() == HTTPRequest::State::Error) {
112 [ - + ]: 18001 : const size_t buffered{client->GetRecvBuffer().size()};
113 [ + - + - : 36002 : Assert(HTTPRemoteClient::TryReadRequest(client) == nullptr);
+ - + - ]
114 [ - + - + ]: 18001 : assert(client->GetRecvBuffer().size() == buffered);
115 : 18001 : out.errored = true;
116 : 18001 : break;
117 : : }
118 : :
119 [ + - + - ]: 703988 : std::unique_ptr<HTTPRequest> req{HTTPRemoteClient::TryReadRequest(client)};
120 : :
121 [ + + ]: 351994 : if (!req) {
122 [ + - ]: 343260 : if (const HTTPRequest* cur{client->GetRequest()}) {
123 : : // TryReadRequest() returns empty if the HTTPRemoteClient is still
124 : : // holding on to the request. In this test, the request is guaranteed
125 : : // to be incomplete or failed, although in production it may be a result of
126 : : // a busy worker or a full send buffer.
127 [ - + ]: 343260 : assert(cur->GetState() != HTTPRequest::State::Complete);
128 [ + - + - : 343260 : assert(StateRank(cur->GetState()) >= StateRank(out.last_state));
- + ]
129 [ + + ]: 343260 : out.last_state = cur->GetState();
130 [ + + ]: 343260 : if (cur->GetState() == HTTPRequest::State::Error) out.errored = true;
131 [ + + ]: 343260 : if (const auto chunk_size{cur->GetChunkSize()}) {
132 [ - + ]: 9098 : assert(cur->GetChunkProgress() <= *chunk_size);
133 : : }
134 [ - + - + : 1029780 : assert(cur->ReadBody().size() <= MAX_BODY_SIZE);
- + ]
135 : : }
136 : 343260 : break;
137 : : }
138 : :
139 [ - + ]: 8734 : assert(req->GetState() == HTTPRequest::State::Complete);
140 [ + - ]: 8734 : CheckBodyMatchesFraming(*req);
141 [ - + - + : 34936 : out.requests.emplace_back(req->GetRequestMethod(), req->GetURI(),
+ - ]
142 [ - + + - : 26202 : req->GetHeader("Host").value_or(""), req->ReadBody());
+ - ]
143 : :
144 : : // While a request is with a worker, nothing new is parsed or consumed.
145 [ - + ]: 8734 : const size_t buffered{client->GetRecvBuffer().size()};
146 [ + - + - : 17468 : Assert(HTTPRemoteClient::TryReadRequest(client) == nullptr);
+ - + - ]
147 [ - + - + ]: 8734 : assert(client->GetRecvBuffer().size() == buffered);
148 : :
149 [ + - ]: 8734 : req->WriteReply(HTTP_OK, ""); // clears m_req_busy
150 : 8734 : out.last_state = HTTPRequest::State::Init;
151 : 351994 : }
152 : 361261 : }
153 : :
154 : : //! HTTP framing is defined by the byte stream, not by how TCP happened to split
155 : : //! it, so parsing the same bytes whole and in pieces must give the same result.
156 : : //! Only the piecewise run reaches the resumable parse paths.
157 : 604 : void CheckSegmentationIndependence(const std::string& input, FuzzedDataProvider& provider)
158 : : {
159 : : // WriteReply() stamps a wall-clock Date header and the client stamps a
160 : : // steady-clock idle time, both of which the fuzz determinism check rejects.
161 : 604 : FakeNodeClock clock{1610000000s};
162 [ + - ]: 604 : FakeSteadyClock steady_clock;
163 : :
164 : : // The whole stream arrives in one I/O cycle. This is similar to
165 : : // SingleShotParse(), although here we pipe it through the client and also
166 : : // parse multiple requests.
167 [ + - ]: 604 : RunResult one_shot;
168 : 604 : {
169 [ + - ]: 604 : auto client{std::make_shared<FuzzClient>()};
170 [ - + + - ]: 604 : client->Receive(input);
171 [ + - ]: 604 : Drain(client, one_shot);
172 [ + - + - ]: 1208 : one_shot.remainder = client->GetRecvBuffer();
173 : 0 : }
174 : :
175 : : // The same stream arrives in arbitrary pieces. Once the provider runs dry
176 : : // this becomes one byte per cycle, the maximally fragmented case.
177 [ + - ]: 604 : RunResult sliced;
178 : 604 : {
179 [ + - ]: 604 : auto client{std::make_shared<FuzzClient>()};
180 [ - + ]: 604 : std::string_view remaining{input};
181 [ + + ]: 361261 : while (!remaining.empty()) {
182 : 360657 : const size_t n{provider.ConsumeIntegralInRange<size_t>(1, remaining.size())};
183 [ + - ]: 360657 : client->Receive(remaining.substr(0, n));
184 [ + - ]: 360657 : remaining = remaining.substr(n);
185 [ + - ]: 360657 : Drain(client, sliced);
186 : : }
187 [ + - + - ]: 1208 : sliced.remainder = client->GetRecvBuffer();
188 : 0 : }
189 : :
190 [ - + ]: 604 : assert(one_shot.requests == sliced.requests);
191 [ - + ]: 604 : assert(one_shot.errored == sliced.errored);
192 : : // The whole receive buffer is discarded on a parse error, so in the sliced case
193 : : // what is left over depends on how much had arrived when the error fired.
194 [ + + - + ]: 604 : if (!one_shot.errored) assert(one_shot.remainder == sliced.remainder);
195 : 1208 : }
196 : :
197 : : } // namespace
198 : :
199 [ + - ]: 1080 : FUZZ_TARGET(http_request)
200 : : {
201 : 604 : FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
202 : : // MAX_HEADERS_SIZE is 8192: leave room for a headers section that can
203 : : // reach the limit, plus a body.
204 : 604 : const std::string http_buffer{fuzzed_data_provider.ConsumeRandomLengthString(2 * MAX_HEADERS_SIZE)};
205 : :
206 [ + - ]: 604 : SingleShotParse(http_buffer, fuzzed_data_provider);
207 [ + - ]: 604 : CheckSegmentationIndependence(http_buffer, fuzzed_data_provider);
208 : 604 : }
|