Bitcoin Core 32.99.0
P2P Digital Currency
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>
11#include <util/strencodings.h>
12
13#include <cassert>
14#include <cstdint>
15#include <string>
16#include <vector>
17
18
19std::string_view RequestMethodString(HTTPRequestMethod m);
20
21FUZZ_TARGET(http_request)
22{
23 using util::LineReader;
24 using namespace bitcoin_http;
25
26 FuzzedDataProvider fuzzed_data_provider{buffer.data(), buffer.size()};
27 const std::string http_buffer{fuzzed_data_provider.ConsumeRandomLengthString(4096)};
28
29 HTTPRequest http_request;
30 LineReader reader(http_buffer, MAX_HEADERS_SIZE);
31 try {
32 if (!http_request.LoadControlData(reader)) return;
33 if (!http_request.LoadHeaders(reader)) return;
34 if (!http_request.LoadBody(reader)) return;
35 } catch (const std::runtime_error&) {
36 return;
37 }
38
39 const HTTPRequestMethod request_method = http_request.GetRequestMethod();
40 (void)RequestMethodString(request_method);
41 (void)http_request.GetURI();
42 (void)http_request.GetHeader("Host");
43 std::string header = fuzzed_data_provider.ConsumeRandomLengthString(16);
44 (void)http_request.GetHeader(header);
45 (void)http_request.WriteHeader(std::string(header), fuzzed_data_provider.ConsumeRandomLengthString(16));
46 (void)http_request.GetHeader(header);
47 // Reaching here means LoadControlData/LoadHeaders/LoadBody all succeeded, so the
48 // parsed body must be consistent with the message framing. Before libevent was
49 // replaced with HTTPRequest (#35182), ReadBody() always returned an
50 // empty string here; LoadBody now populates the body per RFC 9112 framing, so mirror
51 // its branch logic to assert the body matches the framing that produced it.
52 const std::string body = http_request.ReadBody();
53 const auto transfer_encoding = http_request.GetHeader("Transfer-Encoding");
54 const auto content_length = http_request.GetHeader("Content-Length");
55 if (transfer_encoding && ToLower(*transfer_encoding) == "chunked") {
56 // A chunked body is the concatenation of the decoded chunks, bounded by MAX_BODY_SIZE.
57 assert(body.size() <= MAX_BODY_SIZE);
58 } else if (content_length) {
59 // A Content-Length body is exactly that many bytes.
60 const auto parsed_length{ToIntegral<uint64_t>(*content_length)};
61 assert(parsed_length);
62 assert(body.size() == *parsed_length);
63 } else {
64 // Absent both framing headers there is no body.
65 assert(body.empty());
66 }
67}
std::string ConsumeRandomLengthString(size_t max_length)
void WriteHeader(std::string &&hdr, std::string &&value)
Definition: httpserver.cpp:703
std::string GetURI() const
Definition: httpserver.h:186
bool LoadHeaders(util::LineReader &reader)
Definition: httpserver.cpp:426
bool LoadControlData(util::LineReader &reader)
Methods that attempt to parse HTTP request fields line-by-line from a receive buffer.
Definition: httpserver.cpp:378
std::optional< std::string > GetHeader(std::string_view hdr) const
Definition: httpserver.cpp:698
HTTPRequestMethod GetRequestMethod() const
Definition: httpserver.h:188
std::string ReadBody() const
Definition: httpserver.h:191
bool LoadBody(util::LineReader &reader)
Definition: httpserver.cpp:431
std::string_view RequestMethodString(HTTPRequestMethod m)
HTTP request method as string - use for logging only.
Definition: httpserver.cpp:117
FUZZ_TARGET(http_request)
HTTPRequestMethod
Definition: httpserver.h:49
util::LineReader reader
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
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
assert(!tx.IsCoinBase())
FuzzedDataProvider & fuzzed_data_provider
Definition: fees.cpp:45