113 lines
4.5 KiB
Rust
113 lines
4.5 KiB
Rust
//! Short fuzz runs of the parsers (S037): seeds mutated at random, no panic, and the safety rules hold.
|
|
//! Fixed seeds of the random source, so a failure repeats; `MEDLAR_FUZZ=n` runs more rounds.
|
|
|
|
use crate::{batch::Batch, http::name, listing, metalink, redact};
|
|
|
|
pub(crate) fn rounds() -> usize {
|
|
std::env::var("MEDLAR_FUZZ").ok().and_then(|n| n.parse().ok()).unwrap_or(2000)
|
|
}
|
|
|
|
const PIECES: &[&[u8]] = &[
|
|
b"\0", b"\r\n", b"\n", b"/", b"\\", b"..", b"%2F", b"%00", b"\"", b"'", b";", b"=", b"*", b"<", b">", b"&", b"[", b"]",
|
|
b"{", b"}", b"-", b":", b"99999999999999999999", b"18446744073709551615", b"-1", b"0", b"\xff\xfe", b"\xe2\x80\xae",
|
|
b"\xc3", b"UTF-8''", b"<![CDATA[", b"]]>", b"<!--", b"#EXT", b"$Number$", b"$Time$", b"PT9999999H", b"r=\"-1\"",
|
|
];
|
|
|
|
/// The seed with a few random cuts, copies and inserts.
|
|
pub(crate) fn mutate(rng: &mut fastrand::Rng, seed: &[u8]) -> Vec<u8> {
|
|
let mut out = seed.to_vec();
|
|
for _ in 0..rng.usize(1..8) {
|
|
let at = rng.usize(0..=out.len());
|
|
match rng.u8(0..5) {
|
|
0 => out.splice(at..at, PIECES[rng.usize(0..PIECES.len())].iter().copied()).for_each(drop),
|
|
1 => {
|
|
let end = (at + rng.usize(0..32)).min(out.len());
|
|
out.drain(at..end);
|
|
}
|
|
2 => {
|
|
let end = (at + rng.usize(0..64)).min(out.len());
|
|
let copy = out[at..end].to_vec();
|
|
out.splice(at..at, copy).for_each(drop);
|
|
}
|
|
3 => out.insert(at, rng.u8(..)),
|
|
_ => {
|
|
if at < out.len() {
|
|
out[at] = rng.u8(..);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
pub(crate) fn run(seeds: &[&[u8]], mut check: impl FnMut(&[u8])) {
|
|
let mut rng = fastrand::Rng::with_seed(37);
|
|
for seed in seeds {
|
|
check(seed);
|
|
}
|
|
for _ in 0..rounds() {
|
|
let seed = seeds[rng.usize(0..seeds.len())];
|
|
check(&mutate(&mut rng, seed));
|
|
}
|
|
}
|
|
|
|
pub(crate) fn assert_safe_name(n: &str) {
|
|
assert!(!n.is_empty() && n.len() <= 255, "{n:?}");
|
|
assert!(!n.contains(['/', '\\']) && !n.starts_with('.') && !n.chars().any(char::is_control), "{n:?}");
|
|
}
|
|
|
|
#[test]
|
|
fn names() {
|
|
run(&[b"a.zip", b"../../etc/passwd", b"..\\..\\x.exe", b".bashrc", b"CON.txt", b"\xe2\x80\xaegpj.exe", &[b'a'; 400]], |b| {
|
|
if let Some(n) = name::safe(&String::from_utf8_lossy(b)) {
|
|
assert_safe_name(&n);
|
|
}
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn metalinks() {
|
|
const V4: &[u8] = br#"<?xml version="1.0"?><metalink xmlns="urn:ietf:params:xml:ns:metalink"><file name="a/../b.iso"><size>10</size>
|
|
<hash type="sha-256">00</hash><url priority="1">https://h/b.iso</url><url>ftp://h/b.iso</url><pieces length="4" type="sha-1"><hash>aa</hash></pieces></file></metalink>"#;
|
|
const V3: &[u8] = br#"<metalink version="3.0" xmlns="http://www.metalinker.org/"><files><file name="c.zip"><verification><hash type="md5">00</hash></verification>
|
|
<resources><url type="http" preference="100">http://h/c.zip</url></resources></file></files></metalink>"#;
|
|
run(&[V4, V3, b"<!DOCTYPE x [<!ENTITY a \"aaaa\">]><metalink>&a;&a;</metalink>"], |b| {
|
|
let _ = metalink::parse(b);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn listings() {
|
|
const MLSD: &[u8] = b"type=file;size=10;modify=20260101120000; a.zip\r\ntype=dir;modify=20260101120000; sub\r\ntype=cdir; .\r\n";
|
|
const UNIX: &[u8] = b"-rw-r--r-- 1 u g 1234 Jan 01 12:00 a b.zip\ndrwxr-xr-x 2 u g 4096 2025 sub\nlrwxrwxrwx 1 u g 3 Jan 1 2025 l -> ../x\n";
|
|
const DOS: &[u8] = b"01-01-26 12:00PM <DIR> sub\r\n01-01-26 12:00PM 1234 a.zip\r\n";
|
|
let now = chrono::Utc::now();
|
|
run(&[MLSD, UNIX, DOS], |b| {
|
|
let _ = listing::mlsd(b);
|
|
let _ = listing::plain(b, now);
|
|
});
|
|
}
|
|
|
|
#[test]
|
|
fn patterns() {
|
|
run(
|
|
&[b"https://h/[001-120].jpg", b"https://h/{a,b,c}/[a-f]/[0-100:10]", b"https://h/[2026-01-30..2026-02-02].log", b"https://h/[0-99999999999999999]/[0-9999999]"],
|
|
|b| {
|
|
let text = String::from_utf8_lossy(b);
|
|
for patterns in [true, false] {
|
|
let batch = Batch::of(&text, patterns);
|
|
assert!(batch.links.len() <= crate::batch::MOST && batch.total >= batch.links.len() as u64);
|
|
}
|
|
},
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn redaction() {
|
|
run(&[b"https://user:pass@h/a?token=secret#x", b"Cookie: k=v\nAuthorization: Basic abc"], |b| {
|
|
let text = String::from_utf8_lossy(b);
|
|
let _ = redact::text(&text);
|
|
let _ = redact::url(&text);
|
|
});
|
|
assert!(!redact::url("https://user:hunter2@h/a?sig=hunter3").contains("hunter"));
|
|
}
|