onionwire/fuzz/fuzz_targets/file_decode.rs
rust-dev 2cf9e8c7c6
fuzz: cargo-fuzz harness for the ratchet/qr/dispatch/file parsers
Four pure parsers eat untrusted peer input and had no structured fuzzing:

- ratchet_frame  -> RatchetState::from_bytes + RatchetState::decrypt
                    (the 'rat ' frame parser: prefix, version, length,
                    N/PN bounds, DH step, AEAD open)
- qr_decode      -> qr::decode on pasted/scanned invite strings
- dispatch_classify -> dispatch::classify on raw decrypted plaintext,
                    including the 'rat ' -> Kind::Ratchet routing arm
- file_decode    -> file::decode on 'fil ' chunk frames

No src/ change: every target calls the shipped public API. The fuzz crate is
its own workspace (empty [workspace] table), so no root Cargo.toml dependency
is added and cargo build/test at the root never compiles it.

Seeds in fuzz/seeds/ are committed and copied into the gitignored working
corpus by scripts/run-fuzz.sh; each target asserts properties a panic-free
decode must hold (a rejected ratchet frame leaves the state byte-identical,
a decoded invite is 32/32/onion-shaped, a decoded chunk re-encodes).

Verified: cargo +nightly fuzz list -> 4 targets; scripts/run-fuzz.sh with
FUZZ_SECS=60 FUZZ_JOBS=4 -> exit 0, oom/timeout/crash: 0/0/0 for all four,
fuzz/artifacts/* empty.
2026-09-17 21:04:20 -04:00

26 lines
1.1 KiB
Rust

//! Untrusted file-chunk bytes against the chunk decoder.
//!
//! A peer can send a `fil ` chunk at any time; `file::decode` header-parses it
//! (hex transfer id, filename, hex sha256, `idx/total`, then the payload) before
//! anything is written to disk. It must return `None` for every malformed
//! chunk and never panic — including on absurd `idx`/`total` and on filenames
//! that fail `safe_name`.
#![no_main]
use libfuzzer_sys::fuzz_target;
use onionwire::file;
fuzz_target!(|data: &[u8]| {
if let Some(chunk) = file::decode(data) {
// A chunk it accepted must survive an encode round-trip and decode
// back to the same header, byte for byte.
let re = file::encode(&chunk);
let again = file::decode(&re).expect("encoded chunk must decode");
assert_eq!(again.xfer_id, chunk.xfer_id);
assert_eq!(again.filename, chunk.filename);
assert_eq!(again.sha256, chunk.sha256);
assert_eq!(again.idx, chunk.idx);
assert_eq!(again.total, chunk.total);
assert_eq!(again.data, chunk.data);
}
});