Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
simple_absolute.cc
Go to the documentation of this file.
1#include <fuzzer/FuzzedDataProvider.h>
2
3#include <cstdio>
4#include <string>
5#include <string_view>
6
7#include "ada.cpp"
8#include "ada.h"
9
10static constexpr const char* kSchemes[] = {
11 "http://", "https://", "HTTP://", "Https://",
12 "http:", "https:", "htTP://", "HTTPS://"};
13
14static constexpr const char* kHosts[] = {
15 "example.com",
16 "www.example.com",
17 "WWW.Example.COM",
18 "a",
19 "a.b.c",
20 "maps.google.com",
21 "192.168.0.1",
22 "0x7f.1",
23 "127.1",
24 "0",
25 "3232235777",
26 "xn--nxasmq6b.com",
27 "xn--a",
28 "XN--A",
29 "user@example.com",
30 "user:pass@example.com",
31 "example.com:8080",
32 "example.com:0",
33 "example.com:65535",
34 "",
35};
36
37static constexpr const char* kPaths[] = {
38 "",
39 "/",
40 "/path",
41 "/path/file.js",
42 "/a/./b/../c",
43 "/foo/%2e",
44 "/foo/%2e%2e",
45 "/foo/%2E%2E/bar",
46 "/path with space",
47 "/path\twith\ttab",
48 "\\path",
49 "/continue=https%3A%2F%2Fexample.com%2F",
50 "/imghp",
51 "/./",
52 "/../",
53 "/%2e%2e%2f",
54};
55
56static constexpr const char* kQueries[] = {
57 "",
58 "?",
59 "?q=1",
60 "?hl=en&tab=wi",
61 "?continue=https%3A%2F%2Fexample.com%2F",
62 "?a=1&b=2&c=3",
63};
64
65static constexpr const char* kFrags[] = {
66 "", "#", "#frag", "#x%20y", "#/",
67};
68
69template <size_t N>
70static const char* pick(FuzzedDataProvider& fdp, const char* const (&arr)[N]) {
71 return arr[fdp.ConsumeIntegralInRange<size_t>(0, N - 1)];
72}
73
74static std::string make_candidate(FuzzedDataProvider& fdp) {
75 std::string out;
76 out += pick(fdp, kSchemes);
77 out += pick(fdp, kHosts);
78 out += pick(fdp, kPaths);
79 out += pick(fdp, kQueries);
80 out += pick(fdp, kFrags);
81
82 if (fdp.ConsumeBool() && !out.empty()) {
83 std::string mid = fdp.ConsumeRandomLengthString(24);
84 size_t pos = fdp.ConsumeIntegralInRange<size_t>(0, out.size());
85 out.insert(pos, mid);
86 }
87 if (fdp.ConsumeBool() && !out.empty()) {
88 size_t i = fdp.ConsumeIntegralInRange<size_t>(0, out.size() - 1);
89 out[i] = static_cast<char>(fdp.ConsumeIntegral<uint8_t>());
90 }
91 return out;
92}
93
94static void check_href_size(const ada::url& u, std::string_view input) {
95 if (u.get_href_size() != u.get_href().size()) {
96 printf(
97 "get_href_size mismatch (url)\n"
98 " input: %.*s\n"
99 " size: %zu href.size: %zu\n"
100 " href: %s\n",
101 static_cast<int>(input.size()), input.data(), u.get_href_size(),
102 u.get_href().size(), u.get_href().c_str());
103 abort();
104 }
105}
106
108 std::string_view input) {
109 if (u.get_href_size() != u.get_href().size()) {
110 printf(
111 "get_href_size mismatch (aggregator)\n"
112 " input: %.*s\n"
113 " size: %zu href.size: %zu\n"
114 " href: %s\n",
115 static_cast<int>(input.size()), input.data(), u.get_href_size(),
116 u.get_href().size(), std::string(u.get_href()).c_str());
117 abort();
118 }
119}
120
121template <class Result>
122static void check_reparse_idempotent(const Result& parsed,
123 std::string_view input) {
124 const std::string href = std::string(parsed.get_href());
125 auto again = ada::parse<Result>(href);
126 if (!again) {
127 printf(
128 "re-parse of href failed\n"
129 " input: %.*s\n"
130 " href: %s\n",
131 static_cast<int>(input.size()), input.data(), href.c_str());
132 abort();
133 }
134 if (std::string(again->get_href()) != href) {
135 printf(
136 "href not idempotent\n"
137 " input: %.*s\n"
138 " href1: %s\n"
139 " href2: %s\n",
140 static_cast<int>(input.size()), input.data(), href.c_str(),
141 std::string(again->get_href()).c_str());
142 abort();
143 }
144}
145
146static void check_components_agree(const ada::url& u,
147 const ada::url_aggregator& a,
148 std::string_view input) {
149 if (u.get_protocol() != a.get_protocol() ||
150 u.get_href() != std::string(a.get_href()) ||
151 std::string(u.get_hostname()) != std::string(a.get_hostname()) ||
152 std::string(u.get_pathname()) != std::string(a.get_pathname()) ||
153 std::string(u.get_search()) != std::string(a.get_search()) ||
154 std::string(u.get_hash()) != std::string(a.get_hash()) ||
155 std::string(u.get_port()) != std::string(a.get_port()) ||
156 u.get_username() != std::string(a.get_username()) ||
157 u.get_password() != std::string(a.get_password()) ||
158 std::string(u.get_host()) != std::string(a.get_host())) {
159 printf(
160 "url vs aggregator component mismatch\n"
161 " input: %.*s\n"
162 " url href: %s\n"
163 " agg href: %s\n",
164 static_cast<int>(input.size()), input.data(), u.get_href().c_str(),
165 std::string(a.get_href()).c_str());
166 fflush(stdout);
167 abort();
168 }
169}
170
171static void fuzz_one_input(std::string_view input) {
172 auto url = ada::parse<ada::url>(input);
173 auto agg = ada::parse<ada::url_aggregator>(input);
174
175 if (url.has_value() != agg.has_value()) {
176 printf(
177 "parse agreement failure\n"
178 " input: %.*s\n"
179 " url: %d aggregator: %d\n",
180 static_cast<int>(input.size()), input.data(), url.has_value(),
181 agg.has_value());
182 abort();
183 }
184
185 if (!url) {
186 return;
187 }
188
189 check_components_agree(*url, *agg, input);
190 check_href_size(*url, input);
191 check_href_size(*agg, input);
192 check_reparse_idempotent(*url, input);
193 check_reparse_idempotent(*agg, input);
194
195 const std::string href = url->get_href();
196 url->set_href(href);
197 agg->set_href(href);
198 if (url->get_href() != std::string(agg->get_href())) {
199 printf(
200 "set_href agreement failure\n"
201 " href: %s\n",
202 href.c_str());
203 abort();
204 }
205 check_href_size(*url, href);
206 check_href_size(*agg, href);
207
208 volatile bool valid = agg->validate();
209 (void)valid;
210}
211
212extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
213 FuzzedDataProvider fdp(data, size);
214
216
217 if (fdp.remaining_bytes() > 0) {
218 std::string raw = fdp.ConsumeRemainingBytesAsString();
219 if (raw.size() > 512) {
220 raw.resize(512);
221 }
222 if (!raw.empty() && (static_cast<unsigned char>(raw[0]) & 1u)) {
223 raw = std::string("https://") + raw;
224 } else if (!raw.empty()) {
225 raw = std::string("http://") + raw;
226 }
227 fuzz_one_input(raw);
228 }
229
230 static constexpr const char* kAnchors[] = {
231 "https://example.com",
232 "https://example.com/",
233 "https://example.com?q=1",
234 "https://example.com#frag",
235 "https://example.com/?q=1#frag",
236 "http://www.example.com/path/file.js",
237 "http://WWW.Example.COM/file.js",
238 "https://www.google.com/imghp?hl=en&tab=wi",
239 "http://192.168.0.1/x",
240 "http://0x7f.1/",
241 "https://user:pass@example.com/x",
242 "https://example.com:8080/x",
243 "https://example.com/a/./b/../c",
244 "https://example.com/foo/%2e%2e",
245 "https://xn--nxasmq6b.com/",
246 "https://xn--a/",
247 "http://foo.0xffffffff/",
248 "http://example.0x/",
249 "https://example.com:0080/x",
250 "http://@19%2E68.1.10.",
251 "http://19%2E68.1.10.",
252 "http://0xffffffff.",
253 };
254 for (const char* seed : kAnchors) {
255 fuzz_one_input(seed);
256 }
257
258 return 0;
259}
Main header for the Ada URL parser library.
size_t N
Definition to_ascii.cpp:15
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
static std::string make_candidate(FuzzedDataProvider &fdp)
static constexpr const char * kPaths[]
static constexpr const char * kFrags[]
static void fuzz_one_input(std::string_view input)
static void check_components_agree(const ada::url &u, const ada::url_aggregator &a, std::string_view input)
static void check_href_size(const ada::url &u, std::string_view input)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
static constexpr const char * kSchemes[]
static constexpr const char * kHosts[]
static const char * pick(FuzzedDataProvider &fdp, const char *const (&arr)[N])
static void check_reparse_idempotent(const Result &parsed, std::string_view input)
static constexpr const char * kQueries[]
Memory-efficient URL representation using a single buffer.
constexpr size_t get_href_size() const noexcept
std::string_view get_search() const ada_lifetime_bound
std::string_view get_hash() const ada_lifetime_bound
std::string_view get_password() const ada_lifetime_bound
std::string_view get_host() const ada_lifetime_bound
std::string_view get_username() const ada_lifetime_bound
std::string_view get_port() const ada_lifetime_bound
constexpr std::string_view get_href() const noexcept ada_lifetime_bound
constexpr std::string_view get_pathname() const ada_lifetime_bound
std::string_view get_hostname() const ada_lifetime_bound
std::string_view get_protocol() const ada_lifetime_bound
Represents a parsed URL with individual string components.
Definition url.h:64
std::string get_search() const
Definition url.cpp:591
std::string get_port() const
Definition url.cpp:605
std::string get_protocol() const
Definition url.cpp:565
std::string get_host() const
Definition url.cpp:573
constexpr std::string_view get_pathname() const noexcept
Definition url-inl.h:51
size_t get_href_size() const noexcept
Definition url-inl.h:283
std::string get_hostname() const
Definition url.cpp:587
ada_really_inline std::string get_href() const
Definition url-inl.h:196
const std::string & get_password() const noexcept
Definition url.cpp:601
const std::string & get_username() const noexcept
Definition url.cpp:597
std::string get_hash() const
Definition url.cpp:609