Skip to content

Commit ede5de4

Browse files
authored
ci: add simple fuzzing implementation for parser (#73)
1 parent ec34485 commit ede5de4

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

fuzz/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target
2+
corpus
3+
artifacts
4+
coverage

fuzz/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "urlpattern-fuzz"
3+
version = "0.0.0"
4+
publish = false
5+
edition = "2021"
6+
7+
[package.metadata]
8+
cargo-fuzz = true
9+
10+
[dependencies]
11+
libfuzzer-sys = "0.4"
12+
arbitrary = { version = "1", features = ["derive"] }
13+
regex = "1"
14+
15+
[dependencies.urlpattern]
16+
path = ".."
17+
18+
[[bin]]
19+
name = "fuzz_parse"
20+
path = "fuzz_targets/fuzz_parse.rs"
21+
test = false
22+
doc = false
23+
bench = false

fuzz/fuzz_targets/fuzz_parse.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![no_main]
2+
use urlpattern::{UrlPattern, UrlPatternInit, UrlPatternOptions};
3+
use libfuzzer_sys::arbitrary::Arbitrary;
4+
use libfuzzer_sys::fuzz_target;
5+
6+
#[derive(Arbitrary, Debug)]
7+
struct FuzzInput {
8+
protocol: Option<String>,
9+
username: Option<String>,
10+
password: Option<String>,
11+
hostname: Option<String>,
12+
port: Option<String>,
13+
pathname: Option<String>,
14+
search: Option<String>,
15+
hash: Option<String>,
16+
base_url: Option<String>,
17+
ignore_case: bool,
18+
}
19+
20+
fuzz_target!(|input: FuzzInput| {
21+
let init = UrlPatternInit {
22+
protocol: input.protocol,
23+
username: input.username,
24+
password: input.password,
25+
hostname: input.hostname,
26+
port: input.port,
27+
pathname: input.pathname,
28+
search: input.search,
29+
hash: input.hash,
30+
base_url: input.base_url.and_then(|s| s.parse().ok()),
31+
};
32+
let options = UrlPatternOptions {
33+
ignore_case: input.ignore_case,
34+
};
35+
let _ = UrlPattern::<regex::Regex>::parse(init, options);
36+
});

0 commit comments

Comments
 (0)