Bitcoin Core
32.1.0
P2P Digital Currency
Toggle main menu visibility
Loading...
Searching...
No Matches
src
test
fuzz
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
>
7
#include <
test/fuzz/FuzzedDataProvider.h
>
8
#include <
test/fuzz/fuzz.h
>
9
#include <
test/fuzz/util.h
>
10
#include <
util/signalinterrupt.h
>
11
#include <
util/strencodings.h
>
12
13
#include <cassert>
14
#include <cstdint>
15
#include <string>
16
#include <vector>
17
18
19
std::string_view
RequestMethodString
(
HTTPRequestMethod
m);
20
21
FUZZ_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
}
FuzzedDataProvider.h
FuzzedDataProvider
Definition
FuzzedDataProvider.h:32
FuzzedDataProvider::ConsumeRandomLengthString
std::string ConsumeRandomLengthString(size_t max_length)
Definition
FuzzedDataProvider.h:153
HTTPRequest
Definition
httpserver.h:155
HTTPRequest::WriteHeader
void WriteHeader(std::string &&hdr, std::string &&value)
Definition
httpserver.cpp:703
HTTPRequest::GetURI
std::string GetURI() const
Definition
httpserver.h:186
HTTPRequest::LoadHeaders
bool LoadHeaders(util::LineReader &reader)
Definition
httpserver.cpp:426
HTTPRequest::LoadControlData
bool LoadControlData(util::LineReader &reader)
Methods that attempt to parse HTTP request fields line-by-line from a receive buffer.
Definition
httpserver.cpp:378
HTTPRequest::GetHeader
std::optional< std::string > GetHeader(std::string_view hdr) const
Definition
httpserver.cpp:698
HTTPRequest::GetRequestMethod
HTTPRequestMethod GetRequestMethod() const
Definition
httpserver.h:188
HTTPRequest::ReadBody
std::string ReadBody() const
Definition
httpserver.h:191
HTTPRequest::LoadBody
bool LoadBody(util::LineReader &reader)
Definition
httpserver.cpp:431
LineReader
Definition
string.h:270
util::LineReader
Definition
string.h:270
fuzz.h
FUZZ_TARGET
#define FUZZ_TARGET(...)
Definition
fuzz.h:35
RequestMethodString
std::string_view RequestMethodString(HTTPRequestMethod m)
HTTP request method as string - use for logging only.
Definition
httpserver.cpp:117
httpserver.h
HTTPRequestMethod
HTTPRequestMethod
Definition
httpserver.h:49
reader
LineReader reader("GET / HTTP/1.0\r\nHost: ", MAX_HEADERS_SIZE)
bitcoin_http
Definition
httpserver.h:70
bitcoin_http::MAX_BODY_SIZE
constexpr uint64_t MAX_BODY_SIZE
Definition
httpserver.h:82
bitcoin_http::MAX_HEADERS_SIZE
constexpr size_t MAX_HEADERS_SIZE
Definition
httpserver.h:78
netaddress.h
signalinterrupt.h
strencodings.h
ToIntegral
std::optional< T > ToIntegral(std::string_view str, size_t base=10)
Convert string to integral type T.
Definition
strencodings.h:180
util.h
ToLower
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
Definition
strencodings.cpp:362
assert
assert(!tx.IsCoinBase())
Generated on
for Bitcoin Core by
1.18.0