Coverage Report

Created: 2026-12-01 06:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ada-url/fuzz/url_pattern.cc
Line
Count
Source
1
#include <fuzzer/FuzzedDataProvider.h>
2
3
#include <memory>
4
#include <string>
5
6
#include "ada.cpp"
7
#include "ada.h"
8
9
using regex_provider = ada::url_pattern_regex::std_regex_provider;
10
11
126k
void exercise_result(auto result) {
12
126k
  (void)result.get_protocol();
13
126k
  (void)result.get_username();
14
126k
  (void)result.get_password();
15
126k
  (void)result.get_hostname();
16
126k
  (void)result.get_port();
17
126k
  (void)result.get_pathname();
18
126k
  (void)result.get_search();
19
126k
  (void)result.get_hash();
20
126k
  (void)result.ignore_case();
21
126k
  (void)result.has_regexp_groups();
22
126k
}
23
24
// Shared helper: walk every field of a url_pattern_result.
25
61.6k
static void exercise_match_result(const ada::url_pattern_result& match) {
26
61.6k
  volatile size_t len = 0;
27
492k
  auto exercise_component = [&len](const ada::url_pattern_component_result& c) {
28
492k
    len += c.input.size();
29
492k
    for (const auto& [k, v] : c.groups) {
30
458k
      len += k.size();
31
458k
      if (v.has_value()) len += v->size();
32
458k
    }
33
492k
  };
34
61.6k
  exercise_component(match.protocol);
35
61.6k
  exercise_component(match.username);
36
61.6k
  exercise_component(match.password);
37
61.6k
  exercise_component(match.hostname);
38
61.6k
  exercise_component(match.port);
39
61.6k
  exercise_component(match.pathname);
40
61.6k
  exercise_component(match.search);
41
61.6k
  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). Read every
44
  // byte: the views point into storage owned by the result, and a dangling
45
  // view is only visible to the sanitizers when it is dereferenced.
46
87.3k
  for (const auto& inp : match.inputs) {
47
87.3k
    if (std::holds_alternative<std::string_view>(inp)) {
48
840k
      for (char c : std::get<std::string_view>(inp)) {
49
840k
        len += static_cast<unsigned char>(c);
50
840k
      }
51
59.9k
    }
52
87.3k
  }
53
61.6k
  (void)len;
