Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
serializers.cc
Go to the documentation of this file.
1#include <fuzzer/FuzzedDataProvider.h>
2
3#include <array>
4#include <cassert>
5#include <cstdio>
6#include <string>
7
8#include "ada.cpp"
9#include "ada.h"
10
11// ============================================================
12// Fuzzer for IP address serializers, fast IPv4 parser, and
13// percent encode/decode utilities.
14//
15// These code paths are exercised indirectly by parse.cc but
16// never directly. Targeting them independently lets the fuzzer
17// discover edge cases in the serialization and encoding layers
18// without depending on a valid URL to reach them.
19// ============================================================
20
21extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
22 FuzzedDataProvider fdp(data, size);
23
24 // ===== IPv4 Serialization =====
25 // ipv4() takes a uint64_t (valid addresses are 0..0xFFFFFFFF, but the
26 // function accepts any value – out-of-range inputs should still be safe).
27 uint64_t ipv4_addr = fdp.ConsumeIntegral<uint64_t>();
28 std::string ipv4_str = ada::serializers::ipv4(ipv4_addr);
29 volatile size_t ipv4_len = ipv4_str.size();
30 (void)ipv4_len;
31
32 // ===== IPv6 Serialization =====
33 std::array<uint16_t, 8> ipv6_addr{};
34 for (auto& piece : ipv6_addr) {
35 piece = fdp.ConsumeIntegral<uint16_t>();
36 }
37
38 // find_longest_sequence_of_ipv6_pieces: basic invariants must hold.
39 size_t compress = 0, compress_length = 0;
41 compress_length);
42 // The longest run cannot exceed 8 pieces.
43 assert(compress_length <= 8);
44 // If a run was found (length > 0) its start index must be in-bounds.
45 if (compress_length > 0) {
46 assert(compress < 8);
47 assert(compress + compress_length <= 8);
48 }
49
50 std::string ipv6_str = ada::serializers::ipv6(ipv6_addr);
51 volatile size_t ipv6_len = ipv6_str.size();
52 (void)ipv6_len;
53
54 // Serialized IPv6 must always be a non-empty string.
55 assert(!ipv6_str.empty());
56
57 // ===== Fast IPv4 Parser =====
58 // try_parse_ipv4_fast() should agree with the full parsing pipeline on its
59 // output: if it succeeds (result <= 0xFFFFFFFF), re-serializing with
60 // ipv4() and parsing again must yield the same address value.
61 {
62 std::string ip_candidate = fdp.ConsumeRandomLengthString(32);
63 uint64_t fast_result = ada::checkers::try_parse_ipv4_fast(ip_candidate);
64 if (fast_result <= 0xFFFFFFFF) {
65 // Serialize the parsed address and re-parse.
66 std::string canonical = ada::serializers::ipv4(fast_result);
67 uint64_t recheck = ada::checkers::try_parse_ipv4_fast(canonical);
68 if (recheck != fast_result) {
69 printf(
70 "try_parse_ipv4_fast round-trip failure:\n"
71 " input='%s' result=%llu canonical='%s' recheck=%llu\n",
72 ip_candidate.c_str(), (unsigned long long)fast_result,
73 canonical.c_str(), (unsigned long long)recheck);
74 abort();
75 }
76 }
77 }
78
79 // ===== Percent Encode / Decode =====
80 {
81 std::string source = fdp.ConsumeRandomLengthString(128);
82
83 // Exercise all six standard character sets used by the URL parser.
84 const uint8_t* sets[] = {
91 };
92
93 for (const uint8_t* charset : sets) {
94 // Two-argument percent_encode: returns the encoded string.
95 std::string encoded = ada::unicode::percent_encode(source, charset);
96 volatile size_t enc_len = encoded.size();
97 (void)enc_len;
98
99 // Encoded output must be at least as long as the input (each byte
100 // either stays the same or expands to %XX – three bytes).
101 assert(encoded.size() >= source.size());
102
103 // Three-argument percent_encode: starts encoding from a given index.
104 size_t start_idx = fdp.ConsumeIntegralInRange<size_t>(0, source.size());
105 std::string encoded_from =
106 ada::unicode::percent_encode(source, charset, start_idx);
107 volatile size_t enf_len = encoded_from.size();
108 (void)enf_len;
109
110 // Template form: percent_encode<false> (replace).
111 {
112 std::string out;
113 bool changed =
114 ada::unicode::percent_encode<false>(source, charset, out);
115 volatile bool c = changed;
116 (void)c;
117 // When encoding was needed 'out' holds the encoded string; when not,
118 // 'out' is unchanged (empty). If changed, 'out' must not be shorter.
119 if (changed) {
120 assert(out.size() >= source.size());
121 }
122 }
123
124 // Template form: percent_encode<true> (append).
125 {
126 std::string out = "prefix_";
127 size_t prefix_len = out.size();
128 ada::unicode::percent_encode<true>(source, charset, out);
129 // Append mode: 'out' must grow by at least source.size() bytes if
130 // encoding was needed, or stay unchanged if it wasn't.
131 assert(out.size() >= prefix_len);
132 }
133 }
134
135 // Percent decode: feed raw fuzz input (may contain invalid sequences).
136 {
137 size_t pct_pos = source.find('%');
138 if (pct_pos != std::string::npos) {
139 std::string decoded = ada::unicode::percent_decode(source, pct_pos);
140 // Decoded output can't be longer than the input.
141 assert(decoded.size() <= source.size());
142 volatile size_t dec_len = decoded.size();
143 (void)dec_len;
144 }
145 }
146
147 // Round-trip: encode with PATH set, then decode the result; the decoded
148 // form must equal the original (encoding then decoding is an identity).
149 {
150 std::string encoded = ada::unicode::percent_encode(
152 size_t pct_pos = encoded.find('%');
153 if (pct_pos != std::string::npos) {
154 std::string decoded = ada::unicode::percent_decode(encoded, pct_pos);
155 // PATH percent-encode does not encode '%', so a source that already
156 // contains %HH is decoded and the round-trip is not an identity.
157 if (source.find('%') == std::string::npos && decoded != source) {
158 printf(
159 "percent_encode/decode round-trip failure!\n"
160 " source='%s'\n encoded='%s'\n decoded='%s'\n",
161 source.c_str(), encoded.c_str(), decoded.c_str());
162 abort();
163 }
164 } else {
165 // No encoding was needed; the output should equal the input.
166 assert(encoded == source);
167 }
168 }
169
170 // percent_encode_index: the returned index must be within [0, size].
171 {
174 assert(idx <= source.size());
175 }
176 }
177
178 // ===== Checker and Unicode Utility Functions =====
179 // These are internal helpers used throughout the parser. Fuzzing them
180 // directly (rather than only through the URL parsing pipeline) ensures
181 // every edge case is reachable without a valid URL structure.
182 {
183 std::string util_input = fdp.ConsumeRandomLengthString(128);
184
185 // has_tabs_or_newline: any string is valid input.
186 volatile bool has_tn = ada::unicode::has_tabs_or_newline(util_input);
187 (void)has_tn;
188
189 // is_ipv4 assumes a non-empty, lower-cased input, so honor that
190 // precondition here. Cross-check against URL parsing: if is_ipv4 reports
191 // true, embedding the string as a hostname in an http:// URL must succeed
192 // (it must be a parseable IPv4 address).
193 if (!util_input.empty()) {
194 volatile bool is_v4 = ada::checkers::is_ipv4(util_input);
195 (void)is_v4;
196 if (is_v4) {
197 std::string ipv4_url = "http://" + util_input + "/";
198 auto parsed = ada::parse<ada::url_aggregator>(ipv4_url);
199 // is_ipv4 reports true only for strings that look like IPv4 addresses;
200 // the full parser may still reject them (e.g. out-of-range octets), but
201 // if it accepts them the host type must be IPv4.
202 if (parsed) {
203 volatile bool v = parsed->validate();
204 (void)v;
205 }
206 }
207 }
208
209 // path_signature: returns a bitmask; must not crash.
210 volatile uint8_t sig = ada::checkers::path_signature(util_input);
211 (void)sig;
212
213 // is_windows_drive_letter: must not crash on short or long inputs.
214 volatile bool is_wdl = ada::checkers::is_windows_drive_letter(util_input);
215 (void)is_wdl;
216
217 // is_normalized_windows_drive_letter
218 volatile bool is_nwdl =
220 (void)is_nwdl;
221
222 // Consistency: a normalised Windows drive letter is a subset of
223 // Windows drive letters.
224 if (is_nwdl && !is_wdl) {
225 printf(
226 "is_normalized_windows_drive_letter implies is_windows_drive_letter"
227 " but got inconsistent results for '%s'\n",
228 util_input.c_str());
229 abort();
230 }
231
232 // to_lower_ascii: works in-place; must not crash.
233 {
234 std::string lower_copy = util_input;
235 volatile bool all_ascii =
236 ada::unicode::to_lower_ascii(lower_copy.data(), lower_copy.size());
237 (void)all_ascii;
238 // The result should be at most as long as the input.
239 assert(lower_copy.size() == util_input.size());
240 }
241
242 // contains_forbidden_domain_code_point: must not crash.
243 volatile bool has_forbidden =
244 ada::unicode::contains_forbidden_domain_code_point(util_input.data(),
245 util_input.size());
246 (void)has_forbidden;
247
248 // contains_forbidden_domain_code_point_or_upper: must not crash.
249 volatile uint8_t forbidden_or_upper =
250 ada::unicode::contains_forbidden_domain_code_point_or_upper(
251 util_input.data(), util_input.size());
252 (void)forbidden_or_upper;
253
254 // verify_dns_length: must not crash; also check consistency with
255 // to_ascii output.
256 volatile bool dns_ok = ada::checkers::verify_dns_length(util_input);
257 (void)dns_ok;
258 }
259
260 // ===== Integration: embed serialized addresses into real URLs =====
261 // This exercises the full parsing pipeline with our synthetic addresses
262 // and verifies that serialization output is always round-trip safe.
263 {
264 // Only valid IPv4 address range (0..0xFFFFFFFF) should embed cleanly.
265 if (ipv4_addr <= 0xFFFFFFFF) {
266 std::string url_str = "http://" + ipv4_str + "/path?q=1";
267 auto parsed = ada::parse<ada::url_aggregator>(url_str);
268 if (parsed) {
269 volatile bool v = parsed->validate();
270 (void)v;
271 // The URL's hostname must be the canonical dotted-decimal address.
272 std::string host = std::string(parsed->get_hostname());
273 // host == ipv4_str (not asserted here because the URL parser may
274 // normalise the address differently for non-standard values, but it
275 // must always successfully parse our serialized form).
276 (void)host;
277 }
278 }
279
280 // IPv6: wrap in brackets.
281 std::string ipv6_url_str = "http://[" + ipv6_str + "]/path";
282 auto parsed_ipv6 = ada::parse<ada::url_aggregator>(ipv6_url_str);
283 if (parsed_ipv6) {
284 volatile bool v = parsed_ipv6->validate();
285 (void)v;
286 // Re-parse the href – must be idempotent.
287 std::string href = std::string(parsed_ipv6->get_href());
288 auto reparsed = ada::parse<ada::url_aggregator>(href);
289 if (!reparsed) {
290 printf("IPv6 URL re-parse failure: '%s'\n", href.c_str());
291 abort();
292 }
293 if (std::string(reparsed->get_href()) != href) {
294 printf("IPv6 URL re-parse href mismatch: '%s' vs '%s'\n", href.c_str(),
295 std::string(reparsed->get_href()).c_str());
296 abort();
297 }
298 }
299 }
300
301 return 0;
302}
Main header for the Ada URL parser library.
constexpr uint8_t QUERY_PERCENT_ENCODE[32]
constexpr uint8_t SPECIAL_QUERY_PERCENT_ENCODE[32]
constexpr uint8_t PATH_PERCENT_ENCODE[32]
constexpr uint8_t C0_CONTROL_PERCENT_ENCODE[32]
constexpr uint8_t USERINFO_PERCENT_ENCODE[32]
constexpr uint8_t FRAGMENT_PERCENT_ENCODE[32]
constexpr bool is_normalized_windows_drive_letter(std::string_view input) noexcept
constexpr bool is_windows_drive_letter(std::string_view input) noexcept
ada_really_inline uint64_t try_parse_ipv4_fast(std::string_view input) noexcept
void find_longest_sequence_of_ipv6_pieces(const std::array< uint16_t, 8 > &address, size_t &compress, size_t &compress_length) noexcept
std::string ipv6(const std::array< uint16_t, 8 > &address)
std::string ipv4(uint64_t address)
ada_really_inline size_t percent_encode_index(const std::string_view input, const uint8_t character_set[])
Definition unicode-inl.h:24
template bool percent_encode< true >(std::string_view input, const uint8_t character_set[], std::string &out)
template bool percent_encode< false >(std::string_view input, const uint8_t character_set[], std::string &out)
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)