From cbedaf92ea9ceccc7d51283e3aeebe9f74fb8d27 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:25:22 +0200 Subject: [PATCH 1/8] fix(transport): enforce fast-forward and ref target kind on the server UpdateRef checked only compare-and-swap; the fast-forward check lived in the pushing client, so any writer could rewind or sideways-move a served branch or point it at an absent object or a blob. LocalTransport and BucketTransport (so `sc serve --store` too) now refuse, under their own lock, a target that is not a held snapshot or manifest, and a move whose current tip is not an ancestor of the new one. A walk that hits an absent object fails closed. --- crates/repo/src/bucket_transport.rs | 122 ++++++++++++++++++++++++++-- crates/repo/src/merge.rs | 55 ++++++++++--- crates/repo/src/private.rs | 36 ++++++++ crates/repo/src/test_support.rs | 15 ++++ crates/repo/src/transport.rs | 92 +++++++++++++++++++-- crates/repo/src/wire.rs | 33 +++++++- docs/THREAT-MODEL.md | 15 +++- 7 files changed, 341 insertions(+), 27 deletions(-) diff --git a/crates/repo/src/bucket_transport.rs b/crates/repo/src/bucket_transport.rs index 11db2d02..d5faa92b 100644 --- a/crates/repo/src/bucket_transport.rs +++ b/crates/repo/src/bucket_transport.rs @@ -74,6 +74,12 @@ pub struct BucketTransport { /// (possibly long-lived, e.g. `wire::serve`-hosted) transport regardless /// of whether this call succeeds or fails. pending_packs: RefCell>, + /// Parents of every snapshot and manifest received since the last + /// `update_ref`, which is not yet on the manifest chain and so not in + /// `view.index`; lets `update_ref` check fast-forward over objects that + /// are only in a pending pack. Taken, like `pending_packs`, at the top of + /// each `update_ref`. + pending_commits: RefCell>>, /// Signature and transcript objects found in each scanned pack, keyed by /// pack hash (#229). Packs are immutable, so an entry never goes stale; /// it spares a long-lived `serve --store` session from re-reading every @@ -233,6 +239,7 @@ impl BucketTransport { pack_cache: RefCell::new(None), staged: RefCell::new(Vec::new()), pending_packs: RefCell::new(Vec::new()), + pending_commits: RefCell::new(BTreeMap::new()), leaf_cache: RefCell::new(BTreeMap::new()), saw_absent: Cell::new(false), attempt_tag: RefCell::new(None), @@ -675,6 +682,17 @@ impl crate::reachable::ObjectSource for BucketSource<'_> { } } +/// The parents of `obj` if it is a snapshot or a manifest (what `update_ref` +/// may point a branch at), else `None`. +fn commit_parents_of(id: &ObjectId, obj: &Object) -> Option> { + match obj { + Object::Snapshot(_) | Object::Manifest(_) => { + crate::merge::commit_parents(id, obj.clone()).ok() + } + _ => None, + } +} + /// Attempts `update_ref` makes at the manifest CAS before it gives up (the /// documented "rerun the push" outcome). const MAX_CAS_RETRIES: u32 = 16; @@ -775,6 +793,9 @@ impl Transport for BucketTransport { if ObjectId::of(bytes) != *id { return Err(Error::CorruptObject(*id)); } + if let Some(parents) = commit_parents_of(id, &Object::decode(bytes)?) { + self.pending_commits.borrow_mut().insert(*id, parents); + } self.staged.borrow_mut().push((*id, bytes.to_vec())); Ok(()) } @@ -787,12 +808,18 @@ impl Transport for BucketTransport { // pinned byte-stable for a given object set (core), so the rebuilt // pack still content-addresses identically for identical input. let mut objects: Vec<(ObjectId, Vec)> = Vec::new(); + let mut commits = BTreeMap::new(); scl_core::pack::parse_pack_reader(src, |id, obj| { + if let Some(parents) = commit_parents_of(&id, &obj) { + commits.insert(id, parents); + } objects.push((id, obj.encode())); Ok(()) })?; let (hash, ids) = self.upload_pack(&objects)?; self.pending_packs.borrow_mut().push(hash); + // Only after the upload: a failed one must not leave ids that no pack holds. + self.pending_commits.borrow_mut().extend(commits); Ok(ids) } @@ -821,6 +848,7 @@ impl Transport for BucketTransport { // the bucket regardless (content-addressed, inert orphan garbage if // never referenced — correct per spec; `compact` sweeps it later, ADR-0057). let pending: Vec = std::mem::take(&mut *self.pending_packs.borrow_mut()); + let pending_commits = std::mem::take(&mut *self.pending_commits.borrow_mut()); for attempt in 0..MAX_CAS_RETRIES { if attempt > 0 { std::thread::sleep(cas_backoff(attempt)); @@ -848,6 +876,27 @@ impl Transport for BucketTransport { if current.as_ref() != expected_old { return Err(Error::NonFastForward); } + // The pushing client checks fast-forward too, but a writer need not + // be our client (`sc serve --store`, or a direct bucket writer): + // refuse a rewind, a sideways move, or a target that is absent or + // not a snapshot or manifest. Objects only in a pending pack are + // not in the view yet, so their parents come from `pending_commits`. + crate::merge::check_ref_move(current, *id, |o| { + if let Some(parents) = pending_commits.get(o) { + return Ok(parents.clone()); + } + let on_chain = self + .view + .borrow() + .as_ref() + .is_some_and(|v| v.index.contains_key(o)); + if !on_chain { + return Err(Error::InvalidArgument(format!( + "object {o} is not a snapshot or manifest held here, so the ref update cannot be verified" + ))); + } + crate::merge::commit_parents(o, Object::decode(&self.object_bytes(o)?)?) + })?; // Claim a log slot for this attempt. `put_new` on `log/` is // the claim: if another writer already landed that seq, we didn't // win it and try the next one — the entry we didn't win becomes @@ -932,6 +981,7 @@ mod tests { } use super::*; + use crate::test_support::fake_commit; use crate::transport::Transport; use crate::walfmt::{ checkpoint_key, idx_key, log_key, pack_key, Checkpoint, LogEntry, Manifest, RefUpdate, @@ -1179,6 +1229,64 @@ mod tests { assert!(!broot.exists()); } + /// `sc serve --store` and direct bucket writers get the same server-side + /// rule as `LocalTransport`: no rewind, no sideways move, and a target that + /// is a held snapshot (including one only in the pending pack). + #[test] + fn update_ref_enforces_fast_forward_and_target_kind() { + let broot = std::env::temp_dir().join(format!("scl-bt-ff-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&broot); + let t = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); + let put = |c: &Object| t.put_object(&c.id(), &c.encode()).unwrap(); + let c1 = fake_commit("c1", None); + let c2 = fake_commit("c2", Some(c1.id())); + let c3 = fake_commit("c3", Some(c2.id())); + let unrelated = fake_commit("unrelated", None); + let blob = Object::blob(b"a blob".to_vec()); + let absent = ObjectId::of(b"never stored"); + + // Creation, then a two-step fast-forward whose objects are all pending. + put(&c1); + t.update_ref("main", &c1.id(), None).unwrap(); + put(&c2); + put(&c3); + t.update_ref("main", &c3.id(), Some(&c1.id())).unwrap(); + let tip_is = |want: ObjectId| { + assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), want)]); + }; + tip_is(c3.id()); + + // Each refused call takes the pending pack with it, so stage the + // pending-only objects (an unrelated snapshot, a blob) again before it. + let stage = || { + put(&unrelated); + put(&blob); + }; + // Rewind and sideways moves: on-chain targets and a pending one. + for target in [c1.id(), c2.id(), unrelated.id()] { + stage(); + assert!( + matches!( + t.update_ref("main", &target, Some(&c3.id())), + Err(Error::NonFastForward) + ), + "{target} must be refused" + ); + tip_is(c3.id()); + } + // Absent and wrong-kind targets, for an existing branch and a new one. + for branch in ["main", "fresh"] { + let old = (branch == "main").then(|| c3.id()); + for target in [absent, blob.id()] { + stage(); + assert!(t.update_ref(branch, &target, old.as_ref()).is_err()); + } + } + tip_is(c3.id()); + drop(t); + std::fs::remove_dir_all(&broot).unwrap(); + } + #[test] fn put_object_stages_and_update_ref_commits_them() { let broot = std::env::temp_dir().join(format!("scl-bt-stage-{}", std::process::id())); @@ -1348,7 +1456,7 @@ mod tests { // racer-b's thread is even scheduled, and the (1, 1) result below // would hold trivially with the manifest CAS never actually contended. let mk_tip = |tag: &[u8]| { - let obj = Object::blob(tag.to_vec()); // any distinct object works as a fake tip + let obj = fake_commit(std::str::from_utf8(tag).unwrap(), Some(base)); (obj.id(), obj.encode()) }; let gate = std::sync::Barrier::new(2); @@ -1428,7 +1536,7 @@ mod tests { DirBucket::open(&broot).unwrap(), )) .unwrap(); - let obj = Object::blob(format!("agent-{i}").into_bytes()); + let obj = fake_commit(&format!("agent-{i}"), None); t.put_object(&obj.id(), &obj.encode()).unwrap(); note(status, format!("writer-{i:03}"), "at the gate".into()); gate.wait(); @@ -1530,7 +1638,7 @@ mod tests { lost: AtomicU32::new(0), })) .unwrap(); - let obj = Object::blob(b"budget".to_vec()); + let obj = fake_commit("budget", None); t.put_object(&obj.id(), &obj.encode()).unwrap(); let res = t.update_ref("main", &obj.id(), None); if lands { @@ -1611,7 +1719,7 @@ mod tests { assert_eq!(t2.list_refs().unwrap(), vec![("main".to_string(), tip)]); assert!(!t2.has_object(&orphan_obj.id()).unwrap()); // …and a live push steps over the claimed seq 2 (lands at 3+) and works. - let next = Object::blob(b"after crash".to_vec()); + let next = fake_commit("after crash", None); t2.put_object(&next.id(), &next.encode()).unwrap(); t2.update_ref("recovered", &next.id(), None).unwrap(); let t3 = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); @@ -1933,7 +2041,7 @@ mod tests { .unwrap(); let t = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); - let obj = Object::blob(b"carry-push".to_vec()); + let obj = fake_commit("carry-push", None); let (tip, bytes) = (obj.id(), obj.encode()); t.put_object(&tip, &bytes).unwrap(); t.update_ref("carried", &tip, None).unwrap(); @@ -1960,7 +2068,7 @@ mod tests { let t = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); let n = CHECKPOINT_INTERVAL + 2; for i in 0..n { - let obj = Object::blob(format!("fold-{i}").into_bytes()); + let obj = fake_commit(&format!("fold-{i}"), None); t.put_object(&obj.id(), &obj.encode()).unwrap(); t.update_ref(&format!("w-{i}"), &obj.id(), None).unwrap(); } @@ -2037,7 +2145,7 @@ mod tests { ))) .unwrap(); for i in 0..(CHECKPOINT_INTERVAL + 2) { - let obj = Object::blob(format!("foldfail-{i}").into_bytes()); + let obj = fake_commit(&format!("foldfail-{i}"), None); t.put_object(&obj.id(), &obj.encode()).unwrap(); t.update_ref(&format!("w-{i}"), &obj.id(), None).unwrap(); // must all be Ok } diff --git a/crates/repo/src/merge.rs b/crates/repo/src/merge.rs index ba182550..133dbaad 100644 --- a/crates/repo/src/merge.rs +++ b/crates/repo/src/merge.rs @@ -57,6 +57,19 @@ pub fn is_ancestor(store: &mut Store, anc: ObjectId, desc: ObjectId) -> Result Result> { + match obj { + Object::Snapshot(s) => Ok(s.parents), + Object::Manifest(m) => Ok(m.prev.into_iter().chain(std::iter::once(m.base)).collect()), + other => Err(Error::BadRef(format!( + "expected snapshot or manifest at {id}, found {}", + other.kind_name() + ))), + } +} + /// All ancestors of `id`, inclusive. Kind-aware: snapshots contribute their /// parents, branch manifests their `prev` chain + public base. fn ancestors(store: &mut Store, id: ObjectId) -> Result> { @@ -65,17 +78,7 @@ fn ancestors(store: &mut Store, id: ObjectId) -> Result> { q.push_back(id); set.insert(id); while let Some(cur) = q.pop_front() { - let parents: Vec = match store.get(&cur)? { - Object::Snapshot(s) => s.parents, - Object::Manifest(m) => m.prev.into_iter().chain(std::iter::once(m.base)).collect(), - other => { - return Err(Error::BadRef(format!( - "expected snapshot or manifest at {cur}, found {}", - other.kind_name() - ))) - } - }; - for p in parents { + for p in commit_parents(&cur, store.get(&cur)?)? { if set.insert(p) { q.push_back(p); } @@ -84,6 +87,36 @@ fn ancestors(store: &mut Store, id: ObjectId) -> Result> { Ok(set) } +/// The rule every ref-serving [`crate::transport::Transport::update_ref`] +/// applies under its own lock, so a writer cannot rewind or sideways-move a +/// branch, or point one at an absent object or a non-commit: `new` must be a +/// snapshot or manifest, and when the branch exists (`current`) it must be an +/// ancestor of `new` (the same ancestry as [`is_ancestor`]). `parents_of` reads +/// one object's parents and must fail for an absent or wrong-kind object, so +/// an ancestry walk that hits a gap (partial clone) fails closed. +pub(crate) fn check_ref_move( + current: Option, + new: ObjectId, + mut parents_of: impl FnMut(&ObjectId) -> Result>, +) -> Result<()> { + let Some(cur) = current else { + return parents_of(&new).map(|_| ()); + }; + let mut seen = BTreeSet::from([new]); + let mut q = VecDeque::from([new]); + while let Some(id) = q.pop_front() { + for p in parents_of(&id)? { + if p == cur { + return Ok(()); + } + if seen.insert(p) { + q.push_back(p); + } + } + } + Err(Error::NonFastForward) +} + /// One merged file in a `FileMerge` working set. /// /// `bytes` carries ciphertext when a protected entry was resolved by diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index ad15349b..1fb234e9 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -2749,6 +2749,42 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); } + /// The receiving side enforces fast-forward over the manifest chain too + /// (`prev` plus base): a first push, a commit and a grant (each a new + /// manifest chained to the last) all land, and a rolled-back manifest is + /// refused by the server even when the caller names the right old tip. + #[test] + fn private_branch_pushes_fast_forward_on_the_server() { + use crate::transport::{LocalTransport, Transport}; + let (root, repo, (alice_sk, _), (_, bob_pk)) = setup("pushff"); + let dst = tmp_root("pushff-dst"); + drop(Repo::init(&dst).unwrap()); + repo.remote_add("origin", dst.to_str().unwrap()).unwrap(); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + let (m1, _) = repo.branch_manifest("fix").unwrap().unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + repo.push("origin").unwrap(); + + write(&root, "fix.txt", "v1\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + repo.push("origin").unwrap(); + repo.branch_grant("fix", &alice_sk, &bob_pk).unwrap(); + let (m3, _) = repo.branch_manifest("fix").unwrap().unwrap(); + repo.push("origin").unwrap(); + + let remote = LocalTransport::open(&dst).unwrap(); + assert_eq!(remote.list_refs().unwrap(), vec![("fix".to_string(), m3)]); + assert!(matches!( + remote.update_ref("fix", &m1, Some(&m3)), + Err(Error::NonFastForward) + )); + drop(remote); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + std::fs::remove_dir_all(&dst).unwrap(); + } + #[test] fn protected_paths_survive_a_private_round_trip() { let (root, repo, (alice_sk, alice_pk), _bob) = setup("protpath"); diff --git a/crates/repo/src/test_support.rs b/crates/repo/src/test_support.rs index 438354a0..76f21cde 100644 --- a/crates/repo/src/test_support.rs +++ b/crates/repo/src/test_support.rs @@ -56,6 +56,21 @@ pub(crate) fn watchdog<'a>( }); } +/// A tiny snapshot with no tree behind it, distinct per `tag`: a stand-in tip +/// for transport tests that move refs without building a repository. A +/// `parent` makes it a descendant, so a move to it is a fast-forward. +pub(crate) fn fake_commit(tag: &str, parent: Option) -> scl_core::Object { + scl_core::Object::Snapshot(scl_core::Snapshot { + root: scl_core::ObjectId::of(tag.as_bytes()), + parents: parent.into_iter().collect(), + author: "t".into(), + timestamp: 0, + message: tag.into(), + secrets: Default::default(), + protection: Default::default(), + }) +} + /// Windows: assert `path` carries exactly one explicit, non-inherited /// full-control ACE for the current user, read back with `icacls`. #[cfg(windows)] diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index b0c8d370..c4814427 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -747,6 +747,19 @@ impl Transport for LocalTransport { if current.as_ref() != expected_old { return Err(Error::NonFastForward); } + // The pushing client checks fast-forward too, but a writer need not be + // our client: enforce it here, under the lock. + { + let mut store = self.store.borrow_mut(); + crate::merge::check_ref_move(current, *id, |o| { + if !store.contains(o) { + return Err(Error::InvalidArgument(format!( + "object {o} is not a snapshot or manifest held here, so the ref update cannot be verified" + ))); + } + crate::merge::commit_parents(o, store.get(o)?) + })?; + } crate::refs::write_branch_tip(&self.layout, branch, id) } @@ -965,13 +978,76 @@ mod tests { Err(Error::CorruptObject(_)) )); - t.update_ref("main", &id, None).unwrap(); - assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), id)]); + let tip = put_commit(&t, "tip", None); + t.update_ref("main", &tip, None).unwrap(); + assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), tip)]); assert_eq!(t.head_branch().unwrap(), "main"); std::fs::remove_dir_all(&layout.root).unwrap(); } + /// Store a stand-in snapshot (see `fake_commit`) and return its id. + fn put_commit(t: &LocalTransport, tag: &str, parent: Option) -> ObjectId { + let o = crate::test_support::fake_commit(tag, parent); + t.put_object(&o.id(), &o.encode()).unwrap(); + o.id() + } + + /// The server, not the pushing client, enforces fast-forward and the + /// target's kind: a writer with a ref-update verb must not be able to + /// rewind or sideways-move a branch, or point it at something that is not + /// a held snapshot (which would break clone, fetch and gc for everyone). + #[test] + fn update_ref_enforces_fast_forward_and_target_kind() { + let layout = tmp_remote("ff"); + let t = LocalTransport::open(&layout.root).unwrap(); + let c1 = put_commit(&t, "c1", None); + let c2 = put_commit(&t, "c2", Some(c1)); + let c3 = put_commit(&t, "c3", Some(c2)); + let unrelated = put_commit(&t, "unrelated", None); + let blob = Object::blob(b"a blob".to_vec()); + t.put_object(&blob.id(), &blob.encode()).unwrap(); + let absent = Object::blob(b"never stored".to_vec()).id(); + let tip_is = |want: ObjectId| { + assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), want)]); + }; + + // Creation to a held snapshot; a fast-forward over more than one step. + t.update_ref("main", &c1, None).unwrap(); + t.update_ref("main", &c3, Some(&c1)).unwrap(); + tip_is(c3); + + // Rewind and sideways moves are refused even with a correct + // expected_old, and nothing moves. + for target in [c1, c2, unrelated] { + assert!( + matches!( + t.update_ref("main", &target, Some(&c3)), + Err(Error::NonFastForward) + ), + "{target} must be refused" + ); + tip_is(c3); + } + + // An absent object, a blob, and a blob as a branch creation. + assert!(t.update_ref("main", &absent, Some(&c3)).is_err()); + assert!(matches!( + t.update_ref("main", &blob.id(), Some(&c3)), + Err(Error::BadRef(_)) + )); + assert!(t.update_ref("fresh", &absent, None).is_err()); + assert!(matches!( + t.update_ref("fresh", &blob.id(), None), + Err(Error::BadRef(_)) + )); + tip_is(c3); + + // A new branch may point at any held snapshot, and an unrelated one + // is a fresh history, not a sideways move. + t.update_ref("fresh", &unrelated, None).unwrap(); + } + #[test] fn update_ref_is_compare_and_swap() { // Two pushers can both pass the fast-forward check against the same old @@ -979,9 +1055,9 @@ mod tests { // the second writer fails instead of silently clobbering the first. let layout = tmp_remote("cas"); let t = LocalTransport::open(&layout.root).unwrap(); - let c1 = Object::blob(b"c1".to_vec()).id(); - let c2 = Object::blob(b"c2".to_vec()).id(); - let c3 = Object::blob(b"c3".to_vec()).id(); + let c1 = put_commit(&t, "c1", None); + let c2 = put_commit(&t, "c2", Some(c1)); + let c3 = put_commit(&t, "c3", Some(c1)); // Create: expected None means "branch must not exist". t.update_ref("main", &c1, None).unwrap(); @@ -1301,6 +1377,11 @@ mod tests { repo.branch("other").unwrap(); std::fs::write(root.join("x.txt"), b"a\nMAIN\nc\n").unwrap(); let main_tip = repo.commit("t", "main").unwrap(); + // A descendant of main's tip, which is what a legitimate push brings. + repo.branch("ahead").unwrap(); + repo.switch("ahead").unwrap(); + std::fs::write(root.join("y.txt"), b"y\n").unwrap(); + let pushed = repo.commit("t", "ahead").unwrap(); repo.switch("other").unwrap(); std::fs::write(root.join("x.txt"), b"a\nOTHER\nc\n").unwrap(); repo.commit("t", "other").unwrap(); @@ -1313,7 +1394,6 @@ mod tests { drop(repo); let t = LocalTransport::open(&root).unwrap(); - let pushed = Object::blob(b"pushed".to_vec()).id(); // The checked-out branch: refused, and nothing moved. assert!(matches!( t.update_ref("main", &pushed, Some(&main_tip)), diff --git a/crates/repo/src/wire.rs b/crates/repo/src/wire.rs index cdd5d41f..4f31ba82 100644 --- a/crates/repo/src/wire.rs +++ b/crates/repo/src/wire.rs @@ -2173,6 +2173,36 @@ mod tests { (client, handle) } + /// A client with write access still cannot rewind a served branch or point + /// it at a non-snapshot: the server checks fast-forward and target kind + /// itself, whatever the client did. + #[test] + fn served_update_ref_refuses_a_rewind_and_a_non_snapshot_target() { + use crate::transport::Transport; + + let root = tmp_repo("served-ff"); + let repo = crate::repo::Repo::open(&root).unwrap(); + std::fs::write(root.join("a.txt"), b"one").unwrap(); + let c1 = repo.commit("t", "c1").unwrap(); + std::fs::write(root.join("a.txt"), b"two").unwrap(); + let c2 = repo.commit("t", "c2").unwrap(); + let blob = scl_core::Object::blob(b"not a commit".to_vec()); + let blob_id = repo.vfs().store().lock().unwrap().put(blob).unwrap(); + drop(repo); + + let (client, server_join) = spawn_wire_pair_with_policy(&root, false); + assert!(matches!( + client.update_ref("main", &c1, Some(&c2)), + Err(Error::NonFastForward) + )); + assert!(client.update_ref("main", &blob_id, Some(&c2)).is_err()); + assert_eq!(client.list_refs().unwrap(), vec![("main".to_string(), c2)]); + client.bye().unwrap(); + drop(client); + server_join.join().unwrap().unwrap(); + std::fs::remove_dir_all(&root).unwrap(); + } + #[test] fn read_only_policy_rejects_mutations_allows_reads() { use crate::transport::Transport; @@ -3182,7 +3212,8 @@ mod tests { } // PutPack + UpdateRef land a second commit through the same session. - let (tip2, objects2) = tiny_history_for_bucket("seed2", b"second commit"); + let second = crate::test_support::fake_commit("second commit", Some(tip)); + let (tip2, objects2) = (second.id(), vec![(second.id(), second.encode())]); let (pack2, _idx2) = scl_core::pack::build_pack(&objects2).unwrap(); let ids2 = client.put_pack(&mut std::io::Cursor::new(pack2)).unwrap(); assert_eq!(ids2.len(), objects2.len()); diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 64295074..7350ca2e 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -501,6 +501,16 @@ too, not just the src-control-side metadata. unchanged; the diagnostic text is now sent raw (not as escaped `Display` output) and without the doubled prefix `Protocol` and `Remote` used to carry, and is escaped only when displayed. Paths are not escaped. +- **The server enforces fast-forward and ref-target kind, not just the client.** + `UpdateRef` (every transport, local-path pushes and `sc serve --store` + included) is checked under the remote's own lock: the new tip must be a held + snapshot or private-branch manifest, and an existing branch's tip must be an + ancestor of it (a manifest's ancestry is its `prev` chain plus its base). + A writer therefore cannot rewind or sideways-move a branch, or point one at + an absent object or a blob, whatever its client does; an ancestry walk that + hits an object the repository does not hold fails closed. Write access can + still add history and fast-forward any branch, so it stays full authority + over new commits. ## Bucket remotes (`sc+wal://` / `sc+s3://`) — ADR-0046/0057 @@ -513,8 +523,9 @@ too, not just the src-control-side metadata. - **Write access is full authority over refs and history.** Anyone who can write to the bucket's manifest key can move any branch to any commit — fast-forward-only enforcement (the manifest compare-and-swap plus the - `Error::NonFastForward` check) is **cooperative**, honored by the `sc` - client, not cryptographically enforced by the bucket. A writer with raw + `Error::NonFastForward` check) is **cooperative** for a writer that talks to + the bucket directly (a served `--store` instance enforces it, see Network + transport), not cryptographically enforced by the bucket. A writer with raw bucket access (outside the `sc` client) can bypass it entirely, the same way a user with raw filesystem access to a git bare repo can force-move a ref underneath git's own client-side checks. Scope bucket write credentials From 528bb1ad1e73f1727cf414b104bdf65dc4495735 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:13:28 +0200 Subject: [PATCH 2/8] fix(transport): refuse kind flips and dangling tips on ref updates Review of the server-side fast-forward check found it too weak. - A manifest's `base` counted as a parent, so a writer could move a public branch to a private manifest (and a snapshot could name a manifest as a parent). The ancestry rule is now per kind: snapshot parents must be snapshots, a manifest's ancestors are its `prev` chain only, and the new tip must have the same kind as the current one. - The walk stopped at the first parent equal to the current tip, so a commit with an absent second parent, root tree, nested blob or manifest closure entry landed and broke clone and fetch for the branch. New `ref_guard` module: a connectivity check like receive-pack's, covering only what the push added (it stops at existing ref tips and descends only into subtrees that differ from a parent's). - Local: the check runs before the repo-wide lock, from `expected_old`; under the lock only `current == expected_old` and the stopped-op guard remain. It reads a fresh store view, so an object `sc gc` pruned while a served session still cached it is not trusted. - Bucket: `PutPack` and `PutObject` refuse a record that decodes but is not in canonical encoding (the upload re-encodes, which stored it under an id it does not hash to). Pending objects keep decoded trees, snapshots and manifests so the check can walk what a push just uploaded. - THREAT-MODEL: drop the overclaims, describe the rules, and record that a keyless writer can still roll a private branch back under a new `prev`. --- crates/repo/src/bucket_transport.rs | 259 ++++++++++++++---------- crates/repo/src/lib.rs | 1 + crates/repo/src/merge.rs | 55 ++---- crates/repo/src/ref_guard.rs | 292 ++++++++++++++++++++++++++++ crates/repo/src/test_support.rs | 202 ++++++++++++++++++- crates/repo/src/transcripts.rs | 16 +- crates/repo/src/transport.rs | 144 ++++++++------ crates/repo/src/wire.rs | 6 +- docs/THREAT-MODEL.md | 31 ++- 9 files changed, 782 insertions(+), 224 deletions(-) create mode 100644 crates/repo/src/ref_guard.rs diff --git a/crates/repo/src/bucket_transport.rs b/crates/repo/src/bucket_transport.rs index d5faa92b..d0cf30d8 100644 --- a/crates/repo/src/bucket_transport.rs +++ b/crates/repo/src/bucket_transport.rs @@ -74,12 +74,10 @@ pub struct BucketTransport { /// (possibly long-lived, e.g. `wire::serve`-hosted) transport regardless /// of whether this call succeeds or fails. pending_packs: RefCell>, - /// Parents of every snapshot and manifest received since the last - /// `update_ref`, which is not yet on the manifest chain and so not in - /// `view.index`; lets `update_ref` check fast-forward over objects that - /// are only in a pending pack. Taken, like `pending_packs`, at the top of - /// each `update_ref`. - pending_commits: RefCell>>, + /// What was received since the last `update_ref`: not yet on the manifest + /// chain, so not in `view.index`, but what the ref rule must be able to + /// read. Taken, like `pending_packs`, at the top of each `update_ref`. + pending_objects: RefCell, /// Signature and transcript objects found in each scanned pack, keyed by /// pack hash (#229). Packs are immutable, so an entry never goes stale; /// it spares a long-lived `serve --store` session from re-reading every @@ -239,7 +237,7 @@ impl BucketTransport { pack_cache: RefCell::new(None), staged: RefCell::new(Vec::new()), pending_packs: RefCell::new(Vec::new()), - pending_commits: RefCell::new(BTreeMap::new()), + pending_objects: RefCell::new(PendingObjects::default()), leaf_cache: RefCell::new(BTreeMap::new()), saw_absent: Cell::new(false), attempt_tag: RefCell::new(None), @@ -682,14 +680,65 @@ impl crate::reachable::ObjectSource for BucketSource<'_> { } } -/// The parents of `obj` if it is a snapshot or a manifest (what `update_ref` -/// may point a branch at), else `None`. -fn commit_parents_of(id: &ObjectId, obj: &Object) -> Option> { - match obj { - Object::Snapshot(_) | Object::Manifest(_) => { - crate::merge::commit_parents(id, obj.clone()).ok() +/// Objects received but not yet committed to the manifest chain: the structural +/// ones decoded (the ref rule walks them), every other kind by id only. +#[derive(Default)] +struct PendingObjects { + nodes: BTreeMap, + leaves: std::collections::BTreeSet, +} + +impl PendingObjects { + fn note(&mut self, id: ObjectId, obj: Object) { + match obj { + Object::Snapshot(_) | Object::Tree(_) | Object::Manifest(_) => { + self.nodes.insert(id, crate::ref_guard::Node::of(obj)); + } + _ => { + self.leaves.insert(id); + } } - _ => None, + } + + fn extend(&mut self, other: PendingObjects) { + self.nodes.extend(other.nodes); + self.leaves.extend(other.leaves); + } +} + +/// The objects a ref move is judged against: this call's pending ones, then +/// what the manifest chain names. +struct RefView<'a> { + bucket: &'a BucketTransport, + pending: &'a PendingObjects, +} + +impl RefView<'_> { + fn on_chain(&self, id: &ObjectId) -> bool { + let view = self.bucket.view.borrow(); + view.as_ref().is_some_and(|v| v.index.contains_key(id)) + } +} + +impl crate::ref_guard::RefObjects for RefView<'_> { + fn node(&mut self, id: &ObjectId) -> Result> { + if let Some(n) = self.pending.nodes.get(id) { + return Ok(Some(n.clone())); + } + if self.pending.leaves.contains(id) { + return Ok(Some(crate::ref_guard::Node::Leaf)); + } + if !self.on_chain(id) { + return Ok(None); + } + let obj = Object::decode(&self.bucket.object_bytes(id)?)?; + Ok(Some(crate::ref_guard::Node::of(obj))) + } + + fn held(&mut self, id: &ObjectId) -> Result { + Ok(self.pending.nodes.contains_key(id) + || self.pending.leaves.contains(id) + || self.on_chain(id)) } } @@ -793,9 +842,11 @@ impl Transport for BucketTransport { if ObjectId::of(bytes) != *id { return Err(Error::CorruptObject(*id)); } - if let Some(parents) = commit_parents_of(id, &Object::decode(bytes)?) { - self.pending_commits.borrow_mut().insert(*id, parents); + let obj = Object::decode(bytes)?; + if obj.id() != *id { + return Err(Error::CorruptObject(*id)); // decodes, but not canonically } + self.pending_objects.borrow_mut().note(*id, obj); self.staged.borrow_mut().push((*id, bytes.to_vec())); Ok(()) } @@ -808,18 +859,25 @@ impl Transport for BucketTransport { // pinned byte-stable for a given object set (core), so the rebuilt // pack still content-addresses identically for identical input. let mut objects: Vec<(ObjectId, Vec)> = Vec::new(); - let mut commits = BTreeMap::new(); + let mut received = PendingObjects::default(); scl_core::pack::parse_pack_reader(src, |id, obj| { - if let Some(parents) = commit_parents_of(&id, &obj) { - commits.insert(id, parents); + // The upload below re-encodes each object; bytes that decode but + // are not canonical would be stored under an id they do not hash + // to, and every later read would fail verification. + let bytes = obj.encode(); + if ObjectId::of(&bytes) != id { + return Err(scl_core::Error::PackCorrupt(format!( + "object {id} is not in canonical encoding" + ))); } - objects.push((id, obj.encode())); + received.note(id, obj); + objects.push((id, bytes)); Ok(()) })?; let (hash, ids) = self.upload_pack(&objects)?; self.pending_packs.borrow_mut().push(hash); // Only after the upload: a failed one must not leave ids that no pack holds. - self.pending_commits.borrow_mut().extend(commits); + self.pending_objects.borrow_mut().extend(received); Ok(ids) } @@ -848,7 +906,10 @@ impl Transport for BucketTransport { // the bucket regardless (content-addressed, inert orphan garbage if // never referenced — correct per spec; `compact` sweeps it later, ADR-0057). let pending: Vec = std::mem::take(&mut *self.pending_packs.borrow_mut()); - let pending_commits = std::mem::take(&mut *self.pending_commits.borrow_mut()); + let pending_objects = std::mem::take(&mut *self.pending_objects.borrow_mut()); + // The ref rule's verdict for one current tip; a CAS retry that finds the + // same tip need not repeat the walk (objects only accumulate). + let mut checked_against: Option> = None; for attempt in 0..MAX_CAS_RETRIES { if attempt > 0 { std::thread::sleep(cas_backoff(attempt)); @@ -878,25 +939,22 @@ impl Transport for BucketTransport { } // The pushing client checks fast-forward too, but a writer need not // be our client (`sc serve --store`, or a direct bucket writer): - // refuse a rewind, a sideways move, or a target that is absent or - // not a snapshot or manifest. Objects only in a pending pack are - // not in the view yet, so their parents come from `pending_commits`. - crate::merge::check_ref_move(current, *id, |o| { - if let Some(parents) = pending_commits.get(o) { - return Ok(parents.clone()); - } - let on_chain = self + // the shared ref rule (kind, ancestry, connectivity) runs here, + // over this call's pending objects and the chain. + if checked_against != Some(current) { + let tips = self .view .borrow() .as_ref() - .is_some_and(|v| v.index.contains_key(o)); - if !on_chain { - return Err(Error::InvalidArgument(format!( - "object {o} is not a snapshot or manifest held here, so the ref update cannot be verified" - ))); - } - crate::merge::commit_parents(o, Object::decode(&self.object_bytes(o)?)?) - })?; + .map(|v| v.refs.values().copied().collect()) + .unwrap_or_default(); + let mut objects = RefView { + bucket: self, + pending: &pending_objects, + }; + crate::ref_guard::check_ref_move(&mut objects, &tips, current, *id)?; + checked_against = Some(current); + } // Claim a log slot for this attempt. `put_new` on `log/` is // the claim: if another writer already landed that seq, we didn't // win it and try the next one — the entry we didn't win becomes @@ -981,7 +1039,7 @@ mod tests { } use super::*; - use crate::test_support::fake_commit; + use crate::test_support::{fake_commit, put_fake}; use crate::transport::Transport; use crate::walfmt::{ checkpoint_key, idx_key, log_key, pack_key, Checkpoint, LogEntry, Manifest, RefUpdate, @@ -1230,59 +1288,64 @@ mod tests { } /// `sc serve --store` and direct bucket writers get the same server-side - /// rule as `LocalTransport`: no rewind, no sideways move, and a target that - /// is a held snapshot (including one only in the pending pack). + /// ref rules as `LocalTransport` (`ref_guard`), including over objects that + /// are only in a pending pack. #[test] - fn update_ref_enforces_fast_forward_and_target_kind() { + fn update_ref_enforces_the_ref_rules() { let broot = std::env::temp_dir().join(format!("scl-bt-ff-{}", std::process::id())); let _ = std::fs::remove_dir_all(&broot); let t = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); - let put = |c: &Object| t.put_object(&c.id(), &c.encode()).unwrap(); - let c1 = fake_commit("c1", None); - let c2 = fake_commit("c2", Some(c1.id())); - let c3 = fake_commit("c3", Some(c2.id())); - let unrelated = fake_commit("unrelated", None); - let blob = Object::blob(b"a blob".to_vec()); - let absent = ObjectId::of(b"never stored"); - - // Creation, then a two-step fast-forward whose objects are all pending. - put(&c1); - t.update_ref("main", &c1.id(), None).unwrap(); - put(&c2); - put(&c3); - t.update_ref("main", &c3.id(), Some(&c1.id())).unwrap(); - let tip_is = |want: ObjectId| { - assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), want)]); - }; - tip_is(c3.id()); + crate::test_support::ref_rules_hold(&t); + drop(t); + std::fs::remove_dir_all(&broot).unwrap(); + } - // Each refused call takes the pending pack with it, so stage the - // pending-only objects (an unrelated snapshot, a blob) again before it. - let stage = || { - put(&unrelated); - put(&blob); + /// A pack record that decodes but is not in canonical encoding would be + /// re-encoded by the upload and stored under an id it does not hash to, so + /// later reads fail verification. The whole pack is refused instead. + #[test] + fn put_pack_refuses_a_non_canonical_record() { + let broot = std::env::temp_dir().join(format!("scl-bt-noncanon-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&broot); + let t = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); + // A snapshot whose secrets registry is written out of order: it decodes + // into a sorted map, so re-encoding gives different bytes. + let (id_a, id_b) = (ObjectId::of(b"a"), ObjectId::of(b"b")); + let mut snap = scl_core::Snapshot { + root: ObjectId::of(b"root"), + parents: vec![], + author: "t".into(), + timestamp: 0, + message: "m".into(), + secrets: Default::default(), + protection: Default::default(), }; - // Rewind and sideways moves: on-chain targets and a pending one. - for target in [c1.id(), c2.id(), unrelated.id()] { - stage(); - assert!( - matches!( - t.update_ref("main", &target, Some(&c3.id())), - Err(Error::NonFastForward) - ), - "{target} must be refused" - ); - tip_is(c3.id()); - } - // Absent and wrong-kind targets, for an existing branch and a new one. - for branch in ["main", "fresh"] { - let old = (branch == "main").then(|| c3.id()); - for target in [absent, blob.id()] { - stage(); - assert!(t.update_ref(branch, &target, old.as_ref()).is_err()); - } - } - tip_is(c3.id()); + snap.secrets.insert("a".into(), id_a); + snap.secrets.insert("b".into(), id_b); + let canonical = Object::Snapshot(snap).encode(); + let entry = |name: &[u8], id: &ObjectId| { + let mut e = (name.len() as u32).to_be_bytes().to_vec(); + e.extend_from_slice(name); + e.extend_from_slice(id.as_bytes()); + e + }; + let (ea, eb) = (entry(b"a", &id_a), entry(b"b", &id_b)); + let pos = canonical + .windows(ea.len() + eb.len()) + .position(|w| w[..ea.len()] == ea[..] && w[ea.len()..] == eb[..]) + .expect("secrets section in the encoding"); + let mut raw = canonical.clone(); + raw[pos..pos + ea.len()].copy_from_slice(&eb); + raw[pos + ea.len()..pos + ea.len() + eb.len()].copy_from_slice(&ea); + assert_ne!(raw, canonical); + let decoded = Object::decode(&raw).expect("non-canonical bytes still decode"); + assert_eq!(decoded.encode(), canonical, "setup: decoding normalizes"); + + let (pack, _) = scl_core::pack::build_pack(&[(ObjectId::of(&raw), raw)]).unwrap(); + let err = t.put_pack(&mut std::io::Cursor::new(pack)).unwrap_err(); + assert!(err.to_string().contains("canonical"), "{err}"); + // Nothing was uploaded for it. + assert!(t.pending_packs.borrow().is_empty()); drop(t); std::fs::remove_dir_all(&broot).unwrap(); } @@ -1455,10 +1518,7 @@ mod tests { // without it, the OS could simply run racer-a to completion before // racer-b's thread is even scheduled, and the (1, 1) result below // would hold trivially with the manifest CAS never actually contended. - let mk_tip = |tag: &[u8]| { - let obj = fake_commit(std::str::from_utf8(tag).unwrap(), Some(base)); - (obj.id(), obj.encode()) - }; + let mk_tip = |tag: &[u8]| fake_commit(std::str::from_utf8(tag).unwrap(), Some(base)); let gate = std::sync::Barrier::new(2); let finished = std::sync::atomic::AtomicBool::new(false); let status = crate::test_support::Status::default(); @@ -1481,8 +1541,9 @@ mod tests { DirBucket::open(&broot).unwrap(), )) .unwrap(); - let (tip, bytes) = mk_tip(tag); - t.put_object(&tip, &bytes).unwrap(); + let obj = mk_tip(tag); + let tip = obj.id(); + put_fake(&t, &obj); gate.wait(); t.update_ref("main", &tip, Some(&base)) }) @@ -1537,7 +1598,7 @@ mod tests { )) .unwrap(); let obj = fake_commit(&format!("agent-{i}"), None); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + put_fake(&t, &obj); note(status, format!("writer-{i:03}"), "at the gate".into()); gate.wait(); note(status, format!("writer-{i:03}"), "update_ref".into()); @@ -1639,7 +1700,7 @@ mod tests { })) .unwrap(); let obj = fake_commit("budget", None); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + put_fake(&t, &obj); let res = t.update_ref("main", &obj.id(), None); if lands { res.unwrap(); @@ -1720,7 +1781,7 @@ mod tests { assert!(!t2.has_object(&orphan_obj.id()).unwrap()); // …and a live push steps over the claimed seq 2 (lands at 3+) and works. let next = fake_commit("after crash", None); - t2.put_object(&next.id(), &next.encode()).unwrap(); + put_fake(&t2, &next); t2.update_ref("recovered", &next.id(), None).unwrap(); let t3 = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); let refs = t3.list_refs().unwrap(); @@ -2042,8 +2103,8 @@ mod tests { let t = BucketTransport::from_bucket(Box::new(DirBucket::open(&broot).unwrap())).unwrap(); let obj = fake_commit("carry-push", None); - let (tip, bytes) = (obj.id(), obj.encode()); - t.put_object(&tip, &bytes).unwrap(); + let tip = obj.id(); + put_fake(&t, &obj); t.update_ref("carried", &tip, None).unwrap(); drop(t); @@ -2069,7 +2130,7 @@ mod tests { let n = CHECKPOINT_INTERVAL + 2; for i in 0..n { let obj = fake_commit(&format!("fold-{i}"), None); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + put_fake(&t, &obj); t.update_ref(&format!("w-{i}"), &obj.id(), None).unwrap(); } // the bucket now carries a manifest whose checkpoint_seq > 0 and the @@ -2146,7 +2207,7 @@ mod tests { .unwrap(); for i in 0..(CHECKPOINT_INTERVAL + 2) { let obj = fake_commit(&format!("foldfail-{i}"), None); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + put_fake(&t, &obj); t.update_ref(&format!("w-{i}"), &obj.id(), None).unwrap(); // must all be Ok } // no checkpoint could land; manifest still says 0 and reads still work diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index 4dd9c4f0..1f4ec5f9 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -43,6 +43,7 @@ pub mod protect; pub mod protect_ops; pub mod reachable; pub mod rebase_state; +mod ref_guard; pub mod refs; pub mod remote; pub mod replay; diff --git a/crates/repo/src/merge.rs b/crates/repo/src/merge.rs index 133dbaad..ba182550 100644 --- a/crates/repo/src/merge.rs +++ b/crates/repo/src/merge.rs @@ -57,19 +57,6 @@ pub fn is_ancestor(store: &mut Store, anc: ObjectId, desc: ObjectId) -> Result Result> { - match obj { - Object::Snapshot(s) => Ok(s.parents), - Object::Manifest(m) => Ok(m.prev.into_iter().chain(std::iter::once(m.base)).collect()), - other => Err(Error::BadRef(format!( - "expected snapshot or manifest at {id}, found {}", - other.kind_name() - ))), - } -} - /// All ancestors of `id`, inclusive. Kind-aware: snapshots contribute their /// parents, branch manifests their `prev` chain + public base. fn ancestors(store: &mut Store, id: ObjectId) -> Result> { @@ -78,7 +65,17 @@ fn ancestors(store: &mut Store, id: ObjectId) -> Result> { q.push_back(id); set.insert(id); while let Some(cur) = q.pop_front() { - for p in commit_parents(&cur, store.get(&cur)?)? { + let parents: Vec = match store.get(&cur)? { + Object::Snapshot(s) => s.parents, + Object::Manifest(m) => m.prev.into_iter().chain(std::iter::once(m.base)).collect(), + other => { + return Err(Error::BadRef(format!( + "expected snapshot or manifest at {cur}, found {}", + other.kind_name() + ))) + } + }; + for p in parents { if set.insert(p) { q.push_back(p); } @@ -87,36 +84,6 @@ fn ancestors(store: &mut Store, id: ObjectId) -> Result> { Ok(set) } -/// The rule every ref-serving [`crate::transport::Transport::update_ref`] -/// applies under its own lock, so a writer cannot rewind or sideways-move a -/// branch, or point one at an absent object or a non-commit: `new` must be a -/// snapshot or manifest, and when the branch exists (`current`) it must be an -/// ancestor of `new` (the same ancestry as [`is_ancestor`]). `parents_of` reads -/// one object's parents and must fail for an absent or wrong-kind object, so -/// an ancestry walk that hits a gap (partial clone) fails closed. -pub(crate) fn check_ref_move( - current: Option, - new: ObjectId, - mut parents_of: impl FnMut(&ObjectId) -> Result>, -) -> Result<()> { - let Some(cur) = current else { - return parents_of(&new).map(|_| ()); - }; - let mut seen = BTreeSet::from([new]); - let mut q = VecDeque::from([new]); - while let Some(id) = q.pop_front() { - for p in parents_of(&id)? { - if p == cur { - return Ok(()); - } - if seen.insert(p) { - q.push_back(p); - } - } - } - Err(Error::NonFastForward) -} - /// One merged file in a `FileMerge` working set. /// /// `bytes` carries ciphertext when a protected entry was resolved by diff --git a/crates/repo/src/ref_guard.rs b/crates/repo/src/ref_guard.rs new file mode 100644 index 00000000..8e32757b --- /dev/null +++ b/crates/repo/src/ref_guard.rs @@ -0,0 +1,292 @@ +//! The server-side rule for moving a ref (`Transport::update_ref`), shared by +//! every transport that serves refs. +//! +//! The pushing client checks fast-forward itself, but a writer need not be our +//! client, and object ingest has no connectivity check, so the receiver must +//! decide on its own (as git's receive-pack does). A ref move is accepted only +//! when: +//! +//! - **Kind:** the new tip is a snapshot or a private-branch manifest, and has +//! the same kind as the branch's current tip (a branch cannot flip between +//! public and private; publishing is a rewrite, not a fast-forward). +//! - **Ancestry:** the current tip is an ancestor of the new one. A snapshot's +//! ancestors are its parents, which must be snapshots; a manifest's are its +//! `prev` chain, which must be manifests. A manifest's `base` is deliberately +//! not an ancestor link: it names a public snapshot, not an earlier state of +//! the branch. +//! - **Connectivity:** everything the new history needs is held, so no clone, +//! fetch or gc ever meets a dangling id. The check covers only what the push +//! added: it stops at any existing ref tip (trusted, like the current tip), +//! and under a new commit it descends only into subtrees that differ from the +//! same path in a parent. A parent that is itself new is verified in the same +//! walk, so skipping what it already names is sound. +//! +//! Anything missing, of the wrong kind, or unreadable is a refusal. + +use std::collections::{BTreeSet, HashSet, VecDeque}; + +use scl_core::{BranchManifest, EntryKind, Object, ObjectId, Snapshot, Tree}; + +use crate::error::{Error, Result}; + +/// What an object is, as far as the ref rule cares. +#[derive(Clone)] +pub(crate) enum Node { + Snapshot(Snapshot), + Tree(Tree), + Manifest(BranchManifest), + /// Any other kind (blob, secret, sealed, signature, transcript). + Leaf, +} + +impl Node { + pub(crate) fn of(obj: Object) -> Node { + match obj { + Object::Snapshot(s) => Node::Snapshot(s), + Object::Tree(t) => Node::Tree(t), + Object::Manifest(m) => Node::Manifest(m), + _ => Node::Leaf, + } + } + + fn kind(&self) -> &'static str { + match self { + Node::Snapshot(_) => "snapshot", + Node::Tree(_) => "tree", + Node::Manifest(_) => "manifest", + Node::Leaf => "leaf object", + } + } +} + +/// Read access to the objects a ref move is judged against. Implementations +/// must read what is durably held now (not a cache that outlived a gc), and +/// must include what the same push just uploaded. +pub(crate) trait RefObjects { + /// The object, or `None` when it is not held. + fn node(&mut self, id: &ObjectId) -> Result>; + /// Whether the object is held, without reading it where that is cheaper. + fn held(&mut self, id: &ObjectId) -> Result; +} + +fn refuse(msg: String) -> Result { + Err(Error::InvalidArgument(format!("ref update refused: {msg}"))) +} + +fn read(src: &mut S, id: &ObjectId, what: &str) -> Result { + match src.node(id)? { + Some(n) => Ok(n), + None => refuse(format!("{what} {id} is not held here")), + } +} + +fn read_snapshot(src: &mut S, id: &ObjectId, what: &str) -> Result { + match read(src, id, what)? { + Node::Snapshot(s) => Ok(s), + other => refuse(format!("{what} {id} is a {}, not a snapshot", other.kind())), + } +} + +fn read_manifest(src: &mut S, id: &ObjectId, what: &str) -> Result { + match read(src, id, what)? { + Node::Manifest(m) => Ok(m), + other => refuse(format!("{what} {id} is a {}, not a manifest", other.kind())), + } +} + +fn require_held(src: &mut S, id: &ObjectId, what: &str) -> Result<()> { + if src.held(id)? { + Ok(()) + } else { + refuse(format!("{what} {id} is not held here")) + } +} + +/// Decide whether branch `current` (its tip, `None` for a new branch) may move +/// to `new`. `tips` is every existing ref's tip, trusted as already connected. +pub(crate) fn check_ref_move( + src: &mut S, + tips: &BTreeSet, + current: Option, + new: ObjectId, +) -> Result<()> { + let new_node = read(src, &new, "ref target")?; + if !matches!(new_node, Node::Snapshot(_) | Node::Manifest(_)) { + return refuse(format!( + "ref target {new} is a {}, not a snapshot or manifest", + new_node.kind() + )); + } + if let Some(cur) = current { + let cur_node = read(src, &cur, "current tip")?; + if std::mem::discriminant(&cur_node) != std::mem::discriminant(&new_node) { + return refuse(format!( + "branch tip {cur} is a {} but {new} is a {}; a branch cannot change kind", + cur_node.kind(), + new_node.kind() + )); + } + } + let mut boundary = tips.clone(); + boundary.extend(current); + match new_node { + Node::Snapshot(_) => { + if let Some(cur) = current { + if !snapshot_reaches(src, new, cur)? { + return Err(Error::NonFastForward); + } + } + verify_snapshots(src, &boundary, &[new], &mut HashSet::new()) + } + Node::Manifest(m) => verify_manifests(src, &boundary, current, new, m), + _ => unreachable!("kind checked above"), + } +} + +/// Is `target` an ancestor of `from` over snapshot parents (early exit)? +fn snapshot_reaches(src: &mut S, from: ObjectId, target: ObjectId) -> Result { + let mut seen = HashSet::from([from]); + let mut queue = VecDeque::from([from]); + while let Some(id) = queue.pop_front() { + for p in read_snapshot(src, &id, "commit")?.parents { + if p == target { + return Ok(true); + } + if seen.insert(p) { + queue.push_back(p); + } + } + } + Ok(false) +} + +/// Connectivity of the snapshot history reachable from `starts`, stopping at +/// `boundary`. `done` collects tree and blob ids already verified. +fn verify_snapshots( + src: &mut S, + boundary: &BTreeSet, + starts: &[ObjectId], + done: &mut HashSet, +) -> Result<()> { + let mut seen: HashSet = starts.iter().copied().collect(); + let mut queue: VecDeque = seen.iter().copied().collect(); + while let Some(id) = queue.pop_front() { + if boundary.contains(&id) { + continue; + } + let snap = read_snapshot(src, &id, "commit")?; + let mut parent_roots = Vec::with_capacity(snap.parents.len()); + for p in &snap.parents { + parent_roots.push(read_snapshot(src, p, "parent commit")?.root); + if seen.insert(*p) { + queue.push_back(*p); + } + } + for sid in snap.secrets.values() { + require_held(src, sid, "secret")?; + } + verify_tree(src, snap.root, &parent_roots, done)?; + } + Ok(()) +} + +/// Everything under tree `root` is held, descending only into entries that +/// differ from the same path under one of `parent_roots`. An explicit stack +/// keeps a hostile, deeply nested tree from overflowing the call stack, and +/// `done` keeps a shared-subtree DAG from being walked once per path. +fn verify_tree( + src: &mut S, + root: ObjectId, + parent_roots: &[ObjectId], + done: &mut HashSet, +) -> Result<()> { + let mut stack = vec![(root, parent_roots.to_vec())]; + while let Some((id, parents)) = stack.pop() { + if parents.contains(&id) || !done.insert(id) { + continue; + } + let tree = match read(src, &id, "tree")? { + Node::Tree(t) => t, + other => return refuse(format!("tree {id} is a {}, not a tree", other.kind())), + }; + // A parent tree that is not held cannot vouch for anything; the + // commit that names it fails its own check. + let mut parent_trees = Vec::new(); + for p in &parents { + if let Some(Node::Tree(t)) = src.node(p)? { + parent_trees.push(t); + } + } + for e in &tree.entries { + let same: Vec<&scl_core::TreeEntry> = parent_trees + .iter() + .filter_map(|t| t.get(&e.name)) + .filter(|pe| pe.kind == e.kind) + .collect(); + if same.iter().any(|pe| pe.id == e.id) { + continue; + } + match e.kind { + EntryKind::Tree => stack.push((e.id, same.iter().map(|pe| pe.id).collect())), + EntryKind::Blob => { + if !done.contains(&e.id) { + require_held(src, &e.id, "file content")?; + done.insert(e.id); + } + } + } + } + } + Ok(()) +} + +/// A manifest tip: its `prev` chain (manifests) must reach `current`, and each +/// new manifest must have its `base`, `anchors` (snapshots with held history) +/// and sealed `closure` held. +fn verify_manifests( + src: &mut S, + boundary: &BTreeSet, + current: Option, + new: ObjectId, + new_manifest: BranchManifest, +) -> Result<()> { + let mut chain = Vec::new(); + let (mut id, mut m) = (new, new_manifest); + let mut found = false; + loop { + if Some(id) == current { + found = true; + break; + } + // A new branch needs no ancestry, so it can stop at a trusted tip. + if current.is_none() && boundary.contains(&id) { + break; + } + let prev = m.prev; + chain.push(m); + match prev { + None => break, + Some(p) => { + m = read_manifest(src, &p, "previous manifest")?; + id = p; + } + } + } + if current.is_some() && !found { + return Err(Error::NonFastForward); + } + let mut done = HashSet::new(); + for m in &chain { + let roots: Vec = std::iter::once(m.base) + .chain(m.anchors.iter().copied()) + .collect(); + for r in &roots { + read_snapshot(src, r, "manifest base or anchor")?; + } + verify_snapshots(src, boundary, &roots, &mut done)?; + for c in &m.closure { + require_held(src, c, "sealed object")?; + } + } + Ok(()) +} diff --git a/crates/repo/src/test_support.rs b/crates/repo/src/test_support.rs index 76f21cde..910b190c 100644 --- a/crates/repo/src/test_support.rs +++ b/crates/repo/src/test_support.rs @@ -56,12 +56,24 @@ pub(crate) fn watchdog<'a>( }); } -/// A tiny snapshot with no tree behind it, distinct per `tag`: a stand-in tip +/// The root tree of every [`fake_commit`]. +pub(crate) fn empty_tree() -> scl_core::Object { + scl_core::Object::Tree(scl_core::Tree::default()) +} + +/// Store a [`fake_commit`] with its root tree, so the ref rule finds it connected. +pub(crate) fn put_fake(t: &T, commit: &scl_core::Object) { + for o in [empty_tree(), commit.clone()] { + t.put_object(&o.id(), &o.encode()).unwrap(); + } +} + +/// A tiny snapshot over the empty tree, distinct per `tag`: a stand-in tip /// for transport tests that move refs without building a repository. A /// `parent` makes it a descendant, so a move to it is a fast-forward. pub(crate) fn fake_commit(tag: &str, parent: Option) -> scl_core::Object { scl_core::Object::Snapshot(scl_core::Snapshot { - root: scl_core::ObjectId::of(tag.as_bytes()), + root: empty_tree().id(), parents: parent.into_iter().collect(), author: "t".into(), timestamp: 0, @@ -71,6 +83,192 @@ pub(crate) fn fake_commit(tag: &str, parent: Option) -> scl_ }) } +/// The server-side ref rule (`ref_guard`) from the outside, for any transport: +/// every refusal leaves the ref where it was, every legitimate move lands. +/// Each attempt stores the objects it needs right before `update_ref`, since a +/// refused call may discard what was staged for it. +pub(crate) fn ref_rules_hold(t: &T) { + use scl_core::{ + BranchManifest, EntryKind, FileMode, Object, ObjectId, SealedObj, Snapshot, Tree, TreeEntry, + }; + + let tree = |entries: &[(&str, EntryKind, ObjectId)]| { + Object::Tree(Tree::new( + entries + .iter() + .map(|(name, kind, id)| TreeEntry { + name: name.to_string(), + kind: *kind, + id: *id, + mode: FileMode::FILE, + perms: 0, + }) + .collect(), + )) + }; + let snap = |root: ObjectId, parents: &[ObjectId], msg: &str| { + Object::Snapshot(Snapshot { + root, + parents: parents.to_vec(), + author: "t".into(), + timestamp: 0, + message: msg.into(), + secrets: Default::default(), + protection: Default::default(), + }) + }; + let manifest = |base: ObjectId, prev: Option, closure: &[ObjectId]| { + Object::Manifest(BranchManifest { + base, + prev, + anchors: vec![], + closure: closure.to_vec(), + index_ct: vec![1], + kek_wraps: vec![], + }) + }; + // Store `objs` and `new`, then try to move `branch` from `old` to `new`. + let mv = |objs: &[&Object], branch: &str, new: &Object, old: Option<&Object>| { + for o in objs.iter().copied().chain([new]) { + t.put_object(&o.id(), &o.encode()).unwrap(); + } + t.update_ref(branch, &new.id(), old.map(|o| o.id()).as_ref()) + }; + let tip_of = |branch: &str| { + t.list_refs() + .unwrap() + .into_iter() + .find(|(b, _)| b == branch) + .map(|(_, id)| id) + }; + + let (b1, b2) = (Object::blob(b"one".to_vec()), Object::blob(b"two".to_vec())); + let absent_blob = Object::blob(b"never stored".to_vec()); + let absent_tree = tree(&[("x", EntryKind::Blob, absent_blob.id())]); + // Stored by some cases, but never connected: its root tree is absent. + let absent_snap = snap(absent_tree.id(), &[], "unconnected"); + // An id nothing ever stores. + let ghost = ObjectId::of(b"ghost"); + let dir1 = tree(&[("f", EntryKind::Blob, b1.id())]); + let root1 = tree(&[("a", EntryKind::Tree, dir1.id())]); + let c1 = snap(root1.id(), &[], "c1"); + let dir2 = tree(&[ + ("f", EntryKind::Blob, b1.id()), + ("g", EntryKind::Blob, b2.id()), + ]); + let root2 = tree(&[("a", EntryKind::Tree, dir2.id())]); + let c2 = snap(root2.id(), &[c1.id()], "c2"); + + // Creation, then a fast-forward that changes one subtree. + mv(&[&b1, &dir1, &root1], "main", &c1, None).unwrap(); + mv(&[&b2, &dir2, &root2], "main", &c2, Some(&c1)).unwrap(); + assert_eq!(tip_of("main"), Some(c2.id())); + + // Public branch: each of these is refused and `main` does not move. + let dir3 = tree(&[ + ("f", EntryKind::Blob, b1.id()), + ("g", EntryKind::Blob, b2.id()), + ("h", EntryKind::Blob, absent_blob.id()), + ]); + let root3 = tree(&[("a", EntryKind::Tree, dir3.id())]); + let sealed = Object::Sealed(SealedObj { + payload: std::sync::Arc::from(b"ciphertext".to_vec().into_boxed_slice()), + }); + let m_over_main = manifest(c2.id(), None, &[]); + let cases: Vec<(&str, Vec<&Object>, Object)> = vec![ + // a fast-forward whose changed subtree names an absent blob + ( + "absent nested blob", + vec![&dir3, &root3], + snap(root3.id(), &[c2.id()], "x1"), + ), + // an absent root tree + ( + "absent root", + vec![], + snap(absent_tree.id(), &[c2.id()], "x2"), + ), + // the current tip is a parent, but so is something absent + ( + "absent second parent", + vec![&root2], + snap(root2.id(), &[c2.id(), ghost], "x3"), + ), + // ... or something that is not a snapshot + ( + "blob as second parent", + vec![&root2, &b1], + snap(root2.id(), &[c2.id(), b1.id()], "x4"), + ), + // a new tip whose parent is a manifest + ( + "manifest as parent", + vec![&root2, &m_over_main], + snap(root2.id(), &[c2.id(), m_over_main.id()], "x5"), + ), + // rewind, and a sideways move to an unrelated history + ("rewind", vec![], c1.clone()), + ("sideways", vec![&root1], snap(root1.id(), &[], "unrelated")), + // not a snapshot at all + ("blob target", vec![], b1.clone()), + // public `main` turned into a private branch over its own tip + ("kind flip", vec![], m_over_main.clone()), + ]; + for (what, objs, new) in cases { + let r = mv(&objs, "main", &new, Some(&c2)); + assert!(r.is_err(), "{what} must be refused"); + assert_eq!(tip_of("main"), Some(c2.id()), "{what} moved the ref"); + } + // The kind rule answers for the flip itself (the ancestry rules would + // refuse it too, but as a different error). + let flip = mv(&[], "main", &m_over_main, Some(&c2)).unwrap_err(); + assert!(flip.to_string().contains("change kind"), "{flip}"); + assert!(t.update_ref("main", &ghost, Some(&c2.id())).is_err()); + // New branches: an unconnected commit, and one with an absent parent. + assert!(mv(&[], "fresh", &absent_snap, None).is_err()); + assert!(mv(&[&root2], "fresh", &snap(root2.id(), &[ghost], "y"), None).is_err()); + assert_eq!(tip_of("fresh"), None); + + // Private branch: a manifest's `prev` chain is its ancestry. + let m1 = manifest(c1.id(), None, &[sealed.id()]); + let m2 = manifest(c1.id(), Some(m1.id()), &[sealed.id()]); + let cases: Vec<(&str, Vec<&Object>, Object, Option<&Object>)> = vec![ + ( + "absent closure entry", + vec![], + manifest(c1.id(), None, &[absent_blob.id()]), + None, + ), + ("absent base", vec![], manifest(ghost, None, &[]), None), + ]; + for (what, objs, new, old) in cases { + assert!(mv(&objs, "priv", &new, old).is_err(), "{what}"); + assert_eq!(tip_of("priv"), None, "{what} created the ref"); + } + mv(&[&sealed], "priv", &m1, None).unwrap(); + mv(&[&sealed], "priv", &m2, Some(&m1)).unwrap(); + let cases: Vec<(&str, Object)> = vec![ + ("manifest rewind", m1.clone()), + ("manifest sideways", manifest(c2.id(), None, &[sealed.id()])), + ( + "manifest absent closure", + manifest(c1.id(), Some(m2.id()), &[absent_blob.id()]), + ), + ("manifest absent base", manifest(ghost, Some(m2.id()), &[])), + ("snapshot over manifest", snap(root2.id(), &[m2.id()], "z")), + ("snapshot target", c2.clone()), + ]; + for (what, new) in cases { + assert!( + mv(&[&sealed, &root2], "priv", &new, Some(&m2)).is_err(), + "{what} must be refused" + ); + assert_eq!(tip_of("priv"), Some(m2.id()), "{what} moved the ref"); + } + let m3 = manifest(c1.id(), Some(m2.id()), &[sealed.id()]); + mv(&[&sealed], "priv", &m3, Some(&m2)).unwrap(); +} + /// Windows: assert `path` carries exactly one explicit, non-inherited /// full-control ACE for the current user, read back with `icacls`. #[cfg(windows)] diff --git a/crates/repo/src/transcripts.rs b/crates/repo/src/transcripts.rs index a58253dd..393f55ac 100644 --- a/crates/repo/src/transcripts.rs +++ b/crates/repo/src/transcripts.rs @@ -2121,11 +2121,17 @@ pub(crate) mod tests { packs.push((path, enc, tomb_id)); } let dst = Repo::init(&dst_root).unwrap(); - dst.store_arc() - .lock() - .unwrap() - .put(src_arc.lock().unwrap().get(&snap).unwrap()) - .unwrap(); + { + // The snapshot with its whole tree: `update_ref` refuses a tip + // whose objects the destination does not hold. + let mut src = src_arc.lock().unwrap(); + let ids = crate::reachable::reachable_objects(&mut *src, &[snap]).unwrap(); + let dst_arc = dst.store_arc(); + let mut dst_store = dst_arc.lock().unwrap(); + for id in ids { + dst_store.put(src.get(&id).unwrap()).unwrap(); + } + } for (path, enc, _) in &packs { std::fs::write(path, scl_core::pack::build_pack(enc).unwrap().0).unwrap(); } diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index c4814427..c74a49ae 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -231,6 +231,28 @@ impl LocalTransport { &self.layout } + /// The server's own ref-move rule (`ref_guard`), read from a fresh store + /// view: this transport's long-lived store can hold an object in its + /// resident cache, or an index entry, that `sc gc` has since removed. + fn check_ref_move(&self, current: Option, new: ObjectId) -> Result<()> { + struct Fresh(Store); + impl crate::ref_guard::RefObjects for Fresh { + fn node(&mut self, id: &ObjectId) -> Result> { + if !self.0.contains(id) { + return Ok(None); + } + Ok(Some(crate::ref_guard::Node::of(self.0.get(id)?))) + } + fn held(&mut self, id: &ObjectId) -> Result { + Ok(self.0.contains(id)) + } + } + let tips = self.list_refs()?.into_iter().map(|(_, t)| t).collect(); + let store = + Store::open_persistent(self.layout.objects_dir(), crate::repo::default_budget())?; + crate::ref_guard::check_ref_move(&mut Fresh(store), &tips, current, new) + } + /// Bounded pack sender (P25): gather the transfer set exactly as before, /// but instead of collecting every object's encoded bytes into one /// `Vec<(ObjectId, Vec)>` (unbounded RAM) and calling `build_pack`, @@ -716,9 +738,26 @@ impl Transport for LocalTransport { id: &ObjectId, expected_old: Option<&ObjectId>, ) -> Result<()> { + crate::refs::validate_branch_name(branch)?; + // Setting the ref to the value it already has is a no-op, whatever the + // caller expected (re-checked under the lock). + let seen = crate::refs::read_branch_tip(&self.layout, branch)?; + if seen.as_ref() == Some(id) { + return Ok(()); + } + // A stale expectation is the common, cheap refusal: say so before + // walking anything. + if seen.as_ref() != expected_old { + return Err(Error::NonFastForward); + } + // The ancestry and connectivity walk can be long, so it runs before + // the repo-wide lock, from the tip the caller expects; under the lock + // only `current == expected_old` has to be confirmed, which makes the + // verified relation the one that is applied. + self.check_ref_move(expected_old.copied(), *id)?; let _lock = RepoLock::acquire(&self.layout)?; - // Revalidate inside the lock: the caller's fast-forward check ran - // unlocked, so the ref may have moved since (two concurrent pushes). + // Revalidate inside the lock: the checks above ran unlocked, so the ref + // may have moved since (two concurrent pushes). let current = crate::refs::read_branch_tip(&self.layout, branch)?; if current.as_ref() == Some(id) { return Ok(()); // already there — idempotent, nothing moves @@ -747,19 +786,6 @@ impl Transport for LocalTransport { if current.as_ref() != expected_old { return Err(Error::NonFastForward); } - // The pushing client checks fast-forward too, but a writer need not be - // our client: enforce it here, under the lock. - { - let mut store = self.store.borrow_mut(); - crate::merge::check_ref_move(current, *id, |o| { - if !store.contains(o) { - return Err(Error::InvalidArgument(format!( - "object {o} is not a snapshot or manifest held here, so the ref update cannot be verified" - ))); - } - crate::merge::commit_parents(o, store.get(o)?) - })?; - } crate::refs::write_branch_tip(&self.layout, branch, id) } @@ -989,63 +1015,52 @@ mod tests { /// Store a stand-in snapshot (see `fake_commit`) and return its id. fn put_commit(t: &LocalTransport, tag: &str, parent: Option) -> ObjectId { let o = crate::test_support::fake_commit(tag, parent); - t.put_object(&o.id(), &o.encode()).unwrap(); + crate::test_support::put_fake(t, &o); o.id() } - /// The server, not the pushing client, enforces fast-forward and the - /// target's kind: a writer with a ref-update verb must not be able to - /// rewind or sideways-move a branch, or point it at something that is not - /// a held snapshot (which would break clone, fetch and gc for everyone). + /// The server, not the pushing client, enforces kind, fast-forward and + /// connectivity (`ref_guard`); see `test_support::ref_rules_hold`. #[test] - fn update_ref_enforces_fast_forward_and_target_kind() { + fn update_ref_enforces_the_ref_rules() { let layout = tmp_remote("ff"); let t = LocalTransport::open(&layout.root).unwrap(); + crate::test_support::ref_rules_hold(&t); + drop(t); + std::fs::remove_dir_all(&layout.root).unwrap(); + } + + /// The check reads what is on disk now: an object this long-lived transport + /// still has in memory, but `sc gc` has since removed, does not count. + #[test] + fn update_ref_does_not_trust_an_object_pruned_from_disk() { + let layout = tmp_remote("pruned"); + let t = LocalTransport::open(&layout.root).unwrap(); let c1 = put_commit(&t, "c1", None); let c2 = put_commit(&t, "c2", Some(c1)); - let c3 = put_commit(&t, "c3", Some(c2)); - let unrelated = put_commit(&t, "unrelated", None); - let blob = Object::blob(b"a blob".to_vec()); - t.put_object(&blob.id(), &blob.encode()).unwrap(); - let absent = Object::blob(b"never stored".to_vec()).id(); - let tip_is = |want: ObjectId| { - assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), want)]); - }; - - // Creation to a held snapshot; a fast-forward over more than one step. t.update_ref("main", &c1, None).unwrap(); - t.update_ref("main", &c3, Some(&c1)).unwrap(); - tip_is(c3); - - // Rewind and sideways moves are refused even with a correct - // expected_old, and nothing moves. - for target in [c1, c2, unrelated] { - assert!( - matches!( - t.update_ref("main", &target, Some(&c3)), - Err(Error::NonFastForward) - ), - "{target} must be refused" - ); - tip_is(c3); + // Delete c2's loose file from under the transport (what a gc prune does). + let hex = c2.to_hex(); + let mut stack = vec![layout.objects_dir()]; + while let Some(dir) = stack.pop() { + for e in std::fs::read_dir(dir).unwrap() { + let p = e.unwrap().path(); + if p.is_dir() { + stack.push(p); + } else if p + .to_string_lossy() + .replace(std::path::MAIN_SEPARATOR, "") + .ends_with(&hex) + { + std::fs::remove_file(p).unwrap(); + } + } } - - // An absent object, a blob, and a blob as a branch creation. - assert!(t.update_ref("main", &absent, Some(&c3)).is_err()); - assert!(matches!( - t.update_ref("main", &blob.id(), Some(&c3)), - Err(Error::BadRef(_)) - )); - assert!(t.update_ref("fresh", &absent, None).is_err()); - assert!(matches!( - t.update_ref("fresh", &blob.id(), None), - Err(Error::BadRef(_)) - )); - tip_is(c3); - - // A new branch may point at any held snapshot, and an unrelated one - // is a fresh history, not a sideways move. - t.update_ref("fresh", &unrelated, None).unwrap(); + assert!(t.has_object(&c2).unwrap(), "setup: still resident"); + assert!(t.update_ref("main", &c2, Some(&c1)).is_err()); + assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), c1)]); + drop(t); + std::fs::remove_dir_all(&layout.root).unwrap(); } #[test] @@ -1448,6 +1463,10 @@ mod tests { repo.branch("other").unwrap(); std::fs::write(root.join("x.txt"), b"a\nMAIN\nc\n").unwrap(); let main_tip = repo.commit("t", "main").unwrap(); + repo.branch("ahead").unwrap(); + repo.switch("ahead").unwrap(); + std::fs::write(root.join("y.txt"), b"y\n").unwrap(); + let pushed = repo.commit("t", "ahead").unwrap(); repo.switch("other").unwrap(); std::fs::write(root.join("x.txt"), b"a\nOTHER\nc\n").unwrap(); repo.commit("t", "other").unwrap(); @@ -1457,7 +1476,6 @@ mod tests { drop(repo); let t = LocalTransport::open(&root).unwrap(); - let pushed = Object::blob(b"pushed".to_vec()).id(); assert!(matches!( t.update_ref("main", &pushed, Some(&main_tip)), Err(Error::PickInProgress) diff --git a/crates/repo/src/wire.rs b/crates/repo/src/wire.rs index 4f31ba82..b2888c1b 100644 --- a/crates/repo/src/wire.rs +++ b/crates/repo/src/wire.rs @@ -3213,7 +3213,11 @@ mod tests { // PutPack + UpdateRef land a second commit through the same session. let second = crate::test_support::fake_commit("second commit", Some(tip)); - let (tip2, objects2) = (second.id(), vec![(second.id(), second.encode())]); + let tree = crate::test_support::empty_tree(); + let (tip2, objects2) = ( + second.id(), + vec![(tree.id(), tree.encode()), (second.id(), second.encode())], + ); let (pack2, _idx2) = scl_core::pack::build_pack(&objects2).unwrap(); let ids2 = client.put_pack(&mut std::io::Cursor::new(pack2)).unwrap(); assert_eq!(ids2.len(), objects2.len()); diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 7350ca2e..6e889048 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -203,6 +203,12 @@ too, not just the src-control-side metadata. Publish re-runs the P5 secret scanner over all decrypted content **before** writing any public object, so a secret committed under seal cannot sail into plaintext history at release. +- **A writer without a key can roll a private branch back (tracked + follow-on).** The server only checks that a pushed manifest chains to the + current one through `prev`, so a keyless writer can push a manifest that + reuses an older manifest's index and key wraps under a new `prev`. The index + AAD does not bind `prev`, so recipients decrypt it as the branch's content at + that older point. Binding `prev` into the AAD closes it. - **Does NOT defend:** a malicious *recipient* (any key-holder can read, commit, grant, or publish); the git bridge carries private branches **not at all** (export/push refuse unconditionally — `--include-encrypted` does not @@ -501,16 +507,21 @@ too, not just the src-control-side metadata. unchanged; the diagnostic text is now sent raw (not as escaped `Display` output) and without the doubled prefix `Protocol` and `Remote` used to carry, and is escaped only when displayed. Paths are not escaped. -- **The server enforces fast-forward and ref-target kind, not just the client.** - `UpdateRef` (every transport, local-path pushes and `sc serve --store` - included) is checked under the remote's own lock: the new tip must be a held - snapshot or private-branch manifest, and an existing branch's tip must be an - ancestor of it (a manifest's ancestry is its `prev` chain plus its base). - A writer therefore cannot rewind or sideways-move a branch, or point one at - an absent object or a blob, whatever its client does; an ancestry walk that - hits an object the repository does not hold fails closed. Write access can - still add history and fast-forward any branch, so it stays full authority - over new commits. +- **The server decides a ref move itself, not just the client** (`ref_guard`). + Every `UpdateRef` (all transports, local-path pushes and `sc serve --store` + included) is checked against objects read from storage now, before the + repo-wide lock is taken, and the compare-and-swap is then confirmed under it. + The new tip must be a snapshot or a private-branch manifest of the **same + kind** as the branch's current tip, and the current tip must be its ancestor + (a snapshot's parents, which must be snapshots; a manifest's `prev` chain, + which must be manifests; a manifest's `base` is not an ancestor link). It + must also be **connected**: every parent, root tree, nested object, secret, + manifest `closure` entry, `base` and `anchor` the push added is held, checked + only along what differs from an existing ref tip. Anything absent, of the + wrong kind or unreadable is refused. This stops a rewind, a sideways move, a + public/private kind flip and a tip whose history dangles, which would break + clone, fetch and gc for everyone. It is not a content check: write access + still adds any history and fast-forwards any branch it can name. ## Bucket remotes (`sc+wal://` / `sc+s3://`) — ADR-0046/0057 From e537d08b58bebdcddaa4f64442c9852287f7bfd9 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:31:44 +0200 Subject: [PATCH 3/8] fix(transport): confirm checked objects are on disk under the lock, and check entry kinds Second review round on the server-side ref rule. - Local: the walk uses the session's store again (no per-push pack index reload) and records every object the verdict relied on. Under the repo lock, which gc also takes, each is confirmed to still be on disk (new `Store::contains_on_disk`: a loose-file stat, or an indexed pack that still exists; a miss rescans the pack directory once). A gc prune between the walk and the lock, or of an object a long-lived session still caches, now refuses the move instead of leaving a dangling ref. - A file entry's id was memoized in the same set as subtrees, so an id named as both a file and a directory skipped the subtree walk. Trees and leaves are memoized apart, a file entry must name a blob (or sealed object), and a tree entry must name a tree. - `LocalTransport::list_refs` goes through `refs::list_heads`, which skips a ref's in-flight temp file and tolerates a ref that vanishes while the directory is read. --- crates/core/src/store.rs | 13 ++ crates/repo/src/bucket_transport.rs | 22 +-- crates/repo/src/ref_guard.rs | 76 ++++++-- crates/repo/src/refs.rs | 7 +- crates/repo/src/test_support.rs | 21 +++ crates/repo/src/transport.rs | 278 ++++++++++++++++++++-------- docs/THREAT-MODEL.md | 10 +- 7 files changed, 307 insertions(+), 120 deletions(-) diff --git a/crates/core/src/store.rs b/crates/core/src/store.rs index 7657e789..e0151c8b 100644 --- a/crates/core/src/store.rs +++ b/crates/core/src/store.rs @@ -255,6 +255,19 @@ impl Store { self.existing_loose_path(id).is_some() || self.pack_index.contains_key(id) } + /// Whether the object is on disk right now, ignoring the resident cache + /// and any spill: a loose file, or a pack this store has indexed that + /// still exists. A stat per call, so it is cheap enough to confirm each id + /// a long check relied on once the repo lock is held (a gc prune can + /// delete an unreferenced object a cached read still returns). + pub fn contains_on_disk(&self, id: &ObjectId) -> bool { + self.existing_loose_path(id).is_some() + || self + .pack_index + .get(id) + .is_some_and(|loc| loc.pack_path.is_file()) + } + /// Return a blob's logical byte length without admitting its contents to /// the resident object cache. Persistent payloads are verified while they /// are streamed through a fixed-size buffer, allowing callers to enforce diff --git a/crates/repo/src/bucket_transport.rs b/crates/repo/src/bucket_transport.rs index d0cf30d8..7e271323 100644 --- a/crates/repo/src/bucket_transport.rs +++ b/crates/repo/src/bucket_transport.rs @@ -680,29 +680,20 @@ impl crate::reachable::ObjectSource for BucketSource<'_> { } } -/// Objects received but not yet committed to the manifest chain: the structural -/// ones decoded (the ref rule walks them), every other kind by id only. +/// Objects received but not yet committed to the manifest chain, as the ref +/// rule sees them (structure decoded, blobs and the like by kind only). #[derive(Default)] struct PendingObjects { nodes: BTreeMap, - leaves: std::collections::BTreeSet, } impl PendingObjects { fn note(&mut self, id: ObjectId, obj: Object) { - match obj { - Object::Snapshot(_) | Object::Tree(_) | Object::Manifest(_) => { - self.nodes.insert(id, crate::ref_guard::Node::of(obj)); - } - _ => { - self.leaves.insert(id); - } - } + self.nodes.insert(id, crate::ref_guard::Node::of(obj)); } fn extend(&mut self, other: PendingObjects) { self.nodes.extend(other.nodes); - self.leaves.extend(other.leaves); } } @@ -725,9 +716,6 @@ impl crate::ref_guard::RefObjects for RefView<'_> { if let Some(n) = self.pending.nodes.get(id) { return Ok(Some(n.clone())); } - if self.pending.leaves.contains(id) { - return Ok(Some(crate::ref_guard::Node::Leaf)); - } if !self.on_chain(id) { return Ok(None); } @@ -736,9 +724,7 @@ impl crate::ref_guard::RefObjects for RefView<'_> { } fn held(&mut self, id: &ObjectId) -> Result { - Ok(self.pending.nodes.contains_key(id) - || self.pending.leaves.contains(id) - || self.on_chain(id)) + Ok(self.pending.nodes.contains_key(id) || self.on_chain(id)) } } diff --git a/crates/repo/src/ref_guard.rs b/crates/repo/src/ref_guard.rs index 8e32757b..5d21a982 100644 --- a/crates/repo/src/ref_guard.rs +++ b/crates/repo/src/ref_guard.rs @@ -29,14 +29,17 @@ use scl_core::{BranchManifest, EntryKind, Object, ObjectId, Snapshot, Tree}; use crate::error::{Error, Result}; -/// What an object is, as far as the ref rule cares. +/// What an object is, as far as the ref rule cares. Blobs carry no content: +/// the rule only needs to know one is there and what it is. #[derive(Clone)] pub(crate) enum Node { Snapshot(Snapshot), Tree(Tree), Manifest(BranchManifest), - /// Any other kind (blob, secret, sealed, signature, transcript). - Leaf, + Blob, + Sealed, + /// A secret, signature or transcript. + Other, } impl Node { @@ -45,7 +48,9 @@ impl Node { Object::Snapshot(s) => Node::Snapshot(s), Object::Tree(t) => Node::Tree(t), Object::Manifest(m) => Node::Manifest(m), - _ => Node::Leaf, + Object::Blob(_) => Node::Blob, + Object::Sealed(_) => Node::Sealed, + _ => Node::Other, } } @@ -54,21 +59,36 @@ impl Node { Node::Snapshot(_) => "snapshot", Node::Tree(_) => "tree", Node::Manifest(_) => "manifest", - Node::Leaf => "leaf object", + Node::Blob => "blob", + Node::Sealed => "sealed object", + Node::Other => "secret, signature or transcript", } } } -/// Read access to the objects a ref move is judged against. Implementations -/// must read what is durably held now (not a cache that outlived a gc), and -/// must include what the same push just uploaded. +/// Read access to the objects a ref move is judged against; it must include +/// what the same push just uploaded. A cached read can outlive a gc prune, so +/// the caller confirms what it relied on is still on disk once it holds the +/// repo lock. pub(crate) trait RefObjects { /// The object, or `None` when it is not held. fn node(&mut self, id: &ObjectId) -> Result>; + /// Like [`RefObjects::node`], for an object expected to be a file's + /// content: an implementation may avoid loading a large blob to say so. + fn leaf(&mut self, id: &ObjectId) -> Result> { + self.node(id) + } /// Whether the object is held, without reading it where that is cheaper. fn held(&mut self, id: &ObjectId) -> Result; } +/// Tree and file-content ids already verified in one check. +#[derive(Default)] +struct Verified { + trees: HashSet, + leaves: HashSet, +} + fn refuse(msg: String) -> Result { Err(Error::InvalidArgument(format!("ref update refused: {msg}"))) } @@ -136,7 +156,7 @@ pub(crate) fn check_ref_move( return Err(Error::NonFastForward); } } - verify_snapshots(src, &boundary, &[new], &mut HashSet::new()) + verify_snapshots(src, &boundary, &[new], &mut Verified::default()) } Node::Manifest(m) => verify_manifests(src, &boundary, current, new, m), _ => unreachable!("kind checked above"), @@ -166,7 +186,7 @@ fn verify_snapshots( src: &mut S, boundary: &BTreeSet, starts: &[ObjectId], - done: &mut HashSet, + done: &mut Verified, ) -> Result<()> { let mut seen: HashSet = starts.iter().copied().collect(); let mut queue: VecDeque = seen.iter().copied().collect(); @@ -190,19 +210,22 @@ fn verify_snapshots( Ok(()) } -/// Everything under tree `root` is held, descending only into entries that -/// differ from the same path under one of `parent_roots`. An explicit stack -/// keeps a hostile, deeply nested tree from overflowing the call stack, and -/// `done` keeps a shared-subtree DAG from being walked once per path. +/// Everything under tree `root` is held, with the right kind, descending only +/// into entries that differ from the same path under one of `parent_roots`. +/// An explicit stack keeps a hostile, deeply nested tree from overflowing the +/// call stack. `trees` and `leaves` memoize what has been verified, apart from +/// each other: an id is verified as one kind only, so an entry that names it as +/// the other kind is refused rather than trusted, and a shared-subtree DAG is +/// not walked once per path. fn verify_tree( src: &mut S, root: ObjectId, parent_roots: &[ObjectId], - done: &mut HashSet, + seen: &mut Verified, ) -> Result<()> { let mut stack = vec![(root, parent_roots.to_vec())]; while let Some((id, parents)) = stack.pop() { - if parents.contains(&id) || !done.insert(id) { + if parents.contains(&id) || !seen.trees.insert(id) { continue; } let tree = match read(src, &id, "tree")? { @@ -229,9 +252,22 @@ fn verify_tree( match e.kind { EntryKind::Tree => stack.push((e.id, same.iter().map(|pe| pe.id).collect())), EntryKind::Blob => { - if !done.contains(&e.id) { - require_held(src, &e.id, "file content")?; - done.insert(e.id); + if seen.leaves.contains(&e.id) { + continue; + } + match src.leaf(&e.id)? { + Some(Node::Blob | Node::Sealed) => { + seen.leaves.insert(e.id); + } + Some(other) => { + return refuse(format!( + "file entry {} names {}, a {}, not file content", + e.name, + e.id, + other.kind() + )) + } + None => return refuse(format!("file content {} is not held here", e.id)), } } } @@ -275,7 +311,7 @@ fn verify_manifests( if current.is_some() && !found { return Err(Error::NonFastForward); } - let mut done = HashSet::new(); + let mut done = Verified::default(); for m in &chain { let roots: Vec = std::iter::once(m.base) .chain(m.anchors.iter().copied()) diff --git a/crates/repo/src/refs.rs b/crates/repo/src/refs.rs index 22f86d3e..438cdaea 100644 --- a/crates/repo/src/refs.rs +++ b/crates/repo/src/refs.rs @@ -167,7 +167,12 @@ pub fn list_heads(layout: &Layout) -> Result> { if name.contains(".tmp") { continue; } - let text = std::fs::read_to_string(e.path())?; + // A ref gone since the directory was read is simply not there. + let text = match std::fs::read_to_string(e.path()) { + Ok(t) => t, + Err(err) if err.kind() == std::io::ErrorKind::NotFound => continue, + Err(err) => return Err(err.into()), + }; let id = ObjectId::from_str(text.trim()) .map_err(|_| Error::BadRef(format!("head {name} has bad id")))?; out.push((name, id)); diff --git a/crates/repo/src/test_support.rs b/crates/repo/src/test_support.rs index 910b190c..759e5bfe 100644 --- a/crates/repo/src/test_support.rs +++ b/crates/repo/src/test_support.rs @@ -219,6 +219,27 @@ pub(crate) fn ref_rules_hold(t: &T) { assert!(r.is_err(), "{what} must be refused"); assert_eq!(tip_of("main"), Some(c2.id()), "{what} moved the ref"); } + // An entry id reused across kinds. `dirx` is a tree whose own file is + // absent; naming it as a file in one entry must not make the tree entry + // that follows look verified, and a tree entry naming a blob is wrong too. + let dirx = tree(&[("gone", EntryKind::Blob, absent_blob.id())]); + let reuse = tree(&[ + ("a", EntryKind::Blob, dirx.id()), + ("b", EntryKind::Tree, dirx.id()), + ]); + let tree_over_blob = tree(&[("a", EntryKind::Tree, b1.id())]); + let file_over_tree = tree(&[("a", EntryKind::Blob, dir1.id())]); + for (what, objs, root) in [ + ("file and tree entry share an id", vec![&dirx], &reuse), + ("tree entry names a blob", vec![&b1], &tree_over_blob), + ("file entry names a tree", vec![&dir1, &b1], &file_over_tree), + ] { + let c = snap(root.id(), &[c2.id()], what); + let mut objs = objs; + objs.push(root); + assert!(mv(&objs, "main", &c, Some(&c2)).is_err(), "{what}"); + assert_eq!(tip_of("main"), Some(c2.id()), "{what} moved the ref"); + } // The kind rule answers for the flip itself (the ancestry rules would // refuse it too, but as a different error). let flip = mv(&[], "main", &m_over_main, Some(&c2)).unwrap_err(); diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index c74a49ae..3ba962c0 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -4,7 +4,6 @@ use std::cell::RefCell; use std::io::{Read, Write}; -use std::str::FromStr; use std::sync::atomic::{AtomicU64, Ordering}; use scl_core::{Object, ObjectId, Store}; @@ -231,26 +230,136 @@ impl LocalTransport { &self.layout } - /// The server's own ref-move rule (`ref_guard`), read from a fresh store - /// view: this transport's long-lived store can hold an object in its - /// resident cache, or an index entry, that `sc gc` has since removed. - fn check_ref_move(&self, current: Option, new: ObjectId) -> Result<()> { - struct Fresh(Store); - impl crate::ref_guard::RefObjects for Fresh { - fn node(&mut self, id: &ObjectId) -> Result> { - if !self.0.contains(id) { + /// The server's own ref-move rule (`ref_guard`), judged from the session's + /// store without the repo lock (the walk can be long). Returns every object + /// the verdict relied on: a cached read can outlive a gc prune, so + /// [`LocalTransport::commit_ref_move`] confirms each is still on disk once + /// it holds the lock, which gc also takes. + fn verify_ref_move( + &self, + current: Option, + new: ObjectId, + ) -> Result> { + use crate::ref_guard::{Node, RefObjects}; + struct Session<'a> { + store: &'a mut Store, + relied_on: std::collections::BTreeSet, + reloaded: bool, + } + impl Session<'_> { + /// Held? A miss rescans the pack directory once (a gc repack or + /// another connection's ingest may have moved objects since this + /// session indexed them), so a rescan costs only a refusal. + fn present(&mut self, id: &ObjectId) -> bool { + if self.store.contains(id) { + return true; + } + if !self.reloaded { + self.reloaded = true; + let _ = self.store.reload_packs(); + return self.store.contains(id); + } + false + } + } + impl RefObjects for Session<'_> { + fn node(&mut self, id: &ObjectId) -> Result> { + if !self.present(id) { return Ok(None); } - Ok(Some(crate::ref_guard::Node::of(self.0.get(id)?))) + self.relied_on.insert(*id); + Ok(Some(Node::of(self.store.get(id)?))) + } + fn leaf(&mut self, id: &ObjectId) -> Result> { + if !self.present(id) { + return Ok(None); + } + self.relied_on.insert(*id); + // Measuring streams and hash-checks a blob without loading it. + match self.store.blob_size(id) { + Ok(_) => Ok(Some(Node::Blob)), + Err(scl_core::Error::WrongKind(..)) => Ok(Some(Node::of(self.store.get(id)?))), + Err(e) => Err(e.into()), + } } fn held(&mut self, id: &ObjectId) -> Result { - Ok(self.0.contains(id)) + let held = self.present(id); + if held { + self.relied_on.insert(*id); + } + Ok(held) } } let tips = self.list_refs()?.into_iter().map(|(_, t)| t).collect(); - let store = - Store::open_persistent(self.layout.objects_dir(), crate::repo::default_budget())?; - crate::ref_guard::check_ref_move(&mut Fresh(store), &tips, current, new) + let mut store = self.store.borrow_mut(); + let mut session = Session { + store: &mut store, + relied_on: Default::default(), + reloaded: false, + }; + crate::ref_guard::check_ref_move(&mut session, &tips, current, new)?; + Ok(session.relied_on) + } + + /// The locked half of `update_ref`: with the repo lock held, confirm the + /// compare-and-swap, the stopped-operation guard and that everything the + /// unlocked walk relied on is still on disk, then move the ref. + fn commit_ref_move( + &self, + branch: &str, + id: &ObjectId, + expected_old: Option<&ObjectId>, + relied_on: &std::collections::BTreeSet, + ) -> Result<()> { + let _lock = RepoLock::acquire(&self.layout)?; + // Revalidate inside the lock: the checks before it ran unlocked, so the + // ref may have moved since (two concurrent pushes). + let current = crate::refs::read_branch_tip(&self.layout, branch)?; + if current.as_ref() == Some(id) { + return Ok(()); // already there — idempotent, nothing moves + } + // A stopped merge, cherry-pick or rebase here is completed against the + // checked-out branch's tip: moving that tip from underneath it would + // make the completing commit silently drop what was pushed (the P19-I1 + // discard shape, reached by a remote writer). Refuse before any + // write; other branches are unaffected. HEAD is read only when an op + // is stopped, and a failure to read it then fails closed: the op's + // state exists, so a push must not guess which branch it belongs to. + let stopped = if crate::merge_state::in_progress(&self.layout) { + Some(Error::MergeInProgress) + } else if crate::pick_state::in_progress(&self.layout) { + Some(Error::PickInProgress) + } else if crate::rebase_state::in_progress(&self.layout) { + Some(Error::RebaseInProgress) + } else { + None + }; + if let Some(err) = stopped { + if crate::refs::current_branch(&self.layout)? == branch { + return Err(err); + } + } + if current.as_ref() != expected_old { + return Err(Error::NonFastForward); + } + // A stat per object, under the lock. A miss rescans the pack directory + // once before it counts (a repack moves objects between files). + let mut store = self.store.borrow_mut(); + let mut gone: Vec<&ObjectId> = relied_on + .iter() + .filter(|o| !store.contains_on_disk(o)) + .collect(); + if !gone.is_empty() { + store.reload_packs()?; + gone.retain(|o| !store.contains_on_disk(o)); + } + if let Some(o) = gone.first() { + return Err(Error::InvalidArgument(format!( + "ref update refused: object {o} is no longer on disk (pruned since it was checked); retry the push" + ))); + } + drop(store); + crate::refs::write_branch_tip(&self.layout, branch, id) } /// Bounded pack sender (P25): gather the transfer set exactly as before, @@ -675,23 +784,9 @@ fn ingest_pack_file_impl( impl Transport for LocalTransport { fn list_refs(&self) -> Result> { - let mut out = Vec::new(); - let dir = self.layout.refs_heads_dir(); - let entries = match std::fs::read_dir(&dir) { - Ok(e) => e, - Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(out), - Err(e) => return Err(e.into()), - }; - for e in entries { - let e = e?; - if e.file_type()?.is_file() { - let name = e.file_name().to_string_lossy().into_owned(); - let text = std::fs::read_to_string(e.path())?; - let id = ObjectId::from_str(text.trim()) - .map_err(|_| Error::BadRef(format!("remote ref {name} has bad id")))?; - out.push((name, id)); - } - } + // `list_heads` skips a ref's in-flight temp file, which a concurrent + // push can have on disk while this runs. + let mut out = crate::refs::list_heads(&self.layout)?; out.sort_by(|a, b| a.0.cmp(&b.0)); Ok(out) } @@ -752,41 +847,10 @@ impl Transport for LocalTransport { } // The ancestry and connectivity walk can be long, so it runs before // the repo-wide lock, from the tip the caller expects; under the lock - // only `current == expected_old` has to be confirmed, which makes the - // verified relation the one that is applied. - self.check_ref_move(expected_old.copied(), *id)?; - let _lock = RepoLock::acquire(&self.layout)?; - // Revalidate inside the lock: the checks above ran unlocked, so the ref - // may have moved since (two concurrent pushes). - let current = crate::refs::read_branch_tip(&self.layout, branch)?; - if current.as_ref() == Some(id) { - return Ok(()); // already there — idempotent, nothing moves - } - // A stopped merge, cherry-pick or rebase here is completed against the - // checked-out branch's tip: moving that tip from underneath it would - // make the completing commit silently drop what was pushed (the P19-I1 - // discard shape, reached by a remote writer). Refuse before any - // write; other branches are unaffected. HEAD is read only when an op - // is stopped, and a failure to read it then fails closed: the op's - // state exists, so a push must not guess which branch it belongs to. - let stopped = if crate::merge_state::in_progress(&self.layout) { - Some(Error::MergeInProgress) - } else if crate::pick_state::in_progress(&self.layout) { - Some(Error::PickInProgress) - } else if crate::rebase_state::in_progress(&self.layout) { - Some(Error::RebaseInProgress) - } else { - None - }; - if let Some(err) = stopped { - if crate::refs::current_branch(&self.layout)? == branch { - return Err(err); - } - } - if current.as_ref() != expected_old { - return Err(Error::NonFastForward); - } - crate::refs::write_branch_tip(&self.layout, branch, id) + // only `current == expected_old` and the objects' presence have to be + // confirmed, which makes the verified relation the one that is applied. + let relied_on = self.verify_ref_move(expected_old.copied(), *id)?; + self.commit_ref_move(branch, id, expected_old, &relied_on) } /// Bounded sender (P25): builds the transfer set into a temp pack file @@ -1030,18 +1094,12 @@ mod tests { std::fs::remove_dir_all(&layout.root).unwrap(); } - /// The check reads what is on disk now: an object this long-lived transport - /// still has in memory, but `sc gc` has since removed, does not count. - #[test] - fn update_ref_does_not_trust_an_object_pruned_from_disk() { - let layout = tmp_remote("pruned"); - let t = LocalTransport::open(&layout.root).unwrap(); - let c1 = put_commit(&t, "c1", None); - let c2 = put_commit(&t, "c2", Some(c1)); - t.update_ref("main", &c1, None).unwrap(); - // Delete c2's loose file from under the transport (what a gc prune does). - let hex = c2.to_hex(); + /// Delete an object's loose file, as a gc prune does, and leave the + /// transport's own caches alone. + fn prune_loose(layout: &Layout, id: &ObjectId) { + let hex = id.to_hex(); let mut stack = vec![layout.objects_dir()]; + let mut pruned = false; while let Some(dir) = stack.pop() { for e in std::fs::read_dir(dir).unwrap() { let p = e.unwrap().path(); @@ -1053,9 +1111,23 @@ mod tests { .ends_with(&hex) { std::fs::remove_file(p).unwrap(); + pruned = true; } } } + assert!(pruned, "setup: {id} had a loose file"); + } + + /// An object this long-lived transport still has in memory, but `sc gc` + /// has since pruned from disk, does not count. + #[test] + fn update_ref_does_not_trust_an_object_pruned_from_disk() { + let layout = tmp_remote("pruned"); + let t = LocalTransport::open(&layout.root).unwrap(); + let c1 = put_commit(&t, "c1", None); + let c2 = put_commit(&t, "c2", Some(c1)); + t.update_ref("main", &c1, None).unwrap(); + prune_loose(&layout, &c2); assert!(t.has_object(&c2).unwrap(), "setup: still resident"); assert!(t.update_ref("main", &c2, Some(&c1)).is_err()); assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), c1)]); @@ -1063,6 +1135,58 @@ mod tests { std::fs::remove_dir_all(&layout.root).unwrap(); } + /// The walk runs without the repo lock, so a gc can prune an object it + /// verified before the lock is taken. The locked half confirms each object + /// the walk relied on is still on disk, so that move is refused, and a + /// retry (the objects pushed again) lands. + #[test] + fn an_object_pruned_between_the_walk_and_the_lock_refuses_the_move() { + let layout = tmp_remote("race"); + let t = LocalTransport::open(&layout.root).unwrap(); + let c1 = put_commit(&t, "c1", None); + let c2 = put_commit(&t, "c2", Some(c1)); + t.update_ref("main", &c1, None).unwrap(); + + let relied_on = t.verify_ref_move(Some(c1), c2).unwrap(); + assert!(relied_on.contains(&c2), "the walk relied on the new tip"); + prune_loose(&layout, &c2); + let err = t + .commit_ref_move("main", &c2, Some(&c1), &relied_on) + .unwrap_err(); + assert!(err.to_string().contains("no longer on disk"), "{err}"); + assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), c1)]); + + // A new connection's pushing the objects again lands it (this + // session still has them cached and would not write them). + drop(t); + let t = LocalTransport::open(&layout.root).unwrap(); + let c2 = put_commit(&t, "c2", Some(c1)); + let relied_on = t.verify_ref_move(Some(c1), c2).unwrap(); + t.commit_ref_move("main", &c2, Some(&c1), &relied_on) + .unwrap(); + assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), c2)]); + drop(t); + std::fs::remove_dir_all(&layout.root).unwrap(); + } + + /// Enumerating refs while another push writes one must not trip over its + /// in-flight temp file. + #[test] + fn list_refs_skips_a_refs_temp_file() { + let layout = tmp_remote("tmpref"); + let t = LocalTransport::open(&layout.root).unwrap(); + let c1 = put_commit(&t, "c1", None); + t.update_ref("main", &c1, None).unwrap(); + std::fs::write( + layout.refs_heads_dir().join("other.4242.tmp"), + b"half a ref", + ) + .unwrap(); + assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), c1)]); + drop(t); + std::fs::remove_dir_all(&layout.root).unwrap(); + } + #[test] fn update_ref_is_compare_and_swap() { // Two pushers can both pass the fast-forward check against the same old diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 6e889048..1e960647 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -509,15 +509,17 @@ too, not just the src-control-side metadata. and is escaped only when displayed. Paths are not escaped. - **The server decides a ref move itself, not just the client** (`ref_guard`). Every `UpdateRef` (all transports, local-path pushes and `sc serve --store` - included) is checked against objects read from storage now, before the - repo-wide lock is taken, and the compare-and-swap is then confirmed under it. + included) is judged before the repo-wide lock is taken, so a long walk does + not hold it; under the lock the compare-and-swap is confirmed and every + object the verdict relied on is checked to still be on disk (a gc prune + cannot slip between the two). The new tip must be a snapshot or a private-branch manifest of the **same kind** as the branch's current tip, and the current tip must be its ancestor (a snapshot's parents, which must be snapshots; a manifest's `prev` chain, which must be manifests; a manifest's `base` is not an ancestor link). It must also be **connected**: every parent, root tree, nested object, secret, - manifest `closure` entry, `base` and `anchor` the push added is held, checked - only along what differs from an existing ref tip. Anything absent, of the + manifest `closure` entry, `base` and `anchor` the push added is held and of + the right kind, checked only along what differs from an existing ref tip. Anything absent, of the wrong kind or unreadable is refused. This stops a rewind, a sideways move, a public/private kind flip and a tip whose history dangles, which would break clone, fetch and gc for everyone. It is not a content check: write access From 75986a01d07d50d37c1a88e59a7c923a212453c4 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:54:38 +0200 Subject: [PATCH 4/8] fix(transport): check closure and secret kinds, dot-prefix ref temp files Third review round on the server-side ref rule. - A manifest closure entry or a snapshot secret was only checked for presence. A closure entry naming the public base made reachability mark it seen without walking it, so clone and gc could omit its tree. Closure entries must now be sealed objects and registry entries secrets. Entries a parent snapshot or the previous manifest already lists are inside its verified closure and are not read again. - The local walk now asks for an object's kind through `Store::peek_kind`, which reads the first encoded byte (loose or packed) instead of streaming the whole blob; `Object::kind_name_of_tag` maps the byte. Unchanged file entries in a changed directory were already skipped against the parent's tree; a counting test now pins that for files, secrets and closure entries. The bucket still decodes an on-chain object to learn its kind (its index has no kind), only for what the push changed. - `list_heads` hid any branch whose name contained ".tmp". Ref writes now use a dot-prefixed temp file (no branch name can start with a dot) and the listing skips only dot-prefixed entries. --- crates/core/src/object.rs | 16 ++ crates/core/src/store.rs | 79 ++++++++ crates/repo/src/bucket_transport.rs | 4 - crates/repo/src/ref_guard.rs | 282 ++++++++++++++++++++++++---- crates/repo/src/refs.rs | 69 ++++++- crates/repo/src/test_support.rs | 61 ++++-- crates/repo/src/transport.rs | 21 +-- 7 files changed, 462 insertions(+), 70 deletions(-) diff --git a/crates/core/src/object.rs b/crates/core/src/object.rs index 7582fa88..9323c478 100644 --- a/crates/core/src/object.rs +++ b/crates/core/src/object.rs @@ -346,6 +346,22 @@ impl Object { } } + /// The kind name for an encoding's leading tag byte, as [`Object::kind_name`] + /// reports it; `None` for a tag no current object uses. + pub fn kind_name_of_tag(tag: u8) -> Option<&'static str> { + Some(match tag { + TAG_BLOB => "blob", + TAG_TREE => "tree", + TAG_SNAPSHOT => "snapshot", + TAG_SECRET => "secret", + TAG_SIGNATURE => "signature", + TAG_TRANSCRIPT => "transcript", + TAG_SEALED => "sealed", + TAG_MANIFEST => "manifest", + _ => return None, + }) + } + pub fn kind_name(&self) -> &'static str { match self { Object::Blob(_) => "blob", diff --git a/crates/core/src/store.rs b/crates/core/src/store.rs index e0151c8b..17dda992 100644 --- a/crates/core/src/store.rs +++ b/crates/core/src/store.rs @@ -255,6 +255,48 @@ impl Store { self.existing_loose_path(id).is_some() || self.pack_index.contains_key(id) } + /// The kind name of a stored object (see [`Object::kind_name`]), read from + /// its first encoded byte without loading the rest or admitting it to the + /// cache, so asking about a large blob is cheap. It does not verify the + /// object's hash (reads do); ingest already did. + pub fn peek_kind(&self, id: &ObjectId) -> Result<&'static str> { + use std::io::{BufReader, Read, Seek, SeekFrom}; + const ZSTD_MAGIC: [u8; 4] = [0x28, 0xb5, 0x2f, 0xfd]; + if let Some(resident) = self.resident.get(id) { + return Ok(resident.obj.kind_name()); + } + if self.spilled.contains_key(id) { + return Ok("blob"); + } + let mut first = [0_u8; 1]; + if let Some(path) = self.existing_loose_path(id) { + let mut file = std::fs::File::open(&path)?; + let mut prefix = [0_u8; 4]; + let n = file.read(&mut prefix)?; + if n == prefix.len() && prefix == ZSTD_MAGIC { + let mut decoder = + zstd::stream::read::Decoder::new(BufReader::new(std::fs::File::open(&path)?))?; + decoder.read_exact(&mut first)?; + } else if n > 0 { + first[0] = prefix[0]; + } else { + return Err(Error::Malformed(format!("object {id} is empty"))); + } + } else if let Some(loc) = self.pack_index.get(id) { + let mut file = std::fs::File::open(&loc.pack_path)?; + file.seek(SeekFrom::Start(loc.offset))?; + let mut len_buf = [0_u8; 4]; + file.read_exact(&mut len_buf)?; + let len = u32::from_le_bytes(len_buf) as u64; + let mut decoder = zstd::stream::read::Decoder::new(BufReader::new(file.take(len)))?; + decoder.read_exact(&mut first)?; + } else { + return Err(Error::NotFound(*id)); + } + Object::kind_name_of_tag(first[0]) + .ok_or_else(|| Error::Malformed(format!("object {id} has an unknown kind tag"))) + } + /// Whether the object is on disk right now, ignoring the resident cache /// and any spill: a loose file, or a pack this store has indexed that /// still exists. A stat per call, so it is cheap enough to confirm each id @@ -783,6 +825,43 @@ fn write_atomic(path: &std::path::Path, bytes: &[u8]) -> Result<()> { mod tests { use super::*; + /// `peek_kind` names a stored object's kind from disk alone, loose or + /// packed, without loading it. + #[test] + fn peek_kind_reads_the_kind_byte_from_disk() { + let dir = std::env::temp_dir().join(format!("scl-peek-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let objs = [ + Object::blob(vec![7; 100_000]), + Object::Tree(crate::object::Tree::default()), + Object::Sealed(crate::object::SealedObj { + payload: std::sync::Arc::from(vec![1, 2, 3].into_boxed_slice()), + }), + ]; + let mut st = Store::open_persistent(&dir, 1 << 20).unwrap(); + for o in &objs { + st.put(o.clone()).unwrap(); + } + // A fresh store has nothing resident, so this is the disk path. + let fresh = Store::open_persistent(&dir, 1 << 20).unwrap(); + for o in &objs { + assert_eq!(fresh.peek_kind(&o.id()).unwrap(), o.kind_name()); + } + assert!(fresh.peek_kind(&ObjectId::of(b"absent")).is_err()); + // The same from a pack, once the loose files are gone. + let ids: Vec = objs.iter().map(Object::id).collect(); + st.write_pack(&ids).unwrap(); + for id in &ids { + st.delete(id).unwrap(); + } + let packed = Store::open_persistent(&dir, 1 << 20).unwrap(); + for o in &objs { + assert_eq!(packed.peek_kind(&o.id()).unwrap(), o.kind_name()); + } + drop((st, fresh, packed)); + std::fs::remove_dir_all(&dir).unwrap(); + } + fn blob(n: usize, fill: u8) -> Object { Object::blob(vec![fill; n]) } diff --git a/crates/repo/src/bucket_transport.rs b/crates/repo/src/bucket_transport.rs index 7e271323..97df18fd 100644 --- a/crates/repo/src/bucket_transport.rs +++ b/crates/repo/src/bucket_transport.rs @@ -722,10 +722,6 @@ impl crate::ref_guard::RefObjects for RefView<'_> { let obj = Object::decode(&self.bucket.object_bytes(id)?)?; Ok(Some(crate::ref_guard::Node::of(obj))) } - - fn held(&mut self, id: &ObjectId) -> Result { - Ok(self.pending.nodes.contains_key(id) || self.on_chain(id)) - } } /// Attempts `update_ref` makes at the manifest CAS before it gives up (the diff --git a/crates/repo/src/ref_guard.rs b/crates/repo/src/ref_guard.rs index 5d21a982..99b8da36 100644 --- a/crates/repo/src/ref_guard.rs +++ b/crates/repo/src/ref_guard.rs @@ -38,7 +38,8 @@ pub(crate) enum Node { Manifest(BranchManifest), Blob, Sealed, - /// A secret, signature or transcript. + Secret, + /// A signature or transcript. Other, } @@ -50,18 +51,52 @@ impl Node { Object::Manifest(m) => Node::Manifest(m), Object::Blob(_) => Node::Blob, Object::Sealed(_) => Node::Sealed, + Object::Secret(_) => Node::Secret, _ => Node::Other, } } - fn kind(&self) -> &'static str { + fn kind(&self) -> Kind { match self { - Node::Snapshot(_) => "snapshot", - Node::Tree(_) => "tree", - Node::Manifest(_) => "manifest", - Node::Blob => "blob", - Node::Sealed => "sealed object", - Node::Other => "secret, signature or transcript", + Node::Blob => Kind::Blob, + Node::Sealed => Kind::Sealed, + Node::Secret => Kind::Secret, + Node::Snapshot(_) => Kind::Other("snapshot"), + Node::Tree(_) => Kind::Other("tree"), + Node::Manifest(_) => Kind::Other("manifest"), + Node::Other => Kind::Other("signature or transcript"), + } + } +} + +/// An object's kind, for the places the rule only needs that and not the +/// content (file content, sealed closure entries, secrets). +#[derive(Clone, Copy, PartialEq, Eq)] +pub(crate) enum Kind { + Blob, + Sealed, + Secret, + /// Anything else, by name. + Other(&'static str), +} + +impl Kind { + /// From a kind name as [`Object::kind_name`] spells it. + pub(crate) fn of_name(name: &'static str) -> Kind { + match name { + "blob" => Kind::Blob, + "sealed" => Kind::Sealed, + "secret" => Kind::Secret, + other => Kind::Other(other), + } + } + + fn name(self) -> &'static str { + match self { + Kind::Blob => "blob", + Kind::Sealed => "sealed object", + Kind::Secret => "secret", + Kind::Other(n) => n, } } } @@ -73,13 +108,12 @@ impl Node { pub(crate) trait RefObjects { /// The object, or `None` when it is not held. fn node(&mut self, id: &ObjectId) -> Result>; - /// Like [`RefObjects::node`], for an object expected to be a file's - /// content: an implementation may avoid loading a large blob to say so. - fn leaf(&mut self, id: &ObjectId) -> Result> { - self.node(id) + /// The kind of an object expected to be file content, a sealed closure + /// entry or a secret, or `None` when it is not held. An implementation + /// that can say so without loading the object should (a blob can be large). + fn leaf(&mut self, id: &ObjectId) -> Result> { + Ok(self.node(id)?.map(|n| n.kind())) } - /// Whether the object is held, without reading it where that is cheaper. - fn held(&mut self, id: &ObjectId) -> Result; } /// Tree and file-content ids already verified in one check. @@ -103,22 +137,33 @@ fn read(src: &mut S, id: &ObjectId, what: &str) -> Result { fn read_snapshot(src: &mut S, id: &ObjectId, what: &str) -> Result { match read(src, id, what)? { Node::Snapshot(s) => Ok(s), - other => refuse(format!("{what} {id} is a {}, not a snapshot", other.kind())), + other => refuse(format!( + "{what} {id} is a {}, not a snapshot", + other.kind().name() + )), } } fn read_manifest(src: &mut S, id: &ObjectId, what: &str) -> Result { match read(src, id, what)? { Node::Manifest(m) => Ok(m), - other => refuse(format!("{what} {id} is a {}, not a manifest", other.kind())), + other => refuse(format!( + "{what} {id} is a {}, not a manifest", + other.kind().name() + )), } } -fn require_held(src: &mut S, id: &ObjectId, what: &str) -> Result<()> { - if src.held(id)? { - Ok(()) - } else { - refuse(format!("{what} {id} is not held here")) +/// `id` must be held and be of kind `want`. +fn require_kind(src: &mut S, id: &ObjectId, want: Kind, what: &str) -> Result<()> { + match src.leaf(id)? { + Some(k) if k == want => Ok(()), + Some(k) => refuse(format!( + "{what} {id} is a {}, not a {}", + k.name(), + want.name() + )), + None => refuse(format!("{what} {id} is not held here")), } } @@ -134,7 +179,7 @@ pub(crate) fn check_ref_move( if !matches!(new_node, Node::Snapshot(_) | Node::Manifest(_)) { return refuse(format!( "ref target {new} is a {}, not a snapshot or manifest", - new_node.kind() + new_node.kind().name() )); } if let Some(cur) = current { @@ -142,8 +187,8 @@ pub(crate) fn check_ref_move( if std::mem::discriminant(&cur_node) != std::mem::discriminant(&new_node) { return refuse(format!( "branch tip {cur} is a {} but {new} is a {}; a branch cannot change kind", - cur_node.kind(), - new_node.kind() + cur_node.kind().name(), + new_node.kind().name() )); } } @@ -196,14 +241,22 @@ fn verify_snapshots( } let snap = read_snapshot(src, &id, "commit")?; let mut parent_roots = Vec::with_capacity(snap.parents.len()); + let mut parent_secrets = Vec::with_capacity(snap.parents.len()); for p in &snap.parents { - parent_roots.push(read_snapshot(src, p, "parent commit")?.root); + let parent = read_snapshot(src, p, "parent commit")?; + parent_roots.push(parent.root); + parent_secrets.push(parent.secrets); if seen.insert(*p) { queue.push_back(*p); } } - for sid in snap.secrets.values() { - require_held(src, sid, "secret")?; + // Secret registry entries must be secrets; one a parent already has + // is inside its verified closure. + for (name, sid) in &snap.secrets { + if parent_secrets.iter().any(|m| m.get(name) == Some(sid)) { + continue; + } + require_kind(src, sid, Kind::Secret, "secret registry entry")?; } verify_tree(src, snap.root, &parent_roots, done)?; } @@ -230,7 +283,12 @@ fn verify_tree( } let tree = match read(src, &id, "tree")? { Node::Tree(t) => t, - other => return refuse(format!("tree {id} is a {}, not a tree", other.kind())), + other => { + return refuse(format!( + "tree {id} is a {}, not a tree", + other.kind().name() + )) + } }; // A parent tree that is not held cannot vouch for anything; the // commit that names it fails its own check. @@ -256,7 +314,7 @@ fn verify_tree( continue; } match src.leaf(&e.id)? { - Some(Node::Blob | Node::Sealed) => { + Some(Kind::Blob | Kind::Sealed) => { seen.leaves.insert(e.id); } Some(other) => { @@ -264,7 +322,7 @@ fn verify_tree( "file entry {} names {}, a {}, not file content", e.name, e.id, - other.kind() + other.name() )) } None => return refuse(format!("file content {} is not held here", e.id)), @@ -286,16 +344,21 @@ fn verify_manifests( new: ObjectId, new_manifest: BranchManifest, ) -> Result<()> { - let mut chain = Vec::new(); + let mut chain: Vec = Vec::new(); + // The manifest the chain stops at (the current tip, or a trusted tip for a + // new branch), whose closure is already verified. + let mut terminal: Option = None; let (mut id, mut m) = (new, new_manifest); let mut found = false; loop { if Some(id) == current { found = true; + terminal = Some(m); break; } // A new branch needs no ancestry, so it can stop at a trusted tip. if current.is_none() && boundary.contains(&id) { + terminal = Some(m); break; } let prev = m.prev; @@ -312,7 +375,7 @@ fn verify_manifests( return Err(Error::NonFastForward); } let mut done = Verified::default(); - for m in &chain { + for (i, m) in chain.iter().enumerate() { let roots: Vec = std::iter::once(m.base) .chain(m.anchors.iter().copied()) .collect(); @@ -320,9 +383,162 @@ fn verify_manifests( read_snapshot(src, r, "manifest base or anchor")?; } verify_snapshots(src, boundary, &roots, &mut done)?; + // Every closure entry must be a sealed object: one naming a public + // snapshot would make reachability mark it seen without walking it. + // An entry the previous manifest already lists is inside its verified + // closure (that manifest is the next in the chain, or the terminal). + let known: HashSet<&ObjectId> = chain + .get(i + 1) + .or(terminal.as_ref()) + .map(|o| o.closure.iter().collect()) + .unwrap_or_default(); for c in &m.closure { - require_held(src, c, "sealed object")?; + if !known.contains(c) { + require_kind(src, c, Kind::Sealed, "manifest closure entry")?; + } } } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + use scl_core::{FileMode, SealedObj, TreeEntry}; + use std::collections::HashMap; + + /// An in-memory object source that records every id it is asked about. + #[derive(Default)] + struct Counting { + objects: HashMap, + asked: Vec, + } + + impl Counting { + fn add(&mut self, o: Object) -> ObjectId { + let id = o.id(); + self.objects.insert(id, Node::of(o)); + id + } + } + + impl RefObjects for Counting { + fn node(&mut self, id: &ObjectId) -> Result> { + self.asked.push(*id); + Ok(self.objects.get(id).cloned()) + } + } + + fn tree(entries: &[(&str, EntryKind, ObjectId)]) -> Object { + Object::Tree(Tree::new( + entries + .iter() + .map(|(name, kind, id)| TreeEntry { + name: name.to_string(), + kind: *kind, + id: *id, + mode: FileMode::FILE, + perms: 0, + }) + .collect(), + )) + } + + fn snap(root: ObjectId, parents: &[ObjectId]) -> Object { + Object::Snapshot(Snapshot { + root, + parents: parents.to_vec(), + author: "t".into(), + timestamp: 0, + message: String::new(), + secrets: Default::default(), + protection: Default::default(), + }) + } + + /// Only what a push changed is looked at: unchanged files in a changed + /// directory, and unchanged closure entries and secrets, are inside the + /// parent's verified closure and are never read (a file can be large). + #[test] + fn unchanged_content_is_not_read() { + let mut src = Counting::default(); + let big: Vec = (0..3u8) + .map(|i| src.add(Object::blob(vec![i; 64]))) + .collect(); + let entry = |name: &'static str, id| (name, EntryKind::Blob, id); + let dir_old = src.add(tree(&[ + entry("big0", big[0]), + entry("big1", big[1]), + entry("big2", big[2]), + ])); + let root_old = src.add(tree(&[("d", EntryKind::Tree, dir_old)])); + let secret = src.add(Object::Secret(scl_core::Secret { + name: "k".into(), + nonce: vec![], + ciphertext: vec![], + wrapped_keys: vec![], + })); + let parent = { + let Object::Snapshot(mut s) = snap(root_old, &[]) else { + unreachable!() + }; + s.secrets.insert("k".into(), secret); + src.add(Object::Snapshot(s)) + }; + + // The child adds one file to the directory and keeps everything else. + let added = src.add(Object::blob(b"new file".to_vec())); + let dir_new = src.add(tree(&[ + entry("big0", big[0]), + entry("big1", big[1]), + entry("big2", big[2]), + entry("new", added), + ])); + let root_new = src.add(tree(&[("d", EntryKind::Tree, dir_new)])); + let child = { + let Object::Snapshot(mut s) = snap(root_new, &[parent]) else { + unreachable!() + }; + s.secrets.insert("k".into(), secret); + src.add(Object::Snapshot(s)) + }; + + src.asked.clear(); + check_ref_move(&mut src, &BTreeSet::from([parent]), Some(parent), child).unwrap(); + for unchanged in big.iter().chain([&secret]) { + assert!( + !src.asked.contains(unchanged), + "{unchanged} is unchanged and must not be read" + ); + } + assert!(src.asked.contains(&added), "the new file is checked"); + + // The same for a manifest: entries its predecessor already lists. + let sealed: Vec = (0..3u8) + .map(|i| { + src.add(Object::Sealed(SealedObj { + payload: std::sync::Arc::from(vec![i].into_boxed_slice()), + })) + }) + .collect(); + let manifest = |prev: Option, closure: &[ObjectId]| { + Object::Manifest(scl_core::BranchManifest { + base: parent, + prev, + anchors: vec![], + closure: closure.to_vec(), + index_ct: vec![], + kek_wraps: vec![], + }) + }; + let m1 = src.add(manifest(None, &sealed[..2])); + let m2 = src.add(manifest(Some(m1), &sealed)); + src.asked.clear(); + check_ref_move(&mut src, &BTreeSet::from([m1]), Some(m1), m2).unwrap(); + assert!(!src.asked.contains(&sealed[0]) && !src.asked.contains(&sealed[1])); + assert!( + src.asked.contains(&sealed[2]), + "the new closure entry is checked" + ); + } +} diff --git a/crates/repo/src/refs.rs b/crates/repo/src/refs.rs index 438cdaea..48db8f0b 100644 --- a/crates/repo/src/refs.rs +++ b/crates/repo/src/refs.rs @@ -164,7 +164,9 @@ pub fn list_heads(layout: &Layout) -> Result> { continue; } let name = e.file_name().to_string_lossy().into_owned(); - if name.contains(".tmp") { + // A ref being written has a dot-prefixed temp file (`atomic_write`); no + // branch name starts with a dot, so only those are skipped. + if name.starts_with('.') { continue; } // A ref gone since the directory was read is simply not there. @@ -201,7 +203,7 @@ pub fn list_remote_tips(layout: &Layout) -> Result Result> { } /// Write `bytes` to `path` via a temp file + rename so a reader never observes -/// a half-written ref. The temp sibling is per-process (`..tmp`) so -/// writers in different processes never clobber each other's temp file; the -/// rename itself is atomic. The single-writer repo lock +/// a half-written ref. The temp sibling is per-process and per-call +/// (`....tmp`) so writers never clobber each other's temp file; +/// the rename itself is atomic. The single-writer repo lock /// ([`crate::lock::RepoLock`]) still serializes the final ref content. +/// +/// The temp file's name starts with a dot, which no branch name can +/// (`validate_branch_name`), so a directory listing can tell a ref being +/// written from a branch that happens to be called `x.1.tmp`. fn atomic_write(path: &std::path::Path, bytes: &[u8]) -> Result<()> { - Ok(scl_core::fsutil::atomic_write_durable(path, bytes)?) + use std::io::Write; + use std::sync::atomic::{AtomicU64, Ordering}; + static COUNTER: AtomicU64 = AtomicU64::new(0); + let (Some(parent), Some(name)) = (path.parent(), path.file_name()) else { + return Err(Error::BadRef(format!("bad ref path {}", path.display()))); + }; + let n = COUNTER.fetch_add(1, Ordering::Relaxed); + let tmp = parent.join(format!( + ".{}.{}.{n}.tmp", + name.to_string_lossy(), + std::process::id() + )); + { + let mut f = std::fs::File::create(&tmp)?; + f.write_all(bytes)?; + f.sync_all()?; + } + if let Err(e) = std::fs::rename(&tmp, path) { + let _ = std::fs::remove_file(&tmp); + return Err(e.into()); + } + Ok(scl_core::fsutil::fsync_dir(parent)?) } #[cfg(test)] @@ -356,6 +383,36 @@ mod tests { std::fs::remove_dir_all(&layout.root).unwrap(); } + /// A branch called `x.1.tmp` is a branch; only a ref's in-flight temp file + /// (dot-prefixed, which no branch name can be) is skipped. + #[test] + fn list_heads_lists_tmp_looking_branches_and_skips_temp_files() { + let layout = tmp_layout("tmp-names"); + let id = ObjectId::of(b"tip"); + for name in ["x.1.tmp", "release.tmp-fix", "main"] { + write_branch_tip(&layout, name, &id).unwrap(); + } + let dir = layout.refs_heads_dir(); + std::fs::write(dir.join(".main.4242.1.tmp"), b"half a ref").unwrap(); + let mut names: Vec = list_heads(&layout) + .unwrap() + .into_iter() + .map(|(n, _)| n) + .collect(); + names.sort(); + assert_eq!(names, ["main", "release.tmp-fix", "x.1.tmp"]); + // The writer leaves no temp file behind. + let leftovers = std::fs::read_dir(&dir) + .unwrap() + .filter(|e| { + let n = e.as_ref().unwrap().file_name(); + n.to_string_lossy() != ".main.4242.1.tmp" && n.to_string_lossy().starts_with('.') + }) + .count(); + assert_eq!(leftovers, 0); + std::fs::remove_dir_all(&layout.root).unwrap(); + } + #[test] fn write_branch_tip_rejects_unsafe_names() { let layout = tmp_layout("write-branch-unsafe"); diff --git a/crates/repo/src/test_support.rs b/crates/repo/src/test_support.rs index 759e5bfe..680584fd 100644 --- a/crates/repo/src/test_support.rs +++ b/crates/repo/src/test_support.rs @@ -106,17 +106,19 @@ pub(crate) fn ref_rules_hold(t: &T) { .collect(), )) }; - let snap = |root: ObjectId, parents: &[ObjectId], msg: &str| { - Object::Snapshot(Snapshot { - root, - parents: parents.to_vec(), - author: "t".into(), - timestamp: 0, - message: msg.into(), - secrets: Default::default(), - protection: Default::default(), - }) - }; + let snap_with = + |root: ObjectId, parents: &[ObjectId], msg: &str, secrets: &[(&str, ObjectId)]| { + Object::Snapshot(Snapshot { + root, + parents: parents.to_vec(), + author: "t".into(), + timestamp: 0, + message: msg.into(), + secrets: secrets.iter().map(|(n, id)| (n.to_string(), *id)).collect(), + protection: Default::default(), + }) + }; + let snap = |root: ObjectId, parents: &[ObjectId], msg: &str| snap_with(root, parents, msg, &[]); let manifest = |base: ObjectId, prev: Option, closure: &[ObjectId]| { Object::Manifest(BranchManifest { base, @@ -250,6 +252,25 @@ pub(crate) fn ref_rules_hold(t: &T) { assert!(mv(&[&root2], "fresh", &snap(root2.id(), &[ghost], "y"), None).is_err()); assert_eq!(tip_of("fresh"), None); + // Secret registry entries must be secrets. + let secret = Object::Secret(scl_core::Secret { + name: "k".into(), + nonce: vec![0; 24], + ciphertext: vec![1, 2, 3], + wrapped_keys: vec![], + }); + for (what, reg) in [ + ("secret entry names a blob", b1.id()), + ("secret entry names a tree", dir1.id()), + ("secret entry is absent", ghost), + ] { + let c = snap_with(root2.id(), &[], what, &[("k", reg)]); + assert!(mv(&[&root2], "sec", &c, None).is_err(), "{what}"); + assert_eq!(tip_of("sec"), None, "{what} created the ref"); + } + let with_secret = snap_with(root2.id(), &[], "sec", &[("k", secret.id())]); + mv(&[&secret], "sec", &with_secret, None).unwrap(); + // Private branch: a manifest's `prev` chain is its ancestry. let m1 = manifest(c1.id(), None, &[sealed.id()]); let m2 = manifest(c1.id(), Some(m1.id()), &[sealed.id()]); @@ -261,6 +282,20 @@ pub(crate) fn ref_rules_hold(t: &T) { None, ), ("absent base", vec![], manifest(ghost, None, &[]), None), + // A closure entry must be a sealed object. One naming the public base + // would make the reachability walk mark it seen without walking it. + ( + "closure names a snapshot", + vec![], + manifest(c1.id(), None, &[c1.id()]), + None, + ), + ( + "closure names a blob", + vec![], + manifest(c1.id(), None, &[b1.id()]), + None, + ), ]; for (what, objs, new, old) in cases { assert!(mv(&objs, "priv", &new, old).is_err(), "{what}"); @@ -276,6 +311,10 @@ pub(crate) fn ref_rules_hold(t: &T) { manifest(c1.id(), Some(m2.id()), &[absent_blob.id()]), ), ("manifest absent base", manifest(ghost, Some(m2.id()), &[])), + ( + "manifest closure names a snapshot", + manifest(c1.id(), Some(m2.id()), &[sealed.id(), c1.id()]), + ), ("snapshot over manifest", snap(root2.id(), &[m2.id()], "z")), ("snapshot target", c2.clone()), ]; diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index 3ba962c0..860f2da7 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -240,7 +240,7 @@ impl LocalTransport { current: Option, new: ObjectId, ) -> Result> { - use crate::ref_guard::{Node, RefObjects}; + use crate::ref_guard::{Kind, Node, RefObjects}; struct Session<'a> { store: &'a mut Store, relied_on: std::collections::BTreeSet, @@ -270,24 +270,13 @@ impl LocalTransport { self.relied_on.insert(*id); Ok(Some(Node::of(self.store.get(id)?))) } - fn leaf(&mut self, id: &ObjectId) -> Result> { + fn leaf(&mut self, id: &ObjectId) -> Result> { if !self.present(id) { return Ok(None); } self.relied_on.insert(*id); - // Measuring streams and hash-checks a blob without loading it. - match self.store.blob_size(id) { - Ok(_) => Ok(Some(Node::Blob)), - Err(scl_core::Error::WrongKind(..)) => Ok(Some(Node::of(self.store.get(id)?))), - Err(e) => Err(e.into()), - } - } - fn held(&mut self, id: &ObjectId) -> Result { - let held = self.present(id); - if held { - self.relied_on.insert(*id); - } - Ok(held) + // Reads the kind byte only: a file's content can be large. + Ok(Some(Kind::of_name(self.store.peek_kind(id)?))) } } let tips = self.list_refs()?.into_iter().map(|(_, t)| t).collect(); @@ -1178,7 +1167,7 @@ mod tests { let c1 = put_commit(&t, "c1", None); t.update_ref("main", &c1, None).unwrap(); std::fs::write( - layout.refs_heads_dir().join("other.4242.tmp"), + layout.refs_heads_dir().join(".other.4242.1.tmp"), b"half a ref", ) .unwrap(); From 7b76a439d43cc55866329fa74350e31e95b07361 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 12:01:06 +0200 Subject: [PATCH 5/8] fix(refs): stage ref writes under a bounded dot-prefixed name --- crates/repo/src/refs.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/crates/repo/src/refs.rs b/crates/repo/src/refs.rs index 48db8f0b..eaedcea3 100644 --- a/crates/repo/src/refs.rs +++ b/crates/repo/src/refs.rs @@ -233,15 +233,12 @@ fn atomic_write(path: &std::path::Path, bytes: &[u8]) -> Result<()> { use std::io::Write; use std::sync::atomic::{AtomicU64, Ordering}; static COUNTER: AtomicU64 = AtomicU64::new(0); - let (Some(parent), Some(name)) = (path.parent(), path.file_name()) else { + let (Some(parent), Some(_)) = (path.parent(), path.file_name()) else { return Err(Error::BadRef(format!("bad ref path {}", path.display()))); }; let n = COUNTER.fetch_add(1, Ordering::Relaxed); - let tmp = parent.join(format!( - ".{}.{}.{n}.tmp", - name.to_string_lossy(), - std::process::id() - )); + // Bounded and independent of the branch name, so a long name still fits NAME_MAX. + let tmp = parent.join(format!(".tmp.{}.{n}", std::process::id())); { let mut f = std::fs::File::create(&tmp)?; f.write_all(bytes)?; @@ -393,7 +390,7 @@ mod tests { write_branch_tip(&layout, name, &id).unwrap(); } let dir = layout.refs_heads_dir(); - std::fs::write(dir.join(".main.4242.1.tmp"), b"half a ref").unwrap(); + std::fs::write(dir.join(".tmp.4242.1"), b"half a ref").unwrap(); let mut names: Vec = list_heads(&layout) .unwrap() .into_iter() @@ -406,7 +403,7 @@ mod tests { .unwrap() .filter(|e| { let n = e.as_ref().unwrap().file_name(); - n.to_string_lossy() != ".main.4242.1.tmp" && n.to_string_lossy().starts_with('.') + n.to_string_lossy() != ".tmp.4242.1" && n.to_string_lossy().starts_with('.') }) .count(); assert_eq!(leftovers, 0); From 0ad39e9b8faa685f17d21e6d2926a6c33d181fb8 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 12:12:48 +0200 Subject: [PATCH 6/8] fix(transport): confirm trusted branch tips are still tips under the lock The unlocked walk stops at other branches' tips as already connected. If such a branch was removed or rewritten before the lock was taken and gc pruned its closure, the locked check (which only re-confirmed objects the walk read) could publish a dangling ref. `check_ref_move` now returns the tips the walk actually stopped at, and `commit_ref_move` requires each to still be the tip of some local branch (one `refs::list_heads` read under the lock) or refuses with the usual "retry the push" error. Bucket remotes: the manifest CAS the check read from is what the commit lands against, so those tips are still tips, and compaction never deletes what the chain names. The check skipped on a CAS retry is now reused only while the tips it trusted are still in the refreshed view. --- crates/repo/src/bucket_transport.rs | 32 ++++++---- crates/repo/src/ref_guard.rs | 27 ++++++-- crates/repo/src/transport.rs | 98 ++++++++++++++++++++++++----- 3 files changed, 124 insertions(+), 33 deletions(-) diff --git a/crates/repo/src/bucket_transport.rs b/crates/repo/src/bucket_transport.rs index 97df18fd..c5b27b5c 100644 --- a/crates/repo/src/bucket_transport.rs +++ b/crates/repo/src/bucket_transport.rs @@ -889,9 +889,14 @@ impl Transport for BucketTransport { // never referenced — correct per spec; `compact` sweeps it later, ADR-0057). let pending: Vec = std::mem::take(&mut *self.pending_packs.borrow_mut()); let pending_objects = std::mem::take(&mut *self.pending_objects.borrow_mut()); - // The ref rule's verdict for one current tip; a CAS retry that finds the - // same tip need not repeat the walk (objects only accumulate). - let mut checked_against: Option> = None; + // The ref rule's verdict for one current tip and the tips it trusted; a + // CAS retry that finds the same tip, with those tips still in place, + // need not repeat the walk (objects only accumulate). The commit itself + // is a CAS on the manifest the check read, so the tips it trusted are + // still tips when it lands, and compaction never deletes what the + // chain names. + let mut checked_against: Option<(Option, std::collections::BTreeSet)> = + None; for attempt in 0..MAX_CAS_RETRIES { if attempt > 0 { std::thread::sleep(cas_backoff(attempt)); @@ -923,19 +928,22 @@ impl Transport for BucketTransport { // be our client (`sc serve --store`, or a direct bucket writer): // the shared ref rule (kind, ancestry, connectivity) runs here, // over this call's pending objects and the chain. - if checked_against != Some(current) { - let tips = self - .view - .borrow() - .as_ref() - .map(|v| v.refs.values().copied().collect()) - .unwrap_or_default(); + let tips: std::collections::BTreeSet = self + .view + .borrow() + .as_ref() + .map(|v| v.refs.values().copied().collect()) + .unwrap_or_default(); + let still_valid = checked_against + .as_ref() + .is_some_and(|(c, trusted)| *c == current && trusted.is_subset(&tips)); + if !still_valid { let mut objects = RefView { bucket: self, pending: &pending_objects, }; - crate::ref_guard::check_ref_move(&mut objects, &tips, current, *id)?; - checked_against = Some(current); + let trusted = crate::ref_guard::check_ref_move(&mut objects, &tips, current, *id)?; + checked_against = Some((current, trusted)); } // Claim a log slot for this attempt. `put_new` on `log/` is // the claim: if another writer already landed that seq, we didn't diff --git a/crates/repo/src/ref_guard.rs b/crates/repo/src/ref_guard.rs index 99b8da36..b4b559fe 100644 --- a/crates/repo/src/ref_guard.rs +++ b/crates/repo/src/ref_guard.rs @@ -169,12 +169,15 @@ fn require_kind(src: &mut S, id: &ObjectId, want: Kind, what: &st /// Decide whether branch `current` (its tip, `None` for a new branch) may move /// to `new`. `tips` is every existing ref's tip, trusted as already connected. +/// Returns the tips the verdict actually leaned on (where the walk stopped), so +/// the caller can confirm each is still a tip when the move is applied: a tip +/// that was removed or rewritten meanwhile no longer vouches for its history. pub(crate) fn check_ref_move( src: &mut S, tips: &BTreeSet, current: Option, new: ObjectId, -) -> Result<()> { +) -> Result> { let new_node = read(src, &new, "ref target")?; if !matches!(new_node, Node::Snapshot(_) | Node::Manifest(_)) { return refuse(format!( @@ -194,6 +197,7 @@ pub(crate) fn check_ref_move( } let mut boundary = tips.clone(); boundary.extend(current); + let mut trusted = BTreeSet::new(); match new_node { Node::Snapshot(_) => { if let Some(cur) = current { @@ -201,11 +205,18 @@ pub(crate) fn check_ref_move( return Err(Error::NonFastForward); } } - verify_snapshots(src, &boundary, &[new], &mut Verified::default()) + verify_snapshots( + src, + &boundary, + &mut trusted, + &[new], + &mut Verified::default(), + )?; } - Node::Manifest(m) => verify_manifests(src, &boundary, current, new, m), + Node::Manifest(m) => verify_manifests(src, &boundary, &mut trusted, current, new, m)?, _ => unreachable!("kind checked above"), } + Ok(trusted) } /// Is `target` an ancestor of `from` over snapshot parents (early exit)? @@ -226,10 +237,12 @@ fn snapshot_reaches(src: &mut S, from: ObjectId, target: ObjectId } /// Connectivity of the snapshot history reachable from `starts`, stopping at -/// `boundary`. `done` collects tree and blob ids already verified. +/// `boundary`, which it records in `trusted` as it stops. `done` collects tree +/// and blob ids already verified. fn verify_snapshots( src: &mut S, boundary: &BTreeSet, + trusted: &mut BTreeSet, starts: &[ObjectId], done: &mut Verified, ) -> Result<()> { @@ -237,6 +250,7 @@ fn verify_snapshots( let mut queue: VecDeque = seen.iter().copied().collect(); while let Some(id) = queue.pop_front() { if boundary.contains(&id) { + trusted.insert(id); continue; } let snap = read_snapshot(src, &id, "commit")?; @@ -340,6 +354,7 @@ fn verify_tree( fn verify_manifests( src: &mut S, boundary: &BTreeSet, + trusted: &mut BTreeSet, current: Option, new: ObjectId, new_manifest: BranchManifest, @@ -353,11 +368,13 @@ fn verify_manifests( loop { if Some(id) == current { found = true; + trusted.insert(id); terminal = Some(m); break; } // A new branch needs no ancestry, so it can stop at a trusted tip. if current.is_none() && boundary.contains(&id) { + trusted.insert(id); terminal = Some(m); break; } @@ -382,7 +399,7 @@ fn verify_manifests( for r in &roots { read_snapshot(src, r, "manifest base or anchor")?; } - verify_snapshots(src, boundary, &roots, &mut done)?; + verify_snapshots(src, boundary, trusted, &roots, &mut done)?; // Every closure entry must be a sealed object: one naming a public // snapshot would make reachability mark it seen without walking it. // An entry the previous manifest already lists is inside its verified diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index 860f2da7..f271535c 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -140,6 +140,16 @@ fn refused_by_id(obj: &Object) -> bool { matches!(obj, Object::Sealed(_) | Object::Manifest(_)) } +/// What the unlocked half of a local `update_ref` leaned on, for the locked +/// half to confirm (see `LocalTransport::commit_ref_move`). +struct RefVerdict { + /// Every object the verdict read: each must still be on disk. + relied_on: std::collections::BTreeSet, + /// Tips the walk stopped at as already connected: each must still be a + /// branch tip. + trusted_tips: std::collections::BTreeSet, +} + /// Transport over a remote `.sc/` directory on the local filesystem. pub struct LocalTransport { layout: Layout, @@ -235,11 +245,7 @@ impl LocalTransport { /// the verdict relied on: a cached read can outlive a gc prune, so /// [`LocalTransport::commit_ref_move`] confirms each is still on disk once /// it holds the lock, which gc also takes. - fn verify_ref_move( - &self, - current: Option, - new: ObjectId, - ) -> Result> { + fn verify_ref_move(&self, current: Option, new: ObjectId) -> Result { use crate::ref_guard::{Kind, Node, RefObjects}; struct Session<'a> { store: &'a mut Store, @@ -286,8 +292,11 @@ impl LocalTransport { relied_on: Default::default(), reloaded: false, }; - crate::ref_guard::check_ref_move(&mut session, &tips, current, new)?; - Ok(session.relied_on) + let trusted = crate::ref_guard::check_ref_move(&mut session, &tips, current, new)?; + Ok(RefVerdict { + relied_on: session.relied_on, + trusted_tips: trusted, + }) } /// The locked half of `update_ref`: with the repo lock held, confirm the @@ -298,8 +307,9 @@ impl LocalTransport { branch: &str, id: &ObjectId, expected_old: Option<&ObjectId>, - relied_on: &std::collections::BTreeSet, + verdict: &RefVerdict, ) -> Result<()> { + let relied_on = &verdict.relied_on; let _lock = RepoLock::acquire(&self.layout)?; // Revalidate inside the lock: the checks before it ran unlocked, so the // ref may have moved since (two concurrent pushes). @@ -331,6 +341,20 @@ impl LocalTransport { if current.as_ref() != expected_old { return Err(Error::NonFastForward); } + // The walk stopped at other branches' tips as already connected. If one + // was removed or rewritten since, gc may have pruned what it vouched for. + if !verdict.trusted_tips.is_empty() { + let heads: std::collections::BTreeSet = + crate::refs::list_heads(&self.layout)? + .into_iter() + .map(|(_, t)| t) + .collect(); + if let Some(t) = verdict.trusted_tips.iter().find(|t| !heads.contains(t)) { + return Err(Error::InvalidArgument(format!( + "ref update refused: {t} is no longer a branch tip, so its history was not vouched for; retry the push" + ))); + } + } // A stat per object, under the lock. A miss rescans the pack directory // once before it counts (a repack moves objects between files). let mut store = self.store.borrow_mut(); @@ -838,8 +862,8 @@ impl Transport for LocalTransport { // the repo-wide lock, from the tip the caller expects; under the lock // only `current == expected_old` and the objects' presence have to be // confirmed, which makes the verified relation the one that is applied. - let relied_on = self.verify_ref_move(expected_old.copied(), *id)?; - self.commit_ref_move(branch, id, expected_old, &relied_on) + let verdict = self.verify_ref_move(expected_old.copied(), *id)?; + self.commit_ref_move(branch, id, expected_old, &verdict) } /// Bounded sender (P25): builds the transfer set into a temp pack file @@ -1136,11 +1160,14 @@ mod tests { let c2 = put_commit(&t, "c2", Some(c1)); t.update_ref("main", &c1, None).unwrap(); - let relied_on = t.verify_ref_move(Some(c1), c2).unwrap(); - assert!(relied_on.contains(&c2), "the walk relied on the new tip"); + let verdict = t.verify_ref_move(Some(c1), c2).unwrap(); + assert!( + verdict.relied_on.contains(&c2), + "the walk relied on the new tip" + ); prune_loose(&layout, &c2); let err = t - .commit_ref_move("main", &c2, Some(&c1), &relied_on) + .commit_ref_move("main", &c2, Some(&c1), &verdict) .unwrap_err(); assert!(err.to_string().contains("no longer on disk"), "{err}"); assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), c1)]); @@ -1150,14 +1177,53 @@ mod tests { drop(t); let t = LocalTransport::open(&layout.root).unwrap(); let c2 = put_commit(&t, "c2", Some(c1)); - let relied_on = t.verify_ref_move(Some(c1), c2).unwrap(); - t.commit_ref_move("main", &c2, Some(&c1), &relied_on) - .unwrap(); + let verdict = t.verify_ref_move(Some(c1), c2).unwrap(); + t.commit_ref_move("main", &c2, Some(&c1), &verdict).unwrap(); assert_eq!(t.list_refs().unwrap(), vec![("main".to_string(), c2)]); drop(t); std::fs::remove_dir_all(&layout.root).unwrap(); } + /// The walk trusts other branches' tips as connected. If such a branch is + /// moved or removed before the lock is taken, gc could have pruned what it + /// vouched for, so the locked half refuses and nothing moves. + #[test] + fn a_trusted_tip_that_stops_being_a_tip_refuses_the_move() { + for remove in [true, false] { + let layout = tmp_remote(if remove { "bound-rm" } else { "bound-mv" }); + let t = LocalTransport::open(&layout.root).unwrap(); + let base = put_commit(&t, "base", None); + let ahead = put_commit(&t, "ahead", Some(base)); + t.update_ref("base-branch", &base, None).unwrap(); + // `feature` is created from the commit `base-branch` points at, so + // the walk stops there and leans on that branch. + let verdict = t.verify_ref_move(None, ahead).unwrap(); + assert!(verdict.trusted_tips.contains(&base)); + if remove { + std::fs::remove_file(layout.ref_path("base-branch")).unwrap(); + } else { + t.update_ref("base-branch", &ahead, Some(&base)).unwrap(); + // ...and the old tip is no longer a tip anywhere. + std::fs::write( + layout.ref_path("base-branch"), + format!("{}\n", put_commit(&t, "elsewhere", None).to_hex()), + ) + .unwrap(); + } + let err = t + .commit_ref_move("feature", &ahead, None, &verdict) + .unwrap_err(); + assert!(err.to_string().contains("no longer a branch tip"), "{err}"); + assert!(!t.list_refs().unwrap().iter().any(|(b, _)| b == "feature")); + // Judged afresh, with the branch where it is now, it lands. + let verdict = t.verify_ref_move(None, ahead).unwrap(); + t.commit_ref_move("feature", &ahead, None, &verdict) + .unwrap(); + drop(t); + std::fs::remove_dir_all(&layout.root).unwrap(); + } + } + /// Enumerating refs while another push writes one must not trip over its /// in-flight temp file. #[test] From 90f87be1763540dd8b2df451d80941d29b7876dc Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 12:33:48 +0200 Subject: [PATCH 7/8] fix(serve): a file entry must name a blob; note the walk cost --- crates/repo/src/ref_guard.rs | 2 +- crates/repo/src/test_support.rs | 3 +++ docs/THREAT-MODEL.md | 4 +++- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/crates/repo/src/ref_guard.rs b/crates/repo/src/ref_guard.rs index b4b559fe..3384604b 100644 --- a/crates/repo/src/ref_guard.rs +++ b/crates/repo/src/ref_guard.rs @@ -328,7 +328,7 @@ fn verify_tree( continue; } match src.leaf(&e.id)? { - Some(Kind::Blob | Kind::Sealed) => { + Some(Kind::Blob) => { seen.leaves.insert(e.id); } Some(other) => { diff --git a/crates/repo/src/test_support.rs b/crates/repo/src/test_support.rs index 680584fd..86ef3519 100644 --- a/crates/repo/src/test_support.rs +++ b/crates/repo/src/test_support.rs @@ -231,10 +231,13 @@ pub(crate) fn ref_rules_hold(t: &T) { ]); let tree_over_blob = tree(&[("a", EntryKind::Tree, b1.id())]); let file_over_tree = tree(&[("a", EntryKind::Blob, dir1.id())]); + // Checkout reads only blobs, so a sealed object as file content would break every checkout. + let file_over_sealed = tree(&[("a", EntryKind::Blob, sealed.id())]); for (what, objs, root) in [ ("file and tree entry share an id", vec![&dirx], &reuse), ("tree entry names a blob", vec![&b1], &tree_over_blob), ("file entry names a tree", vec![&dir1, &b1], &file_over_tree), + ("file entry names a sealed object", vec![&sealed], &file_over_sealed), ] { let c = snap(root.id(), &[c2.id()], what); let mut objs = objs; diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 1e960647..316e3f61 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -523,7 +523,9 @@ too, not just the src-control-side metadata. wrong kind or unreadable is refused. This stops a rewind, a sideways move, a public/private kind flip and a tip whose history dangles, which would break clone, fetch and gc for everyone. It is not a content check: write access - still adds any history and fast-forwards any branch it can name. + still adds any history and fast-forwards any branch it can name. Cost: a push + whose history joins an existing branch below its tip (not at a tip) is walked + back to the root, so an rw client can make a push cost a full-history walk. ## Bucket remotes (`sc+wal://` / `sc+s3://`) — ADR-0046/0057 From c15757572cd20a956a4ac546009971c2ceaac927 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 12:36:38 +0200 Subject: [PATCH 8/8] style: cargo fmt --- crates/repo/src/test_support.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/repo/src/test_support.rs b/crates/repo/src/test_support.rs index 86ef3519..258d2c43 100644 --- a/crates/repo/src/test_support.rs +++ b/crates/repo/src/test_support.rs @@ -237,7 +237,11 @@ pub(crate) fn ref_rules_hold(t: &T) { ("file and tree entry share an id", vec![&dirx], &reuse), ("tree entry names a blob", vec![&b1], &tree_over_blob), ("file entry names a tree", vec![&dir1, &b1], &file_over_tree), - ("file entry names a sealed object", vec![&sealed], &file_over_sealed), + ( + "file entry names a sealed object", + vec![&sealed], + &file_over_sealed, + ), ] { let c = snap(root.id(), &[c2.id()], what); let mut objs = objs;