68 lines
2.8 KiB
Rust
68 lines
2.8 KiB
Rust
//! Fuzzes the host's HTTP request parsing: the head cutter, the head parser, the target parser and the query parser.
|
|
//! Asserts invariants, not only "no panic": a parsed head ends exactly where the cutter says, a parsed query
|
|
//! re-encodes to the same parameters, and every typed value of a parsed request is within its documented range.
|
|
//! How to run: see the comment at the top of `fuzz/Cargo.toml`.
|
|
|
|
#![no_main]
|
|
|
|
use libfuzzer_sys::fuzz_target;
|
|
|
|
use domistream::host::http::{
|
|
HostRequest, MAX_HEAD_BYTES, MAX_MODE_DIMENSION, MAX_MODE_FPS, MAX_PARAMS, Route, encode_query, find_head_end,
|
|
parse_query, parse_request_head, parse_target,
|
|
};
|
|
use domistream::host::pairing::PairPhase;
|
|
|
|
fn check(req: &HostRequest) {
|
|
assert!(req.other_params.len() <= MAX_PARAMS);
|
|
if let Some(u) = &req.unique_id {
|
|
assert!(!u.is_empty() && u.len() <= 64, "uniqueid {u:?}");
|
|
}
|
|
let mode_ok = |m: &domistream::host::http::DisplayMode| {
|
|
(1..=MAX_MODE_DIMENSION).contains(&m.width)
|
|
&& (1..=MAX_MODE_DIMENSION).contains(&m.height)
|
|
&& (1..=MAX_MODE_FPS).contains(&m.fps)
|
|
};
|
|
match &req.route {
|
|
Route::Launch(l) => assert!(mode_ok(&l.mode), "{:?}", l.mode),
|
|
Route::Resume(r) => assert!(r.mode.as_ref().is_none_or(mode_ok)),
|
|
Route::Pair(p) => {
|
|
assert_eq!(req.unique_id.as_deref(), Some(p.unique_id.as_str()), "/pair needs uniqueid");
|
|
match &p.phase {
|
|
PairPhase::GetServerCert { client_cert_pem, .. } => assert!(!client_cert_pem.is_empty()),
|
|
PairPhase::ClientChallenge(v)
|
|
| PairPhase::ServerChallengeResp(v)
|
|
| PairPhase::ClientPairingSecret(v) => {
|
|
assert!(!v.is_empty())
|
|
}
|
|
PairPhase::PairChallenge => {}
|
|
}
|
|
}
|
|
Route::Unknown { path } => assert!(path.starts_with('/') && path.len() <= 256),
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fuzz_target!(|data: &[u8]| {
|
|
let cut = find_head_end(data);
|
|
if let Ok(Some(n)) = cut {
|
|
assert!((4..=MAX_HEAD_BYTES).contains(&n) && data[..n].ends_with(b"\r\n\r\n"));
|
|
}
|
|
if let Ok(req) = parse_request_head(data) {
|
|
assert_eq!(cut, Ok(Some(data.len())), "a parsed head ends where the cutter says");
|
|
check(&req);
|
|
}
|
|
let Ok(text) = core::str::from_utf8(data) else { return };
|
|
if let Ok(req) = parse_target(text) {
|
|
check(&req);
|
|
}
|
|
if let Ok(params) = parse_query(text) {
|
|
let list: Vec<(&str, &str)> = params.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
|
|
match parse_query(&encode_query(&list)) {
|
|
Ok(again) => assert_eq!(again, params, "parse -> encode -> parse"),
|
|
// Encoding can triple a value (`+` becomes `%20`), so only a cap may refuse it.
|
|
Err(e) => assert!(matches!(e, domistream::Error::TooLarge { .. }), "{e:?}"),
|
|
}
|
|
}
|
|
});
|