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 7657e789..17dda992 100644 --- a/crates/core/src/store.rs +++ b/crates/core/src/store.rs @@ -255,6 +255,61 @@ 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 + /// 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 @@ -770,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 11db2d02..c5b27b5c 100644 --- a/crates/repo/src/bucket_transport.rs +++ b/crates/repo/src/bucket_transport.rs @@ -74,6 +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>, + /// 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 @@ -233,6 +237,7 @@ impl BucketTransport { pack_cache: RefCell::new(None), staged: RefCell::new(Vec::new()), pending_packs: RefCell::new(Vec::new()), + pending_objects: RefCell::new(PendingObjects::default()), leaf_cache: RefCell::new(BTreeMap::new()), saw_absent: Cell::new(false), attempt_tag: RefCell::new(None), @@ -675,6 +680,50 @@ impl crate::reachable::ObjectSource for BucketSource<'_> { } } +/// 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, +} + +impl PendingObjects { + fn note(&mut self, id: ObjectId, obj: Object) { + self.nodes.insert(id, crate::ref_guard::Node::of(obj)); + } + + fn extend(&mut self, other: PendingObjects) { + self.nodes.extend(other.nodes); + } +} + +/// 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.on_chain(id) { + return Ok(None); + } + let obj = Object::decode(&self.bucket.object_bytes(id)?)?; + Ok(Some(crate::ref_guard::Node::of(obj))) + } +} + /// 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 +824,11 @@ impl Transport for BucketTransport { if ObjectId::of(bytes) != *id { return Err(Error::CorruptObject(*id)); } + 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(()) } @@ -787,12 +841,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 received = PendingObjects::default(); scl_core::pack::parse_pack_reader(src, |id, obj| { - objects.push((id, obj.encode())); + // 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" + ))); + } + 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_objects.borrow_mut().extend(received); Ok(ids) } @@ -821,6 +888,15 @@ 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_objects = std::mem::take(&mut *self.pending_objects.borrow_mut()); + // 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)); @@ -848,6 +924,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): + // the shared ref rule (kind, ancestry, connectivity) runs here, + // over this call's pending objects and the chain. + 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, + }; + 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 // win it and try the next one — the entry we didn't win becomes @@ -932,6 +1029,7 @@ mod tests { } use super::*; + 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, @@ -1179,6 +1277,69 @@ mod tests { assert!(!broot.exists()); } + /// `sc serve --store` and direct bucket writers get the same server-side + /// ref rules as `LocalTransport` (`ref_guard`), including over objects that + /// are only in a pending pack. + #[test] + 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(); + crate::test_support::ref_rules_hold(&t); + drop(t); + std::fs::remove_dir_all(&broot).unwrap(); + } + + /// 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(), + }; + 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(); + } + #[test] fn put_object_stages_and_update_ref_commits_them() { let broot = std::env::temp_dir().join(format!("scl-bt-stage-{}", std::process::id())); @@ -1347,10 +1508,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 = Object::blob(tag.to_vec()); // any distinct object works as a fake tip - (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(); @@ -1373,8 +1531,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)) }) @@ -1428,8 +1587,8 @@ mod tests { DirBucket::open(&broot).unwrap(), )) .unwrap(); - let obj = Object::blob(format!("agent-{i}").into_bytes()); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + let obj = fake_commit(&format!("agent-{i}"), None); + 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()); @@ -1530,8 +1689,8 @@ mod tests { lost: AtomicU32::new(0), })) .unwrap(); - let obj = Object::blob(b"budget".to_vec()); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + let obj = fake_commit("budget", None); + put_fake(&t, &obj); let res = t.update_ref("main", &obj.id(), None); if lands { res.unwrap(); @@ -1611,8 +1770,8 @@ 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()); - t2.put_object(&next.id(), &next.encode()).unwrap(); + let next = fake_commit("after crash", None); + 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(); @@ -1933,9 +2092,9 @@ 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 (tip, bytes) = (obj.id(), obj.encode()); - t.put_object(&tip, &bytes).unwrap(); + let obj = fake_commit("carry-push", None); + let tip = obj.id(); + put_fake(&t, &obj); t.update_ref("carried", &tip, None).unwrap(); drop(t); @@ -1960,8 +2119,8 @@ 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()); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + let obj = fake_commit(&format!("fold-{i}"), None); + 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 @@ -2037,8 +2196,8 @@ mod tests { ))) .unwrap(); for i in 0..(CHECKPOINT_INTERVAL + 2) { - let obj = Object::blob(format!("foldfail-{i}").into_bytes()); - t.put_object(&obj.id(), &obj.encode()).unwrap(); + let obj = fake_commit(&format!("foldfail-{i}"), None); + 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 793ed320..945e0f2f 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -45,6 +45,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/private.rs b/crates/repo/src/private.rs index 4bb8d06c..52fbaeb2 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -2751,6 +2751,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/ref_guard.rs b/crates/repo/src/ref_guard.rs new file mode 100644 index 00000000..3384604b --- /dev/null +++ b/crates/repo/src/ref_guard.rs @@ -0,0 +1,561 @@ +//! 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. 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), + Blob, + Sealed, + Secret, + /// A signature or transcript. + Other, +} + +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), + Object::Blob(_) => Node::Blob, + Object::Sealed(_) => Node::Sealed, + Object::Secret(_) => Node::Secret, + _ => Node::Other, + } + } + + fn kind(&self) -> Kind { + match self { + 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, + } + } +} + +/// 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>; + /// 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())) + } +} + +/// 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}"))) +} + +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().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().name() + )), + } +} + +/// `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")), + } +} + +/// 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> { + 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().name() + )); + } + 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().name(), + new_node.kind().name() + )); + } + } + let mut boundary = tips.clone(); + boundary.extend(current); + let mut trusted = BTreeSet::new(); + match new_node { + Node::Snapshot(_) => { + if let Some(cur) = current { + if !snapshot_reaches(src, new, cur)? { + return Err(Error::NonFastForward); + } + } + verify_snapshots( + src, + &boundary, + &mut trusted, + &[new], + &mut Verified::default(), + )?; + } + 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)? +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`, 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<()> { + 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) { + trusted.insert(id); + continue; + } + 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 { + 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); + } + } + // 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)?; + } + Ok(()) +} + +/// 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], + seen: &mut Verified, +) -> Result<()> { + let mut stack = vec![(root, parent_roots.to_vec())]; + while let Some((id, parents)) = stack.pop() { + if parents.contains(&id) || !seen.trees.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().name() + )) + } + }; + // 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 seen.leaves.contains(&e.id) { + continue; + } + match src.leaf(&e.id)? { + Some(Kind::Blob) => { + seen.leaves.insert(e.id); + } + Some(other) => { + return refuse(format!( + "file entry {} names {}, a {}, not file content", + e.name, + e.id, + other.name() + )) + } + None => return refuse(format!("file content {} is not held here", 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, + trusted: &mut BTreeSet, + current: Option, + new: ObjectId, + new_manifest: BranchManifest, +) -> Result<()> { + 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; + 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; + } + 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 = Verified::default(); + for (i, m) in chain.iter().enumerate() { + 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, 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 + // 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 { + 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 22f86d3e..eaedcea3 100644 --- a/crates/repo/src/refs.rs +++ b/crates/repo/src/refs.rs @@ -164,10 +164,17 @@ 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; } - 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)); @@ -196,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(_)) = (path.parent(), path.file_name()) else { + return Err(Error::BadRef(format!("bad ref path {}", path.display()))); + }; + let n = COUNTER.fetch_add(1, Ordering::Relaxed); + // 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)?; + 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)] @@ -351,6 +380,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(".tmp.4242.1"), 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() != ".tmp.4242.1" && 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 438354a0..258d2c43 100644 --- a/crates/repo/src/test_support.rs +++ b/crates/repo/src/test_support.rs @@ -56,6 +56,286 @@ pub(crate) fn watchdog<'a>( }); } +/// 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: empty_tree().id(), + parents: parent.into_iter().collect(), + author: "t".into(), + timestamp: 0, + message: tag.into(), + secrets: Default::default(), + protection: Default::default(), + }) +} + +/// 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_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, + 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"); + } + // 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())]); + // 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; + 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(); + 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); + + // 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()]); + 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), + // 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}"); + 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()), &[])), + ( + "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()), + ]; + 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 b0c8d370..f271535c 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}; @@ -141,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, @@ -231,6 +240,141 @@ impl LocalTransport { &self.layout } + /// 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::{Kind, 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); + } + 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); + // 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(); + let mut store = self.store.borrow_mut(); + let mut session = Session { + store: &mut store, + relied_on: Default::default(), + reloaded: false, + }; + 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 + /// 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>, + 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). + 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); + } + // 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(); + 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, /// but instead of collecting every object's encoded bytes into one /// `Vec<(ObjectId, Vec)>` (unbounded RAM) and calling `build_pack`, @@ -653,23 +797,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) } @@ -716,38 +846,24 @@ impl Transport for LocalTransport { id: &ObjectId, expected_old: Option<&ObjectId>, ) -> Result<()> { - 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). - 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); - } + 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(()); } - if current.as_ref() != expected_old { + // A stale expectation is the common, cheap refusal: say so before + // walking anything. + if seen.as_ref() != expected_old { return Err(Error::NonFastForward); } - crate::refs::write_branch_tip(&self.layout, branch, id) + // 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` and the objects' presence have to be + // confirmed, which makes the verified relation the one that is applied. + 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 @@ -965,13 +1081,167 @@ 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); + crate::test_support::put_fake(t, &o); + o.id() + } + + /// 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_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(); + } + + /// 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(); + 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(); + 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)]); + drop(t); + 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 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), &verdict) + .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 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] + 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.1.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 @@ -979,9 +1249,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 +1571,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 +1588,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)), @@ -1368,6 +1642,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(); @@ -1377,7 +1655,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 cdd5d41f..b2888c1b 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,12 @@ 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 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 d38ecad5..dfa79c43 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,6 +507,25 @@ 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 decides a ref move itself, not just the client** (`ref_guard`). + Every `UpdateRef` (all transports, local-path pushes and `sc serve --store` + 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 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 + 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 @@ -513,8 +538,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