diff --git a/CLAUDE.md b/CLAUDE.md index 358a76e9..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 @@ -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..1544cf20 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3996,21 +3996,42 @@ fn run_clone_git(url: &str, dst: &std::path::Path) -> Result<()> { ); } - 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)?; + // An existing destination must be an empty directory (git parity). + let mut claim = scl_repo::sync::claim_clone_dir(dst)?; + let repo = match claim.init_repo() { + Ok(r) => r, + Err(e) => { + // Removes `.sc/` only if `init_repo` created it exclusively. + scl_repo::sync::discard_failed_clone(&claim); + return Err(e.into()); + } + }; + // 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 + // 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(&claim); + drop(repo); + return Err(e); + } + }; println!("cloned {url} into {} (branch {default})", dst.display()); Ok(()) } @@ -5381,6 +5402,80 @@ 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(); + } + + /// 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/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..e5f3779e --- /dev/null +++ b/crates/core/src/pathname.rs @@ -0,0 +1,209 @@ +//! 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). 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), 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(""); + 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; + } + // 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 + && 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 +/// 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~4", + "sc1a2B~1", + "SCABCD~9", + "a/sc0000~3", + ".\u{17f}c", + ".\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", + "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~5", + "sc123~1", + "sc12345~1", + "scxyzw~1", + "sc1234~0", + "sc1234~10", + "\u{17f}x", + "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..", + ] { + 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..3bc8e2fc --- /dev/null +++ b/crates/repo/src/path_guard.rs @@ -0,0 +1,435 @@ +//! 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); + // 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!( + !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_empty_directory_keeps_the_directory() { + let (src, src_root) = source_with_planted_tip("clone-existing-src", true); + let dst = tmp_root("clone-existing-dst"); + let err = Repo::clone_to(&src_root, &dst) + .err() + .expect("clone must fail"); + assert_unsafe(&err); + 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); +} + +#[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); +} + +/// 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() { + // 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 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"); + 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"); + 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()); + assert!(created.join("stray").exists(), "remove_dir only, never all"); + std::fs::remove_dir_all(&parent).unwrap(); + + // An existing empty directory is accepted and kept; only its `.sc/` goes. + let existing = tmp_root("discard-existing"); + 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(); + assert!(!existing.exists()); +} + +#[test] +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 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 = claim.init_repo().err().expect("not ours"); + assert!(matches!(err, Error::RepoExists(_)), "{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 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`. + 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() { + 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] +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"); + 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/repo.rs b/crates/repo/src/repo.rs index 748fbe33..61841091 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -69,13 +69,28 @@ 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. + 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 00b9cbed..dbf13822 100644 --- a/crates/repo/src/sync.rs +++ b/crates/repo/src/sync.rs @@ -17,6 +17,127 @@ use crate::stdio_transport::open_transport; use crate::transport::Transport; 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) 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 ([`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, + }; + // 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(); + 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()), + } + } + 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); + Err(e) + } + } +} + +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/` (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 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")); + } + 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; + } + } + } +} + 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 +194,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`, `dst` may be left with a partially-initialized `.sc/`; the - /// caller should remove it before retrying. + /// 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, @@ -122,123 +244,145 @@ impl Repo { let remote_refs = transport.list_refs()?; let head_branch = transport.head_branch()?; - 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; + let mut claim = claim_clone_dir(dst.as_ref())?; + let dst_repo = match claim.init_repo() { + Ok(r) => r, + Err(e) => { + // Removes `.sc/` only if `init_repo` created it exclusively. + discard_failed_clone(&claim); + return Err(e); } - 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 + // Anything that fails from here leaves a half-built clone: take it + // 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(); + { + 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)?; } - 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); + // 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)?; + } + // 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(()); + } + 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(()) + })(); + 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) } - 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) } /// Backfill: widen a partial clone by fetching every object matching 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/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 ecc926d6..ea7434f9 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,24 +706,45 @@ 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)) } +/// 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 @@ -784,6 +810,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) { @@ -945,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; } @@ -1238,10 +1269,28 @@ 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. 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) { + 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(); @@ -1310,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)) @@ -1323,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 { @@ -1340,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)?; @@ -1654,7 +1708,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/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/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..d38ecad5 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`, @@ -795,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 @@ -813,6 +819,25 @@ 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, 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` 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 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. - **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.