Files
wanderbooks/fuzz/fuzz_targets/fuzz_store_replay.rs
T
swaitsandClaude Opus 5.5 aec55b8d12 Store: say "newer wanderbooks", not "corrupt", for books from a newer version
An unknown event type on a whole, chained line, or a journal.format_set
declaring a newer journal format, now raises StoreError::NewerJournal
(update wanderbooks) instead of JournalCorrupt. Corruption (bad hash,
seq, or JSON) is still corruption. The gate runs in the store, the
Snapshot and the GUI, after each line's parse, seq and hash checks, and
never skips a line it can't read. Adds the journal.format_set event
(format 1 today; nothing writes it yet) and a registry of every event
type this version reads, tested against the replay and projection arms.

Review fixes (P0 review, 2026-10-16):
- A line's own hash is checked only by the next line's prev, and the
  gate used to refuse a line before reading the next: a flipped bit in a
  middle line's type (or a format_set edited from 1 to 2) was reported
  as "newer, the books are not damaged". Now the reader settles every
  refusal against the next line (format::settle): a next line that
  doesn't chain makes it corrupt; one that chains makes it newer and
  verified; with no next line the message says "newer or damaged; update,
  then run verify", never "not damaged" (NewerJournal.verified).
- A newer format number is judged newer before its features are parsed:
  a newer format may shape them differently.
- Snapshot treats a blank complete line as corrupt, as the writer does.
- docs/spec/rules.md B-27, which the code cites, now exists.

Round-2 review fixes (2026-10-16):
- The envelope {seq, ts, type, payload, prev} and its hash (SHA-256 of
  the canonical JSON) are frozen across formats (rules.md B-27); new
  fields go in payload. settle reads only the next line's prev, leniently,
  so a newer format's next line (a float, a string seq, keys of its own)
  is never called corrupt: no readable prev means unverified newer.
- A key beside the envelope's was silently dropped (and the chain then
  broke as "corrupt"): it is now NewerJournal, hashed over the raw
  canonical JSON so the next line still checks it; JournalLine::parse
  refuses such a line.
- "the books are not damaged" overclaimed: a verified refusal says line N
  is intact and this version can't check the lines after it.
- The writer read the journal with BufRead::lines (invalid UTF-8 was an
  I/O error); it now splits bytes on newlines as the Snapshot does, so a
  non-UTF-8 line is JournalCorrupt in both.
- Store::open truncated a torn tail before the gate ran, altering newer
  books (perhaps mid-append) before refusing them. The gate now runs over
  the complete lines first; the tail is set aside (and journaled) only
  when they pass.
- Mutation testing (docs/testing.md, P0): the viewer and the writer say
  the same words about the same damaged line (the newline is no part of
  it); read_journal_and_format returns every line and the format in
  force; the journal page tells a format declaration.
Plan: plan/served.md P0 and 5.1.

