test(fuzz): a target for reading a backup folder from bytes someone else wrote

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Fable 5.2 committed 2026-11-21 18:38:30 -07:00
1 parent fd78612dd4
commit 20b2e759e0
10 files changed
+838 -16

No files matched your search

+40
View File
@@ -0,0 +1,40 @@
// Fuzz target: a backup folder is read from a disk someone else may have
// written (a sync service's, another computer's, a damaged one), so every
// file of it is untrusted input: the manifest, the header, a piece. Finding
// and proving a backup must return books or an error, never panic or hang,
// and it must never accept what is not whole.
#![no_main]
use libfuzzer_sys::fuzz_target;
use std::path::Path;
fn put(root: &Path, rel: &str, bytes: &[u8]) {
let p = root.join(rel);
std::fs::create_dir_all(p.parent().unwrap()).unwrap();
std::fs::write(p, bytes).unwrap();
}
fuzz_target!(|data: &[u8]| {
// The input is split in three: a manifest, a header and a piece.
let third = data.len() / 3;
let (manifest, rest) = data.split_at(third);
let (header, piece) = rest.split_at(third.min(rest.len()));
let dir = tempfile::tempdir().unwrap();
let set = dir.path().join("acme");
put(&set, "manifests/000001.json", manifest);
put(&set, "manifests/000001", manifest);
put(&set, "header.json", header);
put(&set, "journal/000001-0000000000000000", piece);
put(&set, "pieces/00", piece);
let ctx = ledger_store::backup::Ctx::new();
for pass in [None, Some("a passphrase of enough length")] {
if let Ok(found) = ledger_store::backup::find(dir.path(), pass, &ctx) {
let scratch = dir.path().join("scratch");
// Whatever a manifest claims, what is made of this input must
// fail: nothing in it is a whole backup.
let proven = ledger_store::backup::verify_found(&found, &scratch, &ctx);
assert!(proven.is_err(), "random bytes proved whole");
let _ = std::fs::remove_dir_all(&scratch);
}
}
});