diff --git a/CLAUDE.md b/CLAUDE.md index 3673405d..52f21dd2 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -233,7 +233,11 @@ transport-adjacent. The rest, imperatively: attach`, `sign`, creating a child branch) refuses on a private branch; and integrating a private branch INTO a public one (merge/cherry-pick/rebase from it, or git export/push) is refused everywhere except `sc branch - publish`. Merging a public branch IN is allowed (keeps an embargo current). + publish`. Merging a public branch IN is allowed (keeps an embargo current); + its completion re-derives the merge outcome for paths it cannot read from + disk (out of view, or protected and skipped) instead of carrying the prior + tip, and under a sparse spec set on the public branch its conflict stop lays + out in-view files only and refuses an out-of-view conflict before any write (#240; no decided root, it would be unsealed). `sc branch revoke` rotates the KEK but a recipient who already fetched keeps the old manifest (rotation ≠ erasure). **Keep identity files outside the working tree** — a key committed under a private branch is sealed in and @@ -248,7 +252,8 @@ transport-adjacent. The rest, imperatively: environment (they arrive on fd N as NUL-terminated `NAME=VALUE` records, `SC_SECRETS_FD` names it): hygiene against env dumps and inheritance, still not isolation. `sc ws run` and `sc work --with-secrets` use the environment. -- **Sparse is checkout-only**, and a stopped merge/pick/rebase writes no +- **Sparse is checkout-only** (a spec set on a public branch still applies on a + private one, where `sparse set` is refused), and a stopped merge/pick/rebase writes no conflict marker or sealing-input plaintext outside the view: an out-of-sparse conflicted path or sealing input refuses with a `sc sparse set` hint before any write (ADR-0034, #210). While a merge/pick/rebase is stopped, `sparse diff --git a/ROADMAP.md b/ROADMAP.md index b3739943..f8539757 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -542,7 +542,8 @@ across every phase. manifest while snapshots, trees, paths, messages, and blobs introduced on the branch remain individually sealed under a branch KEK. Grant is O(1), revoke rotates the KEK, and publish atomically replays scanner-gated public history. - (ADR-0044.) + A conflicted merge of a public branch into a private one under a sparse spec honors the view and decides + out-of-view paths by the merge, not the prior tip (#240). (ADR-0044.) - **Phase 35 — Native desktop browser.** A bundled Tauri v2 application opens `.sc` repositories through the Rust crates and renders local/remote refs, the all-parent snapshot DAG, provenance, public files, and first-parent diffs. diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index 448e5dee..35b8b7b3 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -28,6 +28,8 @@ mod partial_sessions; mod partial_walks; pub mod pick_state; pub mod private; +#[cfg(test)] +mod private_sparse; pub mod promisor; pub mod protect; pub mod protect_ops; diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index b32f977b..974f3c25 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -652,6 +652,7 @@ impl Repo { let ctx = self.open_private(&branch, mid, manifest, identity)?; let sk = identity.expect("open_private succeeded"); let merge_head = crate::merge_state::read_merge_head(&self.layout)?; + let sparse = self.sparse_spec()?; let store_arc = self.vfs.store(); let mut store = store_arc.lock().unwrap(); @@ -660,6 +661,10 @@ impl Repo { // Policy + secrets: tip's, or the union with theirs on merge completion // (same discipline as `snapshot_files`' merge-completion arm). + // A merge completion also re-derives the merge's outcome for the paths + // it cannot read from disk (see below). + let mut decided: Option> = None; + let mut decided_blobs: BTreeMap> = BTreeMap::new(); let (protection, secrets) = match merge_head { None => (prev_snap.protection.clone(), prev_snap.secrets.clone()), Some(mh) => { @@ -676,9 +681,15 @@ impl Repo { let base = self .merge_base_private(&mut store, &ctx, inner_tip, mh)? .ok_or(Error::NoCommonAncestor)?; - let base_secrets = self.get_inner_snapshot(&mut store, &ctx, &base)?.secrets; + let base_snap = self.get_inner_snapshot(&mut store, &ctx, &base)?; + let fm = self.private_three_way( + &mut store, &ctx, &prev_snap, &theirs, &base_snap, identity, + )?; + let (entries, blobs, _) = decide_merged(&fm); + decided = Some(entries); + decided_blobs = blobs; let secs = crate::merge::merge_secrets( - &base_secrets, + &base_snap.secrets, &prev_snap.secrets, &theirs.secrets, )?; @@ -687,9 +698,14 @@ impl Repo { }; let prior = self.inner_tree_files(&mut store, &ctx, prev_snap.root)?; - let tracked: BTreeSet = prior.keys().cloned().collect(); + let mut tracked: BTreeSet = prior.keys().cloned().collect(); + // A path the merge decided is part of the outcome, so an ignore rule + // on the private side must not hide the stop's write of it (as + // `with_decided_paths` does on the public path, ADR-0049 q19). + if let Some(d) = &decided { + tracked.extend(d.keys().cloned()); + } let files = worktree::read_worktree(&self.layout, &tracked)?; - let sparse = self.sparse_spec()?; // Scanner gate on the plaintext partition (protected-prefix files are // sealed at publish, exempt here exactly as in a public commit). @@ -735,14 +751,22 @@ impl Repo { } } } - // Carry prior paths that are expected-absent: outside sparse, or - // PROTECTED and skipped on disk (never silently drop protected content). - for (path, e) in &prior { + // Carry paths that are expected-absent: outside sparse, or PROTECTED + // and skipped on disk (never silently drop protected content). On a + // merge completion the source is the merge's decided outcome, not the prior tip, so a path the public side + // deleted stays deleted and its clean adds and modifies land (#240, + // same rule as the public path, #238). It is re-derived rather than + // recorded: a decided tree cannot sit in unsealed `.sc/` state. + let source = decided.as_ref().unwrap_or(&prior); + for (path, e) in source { if entries.contains_key(path) || disk_paths.contains(path) { continue; } if !sparse.matches(path) || e.2 & PROTECTED != 0 { entries.insert(path.clone(), *e); + if let Some(bytes) = decided_blobs.get(&e.0) { + new_blobs.entry(e.0).or_insert_with(|| bytes.clone()); + } } } @@ -1265,18 +1289,12 @@ impl Repo { .ok_or(Error::NoCommonAncestor)?; let base_snap = self.get_inner_snapshot(&mut store, &ctx, &base)?; - // Stage the three tree closures in a RAM-only store and run the - // existing three-way. Inner objects decode to plaintext values whose - // ids are exactly the inner ids the trees reference. - let mut ram = Store::with_budget(usize::MAX / 2); - self.copy_closure_inner(&mut store, &ctx, ours_snap.root, &mut ram)?; - copy_closure_public(&mut store, theirs_snap.root, &mut ram)?; - copy_closure_public(&mut store, base_snap.root, &mut ram)?; - let fm = crate::merge::three_way_files( - &mut ram, - Some((base_snap.root, &base_snap.protection)), - (ours_snap.root, &ours_snap.protection), - (theirs_snap.root, &theirs_snap.protection), + let fm = self.private_three_way( + &mut store, + &ctx, + &ours_snap, + &theirs_snap, + &base_snap, identity, )?; let secrets = crate::merge::merge_secrets( @@ -1289,55 +1307,111 @@ impl Repo { &theirs_snap.protection.prefixes, ); - // Decide the merged file map for the inner world: - // - carried PROTECTED ciphertext keeps its id + wraps; - // - everything else (plain carries, diff3 outputs, decrypted - // protected merges) becomes a plain inner blob — publish re-seals - // protected prefixes into public form. - let mut entries: BTreeMap = BTreeMap::new(); - let mut new_blobs: BTreeMap> = BTreeMap::new(); - let mut wrapped: BTreeMap> = BTreeMap::new(); - for f in &fm.files { - if f.perms & PROTECTED != 0 && !f.needs_encrypt { - let id = Object::blob(f.bytes.clone()).id(); - if let Some(wks) = fm.wrapped_carry.get(&id) { - wrapped.insert(id, wks.clone()); - } - entries.insert(f.path.clone(), (id, f.mode, f.perms)); - } else { - let id = Object::blob(f.bytes.clone()).id(); - new_blobs.entry(id).or_insert_with(|| f.bytes.clone()); - entries.insert(f.path.clone(), (id, f.mode, 0)); - } - } + let (entries, new_blobs, wrapped) = decide_merged(&fm); if !fm.conflicts.is_empty() { - // Conflict state: everything lands on disk in plaintext (markers, - // sidecars, decrypted protected content — we hold the identity by - // construction). Never through the CAS. Completion is the next - // `commit_private`, which reads MERGE_HEAD. - let prior_paths: BTreeSet = self - .inner_tree_files(&mut store, &ctx, ours_snap.root)? - .into_keys() + // A conflict outside the sparse view has nowhere to show its + // markers: refuse before any write, like the public path (#220). + let sparse = self.sparse_spec()?; + let outside: Vec<&str> = fm + .conflicts + .iter() + .map(String::as_str) + .filter(|p| !sparse.matches(p)) .collect(); + if !outside.is_empty() { + return Err(Error::InvalidArgument(format!( + "conflict in {} is outside your sparse checkout; switch to a public branch, \ + run `sc sparse set` to include it, then retry", + outside.join(", ") + ))); + } + // Conflict state: the IN-VIEW files land on disk in plaintext + // (markers, sidecars, decrypted protected content — we hold the + // identity by construction). Never through the CAS. Out-of-view + // paths are not laid out; completion re-derives their merged + // value (`commit_private`, #240). It reads MERGE_HEAD. + let prior_entries = self.inner_tree_files(&mut store, &ctx, ours_snap.root)?; + let prior_paths: BTreeSet = prior_entries.keys().cloned().collect(); + // Carried protected ciphertext: decrypt for the resolver to see + // (completion re-seals it as needed). One this recipient cannot + // read has its old plaintext dropped, so completion carries the + // merged entry instead of recommitting the stale one. Decide all + // of it, and refuse a collision, before writing anything. + let prots = Protection { + prefixes: prefixes.clone(), + wrapped: fm.wrapped_carry.clone(), + }; + let mut plaintext: BTreeMap<&str, Option>> = BTreeMap::new(); + for f in fm.files.iter().filter(|f| sparse.matches(&f.path)) { + if f.perms & PROTECTED == 0 || f.needs_encrypt { + continue; + } + let id = Object::blob(f.bytes.clone()).id(); + if let Ok(pt) = crate::protect::decrypt_with(&f.bytes, &id, &[&prots], sk, &f.path) + { + plaintext.insert(&f.path, Some(pt.to_vec())); + continue; + } + let full = worktree::safe_join(&self.layout.root, &f.path)?; + // A symlink (even one whose target reads as the old bytes) + // is the user's file, never the checkout's copy. + let on_disk = match std::fs::symlink_metadata(&full) { + Ok(m) if m.file_type().is_file() => Some(Some(std::fs::read(&full)?)), + Ok(_) => Some(None), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => None, + Err(e) => return Err(e.into()), + }; + if let Some(disk) = on_disk { + // Only the tracked prior-tip plaintext is ours to drop. + let stale = match prior_entries.get(&f.path) { + Some((pid, _, perms)) if perms & PROTECTED != 0 => { + let ct = self.inner_blob(&mut store, &ctx, pid)?; + crate::protect::decrypt_with( + &ct, + pid, + &[&ours_snap.protection], + sk, + &f.path, + ) + .map(|pt| Some(pt.as_slice()) == disk.as_deref()) + .unwrap_or(false) + } + _ => false, + }; + if !stale { + return Err(Error::InvalidArgument(format!( + "{} exists and is not the tracked copy, but the merged version is \ + protected and you cannot read it; move it away, then retry", + f.path + ))); + } + } + plaintext.insert(&f.path, None); + } drop(store); - for f in &fm.files { + // Removals first: a failed one must fail the stop before any + // other write, or completion would commit the stale plaintext. + for (path, pt) in &plaintext { + if pt.is_none() { + let full = worktree::safe_join(&self.layout.root, path)?; + match std::fs::remove_file(&full) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } + } + } + for f in fm.files.iter().filter(|f| sparse.matches(&f.path)) { let full = worktree::safe_join(&self.layout.root, &f.path)?; + if matches!(plaintext.get(f.path.as_str()), Some(None)) { + continue; + } if let Some(parent) = full.parent() { std::fs::create_dir_all(parent)?; } - if f.perms & PROTECTED != 0 && !f.needs_encrypt { - // Carried ciphertext: write the decrypted form for the - // resolver to see; completion re-seals it as needed. - let id = Object::blob(f.bytes.clone()).id(); - let prots = Protection { - prefixes: prefixes.clone(), - wrapped: fm.wrapped_carry.clone(), - }; - match crate::protect::decrypt_with(&f.bytes, &id, &[&prots], sk, &f.path) { - Ok(pt) => worktree::write_file(&full, &pt[..], f.mode)?, - Err(_) => continue, // undecryptable: leave prior state - } + if let Some(Some(pt)) = plaintext.get(f.path.as_str()) { + worktree::write_file(&full, &pt[..], f.mode)?; } else { worktree::write_file(&full, &f.bytes, f.mode)?; } @@ -1348,6 +1422,9 @@ impl Repo { } let merged_paths: BTreeSet = fm.files.iter().map(|f| f.path.clone()).collect(); for p in prior_paths.difference(&merged_paths) { + if !sparse.matches(p) { + continue; + } let full = worktree::safe_join(&self.layout.root, p)?; let _ = std::fs::remove_file(full); } @@ -1434,6 +1511,34 @@ impl Repo { Ok((new_tip, skipped)) } + /// The three-way of a public `theirs` into the private `ours`, run against + /// a RAM-only store holding the three tree closures (decrypted inner + /// objects hash to their inner ids, so content addressing is consistent + /// and no plaintext ever touches disk). Deterministic in its inputs, so + /// `commit_private` re-derives a stopped merge's outcome instead of + /// persisting it (#240). + fn private_three_way( + &self, + store: &mut Store, + ctx: &PrivateCtx, + ours: &Snapshot, + theirs: &Snapshot, + base: &Snapshot, + identity: Option<&SecretKey>, + ) -> Result { + let mut ram = Store::with_budget(usize::MAX / 2); + self.copy_closure_inner(store, ctx, ours.root, &mut ram)?; + copy_closure_public(store, theirs.root, &mut ram)?; + copy_closure_public(store, base.root, &mut ram)?; + crate::merge::three_way_files( + &mut ram, + Some((base.root, &base.protection)), + (ours.root, &ours.protection), + (theirs.root, &theirs.protection), + identity, + ) + } + /// Copy an inner tree closure (trees + blobs, decoded plaintext values) /// into a RAM-only store. Ids are content addresses of the decoded /// values, so the copy is a faithful CAS fragment. @@ -1631,6 +1736,36 @@ impl Repo { } } +/// Decide the merged file map for the inner world: carried PROTECTED +/// ciphertext keeps its id + wraps; everything else (plain carries, diff3 +/// outputs, decrypted protected merges) becomes a plain inner blob — publish +/// re-seals protected prefixes into public form. +#[allow(clippy::type_complexity)] +fn decide_merged( + fm: &crate::merge::FileMerge, +) -> ( + BTreeMap, + BTreeMap>, + BTreeMap>, +) { + let mut entries = BTreeMap::new(); + let mut new_blobs = BTreeMap::new(); + let mut wrapped = BTreeMap::new(); + for f in &fm.files { + let id = Object::blob(f.bytes.clone()).id(); + if f.perms & PROTECTED != 0 && !f.needs_encrypt { + if let Some(wks) = fm.wrapped_carry.get(&id) { + wrapped.insert(id, wks.clone()); + } + entries.insert(f.path.clone(), (id, f.mode, f.perms)); + } else { + new_blobs.entry(id).or_insert_with(|| f.bytes.clone()); + entries.insert(f.path.clone(), (id, f.mode, 0)); + } + } + (entries, new_blobs, wrapped) +} + /// Copy a PUBLIC tree closure (trees + blobs) into the RAM store. fn copy_closure_public(store: &mut Store, root: ObjectId, ram: &mut Store) -> Result<()> { let mut stack = vec![root]; @@ -2399,4 +2534,273 @@ mod tests { drop(repo); std::fs::remove_dir_all(&root).unwrap(); } + + /// A stopped merge-in must complete a protected path the completer cannot + /// read (skipped on disk) from the merge's outcome, not the prior tip, on + /// a full checkout too (#240 review): `main` rotated the file and the + /// private side never touched it. + #[test] + fn merge_completion_takes_an_unreadable_protected_path_from_the_merge() { + unreadable_protected_path_case("protmerge", false); + } + + /// Same, with the protected prefix outside a sparse view. + #[test] + fn sparse_merge_completion_takes_an_unreadable_protected_path_from_the_merge() { + unreadable_protected_path_case("protmerge-sparse", true); + } + + fn unreadable_protected_path_case(tag: &str, sparse: bool) { + let (root, repo, (alice_sk, _), (bob_sk, bob_pk)) = setup(tag); + repo.protect("vault/", std::slice::from_ref(&bob_pk), None) + .unwrap(); + write(&root, "vault/key.pem", "PEM-V1\n"); + repo.commit("tester", "add protected").unwrap(); + repo.branch_private("fix", &alice_sk, std::slice::from_ref(&bob_pk), &[]) + .unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "public\n"); + write(&root, "vault/key.pem", "PEM-V2\n"); + repo.commit("tester", "main moves").unwrap(); + if sparse { + repo.set_sparse(&["src/".to_string()], Some(&alice_sk)) + .unwrap(); + } + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert!(matches!( + repo.merge_into_private("main", "alice", Some(&alice_sk)), + Err(Error::MergeConflicts(1)) + )); + write(&root, "src/lib.rs", "resolved\n"); + repo.commit_private("alice", "resolve", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("fix", Some(&bob_sk)).unwrap(); + if sparse { + // The view still applies to bob; widen it to see the vault. + repo.switch_with_identity("main", Some(&bob_sk)).unwrap(); + repo.set_sparse(&["src/".to_string(), "vault/".to_string()], Some(&bob_sk)) + .unwrap(); + repo.switch_with_identity("fix", Some(&bob_sk)).unwrap(); + } + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "resolved\n" + ); + assert_eq!( + std::fs::read_to_string(root.join("vault/key.pem")).unwrap(), + "PEM-V2\n", + "the public rotation was reverted by the stale carry" + ); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists(), "test dir left behind"); + } + + /// The public side revokes the completer from a protected prefix and + /// rotates the file: the stop cannot decrypt the new ciphertext, so the + /// old plaintext must not survive on disk to be recommitted (#240 review). + #[test] + fn merge_completion_does_not_recommit_a_revoked_protected_file() { + let (root, repo, (alice_sk, alice_pk), (bob_sk, bob_pk)) = setup("protrevoke"); + repo.protect("vault/", &[alice_pk.clone(), bob_pk.clone()], None) + .unwrap(); + write(&root, "vault/key.pem", "PEM-V1\n"); + repo.commit("tester", "add protected").unwrap(); + repo.branch_private("fix", &alice_sk, std::slice::from_ref(&bob_pk), &[]) + .unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.revoke("vault/", &alice_pk.recipient_id()).unwrap(); + write(&root, "vault/key.pem", "PEM-V2\n"); + write(&root, "src/lib.rs", "public\n"); + repo.commit("tester", "rotate").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert!(matches!( + repo.merge_into_private("main", "alice", Some(&alice_sk)), + Err(Error::MergeConflicts(1)) + )); + write(&root, "src/lib.rs", "resolved\n"); + repo.commit_private("alice", "resolve", Some(&alice_sk)) + .unwrap(); + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "resolved\n" + ); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("fix", Some(&bob_sk)).unwrap(); + assert_eq!( + std::fs::read_to_string(root.join("vault/key.pem")).unwrap(), + "PEM-V2\n", + "the revoked key came back from the prior tip" + ); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + + /// A path the public side adds in view must land even when the private + /// side's `.scignore` ignores it: completion reads the re-derived paths + /// regardless of ignore rules (#240 review, ADR-0049 q19). + #[test] + fn merge_completion_reads_a_decided_path_an_ignore_rule_hides() { + let (root, repo, (alice_sk, _), _bob) = setup("ignoredadd"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + write(&root, ".scignore", "*.log\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "public\n"); + write(&root, "src/build.log", "from-main\n"); + repo.commit("tester", "main moves").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert!(matches!( + repo.merge_into_private("main", "alice", Some(&alice_sk)), + Err(Error::MergeConflicts(1)) + )); + write(&root, "src/lib.rs", "resolved\n"); + repo.commit_private("alice", "resolve", Some(&alice_sk)) + .unwrap(); + assert_eq!( + std::fs::read_to_string(root.join("src/build.log")).unwrap(), + "from-main\n" + ); + let (_, mid, manifest) = repo.head_private().unwrap().unwrap(); + let ctx = repo + .open_private("fix", mid, manifest, Some(&alice_sk)) + .unwrap(); + let store_arc = repo.vfs().store(); + let mut store = store_arc.lock().unwrap(); + let tip = ctx.inner_tip().unwrap(); + let snap = repo.get_inner_snapshot(&mut store, &ctx, &tip).unwrap(); + let files = repo.inner_tree_files(&mut store, &ctx, snap.root).unwrap(); + assert!(files.contains_key("src/build.log"), "public add dropped"); + drop(store); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + + /// Scenario shared by the two stop-time protected tests: `vault/` is + /// protected to alice and bob, the private `fix` has both as recipients, + /// and `main` revokes alice, adds `vault/new.pem` (sealed to bob only) and + /// edits `src/lib.rs` so the merge stops on that one conflict. + fn revoked_addition_setup(tag: &str) -> (std::path::PathBuf, Repo, SecretKey) { + let (root, repo, (alice_sk, alice_pk), (_bob_sk, bob_pk)) = setup(tag); + repo.protect("vault/", &[alice_pk.clone(), bob_pk.clone()], None) + .unwrap(); + write(&root, "vault/key.pem", "PEM-V1\n"); + repo.commit("tester", "add protected").unwrap(); + repo.branch_private("fix", &alice_sk, std::slice::from_ref(&bob_pk), &[]) + .unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/lib.rs", "private\n"); + repo.commit_private("alice", "private work", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + repo.revoke("vault/", &alice_pk.recipient_id()).unwrap(); + write(&root, "vault/new.pem", "PEM-NEW\n"); + write(&root, "src/lib.rs", "public\n"); + repo.commit("tester", "rotate").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + (root, repo, alice_sk) + } + + /// An untracked file at a path whose merged protected version the + /// recipient cannot read is the user's, not stale plaintext: the stop + /// refuses before any write and leaves it intact. + #[test] + fn private_stop_refuses_an_untracked_file_at_an_unreadable_protected_path() { + let (root, repo, alice_sk) = revoked_addition_setup("protuntracked"); + write(&root, "vault/new.pem", "MINE\n"); + let (_, before, _) = repo.head_private().unwrap().unwrap(); + let err = repo + .merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap_err(); + assert!(err.to_string().contains("vault/new.pem"), "{err}"); + assert_eq!( + std::fs::read_to_string(root.join("vault/new.pem")).unwrap(), + "MINE\n" + ); + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "private\n", + "refusal wrote nothing" + ); + assert!(!crate::merge_state::in_progress(repo.layout())); + assert_eq!(repo.head_private().unwrap().unwrap().1, before); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + + /// If the stale plaintext cannot be removed the stop fails before any + /// write or state change; it must not stop and let completion commit it. + #[cfg(unix)] + #[test] + fn private_stop_fails_when_stale_protected_plaintext_cannot_be_removed() { + use std::os::unix::fs::PermissionsExt; + let (root, repo, alice_sk) = revoked_addition_setup("protrmfail"); + // The tracked copy of vault/key.pem is stale once main rotates it. + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "vault/key.pem", "PEM-V2\n"); + repo.commit("tester", "rotate key").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + let (_, before, _) = repo.head_private().unwrap().unwrap(); + let dir = root.join("vault"); + std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o555)).unwrap(); + let can_write = std::fs::write(dir.join("probe"), b"x").is_ok(); + let res = repo.merge_into_private("main", "alice", Some(&alice_sk)); + std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o755)).unwrap(); + if !can_write { + assert!(matches!(res, Err(Error::Io(_))), "{res:?}"); + assert!(!crate::merge_state::in_progress(repo.layout())); + assert_eq!(repo.head_private().unwrap().unwrap().1, before); + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "private\n", + "nothing else was written" + ); + } + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } + + /// A symlink at an unreadable protected path is the user's file even when + /// its target reads as the old plaintext: the stop refuses, leaves it. + #[cfg(unix)] + #[test] + fn private_stop_refuses_a_symlink_at_an_unreadable_protected_path() { + let (root, repo, alice_sk) = revoked_addition_setup("protsymlink"); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "vault/key.pem", "PEM-V2\n"); + repo.commit("tester", "rotate key").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + // Tracked vault/key.pem replaced by a link whose target reads "PEM-V1". + write(&root, "elsewhere.txt", "PEM-V1\n"); + std::fs::remove_file(root.join("vault/key.pem")).unwrap(); + std::os::unix::fs::symlink("../elsewhere.txt", root.join("vault/key.pem")).unwrap(); + let err = repo + .merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap_err(); + assert!(err.to_string().contains("vault/key.pem"), "{err}"); + assert!(std::fs::symlink_metadata(root.join("vault/key.pem")) + .unwrap() + .file_type() + .is_symlink()); + assert!(!crate::merge_state::in_progress(repo.layout())); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + assert!(!root.exists()); + } } diff --git a/crates/repo/src/private_sparse.rs b/crates/repo/src/private_sparse.rs new file mode 100644 index 00000000..94812ae6 --- /dev/null +++ b/crates/repo/src/private_sparse.rs @@ -0,0 +1,162 @@ +//! A conflicted merge of a public branch into a private branch under a sparse +//! spec must decide out-of-view paths by the merge, not by the prior private +//! tip (#240): a path the public side deleted stays deleted, its clean adds and +//! modifies land, ours-only private edits survive, and the stop writes nothing +//! outside the view. + +use std::collections::BTreeMap; +use std::path::Path; + +use scl_crypto::SecretKey; + +use crate::error::Error; +use crate::repo::Repo; + +fn put(root: &Path, rel: &str, content: &str) { + let p = root.join(rel); + std::fs::create_dir_all(p.parent().unwrap()).unwrap(); + std::fs::write(p, content).unwrap(); +} + +fn docs(root: &Path) -> BTreeMap { + let mut out = BTreeMap::new(); + if let Ok(rd) = std::fs::read_dir(root.join("docs")) { + for e in rd { + let e = e.unwrap(); + out.insert( + e.file_name().to_string_lossy().into_owned(), + std::fs::read_to_string(e.path()).unwrap(), + ); + } + } + out +} + +/// `main` holds src/lib.rs plus docs/{old,mod,ours,lines}.txt; private `fix` edits +/// src/lib.rs, docs/ours.txt and the first line of docs/lines.txt; `main` then edits src/lib.rs, deletes +/// docs/old.txt, edits docs/mod.txt and adds docs/new.txt and edits the last line of docs/lines.txt. With `view` set, +/// the sparse spec `src/` is in force on `main` before switching to `fix`, +/// and `conflict_in_docs` also makes `fix` edit docs/mod.txt (a conflict). +struct Fixture { + root: std::path::PathBuf, + repo: Repo, + sk: SecretKey, +} + +fn fixture(tag: &str, view: bool, conflict_in_docs: bool) -> Fixture { + let root = std::env::temp_dir().join(format!("scl-priv-sparse-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let repo = Repo::init(&root).unwrap(); + put(&root, "src/lib.rs", "base\n"); + put(&root, "docs/old.txt", "old\n"); + put(&root, "docs/mod.txt", "mod-base\n"); + put(&root, "docs/ours.txt", "ours-base\n"); + put(&root, "docs/lines.txt", "a\nb\nc\nd\ne\n"); + repo.commit("t", "base").unwrap(); + let (sk, _pk) = scl_crypto::generate_keypair(); + repo.branch_private("fix", &sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&sk)).unwrap(); + put(&root, "src/lib.rs", "private\n"); + put(&root, "docs/ours.txt", "ours-edit\n"); + put(&root, "docs/lines.txt", "A\nb\nc\nd\ne\n"); + if conflict_in_docs { + put(&root, "docs/mod.txt", "mod-private\n"); + } + repo.commit_private("alice", "private work", Some(&sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&sk)).unwrap(); + put(&root, "src/lib.rs", "public\n"); + std::fs::remove_file(root.join("docs/old.txt")).unwrap(); + put(&root, "docs/mod.txt", "mod-public\n"); + put(&root, "docs/new.txt", "new-public\n"); + put(&root, "docs/lines.txt", "a\nb\nc\nd\nE\n"); + repo.commit("t", "main moves").unwrap(); + if view { + repo.set_sparse(&["src/".to_string()], Some(&sk)).unwrap(); + } + repo.switch_with_identity("fix", Some(&sk)).unwrap(); + Fixture { root, repo, sk } +} + +/// Stop the merge, resolve `src/lib.rs`, complete, widen to the whole tree +/// and return the resulting `docs/` listing. +fn run(f: &Fixture, view: bool) -> BTreeMap { + let sk = Some(&f.sk); + assert!(matches!( + f.repo.merge_into_private("main", "alice", sk), + Err(Error::MergeConflicts(1)) + )); + if view { + assert!(docs(&f.root).is_empty(), "the stop wrote outside the view"); + } + put(&f.root, "src/lib.rs", "resolved\n"); + f.repo.commit_private("alice", "resolve", sk).unwrap(); + assert_eq!( + std::fs::read_to_string(f.root.join("src/lib.rs")).unwrap(), + "resolved\n" + ); + if view { + assert!(docs(&f.root).is_empty(), "completion widened the view"); + f.repo.switch_with_identity("main", sk).unwrap(); + f.repo + .set_sparse(&["src/".to_string(), "docs/".to_string()], sk) + .unwrap(); + f.repo.switch_with_identity("fix", sk).unwrap(); + } + docs(&f.root) +} + +#[test] +fn sparse_merge_into_private_equals_the_run_without_sparse() { + let full = fixture("full", false, false); + let want = run(&full, false); + let sparse = fixture("sparse", true, false); + let got = run(&sparse, true); + assert!(!got.contains_key("old.txt"), "public deletion resurrected"); + assert_eq!(got["mod.txt"], "mod-public\n"); + assert_eq!(got["new.txt"], "new-public\n"); + assert_eq!(got["ours.txt"], "ours-edit\n", "ours-only edit lost"); + // Both sides edited it, out of view: the merged blob is new and sealed. + assert_eq!(got["lines.txt"], "A\nb\nc\nd\nE\n"); + assert_eq!(got, want); + std::fs::remove_dir_all(&full.root).unwrap(); + assert!(!full.root.exists(), "test dir left behind"); + std::fs::remove_dir_all(&sparse.root).unwrap(); + assert!(!sparse.root.exists(), "test dir left behind"); +} + +#[test] +fn an_out_of_view_conflict_is_refused_before_any_write() { + let f = fixture("refuse", true, true); + let (_, before, _) = f.repo.head_private().unwrap().unwrap(); + let err = f + .repo + .merge_into_private("main", "alice", Some(&f.sk)) + .unwrap_err(); + assert!( + err.to_string().contains("sc sparse set"), + "hint widens the spec: {err}" + ); + assert!(!crate::merge_state::in_progress(f.repo.layout())); + assert!(docs(&f.root).is_empty()); + assert_eq!(f.repo.head_private().unwrap().unwrap().1, before); + std::fs::remove_dir_all(&f.root).unwrap(); + assert!(!f.root.exists(), "test dir left behind"); +} + +#[test] +fn replay_onto_a_private_branch_stays_refused_under_sparse() { + let f = fixture("replay", true, false); + assert!(matches!( + f.repo.cherry_pick("main", "alice", Some(&f.sk), None), + Err(Error::PrivateUnsupported(_)) + )); + assert!(matches!( + f.repo.rebase("main", "alice", Some(&f.sk)), + Err(Error::PrivateUnsupported(_)) + )); + assert!(docs(&f.root).is_empty()); + std::fs::remove_dir_all(&f.root).unwrap(); + assert!(!f.root.exists(), "test dir left behind"); +} diff --git a/docs/adr/0034-sparse-checkouts.md b/docs/adr/0034-sparse-checkouts.md index ee23cc85..50c1633b 100644 --- a/docs/adr/0034-sparse-checkouts.md +++ b/docs/adr/0034-sparse-checkouts.md @@ -231,9 +231,12 @@ every claim below is checked against the shipped code, not the plan. ref); the repo lock is held by the CLI as for any mutation. 6. **Completion carries from the decided root, not the parents (#238).** A stopped merge, cherry-pick or rebase on a public branch records a - decided root (`MERGE`/`PICK`/`REBASE_DECIDED_ROOT`); a merge into a - private branch writes none and completes through `commit_private`, which - this refinement does not cover. `snapshot_files` carries an absent + decided root (`MERGE`/`PICK`/`REBASE_DECIDED_ROOT`). A merge into a + private branch records none, because an inner tree must not sit in + unsealed `.sc/` state; `commit_private` re-derives the merge outcome + instead and carries from that (#240, ADR-0044 "Merge-in + under a sparse spec"), and its stop lays out only in-view files and + refuses an out-of-view conflict before any write. `snapshot_files` carries an absent out-of-view or still-protected path from the decided root. Earlier builds also read the tip and merge head, so a path the other side had cleanly deleted (an out-of-view file, or a protected file) came back from the tip diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md index 37bbf542..9f60c9a9 100644 --- a/docs/adr/0044-per-branch-access-control.md +++ b/docs/adr/0044-per-branch-access-control.md @@ -215,8 +215,11 @@ sealed trees — that inability is the feature. So: ref created). - Everywhere else, a private branch travels as its whole manifest closure or not at all. -- `sc sparse` on a private branch works normally for a recipient — sparse - is checkout-only (ADR-0034) and applies after decryption. +- `sc sparse set/disable` is refused while a private branch is checked out + (`refuse_on_private`), but a spec set on a public branch stays in force + when you switch onto one: sparse is checkout-only (ADR-0034) and applies + after decryption. Merging a public branch in and committing honor it (see + "Merge-in under a sparse spec" below). No P27 promisor/gap machinery grows manifest-aware cases. @@ -350,6 +353,57 @@ The decision shipped as designed; the concrete shape settled as follows. `recipient_opens_a_merged_branch_cloned_without_the_public_source` (a peer that never received the public branch still opens the merged private one). +## Merge-in under a sparse spec (#240) + +A conflicted `merge_into_private` completes through `commit_private`, which +carries the paths it cannot read from disk (outside the sparse view, or +protected and skipped). It used to carry them from the prior private tip, so +a path the public side had cleanly deleted outside the view came back, and +its clean adds and modifies there were lost. The public path fixed the same +class in #238 by completing from a recorded decided root (ADR-0034 +refinement 6). **A private merge records none**, on purpose: the root would +be only an id, but it names an inner tree, and that tree's objects (plaintext +paths and blob ids about the private tree) would have to be stored unsealed +next to the unsealed `.sc/MERGE_*`. Sealing them under the branch KEK would +need a second index outside the manifest, for a state that lives only until +the next commit. Instead: + +- The stop is a pure function of the ours inner tip, `MERGE_HEAD` and the + merge base, none of which can move while the merge is stopped (every + ref-moving op is guarded). `commit_private` therefore **re-derives** the + outcome (`private_three_way`, the same call the stop made) on every merge + completion, and carries out-of-view and protected-skipped paths from it, + never from the prior tip. A path absent from the outcome was decided + deleted. Nothing new is stored, so the opacity argument above is unchanged. + It is not limited to sparse: on a full checkout a protected path the + completer cannot read (skipped on disk) was carried from the prior tip too, + silently reverting a clean public change to it. The re-derivation runs with + the completing identity. **Behavior change:** a recipient who cannot + decrypt a protected path both sides changed can no longer finish another + recipient's stopped merge; it errors before any scan, seal or ref move, and + the merge stays stopped for someone who can. Where the merged entry is + unreadable to the completer (for example the public side revoked them and + rotated the file), the stop removes the stale plaintext it would otherwise + leave behind, so completion carries the merged entry, not the old one. It + removes only a file that is byte-identical to the tracked prior-tip + plaintext; any other file at that path is the user's, and the stop refuses + naming the path before any write. Removals run first and a failed one fails + the stop before any other write or state change. + Paths the merge decided are also read from disk regardless of the private + side's `.scignore` (as `with_decided_paths` does publicly, ADR-0049 q19). + The completion's scanner gate covers only files read from disk; re-derived + content comes from already-scanned commits, and `branch publish` rescans. +- The stop honors the view like the public path (#220): it lays out only + in-view files, and a conflict outside the view is refused before any write + (no merge state, no ref move) with a hint to switch to a public branch and + `sc sparse set` to include it. Earlier builds wrote every merged file, + including out-of-view ones, so the checkout was wider than advertised. + +Pinned by `private_sparse.rs`: the final tree equals the run without sparse +(deletion kept, add and modify landed, ours-only private edit kept), the stop +writes nothing outside the view, and an out-of-view conflict refuses cleanly; +and by `private.rs` tests for the full-checkout protected-path case. + ## Deferred (to ROADMAP.md) - `sc branch publish --squash` (hide intermediate history at publish).