From 26b53b8cd7a6e05baeddc2e0b942ed98ad207b25 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:19:40 +0200 Subject: [PATCH 1/7] fix(repo): refuse tree paths that reach the .sc control directory Tree decode accepts any entry name and safe_join rejected only empty, `.` and `..` components. A remote whose tree held `.sc/recipients.toml` got that file written into the cloner's `.sc/` (native and Git-bridge clones), and on Windows a name like `..\..\x` or `C:\x` left the checkout. Add scl_core::check_tree_path, a pure predicate that refuses empty, `.`, `..` and NUL components, `.sc` at any depth under its HFS/NTFS aliases (logic copied from the forge's `.git` guard), and on Windows any `\` or `:`. The tree flatteners, preflight_view, the private-branch flattener and safe_join apply it, so clone, switch, merge, undo and friends refuse before the first write. Worktree::checkout checks every path up front. Both tree builders refuse to commit such a path (without the Windows rule, since `:` and `\` are legal unix names). A clone that fails after init now removes the directory it created (only its `.sc/` if the directory already existed), for native and `--git` clones. --- CLAUDE.md | 5 + crates/cli/src/main.rs | 95 +++++++++++-- crates/core/src/error.rs | 9 ++ crates/core/src/lib.rs | 2 + crates/core/src/pathname.rs | 162 ++++++++++++++++++++++ crates/repo/src/lib.rs | 2 + crates/repo/src/path_guard.rs | 226 ++++++++++++++++++++++++++++++ crates/repo/src/private.rs | 2 + crates/repo/src/sync.rs | 249 +++++++++++++++++++--------------- crates/repo/src/worktree.rs | 53 ++++++-- crates/vfs/src/lib.rs | 97 ++++++++++++- docs/THREAT-MODEL.md | 10 ++ 12 files changed, 770 insertions(+), 142 deletions(-) create mode 100644 crates/core/src/pathname.rs create mode 100644 crates/repo/src/path_guard.rs diff --git a/CLAUDE.md b/CLAUDE.md index 358a76e9..b7905b41 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -375,6 +375,11 @@ transport-adjacent. The rest, imperatively: makes the commit, merge completion, `sc ws harvest` or `sc ws refresh` fail until the directory is removed (ADR-0034 refinement 9). Across commits a path may still change kind (#247). +- **Tree paths go through `scl_core::check_tree_path` before any write.** Tree + decode accepts any entry name, so the flatteners in `worktree.rs`, `safe_join` + and `Worktree::checkout` refuse `.sc` (any depth, any alias), traversal, NUL + and, on Windows, `\` and `:`; both tree builders refuse to commit them. A new + write-out path must flatten through those walkers or call it itself. - **Every ref-moving or commit-creating op (including all secret/protect policy ops) is guarded against an in-progress merge/pick/rebase** — finish or abort the in-progress op first. A push into a repo with a stopped op is diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index d6b0f3d6..a715a7e8 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3996,21 +3996,34 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { ); } + let dst_existed = dst.exists(); std::fs::create_dir_all(dst)?; let repo = scl_repo::Repo::init(dst)?; - repo.remote_add_git("origin", url)?; - - // Sync the mirror, then point the unborn HEAD at the remote's default - // branch name BEFORE fetching, so the tracking ref and local branch agree. - let mirror = git_remote_effective_path(&repo, "origin", url, true)?; - let default = scl_gitio::bridge::remote_default_branch(&mirror)?; - scl_repo::refs::write_head(repo.layout(), &default)?; - - run_fetch_git(&repo, "origin")?; - // Adopt: merge the tracking ref into the unborn branch (ADR-0018's - // unborn fast-forward). Author resolution mirrors run_merge's. - let author = resolve_author(None); - repo.merge(&format!("origin/{default}"), &author)?; + // A failed clone takes itself back out, as `git clone` does; the closure + // owns `repo`, so its lock is released before the directory goes. + let adopted = (move || -> Result { + repo.remote_add_git("origin", url)?; + + // Sync the mirror, then point the unborn HEAD at the remote's default + // branch name BEFORE fetching, so the tracking ref and local branch agree. + let mirror = git_remote_effective_path(&repo, "origin", url, true)?; + let default = scl_gitio::bridge::remote_default_branch(&mirror)?; + scl_repo::refs::write_head(repo.layout(), &default)?; + + run_fetch_git(&repo, "origin")?; + // Adopt: merge the tracking ref into the unborn branch (ADR-0018's + // unborn fast-forward). Author resolution mirrors run_merge's. + let author = resolve_author(None); + repo.merge(&format!("origin/{default}"), &author)?; + Ok(default) + })(); + let default = match adopted { + Ok(default) => default, + Err(e) => { + scl_repo::sync::discard_failed_clone(dst, dst_existed); + return Err(e); + } + }; println!("cloned {url} into {} (branch {default})", dst.display()); Ok(()) } @@ -5381,6 +5394,62 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); } + /// A Git repo that tracks `.sc/recipients.toml` must not get that file into + /// the cloner's `.sc/`: the clone fails and takes its directory back out. + #[test] + fn clone_of_a_git_repo_tracking_a_control_file_is_refused() { + let _g = GIT_ENV_LOCK.lock().unwrap_or_else(|p| p.into_inner()); + let root = std::env::temp_dir().join(format!("scl-cli-gitplant-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + let hub = root.join("hub.git"); + std::process::Command::new("git") + .args(["init", "--bare", "-b", "main", hub.to_str().unwrap()]) + .status() + .unwrap(); + let seed = root.join("seed"); + std::process::Command::new("git") + .args(["init", "-b", "main", seed.to_str().unwrap()]) + .status() + .unwrap(); + std::fs::create_dir_all(seed.join(".sc")).unwrap(); + std::fs::write(seed.join(".sc/recipients.toml"), "planted").unwrap(); + std::fs::write(seed.join("a.txt"), "x").unwrap(); + for args in [ + vec!["add", "."], + vec![ + "-c", + "user.name=t", + "-c", + "user.email=t@t", + "commit", + "-m", + "seed", + ], + ] { + std::process::Command::new("git") + .current_dir(&seed) + .args(&args) + .status() + .unwrap(); + } + std::process::Command::new("git") + .current_dir(&seed) + .args(["push", &format!("file://{}", hub.display()), "main"]) + .status() + .unwrap(); + + let dst = root.join("victim"); + let err = run_clone_git(&format!("file://{}", hub.display()), &dst).unwrap_err(); + assert!( + err.to_string().contains("unsafe path"), + "unexpected error: {err:#}" + ); + assert!(!dst.exists(), "the failed clone removes its directory"); + + std::fs::remove_dir_all(&root).unwrap(); + } + #[test] fn clone_git_flag_with_local_path_errors_clearly() { // Env mutation (git init) — serialize with the shared git-env lock. diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index b3f7f061..9e9080d9 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -31,6 +31,15 @@ pub enum Error { #[error("bad pack index: {0}")] BadPackIndex(String), + /// A tree path that must never be written under a checkout (traversal, + /// NUL, the `.sc` control directory or an alias of it, and on Windows a + /// separator or drive marker); see [`crate::check_tree_path`]. + #[error("unsafe path in tree: {path:?}: {reason}")] + UnsafePath { + path: String, + reason: crate::pathname::PathRefusal, + }, + #[error("backend io error: {0}")] Io(#[from] std::io::Error), } diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 9267e57e..da34580e 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -11,6 +11,7 @@ pub mod id; pub mod object; pub mod owner_only; pub mod pack; +pub mod pathname; pub mod store; #[cfg(windows)] mod winacl; @@ -180,6 +181,7 @@ pub use object::{ RecipientState, SealedObj, Secret, SignatureObj, Snapshot, Transcript, Tree, TreeEntry, WrappedKey, PROTECTED, RANDOMIZED, }; +pub use pathname::{check_tree_path, PathRefusal}; pub use store::{Backend, SpillPolicy, Store, StoreConfig, StoreStats}; #[cfg(test)] diff --git a/crates/core/src/pathname.rs b/crates/core/src/pathname.rs new file mode 100644 index 00000000..a9314b6e --- /dev/null +++ b/crates/core/src/pathname.rs @@ -0,0 +1,162 @@ +//! The one rule for what a tree entry's path may be, before any code joins it +//! onto a checkout directory. +//! +//! A tree comes from a peer (native remote, Git bridge, pack), and tree decode +//! accepts any entry name. Left unchecked, a tree entry such as +//! `.sc/recipients.toml` would be written into the cloner's `.sc/` control +//! directory, and on Windows `..\..\x` or `C:\x` would leave the checkout. The +//! predicate is pure and platform-independent so each platform's rule can be +//! tested everywhere; callers pass `cfg!(windows)`. + +use std::fmt; + +/// Why [`check_tree_path`] refused a path. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum PathRefusal { + /// An empty, `.` or `..` component. + Traversal, + /// A NUL byte: some APIs truncate the name there. + Nul, + /// The control directory `.sc` (or an alias a checkout filesystem + /// resolves to it) at any depth. + ControlDir, + /// A `\` or `:` in a component, which Windows treats as a separator or a + /// drive/stream marker. + WindowsSeparator, +} + +impl fmt::Display for PathRefusal { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(match self { + Self::Traversal => "an empty, `.` or `..` component", + Self::Nul => "a NUL byte", + Self::ControlDir => "a component that names or aliases the `.sc` control directory", + Self::WindowsSeparator => { + "a `\\` or `:` in a component, which Windows would not treat as a name" + } + }) + } +} + +/// Code points HFS+ ignores when it compares names, so `.s\u{200c}c` is `.sc` +/// on a Mac. Copied from `crates/forge/src/gitwrite.rs` (`hfs_ignorable`), which +/// applies the same alias rules to `.git`; `core` must not depend on the forge. +fn hfs_ignorable(c: char) -> bool { + matches!(c, '\u{200c}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{206a}'..='\u{206f}' | '\u{feff}') +} + +/// Whether `component` is `.sc` on any filesystem a checkout may live on: +/// mirrors `is_alias` in `crates/forge/src/gitwrite.rs` (HFS-ignorable code +/// points dropped, an NTFS `:stream` suffix cut, trailing dots and spaces +/// trimmed, ASCII case folded), against `.sc` and the 8.3 short name `sc~1`. +fn is_control_dir(component: &str) -> bool { + let c: String = component.chars().filter(|c| !hfs_ignorable(*c)).collect(); + let c = c.split(':').next().unwrap_or(""); + let c = c.trim_end_matches([' ', '.']).to_ascii_lowercase(); + c == ".sc" || c == "sc~1" +} + +/// Refuse a `/`-separated tree path that must never be written under a +/// checkout: an empty, `.` or `..` component, a NUL, the control directory +/// `.sc` (any alias, any depth), and with `windows` set any `\` or `:`. Callers +/// pass `cfg!(windows)`; the unix rule leaves `:` and `\` alone since they are +/// legal names there. +pub fn check_tree_path(rel: &str, windows: bool) -> Result<(), PathRefusal> { + if rel.contains('\0') { + return Err(PathRefusal::Nul); + } + for comp in rel.split('/') { + if comp.is_empty() || comp == "." || comp == ".." { + return Err(PathRefusal::Traversal); + } + if is_control_dir(comp) { + return Err(PathRefusal::ControlDir); + } + if windows && comp.contains(['\\', ':']) { + return Err(PathRefusal::WindowsSeparator); + } + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn refuses_control_dir_under_every_alias_at_any_depth() { + for p in [ + ".sc", + ".SC", + ".sc.", + ".sc ", + ".sc. .", + ".s\u{200c}c", + "\u{feff}.sc", + "sc~1", + "SC~1", + ".sc:x", + ".sc/recipients.toml", + "a/.Sc/b", + "a/b/.sc", + ] { + for windows in [false, true] { + assert_eq!( + check_tree_path(p, windows), + Err(PathRefusal::ControlDir), + "{p:?} windows={windows}" + ); + } + } + } + + #[test] + fn refuses_traversal_and_nul() { + for p in ["", ".", "..", "a//b", "a/./b", "a/../b", "/a", "a/"] { + assert_eq!( + check_tree_path(p, false), + Err(PathRefusal::Traversal), + "{p:?}" + ); + } + assert_eq!(check_tree_path("a\0b", false), Err(PathRefusal::Nul)); + assert_eq!(check_tree_path("a\0/.sc", true), Err(PathRefusal::Nul)); + } + + #[test] + fn windows_rule_refuses_separators_and_drives() { + for p in ["a\\b", "C:", "C:\\x", "..\\..\\x", "a/b:c"] { + assert!(check_tree_path(p, true).is_err(), "{p:?}"); + } + } + + #[test] + fn unix_rule_allows_colon_and_backslash() { + for p in ["a:b", "a\\b", "dir/a:b", "..\\x"] { + assert_eq!(check_tree_path(p, false), Ok(()), "{p:?}"); + } + } + + #[test] + fn ordinary_names_pass() { + for p in [ + ".scignore", + ".scx", + "sc", + "a/.sc_x/b", + "src/main.rs", + ".sc-keep", + "sc~2", + "..a", + "a..", + ] { + for windows in [false, true] { + assert_eq!( + check_tree_path(p, windows), + Ok(()), + "{p:?} windows={windows}" + ); + } + } + } +} diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index 4dd9c4f0..793ed320 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -32,6 +32,8 @@ mod partial_sessions; mod partial_size; #[cfg(test)] mod partial_walks; +#[cfg(test)] +mod path_guard; pub mod pick_state; pub mod private; #[cfg(test)] diff --git a/crates/repo/src/path_guard.rs b/crates/repo/src/path_guard.rs new file mode 100644 index 00000000..bd75012c --- /dev/null +++ b/crates/repo/src/path_guard.rs @@ -0,0 +1,226 @@ +//! A tree from a peer may name any path, but a checkout must never write into +//! the `.sc/` control directory (or out of the working tree). These tests plant +//! such a tree through the store API (a commit now refuses it) and check that +//! clone, switch and fast-forward refuse before the first write, and that a +//! failed clone takes itself back out. + +use std::path::{Path, PathBuf}; + +use scl_core::{EntryKind, FileMode, Object, ObjectId, Protection, Snapshot, Tree, TreeEntry}; + +use crate::error::Error; +use crate::repo::Repo; + +fn tmp_root(tag: &str) -> PathBuf { + let p = std::env::temp_dir().join(format!("scl-pg-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&p); + std::fs::create_dir_all(&p).unwrap(); + p +} + +fn entry(name: &str, kind: EntryKind, id: ObjectId) -> TreeEntry { + TreeEntry { + name: name.into(), + kind, + id, + mode: FileMode::FILE, + perms: 0, + } +} + +/// Store a snapshot whose tree holds `ok.txt` and the planted +/// `.sc/recipients.toml`, either as a real `.sc` directory (`nested`) or as a +/// single entry whose name contains the slash. Returns the snapshot id. +fn plant(repo: &Repo, parent: Option, nested: bool) -> ObjectId { + let store = repo.vfs_handle().store(); + let mut s = store.lock().unwrap(); + let blob = s.put(Object::blob(b"planted".to_vec())).unwrap(); + let planted = if nested { + let sub = s + .put(Object::Tree(Tree::new(vec![entry( + "recipients.toml", + EntryKind::Blob, + blob, + )]))) + .unwrap(); + entry(".sc", EntryKind::Tree, sub) + } else { + entry(".sc/recipients.toml", EntryKind::Blob, blob) + }; + let root = s + .put(Object::Tree(Tree::new(vec![ + planted, + entry("ok.txt", EntryKind::Blob, blob), + ]))) + .unwrap(); + s.put(Object::Snapshot(Snapshot { + root, + parents: parent.into_iter().collect(), + author: "t".into(), + timestamp: 0, + message: "planted".into(), + secrets: Default::default(), + protection: Protection::default(), + })) + .unwrap() +} + +fn assert_unsafe(err: &Error) { + assert!( + matches!(err, Error::Core(scl_core::Error::UnsafePath { .. })), + "wanted UnsafePath, got {err:?}" + ); +} + +/// No file under `root` (the `.sc` dir included) holds the planted bytes. +fn assert_nothing_planted(root: &Path) { + fn walk(dir: &Path, hits: &mut Vec) { + let Ok(rd) = std::fs::read_dir(dir) else { + return; + }; + for e in rd.flatten() { + let p = e.path(); + if p.is_dir() { + walk(&p, hits); + } else if std::fs::read(&p).is_ok_and(|b| b == b"planted") { + hits.push(p); + } + } + } + let mut hits = Vec::new(); + walk(root, &mut hits); + assert!(hits.is_empty(), "planted bytes were written: {hits:?}"); + assert!(!root.join(".sc/recipients.toml").exists()); +} + +fn source_with_planted_tip(tag: &str, nested: bool) -> (Repo, PathBuf) { + let root = tmp_root(tag); + let src = Repo::init(&root).unwrap(); + let tip = plant(&src, None, nested); + crate::refs::write_branch_tip(src.layout(), "main", &tip).unwrap(); + (src, root) +} + +fn cleanup(repo: Repo, root: &Path) { + drop(repo); + std::fs::remove_dir_all(root).unwrap(); + assert!(!root.exists(), "test root must be removed"); +} + +#[test] +fn clone_of_a_planted_control_file_fails_and_removes_what_it_created() { + for nested in [true, false] { + let (src, src_root) = source_with_planted_tip(&format!("clone-src-{nested}"), nested); + let dst = tmp_root(&format!("clone-dst-{nested}")); + std::fs::remove_dir_all(&dst).unwrap(); + let err = Repo::clone_to(&src_root, &dst) + .err() + .expect("clone must fail"); + assert_unsafe(&err); + assert!(!dst.exists(), "a destination the clone created is removed"); + cleanup(src, &src_root); + } +} + +#[test] +fn failed_clone_into_an_existing_directory_leaves_that_directory_alone() { + let (src, src_root) = source_with_planted_tip("clone-existing-src", true); + let dst = tmp_root("clone-existing-dst"); + std::fs::write(dst.join("mine.txt"), "keep me").unwrap(); + let err = Repo::clone_to(&src_root, &dst) + .err() + .expect("clone must fail"); + assert_unsafe(&err); + assert_eq!(std::fs::read(dst.join("mine.txt")).unwrap(), b"keep me"); + assert!(!dst.join(".sc").exists(), "the clone's own .sc is removed"); + assert_nothing_planted(&dst); + std::fs::remove_dir_all(&dst).unwrap(); + cleanup(src, &src_root); +} + +#[test] +fn switch_onto_a_planted_tip_refuses_before_writing_anything() { + let root = tmp_root("switch"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("a.txt"), "a\n").unwrap(); + let c1 = repo.commit("me", "base").unwrap(); + let evil = plant(&repo, Some(c1), true); + crate::refs::write_branch_tip(repo.layout(), "evil", &evil).unwrap(); + let err = repo.switch("evil").unwrap_err(); + assert_unsafe(&err); + assert_eq!(repo.head_tip().unwrap(), Some(c1), "HEAD did not move"); + assert!(!root.join("ok.txt").exists(), "no file was laid"); + assert!(root.join("a.txt").exists(), "the old tree is untouched"); + assert_nothing_planted(&root); + cleanup(repo, &root); +} + +#[test] +fn fast_forward_onto_a_planted_tip_refuses_before_writing_anything() { + let root = tmp_root("ff"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("a.txt"), "a\n").unwrap(); + let c1 = repo.commit("me", "base").unwrap(); + let evil = plant(&repo, Some(c1), true); + crate::refs::write_branch_tip(repo.layout(), "evil", &evil).unwrap(); + let err = repo.merge("evil", "me").unwrap_err(); + assert_unsafe(&err); + assert_eq!( + repo.head_tip().unwrap(), + Some(c1), + "the branch did not move" + ); + assert!(!root.join("ok.txt").exists(), "no file was laid"); + assert_nothing_planted(&root); + cleanup(repo, &root); +} + +#[test] +fn discard_failed_clone_removes_only_what_the_clone_made() { + let created = tmp_root("discard-created"); + Repo::init(&created).unwrap(); + crate::sync::discard_failed_clone(&created, false); + assert!(!created.exists()); + + let existing = tmp_root("discard-existing"); + std::fs::write(existing.join("mine.txt"), "keep").unwrap(); + Repo::init(&existing).unwrap(); + crate::sync::discard_failed_clone(&existing, true); + assert!(!existing.join(".sc").exists()); + assert_eq!(std::fs::read(existing.join("mine.txt")).unwrap(), b"keep"); + std::fs::remove_dir_all(&existing).unwrap(); + assert!(!existing.exists()); +} + +#[test] +fn commit_refuses_a_control_dir_alias() { + let root = tmp_root("commit"); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("a.txt"), "a\n").unwrap(); + let c1 = repo.commit("me", "base").unwrap(); + std::fs::create_dir_all(root.join("sub/.SC")).unwrap(); + std::fs::write(root.join("sub/.SC/x"), "x").unwrap(); + let err = repo.commit("me", "alias").unwrap_err(); + assert!( + matches!( + &err, + Error::Vfs(scl_vfs::Error::Core(scl_core::Error::UnsafePath { .. })) + ), + "{err:?}" + ); + assert_eq!(repo.head_tip().unwrap(), Some(c1), "no ref moved"); + cleanup(repo, &root); +} + +#[test] +fn private_tree_builder_refuses_a_control_dir_alias() { + let blob = Object::blob(b"x".to_vec()).id(); + let map = |p: &str| std::iter::once((p.to_string(), (blob, FileMode::FILE, 0u8))).collect(); + for bad in [".sc/x", "a/.Sc/b", "sc~1"] { + assert!( + crate::private::build_tree_objects(&map(bad)).is_err(), + "{bad}" + ); + } + assert!(crate::private::build_tree_objects(&map("a/.scignore")).is_ok()); +} diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs index ad15349b..4bb8d06c 100644 --- a/crates/repo/src/private.rs +++ b/crates/repo/src/private.rs @@ -223,6 +223,7 @@ impl Repo { } else { format!("{prefix}/{}", e.name) }; + worktree::check_tree_entry_path(&path)?; match e.kind { EntryKind::Blob => { out.insert(path, (e.id, e.mode, e.perms)); @@ -1106,6 +1107,7 @@ pub(crate) fn build_tree_objects( entries: &BTreeMap, ) -> Result<(ObjectId, Vec)> { scl_vfs::check_file_dir_collisions(entries.keys().map(String::as_str))?; + scl_vfs::check_new_paths(entries.keys().map(String::as_str))?; let mut trees = Vec::new(); let root = build_subtree(entries, "", &mut trees); Ok((root, trees)) diff --git a/crates/repo/src/sync.rs b/crates/repo/src/sync.rs index 00b9cbed..26f7a352 100644 --- a/crates/repo/src/sync.rs +++ b/crates/repo/src/sync.rs @@ -17,6 +17,20 @@ use crate::stdio_transport::open_transport; use crate::transport::Transport; use crate::worktree; +/// Take a failed clone back out of `dst`: the whole directory when the clone +/// created it (`dst_existed` false), otherwise only the `.sc/` it made, so a +/// directory that was already there is never removed. Best effort; the caller +/// must have dropped its [`Repo`] (the repo lock lives in `.sc/`) and reports +/// the clone's own error, not this one. Call it only after `Repo::init` +/// succeeded, since `init` refuses a directory that already holds a `.sc/`. +pub fn discard_failed_clone(dst: &Path, dst_existed: bool) { + let _ = if dst_existed { + std::fs::remove_dir_all(dst.join(".sc")) + } else { + std::fs::remove_dir_all(dst) + }; +} + impl Repo { /// Add a named remote to `.sc/config`. The name becomes a path component /// under `refs/remotes/`, so it is validated like a branch name to keep a @@ -73,8 +87,8 @@ impl Repo { /// gapped (never-transferred) out-of-filter blobs and fail with /// `NotFound` — the sparse spec is what tells `materialize` to skip them. /// - /// On `Err`, `dst` may be left with a partially-initialized `.sc/`; the - /// caller should remove it before retrying. + /// On `Err` the clone is taken back out ([`discard_failed_clone`]): the + /// destination directory if this call created it, else only its `.sc/`. pub fn clone_url_filtered( src_url: &str, dst: impl AsRef, @@ -122,123 +136,132 @@ impl Repo { let remote_refs = transport.list_refs()?; let head_branch = transport.head_branch()?; + let dst_existed = dst.as_ref().exists(); let dst_repo = Repo::init(dst.as_ref())?; - - // Transfer every (in-filter, when filtered) object reachable from - // the remote's branch tips. - let tips: Vec = remote_refs.iter().map(|(_, id)| *id).collect(); - { - let store_arc = dst_repo.vfs.store(); - let mut store = store_arc.lock().unwrap(); - // Fresh clone dst has no local refs yet → no haves → full transfer - // (of whatever `filter` scopes it to). - transfer_objects_opts( - &dst_repo.layout, - transport.as_ref(), - &mut store, - &tips, - &[], - filter, - no_transcripts, - )?; - // Clone-specific belt-and-suspenders (P22 Task 3): the transfer - // above already indexes every signature object it wrote via - // `index_incoming`, but a fresh clone is a wholesale copy of the - // whole (in-filter) reachable set — cheap and simplest to instead - // trust a full post-copy scan of what actually landed on disk, - // rather than depending on the transfer call site's exact - // bookkeeping. Idempotent: `reindex` rewrites the index from - // scratch, so running it after `index_incoming` already populated - // entries is a no-op on top of a no-op, not a double-count. - crate::signatures::reindex(&dst_repo.layout, &mut store)?; - // Clone-specific belt-and-suspenders (P30 Task 4): same - // full-store-scan reindex, for the transcript index. - crate::transcripts::reindex(&dst_repo.layout, &mut store)?; - } - - // Copy branches + HEAD, and seed origin/* remote-tracking refs so - // `merge origin/` resolves immediately and `fetch` has a baseline. - // Filtered clones exclude private branches entirely (ADR-0044): the - // server dropped manifest wants from the pack, so a private ref's - // target never landed — skip the ref rather than minting a dangling - // one. Full clones carry private refs (ciphertext + manifest) intact. - for (branch, tip) in &remote_refs { - if filter.is_some() && !dst_repo.vfs.store().lock().unwrap().contains(tip) { - continue; + // Anything that fails from here leaves a half-built clone: take it + // back out, as `git clone` does. The closure owns `dst_repo`, so its + // repo lock is released before the directory goes. + let result = (|| -> Result { + // Transfer every (in-filter, when filtered) object reachable from + // the remote's branch tips. + let tips: Vec = remote_refs.iter().map(|(_, id)| *id).collect(); + { + let store_arc = dst_repo.vfs.store(); + let mut store = store_arc.lock().unwrap(); + // Fresh clone dst has no local refs yet → no haves → full transfer + // (of whatever `filter` scopes it to). + transfer_objects_opts( + &dst_repo.layout, + transport.as_ref(), + &mut store, + &tips, + &[], + filter, + no_transcripts, + )?; + // Clone-specific belt-and-suspenders (P22 Task 3): the transfer + // above already indexes every signature object it wrote via + // `index_incoming`, but a fresh clone is a wholesale copy of the + // whole (in-filter) reachable set — cheap and simplest to instead + // trust a full post-copy scan of what actually landed on disk, + // rather than depending on the transfer call site's exact + // bookkeeping. Idempotent: `reindex` rewrites the index from + // scratch, so running it after `index_incoming` already populated + // entries is a no-op on top of a no-op, not a double-count. + crate::signatures::reindex(&dst_repo.layout, &mut store)?; + // Clone-specific belt-and-suspenders (P30 Task 4): same + // full-store-scan reindex, for the transcript index. + crate::transcripts::reindex(&dst_repo.layout, &mut store)?; } - refs::write_branch_tip(&dst_repo.layout, branch, tip)?; - refs::write_remote_tip(&dst_repo.layout, "origin", branch, tip)?; - } - // The remote's HEAD branch may itself have been excluded (private): - // fall back to any branch that survived. - let head_branch = if dst_repo.layout.ref_path(&head_branch).exists() { - head_branch - } else { - dst_repo - .branches()? - .first() - .map(|(n, _)| n.clone()) - .unwrap_or(head_branch) - }; - refs::write_head(&dst_repo.layout, &head_branch)?; - - // Record origin. - dst_repo.remote_add("origin", src_url)?; - - // Partial clone: persist the durable fetch-filter marker + a matching - // sparse spec BEFORE the final materialize (see doc comment above for - // why the ordering matters). A full clone keeps the pre-P27 behavior - // exactly: no `.sc/promisor`, and `Sparse::default()` (full - // materialization) rather than `dst_repo.sparse_spec()` — clone still - // doesn't transfer a pre-existing `.sc/sparse` from the source (out - // of P24 scope, sparse config is local like `.scignore`). - let sparse_spec = match filter { - Some(_) => { - let promisor = match blob_limit { - Some(limit) => crate::promisor::Promisor::with_blob_limit( - src_url, - prefixes.to_vec(), - limit, - ), - None => crate::promisor::Promisor::new(src_url, prefixes.to_vec()), - }; - crate::promisor::store(&dst_repo.layout, &promisor)?; - // A size-only clone has no prefixes: the view stays full. - let sparse = crate::sparse::Sparse::new(prefixes.to_vec()); - crate::sparse::store(&dst_repo.layout, &sparse)?; - sparse + + // Copy branches + HEAD, and seed origin/* remote-tracking refs so + // `merge origin/` resolves immediately and `fetch` has a baseline. + // Filtered clones exclude private branches entirely (ADR-0044): the + // server dropped manifest wants from the pack, so a private ref's + // target never landed — skip the ref rather than minting a dangling + // one. Full clones carry private refs (ciphertext + manifest) intact. + for (branch, tip) in &remote_refs { + if filter.is_some() && !dst_repo.vfs.store().lock().unwrap().contains(tip) { + continue; + } + refs::write_branch_tip(&dst_repo.layout, branch, tip)?; + refs::write_remote_tip(&dst_repo.layout, "origin", branch, tip)?; } - None => crate::sparse::Sparse::default(), - }; + // The remote's HEAD branch may itself have been excluded (private): + // fall back to any branch that survived. + let head_branch = if dst_repo.layout.ref_path(&head_branch).exists() { + head_branch + } else { + dst_repo + .branches()? + .first() + .map(|(n, _)| n.clone()) + .unwrap_or(head_branch) + }; + refs::write_head(&dst_repo.layout, &head_branch)?; + + // Record origin. + dst_repo.remote_add("origin", src_url)?; + + // Partial clone: persist the durable fetch-filter marker + a matching + // sparse spec BEFORE the final materialize (see doc comment above for + // why the ordering matters). A full clone keeps the pre-P27 behavior + // exactly: no `.sc/promisor`, and `Sparse::default()` (full + // materialization) rather than `dst_repo.sparse_spec()` — clone still + // doesn't transfer a pre-existing `.sc/sparse` from the source (out + // of P24 scope, sparse config is local like `.scignore`). + let sparse_spec = match filter { + Some(_) => { + let promisor = match blob_limit { + Some(limit) => crate::promisor::Promisor::with_blob_limit( + src_url, + prefixes.to_vec(), + limit, + ), + None => crate::promisor::Promisor::new(src_url, prefixes.to_vec()), + }; + crate::promisor::store(&dst_repo.layout, &promisor)?; + // A size-only clone has no prefixes: the view stays full. + let sparse = crate::sparse::Sparse::new(prefixes.to_vec()); + crate::sparse::store(&dst_repo.layout, &sparse)?; + sparse + } + None => crate::sparse::Sparse::default(), + }; - // Materialize HEAD into the working tree. No identity is available at - // clone time, so PROTECTED files are skipped (ciphertext stays in objects - // but plaintext is not written to disk — correct for unauthorized clones). - // A private HEAD branch (manifest tip, ADR-0044) is not materialized at - // all: checkout needs a recipient identity — `sc switch - // --identity ` after the clone. - if let Some(head_tip) = dst_repo.head_tip()? { - if dst_repo.manifest_at(&head_tip)?.is_some() { - return Ok(dst_repo); + // Materialize HEAD into the working tree. No identity is available at + // clone time, so PROTECTED files are skipped (ciphertext stays in objects + // but plaintext is not written to disk — correct for unauthorized clones). + // A private HEAD branch (manifest tip, ADR-0044) is not materialized at + // all: checkout needs a recipient identity — `sc switch + // --identity ` after the clone. + if let Some(head_tip) = dst_repo.head_tip()? { + if dst_repo.manifest_at(&head_tip)?.is_some() { + return Ok(dst_repo); + } + let store_arc = dst_repo.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let head_snap = store.get_snapshot(&head_tip)?; + let head_root = head_snap.root; + let head_protection = head_snap.protection; + // ensure-view-exempt: clone: the lazy marker is set after the clone, and the materialize preflight covers it + worktree::materialize( + &dst_repo.layout, + &mut store, + head_root, + None, + &head_protection, + None, + &sparse_spec, + None, + )?; } - let store_arc = dst_repo.vfs.store(); - let mut store = store_arc.lock().unwrap(); - let head_snap = store.get_snapshot(&head_tip)?; - let head_root = head_snap.root; - let head_protection = head_snap.protection; - // ensure-view-exempt: clone: the lazy marker is set after the clone, and the materialize preflight covers it - worktree::materialize( - &dst_repo.layout, - &mut store, - head_root, - None, - &head_protection, - None, - &sparse_spec, - None, - )?; + Ok(dst_repo) + })(); + if result.is_err() { + discard_failed_clone(dst.as_ref(), dst_existed); } - Ok(dst_repo) + result } /// Backfill: widen a partial clone by fetching every object matching diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index ecc926d6..af1a45db 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -208,6 +208,7 @@ fn walk_entries( } else { format!("{prefix}/{}", e.name) }; + check_tree_entry_path(&path)?; match e.kind { EntryKind::Blob => { out.insert(path, (e.id, e.mode)); @@ -286,6 +287,7 @@ fn walk_entries_with_perms_sparse( } else { format!("{prefix}/{}", e.name) }; + check_tree_entry_path(&path)?; match e.kind { EntryKind::Blob => { if sparse.matches(&path) { @@ -456,6 +458,7 @@ fn walk_entries_with_perms( } else { format!("{prefix}/{}", e.name) }; + check_tree_entry_path(&path)?; match e.kind { EntryKind::Blob => { out.insert(path, (e.id, e.mode, e.perms)); @@ -544,6 +547,7 @@ fn walk_tree_sparse( } else { format!("{prefix}/{}", e.name) }; + check_tree_entry_path(&path)?; match e.kind { EntryKind::Blob => { if sparse.matches(&path) { @@ -573,6 +577,7 @@ fn walk_tree( } else { format!("{prefix}/{}", e.name) }; + check_tree_entry_path(&path)?; match e.kind { EntryKind::Blob => { out.insert(path, e.id); @@ -701,21 +706,32 @@ pub(crate) fn diff_worktree( Ok(diff) } -/// Join a tree-relative path onto `root`, rejecting any component that could -/// escape the repo root (`..`, `.`, empty, or an absolute path). A committed -/// tree is attacker-influenced data, so an unsafe relpath is a hard error -/// rather than a silent skip — otherwise a malicious tree could write or delete -/// files anywhere on disk during `materialize`. `pub(crate)` so the conflicted -/// merge path (Task 6, P15) can write plaintext marker files straight to the -/// working tree with the same traversal guard. -pub(crate) fn safe_join(root: &Path, rel: &str) -> Result { - for comp in rel.split('/') { - if comp.is_empty() || comp == "." || comp == ".." { - return Err(crate::error::Error::BadRef(format!( - "unsafe path in tree: {rel}" - ))); +/// Refuse a tree path that must never be written under the working tree +/// (traversal, NUL, the `.sc` control directory or an alias of it, and on +/// Windows a separator or drive marker), as the typed +/// [`scl_core::Error::UnsafePath`]. The flatteners below call it on every +/// entry they produce, so a write-out refuses before its first write. +pub(crate) fn check_tree_entry_path(rel: &str) -> Result<()> { + scl_core::check_tree_path(rel, cfg!(windows)).map_err(|reason| { + scl_core::Error::UnsafePath { + path: rel.to_string(), + reason, } - } + .into() + }) +} + +/// Join a tree-relative path onto `root`, rejecting any path +/// [`check_tree_entry_path`] refuses: a component that could escape the repo +/// root (`..`, `.`, empty, an absolute path, on Windows a `\` or drive +/// prefix) or reach the `.sc` control directory. A committed tree is +/// attacker-influenced data, so an unsafe relpath is a hard error rather than +/// a silent skip. The flatteners already refuse before the first write; this +/// is the backstop at the join itself. `pub(crate)` so the conflicted merge +/// path (Task 6, P15) can write plaintext marker files straight to the working +/// tree with the same guard. +pub(crate) fn safe_join(root: &Path, rel: &str) -> Result { + check_tree_entry_path(rel)?; Ok(root.join(rel)) } @@ -784,6 +800,7 @@ pub(crate) fn preflight_view( } else { format!("{path}/{}", e.name) }; + check_tree_entry_path(&child)?; match e.kind { EntryKind::Blob => { if sparse.matches(&child) && !store.contains(&e.id) { @@ -1654,7 +1671,13 @@ mod tests { None, ) .unwrap_err(); - assert!(matches!(err, crate::error::Error::BadRef(_)), "got {err:?}"); + assert!( + matches!( + err, + crate::error::Error::Core(scl_core::Error::UnsafePath { .. }) + ), + "got {err:?}" + ); // Nothing was written outside the repo root: the sibling ".." path // would be the repo's parent dir; assert no stray "pwned" file landed there. let escaped = layout.root.parent().unwrap().join("pwned"); diff --git a/crates/vfs/src/lib.rs b/crates/vfs/src/lib.rs index 1bdd6ae9..cf013b2c 100644 --- a/crates/vfs/src/lib.rs +++ b/crates/vfs/src/lib.rs @@ -231,6 +231,12 @@ impl Repo { .map(String::as_str) .chain(carried.keys().map(String::as_str)), )?; + check_new_paths( + files + .keys() + .map(String::as_str) + .chain(carried.keys().map(String::as_str)), + )?; self.build_subtree_inner(files, carried, "") } @@ -329,6 +335,26 @@ pub fn check_file_dir_collisions<'a>(paths: impl IntoIterator) - Ok(()) } +/// Refuse a path a snapshot must not carry: traversal components, a NUL, or +/// the `.sc` control directory under any alias (see +/// [`scl_core::check_tree_path`]). Checked before any tree object is written, +/// so a new snapshot can never hold what every checkout refuses. The Windows +/// rule is not applied: `:` and `\` are legal names on unix. Public so the +/// private-branch tree builder applies the same rule. +pub fn check_new_paths<'a>(paths: impl IntoIterator) -> Result<()> { + check_paths(paths, false) +} + +fn check_paths<'a>(paths: impl IntoIterator, windows: bool) -> Result<()> { + for p in paths { + scl_core::check_tree_path(p, windows).map_err(|reason| scl_core::Error::UnsafePath { + path: p.to_string(), + reason, + })?; + } + Ok(()) +} + /// A copy-on-write overlay entry. enum Overlay { Written(Arc<[u8]>, FileMode), @@ -494,9 +520,12 @@ impl Worktree { /// the only operation in the engine that writes files; the caller owns the /// destination and is responsible for removing it. pub fn checkout(&self, dest: &Path) -> Result { + // Refuse before the first write: a base tree may come from a peer. + let paths = self.list()?; + check_paths(paths.iter().map(String::as_str), cfg!(windows))?; std::fs::create_dir_all(dest)?; let mut count = 0; - for path in self.list()? { + for path in paths { let bytes = self.read(&path)?; let full = dest.join(&path); if let Some(parent) = full.parent() { @@ -844,4 +873,70 @@ mod tests { std::fs::remove_dir_all(&dest).unwrap(); assert!(!dest.exists()); } + + #[test] + fn a_new_snapshot_refuses_control_dir_aliases_and_nul() { + let r = repo(); + let f = |p: &str| (p.to_string(), b"x".to_vec(), FileMode::FILE, 0u8); + for bad in [".sc/recipients.toml", "a/.SC/b", "sc~1", ".sc.", "a\0b"] { + let err = r.write_tree_with_perms(&[f("ok.txt"), f(bad)]).unwrap_err(); + assert!( + matches!(&err, Error::Core(scl_core::Error::UnsafePath { .. })), + "{bad:?}: {err:?}" + ); + } + // `:` and `\` are legal unix names, and `.scignore` is not `.sc`. + assert!(r + .write_tree_with_perms(&[f("a:b"), f("a\\b"), f(".scignore")]) + .is_ok()); + } + + #[test] + fn checkout_of_a_planted_control_path_refuses_before_writing() { + let r = repo(); + let store = r.store(); + let snap = { + let mut s = store.lock().unwrap(); + let blob = s.put(Object::blob(b"planted".to_vec())).unwrap(); + let entry = |name: &str, kind, id| scl_core::TreeEntry { + name: name.into(), + kind, + id, + mode: FileMode::FILE, + perms: 0, + }; + let sub = s + .put(Object::Tree(Tree::new(vec![entry( + "recipients.toml", + EntryKind::Blob, + blob, + )]))) + .unwrap(); + let root = s + .put(Object::Tree(Tree::new(vec![ + entry(".sc", EntryKind::Tree, sub), + entry("a.txt", EntryKind::Blob, blob), + ]))) + .unwrap(); + s.put(Object::Snapshot(Snapshot { + root, + parents: vec![], + author: "t".into(), + timestamp: 0, + message: "planted".into(), + secrets: Default::default(), + protection: Protection::default(), + })) + .unwrap() + }; + let wt = r.fork(snap, "evil").unwrap(); + let dest = std::env::temp_dir().join(format!("scl-planted-co-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dest); + let err = wt.checkout(&dest).unwrap_err(); + assert!( + matches!(&err, Error::Core(scl_core::Error::UnsafePath { .. })), + "{err:?}" + ); + assert!(!dest.exists(), "nothing is written, not even the directory"); + } } diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 64295074..1d0c4be3 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -813,6 +813,16 @@ protected paths, plaintext. private-branch) refuse it with `FileDirCollision` before any tree is written, for commit, merge completion, `sc ws harvest` and `sc ws refresh`. Remove the directory and retry. + - A tree path is never written if it could reach the `.sc/` control directory + or leave the checkout. A tree comes from a peer and decode accepts any entry + name, so `scl_core::check_tree_path` refuses, per component, an empty, `.`, + `..` or NUL name, `.sc` at any depth under its filesystem aliases (HFS + ignorable code points, NTFS `:stream`, trailing dots and spaces, case, the + short name `sc~1`), and on Windows any `\` or `:`. The tree flatteners and + `safe_join` apply it before the first write, so clone, switch, merge, undo and + `scl_vfs::Worktree::checkout` refuse with `UnsafePath` and change nothing; both + tree builders refuse to commit such a path. A clone that fails removes the + directory it created (only its `.sc/` if the directory already existed). - **Not enforced (known limits):** - A user file whose bytes equal the tracked or laid ones cannot be told from the stop's own write, so it is removed. From 6a80de5ad3eadc48b0a1342d84be2614e74126bf Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:46:44 +0200 Subject: [PATCH 2/7] fix(repo): close review gaps in the tree path guard MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fold case with Unicode uppercase so `.ſc` (U+017F), which a case-insensitive APFS volume resolves to `.sc`, is refused. - Also refuse NTFS's checksum short name of `.sc`: `sc` + four hex digits + `~1` to `~9`, besides `sc~1` to `sc~4`. - Decide clone-directory ownership by claiming it with `create_dir` (`sync::claim_clone_dir`), not from `exists()`: only an `Ok` means the clone created it and may remove it on failure; an existing directory keeps the "remove only the `.sc/` we made" behaviour. - `sc ws fork` and `sc work` validate the target tree's paths before they clear the old session directory, mint `.sc/local-key` or create the session root; on a full clone `preflight_view` did not look at paths. --- crates/cli/src/main.rs | 14 ++++++-- crates/core/src/pathname.rs | 45 +++++++++++++++++++++--- crates/repo/src/path_guard.rs | 64 +++++++++++++++++++++++++++++++++-- crates/repo/src/sync.rs | 53 ++++++++++++++++++++++------- crates/repo/src/workspace.rs | 8 +++++ crates/repo/src/worktree.rs | 10 ++++++ crates/repo/src/ws.rs | 3 ++ docs/THREAT-MODEL.md | 8 +++-- 8 files changed, 180 insertions(+), 25 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index a715a7e8..4df0da1a 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3996,9 +3996,17 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { ); } - let dst_existed = dst.exists(); + let created = scl_repo::sync::claim_clone_dir(dst); std::fs::create_dir_all(dst)?; - let repo = scl_repo::Repo::init(dst)?; + let repo = match scl_repo::Repo::init(dst) { + Ok(r) => r, + Err(e) => { + if created { + scl_repo::sync::discard_failed_clone(dst, true); + } + return Err(e.into()); + } + }; // A failed clone takes itself back out, as `git clone` does; the closure // owns `repo`, so its lock is released before the directory goes. let adopted = (move || -> Result { @@ -4020,7 +4028,7 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { let default = match adopted { Ok(default) => default, Err(e) => { - scl_repo::sync::discard_failed_clone(dst, dst_existed); + scl_repo::sync::discard_failed_clone(dst, created); return Err(e); } }; diff --git a/crates/core/src/pathname.rs b/crates/core/src/pathname.rs index a9314b6e..72eab767 100644 --- a/crates/core/src/pathname.rs +++ b/crates/core/src/pathname.rs @@ -48,12 +48,35 @@ fn hfs_ignorable(c: char) -> bool { /// Whether `component` is `.sc` on any filesystem a checkout may live on: /// mirrors `is_alias` in `crates/forge/src/gitwrite.rs` (HFS-ignorable code /// points dropped, an NTFS `:stream` suffix cut, trailing dots and spaces -/// trimmed, ASCII case folded), against `.sc` and the 8.3 short name `sc~1`. +/// trimmed) but folds case with Unicode uppercase, not ASCII: a case-insensitive +/// APFS volume resolves `.\u{17f}c` (LATIN SMALL LETTER LONG S) to `.sc`, and +/// `'\u{17f}'.to_uppercase()` is `S`. The folded name is then compared against +/// `.SC`, the first-six style 8.3 names `SC~1` to `SC~4`, and NTFS's checksum +/// short name `SC` + four hex digits + `~1` to `~9` (the base of `.sc` is only +/// two characters once the dot is stripped, so NTFS may use that form). fn is_control_dir(component: &str) -> bool { let c: String = component.chars().filter(|c| !hfs_ignorable(*c)).collect(); let c = c.split(':').next().unwrap_or(""); - let c = c.trim_end_matches([' ', '.']).to_ascii_lowercase(); - c == ".sc" || c == "sc~1" + let c: String = c + .trim_end_matches([' ', '.']) + .chars() + .flat_map(char::to_uppercase) + .collect(); + if c == ".SC" { + return true; + } + let Some(tail) = c.strip_prefix("SC") else { + return false; + }; + match tail.split_once('~') { + Some(("", n)) => matches!(n, "1" | "2" | "3" | "4"), + Some((hex, n)) => { + hex.len() == 4 + && hex.bytes().all(|b| b.is_ascii_hexdigit()) + && matches!(n.as_bytes(), [b'1'..=b'9']) + } + None => false, + } } /// Refuse a `/`-separated tree path that must never be written under a @@ -95,6 +118,14 @@ mod tests { "\u{feff}.sc", "sc~1", "SC~1", + "sc~4", + "sc1a2B~1", + "SCABCD~9", + "a/sc0000~3", + ".\u{17f}c", + ".\u{17f}C", + ".\u{17f}c.", + "\u{17f}c~1", ".sc:x", ".sc/recipients.toml", "a/.Sc/b", @@ -146,7 +177,13 @@ mod tests { "a/.sc_x/b", "src/main.rs", ".sc-keep", - "sc~2", + "sc~5", + "sc123~1", + "sc12345~1", + "scxyzw~1", + "sc1234~0", + "sc1234~10", + "\u{17f}x", "..a", "a..", ] { diff --git a/crates/repo/src/path_guard.rs b/crates/repo/src/path_guard.rs index bd75012c..203f5ba1 100644 --- a/crates/repo/src/path_guard.rs +++ b/crates/repo/src/path_guard.rs @@ -175,17 +175,75 @@ fn fast_forward_onto_a_planted_tip_refuses_before_writing_anything() { cleanup(repo, &root); } +/// A repo on `main` whose tip is the planted tree (the working tree is stale, +/// which these commands do not read). +fn repo_at_planted_tip(tag: &str) -> (Repo, PathBuf) { + let root = tmp_root(tag); + let repo = Repo::init(&root).unwrap(); + std::fs::write(root.join("a.txt"), "a\n").unwrap(); + let c1 = repo.commit("me", "base").unwrap(); + let evil = plant(&repo, Some(c1), true); + crate::refs::write_branch_tip(repo.layout(), "main", &evil).unwrap(); + (repo, root) +} + +#[test] +fn ws_fork_on_a_planted_tip_refuses_before_changing_the_filesystem() { + let (repo, root) = repo_at_planted_tip("ws-fork"); + // Residue a fork would clear, and the key it would mint. + let ws = root.join(".sc/ws"); + std::fs::create_dir_all(&ws).unwrap(); + std::fs::write(ws.join("leftover"), "keep").unwrap(); + // The commit in the setup minted the key; drop it so a new one is visible. + let _ = std::fs::remove_file(root.join(".sc/local-key")); + let err = repo.ws_fork(None, 2, "me", None).unwrap_err(); + assert_unsafe(&err); + assert_eq!(std::fs::read(ws.join("leftover")).unwrap(), b"keep"); + assert!(!ws.join("1").exists() && !ws.join("session.toml").exists()); + assert!(!root.join(".sc/local-key").exists(), "no key was minted"); + cleanup(repo, &root); +} + +#[test] +fn sc_work_on_a_planted_tip_refuses_before_creating_the_session() { + let (repo, root) = repo_at_planted_tip("work"); + let scratch = tmp_root("work-scratch"); + let session = scratch.join("session"); + let err = repo + .work(crate::workspace::WorkOptions { + agents: 1, + base_name: "work".into(), + cmd: vec!["true".into()], + author: "t".into(), + message: None, + identity: None, + with_secrets: false, + session_root: Some(session.clone()), + }) + .unwrap_err(); + assert_unsafe(&err); + assert!(!session.exists(), "no session root was created"); + std::fs::remove_dir_all(&scratch).unwrap(); + cleanup(repo, &root); +} + #[test] fn discard_failed_clone_removes_only_what_the_clone_made() { - let created = tmp_root("discard-created"); + // A missing destination (with a missing parent) is claimed, so it is ours. + let parent = tmp_root("discard-parent"); + let created = parent.join("deep/dst"); + assert!(crate::sync::claim_clone_dir(&created)); Repo::init(&created).unwrap(); - crate::sync::discard_failed_clone(&created, false); + crate::sync::discard_failed_clone(&created, true); assert!(!created.exists()); + std::fs::remove_dir_all(&parent).unwrap(); + // An existing directory is never claimed, so only its `.sc/` goes. let existing = tmp_root("discard-existing"); std::fs::write(existing.join("mine.txt"), "keep").unwrap(); + assert!(!crate::sync::claim_clone_dir(&existing)); Repo::init(&existing).unwrap(); - crate::sync::discard_failed_clone(&existing, true); + crate::sync::discard_failed_clone(&existing, false); assert!(!existing.join(".sc").exists()); assert_eq!(std::fs::read(existing.join("mine.txt")).unwrap(), b"keep"); std::fs::remove_dir_all(&existing).unwrap(); diff --git a/crates/repo/src/sync.rs b/crates/repo/src/sync.rs index 26f7a352..51101c06 100644 --- a/crates/repo/src/sync.rs +++ b/crates/repo/src/sync.rs @@ -18,19 +18,38 @@ use crate::transport::Transport; use crate::worktree; /// Take a failed clone back out of `dst`: the whole directory when the clone -/// created it (`dst_existed` false), otherwise only the `.sc/` it made, so a -/// directory that was already there is never removed. Best effort; the caller -/// must have dropped its [`Repo`] (the repo lock lives in `.sc/`) and reports -/// the clone's own error, not this one. Call it only after `Repo::init` -/// succeeded, since `init` refuses a directory that already holds a `.sc/`. -pub fn discard_failed_clone(dst: &Path, dst_existed: bool) { - let _ = if dst_existed { - std::fs::remove_dir_all(dst.join(".sc")) - } else { +/// created it (`created`, as [`claim_clone_dir`] reported), otherwise only the +/// `.sc/` it made, so a directory that was already there is never removed. +/// Best effort; the caller must have dropped its [`Repo`] (the repo lock lives +/// in `.sc/`) and reports the clone's own error, not this one. Call it only +/// after `Repo::init` succeeded, since `init` refuses a directory that already +/// holds a `.sc/`. +pub fn discard_failed_clone(dst: &Path, created: bool) { + let _ = if created { std::fs::remove_dir_all(dst) + } else { + std::fs::remove_dir_all(dst.join(".sc")) }; } +/// Claim the clone destination: `true` only if this call created `dst` (its +/// missing parents are made first), so the clone owns it and may remove it on +/// failure. Anything else, an existing directory included, or an error we +/// cannot interpret, is `false`: the directory is the user's, and a failed +/// clone removes only the `.sc/` it made. It never decides ownership from +/// `exists()`, which a race or a metadata error can get wrong. +pub fn claim_clone_dir(dst: &Path) -> bool { + match std::fs::create_dir(dst) { + Ok(()) => true, + Err(e) if e.kind() == std::io::ErrorKind::NotFound => { + dst.parent() + .is_some_and(|p| std::fs::create_dir_all(p).is_ok()) + && std::fs::create_dir(dst).is_ok() + } + Err(_) => false, + } +} + impl Repo { /// Add a named remote to `.sc/config`. The name becomes a path component /// under `refs/remotes/`, so it is validated like a branch name to keep a @@ -136,8 +155,18 @@ impl Repo { let remote_refs = transport.list_refs()?; let head_branch = transport.head_branch()?; - let dst_existed = dst.as_ref().exists(); - let dst_repo = Repo::init(dst.as_ref())?; + let created = claim_clone_dir(dst.as_ref()); + let dst_repo = match Repo::init(dst.as_ref()) { + Ok(r) => r, + Err(e) => { + // `init` refuses an existing `.sc/`, which is not ours to + // remove; only a directory this call made goes. + if created { + discard_failed_clone(dst.as_ref(), true); + } + return Err(e); + } + }; // Anything that fails from here leaves a half-built clone: take it // back out, as `git clone` does. The closure owns `dst_repo`, so its // repo lock is released before the directory goes. @@ -259,7 +288,7 @@ impl Repo { Ok(dst_repo) })(); if result.is_err() { - discard_failed_clone(dst.as_ref(), dst_existed); + discard_failed_clone(dst.as_ref(), created); } result } diff --git a/crates/repo/src/workspace.rs b/crates/repo/src/workspace.rs index f375e48f..2511b14a 100644 --- a/crates/repo/src/workspace.rs +++ b/crates/repo/src/workspace.rs @@ -284,6 +284,14 @@ impl Repo { } else { crate::sparse::Sparse::default() }; + // Unsafe tree paths refuse before the session root exists (a full + // clone has no other check until the agents' checkouts are written). + { + let tip_root = self.snapshot(&tip)?.root; + let store_arc = self.vfs().store(); + let mut store = store_arc.lock().unwrap(); + crate::worktree::preflight_paths(&mut store, tip_root, &view)?; + } let labels: Vec = (1..=opts.agents) .map(|i| format!("{}-{i}", opts.base_name)) .collect(); diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index af1a45db..4918f0ec 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -735,6 +735,16 @@ pub(crate) fn safe_join(root: &Path, rel: &str) -> Result { Ok(root.join(rel)) } +/// Check that every path `root` lays out under `sparse` is one a checkout may +/// write ([`check_tree_entry_path`]), for a command that changes the +/// filesystem before its own materialize would refuse (`sc ws fork` clears the +/// old session directory and mints `.sc/local-key`; `sc work` creates its +/// session root). Reads only. Call it after [`preflight_view`], so on a partial +/// clone the view's trees are known to be present. +pub(crate) fn preflight_paths(store: &mut Store, root: ObjectId, sparse: &Sparse) -> Result<()> { + tree_file_entries_with_perms_sparse(store, root, sparse).map(|_| ()) +} + /// Check, before anything is written, that every object the sparse view needs /// under `root` is in the store (ADR-0048, the view closure obligation). Only a /// partial clone can have such a gap, so a full clone returns immediately and diff --git a/crates/repo/src/ws.rs b/crates/repo/src/ws.rs index 3cfe3537..6c79ab7c 100644 --- a/crates/repo/src/ws.rs +++ b/crates/repo/src/ws.rs @@ -452,6 +452,9 @@ impl Repo { let store_arc = self.vfs().store(); let mut store = store_arc.lock().unwrap(); crate::worktree::preflight_view(self.layout(), &mut store, tip_root, &sparse)?; + // A full clone returns above without looking at paths: refuse an + // unsafe tree before the old session directory is cleared. + crate::worktree::preflight_paths(&mut store, tip_root, &sparse)?; } let root = ws_dir(self.layout(), name); diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 1d0c4be3..01044918 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -817,9 +817,11 @@ protected paths, plaintext. or leave the checkout. A tree comes from a peer and decode accepts any entry name, so `scl_core::check_tree_path` refuses, per component, an empty, `.`, `..` or NUL name, `.sc` at any depth under its filesystem aliases (HFS - ignorable code points, NTFS `:stream`, trailing dots and spaces, case, the - short name `sc~1`), and on Windows any `\` or `:`. The tree flatteners and - `safe_join` apply it before the first write, so clone, switch, merge, undo and + ignorable code points, NTFS `:stream`, trailing dots and spaces, Unicode case + folding such as `.ſc` on APFS, the 8.3 short names `sc~1` to `sc~4` and + `sc<4 hex>~1` to `~9`), and on Windows any `\` or `:`. The tree flatteners and + `safe_join` apply it before the first write (`sc ws fork` and `sc work` check + first, before they touch the filesystem), so clone, switch, merge, undo and `scl_vfs::Worktree::checkout` refuse with `UnsafePath` and change nothing; both tree builders refuse to commit such a path. A clone that fails removes the directory it created (only its `.sc/` if the directory already existed). From f5536b39f68c865e3e6e7396bc81c3e25db87fe1 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:17:22 +0200 Subject: [PATCH 3/7] fix(repo): make failed-clone cleanup remove only what the clone made MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A `RepoExists` from `Repo::init` after the destination was claimed means another process made `.sc/`: clean up nothing. - `claim_clone_dir` now returns a `CloneClaim` recording each directory this call created (missing parents, then the destination). A failed clone removes its own `.sc/`, then those directories deepest first with `remove_dir`, so a directory that gained other content is left in place and named on stderr. - An existing clone destination must be an empty directory (as `git clone` requires, both clone paths), so a symlink or junction already inside it cannot redirect the checkout's writes. - Match the NTFS checksum short name (`sc` + 4 hex + `~N`) on the original characters with ASCII folding only. Unicode uppercase stays for the `.sc` and `sc~1`..`sc~4` comparison (`.ſc`), so `sc12~1` is no longer a false match. --- crates/cli/src/main.rs | 28 ++++++-- crates/core/src/pathname.rs | 41 +++++------ crates/repo/src/path_guard.rs | 92 +++++++++++++++++++----- crates/repo/src/sync.rs | 129 +++++++++++++++++++++++++--------- 4 files changed, 209 insertions(+), 81 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 4df0da1a..f3ee6372 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3996,14 +3996,12 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { ); } - let created = scl_repo::sync::claim_clone_dir(dst); - std::fs::create_dir_all(dst)?; + // An existing destination must be an empty directory (git parity). + let claim = scl_repo::sync::claim_clone_dir(dst)?; let repo = match scl_repo::Repo::init(dst) { Ok(r) => r, Err(e) => { - if created { - scl_repo::sync::discard_failed_clone(dst, true); - } + scl_repo::sync::discard_after_init_error(&claim, &e); return Err(e.into()); } }; @@ -4028,7 +4026,7 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { let default = match adopted { Ok(default) => default, Err(e) => { - scl_repo::sync::discard_failed_clone(dst, created); + scl_repo::sync::discard_failed_clone(&claim); return Err(e); } }; @@ -5458,6 +5456,24 @@ mod tests { std::fs::remove_dir_all(&root).unwrap(); } + /// Git parity: `sc clone` into a non-empty directory is refused before any + /// network or write, so a symlink already in it cannot redirect the checkout. + #[test] + fn clone_git_into_a_non_empty_directory_is_refused() { + let root = std::env::temp_dir().join(format!("scl-cli-nonempty-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + std::fs::create_dir_all(&root).unwrap(); + std::fs::write(root.join("mine.txt"), "keep me").unwrap(); + let err = run_clone_git("file:///nonexistent/hub.git", &root).unwrap_err(); + assert!( + err.to_string().contains("not an empty directory"), + "unexpected error: {err:#}" + ); + assert_eq!(std::fs::read(root.join("mine.txt")).unwrap(), b"keep me"); + assert!(!root.join(".sc").exists()); + std::fs::remove_dir_all(&root).unwrap(); + } + #[test] fn clone_git_flag_with_local_path_errors_clearly() { // Env mutation (git init) — serialize with the shared git-env lock. diff --git a/crates/core/src/pathname.rs b/crates/core/src/pathname.rs index 72eab767..d326842e 100644 --- a/crates/core/src/pathname.rs +++ b/crates/core/src/pathname.rs @@ -48,35 +48,29 @@ fn hfs_ignorable(c: char) -> bool { /// Whether `component` is `.sc` on any filesystem a checkout may live on: /// mirrors `is_alias` in `crates/forge/src/gitwrite.rs` (HFS-ignorable code /// points dropped, an NTFS `:stream` suffix cut, trailing dots and spaces -/// trimmed) but folds case with Unicode uppercase, not ASCII: a case-insensitive -/// APFS volume resolves `.\u{17f}c` (LATIN SMALL LETTER LONG S) to `.sc`, and -/// `'\u{17f}'.to_uppercase()` is `S`. The folded name is then compared against -/// `.SC`, the first-six style 8.3 names `SC~1` to `SC~4`, and NTFS's checksum -/// short name `SC` + four hex digits + `~1` to `~9` (the base of `.sc` is only -/// two characters once the dot is stripped, so NTFS may use that form). +/// trimmed). The `.sc` and `SC~1` to `SC~4` names are compared after Unicode +/// uppercase folding, not ASCII: a case-insensitive APFS volume resolves +/// `.\u{17f}c` (LATIN SMALL LETTER LONG S) to `.sc`, and +/// `'\u{17f}'.to_uppercase()` is `S`. NTFS's checksum short name, `SC` + four +/// hex digits + `~1` to `~9` (the base of `.sc` is only two characters once the +/// dot is stripped, so NTFS may use that form), is matched on the original +/// characters with ASCII folding only: full Unicode uppercase would turn an +/// ordinary name such as `sc\u{fb00}12~1` (ff ligature) into `SCFF12~1`. fn is_control_dir(component: &str) -> bool { let c: String = component.chars().filter(|c| !hfs_ignorable(*c)).collect(); let c = c.split(':').next().unwrap_or(""); - let c: String = c - .trim_end_matches([' ', '.']) - .chars() - .flat_map(char::to_uppercase) - .collect(); - if c == ".SC" { + let c = c.trim_end_matches([' ', '.']); + let upper: String = c.chars().flat_map(char::to_uppercase).collect(); + if matches!(upper.as_str(), ".SC" | "SC~1" | "SC~2" | "SC~3" | "SC~4") { return true; } - let Some(tail) = c.strip_prefix("SC") else { + let ascii = c.to_ascii_uppercase(); + let Some((hex, n)) = ascii.strip_prefix("SC").and_then(|t| t.split_once('~')) else { return false; }; - match tail.split_once('~') { - Some(("", n)) => matches!(n, "1" | "2" | "3" | "4"), - Some((hex, n)) => { - hex.len() == 4 - && hex.bytes().all(|b| b.is_ascii_hexdigit()) - && matches!(n.as_bytes(), [b'1'..=b'9']) - } - None => false, - } + hex.len() == 4 + && hex.bytes().all(|b| b.is_ascii_hexdigit()) + && matches!(n.as_bytes(), [b'1'..=b'9']) } /// Refuse a `/`-separated tree path that must never be written under a @@ -184,6 +178,9 @@ mod tests { "sc1234~0", "sc1234~10", "\u{17f}x", + "SC\u{fb00}12~1", + "sc\u{fb00}12~1", + "sc\u{17f}123~1", "..a", "a..", ] { diff --git a/crates/repo/src/path_guard.rs b/crates/repo/src/path_guard.rs index 203f5ba1..820199b8 100644 --- a/crates/repo/src/path_guard.rs +++ b/crates/repo/src/path_guard.rs @@ -111,29 +111,33 @@ fn cleanup(repo: Repo, root: &Path) { fn clone_of_a_planted_control_file_fails_and_removes_what_it_created() { for nested in [true, false] { let (src, src_root) = source_with_planted_tip(&format!("clone-src-{nested}"), nested); - let dst = tmp_root(&format!("clone-dst-{nested}")); - std::fs::remove_dir_all(&dst).unwrap(); + // A nested destination: the parents the clone made go too. + let base = tmp_root(&format!("clone-dst-{nested}")); + let dst = base.join("deep/er/victim"); let err = Repo::clone_to(&src_root, &dst) .err() .expect("clone must fail"); assert_unsafe(&err); - assert!(!dst.exists(), "a destination the clone created is removed"); + assert!( + !base.join("deep").exists(), + "the destination and the parents the clone created are removed" + ); + assert!(base.exists(), "the directory that was already there stays"); + std::fs::remove_dir_all(&base).unwrap(); cleanup(src, &src_root); } } #[test] -fn failed_clone_into_an_existing_directory_leaves_that_directory_alone() { +fn failed_clone_into_an_existing_empty_directory_keeps_the_directory() { let (src, src_root) = source_with_planted_tip("clone-existing-src", true); let dst = tmp_root("clone-existing-dst"); - std::fs::write(dst.join("mine.txt"), "keep me").unwrap(); let err = Repo::clone_to(&src_root, &dst) .err() .expect("clone must fail"); assert_unsafe(&err); - assert_eq!(std::fs::read(dst.join("mine.txt")).unwrap(), b"keep me"); - assert!(!dst.join(".sc").exists(), "the clone's own .sc is removed"); - assert_nothing_planted(&dst); + assert!(dst.exists(), "an existing directory is not ours to remove"); + assert_eq!(std::fs::read_dir(&dst).unwrap().count(), 0, "nor to fill"); std::fs::remove_dir_all(&dst).unwrap(); cleanup(src, &src_root); } @@ -229,27 +233,79 @@ fn sc_work_on_a_planted_tip_refuses_before_creating_the_session() { #[test] fn discard_failed_clone_removes_only_what_the_clone_made() { - // A missing destination (with a missing parent) is claimed, so it is ours. + // A missing destination with missing parents is claimed: the destination + // and every parent the claim made go, the directory that was there stays. let parent = tmp_root("discard-parent"); + let created = parent.join("deep/er/dst"); + let claim = crate::sync::claim_clone_dir(&created).unwrap(); + Repo::init(&created).unwrap(); + crate::sync::discard_failed_clone(&claim); + assert!(!parent.join("deep").exists(), "claimed parents are removed"); + assert!(parent.exists(), "a pre-existing parent is not"); + std::fs::remove_dir_all(&parent).unwrap(); + + // A claimed destination that gained a file is left, and so are its parents. + let parent = tmp_root("discard-nonempty"); let created = parent.join("deep/dst"); - assert!(crate::sync::claim_clone_dir(&created)); + let claim = crate::sync::claim_clone_dir(&created).unwrap(); Repo::init(&created).unwrap(); - crate::sync::discard_failed_clone(&created, true); - assert!(!created.exists()); + std::fs::write(created.join("stray"), "x").unwrap(); + crate::sync::discard_failed_clone(&claim); + assert!(!created.join(".sc").exists()); + assert!(created.join("stray").exists(), "remove_dir only, never all"); std::fs::remove_dir_all(&parent).unwrap(); - // An existing directory is never claimed, so only its `.sc/` goes. + // An existing empty directory is accepted and kept; only its `.sc/` goes. let existing = tmp_root("discard-existing"); - std::fs::write(existing.join("mine.txt"), "keep").unwrap(); - assert!(!crate::sync::claim_clone_dir(&existing)); + let claim = crate::sync::claim_clone_dir(&existing).unwrap(); Repo::init(&existing).unwrap(); - crate::sync::discard_failed_clone(&existing, false); - assert!(!existing.join(".sc").exists()); - assert_eq!(std::fs::read(existing.join("mine.txt")).unwrap(), b"keep"); + crate::sync::discard_failed_clone(&claim); + assert!(existing.exists() && !existing.join(".sc").exists()); std::fs::remove_dir_all(&existing).unwrap(); assert!(!existing.exists()); } +#[test] +fn a_repo_exists_race_during_clone_removes_nothing() { + // The destination is claimed while empty, then another process makes `.sc/`. + let parent = tmp_root("race"); + let dst = parent.join("deep/dst"); + let claim = crate::sync::claim_clone_dir(&dst).unwrap(); + std::fs::create_dir_all(dst.join(".sc")).unwrap(); + std::fs::write(dst.join(".sc/precious"), "theirs").unwrap(); + let err = Repo::init(&dst).err().expect("init must refuse"); + assert!(matches!(err, Error::RepoExists(_)), "{err:?}"); + crate::sync::discard_after_init_error(&claim, &err); + assert_eq!(std::fs::read(dst.join(".sc/precious")).unwrap(), b"theirs"); + std::fs::remove_dir_all(&parent).unwrap(); +} + +#[test] +fn clone_into_a_non_empty_directory_is_refused_and_changes_nothing() { + let (src, src_root) = source_with_planted_tip("clone-nonempty-src", true); + let dst = tmp_root("clone-nonempty-dst"); + std::fs::write(dst.join("mine.txt"), "keep me").unwrap(); + let err = Repo::clone_to(&src_root, &dst) + .err() + .expect("clone must fail"); + assert!(matches!(err, Error::InvalidArgument(_)), "{err:?}"); + assert_eq!(std::fs::read(dst.join("mine.txt")).unwrap(), b"keep me"); + assert!(!dst.join(".sc").exists()); + // A symlink already inside the destination could redirect checkout writes. + #[cfg(unix)] + { + std::fs::remove_file(dst.join("mine.txt")).unwrap(); + std::os::unix::fs::symlink("/nonexistent-sc-target", dst.join("link")).unwrap(); + let err = Repo::clone_to(&src_root, &dst) + .err() + .expect("clone must fail"); + assert!(matches!(err, Error::InvalidArgument(_)), "{err:?}"); + assert!(!dst.join(".sc").exists()); + } + std::fs::remove_dir_all(&dst).unwrap(); + cleanup(src, &src_root); +} + #[test] fn commit_refuses_a_control_dir_alias() { let root = tmp_root("commit"); diff --git a/crates/repo/src/sync.rs b/crates/repo/src/sync.rs index 51101c06..3153bce8 100644 --- a/crates/repo/src/sync.rs +++ b/crates/repo/src/sync.rs @@ -17,36 +17,98 @@ use crate::stdio_transport::open_transport; use crate::transport::Transport; use crate::worktree; -/// Take a failed clone back out of `dst`: the whole directory when the clone -/// created it (`created`, as [`claim_clone_dir`] reported), otherwise only the -/// `.sc/` it made, so a directory that was already there is never removed. -/// Best effort; the caller must have dropped its [`Repo`] (the repo lock lives -/// in `.sc/`) and reports the clone's own error, not this one. Call it only -/// after `Repo::init` succeeded, since `init` refuses a directory that already -/// holds a `.sc/`. -pub fn discard_failed_clone(dst: &Path, created: bool) { - let _ = if created { - std::fs::remove_dir_all(dst) - } else { - std::fs::remove_dir_all(dst.join(".sc")) - }; +/// What a clone destination is, as [`claim_clone_dir`] found or made it: the +/// directories this invocation created (missing parents first, then the +/// destination itself), so a failed clone can remove exactly those. +#[derive(Debug)] +pub struct CloneClaim { + dst: std::path::PathBuf, + made: Vec, } -/// Claim the clone destination: `true` only if this call created `dst` (its -/// missing parents are made first), so the clone owns it and may remove it on -/// failure. Anything else, an existing directory included, or an error we -/// cannot interpret, is `false`: the directory is the user's, and a failed -/// clone removes only the `.sc/` it made. It never decides ownership from +/// Claim the clone destination. An existing destination must be an empty +/// directory (as `git clone` requires): a non-empty one is refused, which also +/// keeps a symlink or junction already inside it from redirecting the +/// checkout's writes. A missing one is created, parents included, one +/// `create_dir` at a time, and each directory that call made is recorded. +/// Ownership comes from a `create_dir` that returned `Ok`, never from /// `exists()`, which a race or a metadata error can get wrong. -pub fn claim_clone_dir(dst: &Path) -> bool { - match std::fs::create_dir(dst) { - Ok(()) => true, - Err(e) if e.kind() == std::io::ErrorKind::NotFound => { - dst.parent() - .is_some_and(|p| std::fs::create_dir_all(p).is_ok()) - && std::fs::create_dir(dst).is_ok() +pub fn claim_clone_dir(dst: &Path) -> Result { + let not_empty = || { + Error::InvalidArgument(format!( + "destination path '{}' already exists and is not an empty directory", + dst.display() + )) + }; + let mut claim = CloneClaim { + dst: dst.to_path_buf(), + made: Vec::new(), + }; + match std::fs::metadata(dst) { + Ok(m) if m.is_dir() => { + return if std::fs::read_dir(dst)?.next().is_none() { + Ok(claim) + } else { + Err(not_empty()) + }; + } + Ok(_) => return Err(not_empty()), + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } + // Missing ancestors, outermost first. + let mut missing: Vec<&Path> = dst + .ancestors() + .take_while(|p| { + !p.as_os_str().is_empty() + && matches!(p.symlink_metadata(), Err(e) if e.kind() == std::io::ErrorKind::NotFound) + }) + .collect(); + missing.reverse(); + for dir in missing { + match std::fs::create_dir(dir) { + Ok(()) => claim.made.push(dir.to_path_buf()), + // Someone else made it between our look and now: theirs, not ours. + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {} + Err(e) => { + discard_failed_clone(&claim); + return Err(e.into()); + } } - Err(_) => false, + } + Ok(claim) +} + +/// Take a failed clone back out of the claimed destination, removing only what +/// this invocation made: its own `.sc/`, then the destination and any parents +/// the claim created, deepest first, each only while empty (`remove_dir`, never +/// `remove_dir_all`). A directory that is not empty is left in place and named +/// on stderr. Best effort; the caller must have dropped its [`Repo`] (the repo +/// lock lives in `.sc/`) and reports the clone's own error, not this one. Do +/// not call it when `Repo::init` failed with `RepoExists`: that `.sc/` is +/// another process's. See [`discard_after_init_error`]. +pub fn discard_failed_clone(claim: &CloneClaim) { + let _ = std::fs::remove_dir_all(claim.dst.join(".sc")); + for dir in claim.made.iter().rev() { + if let Err(e) = std::fs::remove_dir(dir) { + if e.kind() != std::io::ErrorKind::NotFound { + eprintln!( + "sc: clone failed; left {} in place (not empty)", + dir.display() + ); + // A parent of a directory we could not remove cannot be empty. + break; + } + } + } +} + +/// [`discard_failed_clone`] for a failed `Repo::init`: if `init` refused with +/// `RepoExists`, another process made the `.sc/` after the claim, so nothing is +/// removed at all; any other `init` failure left only what this call wrote. +pub fn discard_after_init_error(claim: &CloneClaim, err: &Error) { + if !matches!(err, Error::RepoExists(_)) { + discard_failed_clone(claim); } } @@ -106,8 +168,9 @@ impl Repo { /// gapped (never-transferred) out-of-filter blobs and fail with /// `NotFound` — the sparse spec is what tells `materialize` to skip them. /// - /// On `Err` the clone is taken back out ([`discard_failed_clone`]): the - /// destination directory if this call created it, else only its `.sc/`. + /// An existing destination must be an empty directory. On `Err` the clone + /// is taken back out ([`discard_failed_clone`]): its `.sc/`, and the + /// destination and parents this call created while they are empty. pub fn clone_url_filtered( src_url: &str, dst: impl AsRef, @@ -155,15 +218,11 @@ impl Repo { let remote_refs = transport.list_refs()?; let head_branch = transport.head_branch()?; - let created = claim_clone_dir(dst.as_ref()); + let claim = claim_clone_dir(dst.as_ref())?; let dst_repo = match Repo::init(dst.as_ref()) { Ok(r) => r, Err(e) => { - // `init` refuses an existing `.sc/`, which is not ours to - // remove; only a directory this call made goes. - if created { - discard_failed_clone(dst.as_ref(), true); - } + discard_after_init_error(&claim, &e); return Err(e); } }; @@ -288,7 +347,7 @@ impl Repo { Ok(dst_repo) })(); if result.is_err() { - discard_failed_clone(dst.as_ref(), created); + discard_failed_clone(&claim); } result } From bdcd5a7599def8e9ac236fd1432808b0fc8723ef Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:37:03 +0200 Subject: [PATCH 4/7] fix(repo): own `.sc/` exclusively in clone; fold the short-name prefix MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - A clone owns `.sc/` only if its own `create_dir` created it (`CloneClaim::init_repo`, `Repo::init_in_claimed_dot_sc`). Two initializers can both pass an existence check, so the recursive `.sc/` cleanup now runs only for the one whose `create_dir` succeeded; any failure where `.sc/` is not ours leaves it alone. This replaces `discard_after_init_error`. - A destination that appears between the initial lookup and our `create_dir` (`AlreadyExists`) gets the empty-directory check instead of being trusted; a dangling symlink destination is refused. - Fold the `S` and `C` of the NTFS checksum short name with Unicode uppercase, like `.sc` (so `ſC1234~1` is refused), keeping ASCII-only matching for the four hex digits and `~1`..`~9`. - THREAT-MODEL: scope the "changes nothing" guarantee to working-tree writes and ref moves, and describe the clone cleanup rules. --- crates/cli/src/main.rs | 7 +- crates/core/src/pathname.rs | 16 ++++- crates/repo/src/path_guard.rs | 49 ++++++++++--- crates/repo/src/repo.rs | 11 +++ crates/repo/src/sync.rs | 125 +++++++++++++++++++++------------- docs/THREAT-MODEL.md | 11 ++- 6 files changed, 153 insertions(+), 66 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index f3ee6372..fd0c5e3f 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3997,11 +3997,12 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { } // An existing destination must be an empty directory (git parity). - let claim = scl_repo::sync::claim_clone_dir(dst)?; - let repo = match scl_repo::Repo::init(dst) { + let mut claim = scl_repo::sync::claim_clone_dir(dst)?; + let repo = match claim.init_repo() { Ok(r) => r, Err(e) => { - scl_repo::sync::discard_after_init_error(&claim, &e); + // Removes `.sc/` only if `init_repo` created it exclusively. + scl_repo::sync::discard_failed_clone(&claim); return Err(e.into()); } }; diff --git a/crates/core/src/pathname.rs b/crates/core/src/pathname.rs index d326842e..aaebb5b6 100644 --- a/crates/core/src/pathname.rs +++ b/crates/core/src/pathname.rs @@ -64,8 +64,16 @@ fn is_control_dir(component: &str) -> bool { if matches!(upper.as_str(), ".SC" | "SC~1" | "SC~2" | "SC~3" | "SC~4") { return true; } - let ascii = c.to_ascii_uppercase(); - let Some((hex, n)) = ascii.strip_prefix("SC").and_then(|t| t.split_once('~')) else { + // The `S` and `C` fold like `.sc` does (NTFS upcases `\u{17f}` to `S`); the + // four hex digits and the `~N` are ASCII only. + let folds_to = |ch: Option, want: char| { + ch.is_some_and(|ch| ch.to_uppercase().eq(std::iter::once(want))) + }; + let mut chars = c.chars(); + if !(folds_to(chars.next(), 'S') && folds_to(chars.next(), 'C')) { + return false; + } + let Some((hex, n)) = chars.as_str().split_once('~') else { return false; }; hex.len() == 4 @@ -120,6 +128,8 @@ mod tests { ".\u{17f}C", ".\u{17f}c.", "\u{17f}c~1", + "\u{17f}C1234~1", + "\u{17f}c1a2b~1", ".sc:x", ".sc/recipients.toml", "a/.Sc/b", @@ -181,6 +191,8 @@ mod tests { "SC\u{fb00}12~1", "sc\u{fb00}12~1", "sc\u{17f}123~1", + "s\u{17f}1a2b~9", + "\u{17f}\u{17f}1a2b~9", "..a", "a..", ] { diff --git a/crates/repo/src/path_guard.rs b/crates/repo/src/path_guard.rs index 820199b8..b5dae690 100644 --- a/crates/repo/src/path_guard.rs +++ b/crates/repo/src/path_guard.rs @@ -237,8 +237,8 @@ fn discard_failed_clone_removes_only_what_the_clone_made() { // and every parent the claim made go, the directory that was there stays. let parent = tmp_root("discard-parent"); let created = parent.join("deep/er/dst"); - let claim = crate::sync::claim_clone_dir(&created).unwrap(); - Repo::init(&created).unwrap(); + let mut claim = crate::sync::claim_clone_dir(&created).unwrap(); + drop(claim.init_repo().unwrap()); crate::sync::discard_failed_clone(&claim); assert!(!parent.join("deep").exists(), "claimed parents are removed"); assert!(parent.exists(), "a pre-existing parent is not"); @@ -247,8 +247,8 @@ fn discard_failed_clone_removes_only_what_the_clone_made() { // A claimed destination that gained a file is left, and so are its parents. let parent = tmp_root("discard-nonempty"); let created = parent.join("deep/dst"); - let claim = crate::sync::claim_clone_dir(&created).unwrap(); - Repo::init(&created).unwrap(); + let mut claim = crate::sync::claim_clone_dir(&created).unwrap(); + drop(claim.init_repo().unwrap()); std::fs::write(created.join("stray"), "x").unwrap(); crate::sync::discard_failed_clone(&claim); assert!(!created.join(".sc").exists()); @@ -257,8 +257,8 @@ fn discard_failed_clone_removes_only_what_the_clone_made() { // An existing empty directory is accepted and kept; only its `.sc/` goes. let existing = tmp_root("discard-existing"); - let claim = crate::sync::claim_clone_dir(&existing).unwrap(); - Repo::init(&existing).unwrap(); + let mut claim = crate::sync::claim_clone_dir(&existing).unwrap(); + drop(claim.init_repo().unwrap()); crate::sync::discard_failed_clone(&claim); assert!(existing.exists() && !existing.join(".sc").exists()); std::fs::remove_dir_all(&existing).unwrap(); @@ -266,18 +266,45 @@ fn discard_failed_clone_removes_only_what_the_clone_made() { } #[test] -fn a_repo_exists_race_during_clone_removes_nothing() { - // The destination is claimed while empty, then another process makes `.sc/`. +fn a_dot_sc_we_did_not_create_is_never_removed() { + // The destination is claimed while empty, then another process makes + // `.sc/` (with its own repo lock and content) before our exclusive + // `create_dir`: ours fails, and cleanup must leave theirs alone. let parent = tmp_root("race"); let dst = parent.join("deep/dst"); - let claim = crate::sync::claim_clone_dir(&dst).unwrap(); + let mut claim = crate::sync::claim_clone_dir(&dst).unwrap(); std::fs::create_dir_all(dst.join(".sc")).unwrap(); std::fs::write(dst.join(".sc/precious"), "theirs").unwrap(); - let err = Repo::init(&dst).err().expect("init must refuse"); + let err = claim.init_repo().err().expect("not ours"); assert!(matches!(err, Error::RepoExists(_)), "{err:?}"); - crate::sync::discard_after_init_error(&claim, &err); + crate::sync::discard_failed_clone(&claim); assert_eq!(std::fs::read(dst.join(".sc/precious")).unwrap(), b"theirs"); std::fs::remove_dir_all(&parent).unwrap(); + + // Same when the destination already existed (and was empty at the claim). + let dst = tmp_root("race-existing"); + let mut claim = crate::sync::claim_clone_dir(&dst).unwrap(); + std::fs::create_dir(dst.join(".sc")).unwrap(); + std::fs::write(dst.join(".sc/precious"), "theirs").unwrap(); + assert!(claim.init_repo().is_err()); + crate::sync::discard_failed_clone(&claim); + assert_eq!(std::fs::read(dst.join(".sc/precious")).unwrap(), b"theirs"); + std::fs::remove_dir_all(&dst).unwrap(); +} + +#[cfg(unix)] +#[test] +fn a_dangling_symlink_destination_is_refused() { + let base = tmp_root("dangling"); + let dst = base.join("dst"); + std::os::unix::fs::symlink(base.join("nowhere"), &dst).unwrap(); + let err = crate::sync::claim_clone_dir(&dst).unwrap_err(); + assert!(matches!(err, Error::InvalidArgument(_)), "{err:?}"); + assert!( + !base.join("nowhere").exists(), + "nothing was created through it" + ); + std::fs::remove_dir_all(&base).unwrap(); } #[test] diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 748fbe33..86673853 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -76,6 +76,17 @@ impl Repo { if layout.dot_sc.exists() { return Err(Error::RepoExists(layout.dot_sc.display().to_string())); } + Self::init_in_new_dot_sc(layout) + } + + /// [`Repo::init`] for a caller that has just created `.sc/` itself with an + /// exclusive `create_dir` (clone, via [`crate::sync::CloneClaim`]) and so + /// knows it owns the directory: there is no existence check left to race. + pub(crate) fn init_in_claimed_dot_sc(root: &Path) -> Result { + Self::init_in_new_dot_sc(Layout::at(root)) + } + + fn init_in_new_dot_sc(layout: Layout) -> Result { std::fs::create_dir_all(layout.objects_dir())?; std::fs::create_dir_all(layout.refs_heads_dir())?; refs::write_head(&layout, DEFAULT_BRANCH)?; diff --git a/crates/repo/src/sync.rs b/crates/repo/src/sync.rs index 3153bce8..470d1612 100644 --- a/crates/repo/src/sync.rs +++ b/crates/repo/src/sync.rs @@ -19,40 +19,48 @@ use crate::worktree; /// What a clone destination is, as [`claim_clone_dir`] found or made it: the /// directories this invocation created (missing parents first, then the -/// destination itself), so a failed clone can remove exactly those. +/// destination itself) and whether it created `.sc/`, so a failed clone can +/// remove exactly those and nothing else. #[derive(Debug)] pub struct CloneClaim { dst: std::path::PathBuf, made: Vec, + /// `.sc/` was created by this invocation's exclusive `create_dir`. + sc_made: bool, +} + +/// The empty-directory rule for an existing clone destination (as `git clone` +/// requires). A non-empty one is refused, which also keeps a symlink or +/// junction already inside it from redirecting the checkout's writes. +fn ensure_empty_dir(dst: &Path) -> Result<()> { + if std::fs::metadata(dst).is_ok_and(|m| m.is_dir()) && std::fs::read_dir(dst)?.next().is_none() + { + return Ok(()); + } + Err(Error::InvalidArgument(format!( + "destination path '{}' already exists and is not an empty directory", + dst.display() + ))) } /// Claim the clone destination. An existing destination must be an empty -/// directory (as `git clone` requires): a non-empty one is refused, which also -/// keeps a symlink or junction already inside it from redirecting the -/// checkout's writes. A missing one is created, parents included, one -/// `create_dir` at a time, and each directory that call made is recorded. -/// Ownership comes from a `create_dir` that returned `Ok`, never from -/// `exists()`, which a race or a metadata error can get wrong. +/// directory ([`ensure_empty_dir`]). A missing one is created, parents +/// included, one `create_dir` at a time, and each directory that call made is +/// recorded. Ownership comes from a `create_dir` that returned `Ok`, never from +/// `exists()`, which a race or a metadata error can get wrong; a destination +/// that appears between our look and our `create_dir` (`AlreadyExists`) gets +/// the same empty-directory check instead of being trusted. pub fn claim_clone_dir(dst: &Path) -> Result { - let not_empty = || { - Error::InvalidArgument(format!( - "destination path '{}' already exists and is not an empty directory", - dst.display() - )) - }; let mut claim = CloneClaim { dst: dst.to_path_buf(), made: Vec::new(), + sc_made: false, }; - match std::fs::metadata(dst) { - Ok(m) if m.is_dir() => { - return if std::fs::read_dir(dst)?.next().is_none() { - Ok(claim) - } else { - Err(not_empty()) - }; + match dst.symlink_metadata() { + Ok(_) => { + ensure_empty_dir(dst)?; + return Ok(claim); } - Ok(_) => return Err(not_empty()), Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} Err(e) => return Err(e.into()), } @@ -66,29 +74,60 @@ pub fn claim_clone_dir(dst: &Path) -> Result { .collect(); missing.reverse(); for dir in missing { - match std::fs::create_dir(dir) { - Ok(()) => claim.made.push(dir.to_path_buf()), + let made = match std::fs::create_dir(dir) { + Ok(()) => { + claim.made.push(dir.to_path_buf()); + continue; + } // Someone else made it between our look and now: theirs, not ours. - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {} - Err(e) => { - discard_failed_clone(&claim); - return Err(e.into()); + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + if dir == dst { + ensure_empty_dir(dst) + } else { + Ok(()) + } } + Err(e) => Err(e.into()), + }; + if let Err(e) = made { + discard_failed_clone(&claim); + return Err(e); } } Ok(claim) } +impl CloneClaim { + /// Create the repository in the claimed destination, owning `.sc/` by + /// creating it exclusively (`create_dir`; `AlreadyExists` means another + /// process's, reported as `RepoExists`, and it is never removed by + /// [`discard_failed_clone`]). Two initializers can both pass an + /// existence check, so cleanup of `.sc/` is allowed only for the one whose + /// `create_dir` succeeded. + pub fn init_repo(&mut self) -> Result { + let dot_sc = self.dst.join(".sc"); + match std::fs::create_dir(&dot_sc) { + Ok(()) => self.sc_made = true, + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(Error::RepoExists(dot_sc.display().to_string())); + } + Err(e) => return Err(e.into()), + } + Repo::init_in_claimed_dot_sc(&self.dst) + } +} + /// Take a failed clone back out of the claimed destination, removing only what -/// this invocation made: its own `.sc/`, then the destination and any parents -/// the claim created, deepest first, each only while empty (`remove_dir`, never -/// `remove_dir_all`). A directory that is not empty is left in place and named -/// on stderr. Best effort; the caller must have dropped its [`Repo`] (the repo -/// lock lives in `.sc/`) and reports the clone's own error, not this one. Do -/// not call it when `Repo::init` failed with `RepoExists`: that `.sc/` is -/// another process's. See [`discard_after_init_error`]. +/// this invocation made: its own `.sc/` (only if [`CloneClaim::init_repo`] +/// created it), then the destination and any parents the claim created, +/// deepest first, each only while empty (`remove_dir`, never `remove_dir_all`). +/// A directory that is not empty is left in place and named on stderr. Best +/// effort; the caller must have dropped its [`Repo`] (the repo lock lives in +/// `.sc/`) and reports the clone's own error, not this one. pub fn discard_failed_clone(claim: &CloneClaim) { - let _ = std::fs::remove_dir_all(claim.dst.join(".sc")); + if claim.sc_made { + let _ = std::fs::remove_dir_all(claim.dst.join(".sc")); + } for dir in claim.made.iter().rev() { if let Err(e) = std::fs::remove_dir(dir) { if e.kind() != std::io::ErrorKind::NotFound { @@ -103,15 +142,6 @@ pub fn discard_failed_clone(claim: &CloneClaim) { } } -/// [`discard_failed_clone`] for a failed `Repo::init`: if `init` refused with -/// `RepoExists`, another process made the `.sc/` after the claim, so nothing is -/// removed at all; any other `init` failure left only what this call wrote. -pub fn discard_after_init_error(claim: &CloneClaim, err: &Error) { - if !matches!(err, Error::RepoExists(_)) { - discard_failed_clone(claim); - } -} - impl Repo { /// Add a named remote to `.sc/config`. The name becomes a path component /// under `refs/remotes/`, so it is validated like a branch name to keep a @@ -218,11 +248,12 @@ impl Repo { let remote_refs = transport.list_refs()?; let head_branch = transport.head_branch()?; - let claim = claim_clone_dir(dst.as_ref())?; - let dst_repo = match Repo::init(dst.as_ref()) { + let mut claim = claim_clone_dir(dst.as_ref())?; + let dst_repo = match claim.init_repo() { Ok(r) => r, Err(e) => { - discard_after_init_error(&claim, &e); + // Removes `.sc/` only if `init_repo` created it exclusively. + discard_failed_clone(&claim); return Err(e); } }; diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 01044918..eab1577f 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -822,9 +822,14 @@ protected paths, plaintext. `sc<4 hex>~1` to `~9`), and on Windows any `\` or `:`. The tree flatteners and `safe_join` apply it before the first write (`sc ws fork` and `sc work` check first, before they touch the filesystem), so clone, switch, merge, undo and - `scl_vfs::Worktree::checkout` refuse with `UnsafePath` and change nothing; both - tree builders refuse to commit such a path. A clone that fails removes the - directory it created (only its `.sc/` if the directory already existed). + `scl_vfs::Worktree::checkout` refuse with `UnsafePath` before any + working-tree write or ref move; both tree builders refuse to commit such a + path. The guarantee covers working-tree writes and refs, not every file: an + unborn merge can mint `.sc/local-key` before the refusal, and a lazy fill can + keep the (hash-verified) objects it fetched. A clone that fails removes only + what it made: a `.sc/` it created exclusively, and the destination and + parents it created while they are empty. An existing clone destination must + be an empty directory. - **Not enforced (known limits):** - A user file whose bytes equal the tracked or laid ones cannot be told from the stop's own write, so it is removed. From 7de8eb24c6a10a704f0d56950d826cc0381d462c Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:57:11 +0200 Subject: [PATCH 5/7] fix(repo): make `init` own `.sc/` exclusively; hold the clone lock in cleanup - `Repo::init` claims `.sc/` with an exclusive `create_dir` (`AlreadyExists` = `RepoExists`), so plain `sc init` can no longer enter a clone's freshly created `.sc/`; both paths own `.sc/` only by creating it. - A failed clone removes its `.sc/` while its repo lock is still held (the clone body borrows the repo instead of owning it), so a concurrent `Repo::open` cannot take the lock in a gap and have its repo deleted. - `claim_clone_dir` always routes the destination itself through `create_dir` and applies the empty-directory check on `AlreadyExists`, so a destination that appears after the lookup is not accepted unchecked. - Docs: a native clone writes branch refs and HEAD in its new `.sc/` before the path check and removes them on failure; fix the short-name comment in pathname.rs. --- crates/cli/src/main.rs | 7 +-- crates/core/src/pathname.rs | 7 +-- crates/repo/src/path_guard.rs | 20 ++++++++ crates/repo/src/repo.rs | 18 ++++--- crates/repo/src/sync.rs | 92 ++++++++++++++++++----------------- docs/THREAT-MODEL.md | 4 +- 6 files changed, 89 insertions(+), 59 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index fd0c5e3f..1544cf20 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -4006,9 +4006,9 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { return Err(e.into()); } }; - // A failed clone takes itself back out, as `git clone` does; the closure - // owns `repo`, so its lock is released before the directory goes. - let adopted = (move || -> Result { + // A failed clone takes itself back out, as `git clone` does, while `repo` + // still holds the repo lock. + let adopted = (|| -> Result { repo.remote_add_git("origin", url)?; // Sync the mirror, then point the unborn HEAD at the remote's default @@ -4028,6 +4028,7 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { Ok(default) => default, Err(e) => { scl_repo::sync::discard_failed_clone(&claim); + drop(repo); return Err(e); } }; diff --git a/crates/core/src/pathname.rs b/crates/core/src/pathname.rs index aaebb5b6..e5f3779e 100644 --- a/crates/core/src/pathname.rs +++ b/crates/core/src/pathname.rs @@ -53,9 +53,10 @@ fn hfs_ignorable(c: char) -> bool { /// `.\u{17f}c` (LATIN SMALL LETTER LONG S) to `.sc`, and /// `'\u{17f}'.to_uppercase()` is `S`. NTFS's checksum short name, `SC` + four /// hex digits + `~1` to `~9` (the base of `.sc` is only two characters once the -/// dot is stripped, so NTFS may use that form), is matched on the original -/// characters with ASCII folding only: full Unicode uppercase would turn an -/// ordinary name such as `sc\u{fb00}12~1` (ff ligature) into `SCFF12~1`. +/// dot is stripped, so NTFS may use that form), folds its `SC` prefix the same +/// way (NTFS upcases `\u{17f}` too) but matches the four hex digits and the +/// `~N` on the original characters, ASCII only: uppercasing the whole name +/// would turn an ordinary `sc\u{fb00}12~1` (ff ligature) into `SCFF12~1`. fn is_control_dir(component: &str) -> bool { let c: String = component.chars().filter(|c| !hfs_ignorable(*c)).collect(); let c = c.split(':').next().unwrap_or(""); diff --git a/crates/repo/src/path_guard.rs b/crates/repo/src/path_guard.rs index b5dae690..ad94837a 100644 --- a/crates/repo/src/path_guard.rs +++ b/crates/repo/src/path_guard.rs @@ -292,6 +292,26 @@ fn a_dot_sc_we_did_not_create_is_never_removed() { std::fs::remove_dir_all(&dst).unwrap(); } +#[test] +fn plain_init_never_enters_a_dot_sc_it_did_not_create() { + // A clone's freshly made `.sc/` must not be adopted by `sc init`. + let parent = tmp_root("init-race"); + let dst = parent.join("dst"); + let mut claim = crate::sync::claim_clone_dir(&dst).unwrap(); + let repo = claim.init_repo().unwrap(); + let err = Repo::init(&dst).err().expect("init must refuse"); + assert!(matches!(err, Error::RepoExists(_)), "{err:?}"); + // The clone fails and cleans up while it still holds its repo lock. + crate::sync::discard_failed_clone(&claim); + assert!(!dst.exists()); + drop(repo); + assert!( + !dst.exists(), + "dropping the lock after cleanup recreates nothing" + ); + std::fs::remove_dir_all(&parent).unwrap(); +} + #[cfg(unix)] #[test] fn a_dangling_symlink_destination_is_refused() { diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 86673853..61841091 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -69,19 +69,23 @@ impl Repo { /// Create a new repo at `root` (errors if `.sc/` already exists). pub fn init(root: impl AsRef) -> Result { let layout = Layout::at(root.as_ref()); - // The exists-check then create_dir_all is a benign TOCTOU: under the - // single-writer assumption (one `sc` process per repo at a time) no - // concurrent creator can race between the two; a second `init` either - // sees `.sc` and errors here or loses the lock in `open_layout`. - if layout.dot_sc.exists() { - return Err(Error::RepoExists(layout.dot_sc.display().to_string())); + // `.sc/` is owned only by whoever creates it: an exclusive `create_dir` + // (`AlreadyExists` = someone else's, e.g. a clone still building it), + // not an existence check that two initializers can both pass. + std::fs::create_dir_all(&layout.root)?; + match std::fs::create_dir(&layout.dot_sc) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { + return Err(Error::RepoExists(layout.dot_sc.display().to_string())); + } + Err(e) => return Err(e.into()), } Self::init_in_new_dot_sc(layout) } /// [`Repo::init`] for a caller that has just created `.sc/` itself with an /// exclusive `create_dir` (clone, via [`crate::sync::CloneClaim`]) and so - /// knows it owns the directory: there is no existence check left to race. + /// knows it owns the directory. pub(crate) fn init_in_claimed_dot_sc(root: &Path) -> Result { Self::init_in_new_dot_sc(Layout::at(root)) } diff --git a/crates/repo/src/sync.rs b/crates/repo/src/sync.rs index 470d1612..dbf13822 100644 --- a/crates/repo/src/sync.rs +++ b/crates/repo/src/sync.rs @@ -44,57 +44,52 @@ fn ensure_empty_dir(dst: &Path) -> Result<()> { } /// Claim the clone destination. An existing destination must be an empty -/// directory ([`ensure_empty_dir`]). A missing one is created, parents -/// included, one `create_dir` at a time, and each directory that call made is -/// recorded. Ownership comes from a `create_dir` that returned `Ok`, never from -/// `exists()`, which a race or a metadata error can get wrong; a destination -/// that appears between our look and our `create_dir` (`AlreadyExists`) gets -/// the same empty-directory check instead of being trusted. +/// directory ([`ensure_empty_dir`]). Missing parents are created one +/// `create_dir` at a time, and the destination itself always goes through +/// `create_dir` too: `Ok` means this call made it and each such directory is +/// recorded; `AlreadyExists` (it was there, or appeared since we looked) gets +/// the empty-directory check instead of being trusted. Ownership never comes +/// from `exists()`, which a race or a metadata error can get wrong. pub fn claim_clone_dir(dst: &Path) -> Result { let mut claim = CloneClaim { dst: dst.to_path_buf(), made: Vec::new(), sc_made: false, }; - match dst.symlink_metadata() { - Ok(_) => { - ensure_empty_dir(dst)?; - return Ok(claim); - } - Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} - Err(e) => return Err(e.into()), - } - // Missing ancestors, outermost first. - let mut missing: Vec<&Path> = dst + // Missing ancestors of the destination, outermost first. + let mut parents: Vec<&Path> = dst .ancestors() + .skip(1) .take_while(|p| { !p.as_os_str().is_empty() && matches!(p.symlink_metadata(), Err(e) if e.kind() == std::io::ErrorKind::NotFound) }) .collect(); - missing.reverse(); - for dir in missing { - let made = match std::fs::create_dir(dir) { - Ok(()) => { - claim.made.push(dir.to_path_buf()); - continue; - } - // Someone else made it between our look and now: theirs, not ours. - Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => { - if dir == dst { - ensure_empty_dir(dst) - } else { - Ok(()) - } + parents.reverse(); + let result = (|| -> Result<()> { + for dir in parents { + match std::fs::create_dir(dir) { + Ok(()) => claim.made.push(dir.to_path_buf()), + // Made by someone else since we looked: theirs, not ours. A + // non-directory there fails the destination's `create_dir`. + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => {} + Err(e) => return Err(e.into()), } - Err(e) => Err(e.into()), - }; - if let Err(e) = made { + } + match std::fs::create_dir(dst) { + Ok(()) => claim.made.push(dst.to_path_buf()), + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => ensure_empty_dir(dst)?, + Err(e) => return Err(e.into()), + } + Ok(()) + })(); + match result { + Ok(()) => Ok(claim), + Err(e) => { discard_failed_clone(&claim); - return Err(e); + Err(e) } } - Ok(claim) } impl CloneClaim { @@ -122,8 +117,9 @@ impl CloneClaim { /// created it), then the destination and any parents the claim created, /// deepest first, each only while empty (`remove_dir`, never `remove_dir_all`). /// A directory that is not empty is left in place and named on stderr. Best -/// effort; the caller must have dropped its [`Repo`] (the repo lock lives in -/// `.sc/`) and reports the clone's own error, not this one. +/// effort; the caller reports the clone's own error, not this one, and should +/// call it while still holding the clone's repo lock (a concurrent open cannot +/// then take the lock in a gap and have its repo deleted). pub fn discard_failed_clone(claim: &CloneClaim) { if claim.sc_made { let _ = std::fs::remove_dir_all(claim.dst.join(".sc")); @@ -258,9 +254,8 @@ impl Repo { } }; // Anything that fails from here leaves a half-built clone: take it - // back out, as `git clone` does. The closure owns `dst_repo`, so its - // repo lock is released before the directory goes. - let result = (|| -> Result { + // back out, as `git clone` does, while still holding the repo lock. + let result = (|| -> Result<()> { // Transfer every (in-filter, when filtered) object reachable from // the remote's branch tips. let tips: Vec = remote_refs.iter().map(|(_, id)| *id).collect(); @@ -356,7 +351,7 @@ impl Repo { // --identity ` after the clone. if let Some(head_tip) = dst_repo.head_tip()? { if dst_repo.manifest_at(&head_tip)?.is_some() { - return Ok(dst_repo); + return Ok(()); } let store_arc = dst_repo.vfs.store(); let mut store = store_arc.lock().unwrap(); @@ -375,12 +370,19 @@ impl Repo { None, )?; } - Ok(dst_repo) + Ok(()) })(); - if result.is_err() { - discard_failed_clone(&claim); + match result { + Ok(()) => Ok(dst_repo), + Err(e) => { + // Remove `.sc/` while `dst_repo` still holds the repo lock, so + // a concurrent `Repo::open` cannot take it in a gap and have + // its repo deleted; the lock file goes with the directory. + discard_failed_clone(&claim); + drop(dst_repo); + Err(e) + } } - result } /// Backfill: widen a partial clone by fetching every object matching diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index eab1577f..251feb35 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -826,7 +826,9 @@ protected paths, plaintext. working-tree write or ref move; both tree builders refuse to commit such a path. The guarantee covers working-tree writes and refs, not every file: an unborn merge can mint `.sc/local-key` before the refusal, and a lazy fill can - keep the (hash-verified) objects it fetched. A clone that fails removes only + keep the (hash-verified) objects it fetched. A native clone writes branch + refs and HEAD in its new `.sc/` before the path check; a failed clone removes + them with the `.sc/` (it holds the repo lock until then). It removes only what it made: a `.sc/` it created exclusively, and the destination and parents it created while they are empty. An existing clone destination must be an empty directory. From 1dd2060f93f34394b965b06300149ad53a963a37 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 12:08:40 +0200 Subject: [PATCH 6/7] fix(repo): refuse materialize writes through a symlinked parent `materialize` skips the parent-obstruction checks when there is no old tree (an unborn merge, a clone), so a remote entry `link/x` would write through a user's untracked `link -> .sc` or a directory outside the checkout. Before its first write, for every path it will write and whatever `old_root` is, refuse when any parent component is a symlink (`under_symlink`, `symlink_metadata`, never followed). The unborn merge keeps its documented "no untracked-overwrite refusal" behaviour otherwise. Docs updated to match. --- CLAUDE.md | 2 +- crates/repo/src/path_guard.rs | 48 +++++++++++++++++++++++++++++++++++ crates/repo/src/worktree.rs | 12 +++++++++ docs/THREAT-MODEL.md | 10 +++++--- 4 files changed, 68 insertions(+), 4 deletions(-) diff --git a/CLAUDE.md b/CLAUDE.md index b7905b41..96e547e7 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -366,7 +366,7 @@ transport-adjacent. The rest, imperatively: directory onto an untracked or ignored one** (`refuse_untracked_overwrite`); move it away and retry. A directory holding only tracked files is not such a case: a file/directory change at one path crosses switch, undo, abort and `--continue`, and the emptied directories are cleared (#247). `materialize` checks every such directory before its first removal, so a refusal changes nothing and an abort stays retryable; a forward op also refuses a user's empty directory there (it cannot tell it from a husk), while `materialize` itself prunes empty ones. (On a case-insensitive checkout, replacing `foo/a` with a file `Foo` is refused: the check does not follow the case alias. Fail-closed.) On a sparse checkout the same holds for a path the result adds outside the view (a later commit would take the user's file instead, #248); there any directory at the path refuses, tracked contents or not. - Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded (except that a private stop refuses a `.theirs` sidecar that would land on an existing file). + Merging into an unborn branch and the conflict stop of a merge into a private branch are not guarded (except that a private stop refuses a `.theirs` sidecar that would land on an existing file, and every `materialize`, unborn merge included, refuses a path with a symlink among its parent directories). - **A snapshot never holds a path as both a file and a directory (#278).** The tree builders (`scl_vfs::Repo::build_root`, and `private::build_tree_objects` for private branches) refuse it with `FileDirCollision { file, child }` before writing any tree. It matters because diff --git a/crates/repo/src/path_guard.rs b/crates/repo/src/path_guard.rs index ad94837a..3bc8e2fc 100644 --- a/crates/repo/src/path_guard.rs +++ b/crates/repo/src/path_guard.rs @@ -292,6 +292,54 @@ fn a_dot_sc_we_did_not_create_is_never_removed() { std::fs::remove_dir_all(&dst).unwrap(); } +#[cfg(unix)] +#[test] +fn unborn_merge_refuses_a_write_through_a_symlinked_parent() { + let root = tmp_root("symlink-parent"); + let outside = tmp_root("symlink-outside"); + let repo = Repo::init(&root).unwrap(); + // The user's own untracked symlink; the remote tree has `link/x`. + std::os::unix::fs::symlink(&outside, root.join("link")).unwrap(); + let theirs = { + let store = repo.vfs_handle().store(); + let mut s = store.lock().unwrap(); + let blob = s.put(Object::blob(b"planted".to_vec())).unwrap(); + let sub = s + .put(Object::Tree(Tree::new(vec![entry( + "x", + EntryKind::Blob, + blob, + )]))) + .unwrap(); + let tree = s + .put(Object::Tree(Tree::new(vec![entry( + "link", + EntryKind::Tree, + sub, + )]))) + .unwrap(); + s.put(Object::Snapshot(Snapshot { + root: tree, + parents: vec![], + author: "t".into(), + timestamp: 0, + message: "theirs".into(), + secrets: Default::default(), + protection: Protection::default(), + })) + .unwrap() + }; + crate::refs::write_branch_tip(repo.layout(), "other", &theirs).unwrap(); + assert_eq!(repo.head_tip().unwrap(), None, "main is unborn"); + let err = repo.merge("other", "me").unwrap_err(); + assert!(matches!(err, Error::InvalidArgument(_)), "{err:?}"); + assert!(!outside.join("x").exists(), "nothing written outside"); + assert_eq!(std::fs::read_dir(&outside).unwrap().count(), 0); + assert_eq!(repo.head_tip().unwrap(), None, "the ref did not move"); + cleanup(repo, &root); + std::fs::remove_dir_all(&outside).unwrap(); +} + #[test] fn plain_init_never_enters_a_dot_sc_it_did_not_create() { // A clone's freshly made `.sc/` must not be adopted by `sc init`. diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index 4918f0ec..ad3e77a5 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -1265,6 +1265,18 @@ fn materialize_impl( // clone never fetched out-of-filter subtrees at all, so an unfiltered // walk here would `NotFound` on them. let target = tree_file_entries_with_perms_sparse(store, target_root, sparse)?; + // Before the first write, whatever `old_root` is (an unborn merge or a clone + // has none, so the obstruction checks below do not run): a symlink among a + // written path's parents would carry the write out of its directory, e.g. + // `link -> .sc` or a directory outside the checkout. Never followed. + for path in target.keys().filter(|p| sparse.matches(p)) { + if under_symlink(&layout.root, path) { + return Err(Error::InvalidArgument(format!( + "cannot write {path}: a parent directory is a symlink; \ + move it away, then retry" + ))); + } + } // Set when an interrupted `sparse set`'s marker widened the removal bound; // cleared once this call has finished laying the tree out under `sparse`. let mut prev_marker_used = false; diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 251feb35..563f1d35 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -785,9 +785,13 @@ protected paths, plaintext. before any write, to lay a file or directory onto an untracked or ignored one (`refuse_untracked_overwrite`), including a path outside the sparse view that the result adds (#248, #251). A refusal changes nothing; move the - file away and retry. Not guarded: merging into an unborn branch, and the - conflict stop of a merge into a private branch (it checks only out-of-view - paths, and refuses a `.theirs` sidecar onto an existing file). + file away and retry. Not guarded: merging into an unborn branch (and a + clone), except that every `materialize` refuses, before its first write, a + path with a symlink among its parent directories, so a remote entry + `link/x` cannot write through a user's `link -> .sc` or out of the + checkout; and the conflict stop of a merge into a private branch (it checks + only out-of-view paths, and refuses a `.theirs` sidecar onto an existing + file). - A stop's `materialize` deletes or overwrites a path only if the tree it replaces tracks it and the disk copy is a regular file with exactly the tracked bytes, or the stop itself recorded the path (`.sc/CONFLICT_PLAINTEXT`, From 2afd31840096985beee94231eb5fd104eedd8bab Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 12:25:01 +0200 Subject: [PATCH 7/7] fix(repo): let an abort keep paths under a symlinked parent The symlinked-parent refusal in `materialize` also fired for `materialize_restore` (merge/pick/rebase abort), which must finish anyway. In restore mode a path with a symlink among its parents is now skipped (not written, not removed, not counted in the blocked-parent or directory-clearable checks) and reported in `kept`, like the leaf-symlink case; forward operations keep refusing. `under_symlink` now fails closed on an lstat error other than NotFound or NotADirectory. The existing abort test that expected a refusal for a symlink standing where a directory must be now expects the abort to finish with the path in `kept` and nothing written through the link. THREAT-MODEL updated. --- crates/repo/src/user_file_safety.rs | 34 +++++++++++++++++++++- crates/repo/src/worktree.rs | 45 +++++++++++++++++++---------- docs/THREAT-MODEL.md | 4 ++- 3 files changed, 66 insertions(+), 17 deletions(-) diff --git a/crates/repo/src/user_file_safety.rs b/crates/repo/src/user_file_safety.rs index eadca58f..c7a22386 100644 --- a/crates/repo/src/user_file_safety.rs +++ b/crates/repo/src/user_file_safety.rs @@ -299,7 +299,9 @@ fn abort_never_writes_through_a_symlink_where_a_directory_must_be() { std::fs::create_dir_all(&outside).unwrap(); std::fs::remove_file(root.join("zz")).unwrap(); std::os::unix::fs::symlink(&outside, root.join("zz")).unwrap(); - repo.merge_abort().unwrap_err(); + // The abort finishes: the path under the link is left alone and reported. + let report = repo.merge_abort().unwrap(); + assert!(report.kept.contains(&"zz/a".to_string()), "{report:?}"); assert!(!outside.join("a").exists(), "wrote through the symlink"); std::fs::remove_dir_all(&outside).unwrap(); finish(&root, repo); @@ -356,6 +358,36 @@ fn abort_keeps_a_symlink_at_a_recorded_protected_path() { finish(&root, repo); } +/// The user replaced a tracked directory with a symlink after the stop: the +/// abort still finishes, writes nothing through the link, and reports the +/// paths under it as kept. (A forward operation refuses such a path.) +#[cfg(unix)] +#[test] +fn abort_keeps_paths_under_a_symlink_that_replaced_a_tracked_directory() { + for op in OPS { + let (root, repo) = stopped("dir-symlink", op, false); + let outside = root_for("dir-symlink-outside", op); + std::fs::remove_dir_all(root.join("docs")).unwrap(); + std::os::unix::fs::symlink(&outside, root.join("docs")).unwrap(); + let report = abort(&repo, op); + for p in ["docs/d.txt", "docs/e.txt"] { + assert!(report.kept.contains(&p.to_string()), "{op:?}: {report:?}"); + } + assert_eq!( + std::fs::read_dir(&outside).unwrap().count(), + 0, + "{op:?}: nothing was written through the link" + ); + assert!(root.join("docs").symlink_metadata().unwrap().is_symlink()); + assert!( + !root.join(laid(op)).exists(), + "{op:?}: the rest still aborts" + ); + std::fs::remove_dir_all(&outside).unwrap(); + finish(&root, repo); + } +} + #[test] fn abort_keeps_an_edit_of_a_clean_file_and_discards_a_conflict_resolution() { for op in OPS { diff --git a/crates/repo/src/worktree.rs b/crates/repo/src/worktree.rs index ad3e77a5..ea7434f9 100644 --- a/crates/repo/src/worktree.rs +++ b/crates/repo/src/worktree.rs @@ -972,12 +972,16 @@ pub(crate) fn under_symlink(root: &Path, rel: &str) -> bool { let mut end = 0; while let Some(i) = rel[end..].find('/') { end += i; - if root - .join(&rel[..end]) - .symlink_metadata() - .is_ok_and(|m| m.is_symlink()) - { - return true; + // Fail closed: a lstat error other than "nothing there" (also when a + // parent is a file) reads as blocked, not as "not a symlink". + match root.join(&rel[..end]).symlink_metadata() { + Ok(m) if !m.is_symlink() => {} + Err(e) + if matches!( + e.kind(), + std::io::ErrorKind::NotFound | std::io::ErrorKind::NotADirectory + ) => {} + _ => return true, } end += 1; } @@ -1268,19 +1272,25 @@ fn materialize_impl( // Before the first write, whatever `old_root` is (an unborn merge or a clone // has none, so the obstruction checks below do not run): a symlink among a // written path's parents would carry the write out of its directory, e.g. - // `link -> .sc` or a directory outside the checkout. Never followed. + // `link -> .sc` or a directory outside the checkout. Never followed. A + // forward operation refuses; a restore (an abort) finishes anyway, so it + // leaves such a path alone and reports it in `kept`. + let mut through_link: BTreeSet = BTreeSet::new(); for path in target.keys().filter(|p| sparse.matches(p)) { if under_symlink(&layout.root, path) { - return Err(Error::InvalidArgument(format!( - "cannot write {path}: a parent directory is a symlink; \ - move it away, then retry" - ))); + if restore_owned.is_none() { + return Err(Error::InvalidArgument(format!( + "cannot write {path}: a parent directory is a symlink; \ + move it away, then retry" + ))); + } + through_link.insert(path.clone()); } } // Set when an interrupted `sparse set`'s marker widened the removal bound; // cleared once this call has finished laying the tree out under `sparse`. let mut prev_marker_used = false; - let mut kept = Vec::new(); + let mut kept: Vec = through_link.iter().cloned().collect(); // Paths the replaced tree tracks (within the removal walk's bound), with // their blob ids. let mut old_tracked: BTreeMap = BTreeMap::new(); @@ -1349,7 +1359,10 @@ fn materialize_impl( // may hold only directories and files this pass is about to delete. // Anything else is the user's, and refusing now (rather than at the // write, after the pass and earlier writes) changes nothing (#247). - for path in target.keys().filter(|p| sparse.matches(p)) { + for path in target + .keys() + .filter(|p| sparse.matches(p) && !through_link.contains(*p)) + { let full = safe_join(&layout.root, path)?; if full.symlink_metadata().is_ok_and(|m| m.is_dir()) && !dir_clearable(&full, path, &|c| removals.contains(c)) @@ -1362,7 +1375,9 @@ fn materialize_impl( } refuse_blocked_parents( &layout.root, - target.keys().filter(|p| sparse.matches(p)), + target + .keys() + .filter(|p| sparse.matches(p) && !through_link.contains(*p)), &|c| removals.contains(c), )?; for p in removals { @@ -1379,7 +1394,7 @@ fn materialize_impl( } let mut skipped = Vec::new(); for (path, (blob_id, mode, perms)) in &target { - if !sparse.matches(path) { + if !sparse.matches(path) || through_link.contains(path) { continue; } let full = safe_join(&layout.root, path)?; diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 563f1d35..d38ecad5 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -799,7 +799,9 @@ protected paths, plaintext. laid. An edit, a replacement or a symlink survives (#255, #256, #259). - `merge --abort`, `cherry-pick --abort` and `rebase --abort` finish past a kept resolution edit instead of refusing, so it cannot trap the user in the - stop (a directory holding untracked content, or a blocked parent, still refuses): + stop (a directory holding untracked content, or a file of yours where a + directory must be, still refuses; a path with a symlink among its parents is + left alone and reported in `kept`, never written through): they restore the tip, drop the stop's state and return an `AbortReport` whose `kept` paths (changed since the stop) and `skipped` protected paths `sc` prints. Recorded plaintext and sidecars are removed whatever their