diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 48b26201..d6b0f3d6 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_line(&snap.author), + scl_core::sanitize_display_line(&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_line(&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_line(agent), transcript_sig_label(status) )); } @@ -3729,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()))?, @@ -4879,6 +4921,37 @@ 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 { + 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\\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] fn default_budget_warning_logic() { assert!(runs_default_budget(false, None)); diff --git a/crates/cli/tests/git_remote.rs b/crates/cli/tests/git_remote.rs index 7c71be6c..7d3e03bb 100644 --- a/crates/cli/tests/git_remote.rs +++ b/crates/cli/tests/git_remote.rs @@ -340,3 +340,56 @@ 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://). 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"); + 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/core/src/display.rs b/crates/core/src/display.rs new file mode 100644 index 00000000..4c743d9c --- /dev/null +++ b/crates/core/src/display.rs @@ -0,0 +1,75 @@ +//! 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 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> { + 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); + } + 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 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/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/core/src/lib.rs b/crates/core/src/lib.rs index ae2bae50..9267e57e 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, sanitize_display_line}; pub use error::{Error, Result}; pub use id::ObjectId; pub use object::{ 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"; diff --git a/crates/gitio/src/export.rs b/crates/gitio/src/export.rs index 7a210f5b..a44e42ac 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, } @@ -60,25 +60,48 @@ 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() { - ( - gix::open(path).with_context(|| { - format!("{} exists but is not a git repository", path.display()) - })?, - false, - ) - } else { + // 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()))?; + } + 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 { + let repo = gix::open(path).with_context(|| { + format!("{} exists but is not a git repository", path.display()) + })?; + // 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")? + .all() + .context("listing refs")? + .next() + .is_none(); + 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<()> { @@ -253,6 +276,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 { @@ -447,6 +478,28 @@ fn count_transcripts( Ok(count) } +/// 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. 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<()> { + 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" + ); + 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 @@ -510,8 +563,9 @@ 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 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 @@ -1445,4 +1499,261 @@ 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", + "gi7eba~9", + "gi7eb~10", + "gi7eb~12", + "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", + // 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"); + } + } + + #[test] + 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. + 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 = |ref_name| ExportOptions { + to: &dir, + ref_name, + include_encrypted: false, + known_git_commits: &empty, + }; + // 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:#}" + ); + // 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"); + 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(); + } + } + + #[test] + 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-link-{}", 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("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 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(); + // 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| { + 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. + 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"), + "{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()); + // 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(); + } } diff --git a/crates/repo/src/error.rs b/crates/repo/src/error.rs index a5c82a7c..eb9807ec 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, @@ -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_line(.0))] Protocol(String), - #[error("connection to remote lost: {0}")] + #[error("connection to remote lost: {}", scl_core::sanitize_display_line(.0))] ConnectionLost(String), - #[error("remote error: {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: {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: {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. @@ -226,3 +226,30 @@ 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()), + Error::BadRef(raw.into()), + Error::InvalidArgument(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)); + } +} 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"); diff --git a/crates/repo/src/wire.rs b/crates/repo/src/wire.rs index a67498f4..cdd5d41f 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,48 @@ 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 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 9096cbfa..64295074 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -150,7 +150,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 @@ -480,6 +488,19 @@ 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`) 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); 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 @@ -893,6 +914,24 @@ 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 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, 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 + 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 + git's own checkout and `fsck` handle. ## sc-forge: authentication, tenancy, transport, resources — P38 F1/F2