From e07503dc885f183236d3bc3e1635a146eaad9987 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:32:10 +0200 Subject: [PATCH 01/16] fix(gitio): refuse unsafe tree names and never emit symlink modes on export --- crates/gitio/src/export.rs | 194 +++++++++++++++++++++++++++++++++++-- 1 file changed, 184 insertions(+), 10 deletions(-) diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index 7a210f5b..8a76397e 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; use std::path::Path; use anyhow::{Context, Result}; -use scl_core::{EntryKind, FileMode, Object, ObjectId, Snapshot, Store, PROTECTED}; +use scl_core::{EntryKind, Object, ObjectId, Snapshot, Store, PROTECTED}; /// A Git repository we write objects and refs into. pub(crate) struct GitTarget { @@ -27,12 +27,11 @@ pub(crate) struct GitTreeEntry { pub oid: gix::ObjectId, } -/// The four Git entry kinds we emit. +/// The Git entry kinds we emit. sc has no symlinks, so there is no link kind. #[derive(Clone, Copy)] pub(crate) enum GitMode { File, Exec, - Link, Tree, } @@ -49,7 +48,6 @@ impl GitMode { match self { GitMode::File => EntryKind::Blob, GitMode::Exec => EntryKind::BlobExecutable, - GitMode::Link => EntryKind::Link, GitMode::Tree => EntryKind::Tree, } .into() @@ -264,10 +262,12 @@ fn map_tree( map_tree(store, target, e.id, tree_memo, blob_memo, protected_count)?, ), EntryKind::Blob => { - let mode = match e.mode { - m if m == FileMode::EXEC => GitMode::Exec, - FileMode(0o120000) => GitMode::Link, - _ => GitMode::File, + // Only 100644 / 100755: a hostile tree must not smuggle in a symlink (120000) + // or gitlink mode, so the owner exec bit alone decides. + let mode = if e.mode.0 & 0o100 != 0 { + GitMode::Exec + } else { + GitMode::File }; (mode, map_blob(store, target, e.id, blob_memo)?) } @@ -447,6 +447,48 @@ fn count_transcripts( Ok(count) } +/// Code points HFS+ ignores when it compares names, so `.g\u{200c}it` is `.git` on a Mac. +fn hfs_ignorable(c: char) -> bool { + matches!(c, '\u{200c}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{206a}'..='\u{206f}' | '\u{feff}') +} + +/// The names `fsck` treats as the special file `.`: the dotted name, and on NTFS the 8.3 short +/// names `~1` to `~4` and `~1` to `~9`. `hash` is NTFS's fall-back short-name +/// prefix for that file (`gi7eba` for `.gitmodules`). +fn special_names(name: &str, hash: Option<&str>) -> Vec { + let mut v = vec![format!(".{name}")]; + v.extend((1..=4).map(|n| format!("{}~{n}", &name[..6]))); + if let Some(h) = hash { + v.extend((1..=9).map(|n| format!("{h}~{n}"))); + } + v +} + +/// Whether a path component is one of `names` on any filesystem git runs on. +fn is_alias(component: &str, names: &[String]) -> bool { + let c: String = component.chars().filter(|c| !hfs_ignorable(*c)).collect(); + // NTFS ignores trailing dots and spaces and treats `name:stream` as the file `name`. + let c = c.split(':').next().unwrap_or(""); + let c = c.trim_end_matches([' ', '.']).to_ascii_lowercase(); + names.contains(&c) +} + +/// Refuse a tree entry name git's `fsck` would refuse (or that a checkout would resolve into `.git` +/// or `.gitmodules`), so a hostile sc tree cannot become a hostile Git tree. +fn check_entry_name(name: &str) -> anyhow::Result<()> { + let bad = name.is_empty() + || name == "." + || name == ".." + || name.contains(['/', '\\', '\0']) + || is_alias(name, &[".git".into(), "git~1".into()]) + || is_alias(name, &special_names("gitmodules", Some("gi7eba"))); + anyhow::ensure!( + !bad, + "refusing to export: tree entry name {name:?} is not a safe Git path component" + ); + Ok(()) +} + /// Record PROTECTED entry paths under `prefix`, recursing into subtrees. Uses an /// explicit stack so deep trees can't overflow. Deduplicates subtree object ids /// via `seen` so shared subtrees (identical content at multiple paths in a @@ -465,6 +507,7 @@ fn scan_tree( } let tree = store.get_tree(&id)?; for e in &tree.entries { + check_entry_name(&e.name)?; let path = if pre.is_empty() { e.name.clone() } else { @@ -610,6 +653,7 @@ pub fn read_ref(path: &Path, ref_name: &str) -> Result> { #[cfg(test)] mod tests { use super::*; + use scl_core::FileMode; use std::process::Command; fn git(dir: &std::path::Path, args: &[&str]) -> std::process::Output { @@ -678,7 +722,7 @@ mod tests { #[test] fn single_snapshot_exports_files_and_modes() { - use scl_core::{EntryKind, FileMode, Object, Store, StoreConfig, Tree, TreeEntry}; + use scl_core::{EntryKind, Object, Store, StoreConfig, Tree, TreeEntry}; use std::collections::{BTreeMap, HashMap}; let mut store = Store::new(StoreConfig::default()); let blob = store.put(Object::blob(b"fn main(){}".to_vec())).unwrap(); @@ -919,7 +963,7 @@ mod tests { // Helper: a snapshot with one PROTECTED entry and one registry secret. fn encrypted_snapshot(store: &mut scl_core::Store) -> scl_core::ObjectId { - use scl_core::{EntryKind, FileMode, Object, Secret, Snapshot, Tree, TreeEntry, PROTECTED}; + use scl_core::{EntryKind, Object, Secret, Snapshot, Tree, TreeEntry, PROTECTED}; use std::collections::BTreeMap; let ct = store .put(Object::blob(b"ciphertext-bytes".to_vec())) @@ -1445,4 +1489,134 @@ mod tests { std::fs::remove_dir_all(&gsrc).unwrap(); std::fs::remove_dir_all(&dst).unwrap(); } + + fn one_entry_snapshot( + store: &mut scl_core::Store, + name: &str, + mode: FileMode, + nested: bool, + ) -> ObjectId { + use scl_core::{Snapshot, Tree, TreeEntry}; + let blob = store.put(Object::blob(b"x".to_vec())).unwrap(); + let entry = |name: &str, kind, id, mode| TreeEntry { + name: name.into(), + kind, + id, + mode, + perms: 0, + }; + let mut root = store + .put(Object::Tree(Tree::new(vec![entry( + name, + EntryKind::Blob, + blob, + mode, + )]))) + .unwrap(); + if nested { + root = store + .put(Object::Tree(Tree::new(vec![ + entry("a.txt", EntryKind::Blob, blob, FileMode::FILE), + entry("sub", EntryKind::Tree, root, FileMode::FILE), + ]))) + .unwrap(); + } + store + .put(Object::Snapshot(Snapshot { + root, + parents: vec![], + author: "t".into(), + timestamp: 1, + message: "m".into(), + secrets: Default::default(), + protection: Default::default(), + })) + .unwrap() + } + + #[test] + fn check_entry_name_refuses_git_special_and_malformed_names() { + for bad in [ + "", + ".", + "..", + ".git", + ".GIT", + ".git.", + ".git ", + "git~1", + ".g\u{200c}it", + ".git:x", + ".gitmodules", + ".GitModules", + "gitmod~1", + "gi7eba~1", + "a/b", + "a\\b", + "a\0b", + ] { + assert!(check_entry_name(bad).is_err(), "{bad:?} must be refused"); + } + for ok in [ + ".gitignore", + ".gitattributes", + "gitmodules", + "main.rs", + ".github", + "..a", + ] { + assert!(check_entry_name(ok).is_ok(), "{ok:?} must be allowed"); + } + } + + #[test] + fn export_refuses_hostile_name_before_writing_anything() { + for name in [".git", ".gitmodules", "GIT~1"] { + let mut store = scl_core::Store::new(scl_core::StoreConfig::default()); + // Nested, so the walk must reach it past a clean sibling blob. + let snap = one_entry_snapshot(&mut store, name, FileMode::FILE, true); + let dir = std::env::temp_dir().join(format!( + "scl-hostile-{}-{}", + std::process::id(), + name.replace('~', "_") + )); + let _ = std::fs::remove_dir_all(&dir); + let empty = HashMap::new(); + let opts = ExportOptions { + to: &dir, + ref_name: "refs/heads/main", + include_encrypted: false, + known_git_commits: &empty, + }; + let err = export_branch(&mut store, snap, &opts).unwrap_err(); + assert!( + format!("{err:#}").contains("not a safe Git path"), + "{err:#}" + ); + assert!(!dir.exists(), "no git repo may be created on refusal"); + } + } + + #[test] + fn export_never_emits_symlink_or_odd_modes() { + let mut store = scl_core::Store::new(scl_core::StoreConfig::default()); + let snap = one_entry_snapshot(&mut store, "link", FileMode(0o120000), false); + let dir = std::env::temp_dir().join(format!("scl-nolink-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let empty = HashMap::new(); + let opts = ExportOptions { + to: &dir, + ref_name: "refs/heads/main", + include_encrypted: false, + known_git_commits: &empty, + }; + export_branch(&mut store, snap, &opts).unwrap(); + let out = Command::new("git") + .args(["--git-dir", dir.to_str().unwrap(), "ls-tree", "main"]) + .output() + .unwrap(); + let listing = String::from_utf8(out.stdout).unwrap(); + assert!(listing.starts_with("100644 blob"), "{listing}"); + std::fs::remove_dir_all(&dir).unwrap(); + } } From aa89b11df267ca1aed974132a6a06199891f66a9 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:40:21 +0200 Subject: [PATCH 02/16] fix(repo): write serve-tokens.toml owner-only --- crates/core/src/fsutil.rs | 38 ++++++++++++++++++++++++++++++++- crates/repo/src/serve_tokens.rs | 27 ++++++++++++++++++++++- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/crates/core/src/fsutil.rs b/crates/core/src/fsutil.rs index 5ea28150..ac2c8e71 100644 --- a/crates/core/src/fsutil.rs +++ b/crates/core/src/fsutil.rs @@ -29,6 +29,17 @@ static TEMP_COUNTER: AtomicU64 = AtomicU64::new(0); /// directory so the rename itself is durable. The parent directory must /// exist. pub fn atomic_write_durable(path: &Path, bytes: &[u8]) -> std::io::Result<()> { + write_durable(path, bytes, false) +} + +/// Like [`atomic_write_durable`], but the staging file (and so the final file, even when it replaces +/// a wider one) is readable by its owner only from the first byte. For files holding credential +/// material, such as `.sc/serve-tokens.toml`. +pub fn atomic_write_durable_owner_only(path: &Path, bytes: &[u8]) -> std::io::Result<()> { + write_durable(path, bytes, true) +} + +fn write_durable(path: &Path, bytes: &[u8], owner_only: bool) -> std::io::Result<()> { let parent = path.parent().ok_or_else(|| { std::io::Error::new( std::io::ErrorKind::InvalidInput, @@ -38,7 +49,11 @@ pub fn atomic_write_durable(path: &Path, bytes: &[u8]) -> std::io::Result<()> { let n = TEMP_COUNTER.fetch_add(1, Ordering::Relaxed); let tmp = path.with_extension(format!("{}.{n}.tmp", std::process::id())); { - let mut f = std::fs::File::create(&tmp)?; + let mut f = if owner_only { + crate::owner_only::create_new_owner_only_or_warn(&tmp)? + } else { + std::fs::File::create(&tmp)? + }; f.write_all(bytes)?; f.sync_all()?; } @@ -96,6 +111,27 @@ mod tests { std::fs::remove_dir_all(&dir).unwrap(); } + #[cfg(unix)] + #[test] + fn owner_only_write_is_0600_on_create_and_on_rewrite_of_a_wider_file() { + use std::os::unix::fs::PermissionsExt; + let dir = std::env::temp_dir().join(format!("scl-fsutil-oo-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + std::fs::create_dir_all(&dir).unwrap(); + let path = dir.join("tokens"); + let mode = |p: &Path| std::fs::metadata(p).unwrap().permissions().mode() & 0o777; + + atomic_write_durable_owner_only(&path, b"one").unwrap(); + assert_eq!(mode(&path), 0o600); + // A pre-existing world-readable file (written by an older build) is replaced by a 0600 one. + std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o644)).unwrap(); + atomic_write_durable_owner_only(&path, b"two").unwrap(); + assert_eq!(std::fs::read(&path).unwrap(), b"two"); + assert_eq!(mode(&path), 0o600); + + std::fs::remove_dir_all(&dir).unwrap(); + } + #[test] fn missing_parent_directory_is_an_error_not_a_panic() { let dir = std::env::temp_dir().join(format!("scl-fsutil-miss-{}", std::process::id())); diff --git a/crates/repo/src/serve_tokens.rs b/crates/repo/src/serve_tokens.rs index 4667b1e6..e861efbe 100644 --- a/crates/repo/src/serve_tokens.rs +++ b/crates/repo/src/serve_tokens.rs @@ -76,7 +76,10 @@ fn save(layout: &Layout, tokens: &[TokenEntry]) -> Result<()> { tokens: tokens.to_vec(), }; let text = toml::to_string(&file).map_err(|e| Error::BadConfig(e.to_string()))?; - scl_core::fsutil::atomic_write_durable(&layout.serve_tokens_path(), text.as_bytes())?; + scl_core::fsutil::atomic_write_durable_owner_only( + &layout.serve_tokens_path(), + text.as_bytes(), + )?; Ok(()) } @@ -307,6 +310,28 @@ mod tests { std::fs::remove_dir_all(&layout.root).unwrap(); } + #[cfg(unix)] + #[test] + fn token_file_is_owner_only_after_add_rotate_and_remove() { + use std::os::unix::fs::PermissionsExt; + let layout = tmp_layout("mode"); + let mode = || { + std::fs::metadata(layout.serve_tokens_path()) + .unwrap() + .permissions() + .mode() + & 0o777 + }; + add(&layout, "ci", Scope::Ro).unwrap(); + add(&layout, "ci2", Scope::Rw).unwrap(); + assert_eq!(mode(), 0o600); + rotate(&layout, "ci", now()).unwrap(); + assert_eq!(mode(), 0o600); + remove(&layout, "ci2").unwrap(); + assert_eq!(mode(), 0o600); + std::fs::remove_dir_all(&layout.root).unwrap(); + } + #[test] fn verify_matches_correct_token_and_returns_scope() { let layout = tmp_layout("verify"); From 370fa562eee13f48f46720b273b9a4b6d20d3f69 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:40:22 +0200 Subject: [PATCH 03/16] fix: escape terminal controls in remote and commit text before printing --- crates/cli/src/main.rs | 60 ++++++++++++++++++++++++++++---------- crates/core/src/display.rs | 48 ++++++++++++++++++++++++++++++ crates/core/src/lib.rs | 2 ++ crates/repo/src/error.rs | 35 ++++++++++++++++++---- 4 files changed, 124 insertions(+), 21 deletions(-) create mode 100644 crates/core/src/display.rs diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 48b26201..3f27dfc7 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2700,19 +2700,7 @@ fn run_log(json: bool, identity: Option) -> Result<()> { return Ok(()); } for ((id, snap), status) in entries.iter().zip(&statuses) { - let merge = if snap.parents.len() > 1 { - " (merge)" - } else { - "" - }; - print_line(&format!( - "{} {} {} — {}{}", - id.short(), - fmt_utc(snap.timestamp), - snap.author, - snap.message, - merge - )); + print_line(&render_log_line(id, snap)); if let Some(line) = render_sig_status_line(status) { print_line(&line); } @@ -2732,6 +2720,23 @@ fn run_log(json: bool, identity: Option) -> Result<()> { /// themselves, but that needs `libc`; catching `BrokenPipe` at the point of /// writing gets the same externally-visible result (clean exit, no panic) /// without a new dependency. +fn render_log_line(id: &scl_core::ObjectId, snap: &scl_core::Snapshot) -> String { + let merge = if snap.parents.len() > 1 { + " (merge)" + } else { + "" + }; + // Author and message are authored by whoever made the commit, not by this user: no raw escapes. + format!( + "{} {} {} — {}{}", + id.short(), + fmt_utc(snap.timestamp), + scl_core::sanitize_display(&snap.author), + scl_core::sanitize_display(&snap.message), + merge + ) +} + fn print_line(line: &str) { use std::io::Write; if let Err(e) = writeln!(std::io::stdout(), "{line}") { @@ -3145,9 +3150,16 @@ fn run_transcript(op: TranscriptOp) -> Result<()> { return Ok(()); } for (tid, t) in &transcripts { - println!("--- {} (agent: {}) ---", tid.short(), t.agent); + println!( + "--- {} (agent: {}) ---", + tid.short(), + scl_core::sanitize_display(&t.agent) + ); let body = repo.open_transcript(tid, &sk)?; - print!("{}", String::from_utf8_lossy(&body)); + print!( + "{}", + scl_core::sanitize_display(&String::from_utf8_lossy(&body)) + ); println!(); } Ok(()) @@ -3194,7 +3206,7 @@ fn run_transcript(op: TranscriptOp) -> Result<()> { "{} {} agent={} {}", snap.short(), tid.short(), - agent, + scl_core::sanitize_display(agent), transcript_sig_label(status) )); } @@ -4879,6 +4891,22 @@ mod tests { use super::*; use std::time::Duration; + #[test] + fn log_line_escapes_terminal_controls_from_author_and_message() { + let snap = scl_core::Snapshot { + root: scl_core::ObjectId::of(b"r"), + parents: vec![], + author: "evil\x1b]0;x\x07".into(), + timestamp: 0, + message: "a\x1b[2J\u{9b}b\nsecond line".into(), + secrets: Default::default(), + protection: Default::default(), + }; + let line = render_log_line(&scl_core::ObjectId::of(b"s"), &snap); + assert!(line.contains("evil\\x1b]0;x\\x07") && line.contains("a\\x1b[2J\\u{9b}b\nsecond")); + assert!(!line.chars().any(|c| c.is_control() && c != '\n')); + } + #[test] fn default_budget_warning_logic() { assert!(runs_default_budget(false, None)); diff --git a/crates/core/src/display.rs b/crates/core/src/display.rs new file mode 100644 index 00000000..727de7c3 --- /dev/null +++ b/crates/core/src/display.rs @@ -0,0 +1,48 @@ +//! Making untrusted text safe to print to a terminal. + +use std::borrow::Cow; + +/// Replace control characters in text that came from a peer or from stored objects (a server's error +/// string, a commit author or message) with a visible escape, so it cannot drive the terminal with +/// ANSI/OSC sequences. `\n` and `\t` pass through. C0 controls and DEL print as `\x1b`, C1 controls +/// as `\u{85}`. Format-time only: stored data is never changed. +pub fn sanitize_display(s: &str) -> Cow<'_, str> { + let bad = |c: char| c.is_control() && c != '\n' && c != '\t'; + if !s.chars().any(bad) { + return Cow::Borrowed(s); + } + let mut out = String::with_capacity(s.len() + 8); + for c in s.chars() { + if !bad(c) { + out.push(c); + } else if (c as u32) < 0x80 { + out.push_str(&format!("\\x{:02x}", c as u32)); + } else { + out.push_str(&format!("\\u{{{:x}}}", c as u32)); + } + } + Cow::Owned(out) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn escapes_ansi_osc_c1_and_del_but_keeps_newline_and_tab() { + let s = "a\x1b[2Jb\x1b]0;pwn\x07c\u{9b}31m\x7fd\re\nf\tg"; + let out = sanitize_display(s); + assert_eq!( + out, + "a\\x1b[2Jb\\x1b]0;pwn\\x07c\\u{9b}31m\\x7fd\\x0de\nf\tg" + ); + assert!(!out + .chars() + .any(|c| c.is_control() && c != '\n' && c != '\t')); + } + + #[test] + fn clean_text_is_untouched_and_unallocated() { + assert!(matches!(sanitize_display("héllo — ok\n"), Cow::Borrowed(_))); + } +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index ae2bae50..2f4d58cd 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -4,6 +4,7 @@ //! BLAKE3-addressed objects (blobs, trees, snapshots, secrets) and a [`Store`] //! that bounds resident blob memory with LRU eviction and optional spill. +pub mod display; pub mod error; pub mod fsutil; pub mod id; @@ -171,6 +172,7 @@ pub fn read_exact_vec(r: &mut impl std::io::Read, len: usize) -> std::io::Result Ok(buf) } +pub use display::sanitize_display; pub use error::{Error, Result}; pub use id::ObjectId; pub use object::{ diff --git a/crates/repo/src/error.rs b/crates/repo/src/error.rs index a5c82a7c..edd68d55 100644 --- a/crates/repo/src/error.rs +++ b/crates/repo/src/error.rs @@ -127,11 +127,11 @@ pub enum Error { RemoteExists(String), #[error("no common ancestor between the branches")] NoCommonAncestor, - #[error("wire protocol error: {0}")] + #[error("wire protocol error: {}", scl_core::sanitize_display(.0))] Protocol(String), - #[error("connection to remote lost: {0}")] + #[error("connection to remote lost: {}", scl_core::sanitize_display(.0))] ConnectionLost(String), - #[error("remote error: {0}")] + #[error("remote error: {}", scl_core::sanitize_display(.0))] Remote(String), #[error("TLS: {0}")] Tls(#[from] scl_tlsio::Error), @@ -184,13 +184,13 @@ pub enum Error { /// The server aborted an incoming pack mid-stream because it exceeded the /// operator's `--max-pack-size` cap (P31). Payload is the server's own /// human-readable limit text, carried verbatim across the wire. - #[error("pack exceeds the server's --max-pack-size limit: {0}")] + #[error("pack exceeds the server's --max-pack-size limit: {}", scl_core::sanitize_display(.0))] PackTooLarge(String), /// The server closed the connection because the bytes it wrote for this /// peer (`PutObject` + `PutPack`) exceeded the operator's /// `--max-conn-bytes` budget (#233). Payload is the server's own /// human-readable limit text, carried verbatim across the wire. - #[error("connection write budget exceeded: {0}")] + #[error("connection write budget exceeded: {}", scl_core::sanitize_display(.0))] ConnectionBudgetExceeded(String), /// The server refused the connection at accept time because /// `--max-connections` was reached (P31). Retryable. @@ -226,3 +226,28 @@ fn repair_command(prefix: &str) -> String { format!("`sc backfill {prefix}`") } } + +#[cfg(test)] +mod tests { + use super::Error; + + #[test] + fn remote_text_is_escaped_when_displayed_but_kept_in_the_value() { + let raw = "boom\x1b]0;pwn\x07\x1b[2J"; + for e in [ + Error::Remote(raw.into()), + Error::Protocol(raw.into()), + Error::ConnectionLost(raw.into()), + Error::PackTooLarge(raw.into()), + Error::ConnectionBudgetExceeded(raw.into()), + ] { + let shown = e.to_string(); + assert!( + !shown.contains('\x1b') && !shown.contains('\x07'), + "{shown:?}" + ); + assert!(shown.contains("boom\\x1b]0;pwn\\x07\\x1b[2J"), "{shown:?}"); + } + assert!(matches!(Error::Remote(raw.into()), Error::Remote(m) if m == raw)); + } +} From c4106b7d11a907380e5331886d9953ea1f19c05e Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:41:05 +0200 Subject: [PATCH 04/16] fix(crypto): redact DEKs from Debug of IndexEntry and BranchIndex --- crates/crypto/src/private.rs | 48 ++++++++++++++++++++++++++++++++++-- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/crates/crypto/src/private.rs b/crates/crypto/src/private.rs index fa74fe3a..6a5089a2 100644 --- a/crates/crypto/src/private.rs +++ b/crates/crypto/src/private.rs @@ -108,12 +108,22 @@ pub fn open_object(payload: &[u8], dek: &[u8; KEY_LEN]) -> Result) -> std::fmt::Result { + f.debug_struct("IndexEntry") + .field("sealed", &self.sealed) + .field("dek", &"") + .finish() + } +} + /// The decrypted branch index: the inner (plaintext-id) tip snapshot plus the /// `inner id -> (sealed id, DEK)` map for every object sealed by this branch. /// An inner id *absent* from the map is a public object, read directly from @@ -122,12 +132,25 @@ pub struct IndexEntry { /// Inner ids are BLAKE3 hashes of plaintext encodings, so the index is exactly /// as sensitive as the DEKs it carries: it exists in plaintext only inside an /// authorized process and is zeroized on drop (best-effort). -#[derive(Clone, PartialEq, Eq, Debug, Default)] +#[derive(Clone, PartialEq, Eq, Default)] pub struct BranchIndex { pub inner_tip: Option, pub entries: BTreeMap, } +/// Prints the tip and the entry count only; the entries carry DEKs. +impl std::fmt::Debug for BranchIndex { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("BranchIndex") + .field("inner_tip", &self.inner_tip) + .field( + "entries", + &format_args!("<{} redacted>", self.entries.len()), + ) + .finish() + } +} + impl Drop for BranchIndex { fn drop(&mut self) { for e in self.entries.values_mut() { @@ -263,6 +286,27 @@ mod tests { ObjectId::from_bytes([b; 32]) } + #[test] + fn debug_output_never_contains_dek_bytes() { + let dek = [0xab; KEY_LEN]; + let entry = IndexEntry { + sealed: oid(1), + dek, + }; + let mut idx = BranchIndex::default(); + idx.entries.insert(oid(2), entry.clone()); + let (hex, dec) = (hex::encode(dek), format!("{:?}", dek.to_vec())); + for shown in [ + format!("{entry:?}"), + format!("{entry:#?}"), + format!("{idx:?}"), + format!("{idx:#?}"), + ] { + assert!(!shown.contains(&hex) && !shown.contains(&dec), "{shown}"); + assert!(shown.contains("redacted"), "{shown}"); + } + } + #[test] fn seal_open_object_roundtrip_is_randomized() { let encoding = b"tag+payload of some inner object"; From 28a98f5058117407a2b5346f5dd72234626349be Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:42:22 +0200 Subject: [PATCH 05/16] feat(cli): warn when an identity file is readable by group or others --- crates/cli/src/main.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 3f27dfc7..a6cf7a5c 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -3741,8 +3741,38 @@ fn run_ws(op: WsOp) -> Result<()> { /// is this function with `.enc` projected out — same file format handling, /// same errors, just a narrower return type so none of those call sites had /// to change shape. +/// Whether a unix file mode lets group or others read or write the file (ssh's test for a private key). +#[cfg(unix)] +fn mode_open_to_group_or_others(mode: u32) -> bool { + mode & 0o077 != 0 +} + +/// Warn once on stderr when the identity file is accessible to group or others, as ssh does for keys. +/// A warning only: an existing setup keeps working. +#[cfg(unix)] +fn warn_if_identity_too_open(path: &std::path::Path) { + use std::os::unix::fs::PermissionsExt; + static WARNED: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); + let Ok(meta) = std::fs::metadata(path) else { + return; + }; + let mode = meta.permissions().mode() & 0o777; + if mode_open_to_group_or_others(mode) + && !WARNED.swap(true, std::sync::atomic::Ordering::Relaxed) + { + eprintln!( + "warning: identity file {} is accessible to other users (mode {mode:04o}); \ + run `chmod 600 {}`", + path.display(), + path.display() + ); + } +} + fn load_identity_full(flag: Option) -> Result { let path = resolve_identity_path(flag); + #[cfg(unix)] + warn_if_identity_too_open(&path); let contents = scl_crypto::Zeroizing::new( std::fs::read_to_string(&path) .with_context(|| format!("reading identity {}", path.display()))?, @@ -4891,6 +4921,17 @@ mod tests { use super::*; use std::time::Duration; + #[cfg(unix)] + #[test] + fn identity_mode_predicate_flags_group_and_other_access() { + for open in [0o644, 0o640, 0o604, 0o660, 0o666, 0o700 | 0o001, 0o620] { + assert!(mode_open_to_group_or_others(open), "{open:o}"); + } + for private in [0o600, 0o400, 0o700, 0o500] { + assert!(!mode_open_to_group_or_others(private), "{private:o}"); + } + } + #[test] fn log_line_escapes_terminal_controls_from_author_and_message() { let snap = scl_core::Snapshot { From def9346392abb1588cac4a4898cb013e9bac5307 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 09:42:56 +0200 Subject: [PATCH 06/16] docs(threat-model): record hygiene fixes and the sealed-blob placement decision --- docs/THREAT-MODEL.md | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index d3d0ab68..f5f5b5a9 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -133,7 +133,15 @@ too, not just the src-control-side metadata. network mounts; Windows FAT, exFAT, some SMB shares) is not detected. For an identity file or TLS serve key the fallback is not small: it leaves the unencrypted private key under inherited permissions, so other local users may - be able to read it. + be able to read it. On unix, `sc` warns once on stderr (it does not refuse) + when an identity file passed with `--identity` is accessible to group or others + (`mode & 0o077`), as ssh does for keys. +- **Sealed blobs bind no path or position (decision):** sealed protected blobs + and private-branch objects use constant associated data, so a writer can move + a sealed blob to another path or roll a path back to an older sealed version. + Accepted: binding the path would force a reseal on every rename and break + carry-by-id. Integrity of placement comes from signed commits + (`sc verify --require`). ## Private branches (`sc branch --private` / `sc branch publish`) — ADR-0044 @@ -463,6 +471,15 @@ too, not just the src-control-side metadata. not authentication: any peer that speaks version 6 and passes the access gates is served. An `ssh://` remote runs the remote host's own `sc`, so that binary must match too. +- **`.sc/serve-tokens.toml` is owner-only.** Created and rewritten (add, rotate, + remove) through a `0600` staging file on unix (protected DACL on Windows), so a + default umask does not expose the token hashes and scopes. +- **Remote-controlled text is escaped before printing.** A server's error string + and a commit's author and message may carry ANSI or OSC escapes, so the CLI + prints C0 controls (other than newline and tab), DEL and C1 controls as visible + escapes (`\x1b`) in those errors, in `sc log` and in `sc transcript` + output. Stored data and `--json` output are unchanged (JSON escapes controls). + Paths are not escaped. ## Bucket remotes (`sc+wal://` / `sc+s3://`) — ADR-0046/0057 @@ -876,6 +893,12 @@ protected paths, plaintext. constants, and the spools bound disk, not RAM. - The ceiling (1 GiB) is compile-time, so no configuration can make a length claim unbounded. +- **Git export refuses unsafe tree names.** `sc export` and a push to a git + remote check every entry name in the exported history before creating the + target or writing any object: `.git`, `.gitmodules` and their HFS/NTFS aliases, + `.`, `..`, empty names and names containing `/`, `\` or NUL are refused. Every + blob is written as `100644` or `100755` by its exec bit; sc has no symlinks, so a + hostile tree cannot produce a Git symlink or gitlink entry. ## sc-forge: authentication, tenancy, transport, resources — P38 F1/F2 From e4b0d3d0e15608fae81fca2919eeebc548c33605 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:11:18 +0200 Subject: [PATCH 07/16] fix(cli): escape newlines and bidi controls in one-line output --- crates/cli/src/main.rs | 16 ++++++++++------ crates/core/src/display.rs | 33 ++++++++++++++++++++++++++++++--- crates/core/src/lib.rs | 2 +- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index a6cf7a5c..d6b0f3d6 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -2731,8 +2731,8 @@ fn render_log_line(id: &scl_core::ObjectId, snap: &scl_core::Snapshot) -> String "{} {} {} — {}{}", id.short(), fmt_utc(snap.timestamp), - scl_core::sanitize_display(&snap.author), - scl_core::sanitize_display(&snap.message), + scl_core::sanitize_display_line(&snap.author), + scl_core::sanitize_display_line(&snap.message), merge ) } @@ -3153,7 +3153,7 @@ fn run_transcript(op: TranscriptOp) -> Result<()> { println!( "--- {} (agent: {}) ---", tid.short(), - scl_core::sanitize_display(&t.agent) + scl_core::sanitize_display_line(&t.agent) ); let body = repo.open_transcript(tid, &sk)?; print!( @@ -3206,7 +3206,7 @@ fn run_transcript(op: TranscriptOp) -> Result<()> { "{} {} agent={} {}", snap.short(), tid.short(), - scl_core::sanitize_display(agent), + scl_core::sanitize_display_line(agent), transcript_sig_label(status) )); } @@ -4944,8 +4944,12 @@ mod tests { protection: Default::default(), }; let line = render_log_line(&scl_core::ObjectId::of(b"s"), &snap); - assert!(line.contains("evil\\x1b]0;x\\x07") && line.contains("a\\x1b[2J\\u{9b}b\nsecond")); - assert!(!line.chars().any(|c| c.is_control() && c != '\n')); + assert!( + line.contains("evil\\x1b]0;x\\x07") && line.contains("a\\x1b[2J\\u{9b}b\\x0asecond"), + "{line}" + ); + // One line: a message cannot start a line that looks like a signature status. + assert!(!line.chars().any(|c| c.is_control())); } #[test] diff --git a/crates/core/src/display.rs b/crates/core/src/display.rs index 727de7c3..4c743d9c 100644 --- a/crates/core/src/display.rs +++ b/crates/core/src/display.rs @@ -4,10 +4,29 @@ use std::borrow::Cow; /// Replace control characters in text that came from a peer or from stored objects (a server's error /// string, a commit author or message) with a visible escape, so it cannot drive the terminal with -/// ANSI/OSC sequences. `\n` and `\t` pass through. C0 controls and DEL print as `\x1b`, C1 controls -/// as `\u{85}`. Format-time only: stored data is never changed. +/// ANSI/OSC sequences or reorder what is shown with bidi controls. `\n` and `\t` pass through. C0 +/// controls and DEL print as `\x1b`, C1, bidi and other invisible format characters as `\u{202e}`. +/// Format-time only: stored data is never changed. pub fn sanitize_display(s: &str) -> Cow<'_, str> { - let bad = |c: char| c.is_control() && c != '\n' && c != '\t'; + escape(s, false) +} + +/// [`sanitize_display`] for text printed inside one line (a `sc log` row, a list row): `\n`, `\r` +/// and `\t` are escaped too, so a commit message cannot start a line that looks like `sc`'s own +/// output, such as a forged signature status. +pub fn sanitize_display_line(s: &str) -> Cow<'_, str> { + escape(s, true) +} + +/// Invisible characters that change how the surrounding text is laid out or read: bidi controls, +/// zero-width characters and the byte-order mark. +fn invisible_format(c: char) -> bool { + matches!(c, '\u{061c}' | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2060}'..='\u{2069}' | '\u{feff}') +} + +fn escape(s: &str, one_line: bool) -> Cow<'_, str> { + let bad = + |c: char| (c.is_control() && (one_line || (c != '\n' && c != '\t'))) || invisible_format(c); if !s.chars().any(bad) { return Cow::Borrowed(s); } @@ -41,6 +60,14 @@ mod tests { .any(|c| c.is_control() && c != '\n' && c != '\t')); } + #[test] + fn one_line_form_escapes_newlines_and_bidi_controls() { + let s = "msg\n signature: good\r\tx\u{202e}y"; + let out = sanitize_display_line(s); + assert_eq!(out, "msg\\x0a signature: good\\x0d\\x09x\\u{202e}y"); + assert_eq!(sanitize_display("a\u{2066}b"), "a\\u{2066}b"); + } + #[test] fn clean_text_is_untouched_and_unallocated() { assert!(matches!(sanitize_display("héllo — ok\n"), Cow::Borrowed(_))); diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 2f4d58cd..9267e57e 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -172,7 +172,7 @@ pub fn read_exact_vec(r: &mut impl std::io::Read, len: usize) -> std::io::Result Ok(buf) } -pub use display::sanitize_display; +pub use display::{sanitize_display, sanitize_display_line}; pub use error::{Error, Result}; pub use id::ObjectId; pub use object::{ From 4c0af82e3ddaa6b7e2f6a0a6af35d2353a3e9a24 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:20:10 +0200 Subject: [PATCH 08/16] fix(gitio): use gix's path validation, keep symlink modes, check only snapshots an export writes --- crates/gitio/src/export.rs | 263 ++++++++++++++++++++++++++++++------- crates/gitio/src/lib.rs | 4 +- 2 files changed, 218 insertions(+), 49 deletions(-) diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index 8a76397e..00172d77 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -9,7 +9,7 @@ use std::collections::HashMap; use std::path::Path; use anyhow::{Context, Result}; -use scl_core::{EntryKind, Object, ObjectId, Snapshot, Store, PROTECTED}; +use scl_core::{EntryKind, FileMode, Object, ObjectId, Snapshot, Store, PROTECTED}; /// A Git repository we write objects and refs into. pub(crate) struct GitTarget { @@ -27,11 +27,12 @@ pub(crate) struct GitTreeEntry { pub oid: gix::ObjectId, } -/// The Git entry kinds we emit. sc has no symlinks, so there is no link kind. +/// The four Git entry kinds we emit. #[derive(Clone, Copy)] pub(crate) enum GitMode { File, Exec, + Link, Tree, } @@ -48,6 +49,7 @@ impl GitMode { match self { GitMode::File => EntryKind::Blob, GitMode::Exec => EntryKind::BlobExecutable, + GitMode::Link => EntryKind::Link, GitMode::Tree => EntryKind::Tree, } .into() @@ -262,12 +264,10 @@ fn map_tree( map_tree(store, target, e.id, tree_memo, blob_memo, protected_count)?, ), EntryKind::Blob => { - // Only 100644 / 100755: a hostile tree must not smuggle in a symlink (120000) - // or gitlink mode, so the owner exec bit alone decides. - let mode = if e.mode.0 & 0o100 != 0 { - GitMode::Exec - } else { - GitMode::File + let mode = match e.mode { + m if m == FileMode::EXEC => GitMode::Exec, + FileMode(0o120000) => GitMode::Link, + _ => GitMode::File, }; (mode, map_blob(store, target, e.id, blob_memo)?) } @@ -447,41 +447,21 @@ fn count_transcripts( Ok(count) } -/// Code points HFS+ ignores when it compares names, so `.g\u{200c}it` is `.git` on a Mac. -fn hfs_ignorable(c: char) -> bool { - matches!(c, '\u{200c}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{206a}'..='\u{206f}' | '\u{feff}') -} - -/// The names `fsck` treats as the special file `.`: the dotted name, and on NTFS the 8.3 short -/// names `~1` to `~4` and `~1` to `~9`. `hash` is NTFS's fall-back short-name -/// prefix for that file (`gi7eba` for `.gitmodules`). -fn special_names(name: &str, hash: Option<&str>) -> Vec { - let mut v = vec![format!(".{name}")]; - v.extend((1..=4).map(|n| format!("{}~{n}", &name[..6]))); - if let Some(h) = hash { - v.extend((1..=9).map(|n| format!("{h}~{n}"))); - } - v -} - -/// Whether a path component is one of `names` on any filesystem git runs on. -fn is_alias(component: &str, names: &[String]) -> bool { - let c: String = component.chars().filter(|c| !hfs_ignorable(*c)).collect(); - // NTFS ignores trailing dots and spaces and treats `name:stream` as the file `name`. - let c = c.split(':').next().unwrap_or(""); - let c = c.trim_end_matches([' ', '.']).to_ascii_lowercase(); - names.contains(&c) -} - /// Refuse a tree entry name git's `fsck` would refuse (or that a checkout would resolve into `.git` -/// or `.gitmodules`), so a hostile sc tree cannot become a hostile Git tree. +/// or `.gitmodules`), so a hostile sc tree cannot become a hostile Git tree. The `.git`, HFS and NTFS +/// alias rules are git's own, via `gix::validate`; the Windows-only rules (reserved names, `:`, `?`) +/// are left off because they are legal on Unix, which a repository may target. `.gitmodules` is +/// refused for any entry kind (the validator only does so for symlinks, so it is told it is one). fn check_entry_name(name: &str) -> anyhow::Result<()> { - let bad = name.is_empty() - || name == "." - || name == ".." - || name.contains(['/', '\\', '\0']) - || is_alias(name, &[".git".into(), "git~1".into()]) - || is_alias(name, &special_names("gitmodules", Some("gi7eba"))); + use gix::validate::path::component::{Mode, Options}; + let opts = Options { + protect_windows: false, + protect_hfs: true, + protect_ntfs: true, + }; + let bad = name.contains(['\\', '\0']) + || gix::validate::path::component(name.as_bytes().into(), Some(Mode::Symlink), opts) + .is_err(); anyhow::ensure!( !bad, "refusing to export: tree entry name {name:?} is not a safe Git path component" @@ -489,6 +469,76 @@ fn check_entry_name(name: &str) -> anyhow::Result<()> { Ok(()) } +/// Check the entry names of every tree of every snapshot that an export would WRITE: the DAG from +/// `tip`, not descending into `stop` (commits the destination already holds, whose trees are never +/// rewritten). Read-only, so a refusal leaves the destination untouched. +fn check_pending_names( + store: &mut Store, + tip: ObjectId, + stop: &std::collections::HashSet, +) -> anyhow::Result<()> { + let mut seen_snaps = std::collections::HashSet::new(); + let mut seen_trees = std::collections::HashSet::new(); + let mut snaps = vec![tip]; + while let Some(sid) = snaps.pop() { + if stop.contains(&sid) || !seen_snaps.insert(sid) { + continue; + } + let snap = store.get_snapshot(&sid)?; + let mut trees = vec![snap.root]; + while let Some(id) = trees.pop() { + if !seen_trees.insert(id) { + continue; + } + for e in &store.get_tree(&id)?.entries { + check_entry_name(&e.name)?; + if let EntryKind::Tree = e.kind { + trees.push(e.id); + } + } + } + snaps.extend(snap.parents.iter().copied()); + } + Ok(()) +} + +/// For a network push: refuse unsafe names in the commits the push would upload, that is the history +/// of `tip` minus everything the server already has (`server_tip` and its ancestors; `None` means the +/// whole history). This covers commits the local mirror already holds (exported by an earlier or +/// older client, so skipped by [`export_branch`]) but the server does not. +pub fn check_names_pending_upload( + store: &mut Store, + tip: ObjectId, + server_tip: Option, +) -> anyhow::Result<()> { + let mut on_server = std::collections::HashSet::new(); + let mut stack: Vec = server_tip.into_iter().collect(); + while let Some(sid) = stack.pop() { + if on_server.insert(sid) { + stack.extend(store.get_snapshot(&sid)?.parents.iter().copied()); + } + } + check_pending_names(store, tip, &on_server) +} + +/// Marked commits that still exist in the Git repo at `path` (so a later export reuses them). Reads +/// only, and creates nothing: a missing repo has none. +fn reusable_commits( + path: &Path, + known: &HashMap, +) -> std::collections::HashSet { + let Ok(repo) = gix::open(path) else { + return Default::default(); + }; + known + .iter() + .filter(|(_, hex)| { + gix::ObjectId::from_hex(hex.as_bytes()).is_ok_and(|g| repo.has_object(g)) + }) + .map(|(id, _)| *id) + .collect() +} + /// Record PROTECTED entry paths under `prefix`, recursing into subtrees. Uses an /// explicit stack so deep trees can't overflow. Deduplicates subtree object ids /// via `seen` so shared subtrees (identical content at multiple paths in a @@ -507,7 +557,6 @@ fn scan_tree( } let tree = store.get_tree(&id)?; for e in &tree.entries { - check_entry_name(&e.name)?; let path = if pre.is_empty() { e.name.clone() } else { @@ -545,6 +594,13 @@ pub fn export_branch( ); } let secrets_dropped = secret_names.len(); + // Unsafe names fail closed too, but only in snapshots this export will write: history the + // destination already holds (reused marks) is not ours to judge. + check_pending_names( + store, + tip, + &reusable_commits(opts.to, opts.known_git_commits), + )?; // Signatures have no fail-closed gate (unlike protected content/secrets, // a dropped signature is a provenance-completeness loss, not a // confidentiality one) — always counted, exported or not. @@ -653,7 +709,6 @@ pub fn read_ref(path: &Path, ref_name: &str) -> Result> { #[cfg(test)] mod tests { use super::*; - use scl_core::FileMode; use std::process::Command; fn git(dir: &std::path::Path, args: &[&str]) -> std::process::Output { @@ -722,7 +777,7 @@ mod tests { #[test] fn single_snapshot_exports_files_and_modes() { - use scl_core::{EntryKind, Object, Store, StoreConfig, Tree, TreeEntry}; + use scl_core::{EntryKind, FileMode, Object, Store, StoreConfig, Tree, TreeEntry}; use std::collections::{BTreeMap, HashMap}; let mut store = Store::new(StoreConfig::default()); let blob = store.put(Object::blob(b"fn main(){}".to_vec())).unwrap(); @@ -963,7 +1018,7 @@ mod tests { // Helper: a snapshot with one PROTECTED entry and one registry secret. fn encrypted_snapshot(store: &mut scl_core::Store) -> scl_core::ObjectId { - use scl_core::{EntryKind, Object, Secret, Snapshot, Tree, TreeEntry, PROTECTED}; + use scl_core::{EntryKind, FileMode, Object, Secret, Snapshot, Tree, TreeEntry, PROTECTED}; use std::collections::BTreeMap; let ct = store .put(Object::blob(b"ciphertext-bytes".to_vec())) @@ -1551,6 +1606,9 @@ mod tests { ".GitModules", "gitmod~1", "gi7eba~1", + "gi7eba~9", + "gi7eb~10", + "gi7eb~12", "a/b", "a\\b", "a\0b", @@ -1564,6 +1622,10 @@ mod tests { "main.rs", ".github", "..a", + // Legal on Unix, refused only by the Windows rules, which are off. + "a:b", + "what?.txt", + "con", ] { assert!(check_entry_name(ok).is_ok(), "{ok:?} must be allowed"); } @@ -1598,10 +1660,12 @@ mod tests { } #[test] - fn export_never_emits_symlink_or_odd_modes() { + fn export_keeps_a_recorded_symlink_mode_as_a_git_symlink() { + // Changing this would change re-synthesized tree ids for repos imported from git with + // symlinks, and break the round trip. let mut store = scl_core::Store::new(scl_core::StoreConfig::default()); let snap = one_entry_snapshot(&mut store, "link", FileMode(0o120000), false); - let dir = std::env::temp_dir().join(format!("scl-nolink-{}", std::process::id())); + let dir = std::env::temp_dir().join(format!("scl-link-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); let empty = HashMap::new(); let opts = ExportOptions { @@ -1616,7 +1680,110 @@ mod tests { .output() .unwrap(); let listing = String::from_utf8(out.stdout).unwrap(); - assert!(listing.starts_with("100644 blob"), "{listing}"); + assert!(listing.starts_with("120000 blob"), "{listing}"); std::fs::remove_dir_all(&dir).unwrap(); } + + /// A snapshot whose root holds one blob named `name`, on top of `parent` if given. + fn commit_with( + store: &mut scl_core::Store, + name: &str, + parent: Option, + msg: &str, + ) -> ObjectId { + use scl_core::{Snapshot, Tree, TreeEntry}; + let blob = store.put(Object::blob(msg.as_bytes().to_vec())).unwrap(); + let root = store + .put(Object::Tree(Tree::new(vec![TreeEntry { + name: name.into(), + kind: EntryKind::Blob, + id: blob, + mode: FileMode::FILE, + perms: 0, + }]))) + .unwrap(); + store + .put(Object::Snapshot(Snapshot { + root, + parents: parent.into_iter().collect(), + author: "t".into(), + timestamp: 1, + message: msg.into(), + secrets: Default::default(), + protection: Default::default(), + })) + .unwrap() + } + + #[test] + fn only_snapshots_that_will_be_written_are_name_checked() { + let mut store = scl_core::Store::new(scl_core::StoreConfig::default()); + // `old` (with .gitmodules) is already on the destination; `fixed` removed it. + let old = commit_with(&mut store, ".gitmodules", None, "old"); + let fixed = commit_with(&mut store, "a.txt", Some(old), "fixed"); + let bad_new = commit_with(&mut store, ".gitmodules", Some(fixed), "readded"); + + // Put `old` on the destination as a real Git commit, as an earlier push would have. + let dir = std::env::temp_dir().join(format!("scl-pending-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&dir); + let target = GitTarget::open_or_init_bare(&dir).unwrap(); + let snap = store.get_snapshot(&old).unwrap(); + let tree = map_tree( + &mut store, + &target, + snap.root, + &mut HashMap::new(), + &mut HashMap::new(), + &mut 0, + ) + .unwrap(); + let old_git = map_commit(&target, &snap, tree, &[]).unwrap(); + let known: HashMap = HashMap::from([(old, old_git.to_hex().to_string())]); + let export = |store: &mut scl_core::Store, tip, known| { + export_branch( + store, + tip, + &ExportOptions { + to: &dir, + ref_name: "refs/heads/main", + include_encrypted: false, + known_git_commits: known, + }, + ) + }; + + // Old, already-pushed history is not judged; the new clean commit goes through. + export(&mut store, fixed, &known).unwrap(); + // A new commit that adds it again is refused, and nothing of it is written. + let err = export(&mut store, bad_new, &known).unwrap_err(); + assert!( + format!("{err:#}").contains("not a safe Git path"), + "{err:#}" + ); + // Without the mark the old commit is re-synthesized, so it is checked. + let none = HashMap::new(); + assert!(export(&mut store, fixed, &none).is_err()); + // A stale mark (commit pruned from the destination) is also re-synthesized and checked. + let stale = HashMap::from([(old, "1".repeat(40))]); + assert!(export(&mut store, fixed, &stale).is_err()); + std::fs::remove_dir_all(&dir).unwrap(); + } + + #[test] + fn network_pending_check_skips_what_the_server_has() { + let mut store = scl_core::Store::new(scl_core::StoreConfig::default()); + let old = commit_with(&mut store, ".gitmodules", None, "old"); + let fixed = commit_with(&mut store, "a.txt", Some(old), "fixed"); + // The server has `old`: only `fixed` is uploaded. + check_names_pending_upload(&mut store, fixed, Some(old)).unwrap(); + // The server has nothing, or has something else: all of it is uploaded. + assert!(check_names_pending_upload(&mut store, fixed, None).is_err()); + let other = commit_with(&mut store, "b.txt", None, "other"); + assert!(check_names_pending_upload(&mut store, fixed, Some(other)).is_err()); + // The server is at `fixed` already: nothing to upload. + check_names_pending_upload(&mut store, fixed, Some(fixed)).unwrap(); + // A hostile commit the server lacks is refused. + let bad_new = commit_with(&mut store, ".gitmodules", Some(fixed), "readded"); + assert!(check_names_pending_upload(&mut store, bad_new, Some(fixed)).is_err()); + } } diff --git a/crates/gitio/src/lib.rs b/crates/gitio/src/lib.rs index 952304d3..9d383ed6 100644 --- a/crates/gitio/src/lib.rs +++ b/crates/gitio/src/lib.rs @@ -7,7 +7,9 @@ //! of the system Git-agnostic. mod export; -pub use export::{export_branch, read_ref, ExportOptions, ExportReport}; +pub use export::{ + check_names_pending_upload, export_branch, read_ref, ExportOptions, ExportReport, +}; mod import; pub use import::{import_history, ImportReport}; From 23be4a4e8cee1115b3008b5ca8123e91fd5afd2f Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:22:17 +0200 Subject: [PATCH 09/16] fix(cli): check names in every commit a network push uploads, including mirror-only ones --- crates/cli/src/main.rs | 27 ++++++++++++++++++ crates/cli/tests/git_remote.rs | 52 ++++++++++++++++++++++++++++++++++ crates/gitio/src/bridge.rs | 11 +++++++ 3 files changed, 90 insertions(+) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index d6b0f3d6..26c9bc04 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -4487,6 +4487,23 @@ fn run_push(remote: &str, include_encrypted: bool, no_transcripts: bool) -> Resu } } +/// Refuse unsafe Git tree names in every commit a network push would upload: the history of +/// `local_tip` minus what the server already has. The server's tip is asked of the server; one sc has +/// no mark for (or no branch at all) means the whole history is checked. +fn check_names_pending_network_upload( + repo: &scl_repo::Repo, + mirror: &std::path::Path, + branch: &str, + local_tip: scl_core::ObjectId, + git_to_sc: &std::collections::HashMap, +) -> Result<()> { + let server_sc = scl_gitio::bridge::remote_branch_tip(mirror, branch)? + .and_then(|hex| git_to_sc.get(&hex).copied()); + let store_arc = repo.vfs().store(); + let mut store = store_arc.lock().unwrap(); + scl_gitio::check_names_pending_upload(&mut store, local_tip, server_sc) +} + /// Push the current branch to a git-backed remote, fast-forward-only. The /// snapshot↔commit marks map carries identity so already-pushed commits are /// reused, not rewritten. @@ -4535,6 +4552,9 @@ fn run_push_git(repo: &scl_repo::Repo, remote: &str, include_encrypted: bool) -> match git_to_sc.get(&remote_git_hex) { Some(&remote_sc) if remote_sc == local_tip => { if scl_gitio::bridge::is_network_git_url(&url) { + check_names_pending_network_upload( + repo, &path, &branch, local_tip, &git_to_sc, + )?; scl_gitio::bridge::mirror_push(&path, &branch)?; } println!("push {remote} (git): already up to date"); @@ -4556,6 +4576,13 @@ fn run_push_git(repo: &scl_repo::Repo, remote: &str, include_encrypted: bool) -> } } + // The mirror can hold commits the server lacks (an earlier push failed after exporting, possibly + // by a client without the name checks), and export skips those as already written. Check + // everything the network push will upload, before export moves anything. + if scl_gitio::bridge::is_network_git_url(&url) { + check_names_pending_network_upload(repo, &path, &branch, local_tip, &git_to_sc)?; + } + // Export (reusing known commits), then persist newly-written marks. let report = { let store_arc = repo.vfs().store(); diff --git a/crates/cli/tests/git_remote.rs b/crates/cli/tests/git_remote.rs index 7c71be6c..68b75099 100644 --- a/crates/cli/tests/git_remote.rs +++ b/crates/cli/tests/git_remote.rs @@ -340,3 +340,55 @@ fn push_refuses_when_remote_has_unseen_commit() { std::fs::remove_dir_all(&root).unwrap(); } + +// A hostile `.gitmodules` is refused in what a push uploads, but not in history the server already +// has: git itself accepted that commit, and a fix that removes the file must still push. Goes +// through the network bridge (file://), which asks the server for its tip. +#[test] +fn network_push_checks_names_only_in_commits_the_server_lacks() { + let root = tmp("netnames"); + let hub = root.join("hub.git"); + git( + &root, + &["init", "-q", "--bare", "-b", "main", hub.to_str().unwrap()], + ); + let up = root.join("up"); + std::fs::create_dir_all(&up).unwrap(); + git(&up, &["init", "-q", "-b", "main"]); + std::fs::write(up.join(".gitmodules"), b"# none\n").unwrap(); + std::fs::write(up.join("a.txt"), b"a").unwrap(); + git(&up, &["add", "."]); + git(&up, &["commit", "-q", "-m", "old"]); + git(&up, &["push", "-q", hub.to_str().unwrap(), "main"]); + + let screpo = root.join("work"); + std::fs::create_dir_all(&screpo).unwrap(); + assert!(sc(&screpo, &["init"]).status.success()); + let url = format!("file://{}", hub.display()); + assert!(sc(&screpo, &["remote", "add", "hub", &url, "--git"]) + .status + .success()); + assert!(sc(&screpo, &["fetch", "hub"]).status.success()); + assert!(sc(&screpo, &["merge", "hub/main"]).status.success()); + + // The fix removes the file: allowed although the server's history has it. + std::fs::remove_file(screpo.join(".gitmodules")).unwrap(); + assert!(sc(&screpo, &["commit", "-m", "drop"]).status.success()); + let p = sc(&screpo, &["push", "hub"]); + assert!(p.status.success(), "{}", String::from_utf8_lossy(&p.stderr)); + let tip = git(&hub, &["rev-parse", "main"]).stdout; + + // A new commit that adds it again is refused, and the server is untouched. + std::fs::write(screpo.join(".gitmodules"), b"# again\n").unwrap(); + assert!(sc(&screpo, &["commit", "-m", "again"]).status.success()); + let p = sc(&screpo, &["push", "hub"]); + assert!(!p.status.success()); + assert!( + String::from_utf8_lossy(&p.stderr).contains("not a safe Git path"), + "{}", + String::from_utf8_lossy(&p.stderr) + ); + assert_eq!(git(&hub, &["rev-parse", "main"]).stdout, tip); + + std::fs::remove_dir_all(&root).unwrap(); +} diff --git a/crates/gitio/src/bridge.rs b/crates/gitio/src/bridge.rs index 2be4db1b..d8b693fe 100644 --- a/crates/gitio/src/bridge.rs +++ b/crates/gitio/src/bridge.rs @@ -117,6 +117,17 @@ pub fn mirror_push(mirror: &Path, branch: &str) -> anyhow::Result<()> { run_git(mirror, &["push", "--quiet", "origin", &spec]) } +/// The commit (hex) that `branch` points at on the network remote itself, asked of the server (the +/// mirror only holds what we last fetched or exported). `None` if the branch does not exist there. +pub fn remote_branch_tip(mirror: &Path, branch: &str) -> anyhow::Result> { + let refname = format!("refs/heads/{branch}"); + let out = run_git_capture(mirror, &["ls-remote", "origin", &refname])?; + Ok(out.lines().find_map(|l| { + let (oid, name) = l.split_once('\t')?; + (name == refname).then(|| oid.to_string()) + })) +} + /// The remote's default branch (`HEAD` symref), e.g. "main". pub fn remote_default_branch(mirror: &Path) -> anyhow::Result { let out = run_git_capture(mirror, &["ls-remote", "--symref", "origin", "HEAD"])?; From eab75efaab7989c802972a5d1252b780231bcbdc Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:25:36 +0200 Subject: [PATCH 10/16] fix(repo): escape ref-name diagnostics, send error text raw on the wire; docs --- crates/repo/src/error.rs | 6 ++++-- crates/repo/src/wire.rs | 37 +++++++++++++++++++++++++++++++++++-- docs/THREAT-MODEL.md | 20 +++++++++++++------- 3 files changed, 52 insertions(+), 11 deletions(-) diff --git a/crates/repo/src/error.rs b/crates/repo/src/error.rs index edd68d55..0daf8e15 100644 --- a/crates/repo/src/error.rs +++ b/crates/repo/src/error.rs @@ -16,7 +16,7 @@ pub enum Error { IndexBusy(String), #[error("object {0} is corrupt (failed hash verification on read)")] CorruptObject(ObjectId), - #[error("malformed ref: {0}")] + #[error("malformed ref: {}", scl_core::sanitize_display_line(.0))] BadRef(String), #[error("bad config: {0}")] BadConfig(String), @@ -28,7 +28,7 @@ pub enum Error { NotProtected(String), #[error("identity is not authorized for protected prefix: {0}")] NotAuthorized(String), - #[error("invalid argument: {0}")] + #[error("invalid argument: {}", scl_core::sanitize_display_line(.0))] InvalidArgument(String), #[error("operation requires at least one commit (branch is unborn)")] Unborn, @@ -240,6 +240,8 @@ mod tests { Error::ConnectionLost(raw.into()), Error::PackTooLarge(raw.into()), Error::ConnectionBudgetExceeded(raw.into()), + Error::BadRef(raw.into()), + Error::InvalidArgument(raw.into()), ] { let shown = e.to_string(); assert!( diff --git a/crates/repo/src/wire.rs b/crates/repo/src/wire.rs index a67498f4..777a974d 100644 --- a/crates/repo/src/wire.rs +++ b/crates/repo/src/wire.rs @@ -642,7 +642,12 @@ pub fn parse_response(mut frame: Vec) -> Result> { /// `Error::PackTooLarge(msg)` from whatever we send here — sending /// `e.to_string()` would carry that prefix as part of `msg`, so the /// round-tripped error's `Display` would show the prefix twice. Sending only -/// `inner` keeps the prefix appearing exactly once, on display. +/// `inner` keeps the prefix appearing exactly once, on display. The same goes for +/// `Protocol` and `Remote`. +/// +/// The message is always the raw text, never `Display` output: `Display` escapes +/// terminal controls for printing, and an escaped payload would change on the wire +/// and be escaped a second time by the next hop. pub fn err_to_wire(e: &Error) -> (u8, String) { let code = match e { Error::NotARepo => EC_NOT_A_REPO, @@ -655,7 +660,13 @@ pub fn err_to_wire(e: &Error) -> (u8, String) { _ => EC_OTHER, }; let msg = match e { - Error::PackTooLarge(inner) | Error::ConnectionBudgetExceeded(inner) => inner.clone(), + Error::PackTooLarge(inner) + | Error::ConnectionBudgetExceeded(inner) + | Error::Protocol(inner) + | Error::Remote(inner) => inner.clone(), + Error::ConnectionLost(inner) => format!("connection to remote lost: {inner}"), + Error::BadRef(inner) => format!("malformed ref: {inner}"), + Error::InvalidArgument(inner) => format!("invalid argument: {inner}"), _ => e.to_string(), }; (code, msg) @@ -1808,6 +1819,28 @@ mod tests { } } + #[test] + fn error_text_crosses_the_wire_raw_and_is_escaped_once_on_display() { + use crate::error::Error; + let raw = "bad\x1b[2Jtext"; + for e in [ + Error::Remote(raw.into()), + Error::Protocol(raw.into()), + Error::ConnectionLost(raw.into()), + Error::BadRef(raw.into()), + Error::InvalidArgument(raw.into()), + ] { + let (code, msg) = err_to_wire(&e); + assert!(msg.contains(raw), "payload must be raw: {msg:?}"); + // A second hop re-encodes what it received: still raw, never escaped twice. + let hop = wire_to_err(code, msg); + let (_, msg2) = err_to_wire(&hop); + assert!(msg2.contains(raw), "{msg2:?}"); + let shown = hop.to_string(); + assert!(shown.contains("bad\\x1b[2Jtext") && !shown.contains("\\\\x1b")); + } + } + #[test] fn body_helpers_roundtrip() { assert_eq!(decode_u32_body(&u32_body(7)).unwrap(), 7); diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index f5f5b5a9..1cf3566a 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -477,8 +477,10 @@ too, not just the src-control-side metadata. - **Remote-controlled text is escaped before printing.** A server's error string and a commit's author and message may carry ANSI or OSC escapes, so the CLI prints C0 controls (other than newline and tab), DEL and C1 controls as visible - escapes (`\x1b`) in those errors, in `sc log` and in `sc transcript` - output. Stored data and `--json` output are unchanged (JSON escapes controls). + escapes (`\x1b`) when displaying those errors and the ref-name diagnostics. + `sc log` and `sc transcript` rows are escaped as one line (newlines and bidi + controls too). Stored data, `--json` output and the bytes on the wire are + unchanged: errors cross the wire raw and are escaped only when displayed. Paths are not escaped. ## Bucket remotes (`sc+wal://` / `sc+s3://`) — ADR-0046/0057 @@ -894,11 +896,15 @@ protected paths, plaintext. - The ceiling (1 GiB) is compile-time, so no configuration can make a length claim unbounded. - **Git export refuses unsafe tree names.** `sc export` and a push to a git - remote check every entry name in the exported history before creating the - target or writing any object: `.git`, `.gitmodules` and their HFS/NTFS aliases, - `.`, `..`, empty names and names containing `/`, `\` or NUL are refused. Every - blob is written as `100644` or `100755` by its exec bit; sc has no symlinks, so a - hostile tree cannot produce a Git symlink or gitlink entry. + remote check every entry name in the snapshots they will write, before creating + the target or writing any object: `.git`, `.gitmodules` and their HFS/NTFS + aliases (git's own rules, via `gix::validate`), `.`, `..`, empty names and names + containing `/`, `\` or NUL are refused. History the destination already holds + (a reused mark) is not judged, and a network push also checks the commits the + server lacks even when the local mirror already holds them. An entry recorded + with symlink mode still exports as a Git symlink (changing that would change + re-synthesized tree ids); a Git symlink is ordinary Git content that git's own + checkout and `fsck` handle. ## sc-forge: authentication, tenancy, transport, resources — P38 F1/F2 From 05a0044cb9b141a296fd287e79cae28cce90dba5 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:47:38 +0200 Subject: [PATCH 11/16] refactor(gitio): check names where export builds a Git tree; escape Remote and Protocol on one line; docs --- crates/cli/src/main.rs | 27 ------- crates/cli/tests/git_remote.rs | 3 +- crates/gitio/src/bridge.rs | 11 --- crates/gitio/src/export.rs | 125 +++++---------------------------- crates/gitio/src/lib.rs | 4 +- crates/repo/src/error.rs | 10 +-- crates/repo/src/wire.rs | 20 ++++++ docs/THREAT-MODEL.md | 31 ++++---- 8 files changed, 63 insertions(+), 168 deletions(-) diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 26c9bc04..d6b0f3d6 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -4487,23 +4487,6 @@ fn run_push(remote: &str, include_encrypted: bool, no_transcripts: bool) -> Resu } } -/// Refuse unsafe Git tree names in every commit a network push would upload: the history of -/// `local_tip` minus what the server already has. The server's tip is asked of the server; one sc has -/// no mark for (or no branch at all) means the whole history is checked. -fn check_names_pending_network_upload( - repo: &scl_repo::Repo, - mirror: &std::path::Path, - branch: &str, - local_tip: scl_core::ObjectId, - git_to_sc: &std::collections::HashMap, -) -> Result<()> { - let server_sc = scl_gitio::bridge::remote_branch_tip(mirror, branch)? - .and_then(|hex| git_to_sc.get(&hex).copied()); - let store_arc = repo.vfs().store(); - let mut store = store_arc.lock().unwrap(); - scl_gitio::check_names_pending_upload(&mut store, local_tip, server_sc) -} - /// Push the current branch to a git-backed remote, fast-forward-only. The /// snapshot↔commit marks map carries identity so already-pushed commits are /// reused, not rewritten. @@ -4552,9 +4535,6 @@ fn run_push_git(repo: &scl_repo::Repo, remote: &str, include_encrypted: bool) -> match git_to_sc.get(&remote_git_hex) { Some(&remote_sc) if remote_sc == local_tip => { if scl_gitio::bridge::is_network_git_url(&url) { - check_names_pending_network_upload( - repo, &path, &branch, local_tip, &git_to_sc, - )?; scl_gitio::bridge::mirror_push(&path, &branch)?; } println!("push {remote} (git): already up to date"); @@ -4576,13 +4556,6 @@ fn run_push_git(repo: &scl_repo::Repo, remote: &str, include_encrypted: bool) -> } } - // The mirror can hold commits the server lacks (an earlier push failed after exporting, possibly - // by a client without the name checks), and export skips those as already written. Check - // everything the network push will upload, before export moves anything. - if scl_gitio::bridge::is_network_git_url(&url) { - check_names_pending_network_upload(repo, &path, &branch, local_tip, &git_to_sc)?; - } - // Export (reusing known commits), then persist newly-written marks. let report = { let store_arc = repo.vfs().store(); diff --git a/crates/cli/tests/git_remote.rs b/crates/cli/tests/git_remote.rs index 68b75099..7d3e03bb 100644 --- a/crates/cli/tests/git_remote.rs +++ b/crates/cli/tests/git_remote.rs @@ -343,7 +343,8 @@ fn push_refuses_when_remote_has_unseen_commit() { // A hostile `.gitmodules` is refused in what a push uploads, but not in history the server already // has: git itself accepted that commit, and a fix that removes the file must still push. Goes -// through the network bridge (file://), which asks the server for its tip. +// through the network bridge (file://). Names are checked where export builds a tree, so a commit +// reused through a mark is never judged and a refusal leaves the remote tip where it was. #[test] fn network_push_checks_names_only_in_commits_the_server_lacks() { let root = tmp("netnames"); diff --git a/crates/gitio/src/bridge.rs b/crates/gitio/src/bridge.rs index d8b693fe..2be4db1b 100644 --- a/crates/gitio/src/bridge.rs +++ b/crates/gitio/src/bridge.rs @@ -117,17 +117,6 @@ pub fn mirror_push(mirror: &Path, branch: &str) -> anyhow::Result<()> { run_git(mirror, &["push", "--quiet", "origin", &spec]) } -/// The commit (hex) that `branch` points at on the network remote itself, asked of the server (the -/// mirror only holds what we last fetched or exported). `None` if the branch does not exist there. -pub fn remote_branch_tip(mirror: &Path, branch: &str) -> anyhow::Result> { - let refname = format!("refs/heads/{branch}"); - let out = run_git_capture(mirror, &["ls-remote", "origin", &refname])?; - Ok(out.lines().find_map(|l| { - let (oid, name) = l.split_once('\t')?; - (name == refname).then(|| oid.to_string()) - })) -} - /// The remote's default branch (`HEAD` symref), e.g. "main". pub fn remote_default_branch(mirror: &Path) -> anyhow::Result { let out = run_git_capture(mirror, &["ls-remote", "--symref", "origin", "HEAD"])?; diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index 00172d77..5d90579f 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -253,6 +253,14 @@ fn map_tree( return Ok(g); } let tree = store.get_tree(&id)?; + // The one place a new Git tree is built from an sc tree, so every name export writes is judged + // here, including a commit re-synthesized after its mark went stale. A commit reused through a + // mark is never rewritten and so never reaches this. The refusal comes before the branch ref + // (and any mirror push) moves; blobs and subtrees already written stay in the target as + // unreferenced objects, which is harmless. + for e in &tree.entries { + check_entry_name(&e.name)?; + } let mut git_entries = Vec::with_capacity(tree.entries.len()); for e in &tree.entries { if e.perms & PROTECTED != 0 { @@ -469,76 +477,6 @@ fn check_entry_name(name: &str) -> anyhow::Result<()> { Ok(()) } -/// Check the entry names of every tree of every snapshot that an export would WRITE: the DAG from -/// `tip`, not descending into `stop` (commits the destination already holds, whose trees are never -/// rewritten). Read-only, so a refusal leaves the destination untouched. -fn check_pending_names( - store: &mut Store, - tip: ObjectId, - stop: &std::collections::HashSet, -) -> anyhow::Result<()> { - let mut seen_snaps = std::collections::HashSet::new(); - let mut seen_trees = std::collections::HashSet::new(); - let mut snaps = vec![tip]; - while let Some(sid) = snaps.pop() { - if stop.contains(&sid) || !seen_snaps.insert(sid) { - continue; - } - let snap = store.get_snapshot(&sid)?; - let mut trees = vec![snap.root]; - while let Some(id) = trees.pop() { - if !seen_trees.insert(id) { - continue; - } - for e in &store.get_tree(&id)?.entries { - check_entry_name(&e.name)?; - if let EntryKind::Tree = e.kind { - trees.push(e.id); - } - } - } - snaps.extend(snap.parents.iter().copied()); - } - Ok(()) -} - -/// For a network push: refuse unsafe names in the commits the push would upload, that is the history -/// of `tip` minus everything the server already has (`server_tip` and its ancestors; `None` means the -/// whole history). This covers commits the local mirror already holds (exported by an earlier or -/// older client, so skipped by [`export_branch`]) but the server does not. -pub fn check_names_pending_upload( - store: &mut Store, - tip: ObjectId, - server_tip: Option, -) -> anyhow::Result<()> { - let mut on_server = std::collections::HashSet::new(); - let mut stack: Vec = server_tip.into_iter().collect(); - while let Some(sid) = stack.pop() { - if on_server.insert(sid) { - stack.extend(store.get_snapshot(&sid)?.parents.iter().copied()); - } - } - check_pending_names(store, tip, &on_server) -} - -/// Marked commits that still exist in the Git repo at `path` (so a later export reuses them). Reads -/// only, and creates nothing: a missing repo has none. -fn reusable_commits( - path: &Path, - known: &HashMap, -) -> std::collections::HashSet { - let Ok(repo) = gix::open(path) else { - return Default::default(); - }; - known - .iter() - .filter(|(_, hex)| { - gix::ObjectId::from_hex(hex.as_bytes()).is_ok_and(|g| repo.has_object(g)) - }) - .map(|(id, _)| *id) - .collect() -} - /// Record PROTECTED entry paths under `prefix`, recursing into subtrees. Uses an /// explicit stack so deep trees can't overflow. Deduplicates subtree object ids /// via `seen` so shared subtrees (identical content at multiple paths in a @@ -594,13 +532,6 @@ pub fn export_branch( ); } let secrets_dropped = secret_names.len(); - // Unsafe names fail closed too, but only in snapshots this export will write: history the - // destination already holds (reused marks) is not ours to judge. - check_pending_names( - store, - tip, - &reusable_commits(opts.to, opts.known_git_commits), - )?; // Signatures have no fail-closed gate (unlike protected content/secrets, // a dropped signature is a provenance-completeness loss, not a // confidentiality one) — always counted, exported or not. @@ -1632,7 +1563,7 @@ mod tests { } #[test] - fn export_refuses_hostile_name_before_writing_anything() { + fn export_refuses_hostile_name_before_any_ref_moves() { for name in [".git", ".gitmodules", "GIT~1"] { let mut store = scl_core::Store::new(scl_core::StoreConfig::default()); // Nested, so the walk must reach it past a clean sibling blob. @@ -1655,7 +1586,8 @@ mod tests { format!("{err:#}").contains("not a safe Git path"), "{err:#}" ); - assert!(!dir.exists(), "no git repo may be created on refusal"); + // Nothing may point at the hostile history (the target itself may have been created). + assert_eq!(read_ref(&dir, "refs/heads/main").unwrap(), None); } } @@ -1723,20 +1655,13 @@ mod tests { let fixed = commit_with(&mut store, "a.txt", Some(old), "fixed"); let bad_new = commit_with(&mut store, ".gitmodules", Some(fixed), "readded"); - // Put `old` on the destination as a real Git commit, as an earlier push would have. + // Put `old` on the destination as a Git commit, as an earlier push (before the check) would have. let dir = std::env::temp_dir().join(format!("scl-pending-{}", std::process::id())); let _ = std::fs::remove_dir_all(&dir); let target = GitTarget::open_or_init_bare(&dir).unwrap(); let snap = store.get_snapshot(&old).unwrap(); - let tree = map_tree( - &mut store, - &target, - snap.root, - &mut HashMap::new(), - &mut HashMap::new(), - &mut 0, - ) - .unwrap(); + // Its tree is irrelevant to reuse (and map_tree would refuse the name): an empty one will do. + let tree = target.write_tree(vec![]).unwrap(); let old_git = map_commit(&target, &snap, tree, &[]).unwrap(); let known: HashMap = HashMap::from([(old, old_git.to_hex().to_string())]); let export = |store: &mut scl_core::Store, tip, known| { @@ -1753,8 +1678,8 @@ mod tests { }; // Old, already-pushed history is not judged; the new clean commit goes through. - export(&mut store, fixed, &known).unwrap(); - // A new commit that adds it again is refused, and nothing of it is written. + let rep = export(&mut store, fixed, &known).unwrap(); + // A new commit that adds it again is refused. let err = export(&mut store, bad_new, &known).unwrap_err(); assert!( format!("{err:#}").contains("not a safe Git path"), @@ -1768,22 +1693,4 @@ mod tests { assert!(export(&mut store, fixed, &stale).is_err()); std::fs::remove_dir_all(&dir).unwrap(); } - - #[test] - fn network_pending_check_skips_what_the_server_has() { - let mut store = scl_core::Store::new(scl_core::StoreConfig::default()); - let old = commit_with(&mut store, ".gitmodules", None, "old"); - let fixed = commit_with(&mut store, "a.txt", Some(old), "fixed"); - // The server has `old`: only `fixed` is uploaded. - check_names_pending_upload(&mut store, fixed, Some(old)).unwrap(); - // The server has nothing, or has something else: all of it is uploaded. - assert!(check_names_pending_upload(&mut store, fixed, None).is_err()); - let other = commit_with(&mut store, "b.txt", None, "other"); - assert!(check_names_pending_upload(&mut store, fixed, Some(other)).is_err()); - // The server is at `fixed` already: nothing to upload. - check_names_pending_upload(&mut store, fixed, Some(fixed)).unwrap(); - // A hostile commit the server lacks is refused. - let bad_new = commit_with(&mut store, ".gitmodules", Some(fixed), "readded"); - assert!(check_names_pending_upload(&mut store, bad_new, Some(fixed)).is_err()); - } } diff --git a/crates/gitio/src/lib.rs b/crates/gitio/src/lib.rs index 9d383ed6..952304d3 100644 --- a/crates/gitio/src/lib.rs +++ b/crates/gitio/src/lib.rs @@ -7,9 +7,7 @@ //! of the system Git-agnostic. mod export; -pub use export::{ - check_names_pending_upload, export_branch, read_ref, ExportOptions, ExportReport, -}; +pub use export::{export_branch, read_ref, ExportOptions, ExportReport}; mod import; pub use import::{import_history, ImportReport}; diff --git a/crates/repo/src/error.rs b/crates/repo/src/error.rs index 0daf8e15..eb9807ec 100644 --- a/crates/repo/src/error.rs +++ b/crates/repo/src/error.rs @@ -127,11 +127,11 @@ pub enum Error { RemoteExists(String), #[error("no common ancestor between the branches")] NoCommonAncestor, - #[error("wire protocol error: {}", scl_core::sanitize_display(.0))] + #[error("wire protocol error: {}", scl_core::sanitize_display_line(.0))] Protocol(String), - #[error("connection to remote lost: {}", scl_core::sanitize_display(.0))] + #[error("connection to remote lost: {}", scl_core::sanitize_display_line(.0))] ConnectionLost(String), - #[error("remote error: {}", scl_core::sanitize_display(.0))] + #[error("remote error: {}", scl_core::sanitize_display_line(.0))] Remote(String), #[error("TLS: {0}")] Tls(#[from] scl_tlsio::Error), @@ -184,13 +184,13 @@ pub enum Error { /// The server aborted an incoming pack mid-stream because it exceeded the /// operator's `--max-pack-size` cap (P31). Payload is the server's own /// human-readable limit text, carried verbatim across the wire. - #[error("pack exceeds the server's --max-pack-size limit: {}", scl_core::sanitize_display(.0))] + #[error("pack exceeds the server's --max-pack-size limit: {}", scl_core::sanitize_display_line(.0))] PackTooLarge(String), /// The server closed the connection because the bytes it wrote for this /// peer (`PutObject` + `PutPack`) exceeded the operator's /// `--max-conn-bytes` budget (#233). Payload is the server's own /// human-readable limit text, carried verbatim across the wire. - #[error("connection write budget exceeded: {}", scl_core::sanitize_display(.0))] + #[error("connection write budget exceeded: {}", scl_core::sanitize_display_line(.0))] ConnectionBudgetExceeded(String), /// The server refused the connection at accept time because /// `--max-connections` was reached (P31). Retryable. diff --git a/crates/repo/src/wire.rs b/crates/repo/src/wire.rs index 777a974d..cdd5d41f 100644 --- a/crates/repo/src/wire.rs +++ b/crates/repo/src/wire.rs @@ -1841,6 +1841,26 @@ mod tests { } } + #[test] + fn forwarded_errors_display_on_one_line_after_decoding() { + use crate::error::Error; + // BadRef and InvalidArgument decode as Remote on the other side; whatever they carry must + // still print as a single line, so a forged `signature:` row cannot be made with a newline. + let raw = "x\n signature: good\ty"; + for e in [ + Error::BadRef(raw.into()), + Error::InvalidArgument(raw.into()), + ] { + let (code, msg) = err_to_wire(&e); + let shown = wire_to_err(code, msg).to_string(); + assert!(!shown.contains(['\n', '\t']), "{shown:?}"); + assert!( + shown.contains("x\\x0a signature: good\\x09y"), + "{shown:?}" + ); + } + } + #[test] fn body_helpers_roundtrip() { assert_eq!(decode_u32_body(&u32_body(7)).unwrap(), 7); diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 1cf3566a..e8d42306 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -479,9 +479,11 @@ too, not just the src-control-side metadata. prints C0 controls (other than newline and tab), DEL and C1 controls as visible escapes (`\x1b`) when displaying those errors and the ref-name diagnostics. `sc log` and `sc transcript` rows are escaped as one line (newlines and bidi - controls too). Stored data, `--json` output and the bytes on the wire are - unchanged: errors cross the wire raw and are escaped only when displayed. - Paths are not escaped. + controls too); remote, protocol and ref diagnostics print on one line as well. + Stored data and `--json` output are unchanged. Wire framing and error codes are + unchanged; the diagnostic text is now sent raw (not as escaped `Display` + output) and without the doubled prefix `Protocol` and `Remote` used to carry, + and is escaped only when displayed. Paths are not escaped. ## Bucket remotes (`sc+wal://` / `sc+s3://`) — ADR-0046/0057 @@ -896,15 +898,20 @@ protected paths, plaintext. - The ceiling (1 GiB) is compile-time, so no configuration can make a length claim unbounded. - **Git export refuses unsafe tree names.** `sc export` and a push to a git - remote check every entry name in the snapshots they will write, before creating - the target or writing any object: `.git`, `.gitmodules` and their HFS/NTFS - aliases (git's own rules, via `gix::validate`), `.`, `..`, empty names and names - containing `/`, `\` or NUL are refused. History the destination already holds - (a reused mark) is not judged, and a network push also checks the commits the - server lacks even when the local mirror already holds them. An entry recorded - with symlink mode still exports as a Git symlink (changing that would change - re-synthesized tree ids); a Git symlink is ordinary Git content that git's own - checkout and `fsck` handle. + remote check every entry name at the one place export builds a Git tree from an + sc tree: `.git`, `.gitmodules` and their HFS/NTFS aliases (git's own rules, via + `gix::validate`), `.`, `..`, empty names and names containing `/`, `\` or NUL + are refused. The check runs before the branch ref (and the mirror push) moves; + blobs and subtrees written before the refusal stay in the target as + unreferenced objects. A commit reused through a mark is never rewritten, so it + is not judged, while anything export writes is, including a commit + re-synthesized after its mark went stale. **Residual:** commits an older client + already wrote into the mirror are pushed as they are; Git hosts that run + `fsck` on receive refuse such names. An entry recorded with symlink mode still + exports as a Git symlink (changing that would change re-synthesized tree ids), + and a symlinked `.gitattributes`, `.gitignore` or `.mailmap` stays exportable + (git treats them as informational); a Git symlink is ordinary Git content that + git's own checkout and `fsck` handle. ## sc-forge: authentication, tenancy, transport, resources — P38 F1/F2 From 93d9bc73a059706c4e3b1925c87ce8acc65ceed6 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 10:48:15 +0200 Subject: [PATCH 12/16] test(gitio): assert the ref stays put when export refuses --- crates/gitio/src/export.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index 5d90579f..428f3acc 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -1685,6 +1685,11 @@ mod tests { format!("{err:#}").contains("not a safe Git path"), "{err:#}" ); + assert_eq!( + read_ref(&dir, "refs/heads/main").unwrap().as_deref(), + Some(rep.git_commit.as_str()), + "the ref must not move on refusal" + ); // Without the mark the old commit is re-synthesized, so it is checked. let none = HashMap::new(); assert!(export(&mut store, fixed, &none).is_err()); From 10b26bdee912d7e2ab64bf8ef4226c48aa1b2cb3 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:22:06 +0200 Subject: [PATCH 13/16] fix(gitio): remove a target repo a refused export created; docs wording --- crates/gitio/src/export.rs | 193 +++++++++++++++++++++++-------------- docs/THREAT-MODEL.md | 8 +- 2 files changed, 124 insertions(+), 77 deletions(-) diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index 428f3acc..65b9355b 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -541,81 +541,89 @@ pub fn export_branch( let transcripts_dropped = count_transcripts(store, &seen_snaps)?; let target = GitTarget::open_or_init_bare(opts.to)?; - - let mut commit_memo: HashMap = HashMap::new(); - // Seed with commits already on the target (marks map): these are reused, not - // rewritten. A bad hex here is a corrupt marks file — surface it. Verify each - // still EXISTS there before reuse: `git gc` in the target can prune commits a - // stale mark still names, and blindly reusing a pruned oid writes a broken - // parent chain (P21 follow-on, ADR-0031). A missing one is treated as - // unknown — the walk below re-synthesizes it — and counted. - let mut stale_marks = 0usize; - for (sc_id, git_hex) in opts.known_git_commits { - let g = gix::ObjectId::from_hex(git_hex.as_bytes()) - .map_err(|e| anyhow::anyhow!("bad git oid in marks for {sc_id}: {e}"))?; - if target.has_object(g) { - commit_memo.insert(*sc_id, g); - } else { - stale_marks += 1; - } - } - let mut new_marks: Vec<(String, ObjectId)> = Vec::new(); - let mut tree_memo: HashMap = HashMap::new(); - let mut blob_memo: HashMap = HashMap::new(); - let mut protected = 0usize; - - // Post-order DAG walk: a commit is written only after all its parents have - // Git oids. Explicit stack (no recursion) so deep history can't overflow. - let mut stack: Vec<(ObjectId, bool)> = vec![(tip, false)]; - while let Some((sid, ready)) = stack.pop() { - if commit_memo.contains_key(&sid) { - continue; + let result = (|| -> anyhow::Result { + let mut commit_memo: HashMap = HashMap::new(); + // Seed with commits already on the target (marks map): these are reused, not + // rewritten. A bad hex here is a corrupt marks file — surface it. Verify each + // still EXISTS there before reuse: `git gc` in the target can prune commits a + // stale mark still names, and blindly reusing a pruned oid writes a broken + // parent chain (P21 follow-on, ADR-0031). A missing one is treated as + // unknown — the walk below re-synthesizes it — and counted. + let mut stale_marks = 0usize; + for (sc_id, git_hex) in opts.known_git_commits { + let g = gix::ObjectId::from_hex(git_hex.as_bytes()) + .map_err(|e| anyhow::anyhow!("bad git oid in marks for {sc_id}: {e}"))?; + if target.has_object(g) { + commit_memo.insert(*sc_id, g); + } else { + stale_marks += 1; + } } - if ready { - let snap = store.get_snapshot(&sid)?; - let tree_oid = map_tree( - store, - &target, - snap.root, - &mut tree_memo, - &mut blob_memo, - &mut protected, - )?; - let parent_oids: Vec = - snap.parents.iter().map(|p| commit_memo[p]).collect(); - let cid = map_commit(&target, &snap, tree_oid, &parent_oids)?; - commit_memo.insert(sid, cid); - new_marks.push((cid.to_hex().to_string(), sid)); - } else { - stack.push((sid, true)); - let snap = store.get_snapshot(&sid)?; - for p in &snap.parents { - if !commit_memo.contains_key(p) { - stack.push((*p, false)); + let mut new_marks: Vec<(String, ObjectId)> = Vec::new(); + let mut tree_memo: HashMap = HashMap::new(); + let mut blob_memo: HashMap = HashMap::new(); + let mut protected = 0usize; + + // Post-order DAG walk: a commit is written only after all its parents have + // Git oids. Explicit stack (no recursion) so deep history can't overflow. + let mut stack: Vec<(ObjectId, bool)> = vec![(tip, false)]; + while let Some((sid, ready)) = stack.pop() { + if commit_memo.contains_key(&sid) { + continue; + } + if ready { + let snap = store.get_snapshot(&sid)?; + let tree_oid = map_tree( + store, + &target, + snap.root, + &mut tree_memo, + &mut blob_memo, + &mut protected, + )?; + let parent_oids: Vec = + snap.parents.iter().map(|p| commit_memo[p]).collect(); + let cid = map_commit(&target, &snap, tree_oid, &parent_oids)?; + commit_memo.insert(sid, cid); + new_marks.push((cid.to_hex().to_string(), sid)); + } else { + stack.push((sid, true)); + let snap = store.get_snapshot(&sid)?; + for p in &snap.parents { + if !commit_memo.contains_key(p) { + stack.push((*p, false)); + } } } } - } - let tip_git = commit_memo[&tip]; - target.set_ref_force(opts.ref_name, tip_git)?; - // If we created the repo, point HEAD at the exported ref so `git log` and - // `import_head` (which resolve HEAD) see the history. Never touch HEAD on a - // pre-existing repo. - if target.created { - target.set_head(opts.ref_name)?; - } + let tip_git = commit_memo[&tip]; + target.set_ref_force(opts.ref_name, tip_git)?; + // If we created the repo, point HEAD at the exported ref so `git log` and + // `import_head` (which resolve HEAD) see the history. Never touch HEAD on a + // pre-existing repo. + if target.created { + target.set_head(opts.ref_name)?; + } - Ok(ExportReport { - git_commit: tip_git.to_hex().to_string(), - commits_written: commit_memo.len(), - protected_blobs_as_ciphertext: protected, - secrets_dropped, - signatures_dropped, - transcripts_dropped, - new_marks, - stale_marks, - }) + Ok(ExportReport { + git_commit: tip_git.to_hex().to_string(), + commits_written: commit_memo.len(), + protected_blobs_as_ciphertext: protected, + secrets_dropped, + signatures_dropped, + transcripts_dropped, + new_marks, + stale_marks, + }) + })(); + // A refused or failed export must not leave a repository THIS call created: its HEAD would name + // Git's default branch, and a clean retry would take it for pre-existing, skip `set_head` and + // report success on a repo `git log` cannot read. A pre-existing repo is never touched. + if result.is_err() && target.created { + let _ = std::fs::remove_dir_all(opts.to); + } + result } /// The current git-oid-hex that `ref_name` points at in the git repo at `path`, @@ -1575,19 +1583,56 @@ mod tests { )); let _ = std::fs::remove_dir_all(&dir); let empty = HashMap::new(); - let opts = ExportOptions { + let opts = |ref_name| ExportOptions { to: &dir, - ref_name: "refs/heads/main", + ref_name, include_encrypted: false, known_git_commits: &empty, }; - let err = export_branch(&mut store, snap, &opts).unwrap_err(); + // A non-default ref: HEAD of a repo left behind would not name it. + let topic = "refs/heads/topic"; + let err = export_branch(&mut store, snap, &opts(topic)).unwrap_err(); assert!( format!("{err:#}").contains("not a safe Git path"), "{err:#}" ); - // Nothing may point at the hostile history (the target itself may have been created). - assert_eq!(read_ref(&dir, "refs/heads/main").unwrap(), None); + // The target this call created is removed, not left half-made. + assert!( + !dir.exists(), + "a refused first export must remove its target" + ); + + // A clean retry to the same ref succeeds and leaves a repo `git log` can read. + let clean = commit_with(&mut store, "a.txt", None, "clean"); + export_branch(&mut store, clean, &opts(topic)).unwrap(); + let out = Command::new("git") + .args([ + "--git-dir", + dir.to_str().unwrap(), + "log", + "--format=%s", + "topic", + ]) + .output() + .unwrap(); + assert!(out.status.success(), "{out:?}"); + assert_eq!(String::from_utf8_lossy(&out.stdout).trim(), "clean"); + let head = Command::new("git") + .args(["--git-dir", dir.to_str().unwrap(), "log", "--format=%s"]) + .output() + .unwrap(); + assert!( + head.status.success(), + "HEAD must name the exported ref: {head:?}" + ); + std::fs::remove_dir_all(&dir).unwrap(); + + // A pre-existing repo is never removed, and no ref moves in it. + GitTarget::open_or_init_bare(&dir).unwrap(); + assert!(export_branch(&mut store, snap, &opts(topic)).is_err()); + assert!(dir.exists()); + assert_eq!(read_ref(&dir, topic).unwrap(), None); + std::fs::remove_dir_all(&dir).unwrap(); } } diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index e8d42306..02d49e2e 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -903,11 +903,13 @@ protected paths, plaintext. `gix::validate`), `.`, `..`, empty names and names containing `/`, `\` or NUL are refused. The check runs before the branch ref (and the mirror push) moves; blobs and subtrees written before the refusal stay in the target as - unreferenced objects. A commit reused through a mark is never rewritten, so it + unreferenced objects, except that a repository this export created is removed + again (a pre-existing one is never touched). A commit reused through a mark is never rewritten, so it is not judged, while anything export writes is, including a commit re-synthesized after its mark went stale. **Residual:** commits an older client - already wrote into the mirror are pushed as they are; Git hosts that run - `fsck` on receive refuse such names. An entry recorded with symlink mode still + already wrote into the mirror are pushed as they are, subject only to the + receiver's own `fsck` rules, which are not export's policy (they accept a + regular `.gitmodules`, which export refuses). An entry recorded with symlink mode still exports as a Git symlink (changing that would change re-synthesized tree ids), and a symlinked `.gitattributes`, `.gitignore` or `.mailmap` stays exportable (git treats them as informational); a Git symlink is ordinary Git content that From e6f46b3760322584ac1e725974e858a22a0ec134 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:25:23 +0200 Subject: [PATCH 14/16] fix(gitio): claim a new export target exclusively before owning it --- crates/gitio/src/export.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index 65b9355b..bed742cd 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -60,19 +60,33 @@ impl GitTarget { /// Open an existing Git repo at `path`; if `path` does not exist, create a /// bare repo there. A path that exists but is not a Git repo is an error. pub(crate) fn open_or_init_bare(path: &Path) -> Result { - let (repo, created) = if path.exists() { + // Claim the directory with an exclusive create: only a call that made it owns the + // repository (and may remove it on failure). Another process that wins the race, or a + // directory that already exists, makes this call treat the target as pre-existing. + if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { + std::fs::create_dir_all(parent) + .with_context(|| format!("creating {}", parent.display()))?; + } + let claimed = match std::fs::create_dir(path) { + Ok(()) => true, + Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => false, + Err(e) => { + return Err(anyhow::Error::new(e).context(format!("creating {}", path.display()))) + } + }; + let (repo, created) = if claimed { + ( + gix::init_bare(path) + .with_context(|| format!("initializing bare git repo at {}", path.display()))?, + true, + ) + } else { ( gix::open(path).with_context(|| { format!("{} exists but is not a git repository", path.display()) })?, false, ) - } else { - ( - gix::init_bare(path) - .with_context(|| format!("initializing bare git repo at {}", path.display()))?, - true, - ) }; Ok(GitTarget { repo, created }) } From 2abb10838479a39bcb7cd965ef7693e0b9a12136 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:29:06 +0200 Subject: [PATCH 15/16] fix(gitio): never remove an export target; a ref-less target takes the exported HEAD --- crates/gitio/src/export.rs | 179 ++++++++++++++++++------------------- docs/THREAT-MODEL.md | 5 +- 2 files changed, 91 insertions(+), 93 deletions(-) diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index bed742cd..e3ca5186 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -14,9 +14,9 @@ use scl_core::{EntryKind, FileMode, Object, ObjectId, Snapshot, Store, PROTECTED /// A Git repository we write objects and refs into. pub(crate) struct GitTarget { repo: gix::Repository, - /// True if THIS call created the repo (so we own its `HEAD`). We only point - /// `HEAD` at the exported ref when we created the repo — never clobber the - /// `HEAD` of a pre-existing repo (it may be someone's working checkout). + /// True if THIS call created the repo, or it holds no refs at all (so its `HEAD` + /// is unborn and we may point it at the exported ref). Never clobber the `HEAD` + /// of a repo that has refs (it may be someone's working checkout). pub(crate) created: bool, } @@ -81,12 +81,19 @@ impl GitTarget { true, ) } else { - ( - gix::open(path).with_context(|| { - format!("{} exists but is not a git repository", path.display()) - })?, - false, - ) + let repo = gix::open(path).with_context(|| { + format!("{} exists but is not a git repository", path.display()) + })?; + // A repo with no refs (for example one a refused export left behind) has an + // unborn HEAD: pointing it at our ref is what a fresh repo gets. + let no_refs = repo + .references() + .context("listing refs")? + .all() + .context("listing refs")? + .next() + .is_none(); + (repo, no_refs) }; Ok(GitTarget { repo, created }) } @@ -554,90 +561,83 @@ pub fn export_branch( // Git-native equivalent either, always counted, exported or not. let transcripts_dropped = count_transcripts(store, &seen_snaps)?; + // A refused export may leave the bare repository it created, with no refs. It is never removed: + // another export may already be writing into it. A retry finds it ref-less and sets its HEAD. let target = GitTarget::open_or_init_bare(opts.to)?; - let result = (|| -> anyhow::Result { - let mut commit_memo: HashMap = HashMap::new(); - // Seed with commits already on the target (marks map): these are reused, not - // rewritten. A bad hex here is a corrupt marks file — surface it. Verify each - // still EXISTS there before reuse: `git gc` in the target can prune commits a - // stale mark still names, and blindly reusing a pruned oid writes a broken - // parent chain (P21 follow-on, ADR-0031). A missing one is treated as - // unknown — the walk below re-synthesizes it — and counted. - let mut stale_marks = 0usize; - for (sc_id, git_hex) in opts.known_git_commits { - let g = gix::ObjectId::from_hex(git_hex.as_bytes()) - .map_err(|e| anyhow::anyhow!("bad git oid in marks for {sc_id}: {e}"))?; - if target.has_object(g) { - commit_memo.insert(*sc_id, g); - } else { - stale_marks += 1; - } + let mut commit_memo: HashMap = HashMap::new(); + // Seed with commits already on the target (marks map): these are reused, not + // rewritten. A bad hex here is a corrupt marks file — surface it. Verify each + // still EXISTS there before reuse: `git gc` in the target can prune commits a + // stale mark still names, and blindly reusing a pruned oid writes a broken + // parent chain (P21 follow-on, ADR-0031). A missing one is treated as + // unknown — the walk below re-synthesizes it — and counted. + let mut stale_marks = 0usize; + for (sc_id, git_hex) in opts.known_git_commits { + let g = gix::ObjectId::from_hex(git_hex.as_bytes()) + .map_err(|e| anyhow::anyhow!("bad git oid in marks for {sc_id}: {e}"))?; + if target.has_object(g) { + commit_memo.insert(*sc_id, g); + } else { + stale_marks += 1; } - let mut new_marks: Vec<(String, ObjectId)> = Vec::new(); - let mut tree_memo: HashMap = HashMap::new(); - let mut blob_memo: HashMap = HashMap::new(); - let mut protected = 0usize; - - // Post-order DAG walk: a commit is written only after all its parents have - // Git oids. Explicit stack (no recursion) so deep history can't overflow. - let mut stack: Vec<(ObjectId, bool)> = vec![(tip, false)]; - while let Some((sid, ready)) = stack.pop() { - if commit_memo.contains_key(&sid) { - continue; - } - if ready { - let snap = store.get_snapshot(&sid)?; - let tree_oid = map_tree( - store, - &target, - snap.root, - &mut tree_memo, - &mut blob_memo, - &mut protected, - )?; - let parent_oids: Vec = - snap.parents.iter().map(|p| commit_memo[p]).collect(); - let cid = map_commit(&target, &snap, tree_oid, &parent_oids)?; - commit_memo.insert(sid, cid); - new_marks.push((cid.to_hex().to_string(), sid)); - } else { - stack.push((sid, true)); - let snap = store.get_snapshot(&sid)?; - for p in &snap.parents { - if !commit_memo.contains_key(p) { - stack.push((*p, false)); - } + } + let mut new_marks: Vec<(String, ObjectId)> = Vec::new(); + let mut tree_memo: HashMap = HashMap::new(); + let mut blob_memo: HashMap = HashMap::new(); + let mut protected = 0usize; + + // Post-order DAG walk: a commit is written only after all its parents have + // Git oids. Explicit stack (no recursion) so deep history can't overflow. + let mut stack: Vec<(ObjectId, bool)> = vec![(tip, false)]; + while let Some((sid, ready)) = stack.pop() { + if commit_memo.contains_key(&sid) { + continue; + } + if ready { + let snap = store.get_snapshot(&sid)?; + let tree_oid = map_tree( + store, + &target, + snap.root, + &mut tree_memo, + &mut blob_memo, + &mut protected, + )?; + let parent_oids: Vec = + snap.parents.iter().map(|p| commit_memo[p]).collect(); + let cid = map_commit(&target, &snap, tree_oid, &parent_oids)?; + commit_memo.insert(sid, cid); + new_marks.push((cid.to_hex().to_string(), sid)); + } else { + stack.push((sid, true)); + let snap = store.get_snapshot(&sid)?; + for p in &snap.parents { + if !commit_memo.contains_key(p) { + stack.push((*p, false)); } } } + } - let tip_git = commit_memo[&tip]; - target.set_ref_force(opts.ref_name, tip_git)?; - // If we created the repo, point HEAD at the exported ref so `git log` and - // `import_head` (which resolve HEAD) see the history. Never touch HEAD on a - // pre-existing repo. - if target.created { - target.set_head(opts.ref_name)?; - } - - Ok(ExportReport { - git_commit: tip_git.to_hex().to_string(), - commits_written: commit_memo.len(), - protected_blobs_as_ciphertext: protected, - secrets_dropped, - signatures_dropped, - transcripts_dropped, - new_marks, - stale_marks, - }) - })(); - // A refused or failed export must not leave a repository THIS call created: its HEAD would name - // Git's default branch, and a clean retry would take it for pre-existing, skip `set_head` and - // report success on a repo `git log` cannot read. A pre-existing repo is never touched. - if result.is_err() && target.created { - let _ = std::fs::remove_dir_all(opts.to); + let tip_git = commit_memo[&tip]; + target.set_ref_force(opts.ref_name, tip_git)?; + // If we created the repo, point HEAD at the exported ref so `git log` and + // `import_head` (which resolve HEAD) see the history. Never touch HEAD on a + // pre-existing repo. + if target.created { + target.set_head(opts.ref_name)?; } - result + + Ok(ExportReport { + git_commit: tip_git.to_hex().to_string(), + commits_written: commit_memo.len(), + protected_blobs_as_ciphertext: protected, + secrets_dropped, + signatures_dropped, + transcripts_dropped, + new_marks, + stale_marks, + }) } /// The current git-oid-hex that `ref_name` points at in the git repo at `path`, @@ -1610,11 +1610,8 @@ mod tests { format!("{err:#}").contains("not a safe Git path"), "{err:#}" ); - // The target this call created is removed, not left half-made. - assert!( - !dir.exists(), - "a refused first export must remove its target" - ); + // No ref moved in the repository the refused export created. + assert_eq!(read_ref(&dir, topic).unwrap(), None); // A clean retry to the same ref succeeds and leaves a repo `git log` can read. let clean = commit_with(&mut store, "a.txt", None, "clean"); diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 02d49e2e..6f9b261a 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -903,8 +903,9 @@ protected paths, plaintext. `gix::validate`), `.`, `..`, empty names and names containing `/`, `\` or NUL are refused. The check runs before the branch ref (and the mirror push) moves; blobs and subtrees written before the refusal stay in the target as - unreferenced objects, except that a repository this export created is removed - again (a pre-existing one is never touched). A commit reused through a mark is never rewritten, so it + unreferenced objects, and a repository the export created stays, with no refs + (it is never removed, since another export may be writing into it; a retry + points its unborn `HEAD` at the exported ref). A commit reused through a mark is never rewritten, so it is not judged, while anything export writes is, including a commit re-synthesized after its mark went stale. **Residual:** commits an older client already wrote into the mirror are pushed as they are, subject only to the From 1f4e7620cea2f5fd34b8a3bee00bf2a9d36df9a5 Mon Sep 17 00:00:00 2001 From: Toni Bergholm Date: Fri, 20 Nov 2026 11:32:11 +0200 Subject: [PATCH 16/16] fix(gitio): adopt only a bare, ref-less target with a symbolic HEAD --- crates/gitio/src/export.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index e3ca5186..a44e42ac 100644 --- a/crates/gitio/src/export.rs +++ b/crates/gitio/src/export.rs @@ -60,9 +60,9 @@ impl GitTarget { /// Open an existing Git repo at `path`; if `path` does not exist, create a /// bare repo there. A path that exists but is not a Git repo is an error. pub(crate) fn open_or_init_bare(path: &Path) -> Result { - // Claim the directory with an exclusive create: only a call that made it owns the - // repository (and may remove it on failure). Another process that wins the race, or a - // directory that already exists, makes this call treat the target as pre-existing. + // Claim the directory with an exclusive create: only a call that made it initializes the + // repository. Another process that wins the race, or a directory that already exists, + // makes this call treat the target as pre-existing. Nothing here ever removes a target. if let Some(parent) = path.parent().filter(|p| !p.as_os_str().is_empty()) { std::fs::create_dir_all(parent) .with_context(|| format!("creating {}", parent.display()))?; @@ -84,8 +84,8 @@ impl GitTarget { let repo = gix::open(path).with_context(|| { format!("{} exists but is not a git repository", path.display()) })?; - // A repo with no refs (for example one a refused export left behind) has an - // unborn HEAD: pointing it at our ref is what a fresh repo gets. + // A bare repo with no refs and a symbolic, unborn HEAD (what a refused export leaves + // behind) gets the HEAD a fresh repo would. A checkout, or a detached HEAD, keeps its own. let no_refs = repo .references() .context("listing refs")? @@ -93,13 +93,15 @@ impl GitTarget { .context("listing refs")? .next() .is_none(); - (repo, no_refs) + let symbolic_head = matches!(repo.head_name(), Ok(Some(_))); + let adopt = no_refs && repo.is_bare() && symbolic_head; + (repo, adopt) }; Ok(GitTarget { repo, created }) } /// Point `HEAD` at `ref_name` (a symbolic ref). Only meaningful on a repo we - /// created — a fresh `init_bare` HEAD may name `main`/`master` regardless of + /// created or a bare one with no refs — a fresh `init_bare` HEAD may name `main`/`master` regardless of /// our branch, so a mirror needs HEAD pointed at the ref we actually wrote or /// `git log`/`import_head` (which resolve HEAD) see nothing. pub(crate) fn set_head(&self, ref_name: &str) -> Result<()> {