54
61.6k
}
55
56
// exec() and match() copy their string arguments into the result, so
57
// inputs[] must read back exactly what the caller passed, including after the
58
// result is copied or moved.
59
static void check_string_inputs(const ada::url_pattern_result& match,
60
                                std::string_view input,
61
171k
                                const std::string_view* base) {
62
171k
  const size_t expected_size = base != nullptr ? 2 : 1;
63
299k
  auto is_view_of = [&match](size_t i, std::string_view expected) {
64
299k
    const auto* view = std::get_if<std::string_view>(&match.inputs[i]);
65
299k
    return view != nullptr && *view == expected;
66
299k
  };
67
171k
  if (match.inputs.size() != expected_size || !is_view_of(0, input) ||
68
171k
      (base != nullptr && !is_view_of(1, *base))) {
69
0
    printf("exec/match result inputs do not match the arguments\n");
70
0
    abort();
71
0
  }
72
171k
}
73
74
static void check_string_inputs_survive_copies(
75
    const ada::url_pattern_result& match, std::string_view input,
76
34.2k
    const std::string_view* base) {
77
34.2k
  check_string_inputs(match, input, base);
78
34.2k
  ada::url_pattern_result copied = match;
79
34.2k
  check_string_inputs(copied, input, base);
80
34.2k
  ada::url_pattern_result moved = std::move(copied);
81
34.2k
  check_string_inputs(moved, input, base);
82
34.2k
  ada::url_pattern_result assigned;
83
34.2k
  assigned = moved;
84
34.2k
  check_string_inputs(assigned, input, base);
85
34.2k
}
86
87
// Exercise exec() and test() on a parsed url_pattern with an ASCII input.
88
// We restrict inputs to ASCII to avoid catastrophic regex backtracking.
89
static void exercise_exec_and_test(ada::url_pattern<regex_provider>& pattern,
90
                                   const std::string& test_input,
91
126k
                                   const std::string& test_base) {
92
126k
  std::string_view test_view(test_input.data(), test_input.size());
93
94
  // exec() and test() must agree: exec finds a match iff test returns true.
95
  // Both operate on the same input so their answers must be consistent.
96
126k
  auto exec_result = pattern.exec(test_view, nullptr);
97
126k
  auto test_result = pattern.test(test_view, nullptr);
98
99
126k
  bool exec_matched = exec_result && exec_result->has_value();
100
126k
  bool test_matched = test_result && *test_result;
101
102
126k
  if (exec_matched != test_matched) {
103
0
    printf(
104
0
        "exec/test inconsistency on input '%s': exec_matched=%d "
105
0
        "test_matched=%d\n",
106
0
        test_input.c_str(), exec_matched, test_matched);
107
0
    abort();
108
0
  }
109
110
126k
  if (exec_result && exec_result->has_value()) {
111
8.46k
    exercise_match_result(**exec_result);
112
8.46k
    check_string_inputs_survive_copies(**exec_result, test_view, nullptr);
113
8.46k
  }
114
115
  // test() with base URL
116
126k
  if (!test_base.empty()) {
117
126k
    std::string_view base_view(test_base.data(), test_base.size());
118
126k
    auto test_result_with_base = pattern.test(test_view, &base_view);
119
126k
    auto exec_with_base = pattern.exec(test_view, &base_view);
120
121
126k
    bool exec_base_matched = exec_with_base && exec_with_base->has_value();
122
126k
    bool test_base_matched = test_result_with_base && *test_result_with_base;
123
124
126k
    if (exec_base_matched != test_base_matched) {
125
0
      printf(
126
0
          "exec/test inconsistency with base on input '%s': "
127
0
          "exec_matched=%d test_matched=%d\n",
128
0
          test_input.c_str(), exec_base_matched, test_base_matched);
129
0
      abort();
130
0
    }
131
132
126k
    if (exec_with_base && exec_with_base->has_value()) {
133
25.7k
      exercise_match_result(**exec_with_base);
134
25.7k
      check_string_inputs_survive_copies(**exec_with_base, test_view,
135
25.7k
                                         &base_view);
136
25.7k
    }
137
138
126k
    auto match_with_base = pattern.match(test_view, &base_view);
139
126k
    if (match_with_base && match_with_base->has_value()) {
140
25.7k
      check_string_inputs(**match_with_base, test_view, &base_view);
141
25.7k
    }
142
126k
  }
143
144
  // test() with url_pattern_init input (sets only the pathname component)
145
126k
  ada::url_pattern_init init_input{};
146
126k
  init_input.pathname = test_input;
147
126k
  auto test_with_init = pattern.test(init_input, nullptr);
148
126k
  auto exec_with_init = pattern.exec(init_input, nullptr);
149
  // exec and test must agree on the init-based input too.
150
126k
  if ((test_with_init && *test_with_init) !=
151
126k
      (exec_with_init && exec_with_init->has_value())) {
152
0
    printf("exec/test inconsistency on url_pattern_init input\n");
153
0
    abort();
154
0
  }
155
126k
  if (exec_with_init && exec_with_init->has_value()) {
156
27.3k
    exercise_match_result(**exec_with_init);
157
27.3k
  }
158
159
  // test_components() — tests each URL component individually
160
126k
  {
161
126k
    std::string_view sv(test_input.data(), test_input.size());
162
126k
    auto parsed = ada::parse<ada::url_aggregator>(sv);
163
126k
    if (parsed) {
164
36.5k
      volatile bool tc = pattern.test_components(
165
36.5k
          std::string(parsed->get_protocol()),
166
36.5k
          std::string(parsed->get_username()),
167
36.5k
          std::string(parsed->get_password()),
168
36.5k
          std::string(parsed->get_hostname()), std::string(parsed->get_port()),
169
36.5k
          std::string(parsed->get_pathname()),
170
36.5k
          std::string(parsed->get_search()), std::string(parsed->get_hash()));
171
36.5k
      (void)tc;
172
36.5k
    }
173
126k
  }
174
175
  // match() — the internal method underlying exec(); must not crash.
176
126k
  auto match_result = pattern.match(test_view, nullptr);
177
126k
  if (match_result && match_result->has_value()) {
178
8.46k
    check_string_inputs(**match_result, test_view, nullptr);
179
8.46k
  }
180
126k
}
181
182
14.7k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
183
447k
  auto to_ascii = [](const std::string& source) -> std::string {
184
447k
    std::string result;
185
447k
    result.reserve(source.size());
186
447k
    for (char c : source) {
187
241k
      result.push_back(static_cast<unsigned char>(c) % 128);
188
241k
    }
189
447k
    return result;
190
447k
  };
191
14.7k
  FuzzedDataProvider fdp(data, size);
192
  // We do not want to trigger arbitrary regex matching.
193
14.7k
  std::string source_1 = "/" + to_ascii(fdp.ConsumeRandomLengthString(50)) +
194
14.7k
                         "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
195
14.7k
  std::string base_source_1 = "/" +
196
14.7k
                              to_ascii(fdp.ConsumeRandomLengthString(50)) +
