diff --git a/Cargo.lock b/Cargo.lock index 28c59744..a163f3d6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -703,7 +703,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" dependencies = [ "generic-array", - "rand_core", + "rand_core 0.6.4", "typenum", ] @@ -3965,6 +3965,31 @@ dependencies = [ "parking_lot", ] +[[package]] +name = "proptest" +version = "1.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b45fcc2344c680f5025fe57779faef368840d0bd1f42f216291f0dc4ace4744" +dependencies = [ + "bit-set", + "bit-vec 0.8.0", + "bitflags 2.13.0", + "num-traits", + "rand", + "rand_chacha 0.9.0", + "rand_xorshift", + "regex-syntax", + "rusty-fork", + "tempfile", + "unarray", +] + +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quick-xml" version = "0.41.0" @@ -3995,6 +4020,16 @@ version = "6.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf" +[[package]] +name = "rand" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ef1d0d795eb7d84685bca4f72f3649f064e6641543d3a8c415898726a57b41" +dependencies = [ + "rand_chacha 0.9.0", + "rand_core 0.9.5", +] + [[package]] name = "rand_chacha" version = "0.3.1" @@ -4002,7 +4037,17 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.6.4", +] + +[[package]] +name = "rand_chacha" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3022b5f1df60f26e1ffddd6c66e8aa15de382ae63b3a0c1bfc0e4d3e3f325cb" +dependencies = [ + "ppv-lite86", + "rand_core 0.9.5", ] [[package]] @@ -4014,6 +4059,24 @@ dependencies = [ "getrandom 0.2.17", ] +[[package]] +name = "rand_core" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "76afc826de14238e6e8c374ddcc1fa19e374fd8dd986b0d2af0d02377261d83c" +dependencies = [ + "getrandom 0.3.4", +] + +[[package]] +name = "rand_xorshift" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "513962919efc330f829edb2535844d1b912b0fbe2ca165d613e4e8788bb05a5a" +dependencies = [ + "rand_core 0.9.5", +] + [[package]] name = "raw-window-handle" version = "0.6.2" @@ -4253,6 +4316,18 @@ version = "1.0.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b39cdef0fa800fc44525c84ccb54a029961a8215f9619753635a9c0d2538d46d" +[[package]] +name = "rusty-fork" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cc6bf79ff24e648f6da1f8d1f011e9cac26491b619e6b9280f2b47f1774e6ee2" +dependencies = [ + "fnv", + "quick-error", + "tempfile", + "wait-timeout", +] + [[package]] name = "same-file" version = "1.0.6" @@ -4336,6 +4411,7 @@ version = "0.1.0" dependencies = [ "blake3", "hex", + "proptest", "thiserror 2.0.18", "zstd", ] @@ -4349,8 +4425,8 @@ dependencies = [ "ed25519-dalek", "hex", "hkdf", - "rand_chacha", - "rand_core", + "rand_chacha 0.3.1", + "rand_core 0.6.4", "scl-core", "sha2 0.10.9", "thiserror 2.0.18", @@ -5594,6 +5670,12 @@ dependencies = [ "arrayvec", ] +[[package]] +name = "unarray" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eaea85b334db583fe3274d12b4cd1880032beab409c0d774be044d4480ab9a94" + [[package]] name = "unic-char-property" version = "0.9.0" @@ -5759,6 +5841,15 @@ dependencies = [ "libc", ] +[[package]] +name = "wait-timeout" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09ac3b126d3914f9849036f826e054cbabdc8519970b8998ddaf3b5bd3c65f11" +dependencies = [ + "libc", +] + [[package]] name = "walkdir" version = "2.5.0" @@ -6515,7 +6606,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c7e468321c81fb07fa7f4c636c3972b9100f0346e5b6a9f2bd0603a52f7ed277" dependencies = [ "curve25519-dalek 4.1.3", - "rand_core", + "rand_core 0.6.4", "serde", "zeroize", ] diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 43cf7505..9299ca40 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -13,3 +13,6 @@ zstd = "0.13.3" [lints] workspace = true + +[dev-dependencies] +proptest = "1.11.0" diff --git a/crates/core/src/object.rs b/crates/core/src/object.rs index 16f38992..5505b0e0 100644 --- a/crates/core/src/object.rs +++ b/crates/core/src/object.rs @@ -1239,3 +1239,231 @@ mod tests { assert!(perms & PROTECTED != 0 && perms & RANDOMIZED != 0); } } + +/// Property tests for the canonical encoding — the content-addressing +/// invariant (same logical content ⇒ same id on any machine) and decode +/// totality on attacker-supplied bytes. `encode` canonicalizes (sorts +/// protection registers, manifest anchors/closure), so the load-bearing +/// property is encode-stability through a decode round trip, not strict +/// struct equality. +#[cfg(test)] +mod prop_tests { + use super::*; + use proptest::prelude::*; + + fn arb_id() -> impl Strategy { + any::<[u8; 32]>().prop_map(ObjectId::from_bytes) + } + + fn arb_wrapped_key() -> impl Strategy { + (".{0,16}", proptest::collection::vec(any::(), 0..64)).prop_map( + |(recipient_id, wrapped_dek)| WrappedKey { + recipient_id, + wrapped_dek, + }, + ) + } + + fn arb_tree() -> impl Strategy { + proptest::collection::vec( + ( + ".{0,12}", + any::(), + arb_id(), + any::(), + any::(), + ), + 0..8, + ) + .prop_map(|es| { + Tree::new( + es.into_iter() + .map(|(name, is_tree, id, mode, perms)| TreeEntry { + name, + kind: if is_tree { + EntryKind::Tree + } else { + EntryKind::Blob + }, + id, + mode: FileMode(mode), + perms, + }) + .collect(), + ) + }) + } + + fn arb_protection() -> impl Strategy { + ( + proptest::collection::vec( + ( + ".{0,12}", + proptest::collection::vec( + (any::<[u8; 32]>(), any::(), any::()), + 0..4, + ), + ), + 0..4, + ), + proptest::collection::btree_map( + arb_id(), + proptest::collection::vec(arb_wrapped_key(), 0..3), + 0..4, + ), + ) + .prop_map(|(prefixes, wrapped)| Protection { + prefixes: prefixes + .into_iter() + .map(|(prefix, recipients)| ProtectPrefix { + prefix, + recipients: recipients + .into_iter() + .map(|(key, epoch, granted)| RecipientEntry { + key, + epoch, + state: if granted { + RecipientState::Granted + } else { + RecipientState::Revoked + }, + }) + .collect(), + }) + .collect(), + wrapped, + }) + } + + fn arb_snapshot() -> impl Strategy { + ( + arb_id(), + proptest::collection::vec(arb_id(), 0..4), + ".{0,20}", + any::(), + ".{0,40}", + proptest::collection::btree_map(".{0,12}", arb_id(), 0..4), + arb_protection(), + ) + .prop_map( + |(root, parents, author, timestamp, message, secrets, protection)| Snapshot { + root, + parents, + author, + timestamp, + message, + secrets, + protection, + }, + ) + } + + fn arb_manifest() -> impl Strategy { + ( + arb_id(), + proptest::option::of(arb_id()), + proptest::collection::vec(arb_id(), 0..4), + proptest::collection::vec(arb_id(), 0..6), + proptest::collection::vec(any::(), 0..64), + proptest::collection::vec(arb_wrapped_key(), 0..3), + ) + .prop_map(|(base, prev, anchors, closure, index_ct, kek_wraps)| { + BranchManifest { + base, + prev, + anchors, + closure, + index_ct, + kek_wraps, + } + }) + } + + fn arb_object() -> impl Strategy { + prop_oneof![ + proptest::collection::vec(any::(), 0..256).prop_map(Object::blob), + arb_tree().prop_map(Object::Tree), + arb_snapshot().prop_map(Object::Snapshot), + ( + ".{0,16}", + proptest::collection::vec(any::(), 0..32), + proptest::collection::vec(any::(), 0..128), + proptest::collection::vec(arb_wrapped_key(), 0..3), + ) + .prop_map(|(name, nonce, ciphertext, wrapped_keys)| { + Object::Secret(Secret { + name, + nonce, + ciphertext, + wrapped_keys, + }) + }), + (arb_id(), any::<[u8; 32]>(), any::<[u8; 64]>()).prop_map(|(snapshot, signer, sig)| { + Object::Signature(SignatureObj { + snapshot, + signer, + sig, + }) + }), + ( + arb_id(), + ".{0,12}", + ".{0,12}", + proptest::collection::vec(any::(), 0..32), + proptest::collection::vec(any::(), 0..128), + proptest::collection::vec(arb_wrapped_key(), 0..3), + ) + .prop_map( + |(snapshot, agent, session, nonce, ciphertext, wrapped_keys)| { + Object::Transcript(Transcript { + snapshot, + agent, + session, + nonce, + ciphertext, + wrapped_keys, + }) + } + ), + proptest::collection::vec(any::(), 0..256).prop_map(|p| Object::Sealed( + SealedObj { + payload: Arc::from(p.into_boxed_slice()), + } + )), + arb_manifest().prop_map(Object::Manifest), + ] + } + + proptest! { + /// decode(encode(o)) succeeds, re-encodes to the identical bytes, and + /// keeps the content address — the core invariant in CLAUDE.md. + #[test] + fn decode_of_own_encoding_is_id_stable(o in arb_object()) { + let bytes = o.encode(); + let decoded = Object::decode(&bytes).expect("own encoding must decode"); + prop_assert_eq!(decoded.encode(), bytes); + prop_assert_eq!(decoded.id(), o.id()); + } + + /// decode is total on arbitrary bytes: Ok or Err, never a panic — + /// this is the path attacker-supplied wire/pack data reaches. + #[test] + fn decode_never_panics_on_arbitrary_bytes( + bytes in proptest::collection::vec(any::(), 0..2048) + ) { + let _ = Object::decode(&bytes); + } + + /// decode is total on truncations of valid encodings — the malformed + /// input most likely to occur in practice (cut-off transfers). + #[test] + fn decode_never_panics_on_truncated_encodings( + o in arb_object(), + cut in any::() + ) { + let bytes = o.encode(); + let cut = cut.index(bytes.len() + 1); + let _ = Object::decode(&bytes[..cut.min(bytes.len())]); + } + } +}