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:
1 parent
fd78612dd4
commit
20b2e759e0
10 files changed
+838
-16
No files matched your search
@@ -57,7 +57,7 @@ jobs:
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
target: [fuzz_journal_line, fuzz_entry_validate, fuzz_op_sequence, fuzz_core_serde, fuzz_store_open, fuzz_store_replay, fuzz_settings_replay, fuzz_import_csv, fuzz_config_parse, fuzz_extract_pdf, fuzz_extract_xlsx, fuzz_http_mcp_request, fuzz_upload_put]
|
||||
target: [fuzz_journal_line, fuzz_entry_validate, fuzz_op_sequence, fuzz_core_serde, fuzz_store_open, fuzz_backup_read, fuzz_store_replay, fuzz_settings_replay, fuzz_import_csv, fuzz_config_parse, fuzz_extract_pdf, fuzz_extract_xlsx, fuzz_http_mcp_request, fuzz_upload_put]
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: jdx/mise-action@v2
|
||||
|
||||
Generated
+1
@@ -4121,6 +4121,7 @@ name = "ledger-host"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"base64 0.22.1",
|
||||
"chrono",
|
||||
"ledger-core",
|
||||
"ledger-link",
|
||||
"ledger-mcp",
|
||||
|
||||
@@ -24,6 +24,7 @@ tokio-util = { version = "0.7", features = ["rt"] }
|
||||
rpassword = "7"
|
||||
# Token texts and passphrases are wiped from memory when dropped.
|
||||
zeroize = "1"
|
||||
chrono = "0.4"
|
||||
minisign-verify = "0.3.0"
|
||||
# The Claude desktop extension is a zip file (extension.rs).
|
||||
zip = { version = "9.0.1", default-features = false, features = ["deflate"] }
|
||||
|
||||
@@ -1285,6 +1285,19 @@ pub fn backup_status(common: &Common, book: &str) -> Result<Vec<String>, String>
|
||||
backup_standing(common, book).map(|s| backup_words(&s))
|
||||
}
|
||||
|
||||
/// A time the server recorded (UTC, RFC 3339) as a person reads it: their own
|
||||
/// clock, day, month and year, hour and minute.
|
||||
fn when(utc: &str) -> String {
|
||||
chrono::DateTime::parse_from_rfc3339(utc).map_or_else(
|
||||
|_| utc.to_string(),
|
||||
|t| {
|
||||
t.with_timezone(&chrono::Local)
|
||||
.format("%B %-d, %Y at %H:%M")
|
||||
.to_string()
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// The words for a backup standing, as an ordinary person reads them.
|
||||
pub fn backup_words(s: &admin::BackupStanding) -> Vec<String> {
|
||||
let mut lines = Vec::new();
|
||||
@@ -1299,7 +1312,8 @@ pub fn backup_words(s: &admin::BackupStanding) -> Vec<String> {
|
||||
}
|
||||
match (&s.last_ok_at, s.through_seq) {
|
||||
(Some(at), Some(seq)) => lines.push(format!(
|
||||
"Last backed up {at}, through event {seq} of {}.{}",
|
||||
"Last backed up {}, through event {seq} of {}.{}",
|
||||
when(at),
|
||||
s.head_seq,
|
||||
if s.through_seq == Some(s.head_seq) {
|
||||
" Up to date."
|
||||
@@ -4344,6 +4358,89 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// The words a person is told of each state of their backups: none set up,
|
||||
/// a disk not connected, a failure, up to date and protected, behind, a
|
||||
/// failed weekly check. No word of how it is made, and never git.
|
||||
#[test]
|
||||
fn the_backups_are_described_in_words_for_each_state() {
|
||||
let base = admin::BackupStanding {
|
||||
head_seq: 12,
|
||||
folder: None,
|
||||
sealed: false,
|
||||
last_ok_at: None,
|
||||
through_seq: None,
|
||||
stale: false,
|
||||
last_error: None,
|
||||
last_drill_at: None,
|
||||
drill_error: None,
|
||||
disconnected: false,
|
||||
note: None,
|
||||
};
|
||||
let say = |s: &admin::BackupStanding| backup_words(s).join(" | ");
|
||||
assert_eq!(say(&base), "Not backed up: no backup folder is chosen yet.");
|
||||
let with_folder = admin::BackupStanding {
|
||||
folder: Some("/media/disk/Backups".into()),
|
||||
..base.clone()
|
||||
};
|
||||
let said = say(&with_folder);
|
||||
assert!(
|
||||
said.contains("Backing up to /media/disk/Backups."),
|
||||
"{said}"
|
||||
);
|
||||
assert!(
|
||||
said.contains("No backup has been made there yet."),
|
||||
"{said}"
|
||||
);
|
||||
assert!(said.contains("Not protected by a passphrase."), "{said}");
|
||||
let away = admin::BackupStanding {
|
||||
disconnected: true,
|
||||
last_error: Some("the backup folder isn't there (is its disk connected?)".into()),
|
||||
..with_folder.clone()
|
||||
};
|
||||
let said = say(&away);
|
||||
assert!(
|
||||
said.contains("The disk with the backup is not connected"),
|
||||
"{said}"
|
||||
);
|
||||
let failing = admin::BackupStanding {
|
||||
last_error: Some("another set of books is in the folder".into()),
|
||||
..with_folder.clone()
|
||||
};
|
||||
assert!(say(&failing).contains("The last backup didn't work: another set of books"));
|
||||
let fresh = admin::BackupStanding {
|
||||
last_ok_at: Some("2026-11-21T10:00:00.000Z".into()),
|
||||
through_seq: Some(12),
|
||||
last_drill_at: Some("2026-11-21T10:05:00.000Z".into()),
|
||||
sealed: true,
|
||||
..with_folder.clone()
|
||||
};
|
||||
let said = say(&fresh);
|
||||
assert!(
|
||||
said.contains("through event 12 of 12. Up to date."),
|
||||
"{said}"
|
||||
);
|
||||
assert!(said.contains("Proven restorable"), "{said}");
|
||||
assert!(
|
||||
said.contains("Protected by a passphrase. A forgotten passphrase cannot be recovered."),
|
||||
"{said}"
|
||||
);
|
||||
let behind = admin::BackupStanding {
|
||||
through_seq: Some(9),
|
||||
stale: true,
|
||||
drill_error: Some("a piece is damaged".into()),
|
||||
..fresh.clone()
|
||||
};
|
||||
let said = say(&behind);
|
||||
assert!(said.contains("More than a day behind."), "{said}");
|
||||
assert!(
|
||||
said.contains("The weekly check that the backup can be restored failed"),
|
||||
"{said}"
|
||||
);
|
||||
for s in [&base, &with_folder, &away, &failing, &fresh, &behind] {
|
||||
assert!(!say(s).to_lowercase().contains("git"));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_backup_is_restored_under_its_folders_or_files_name() {
|
||||
for (source, want) in [
|
||||
|
||||
@@ -969,7 +969,7 @@ impl Backups {
|
||||
pub async fn set_up(
|
||||
&self,
|
||||
folder: Option<String>,
|
||||
passphrase: Option<String>,
|
||||
passphrase: Option<zeroize::Zeroizing<String>>,
|
||||
remove_passphrase: bool,
|
||||
) -> Result<Vec<String>, String> {
|
||||
let _one = self.running.lock().await;
|
||||
|
||||
@@ -1658,7 +1658,9 @@ impl Books {
|
||||
} => match self.backups_of(&id) {
|
||||
Ok(backups) => {
|
||||
let rt = tokio::runtime::Handle::current();
|
||||
let pass = passphrase.as_ref().map(|p| p.0.clone());
|
||||
let pass = passphrase
|
||||
.as_ref()
|
||||
.map(|p| zeroize::Zeroizing::new(p.0.clone()));
|
||||
match rt.block_on(backups.set_up(folder, pass, remove_passphrase)) {
|
||||
Ok(lines) => Response::Said { lines },
|
||||
Err(why) => Response::Refused(self.hide(why)),
|
||||
|
||||
Generated
+684
-12
File diff suppressed because it is too large.
Load diff
@@ -14,6 +14,7 @@ serde_json = "1"
|
||||
serde = "1"
|
||||
rust_decimal = "1"
|
||||
chrono = "0.4"
|
||||
tempfile = "3"
|
||||
|
||||
[dependencies.ledger-core]
|
||||
path = "../crates/ledger-core"
|
||||
@@ -61,6 +62,13 @@ test = false
|
||||
doc = false
|
||||
bench = false
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_backup_read"
|
||||
path = "fuzz_targets/fuzz_backup_read.rs"
|
||||
test = false
|
||||
doc = false
|
||||
bench = false
|
||||
|
||||
[[bin]]
|
||||
name = "fuzz_store_open"
|
||||
path = "fuzz_targets/fuzz_store_open.rs"
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1 @@
|
||||
{"format":"wanderbooks-backup"}
|
||||
Reference in new issue
Block a user