197
14.7k
                              "/" + to_ascii(fdp.ConsumeRandomLengthString(50));
198
199
14.7k
  std::string source_2 = "https://ada-url.com/*";
200
14.7k
  std::string base_source_2 = "https://ada-url.com";
201
202
  // Additional test input for exec/test calls (also ASCII-only). Relative
203
  // inputs ("./x", "?q", "#h") only parse against the base URL, and short
204
  // ones are stored inline (SSO) by std::string, which moves their bytes
205
  // whenever the owning string moves.
206
14.7k
  std::string test_input =
207
14.7k
      fdp.ConsumeBool()
208
14.7k
          ? to_ascii(fdp.ConsumeRandomLengthString(15))
209
14.7k
          : "https://" + to_ascii(fdp.ConsumeRandomLengthString(30)) + "/" +
210
9.94k
                to_ascii(fdp.ConsumeRandomLengthString(20));
211
14.7k
  std::string test_base = "https://ada-url.com";
212
213
14.7k
  std::array<std::pair<std::string, std::string>, 2> sources = {{
214
14.7k
      {source_1, base_source_1},
215
14.7k
      {source_2, base_source_2},
216
14.7k
  }};
217
218
29.5k
  for (const auto& [source, base_source] : sources) {
219
    // Without base or options
220
29.5k
    auto result =
221
29.5k
        ada::parse_url_pattern<regex_provider>(source, nullptr, nullptr);
222
29.5k
    if (result) {
223
14.7k
      exercise_result(*result);
224
14.7k
      exercise_exec_and_test(*result, test_input, test_base);
225
14.7k
    }
226
227
    // Testing with base_url
228
29.5k
    std::string_view base_source_view(base_source.data(), base_source.length());
229
29.5k
    auto result_with_base = ada::parse_url_pattern<regex_provider>(
230
29.5k
        source, &base_source_view, nullptr);
231
29.5k
    if (result_with_base) {
232
14.7k
      exercise_result(*result_with_base);
233
14.7k
      exercise_exec_and_test(*result_with_base, test_input, test_base);
234
14.7k
    }
235
236
    // Testing with base_url and options
237
29.5k
    ada::url_pattern_options options{.ignore_case = fdp.ConsumeBool()};
238
29.5k
    auto result_with_base_and_options = ada::parse_url_pattern<regex_provider>(
239
29.5k
        source, &base_source_view, &options);
240
29.5k
    if (result_with_base_and_options) {
241
14.7k
      exercise_result(*result_with_base_and_options);
242
14.7k
      exercise_exec_and_test(*result_with_base_and_options, test_input,
243
14.7k
                             test_base);
244
14.7k
    }
245
246
    // Testing with url_pattern_init and base url.
247
29.5k
    int field_index = fdp.ConsumeIntegralInRange(0, 7);
248
29.5k
    std::string random_value = to_ascii(fdp.ConsumeRandomLengthString(50));
249
29.5k
    ada::url_pattern_init init{};
250
29.5k
    switch (field_index) {
251
20.5k
      case 0:
252
20.5k
        init.protocol = random_value;
253
20.5k
        break;
254
1.04k
      case 1:
255
1.04k
        init.username = random_value;
256
1.04k
        break;
257
2.07k
      case 2:
258
2.07k
        init.password = random_value;
259
2.07k
        break;
260
858
      case 3:
261
858
        init.hostname = random_value;
262
858
        break;
263
2.19k
      case 4:
264
2.19k
        init.port = random_value;
265
2.19k
        break;
266
917
      case 5:
267
917
        init.pathname = random_value;
268
917
        break;
269
896
      case 6:
270
896
        init.search = random_value;
271
896
        break;
272
1.04k
      case 7:
273
1.04k
        init.hash = random_value;
274
1.04k
        break;
275
29.5k
    }
276
29.5k
    auto result_with_init = ada::parse_url_pattern<regex_provider>(
277
29.5k
        init, &base_source_view, nullptr);
278
29.5k
    if (result_with_init) {
279
0
      exercise_result(*result_with_init);
280
0
      exercise_exec_and_test(*result_with_init, test_input, test_base);
281
0
    }
282
283
    // Testing url_pattern_init with ALL fields populated simultaneously
284
29.5k
    ada::url_pattern_init init_all{};
285
29.5k
    init_all.protocol = to_ascii(fdp.ConsumeRandomLengthString(10));
286
29.5k
    init_all.username = to_ascii(fdp.ConsumeRandomLengthString(10));
287
29.5k
    init_all.password = to_ascii(fdp.ConsumeRandomLengthString(10));
288
29.5k
    init_all.hostname = to_ascii(fdp.ConsumeRandomLengthString(20));
289
29.5k
    init_all.port = to_ascii(fdp.ConsumeRandomLengthString(5));
290
29.5k
    init_all.pathname = "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
291
29.5k
    init_all.search = to_ascii(fdp.ConsumeRandomLengthString(10));
292
29.5k
    init_all.hash = to_ascii(fdp.ConsumeRandomLengthString(10));
293
29.5k
    auto result_with_init_all =
294
29.5k
        ada::parse_url_pattern<regex_provider>(init_all, nullptr, nullptr);
295
29.5k
    if (result_with_init_all) {
296
25.0k
      exercise_result(*result_with_init_all);
297
25.0k
      exercise_exec_and_test(*result_with_init_all, test_input, test_base);
298
25.0k
    }
299
300
    // Testing url_pattern_init with the base_url field set.
301
    //
302
    // url_pattern_init::base_url is a completely separate code path from the
303
    // base_url *parameter* of parse_url_pattern. When base_url is embedded
304
    // inside the init struct the spec processes it differently. This field
305
    // was previously never exercised by any fuzzer.
306
29.5k
    {
307
29.5k
      ada::url_pattern_init init_base_url{};
308
29.5k
      init_base_url.pathname =
309
29.5k
          "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
310
29.5k
      init_base_url.base_url = "https://example.com";
311
29.5k
      auto result_base_in_init = ada::parse_url_pattern<regex_provider>(
312
29.5k
          init_base_url, nullptr, nullptr);
313
29.5k
      if (result_base_in_init) {
314
27.8k
        exercise_result(*result_base_in_init);
315
27.8k
        exercise_exec_and_test(*result_base_in_init, test_input, test_base);
316
27.8k
      }
317
318
      // Also fuzz the base_url field itself.
319
29.5k
      ada::url_pattern_init init_fuzz_base{};
320
29.5k
      init_fuzz_base.pathname =
321
29.5k
          "/" + to_ascii(fdp.ConsumeRandomLengthString(15));
322
29.5k
      init_fuzz_base.base_url =
323
29.5k
          "https://" + to_ascii(fdp.ConsumeRandomLengthString(20));
324
29.5k
      auto result_fuzz_base = ada::parse_url_pattern<regex_provider>(
325
29.5k
          init_fuzz_base, nullptr, nullptr);
326
29.5k
      if (result_fuzz_base) {
327
1.41k
        exercise_result(*result_fuzz_base);
328
1.41k
        exercise_exec_and_test(*result_fuzz_base, test_input, test_base);
329
1.41k
      }
330
29.5k
    }
331
332
    // Testing url_pattern_init with a random subset (2–4) of fields set.
333
    //
334
    // The single-field case (switch above) and the all-fields case are covered
335
    // above. Here we pick a random bitmask of fields so the parser sees every
336
    // combination of present/absent components.
337
29.5k
    {
338
29.5k
      uint8_t field_mask = fdp.ConsumeIntegral<uint8_t>();
339
29.5k
      ada::url_pattern_init init_subset{};
340
29.5k
      if (field_mask & 0x01)
341
797
        init_subset.protocol = to_ascii(fdp.ConsumeRandomLengthString(8));
342
29.5k
      if (field_mask & 0x02)
343
992
        init_subset.hostname = to_ascii(fdp.ConsumeRandomLengthString(20));
344
29.5k
      if (field_mask & 0x04)
345
1.33k
        init_subset.port = to_ascii(fdp.ConsumeRandomLengthString(5));
346
29.5k
      if (field_mask & 0x08)
347
1.26k
        init_subset.pathname =
348
1.26k
            "/" + to_ascii(fdp.ConsumeRandomLengthString(20));
349
29.5k
      if (field_mask & 0x10)
350
1.19k
        init_subset.search = to_ascii(fdp.ConsumeRandomLengthString(10));
351
29.5k
      if (field_mask & 0x20)
352
1.27k
        init_subset.hash = to_ascii(fdp.ConsumeRandomLengthString(10));
353
29.5k
      if (field_mask & 0x40)
354
1.05k
        init_subset.username = to_ascii(fdp.ConsumeRandomLengthString(10));
355
29.5k
      if (field_mask & 0x80)
356
506
        init_subset.password = to_ascii(fdp.ConsumeRandomLengthString(10));
357
29.5k
      auto result_subset =
358
29.5k
          ada::parse_url_pattern<regex_provider>(init_subset, nullptr, nullptr);
359
29.5k
      if (result_subset) {
360
28.0k
        exercise_result(*result_subset);
361
28.0k
        exercise_exec_and_test(*result_subset, test_input, test_base);
362
28.0k
      }
363
29.5k
    }
364
29.5k
  }
365
366
14.7k
  return 0;
367
14.7k
}