Repository navigation
feat(repo): deliver drop tombstones after gc and after late signer trust #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
7206376
fd9494c
4fdc63c
20eeae7
722c475
f83412f
6fc997b
364c74c
ccdf3a8
acb618d
d066dbb
05b5114
9019ff4
ea45995
e9aae44
b19cabf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -145,13 +145,45 @@ pub(crate) fn index_incoming( | |
| let mut count = 0; | ||
| for id in ids { | ||
| if let Object::Signature(sig) = store.get(id)? { | ||
| append_index(layout, sig.snapshot, *id)?; | ||
| count += 1; | ||
| // Only a signature that checks out for a target this store holds | ||
| // is indexed (and so forwarded and kept by gc): nothing forged or | ||
| // dangling can crowd out a real one (#230, ADR-0038). | ||
| if acceptable(store, &sig)? { | ||
| append_index(layout, sig.snapshot, *id)?; | ||
| count += 1; | ||
| } | ||
| } | ||
| } | ||
| Ok(count) | ||
| } | ||
|
|
||
| /// Whether `sig` may enter the index: its target is held, and the signature | ||
| /// verifies under the key it names in the domain its target kind uses. A | ||
| /// snapshot takes the snapshot domain; a transcript the transcript domain; a | ||
| /// drop tombstone only the domain-separated drop digest (a plain signature | ||
| /// over a tombstone id never authorizes a drop). Any other target kind is | ||
| /// refused. Trust is not judged here: that is the reader's call. | ||
| pub(crate) fn acceptable(store: &mut scl_core::Store, sig: &SignatureObj) -> Result<bool> { | ||
| if !store.contains(&sig.snapshot) { | ||
| return Ok(false); | ||
| } | ||
|
Comment on lines
+167
to
+169
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a valid signature reaches a push-only remote before its target, this early return leaves the signature object in the CAS but out of the index. A later normal push sends the target but skips the signature because Useful? React with 👍 / 👎. |
||
| let target = sig.snapshot; | ||
| Ok(match store.get(&target)? { | ||
| Object::Snapshot(_) => { | ||
| scl_crypto::verify_snapshot_sig(&sig.signer, target.as_bytes(), &sig.sig) | ||
| } | ||
| Object::Transcript(t) if t.dropped_id().is_some() => scl_crypto::verify_transcript_sig( | ||
| &sig.signer, | ||
| &crate::transcripts::drop_digest(&target), | ||
| &sig.sig, | ||
| ), | ||
| Object::Transcript(_) => { | ||
| scl_crypto::verify_transcript_sig(&sig.signer, target.as_bytes(), &sig.sig) | ||
| } | ||
| _ => false, | ||
| }) | ||
| } | ||
|
|
||
| /// Full-store rebuild of the signature index (Task 3): scan every object the | ||
| /// store can resolve (loose + packed — `Store::all_ids`, not a reachability | ||
| /// walk, since a `SignatureObj` is a leaf no tree/snapshot references), keep | ||
|
|
@@ -171,7 +203,7 @@ pub(crate) fn reindex(layout: &Layout, store: &mut scl_core::Store) -> Result<us | |
| let mut entries: Vec<(ObjectId, ObjectId)> = Vec::new(); | ||
| for id in store.all_ids()? { | ||
| if let Object::Signature(sig) = store.get(&id)? { | ||
| if store.contains(&sig.snapshot) { | ||
| if acceptable(store, &sig)? { | ||
| entries.push((sig.snapshot, id)); | ||
| } | ||
| } | ||
|
|
@@ -195,6 +227,26 @@ pub(crate) fn index_by_target( | |
| Ok(out) | ||
| } | ||
|
|
||
| /// [`index_by_target`] restricted to `targets`, keeping at most `per_target` | ||
| /// signature ids each (index order). Only the wanted entries are retained, so | ||
| /// a large index costs a scan, not memory. | ||
| pub(crate) fn index_for_targets( | ||
| layout: &Layout, | ||
| targets: &BTreeSet<ObjectId>, | ||
| per_target: usize, | ||
| ) -> Result<std::collections::HashMap<ObjectId, Vec<ObjectId>>> { | ||
| let mut out: std::collections::HashMap<ObjectId, Vec<ObjectId>> = Default::default(); | ||
| for (target, sig_id) in read_index(layout)? { | ||
| if targets.contains(&target) { | ||
| let v = out.entry(target).or_default(); | ||
| if v.len() < per_target { | ||
| v.push(sig_id); | ||
| } | ||
| } | ||
| } | ||
| Ok(out) | ||
| } | ||
|
|
||
| /// Sender-side seam (Task 3): the indexed signature object ids covering any | ||
| /// of `snapshots`, deduped and sorted — the set a push/export needs to | ||
| /// carry alongside the snapshots themselves so the receiving repo's index | ||
|
|
@@ -671,4 +723,88 @@ mod tests { | |
| drop(repo); | ||
| std::fs::remove_dir_all(&root).unwrap(); | ||
| } | ||
|
|
||
| /// Write `objs` as a pack under `repo` and ingest it like a transfer. | ||
| fn ingest(repo: &Repo, objs: &[Object]) { | ||
| let enc: Vec<(ObjectId, Vec<u8>)> = objs.iter().map(|o| (o.id(), o.encode())).collect(); | ||
| let (pack, _idx) = scl_core::pack::build_pack(&enc).unwrap(); | ||
| let path = repo.layout().dot_sc.join("incoming.pack"); | ||
| std::fs::write(&path, pack).unwrap(); | ||
| let arc = repo.store_arc(); | ||
| let mut store = arc.lock().unwrap(); | ||
| crate::transport::ingest_pack_file(repo.layout(), &mut store, &path).unwrap(); | ||
| std::fs::remove_file(&path).unwrap(); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_signature_sorting_below_its_snapshot_in_the_pack_is_still_indexed() { | ||
| // The pack is ordered by id, so the signature can come first; every | ||
| // object is written before indexing, so its target is held anyway. | ||
| let a_root = tmp_root("order-a"); | ||
| let b_root = tmp_root("order-b"); | ||
| let a = Repo::init(&a_root).unwrap(); | ||
| let (_s, identity) = scl_crypto::generate_identity_v2(); | ||
| let key = identity.signing.as_ref().unwrap(); | ||
| let (snap, snap_obj, sig_obj) = loop { | ||
| std::fs::write(a_root.join("f"), format!("{:?}", std::time::Instant::now())).unwrap(); | ||
| let snap = a.commit("t", "c").unwrap(); | ||
| let sig_obj = Object::Signature(SignatureObj { | ||
| snapshot: snap, | ||
| signer: key.public().to_bytes(), | ||
| sig: scl_crypto::sign_snapshot_id(key, snap.as_bytes()), | ||
| }); | ||
| if sig_obj.id() < snap { | ||
| let snap_obj = a.store_arc().lock().unwrap().get(&snap).unwrap(); | ||
| break (snap, snap_obj, sig_obj); | ||
| } | ||
| }; | ||
| let b = Repo::init(&b_root).unwrap(); | ||
| let mut objs = [snap_obj, sig_obj.clone()]; | ||
| objs.sort_by_key(Object::id); | ||
| assert_eq!(objs[0].id(), sig_obj.id(), "the signature leads the pack"); | ||
| ingest(&b, &objs); | ||
| let mut trust = HashMap::new(); | ||
| trust.insert(key.public().to_bytes(), "alice".to_string()); | ||
| assert_eq!( | ||
| b.sig_status(&snap, &trust).unwrap(), | ||
| SigStatus::Trusted("alice".to_string()) | ||
| ); | ||
| drop((a, b)); | ||
| std::fs::remove_dir_all(&a_root).unwrap(); | ||
| std::fs::remove_dir_all(&b_root).unwrap(); | ||
| assert!(!a_root.exists() && !b_root.exists()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn a_forged_snapshot_signature_over_the_wire_leaves_the_snapshot_unsigned() { | ||
| // Pins the ingest filter: a received forgery is not indexed, so it | ||
| // cannot turn a snapshot Invalid (ADR-0038, #230). | ||
| let a_root = tmp_root("forge-a"); | ||
| let b_root = tmp_root("forge-b"); | ||
| let a = Repo::init(&a_root).unwrap(); | ||
| std::fs::write(a_root.join("f"), b"x").unwrap(); | ||
| let snap = a.commit("t", "c").unwrap(); | ||
| let snap_obj = a.store_arc().lock().unwrap().get(&snap).unwrap(); | ||
| let (_s, identity) = scl_crypto::generate_identity_v2(); | ||
| let signer = identity.signing.as_ref().unwrap().public().to_bytes(); | ||
| let forged = Object::Signature(SignatureObj { | ||
| snapshot: snap, | ||
| signer, | ||
| sig: [9u8; 64], | ||
| }); | ||
| let b = Repo::init(&b_root).unwrap(); | ||
| ingest(&b, &[snap_obj, forged.clone()]); | ||
| let mut trust = HashMap::new(); | ||
| trust.insert(signer, "alice".to_string()); | ||
| assert_eq!(b.sig_status(&snap, &trust).unwrap(), SigStatus::Unsigned); | ||
| assert!(b.signatures_for(&snap).unwrap().is_empty()); | ||
| assert!( | ||
| b.store_arc().lock().unwrap().contains(&forged.id()), | ||
| "the object is stored, only the index refuses it" | ||
| ); | ||
| drop((a, b)); | ||
| std::fs::remove_dir_all(&a_root).unwrap(); | ||
| std::fs::remove_dir_all(&b_root).unwrap(); | ||
| assert!(!a_root.exists() && !b_root.exists()); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On any writable transport, a peer can submit a large pack of distinct invalid
Signatureobjects targeting one held snapshot; this callsacceptableand performs an Ed25519 verification for every record with no batch-level work cap. Because the default accepted pack size is 16 GiB, a comparatively modest signature-only pack can monopolize a server handler with hundreds of thousands of serial cryptographic checks, and concurrent connections can exhaust all handlers. Apply a total signature-verification budget per ingest rather than verifying every attacker-supplied signature.Useful? React with 👍 / 👎.