Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern.cc
Go to the documentation of this file.
1#include <fuzzer/FuzzedDataProvider.h>
2
3#include <memory>
4#include <string>
5
6#include "ada.cpp"
7#include "ada.h"
8
9using regex_provider = ada::url_pattern_regex::std_regex_provider;
10
11void exercise_result(auto result) {
12 (void)result.get_protocol();
13 (void)result.get_username();
14 (void)result.get_password();
15 (void)result.get_hostname();
16 (void)result.get_port();
17 (void)result.get_pathname();
18 (void)result.get_search();
19 (void)result.get_hash();
20 (void)result.ignore_case();
21 (void)result.has_regexp_groups();
22}
23
24// Shared helper: walk every field of a url_pattern_result.
25static void exercise_match_result(const ada::url_pattern_result& match) {
26 volatile size_t len = 0;
27 auto exercise_component = [&len](const ada::url_pattern_component_result& c) {
28 len += c.input.size();
29 for (const auto& [k, v] : c.groups) {
30 len += k.size();
31 if (v.has_value()) len += v->size();
32 }
33 };
34 exercise_component(match.protocol);
35 exercise_component(match.username);
36 exercise_component(match.password);
37 exercise_component(match.hostname);
38 exercise_component(match.port);
39 exercise_component(match.pathname);
40 exercise_component(match.search);
41 exercise_component(match.hash);
42 // Exercise the 'inputs' vector (each element is a url_pattern_input
43 // variant holding either a string_view or url_pattern_init).
44 for (const auto& inp : match.inputs) {
45 if (std::holds_alternative<std::string_view>(inp)) {
46 len += std::get<std::string_view>(inp).size();
47 }
48 }
49 (void)len;
50}
51
52// Exercise exec() and test() on a parsed url_pattern with an ASCII input.
53// We restrict inputs to ASCII to avoid catastrophic regex backtracking.
54static void exercise_exec_and_test(ada::url_pattern<regex_provider>& pattern,
55 const std::string& test_input,
56 const std::string& test_base) {
57 std::string_view test_view(test_input.data(), test_input.size());
58
59 // exec() and test() must agree: exec finds a match iff test returns true.
60 // Both operate on the same input so their answers must be consistent.
61 auto exec_result = pattern.exec(test_view, nullptr);
62 auto test_result = pattern.test(test_view, nullptr);
63
64 bool exec_matched = exec_result && exec_result->has_value();
65 bool test_matched = test_result && *test_result;
66
67 if (exec_matched != test_matched) {
68 printf(
69 "exec/test inconsistency on input '%s': exec_matched=%d "
70 "test_matched=%d\n",
71 test_input.c_str(), exec_matched, test_matched);
72 abort();
73 }
74
75 if (exec_result && exec_result->has_value()) {
76 exercise_match_result(**exec_result);
77 }
78
79 // test() with base URL
80 if (!test_base.empty()) {
81 std::string_view base_view(test_base.data(), test_base.size());
82 auto test_result_with_base = pattern.test(test_view, &base_view);
83 auto exec_with_base = pattern.exec(test_view, &base_view);
84
85 bool exec_base_matched = exec_with_base && exec_with_base->has_value();
86 bool test_base_matched = test_result_with_base && *test_result_with_base;
87
88 if (exec_base_matched != test_base_matched) {
89 printf(
90 "exec/test inconsistency with base on input '%s': "
91 "exec_matched=%d test_matched=%d\n",
92 test_input.c_str(), exec_base_matched, test_base_matched);
93 abort();
94 }
95
96 if (exec_with_base && exec_with_base->has_value()) {
97 exercise_match_result(**exec_with_base);
98 }
99 }
100
101 // test() with url_pattern_init input (sets only the pathname component)
102 ada::url_pattern_init init_input{};
103 init_input.pathname = test_input;
104 auto test_with_init = pattern.test(init_input, nullptr);
105 auto exec_with_init = pattern.exec(init_input, nullptr);
106 // exec and test must agree on the init-based input too.
107 if ((test_with_init && *test_with_init) !=
108 (exec_with_init && exec_with_init->has_value())) {
109 printf("exec/test inconsistency on url_pattern_init input\n");
110 abort();
111 }
112 if (exec_with_init && exec_with_init->has_value()) {
113 exercise_match_result(**exec_with_init);
114 }
115
116 // test_components() — tests each URL component individually
117 {
118 std::string_view sv(test_input.data(), test_input.size());
119 auto parsed = ada::parse<ada::url_aggregator>(sv);
120 if (parsed) {
121 volatile bool tc = pattern.test_components(
122 std::string(parsed->get_protocol()),
123 std::string(parsed->get_username()),
124 std::string(parsed->get_password()),
125 std::string(parsed->get_hostname()), std::string(parsed->get_port()),
126 std::string(parsed->get_pathname()),
127 std::string(parsed->get_search()), std::string(parsed->get_hash()));
128 (void)tc;
129 }
130 }
131
132 // match() — the internal method underlying exec(); must not crash.
133 auto match_result = pattern.match(test_view, nullptr);
134 (void)match_result;
135}
136
137extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
138 auto to_ascii = [](const std::string& source) -> std::string {
139 std::string result;
140 result.reserve(source.size());
141 for (char c : source) {
142 result.push_back(static_cast<unsigned char>(c) % 128);
143 }
144 return result;
145 };
146 FuzzedDataProvider fdp(data, size);
147 // We do not want to trigger arbitrary regex matching.
148 std::string source_1 = "/" + to_ascii(fdp.ConsumeRandomLengthString(50)) +
149 "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
150 std::string base_source_1 = "/" +
151 to_ascii(fdp.ConsumeRandomLengthString(50)) +
152 "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
153
154 std::string source_2 = "https://ada-url.com/*";
155 std::string base_source_2 = "https://ada-url.com";
156
157 // Additional test input for exec/test calls (also ASCII-only)
158 std::string test_input = "https://" +
159 to_ascii(fdp.ConsumeRandomLengthString(30)) + "/" +
160 to_ascii(fdp.ConsumeRandomLengthString(20));
161 std::string test_base = "https://ada-url.com";
162
163 std::array<std::pair<std::string, std::string>, 2> sources = {{
164 {source_1, base_source_1},
165 {source_2, base_source_2},
166 }};
167
168 for (const auto& [source, base_source] : sources) {
169 // Without base or options
170 auto result =
171 ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
172 if (result) {
173 exercise_result(*result);
174 exercise_exec_and_test(*result, test_input, test_base);
175 }
176
177 // Testing with base_url
178 std::string_view base_source_view(base_source.data(), base_source.length());
179 auto result_with_base = ada::parse_url_pattern<regex_provider>(
180 source, &base_source_view, nullptr);
181 if (result_with_base) {
182 exercise_result(*result_with_base);
183 exercise_exec_and_test(*result_with_base, test_input, test_base);
184 }
185
186 // Testing with base_url and options
187 ada::url_pattern_options options{.ignore_case = fdp.ConsumeBool()};
188 auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
189 source, &base_source_view, &options);
190 if (result_with_base_and_options) {
191 exercise_result(*result_with_base_and_options);
192 exercise_exec_and_test(*result_with_base_and_options, test_input,
193 test_base);
194 }
195
196 // Testing with url_pattern_init and base url.
197 int field_index = fdp.ConsumeIntegralInRange(0, 7);
198 std::string random_value = to_ascii(fdp.ConsumeRandomLengthString(50));
199 ada::url_pattern_init init{};
200 switch (field_index) {
201 case 0:
202 init.protocol = random_value;
203 break;
204 case 1:
205 init.username = random_value;
206 break;
207 case 2:
208 init.password = random_value;
209 break;
210 case 3:
211 init.hostname = random_value;
212 break;
213 case 4:
214 init.port = random_value;
215 break;
216 case 5:
217 init.pathname = random_value;
218 break;
219 case 6:
220 init.search = random_value;
221 break;
222 case 7:
223 init.hash = random_value;
224 break;
225 }
226 auto result_with_init = ada::parse_url_pattern<regex_provider>(
227 init, &base_source_view, nullptr);
228 if (result_with_init) {
229 exercise_result(*result_with_init);
230 exercise_exec_and_test(*result_with_init, test_input, test_base);
231 }
232
233 // Testing url_pattern_init with ALL fields populated simultaneously
234 ada::url_pattern_init init_all{};
235 init_all.protocol = to_ascii(fdp.ConsumeRandomLengthString(10));
236 init_all.username = to_ascii(fdp.ConsumeRandomLengthString(10));
237 init_all.password = to_ascii(fdp.ConsumeRandomLengthString(10));
238 init_all.hostname = to_ascii(fdp.ConsumeRandomLengthString(20));
239 init_all.port = to_ascii(fdp.ConsumeRandomLengthString(5));
240 init_all.pathname = "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
241 init_all.search = to_ascii(fdp.ConsumeRandomLengthString(10));
242 init_all.hash = to_ascii(fdp.ConsumeRandomLengthString(10));
243 auto result_with_init_all =
244 ada::parse_url_pattern<regex_provider>(init_all, nullptr, nullptr);
245 if (result_with_init_all) {
246 exercise_result(*result_with_init_all);
247 exercise_exec_and_test(*result_with_init_all, test_input, test_base);
248 }
249
250 // Testing url_pattern_init with the base_url field set.
251 //
252 // url_pattern_init::base_url is a completely separate code path from the
253 // base_url *parameter* of parse_url_pattern. When base_url is embedded
254 // inside the init struct the spec processes it differently. This field
255 // was previously never exercised by any fuzzer.
256 {
257 ada::url_pattern_init init_base_url{};
258 init_base_url.pathname =
259 "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
260 init_base_url.base_url = "https://example.com";
261 auto result_base_in_init = ada::parse_url_pattern<regex_provider>(
262 init_base_url, nullptr, nullptr);
263 if (result_base_in_init) {
264 exercise_result(*result_base_in_init);
265 exercise_exec_and_test(*result_base_in_init, test_input, test_base);
266 }
267
268 // Also fuzz the base_url field itself.
269 ada::url_pattern_init init_fuzz_base{};
270 init_fuzz_base.pathname =
271 "/" + to_ascii(fdp.ConsumeRandomLengthString(15));
272 init_fuzz_base.base_url =
273 "https://" + to_ascii(fdp.ConsumeRandomLengthString(20));
274 auto result_fuzz_base = ada::parse_url_pattern<regex_provider>(
275 init_fuzz_base, nullptr, nullptr);
276 if (result_fuzz_base) {
277 exercise_result(*result_fuzz_base);
278 exercise_exec_and_test(*result_fuzz_base, test_input, test_base);
279 }
280 }
281
282 // Testing url_pattern_init with a random subset (2–4) of fields set.
283 //
284 // The single-field case (switch above) and the all-fields case are covered
285 // above. Here we pick a random bitmask of fields so the parser sees every
286 // combination of present/absent components.
287 {
288 uint8_t field_mask = fdp.ConsumeIntegral<uint8_t>();
289 ada::url_pattern_init init_subset{};
290 if (field_mask & 0x01)
291 init_subset.protocol = to_ascii(fdp.ConsumeRandomLengthString(8));
292 if (field_mask & 0x02)
293 init_subset.hostname = to_ascii(fdp.ConsumeRandomLengthString(20));
294 if (field_mask & 0x04)
295 init_subset.port = to_ascii(fdp.ConsumeRandomLengthString(5));
296 if (field_mask & 0x08)
297 init_subset.pathname =
298 "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
299 if (field_mask & 0x10)
300 init_subset.search = to_ascii(fdp.ConsumeRandomLengthString(10));
301 if (field_mask & 0x20)
302 init_subset.hash = to_ascii(fdp.ConsumeRandomLengthString(10));
303 if (field_mask & 0x40)
304 init_subset.username = to_ascii(fdp.ConsumeRandomLengthString(10));
305 if (field_mask & 0x80)
306 init_subset.password = to_ascii(fdp.ConsumeRandomLengthString(10));
307 auto result_subset =
308 ada::parse_url_pattern<regex_provider>(init_subset, nullptr, nullptr);
309 if (result_subset) {
310 exercise_result(*result_subset);
311 exercise_exec_and_test(*result_subset, test_input, test_base);
312 }
313 }
314 }
315
316 return 0;
317}
Main header for the Ada URL parser library.
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
void exercise_result(auto result)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
static void exercise_exec_and_test(ada::url_pattern< regex_provider > &pattern, const std::string &test_input, const std::string &test_base)
static void exercise_match_result(const ada::url_pattern_result &match)
ada::url_pattern_regex::std_regex_provider regex_provider
Definition url_pattern.cc:9