Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 7 additions & 15 deletions ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -806,21 +806,13 @@ scale-&-reach horizon):
- **`--max-object-size` operator config knob (P28).** `MAX_OBJECT_SIZE`
(256 MiB) is a compile-time constant anchoring every untrusted-length
guard; letting an operator raise or lower it per-deployment is deferred.
- **P28 review follow-ons.** A whole-branch final review of the P28
security sweep closed the one real client-side DoS gap
(`wire::decode_refs_body`'s fabricated-count allocation) and corrected
stale docs, but named four small non-blocking items, deferred rather
than silently dropped: `refs::write_head`/`refs::delete_branch` gaining
a one-line `validate_branch_name` call for ref-validation class
completeness (not exploitable today — `HEAD`'s path is fixed and
`delete_branch` only takes internally-generated names); an
`SC_PACK_CHUNK` upper-clamp to `MAX_OBJECT_SIZE` (an oversized chunk
config today produces frames the receiver's cap rejects — a
self-inflicted confusing failure rather than a clear one);
`Repo::worktree_paths` doing a paths-only walk instead of loading every
file's full bytes; and extracting a shared `path_under_prefix(path,
prefix)` helper so `run_protect`'s `/`-boundary filter and
`protect.rs::matching_prefix` stop duplicating the same rule.
- **P28 review follow-ons — shipped.** `refs::write_head`/`delete_branch` run
`validate_branch_name`; `SC_PACK_CHUNK` is clamped to `MAX_OBJECT_SIZE - 1` (a chunk frame carries a tag byte too; an
oversized value used to make every transfer fail with a confusing receiver
error); `Repo::worktree_paths` is a paths-only walk (`list_worktree_paths`,
the same ignore and tracked-path rules as `read_worktree`, no file read); and
the `/`-boundary rule is one shared `protect::path_under_prefix` used by rule
lookup and the `sc protect` low-entropy nudge.
- **Per-path/per-ref ACLs (P29).** `sc serve --http` access control is
all-or-nothing per connection (`ro`/`rw`); scoping a token to specific
path prefixes or refs is beyond the MVP. Deferred.
Expand Down
4 changes: 1 addition & 3 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4241,10 +4241,8 @@ fn run_protect(prefix: Option<String>, to: Vec<String>, list: bool, json: bool)
// branch (where `worktree_paths` can't read a sealed tree). `repo.protect`
// below then refuses with the authoritative "sc protect" message.
if repo.head_private()?.is_none() {
let boundary_prefix = prefix.trim_end_matches('/');
let boundary_dir_prefix = format!("{boundary_prefix}/");
if let Some(path) = repo.worktree_paths()?.into_iter().find(|path| {
(path == boundary_prefix || path.starts_with(&boundary_dir_prefix))
scl_repo::protect::path_under_prefix(path, &prefix)
&& looks_like_low_entropy_secret(path.rsplit('/').next().unwrap_or(path))
}) {
eprintln!("warning: {path} looks like a low-entropy secret; convergent encryption (sc protect) is equality-confirmable — prefer 'sc secret' for API keys / .env / credentials (see ADR-0014).");
Expand Down
31 changes: 24 additions & 7 deletions crates/repo/src/protect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,23 @@ pub fn matching_prefix<'a>(protection: &'a Protection, path: &str) -> Option<&'a
protection
.prefixes
.iter()
.filter(|p| {
// Match only at a path boundary: a path is governed by a prefix iff it
// equals the prefix's bare form or lies under it at a `/` boundary.
// `starts_with` alone would over-match (e.g. `secret` -> `secretstuff`).
let bare = p.prefix.trim_end_matches('/');
path == bare || path.starts_with(&format!("{bare}/"))
})
.filter(|p| path_under_prefix(path, &p.prefix))
.max_by_key(|p| p.prefix.len())
}

/// Is `path` governed by `prefix`? Matches only at a path boundary: a path is
/// under a prefix iff it equals the prefix's bare form (trailing `/` stripped)
/// or lies under it at a `/` boundary. `starts_with` alone would over-match
/// (`secret` -> `secretstuff`). The one rule `sc protect`, its CLI nudge and
/// rule lookup share.
pub fn path_under_prefix(path: &str, prefix: &str) -> bool {
let bare = prefix.trim_end_matches('/');
path == bare
|| path
.strip_prefix(bare)
.is_some_and(|rest| rest.starts_with('/'))
}

/// Merge two protection policies' prefix rules. Prefixes unite by `prefix`
/// string (rules never disappear — fail-closed, ADR-0025). Within a shared
/// prefix each recipient key is a last-writer-wins register: the higher-epoch
Expand Down Expand Up @@ -419,4 +426,14 @@ mod tests {
let m = merge_prefixes(&a, &b);
assert!(m[0].granted_keys().is_empty());
}

#[test]
fn path_under_prefix_matches_at_a_boundary_only() {
assert!(path_under_prefix("secret", "secret/"));
assert!(path_under_prefix("secret/a/b", "secret/"));
assert!(path_under_prefix("secret/a", "secret"));
assert!(!path_under_prefix("secretstuff", "secret/"));
assert!(!path_under_prefix("secretstuff/x", "secret"));
assert!(!path_under_prefix("other/secret", "secret"));
}
}
29 changes: 29 additions & 0 deletions crates/repo/src/refs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const HEAD_PREFIX: &str = "ref: refs/heads/";

/// Write `HEAD` as a symbolic ref to `branch`.
pub fn write_head(layout: &Layout, branch: &str) -> Result<()> {
// Same grammar gate as every other ref write: HEAD must never name a
// branch the rest of the code could not have created.
crate::repo::validate_branch_name(branch)?;
atomic_write(
&layout.head_path(),
format!("{HEAD_PREFIX}{branch}\n").as_bytes(),
Expand Down Expand Up @@ -128,6 +131,9 @@ fn is_unsafe_ref_component(s: &str) -> bool {
/// names — that would leave `HEAD` dangling — and errors `NoSuchBranch` if
/// the ref is already absent.
pub fn delete_branch(layout: &Layout, name: &str) -> Result<()> {
// `ref_path` joins `name` onto the refs directory: validate first so a
// crafted name can never reach outside it.
crate::repo::validate_branch_name(name)?;
if current_branch(layout)? == name {
return Err(Error::InvalidArgument(format!(
"refusing to delete the current branch: {name}"
Expand Down Expand Up @@ -406,4 +412,27 @@ mod tests {
let _l2 = RepoLock::acquire(&layout).unwrap(); // freed
std::fs::remove_dir_all(&layout.root).unwrap();
}

#[test]
fn write_head_and_delete_branch_validate_the_branch_name() {
let root = std::env::temp_dir().join(format!("scl-refs-validate-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
let repo = crate::repo::Repo::init(&root).unwrap();
let layout = repo.layout().clone();
for bad in ["../escape", "a/b", "", "with space", ".hidden"] {
assert!(write_head(&layout, bad).is_err(), "write_head({bad:?})");
assert!(
delete_branch(&layout, bad).is_err(),
"delete_branch({bad:?})"
);
}
assert_eq!(
current_branch(&layout).unwrap(),
"main",
"HEAD is untouched"
);
drop(repo);
std::fs::remove_dir_all(&root).unwrap();
assert!(!root.exists());
}
}
7 changes: 1 addition & 6 deletions crates/repo/src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1116,12 +1116,7 @@ impl Repo {
// set to enumerate without decrypting. Callers that need one on a
// private branch must publish first.
self.refuse_on_private("listing the working-tree paths")?;
Ok(
worktree::read_worktree(&self.layout, &self.tracked_paths()?)?
.into_iter()
.map(|(path, _bytes, _mode)| path)
.collect(),
)
worktree::list_worktree_paths(&self.layout, &self.tracked_paths()?)
}

/// Line-level unified diff of the working tree against HEAD (`sc diff`).
Expand Down
37 changes: 35 additions & 2 deletions crates/repo/src/wire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,22 @@ pub const CHUNK_SIZE: usize = 1 << 20;
/// sender, the client's `PutPack` sender) call this, so setting the var
/// before either side starts affects the whole round trip.
pub fn pack_chunk_size() -> usize {
std::env::var("SC_PACK_CHUNK")
.ok()
chunk_from(std::env::var("SC_PACK_CHUNK").ok().as_deref())
}

/// The `SC_PACK_CHUNK` decision, pure so it can be tested without touching the
/// process environment: an unset, unparsable or zero value is the default, and
/// anything larger than a frame can carry is clamped. A chunk frame is the
/// `ST_PACK_CHUNK` tag byte plus the data, and the receiver refuses a frame
/// above `MAX_OBJECT_SIZE`, so the data is capped one byte below it; an
/// oversized setting would otherwise fail every large transfer with a
/// confusing error.
fn chunk_from(value: Option<&str>) -> usize {
value
.and_then(|v| v.parse::<usize>().ok())
.filter(|&n| n > 0)
.unwrap_or(CHUNK_SIZE)
.min(scl_core::MAX_OBJECT_SIZE - 1)
}

// Wire error codes. Typed errors that sync logic relies on get their own code;
Expand Down Expand Up @@ -1859,6 +1870,28 @@ mod tests {
let _ = std::fs::remove_dir_all(&root);
}

#[test]
fn chunk_from_defaults_and_clamps_to_the_object_cap() {
assert_eq!(chunk_from(None), CHUNK_SIZE);
assert_eq!(chunk_from(Some("")), CHUNK_SIZE);
assert_eq!(chunk_from(Some("0")), CHUNK_SIZE);
assert_eq!(chunk_from(Some("junk")), CHUNK_SIZE);
assert_eq!(chunk_from(Some("4096")), 4096);
// A chunk frame carries a tag byte too: data at the cap would make a
// frame one byte over what the receiver accepts.
let cap = scl_core::MAX_OBJECT_SIZE - 1;
assert_eq!(
chunk_from(Some(&(scl_core::MAX_OBJECT_SIZE * 8).to_string())),
cap,
"an oversized setting is clamped, not passed through"
);
assert_eq!(
chunk_from(Some(&scl_core::MAX_OBJECT_SIZE.to_string())),
cap
);
assert_eq!(chunk_from(Some(&cap.to_string())), cap);
}

#[test]
fn validate_max_pack_size_floor() {
assert!(validate_max_pack_size(0).is_ok());
Expand Down
85 changes: 85 additions & 0 deletions crates/repo/src/worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,59 @@ pub fn read_worktree(
Ok(out)
}

/// The paths [`read_worktree`] would return, without reading a byte of any file:
/// the same walk, ignore rules and tracked-path exemption, for callers that
/// only need the list (`Repo::worktree_paths`).
pub fn list_worktree_paths(layout: &Layout, tracked: &BTreeSet<String>) -> Result<Vec<String>> {
let ignore = Ignore::load(&layout.root)?;
let mut out = Vec::new();
walk_disk_paths(
&layout.root,
&layout.root,
&layout.dot_sc,
&ignore,
tracked,
&mut out,
)?;
out.sort();
Ok(out)
}

fn walk_disk_paths(
base: &Path,
dir: &Path,
skip: &Path,
ignore: &Ignore,
tracked: &BTreeSet<String>,
out: &mut Vec<String>,
) -> Result<()> {
for entry in std::fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path == skip {
continue;
}
let ft = entry.file_type()?;
let rel = path
.strip_prefix(base)
.unwrap()
.to_string_lossy()
.replace('\\', "/");
if ft.is_dir() {
if ignore.matches(&rel) && !tracked_under(tracked, &rel) {
continue;
}
walk_disk_paths(base, &path, skip, ignore, tracked, out)?;
} else if ft.is_file() {
if ignore.matches(&rel) && !tracked.contains(&rel) {
continue;
}
out.push(rel);
}
}
Ok(())
}

fn walk_disk(
base: &Path,
dir: &Path,
Expand Down Expand Up @@ -1295,4 +1348,36 @@ mod tests {
std::fs::remove_dir_all(&layout.root).unwrap();
assert!(!layout.root.exists());
}

/// The paths-only walk lists exactly what the reading walk returns, honoring
/// `.scignore` and the tracked-path exemption, without reading file bytes.
#[test]
fn list_worktree_paths_matches_read_worktree() {
let root = std::env::temp_dir().join(format!("scl-listpaths-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(root.join(".sc")).unwrap();
std::fs::create_dir_all(root.join("a/b")).unwrap();
std::fs::create_dir_all(root.join("ignored")).unwrap();
std::fs::write(root.join(".scignore"), "ignored/\n*.tmp\n").unwrap();
std::fs::write(root.join("a/b/f.txt"), b"1").unwrap();
std::fs::write(root.join("a/skip.tmp"), b"2").unwrap();
std::fs::write(root.join("ignored/keep.txt"), b"3").unwrap();
std::fs::write(root.join("ignored/drop.txt"), b"4").unwrap();
std::fs::write(root.join("top.txt"), b"5").unwrap();
let layout = Layout::at(&root);
// A tracked path under an ignored directory is still listed.
let tracked: BTreeSet<String> = ["ignored/keep.txt".to_string()].into();
let listed = list_worktree_paths(&layout, &tracked).unwrap();
let read: Vec<String> = read_worktree(&layout, &tracked)
.unwrap()
.into_iter()
.map(|(p, _, _)| p)
.collect();
assert_eq!(listed, read);
assert!(listed.contains(&"ignored/keep.txt".to_string()));
assert!(!listed.contains(&"ignored/drop.txt".to_string()));
assert!(!listed.contains(&"a/skip.tmp".to_string()));
std::fs::remove_dir_all(&root).unwrap();
assert!(!root.exists());
}
}
Loading