Review round 3 (S6, S7). Store::open replays every complete line in
memory (the first line's currency, then the ledger) before a cut-off
last line is set aside, so a complete line the books can't apply
refuses the open with the journal byte for byte as it was and nothing
set aside (B-27 SCENARIO 3; test
a_bad_complete_line_before_a_torn_tail_leaves_the_journal_as_it_was);
catch_up now only projects. The viewer
applies the writer's first-line rule (created_currency, shared) instead
of guessing USD: any other first line, or a store.created without a
sound base_currency, is corrupt to both (test
the_first_line_is_store_created_for_the_viewer_and_the_writer). fuzz_store_replay varies the
first line (14,696 runs clean).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01T3gXMVt7XJPi6ens1or9Dj
2026-10-17 00:32:42 -06:00

119 lines
6.0 KiB
Rust

// Fuzz target: validly chained journals with fuzz-chosen events.
//
// Raw bytes (fuzz_store_open) almost never pass the hash chain, so they
// never reach replay. Here the journal is written by the real
// JournalWriter (whole lines, gapless seq, correct prev), starting with a
// real store.created most of the time (else a store.created with a
// fuzz-chosen currency or payload, or another type: the writer and the
// viewer share one rule for the first line); the events after it are
// fuzz-chosen types with payloads built from the field names replay
// reads. Oracles:
// 1. Opening never panics or hangs (an Err is fine: bad facts are
// refused).
// 2. Books that open must verify (live ledger = replay = projection).
// 3. The viewer (Snapshot::load) accepts exactly the journals the writer
// does, and then holds the same ledger.
// 4. When both refuse, they agree on whether the books are newer
// (NewerJournal: a type or format this version doesn't read) or bad.
#![no_main]
use arbitrary::{Arbitrary, Unstructured};
use libfuzzer_sys::fuzz_target;
use serde_json::{Map, Value, json};
const TYPES: &[&str] = &[
"account.added", "account.tax_set", "asset.depreciation_posted", "asset.disposed",
"asset.registered", "attachment.withdrawn", "bill.created", "bill.paid", "class.added",
"customer.added", "document.filed", "entry.created", "entry.line_added",
"entry.line_class_set", "entry.posted", "entry.reversed", "entry.vendor_set",
"fx.rate_set", "invoice.created", "invoice.payment_received", "journal.tail_repaired",
"line.cleared", "payroll.run", "period.closed", "period.created", "receipt.attached",
"receipt.linked", "receipt.unlinked", "recurring.generated", "recurring.registered",
"schedule.posted", "schedule.registered", "vendor.1099_set", "vendor.added",
"journal.format_set", "settings.set",
];
const FIELDS: &[&str] = &[
"account", "account_id", "accum_depr_account", "amount", "asset_id", "attachment_id",
"balance_account", "bill_id", "category", "class_id", "cleared", "close_to",
"closing_entry", "code", "cost", "cost_account", "currency", "customer_id", "date",
"deductible_percent", "description", "due_date", "employee_name", "end", "entry",
"entry_id", "expense_account", "file_name", "foreign_amount", "foreign_currency", "id",
"into", "invoice_id", "line_index", "lines", "method", "mime_type", "month", "months",
"name", "parent", "period", "pl_account", "placeholder", "proceeds", "purchase_date",
"rate", "reason", "recurring_id", "reversal_of", "reverses", "salvage_value",
"schedule_id", "share", "side", "size_bytes", "start", "tax_category", "tin_on_file",
"total", "useful_life_months", "vendor_id", "type", "account_type", "rounding",
"members", "distributions", "sweep", "format", "features",
];
/// A value replay might plausibly see: small ids, amounts as decimal
/// strings, dates, sides, currencies, account types, nested objects.
fn value(u: &mut Unstructured, depth: u32) -> arbitrary::Result<Value> {
Ok(match u.int_in_range(0..=11)? {
0 => json!(u.int_in_range(0u64..=6)?),
1 => json!(format!("{}.{:02}", u.int_in_range(0u32..=5000)?, u.int_in_range(0u32..=99)?)),
2 => json!(format!("2026-{:02}-{:02}", u.int_in_range(1u32..=12)?, u.int_in_range(1u32..=28)?)),
3 => json!(*u.choose(&["debit", "credit"])?),
4 => json!(*u.choose(&["USD", "EUR", "JPY", "XYZ"])?),
5 => json!(*u.choose(&["asset", "liability", "equity", "revenue", "expense"])?),
6 => json!(bool::arbitrary(u)?),
7 => Value::Null,
8 => json!(*u.choose(&["1000", "2000", "3000", "4000", "6000", "x"])?),
9 if depth < 2 => {
let n = u.int_in_range(0..=3)?;
Value::Array((0..n).map(|_| object(u, depth + 1)).collect::<arbitrary::Result<_>>()?)
}
10 if depth < 2 => object(u, depth + 1)?,
_ => json!(String::arbitrary(u)?),
})
}
fn object(u: &mut Unstructured, depth: u32) -> arbitrary::Result<Value> {
let mut m = Map::new();
for _ in 0..u.int_in_range(0..=7)? {
m.insert(u.choose(FIELDS)?.to_string(), value(u, depth)?);
}
Ok(Value::Object(m))
}
fuzz_target!(|data: &[u8]| {
let mut u = Unstructured::new(data);
let dir = std::env::temp_dir().join(format!("wb-fuzz-replay-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
{
let mut w = ledger_store::JournalWriter::open(&dir.join(ledger_store::JOURNAL_FILE)).unwrap();
let (first, payload) = match u.int_in_range(0..=7).unwrap_or(0) {
0 => ("store.created", object(&mut u, 0).unwrap_or_default()),
1 => (*u.choose(TYPES).unwrap_or(&"account.added"), object(&mut u, 0).unwrap_or_default()),
2 => ("store.created", json!({"base_currency": value(&mut u, 0).unwrap_or_default()})),
_ => ("store.created", json!({"base_currency": "USD"})),
};
w.append(first, payload).unwrap();
for _ in 0..u.int_in_range(0..=24).unwrap_or(0) {
let (Ok(t), Ok(p)) = (u.choose(TYPES), object(&mut u, 0)) else { break };
w.append(t, p).unwrap();
}
}
let store = ledger_store::Store::open(&dir);
let snap = ledger_store::Snapshot::load(&dir);
match (store, snap) {
(Ok(store), Ok(snap)) => {
store.verify().expect("books that open must verify");
assert!(
store.ledger().differs_from(snap.ledger()).is_empty(),
"the viewer and the writer read different books"
);
}
(Err(a), Err(b)) => {
let newer = |e: &ledger_store::StoreError| {
matches!(e, ledger_store::StoreError::NewerJournal { .. })
};
assert_eq!(newer(&a), newer(&b), "the writer says {a}; the viewer says {b}");
}
(Ok(_), Err(e)) => panic!("the writer opens books the viewer refuses: {e}"),
(Err(e), Ok(_)) => panic!("the viewer loads books the writer refuses: {e}"),
}
});