Bitcoin Core 32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
http_request.cpp
Go to the documentation of this file.
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>
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>
14#include <util/strencodings.h>
15
16#include <cstdint>
17#include <memory>
18#include <string>
19#include <string_view>
20#include <vector>
21
23
24using namespace bitcoin_http;
25
26namespace {
27
30{
31 const std::string body{req.ReadBody()};
32 const auto transfer_encoding{req.GetHeader("Transfer-Encoding")};
33 const auto content_length{req.GetHeader("Content-Length")};
34 if (transfer_encoding && ToLower(*transfer_encoding) == "chunked") {
35 assert(body.size() <= MAX_BODY_SIZE);
36 } else if (content_length) {
39 assert(body.size() == *parsed_length);
40 } else {
41 assert(body.empty());
42 }
43}
44
47{
48 using util::LineReader;
49
52 try {
53 if (http_request.Load(reader) != HTTPRequest::State::Complete) return;
54 } catch (const std::runtime_error&) {
55 return;
56 }
57
58 const HTTPRequestMethod request_method = http_request.GetRequestMethod();
60 (void)http_request.GetURI();
61 (void)http_request.GetHeader("Host");
62 std::string header = provider.ConsumeRandomLengthString(16);
63 (void)http_request.GetHeader(header);
64 (void)http_request.WriteHeader(std::string(header), provider.ConsumeRandomLengthString(16));
65 (void)http_request.GetHeader(header);
67}
68
70class FuzzClient : public HTTPRemoteClient
71{
72public:
73 FuzzClient() : HTTPRemoteClient{/*id=*/0, /*addr=*/CService(), /*socket=*/std::make_unique<ZeroSock>()} {}
74 void Receive(std::string_view s) { MutateRecvBuffer().append(s); }
75};
76
78{
79 switch (s) {
80 case HTTPRequest::State::Init: return 0;
81 case HTTPRequest::State::NeedsHeaders: return 1;
82 case HTTPRequest::State::NeedsBody: return 2;
83 case HTTPRequest::State::Complete: return 3;
84 case HTTPRequest::State::Error: return 4;
85 }
86 assert(false);
87}
88
89struct ParsedRequest {
90 HTTPRequestMethod method;
91 std::string uri;
92 std::string host;
93 std::string body;
94 bool operator==(const ParsedRequest&) const = default;
95};
96
97struct RunResult {
98 std::vector<ParsedRequest> requests;
99 bool errored{false};
100 std::string remainder;
101 HTTPRequest::State last_state{HTTPRequest::State::Init};
102};
103
106void Drain(const std::shared_ptr<FuzzClient>& client, RunResult& out)
107{
108 while (true) {
109 // A failed request is inert: further reads consume nothing.
110 if (const HTTPRequest* cur{client->GetRequest()};
111 cur && cur->GetState() == HTTPRequest::State::Error) {
112 const size_t buffered{client->GetRecvBuffer().size()};
114 assert(client->GetRecvBuffer().size() == buffered);
115 out.errored = true;
116 break;
117 }
118
119 std::unique_ptr<HTTPRequest> req{HTTPRemoteClient::TryReadRequest(client)};
120
121 if (!req) {
122 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 assert(cur->GetState() != HTTPRequest::State::Complete);
128 assert(StateRank(cur->GetState()) >= StateRank(out.last_state));
129 out.last_state = cur->GetState();
130 if (cur->GetState() == HTTPRequest::State::Error) out.errored = true;
131 if (const auto chunk_size{cur->GetChunkSize()}) {
132 assert(cur->GetChunkProgress() <= *chunk_size);
133 }
134 assert(cur->ReadBody().size() <= MAX_BODY_SIZE);
135 }
136 break;
137 }
138
139 assert(req->GetState() == HTTPRequest::State::Complete);
141 out.requests.emplace_back(req->GetRequestMethod(), req->GetURI(),
142 req->GetHeader("Host").value_or(""), req->ReadBody());
143
144 // While a request is with a worker, nothing new is parsed or consumed.
145 const size_t buffered{client->GetRecvBuffer().size()};
147 assert(client->GetRecvBuffer().size() == buffered);
148
149 req->WriteReply(HTTP_OK, ""); // clears m_req_busy
150 out.last_state = HTTPRequest::State::Init;
151 }
152}
153
157void 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 FakeNodeClock clock{1610000000s};
162 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 RunResult one_shot;
168 {
169 auto client{std::make_shared<FuzzClient>()};
170 client->Receive(input);
172 one_shot.remainder = client->GetRecvBuffer();
173 }
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 RunResult sliced;
178 {
179 auto client{std::make_shared<FuzzClient>()};
180 std::string_view remaining{input};
181 while (!remaining.empty()) {
182 const size_t n{provider.ConsumeIntegralInRange<size_t>(1, remaining.size())};
183 client->Receive(remaining.substr(0, n));
184 remaining = remaining.substr(n);
186 }
187 sliced.remainder = client->GetRecvBuffer();
188 }
189
190 assert(one_shot.requests == sliced.requests);
191 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 if (!one_shot.errored) assert(one_shot.remainder == sliced.remainder);
195}
196
197} // namespace
198
200{
201 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.
205
208}
#define Assert(val)
Identity function.
Definition check.h:119
A combination of a network address (CNetAddr) and a (TCP) port.
Definition netaddress.h:531
Helper to initialize the global NodeClock, let a duration elapse, and reset it after use in a test.
Definition time.h:54
Helper to initialize the global MockableSteadyClock, let a duration elapse, and reset it after use in...
Definition time.h:29
std::string ConsumeRandomLengthString(size_t max_length)
T ConsumeIntegralInRange(T min, T max)
std::string & MutateRecvBuffer()
Used for tests.
Definition httpserver.h:550
static std::unique_ptr< HTTPRequest > TryReadRequest(const std::shared_ptr< HTTPRemoteClient > &client) EXCLUSIVE_LOCKS_REQUIRED(!client -> m_send_mutex)
Try to read an HTTPRequest from a client's receive buffer.
void Receive() EXCLUSIVE_LOCKS_REQUIRED(!m_sock_mutex)
std::string GetURI() const
Definition httpserver.h:189
std::optional< std::string > GetHeader(std::string_view hdr) const
HTTPRequestMethod GetRequestMethod() const
Definition httpserver.h:191
std::string ReadBody() const
Definition httpserver.h:194
void WriteReply(HTTPStatusCode status, std::span< const std::byte > reply_body={})
State GetState() const
Definition httpserver.h:199
A mocked Sock alternative that succeeds on all operations.
Definition net.h:168
#define FUZZ_TARGET(...)
Definition fuzz.h:35
std::string_view RequestMethodString(HTTPRequestMethod m)
HTTP request method as string - use for logging only.
HTTPRequestMethod
Definition httpserver.h:49
util::LineReader reader
std::unique_ptr< ProxyClient< messages::FooInterface > > client
constexpr uint64_t MAX_BODY_SIZE
Maximum size of an HTTP request body received from a client.
Definition httpserver.h:82
constexpr size_t MAX_HEADERS_SIZE
Maximum size of each headers line in an HTTP request, also the maximum size of all headers total.
Definition httpserver.h:78
bool operator==(const CNetAddr &a, const CNetAddr &b)
@ HTTP_OK
Definition protocol.h:14
Utilities for converting data from/to strings.
FuzzedDataProvider provider
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition time.h:82
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition fees.cpp:46
FakeNodeClock clock