diff --git a/ARCHITECTURE.md b/ARCHITECTURE.md index 93f38052..465bad23 100644 --- a/ARCHITECTURE.md +++ b/ARCHITECTURE.md @@ -989,3 +989,41 @@ dedups. The historical caveat stands forever: pre-P33 convergent ciphertext already in history stays equality-confirmable (rotation ≠ erasure, ADR-0019). See ADR-0043 and the crypto sections above, which describe the current sealing model in full. + +## Phase 34 — per-branch access control: private branches (built) + +The last thesis pillar: staging a fix (canonically an embargoed CVE) on a +branch that is fully opaque to non-recipients until a coordinated release. +Where P7/P33 seal blob *content*, a private branch seals *everything* the +branch introduces — snapshots, trees (so file paths), and blobs — as +individually-encrypted `Object::Sealed` bodies (fresh random DEK each, +distinct AAD from path blobs), and the branch ref points at a +`BranchManifest` rather than a snapshot. The manifest's only plaintext is +structural: a flat sorted list of the sealed-object ids in the branch's +closure, the public fork point, and the KEK wraps — so gc and both +transports get one rule each ("manifest reachable ⇒ every listed sealed id +reachable"; "diff the flat list against the peer's haves") without ever +decrypting. Everything else — the `inner id → (sealed id, DEK)` map and the +inner tip — lives in a `BranchIndex` encrypted under a per-branch KEK, itself +wrapped per recipient + escrow with the P2 X25519 envelope. + +Sealing is **copy-on-write**: an inner id absent from the index resolves to a +public object read straight from the store, so a branch forked from `main` +seals nothing at creation and reseals only what actually changes — an +unchanged subtree keeps its public id. Grant is one KEK wrap (no object +churn); revoke mints a fresh KEK, re-encrypts the index, and rewraps for the +remaining recipients — zero content plaintext, zero sealed-object churn — but +a recipient who already fetched keeps the old manifest (rotation ≠ erasure, +the ADR-0019 boundary). Publish replays the sealed history as plaintext +public snapshots (messages/authors/timestamps preserved, parents remapped, +fork point unchanged) and moves the ref to the published tip; object ids +necessarily change on publish (a sealed id is BLAKE3(ciphertext), a public id +BLAKE3(plaintext) — equal ids would reopen the P33 oracle), so published +commits start unsigned, and the P5 scanner runs over all decrypted content +before any public object is written. The git bridge refuses private branches +unconditionally (there is no git structure to export — it is what's sealed), +integrating a private branch into a public one is refused everywhere except +publish, and filtered clones exclude private branches (sealed trees can't be +path-filtered). Two additive object tags cross the wire, so +`PROTOCOL_VERSION` bumps 3 → 4; no existing object's format changes. See +ADR-0044. diff --git a/CLAUDE.md b/CLAUDE.md index ae62b5ce..a369f10d 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -118,7 +118,35 @@ cargo run --bin sc -- log # history: id, date, author, messag # (--json for scripts; also on secret list) # commit/merge author: --author > $SC_AUTHOR > OS user cargo run --bin sc -- branch # create a new branch at current tip -cargo run --bin sc -- switch # switch branch + materialize working tree +cargo run --bin sc -- branch --private --to ... [--identity ] + # create a PRIVATE branch (P34): commits/trees/ + # blobs sealed to the recipient set (creator + + # escrow always wrapped in); opaque to non- + # recipients until publish; flat name only + # (grammar unchanged); KEEP identity files OUTSIDE + # the working tree (a committed key is sealed in + # and vanishes on the next switch) +cargo run --bin sc -- branch grant --to --identity # wrap the branch + # KEK for another recipient — O(1), no object churn +cargo run --bin sc -- branch revoke --recipient-id --identity # revoke + + # atomically rotate the KEK + rewrap for everyone + # remaining (zero content plaintext, zero id churn; + # a revoked recipient keeps what they already + # fetched — rotation ≠ erasure) +cargo run --bin sc -- branch publish --identity # replay the sealed history as + # PUBLIC commits (messages/authors kept, new ids by + # construction, published commits start unsigned); + # scanner runs over decrypted content before any + # public write; branch becomes ordinary + public +cargo run --bin sc -- branch list [--json] [--identity ] # list branches; private ones + # marked (private) / (private, no access) +cargo run --bin sc -- switch [--identity ] # switch branch + materialize working + # tree; a private branch needs a recipient --identity +cargo run --bin sc -- status --identity # (and diff/log) need --identity on a private branch +cargo run --bin sc -- merge --identity # ON a private branch: merges the + # public branch IN (keeps an embargo current); the + # reverse (merge/cherry-pick/rebase FROM a private + # branch) is refused — publish first cargo run --bin sc -- merge [--identity ] # three-way merge (ff when possible; exits # 1 on conflicts; --identity only needed when a # protected path diverged in content on both sides) @@ -384,6 +412,17 @@ cargo run --bin sc -- clone sc+https://host[:port]/repo # TLS clone (P32 bash demo/run_tls_demo.sh # sc+https proof (P32): TLS round trip w/ signed # chunked blob, TOFU pin/mismatch/strict/pre-pin, # tightened plaintext gate — run twice +bash demo/run_private_branch_demo.sh # private branch proof (P34): alice stages an + # embargoed fix on a private branch, keeps it + # current by merging main IN; a keyless clone gets + # ciphertext + a name marker only (content/paths/ + # message opaque, every read surface refuses); + # grant admits bob, revoke rotates the KEK so a + # post-revoke clone can't open it while bob's pre- + # revoke clone still can (rotation ≠ erasure); + # private→public integration + git export refuse; + # publish flips it public atomically — run twice, + # zero residue ``` Set `CARGO_TARGET_DIR` to a path outside this folder to keep `target/` out of @@ -391,7 +430,7 @@ the project tree if desired. ## Capability map (what's built, by phase) -All 33 phases are built and tested. One line of current fact per phase; the +All 34 phases are built and tested. One line of current fact per phase; the authoritative rationale and full semantics live in the linked ADR, the design in `ARCHITECTURE.md`. The old per-phase narrative log this table replaced is archived verbatim at `docs/archive/claude-md-phase-log-2026-07.md` — do not @@ -433,6 +472,7 @@ the code, those win. | P31 | Listener resource limits: `--max-connections`, `--timeout`, `--max-pack-size` (both transports), capped read-only drain, accept backoff | [0041](docs/adr/0041-listener-resource-limits.md) | | P32 | In-binary TLS: `sc+https://` via leaf crate `tlsio`; accept-new TOFU pinning; public plaintext bind no longer justified by tokens alone | [0042](docs/adr/0042-in-binary-tls-sc-https.md) | | P33 | Randomized protected sealing (fresh DEK + nonce; `RANDOMIZED` perms bit); dual-read of pre-P33 convergent ciphertext; per-checkout keyed stat cache; `sc rewrap` upgrades convergent blobs at the tip | [0043](docs/adr/0043-randomized-protected-encryption.md) | +| P34 | Private branches: ref points at a sealed-branch manifest; every commit/tree/blob individually sealed (copy-on-write) under a per-branch KEK wrapped per recipient + escrow; `sc branch --private/grant/revoke/publish`; opaque to non-recipients (content, paths, messages); grant O(1), revoke rotates the KEK; publish replays to public with a scanner gate; git bridge + private→public integration refused; `PROTOCOL_VERSION` 4 | [0044](docs/adr/0044-per-branch-access-control.md) | ## Standing boundaries & gotchas @@ -440,7 +480,8 @@ Cross-cutting current facts that bite. Security boundaries are consolidated in `docs/THREAT-MODEL.md` — read it before touching anything crypto- or transport-adjacent. The rest, imperatively: -- **Wire protocol is version 3.** `MAX_OBJECT_SIZE` (256 MiB) guards the +- **Wire protocol is version 4** (bumped from 3 in P34 for the two additive + sealed-branch object kinds). `MAX_OBJECT_SIZE` (256 MiB) guards the transfer path only — a larger blob commits locally but fails every subsequent push/fetch/clone at the receiver. - **Partial clones refuse merge, cherry-pick/rebase, `sc ws fork`/`harvest`, @@ -467,7 +508,22 @@ transport-adjacent. The rest, imperatively: fetch/push/clone. `sc undo` twice = redo; undoing the initial commit is refused. - **Local branch names are flat** — the ref grammar reserves `name/branch` for - remote-tracking refs. + remote-tracking refs. (Private branches obey the same grammar — the demo + uses `hotfix-CVE-1234`, not `hotfix/CVE-1234`.) +- **Private branches (P34) are the one branch kind whose ref points at a + manifest, not a snapshot.** They are opaque to non-recipients (content, + paths, messages, DAG shape); only the branch name, sealed-object count/ + sizes, recipient ids, and public fork point leak. `commit`/`status`/`diff`/ + `log`/`switch` need `--identity`; every snapshot-assuming op (`amend`, + `protect`, `secret *`, `rewrap`, `sparse *`, `ws`/`work`, `transcript + attach`, `sign`, creating a child branch) refuses on a private branch; and + integrating a private branch INTO a public one (merge/cherry-pick/rebase + from it, or git export/push) is refused everywhere except `sc branch + publish`. Merging a public branch IN is allowed (keeps an embargo current). + `sc branch revoke` rotates the KEK but a recipient who already fetched keeps + the old manifest (rotation ≠ erasure). **Keep identity files outside the + working tree** — a key committed under a private branch is sealed in and + vanishes on the next switch. - **A signature binds identity to a snapshot id, not a branch position.** `amend`/`rebase`/`merge` results start unsigned by design — re-sign after (`sc sign `). diff --git a/ROADMAP.md b/ROADMAP.md index b7a3745f..a6e15f37 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -898,6 +898,22 @@ scale-&-reach horizon): `sc remote add` parse-validating `sc+http(s)://` URLs at add time, the way `ssh://` already does, instead of deferring the parse to first use. +- **P34 follow-ons (private branches, ADR-0044).** Deferred rather than + built: (1) `sc branch publish --squash` — collapse the sealed history into + one public commit at release, hiding intermediate (embargoed) commit + messages that publish otherwise makes public; (2) `--no-escrow` at + private-branch creation, for an operator who deliberately wants no + break-glass reader of an embargo; (3) sealing the manifest's currently- + plaintext metadata (recipient ids and the public fork point) — needs an + anonymous-recipient KEK-wrap scheme, a real design effort; (4) manifest + scalability past hotfix-scale closures (the flat closure list is rewritten + per commit — fine for a short embargo branch, not for a long-lived one); + (5) slash-names for local branches so `hotfix/CVE-1234` parses (an + independent ref-grammar change, unrelated to access control); and (6) a + fetch-time freshness attestation so a hostile remote can't re-serve a + stale pre-revoke manifest — the same gittuf-shaped signed-ref effort the + P22 provenance boundary already defers. + ## How a phase gets built 1. Focused brainstorm for the phase (this skill) → phase spec. diff --git a/crates/cli/src/main.rs b/crates/cli/src/main.rs index 26b9253b..2161136c 100644 --- a/crates/cli/src/main.rs +++ b/crates/cli/src/main.rs @@ -84,17 +84,50 @@ enum Cmd { /// Emit machine-readable JSON instead of the human listing. #[arg(long)] json: bool, + /// Recipient identity — required when the current branch is private + /// (default `--identity`/`SC_IDENTITY`/`~/.sc/identity`). + #[arg(long)] + identity: Option, }, /// Show line-level working-tree changes against HEAD (unified diff). - Diff, + Diff { + /// Recipient identity — required when the current branch is private + /// (default `--identity`/`SC_IDENTITY`/`~/.sc/identity`). + #[arg(long)] + identity: Option, + }, /// Show commit history from HEAD. Log { /// Emit machine-readable JSON instead of the human listing. #[arg(long)] json: bool, + /// Recipient identity — required when the current branch is private + /// (default `--identity`/`SC_IDENTITY`/`~/.sc/identity`). + #[arg(long)] + identity: Option, + }, + /// Create a new branch at the current tip (add --private to seal it to a + /// recipient set, ADR-0044), or manage private branches via subcommands. + #[command(args_conflicts_with_subcommands = true)] + Branch { + #[command(subcommand)] + op: Option, + /// Branch name to create. + name: Option, + /// Create a PRIVATE branch: commits/trees/blobs sealed to the + /// recipient set; opaque to everyone else until `sc branch publish`. + #[arg(long, requires = "name")] + private: bool, + /// Recipient names (from `.sc/recipients.toml`) to seal the branch + /// to. The creator's identity is always wrapped in; so is the + /// configured escrow set. + #[arg(long, value_delimiter = ',', requires = "private")] + to: Vec, + /// Creator identity for --private (default + /// `--identity`/`SC_IDENTITY`/`~/.sc/identity`). + #[arg(long, requires = "private")] + identity: Option, }, - /// Create a new branch at the current tip. - Branch { name: String }, /// Switch HEAD to a branch and materialize it. Protected files decrypt when /// the resolved identity is a recipient, and are skipped otherwise. Switch { @@ -681,6 +714,61 @@ enum ServeTokenOp { }, } +#[derive(Subcommand)] +enum BranchOp { + /// Wrap the private branch's KEK for additional recipients — O(1) per + /// recipient, no object churn (P34, ADR-0044). + Grant { + /// The private branch. + name: String, + /// Recipient names (from `.sc/recipients.toml`). + #[arg(long, value_delimiter = ',')] + to: Vec, + /// An authorized recipient identity (required). + #[arg(long)] + identity: Option, + }, + /// Revoke a recipient AND atomically rotate the branch KEK + rewrap for + /// everyone remaining — zero content plaintext, zero object-id churn. + /// A revoked recipient keeps what they already fetched (rotation ≠ + /// erasure); they can read nothing sealed after this. + Revoke { + /// The private branch. + name: String, + /// Recipient id (from `sc keygen` / `sc branch list`). + #[arg(long)] + recipient_id: String, + /// An authorized recipient identity (required — the rewrap must + /// unwrap the old KEK). + #[arg(long)] + identity: Option, + }, + /// Replay the private branch's history as plaintext public commits + /// (messages/authors/timestamps preserved, new snapshot ids by + /// construction) and move the branch ref to the published tip. The + /// secret scanner runs over all decrypted content BEFORE anything + /// public is written. Published commits start unsigned — re-sign after. + Publish { + /// The private branch. + name: String, + /// A recipient identity (required — publish decrypts everything). + #[arg(long)] + identity: Option, + }, + /// List branches; private ones are marked `(private)` or + /// `(private, no access)` depending on whether the resolved identity + /// can open them. + List { + /// Emit machine-readable JSON instead of the human listing. + #[arg(long)] + json: bool, + /// Identity used only to test access for the marker (default + /// `--identity`/`SC_IDENTITY`/`~/.sc/identity`; missing is fine). + #[arg(long)] + identity: Option, + }, +} + #[derive(Subcommand)] enum SecretOp { /// Seal a value (read from --value or stdin) to named recipients. @@ -787,10 +875,16 @@ fn main() -> Result<()> { sign, identity, } => run_amend(&resolve_author(author), message, sign, identity), - Cmd::Status { json } => run_status(json), - Cmd::Diff => run_diff(), - Cmd::Log { json } => run_log(json), - Cmd::Branch { name } => run_branch(&name), + Cmd::Status { json, identity } => run_status(json, identity), + Cmd::Diff { identity } => run_diff(identity), + Cmd::Log { json, identity } => run_log(json, identity), + Cmd::Branch { + op, + name, + private, + to, + identity, + } => run_branch(op, name, private, &to, identity), Cmd::Switch { name, identity } => run_switch(&name, identity), Cmd::Conflicts { path, @@ -1620,6 +1714,28 @@ fn sign_and_report( fn run_commit(author: &str, message: &str, sign: bool, identity: Option) -> Result<()> { let repo = open_repo()?; + if repo.head_private()?.is_some() { + // Private branch (P34): the commit seals through the branch KEK. + if sign { + anyhow::bail!( + "--sign is not supported on a private branch (signatures bind public \ + snapshot ids; publish first, then `sc sign`)" + ); + } + let sk = resolve_identity_opt(identity)?; + match repo.commit_private(author, message, sk.as_ref()) { + Ok(id) => { + println!("committed {} (private)", id.short()); + return Ok(()); + } + Err(scl_repo::Error::SecretDetected(report)) => { + drop(repo); + eprint!("{report}"); + std::process::exit(1); + } + Err(e) => return Err(e.into()), + } + } match repo.commit(author, message) { Ok(id) => { println!("committed {}", id.short()); @@ -1681,10 +1797,10 @@ fn run_scan() -> Result<()> { std::process::exit(1); } -fn run_status(json: bool) -> Result<()> { +fn run_status(json: bool, identity: Option) -> Result<()> { let repo = open_repo()?; if json { - let s = repo.status()?; + let s = repo_status(&repo, identity.clone())?; let conflicts = conflicts_json(&repo)?; let sparse = repo.sparse_spec()?; println!( @@ -1749,7 +1865,7 @@ fn run_status(json: bool) -> Result<()> { } } } - let s = repo.status()?; + let s = repo_status(&repo, identity)?; let sparse = repo.sparse_spec()?; if !sparse.prefixes().is_empty() { println!("sparse: {}", sparse.prefixes().join(", ")); @@ -2157,9 +2273,14 @@ fn render_transcript_marker_line( } } -fn run_log(json: bool) -> Result<()> { +fn run_log(json: bool, identity: Option) -> Result<()> { let repo = open_repo()?; - let entries = repo.log()?; + let entries = if repo.head_private()?.is_some() { + let sk = resolve_identity_opt(identity)?; + repo.log_private(sk.as_ref())? + } else { + repo.log()? + }; let trust = load_trust_map(&repo.layout().dot_sc.join("recipients.toml"))?; // Pre-compute every commit's signature status up front, before any // printing starts. `sig_status` does disk I/O against the signature @@ -2239,12 +2360,28 @@ fn print_line(line: &str) { } /// Show line-level working-tree changes against HEAD. -fn run_diff() -> Result<()> { +fn run_diff(identity: Option) -> Result<()> { let repo = open_repo()?; - print!("{}", repo.diff_unified()?); + if repo.head_private()?.is_some() { + let sk = resolve_identity_opt(identity)?; + print!("{}", repo.diff_unified_private(sk.as_ref())?); + } else { + print!("{}", repo.diff_unified()?); + } Ok(()) } +/// Status, dispatching to the private-branch variant (which needs a +/// recipient identity) when HEAD is private. +fn repo_status(repo: &scl_repo::Repo, identity: Option) -> Result { + if repo.head_private()?.is_some() { + let sk = resolve_identity_opt(identity)?; + Ok(repo.status_private(sk.as_ref())?) + } else { + Ok(repo.status()?) + } +} + /// Resolve the commit/merge author: explicit `--author`, then `$SC_AUTHOR`, /// then the OS username, then the historical `"you"` placeholder. fn resolve_author(flag: Option) -> String { @@ -2287,9 +2424,168 @@ fn fmt_utc(ts: i64) -> String { ) } -fn run_branch(name: &str) -> Result<()> { - open_repo()?.branch(name)?; - println!("created branch {name}"); +fn run_branch( + op: Option, + name: Option, + private: bool, + to: &[String], + identity: Option, +) -> Result<()> { + match op { + Some(BranchOp::Grant { name, to, identity }) => run_branch_grant(&name, &to, identity), + Some(BranchOp::Revoke { + name, + recipient_id, + identity, + }) => run_branch_revoke(&name, &recipient_id, identity), + Some(BranchOp::Publish { name, identity }) => run_branch_publish(&name, identity), + Some(BranchOp::List { json, identity }) => run_branch_list(json, identity), + None => { + let name = name.ok_or_else(|| anyhow::anyhow!("branch name required"))?; + if !private { + open_repo()?.branch(&name)?; + println!("created branch {name}"); + return Ok(()); + } + let repo = open_repo()?; + let sk = load_identity(identity)?; + let recipients_path = repo.layout().dot_sc.join("recipients.toml"); + // A missing recipients file is fine when --to is empty (creator- + // only branch); resolving names of course still needs it. + let pks = if to.is_empty() { + Vec::new() + } else { + let dir = load_recipients(&recipients_path)?; + resolve_names(&dir, to)? + }; + let escrows = load_escrows(&recipients_path)?; + let mid = repo.branch_private(&name, &sk, &pks, &escrows)?; + let mut ids: Vec = vec![sk.public().recipient_id().to_string()]; + for pk in pks.iter().chain(escrows.iter()) { + let id = pk.recipient_id().to_string(); + if !ids.contains(&id) { + ids.push(id); + } + } + println!( + "created PRIVATE branch {name} (manifest {}, sealed to {} key(s) incl. creator + escrow)", + mid.short(), + ids.len() + ); + println!("note: content is opaque to non-recipients until `sc branch publish {name}`"); + Ok(()) + } + } +} + +fn run_branch_grant(name: &str, to: &[String], identity: Option) -> Result<()> { + let repo = open_repo()?; + let sk = load_identity(identity)?; + let recipients_path = repo.layout().dot_sc.join("recipients.toml"); + let dir = load_recipients(&recipients_path)?; + let pks = resolve_names(&dir, to)?; + if pks.is_empty() { + anyhow::bail!("pass at least one recipient with --to"); + } + for pk in &pks { + repo.branch_grant(name, &sk, pk)?; + println!("granted {} on {name}", pk.recipient_id()); + } + Ok(()) +} + +fn run_branch_revoke(name: &str, recipient_id: &str, identity: Option) -> Result<()> { + let repo = open_repo()?; + let sk = load_identity(identity)?; + let revoked = scl_crypto::RecipientId::from_hex(recipient_id) + .map_err(|_| anyhow::anyhow!("bad recipient id: {recipient_id}"))?; + // The rewrap needs a public key for every remaining recipient: pool the + // recipients file, the escrow set, and the caller's own identity, then + // resolve the manifest's current wrap list (minus the revoked id) + // against it. An unresolvable id is a loud error in `branch_revoke`, + // never silent access loss. + let (_, manifest) = repo + .branch_manifest(name)? + .ok_or_else(|| anyhow::anyhow!("branch {name} is not private"))?; + let recipients_path = repo.layout().dot_sc.join("recipients.toml"); + let mut pool: Vec = load_recipients(&recipients_path) + .map(|m| m.into_values().collect()) + .unwrap_or_default(); + pool.extend(load_escrows(&recipients_path)?); + pool.push(sk.public()); + let mut remaining: Vec = Vec::new(); + for wrap_id in manifest + .kek_wraps + .iter() + .map(|w| w.recipient_id.as_str()) + .filter(|id| *id != revoked.as_str()) + { + if let Some(pk) = pool.iter().find(|pk| pk.recipient_id().as_str() == wrap_id) { + if !remaining + .iter() + .any(|k| k.recipient_id() == pk.recipient_id()) + { + remaining.push(pk.clone()); + } + } + } + repo.branch_revoke(name, &sk, &revoked, &remaining)?; + println!( + "revoked {recipient_id} on {name}; branch KEK rotated + rewrapped for {} remaining key(s)", + remaining.len() + ); + println!("note: a revoked recipient keeps what they already fetched (rotation != erasure)"); + Ok(()) +} + +fn run_branch_publish(name: &str, identity: Option) -> Result<()> { + let repo = open_repo()?; + let sk = load_identity(identity)?; + let (tip, resealed) = repo.branch_publish(name, &sk)?; + println!("published {name} at {}", tip.short()); + if resealed > 0 { + println!("re-sealed {resealed} protected-path file(s) into public form"); + } + println!("note: published commits start unsigned — `sc sign {name}` to attest them"); + Ok(()) +} + +fn run_branch_list(json: bool, identity: Option) -> Result<()> { + let repo = open_repo()?; + let sk = resolve_identity_opt(identity)?; + let mut rows = Vec::new(); + for (name, is_current) in repo.branches()? { + let (private, access) = match repo.branch_manifest(&name) { + Ok(Some(_)) => { + let ok = repo.can_open_private(&name, sk.as_ref()).unwrap_or(false); + (true, ok) + } + _ => (false, true), + }; + rows.push((name, is_current, private, access)); + } + if json { + let arr: Vec<_> = rows + .iter() + .map(|(name, current, private, access)| { + serde_json::json!({ + "name": name, "current": current, + "private": private, "access": access, + }) + }) + .collect(); + println!("{}", serde_json::json!(arr)); + return Ok(()); + } + for (name, current, private, access) in rows { + let star = if current { "*" } else { " " }; + let marker = match (private, access) { + (false, _) => String::new(), + (true, true) => " (private)".into(), + (true, false) => " (private, no access)".into(), + }; + println!("{star} {name}{marker}"); + } Ok(()) } @@ -3444,7 +3740,19 @@ fn run_push(remote: &str, include_encrypted: bool) -> Result<()> { let repo = open_repo()?; let cfg = scl_repo::RemoteConfig::load(repo.layout())?; match cfg.kind(remote) { - Some(scl_repo::RemoteKind::Git) => run_push_git(&repo, remote, include_encrypted), + Some(scl_repo::RemoteKind::Git) => { + // The git bridge NEVER carries a private branch (ADR-0044 §7): + // there is no git structure to write — the structure is what's + // sealed — and `--include-encrypted` does not apply. + let branch = scl_repo::refs::current_branch(repo.layout())?; + if repo.is_private_branch(&branch)? { + anyhow::bail!( + "branch {branch} is private and cannot cross the git bridge; \ + `sc branch publish {branch}` first" + ); + } + run_push_git(&repo, remote, include_encrypted) + } _ => { let tip = repo.push(remote)?; println!("pushed to {remote}: {}", tip.short()); @@ -3651,13 +3959,18 @@ fn run_protect(prefix: Option, to: Vec, list: bool, json: bool) // served by `sc secret` than convergent `sc protect` (ADR-0014's // equality-confirmability caveat). Never blocks; protect proceeds // regardless. - 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)) - && 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)."); + // The nudge is cosmetic and reads the working tree; skip it on a private + // 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)) + && 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)."); + } } let recipients_path = repo.layout().dot_sc.join("recipients.toml"); @@ -3715,6 +4028,15 @@ fn run_export(to: PathBuf, ref_name: Option, include_encrypted: bool) -> ); } let branch = scl_repo::refs::current_branch(repo.layout())?; + // Private branches never export (ADR-0044 §7) — there is no git + // structure to write, and `--include-encrypted` does not apply (that + // flag covers protected paths, where the exported repo stays coherent). + if repo.is_private_branch(&branch)? { + anyhow::bail!( + "branch {branch} is private and cannot be exported to git; \ + `sc branch publish {branch}` first" + ); + } let tip = repo .head_tip()? .ok_or_else(|| anyhow::anyhow!("branch is unborn — nothing to export"))?; diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index a8da2b33..719daef0 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -25,7 +25,8 @@ pub const MAX_OBJECT_SIZE: usize = 256 * 1024 * 1024; // 256 MiB pub use error::{Error, Result}; pub use id::ObjectId; pub use object::{ - EntryKind, FileMode, Object, ProtectPrefix, Protection, RecipientEntry, RecipientState, Secret, - SignatureObj, Snapshot, Transcript, Tree, TreeEntry, WrappedKey, PROTECTED, RANDOMIZED, + BranchManifest, EntryKind, FileMode, Object, ProtectPrefix, Protection, RecipientEntry, + RecipientState, SealedObj, Secret, SignatureObj, Snapshot, Transcript, Tree, TreeEntry, + WrappedKey, PROTECTED, RANDOMIZED, }; pub use store::{Backend, SpillPolicy, Store, StoreConfig, StoreStats}; diff --git a/crates/core/src/object.rs b/crates/core/src/object.rs index 791daa9b..be1319ae 100644 --- a/crates/core/src/object.rs +++ b/crates/core/src/object.rs @@ -17,6 +17,8 @@ const TAG_SECRET: u8 = 3; const TAG_SNAPSHOT: u8 = 4; const TAG_SIGNATURE: u8 = 5; const TAG_TRANSCRIPT: u8 = 6; +const TAG_SEALED: u8 = 7; +const TAG_MANIFEST: u8 = 8; /// Perms-byte bit: this blob entry holds a `nonce‖ciphertext` envelope (an /// encrypted file), not plaintext. Set on protected-path entries (P7). @@ -211,6 +213,59 @@ pub struct Transcript { pub wrapped_keys: Vec, } +/// A sealed private-branch object (P34, ADR-0044): the canonical encoding of +/// some inner object (snapshot, tree, or blob) encrypted under a fresh random +/// per-object DEK — payload is `nonce(24) ‖ AEAD ciphertext`. Which inner kind +/// it holds, and the DEK that opens it, live only in the branch manifest's +/// encrypted index; to everyone else this is opaque bytes with a random-looking +/// content address. A distinct tag (not `Blob`) so ciphertext can never be +/// misread as plaintext file content. +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct SealedObj { + pub payload: Arc<[u8]>, +} + +/// A private branch's manifest (P34, ADR-0044) — the object a private branch's +/// ref points at instead of a snapshot. Plaintext *structure*, sealed content: +/// keyless parties (gc, transports) get exactly what they need — the closure +/// list and the public base — and nothing else. +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct BranchManifest { + /// The public fork-point snapshot. Kept plaintext so gc can keep the base + /// (and its closure) alive even if the public branch later rewrites it + /// away — publish and merge-base walks need it present. This is a + /// deliberate, documented metadata leak (the fork point is visible). + pub base: ObjectId, + /// The manifest this one supersedes (`None` at branch creation). Manifests + /// form the private branch's meta-history the way parents do for + /// snapshots: the fast-forward check on push walks this chain, and gc + /// keeps superseded manifests (and their closures) reachable exactly as + /// it keeps ancestor snapshots. + pub prev: Option, + /// Public snapshot roots the sealed trees reference but that are NOT + /// reachable from `base` — the tips merged in from public branches by + /// `merge_into_private` (P34). A merged-in public object is carried into + /// the sealed inner tree as an unsealed public reference (copy-on-write, + /// never re-sealed — it's already public), so without an explicit + /// reachability anchor a keyless party walking the manifest could never + /// reach it (it can't walk the sealed tree), and pushing just the private + /// branch to a peer lacking those public commits would strand the tree. + /// Cumulative across the branch's history and sorted (canonical). Leaks + /// which public commits were merged in — the same class of metadata as + /// `base` already leaking the fork point. + pub anchors: Vec, + /// Every sealed object id in the branch's closure, sorted (canonical). + /// gc: manifest reachable ⇒ all listed ids reachable. Transport: diff this + /// flat list against the peer's haves. Leaks count + sizes by design. + pub closure: Vec, + /// The branch index — `inner id -> (sealed id, DEK)` plus the inner tip — + /// encrypted under the branch KEK (`nonce ‖ AEAD`). Opaque to `core`; + /// `crates/crypto` owns the index codec and the KEK envelope. + pub index_ct: Vec, + /// The branch KEK wrapped per recipient (and escrow), P2-shape envelope. + pub kek_wraps: Vec, +} + /// Any object the store can hold. Blob bytes are `Arc`-shared so forking many /// worktrees off one snapshot never copies file content. #[derive(Clone, PartialEq, Eq, Debug)] @@ -221,6 +276,8 @@ pub enum Object { Secret(Secret), Signature(SignatureObj), Transcript(Transcript), + Sealed(SealedObj), + Manifest(BranchManifest), } impl Object { @@ -228,10 +285,13 @@ impl Object { Object::Blob(Arc::from(bytes.into().into_boxed_slice())) } - /// Bytes counted against the store's blob budget (0 for non-blobs). + /// Bytes counted against the store's blob budget (0 for small resident + /// kinds). Sealed payloads are blob-sized ciphertext and get the same + /// budget/eviction treatment as plaintext blobs. pub fn blob_size(&self) -> usize { match self { Object::Blob(b) => b.len(), + Object::Sealed(s) => s.payload.len(), _ => 0, } } @@ -244,6 +304,8 @@ impl Object { Object::Secret(_) => "secret", Object::Signature(_) => "signature", Object::Transcript(_) => "transcript", + Object::Sealed(_) => "sealed", + Object::Manifest(_) => "manifest", } } @@ -350,6 +412,41 @@ impl Object { w.bytes(&wk.wrapped_dek); } } + Object::Sealed(s) => { + w.tag(TAG_SEALED); + w.raw(&s.payload); + } + Object::Manifest(m) => { + w.tag(TAG_MANIFEST); + w.id(&m.base); + match &m.prev { + Some(p) => { + w.u8(1); + w.id(p); + } + None => w.u8(0), + } + // Sort anchors then closure so the same logical set hashes + // identically regardless of insertion order. + let mut anchors = m.anchors.clone(); + anchors.sort_unstable_by(|a, b| a.as_bytes().cmp(b.as_bytes())); + w.u32(anchors.len() as u32); + for id in &anchors { + w.id(id); + } + let mut closure = m.closure.clone(); + closure.sort_unstable_by(|a, b| a.as_bytes().cmp(b.as_bytes())); + w.u32(closure.len() as u32); + for id in &closure { + w.id(id); + } + w.bytes(&m.index_ct); + w.u32(m.kek_wraps.len() as u32); + for k in &m.kek_wraps { + w.str(&k.recipient_id); + w.bytes(&k.wrapped_dek); + } + } } w.0 } @@ -520,6 +617,46 @@ impl Object { wrapped_keys, }) } + TAG_SEALED => Object::Sealed(SealedObj { + payload: Arc::from(r.rest()), + }), + TAG_MANIFEST => { + let base = r.id()?; + let prev = match r.u8()? { + 0 => None, + 1 => Some(r.id()?), + b => return Err(Error::Malformed(format!("bad manifest prev marker {b}"))), + }; + let na = r.count()?; + let mut anchors = Vec::with_capacity(na); + for _ in 0..na { + anchors.push(r.id()?); + } + let nc = r.count()?; + let mut closure = Vec::with_capacity(nc); + for _ in 0..nc { + closure.push(r.id()?); + } + let index_ct = r.bytes()?; + let nk = r.count()?; + let mut kek_wraps = Vec::with_capacity(nk); + for _ in 0..nk { + let recipient_id = r.str()?; + let wrapped_dek = r.bytes()?; + kek_wraps.push(WrappedKey { + recipient_id, + wrapped_dek, + }); + } + Object::Manifest(BranchManifest { + base, + prev, + anchors, + closure, + index_ct, + kek_wraps, + }) + } t => return Err(Error::Malformed(format!("unknown object tag {t}"))), }; Ok(obj) @@ -1028,6 +1165,71 @@ mod tests { assert_eq!(rule.next_epoch(), 3); } + #[test] + fn sealed_object_roundtrips_and_differs_from_blob() { + let payload: Vec = (0..64).collect(); + let sealed = Object::Sealed(SealedObj { + payload: Arc::from(payload.clone().into_boxed_slice()), + }); + assert_eq!(sealed, Object::decode(&sealed.encode()).unwrap()); + assert_eq!(sealed.kind_name(), "sealed"); + assert_eq!(sealed.blob_size(), 64, "sealed bytes are budget-counted"); + // Same bytes as a plain blob must hash to a DIFFERENT id: the tag is + // part of the encoding, so ciphertext can never collide with a + // plaintext blob's address. + assert_ne!(sealed.id(), Object::blob(payload).id()); + } + + #[test] + fn manifest_roundtrips_and_closure_is_order_independent() { + let a = ObjectId::from_bytes([1; 32]); + let b = ObjectId::from_bytes([2; 32]); + let mk = |closure: Vec| { + Object::Manifest(BranchManifest { + base: ObjectId::from_bytes([9; 32]), + prev: Some(ObjectId::from_bytes([8; 32])), + anchors: vec![ObjectId::from_bytes([7; 32])], + closure, + index_ct: vec![7; 40], + kek_wraps: vec![WrappedKey { + recipient_id: "rid".into(), + wrapped_dek: vec![5; 80], + }], + }) + }; + let m1 = mk(vec![a, b]); + let m2 = mk(vec![b, a]); + assert_eq!(m1.id(), m2.id(), "closure order must not change the id"); + let decoded = Object::decode(&m1.encode()).unwrap(); + let Object::Manifest(back) = decoded else { + panic!("not a manifest") + }; + assert_eq!(back.base, ObjectId::from_bytes([9; 32])); + assert_eq!(back.closure, vec![a, b], "decoded closure is sorted"); + assert_eq!(back.index_ct, vec![7; 40]); + assert_eq!(back.kek_wraps.len(), 1); + } + + #[test] + fn manifest_decode_rejects_fabricated_counts() { + const HUGE: u32 = 0xFFFF_FFFF; + // TAG_MANIFEST + base(32) + prev(0) + anchor-count(fabricated). + let mut buf = vec![TAG_MANIFEST]; + buf.extend_from_slice(&[0u8; 32]); + buf.push(0u8); // prev marker: none + buf.extend_from_slice(&HUGE.to_be_bytes()); + assert!(matches!(Object::decode(&buf), Err(Error::Malformed(_)))); + // Same for the kek-wrap count after empty anchors + closure + index. + let mut buf = vec![TAG_MANIFEST]; + buf.extend_from_slice(&[0u8; 32]); + buf.push(0u8); // prev marker: none + buf.extend_from_slice(&0u32.to_be_bytes()); // anchor count + buf.extend_from_slice(&0u32.to_be_bytes()); // closure count + buf.extend_from_slice(&0u32.to_be_bytes()); // index_ct len + buf.extend_from_slice(&HUGE.to_be_bytes()); // kek wrap count + assert!(matches!(Object::decode(&buf), Err(Error::Malformed(_)))); + } + #[test] fn randomized_bit_is_distinct_from_protected() { assert_eq!(PROTECTED & RANDOMIZED, 0, "flags must not overlap"); diff --git a/crates/crypto/src/lib.rs b/crates/crypto/src/lib.rs index d3e15d60..2b4a94b6 100644 --- a/crates/crypto/src/lib.rs +++ b/crates/crypto/src/lib.rs @@ -12,6 +12,7 @@ pub mod envelope; pub mod error; pub mod key; +pub mod private; pub mod provider; pub mod signing; @@ -21,6 +22,9 @@ pub use envelope::{ }; pub use error::{Error, Result}; pub use key::{generate_keypair, PublicKey, RecipientId, SecretKey}; +pub use private::{ + generate_kek, open_object, seal_object, unwrap_kek_with, wrap_kek_for, BranchIndex, IndexEntry, +}; pub use provider::{FileKeyProvider, KeyProvider}; pub use signing::{ generate_identity_v2, parse_identity, sign_snapshot_id, sign_transcript_id, diff --git a/crates/crypto/src/private.rs b/crates/crypto/src/private.rs new file mode 100644 index 00000000..fa74fe3a --- /dev/null +++ b/crates/crypto/src/private.rs @@ -0,0 +1,373 @@ +//! Private-branch primitives (P34, ADR-0044): sealed objects, the branch KEK, +//! and the encrypted branch index. +//! +//! A private branch's object bodies are sealed one-by-one under fresh random +//! per-object DEKs (`seal_object`). The DEKs — and the mapping from *inner* +//! (plaintext) object ids to *sealed* (ciphertext) object ids — live in a +//! [`BranchIndex`], which travels only as one AEAD blob encrypted under the +//! per-branch KEK (`BranchIndex::encrypt`). The KEK itself is wrapped per +//! recipient with the exact P2 X25519 envelope (`wrap_kek_for`). Revocation +//! rewrap is therefore: fresh KEK, re-encrypt the index, re-wrap the KEK — +//! no content plaintext, no sealed-object id churn. + +use std::collections::BTreeMap; + +use chacha20poly1305::aead::{Aead, Payload}; +use chacha20poly1305::{KeyInit, XChaCha20Poly1305, XNonce}; +use rand_core::{CryptoRng, OsRng, RngCore}; +use zeroize::{Zeroize, Zeroizing}; + +use scl_core::{ObjectId, WrappedKey}; + +use crate::error::{Error, Result}; +use crate::key::{PublicKey, SecretKey}; + +const KEY_LEN: usize = 32; +const NONCE_LEN: usize = 24; +/// AAD domains: a sealed object body and an encrypted index can never be +/// swapped for one another (or for a P7 path blob, which uses `scl-path-v1`). +const SEALED_AAD: &[u8] = b"scl-sealed-object-v1"; +const INDEX_AAD: &[u8] = b"scl-branch-index-v1"; + +/// Generate a fresh branch KEK from the OS CSPRNG. +pub fn generate_kek() -> Zeroizing<[u8; KEY_LEN]> { + generate_kek_with_rng(&mut OsRng) +} + +/// `generate_kek` with a caller-supplied RNG (deterministic in tests). +pub fn generate_kek_with_rng(rng: &mut R) -> Zeroizing<[u8; KEY_LEN]> { + let mut kek = Zeroizing::new([0u8; KEY_LEN]); + rng.fill_bytes(kek.as_mut_slice()); + kek +} + +/// Wrap the branch KEK for one recipient. Same X25519→HKDF→AEAD envelope as a +/// secret DEK wrap — a KEK is 32 key bytes like any DEK. +pub fn wrap_kek_for(kek: &[u8; KEY_LEN], recipient: &PublicKey) -> WrappedKey { + crate::envelope::wrap_dek_for(kek, recipient) +} + +/// Unwrap the branch KEK with `identity` (errors if not the wrap's recipient). +pub fn unwrap_kek_with(wrapped: &WrappedKey, identity: &SecretKey) -> Result> { + crate::envelope::unwrap_dek_with(wrapped, identity) +} + +/// Seal an inner object's canonical encoding under a fresh random DEK + nonce. +/// Returns the sealed payload (`nonce ‖ ciphertext`, the bytes of a +/// `Object::Sealed`) and the DEK to be recorded in the branch index. +pub fn seal_object(encoding: &[u8]) -> (Vec, Zeroizing<[u8; KEY_LEN]>) { + seal_object_with_rng(encoding, &mut OsRng) +} + +/// `seal_object` with a caller-supplied RNG (deterministic in tests). +pub fn seal_object_with_rng( + encoding: &[u8], + rng: &mut R, +) -> (Vec, Zeroizing<[u8; KEY_LEN]>) { + let mut dek = Zeroizing::new([0u8; KEY_LEN]); + rng.fill_bytes(dek.as_mut_slice()); + let mut nonce = [0u8; NONCE_LEN]; + rng.fill_bytes(&mut nonce); + + let cipher = XChaCha20Poly1305::new_from_slice(dek.as_slice()).expect("32-byte DEK"); + let ciphertext = cipher + .encrypt( + XNonce::from_slice(&nonce), + Payload { + msg: encoding, + aad: SEALED_AAD, + }, + ) + .expect("aead encrypt is infallible for valid inputs"); + + let mut payload = Vec::with_capacity(NONCE_LEN + ciphertext.len()); + payload.extend_from_slice(&nonce); + payload.extend_from_slice(&ciphertext); + (payload, dek) +} + +/// Open a sealed payload with its DEK (AEAD-verified). Returns the inner +/// object's canonical encoding. +pub fn open_object(payload: &[u8], dek: &[u8; KEY_LEN]) -> Result>> { + if payload.len() < NONCE_LEN { + return Err(Error::Decrypt); + } + let (nonce, ct) = payload.split_at(NONCE_LEN); + let cipher = XChaCha20Poly1305::new_from_slice(dek).map_err(|_| Error::Decrypt)?; + let pt = cipher + .decrypt( + XNonce::from_slice(nonce), + Payload { + msg: ct, + aad: SEALED_AAD, + }, + ) + .map_err(|_| Error::Decrypt)?; + Ok(Zeroizing::new(pt)) +} + +/// One branch-index entry: where an inner object's ciphertext lives and the +/// DEK that opens it. +#[derive(Clone, PartialEq, Eq, Debug)] +pub struct IndexEntry { + pub sealed: ObjectId, + pub dek: [u8; KEY_LEN], +} + +/// 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 +/// the store — that resolution rule is what makes sealing copy-on-write. +/// +/// 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)] +pub struct BranchIndex { + pub inner_tip: Option, + pub entries: BTreeMap, +} + +impl Drop for BranchIndex { + fn drop(&mut self) { + for e in self.entries.values_mut() { + e.dek.zeroize(); + } + } +} + +impl BranchIndex { + /// Serialize to the fixed-width index codec (all fields are 32-byte ids or + /// keys, so no length-prefixing is needed beyond the entry count). + fn encode(&self) -> Zeroizing> { + let mut buf = Zeroizing::new(Vec::with_capacity(33 + self.entries.len() * 96)); + match &self.inner_tip { + Some(id) => { + buf.push(1u8); + buf.extend_from_slice(id.as_bytes()); + } + None => buf.push(0u8), + } + buf.extend_from_slice(&(self.entries.len() as u32).to_be_bytes()); + for (inner, e) in &self.entries { + buf.extend_from_slice(inner.as_bytes()); + buf.extend_from_slice(e.sealed.as_bytes()); + buf.extend_from_slice(&e.dek); + } + buf + } + + fn decode(bytes: &[u8]) -> Result { + let mut pos = 0usize; + let take = |pos: &mut usize, n: usize| -> Result<&[u8]> { + if *pos + n > bytes.len() { + return Err(Error::Decrypt); + } + let s = &bytes[*pos..*pos + n]; + *pos += n; + Ok(s) + }; + let id32 = |pos: &mut usize| -> Result { + let mut a = [0u8; 32]; + a.copy_from_slice(take(pos, 32)?); + Ok(ObjectId::from_bytes(a)) + }; + let inner_tip = match take(&mut pos, 1)?[0] { + 0 => None, + 1 => Some(id32(&mut pos)?), + _ => return Err(Error::Decrypt), + }; + let mut nb = [0u8; 4]; + nb.copy_from_slice(take(&mut pos, 4)?); + let n = u32::from_be_bytes(nb) as usize; + // Every entry is exactly 96 bytes; a count that overruns the buffer is + // fabricated (same guard idiom as core's object decoder). + if n.checked_mul(96).map(|need| pos + need != bytes.len()) != Some(false) { + return Err(Error::Decrypt); + } + let mut entries = BTreeMap::new(); + for _ in 0..n { + let inner = id32(&mut pos)?; + let sealed = id32(&mut pos)?; + let mut dek = [0u8; KEY_LEN]; + dek.copy_from_slice(take(&mut pos, KEY_LEN)?); + entries.insert(inner, IndexEntry { sealed, dek }); + } + Ok(BranchIndex { inner_tip, entries }) + } + + /// Encrypt the index under the branch KEK with a fresh random nonce. + /// Returns `nonce ‖ ciphertext` (the manifest's `index_ct` field). + pub fn encrypt(&self, kek: &[u8; KEY_LEN]) -> Vec { + self.encrypt_with_rng(kek, &mut OsRng) + } + + /// `encrypt` with a caller-supplied RNG (deterministic in tests). + pub fn encrypt_with_rng( + &self, + kek: &[u8; KEY_LEN], + rng: &mut R, + ) -> Vec { + let mut nonce = [0u8; NONCE_LEN]; + rng.fill_bytes(&mut nonce); + let cipher = XChaCha20Poly1305::new_from_slice(kek).expect("32-byte KEK"); + let ct = cipher + .encrypt( + XNonce::from_slice(&nonce), + Payload { + msg: self.encode().as_slice(), + aad: INDEX_AAD, + }, + ) + .expect("aead encrypt is infallible for valid inputs"); + let mut blob = Vec::with_capacity(NONCE_LEN + ct.len()); + blob.extend_from_slice(&nonce); + blob.extend_from_slice(&ct); + blob + } + + /// Decrypt and decode an index blob with the branch KEK (AEAD-verified). + pub fn decrypt(index_ct: &[u8], kek: &[u8; KEY_LEN]) -> Result { + if index_ct.len() < NONCE_LEN { + return Err(Error::Decrypt); + } + let (nonce, ct) = index_ct.split_at(NONCE_LEN); + let cipher = XChaCha20Poly1305::new_from_slice(kek).map_err(|_| Error::Decrypt)?; + let pt = Zeroizing::new( + cipher + .decrypt( + XNonce::from_slice(nonce), + Payload { + msg: ct, + aad: INDEX_AAD, + }, + ) + .map_err(|_| Error::Decrypt)?, + ); + BranchIndex::decode(&pt) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::key::generate_keypair_with_rng; + use rand_chacha::ChaCha20Rng; + use rand_core::SeedableRng; + + fn rng(seed: u64) -> ChaCha20Rng { + ChaCha20Rng::seed_from_u64(seed) + } + + fn oid(b: u8) -> ObjectId { + ObjectId::from_bytes([b; 32]) + } + + #[test] + fn seal_open_object_roundtrip_is_randomized() { + let encoding = b"tag+payload of some inner object"; + let (p1, d1) = seal_object(encoding); + let (p2, d2) = seal_object(encoding); + assert_ne!(p1, p2, "sealing must be randomized (no equality oracle)"); + assert_ne!(d1.as_slice(), d2.as_slice()); + assert_eq!(&open_object(&p1, &d1).unwrap()[..], encoding); + assert_eq!(&open_object(&p2, &d2).unwrap()[..], encoding); + // Wrong DEK / tampered payload fail AEAD. + assert!(open_object(&p1, &d2).is_err()); + let mut tampered = p1.clone(); + let n = tampered.len(); + tampered[n - 1] ^= 0xFF; + assert!(open_object(&tampered, &d1).is_err()); + } + + #[test] + fn sealed_object_aad_differs_from_path_blob() { + // A sealed object cannot be opened by the path decryptor even with the + // right DEK: the AAD domains are distinct on purpose. + let (payload, dek) = seal_object(b"x"); + assert!(crate::envelope::decrypt_path(&payload, &dek).is_err()); + } + + #[test] + fn index_roundtrip_through_kek() { + let kek = generate_kek_with_rng(&mut rng(1)); + let mut idx = BranchIndex { + inner_tip: Some(oid(9)), + entries: BTreeMap::new(), + }; + idx.entries.insert( + oid(1), + IndexEntry { + sealed: oid(2), + dek: [3; 32], + }, + ); + idx.entries.insert( + oid(4), + IndexEntry { + sealed: oid(5), + dek: [6; 32], + }, + ); + let ct = idx.encrypt_with_rng(&kek, &mut rng(2)); + let back = BranchIndex::decrypt(&ct, &kek).unwrap(); + assert_eq!(back, idx); + // Wrong KEK fails AEAD rather than yielding garbage. + let other = generate_kek_with_rng(&mut rng(7)); + assert!(BranchIndex::decrypt(&ct, &other).is_err()); + } + + #[test] + fn empty_index_roundtrips() { + let kek = generate_kek_with_rng(&mut rng(1)); + let idx = BranchIndex::default(); + let ct = idx.encrypt_with_rng(&kek, &mut rng(2)); + assert_eq!(BranchIndex::decrypt(&ct, &kek).unwrap(), idx); + } + + #[test] + fn index_decode_rejects_fabricated_count_and_trailing_bytes() { + let kek = generate_kek_with_rng(&mut rng(1)); + // Build a valid plaintext then corrupt the count field before + // encrypting: decode must reject it after a successful AEAD open. + let idx = BranchIndex { + inner_tip: None, + entries: BTreeMap::new(), + }; + let mut pt = idx.encode().to_vec(); + // count = 1 but no entry bytes follow. + let len = pt.len(); + pt[len - 1] = 1; + let cipher = XChaCha20Poly1305::new_from_slice(kek.as_slice()).unwrap(); + let mut nonce = [0u8; NONCE_LEN]; + rng(3).fill_bytes(&mut nonce); + let ct = cipher + .encrypt( + XNonce::from_slice(&nonce), + Payload { + msg: &pt, + aad: INDEX_AAD, + }, + ) + .unwrap(); + let mut blob = nonce.to_vec(); + blob.extend_from_slice(&ct); + assert!(BranchIndex::decrypt(&blob, &kek).is_err()); + } + + #[test] + fn kek_wrap_unwrap_roundtrip_per_recipient() { + let (sk_a, pk_a) = generate_keypair_with_rng(&mut rng(1)); + let (sk_b, _pk_b) = generate_keypair_with_rng(&mut rng(2)); + let kek = generate_kek_with_rng(&mut rng(3)); + let wk = wrap_kek_for(&kek, &pk_a); + assert_eq!( + unwrap_kek_with(&wk, &sk_a).unwrap().as_slice(), + kek.as_slice() + ); + assert!( + unwrap_kek_with(&wk, &sk_b).is_err(), + "non-recipient must fail" + ); + } +} diff --git a/crates/repo/src/error.rs b/crates/repo/src/error.rs index 1c1fc2d3..274fca03 100644 --- a/crates/repo/src/error.rs +++ b/crates/repo/src/error.rs @@ -60,6 +60,23 @@ pub enum Error { PartialCloneUnsupported(String), #[error("already up to date")] UpToDate, + /// The named branch is private and the caller either provided no identity + /// or one that is not a recipient of the branch KEK (P34, ADR-0044). + #[error("branch {0} is private; provide a recipient --identity to access it")] + PrivateNoAccess(String), + /// An operation that is not supported on a private branch (P34). `{0}` is + /// the operation name; the covering flows are `sc branch publish` (make + /// it public) or performing the operation on a public branch. + #[error("{0} is not supported on a private branch (publish it first: `sc branch publish`)")] + PrivateUnsupported(String), + /// Integrating FROM a private branch INTO a public one is always refused: + /// decrypting sealed content into public objects is publishing, and the + /// only sanctioned path to that is the one loudly-named command (P34). + #[error("cannot integrate private branch {0} into a public branch; use `sc branch publish {0}` to make it public first")] + PrivateIntegration(String), + /// A `sc branch grant/revoke/publish` target that is not a private branch. + #[error("branch {0} is not private")] + NotPrivateBranch(String), #[error("{0}")] SecretDetected(crate::scanner::ScanReport), #[error("non-fast-forward: the remote has commits you don't have; fetch + merge first")] diff --git a/crates/repo/src/lib.rs b/crates/repo/src/lib.rs index 14152ca2..68b54c41 100644 --- a/crates/repo/src/lib.rs +++ b/crates/repo/src/lib.rs @@ -15,6 +15,7 @@ pub mod merge; pub mod merge_state; pub mod oplog; pub mod pick_state; +pub mod private; pub mod promisor; pub mod protect; pub mod protect_ops; diff --git a/crates/repo/src/merge.rs b/crates/repo/src/merge.rs index 695afd6e..8b934739 100644 --- a/crates/repo/src/merge.rs +++ b/crates/repo/src/merge.rs @@ -35,18 +35,47 @@ pub fn merge_base(store: &mut Store, a: ObjectId, b: ObjectId) -> Result Result { Ok(ancestors(store, desc)?.contains(&anc)) } -/// All ancestors of `id`, inclusive. +/// All ancestors of `id`, inclusive. Kind-aware: snapshots contribute their +/// parents, branch manifests their `prev` chain + public base. fn ancestors(store: &mut Store, id: ObjectId) -> Result> { let mut set = BTreeSet::new(); let mut q = VecDeque::new(); q.push_back(id); set.insert(id); while let Some(cur) = q.pop_front() { - for p in store.get_snapshot(&cur)?.parents { + let parents: Vec = match store.get(&cur)? { + Object::Snapshot(s) => s.parents, + Object::Manifest(m) => m.prev.into_iter().chain(std::iter::once(m.base)).collect(), + other => { + return Err(Error::BadRef(format!( + "expected snapshot or manifest at {cur}, found {}", + other.kind_name() + ))) + } + }; + for p in parents { if set.insert(p) { q.push_back(p); } diff --git a/crates/repo/src/oplog.rs b/crates/repo/src/oplog.rs index a783df5b..b2438887 100644 --- a/crates/repo/src/oplog.rs +++ b/crates/repo/src/oplog.rs @@ -369,7 +369,25 @@ impl Repo { Some((_, before, after)) => (before != after, *before), None => (false, refs::read_branch_tip(&self.layout, &restored_head)?), }; - let rematerialize = rec.head_before != rec.head_after || moves_current_tip; + let mut rematerialize = rec.head_before != rec.head_after || moves_current_tip; + + // Private branches (P34): undo carries no identity, and a manifest + // tip cannot be decoded (let alone materialized) without one. When + // the current tip or the restored tip is a branch manifest, restore + // the refs but skip the dirty check + rematerialize — the working + // tree goes stale until `sc switch --identity` re-lays it, + // which the CLI surfaces. Never touches the worktree, so nothing can + // be clobbered. + let cur_tip = refs::read_branch_tip(&self.layout, &refs::current_branch(&self.layout)?)?; + let tip_is_manifest = |t: Option| -> Result { + Ok(match t { + Some(id) => self.manifest_at(&id)?.is_some(), + None => false, + }) + }; + if tip_is_manifest(cur_tip)? || tip_is_manifest(will_be_tip)? { + rematerialize = false; + } if rematerialize && will_be_tip.is_none() { return Err(Error::InvalidArgument( diff --git a/crates/repo/src/private.rs b/crates/repo/src/private.rs new file mode 100644 index 00000000..c766e14e --- /dev/null +++ b/crates/repo/src/private.rs @@ -0,0 +1,2361 @@ +//! Private branches (P34, ADR-0044): per-branch access control. +//! +//! A private branch's ref points at a [`BranchManifest`] instead of a +//! snapshot. Every object the branch introduces — snapshot, tree, blob — is +//! sealed individually (`Object::Sealed`, fresh random DEK each) and the +//! branch's *inner* world is a normal plaintext CAS DAG whose ids appear only +//! inside the KEK-encrypted index. Sealing is **copy-on-write**: an inner id +//! absent from the index is a public object read straight from the store, so +//! unchanged content is never duplicated and the references to it live only +//! inside ciphertext. +//! +//! Nothing in this module ever writes inner plaintext to the persistent +//! store: inner objects exist as decoded values in memory (and, for merges, +//! in a RAM-only ephemeral `Store`) and as sealed ciphertext on disk. The +//! only path that writes inner content as public objects is `publish`, after +//! its scanner gate. + +use std::collections::{BTreeMap, BTreeSet, VecDeque}; + +use scl_core::{ + BranchManifest, EntryKind, FileMode, Object, ObjectId, Protection, SealedObj, Snapshot, Store, + Tree, TreeEntry, WrappedKey, PROTECTED, +}; +use scl_crypto::{BranchIndex, IndexEntry, PublicKey, RecipientId, SecretKey}; + +use crate::error::{Error, Result}; +use crate::refs; +use crate::repo::Repo; +use crate::worktree::{self, Diff}; + +/// An opened private branch: the manifest plus the decrypted key material. +/// Constructing one **is** authorization — it requires an identity that +/// unwraps the branch KEK. The KEK and every DEK in the index zeroize on drop +/// (best-effort, `BranchIndex`'s own contract). +pub(crate) struct PrivateCtx { + pub manifest_id: ObjectId, + pub manifest: BranchManifest, + pub kek: scl_crypto::Zeroizing<[u8; 32]>, + pub index: BranchIndex, +} + +impl PrivateCtx { + /// The inner (plaintext-id) tip snapshot id. At branch creation this is + /// the public base itself; after the first private commit it is a private + /// inner id resolvable only through the index. + fn inner_tip(&self) -> Result { + self.index + .inner_tip + .ok_or_else(|| Error::BadRef("private branch manifest has no inner tip".into())) + } +} + +impl Repo { + /// Decode the object a branch ref points at and return its manifest when + /// the branch is private. `Ok(None)` for a public branch. + pub fn branch_manifest(&self, name: &str) -> Result> { + let tip = refs::read_branch_tip(&self.layout, name)? + .ok_or_else(|| Error::NoSuchBranch(name.to_string()))?; + Ok(self.manifest_at(&tip)?.map(|m| (tip, m))) + } + + /// `Some(manifest)` iff the object at `id` is a branch manifest. + pub(crate) fn manifest_at(&self, id: &ObjectId) -> Result> { + let store_arc = self.vfs.store(); + let obj = store_arc.lock().unwrap().get(id)?; + match obj { + Object::Manifest(m) => Ok(Some(m)), + _ => Ok(None), + } + } + + /// Whether the named branch is private. + pub fn is_private_branch(&self, name: &str) -> Result { + Ok(self.branch_manifest(name)?.is_some()) + } + + /// Whether `identity` can open the named private branch — the listing + /// marker probe (`(private)` vs `(private, no access)`). Discards all key + /// material; `Ok(false)` for a non-recipient or a public branch. + pub fn can_open_private(&self, name: &str, identity: Option<&SecretKey>) -> Result { + match self.branch_manifest(name)? { + None => Ok(false), + Some((mid, manifest)) => match self.open_private(name, mid, manifest, identity) { + Ok(_) => Ok(true), + Err(Error::PrivateNoAccess(_)) => Ok(false), + Err(e) => Err(e), + }, + } + } + + /// `Some((branch, manifest id, manifest))` iff HEAD's branch is private. + pub fn head_private(&self) -> Result> { + let branch = refs::current_branch(&self.layout)?; + match refs::read_branch_tip(&self.layout, &branch)? { + None => Ok(None), + Some(tip) => Ok(self.manifest_at(&tip)?.map(|m| (branch.clone(), tip, m))), + } + } + + /// Guard: refuse `op` when the current branch is private. The cheap check + /// every snapshot-assuming ref-moving operation calls first, so the + /// refusal names the real cause instead of surfacing as a decode error. + pub(crate) fn refuse_on_private(&self, op: &str) -> Result<()> { + if self.head_private()?.is_some() { + return Err(Error::PrivateUnsupported(op.to_string())); + } + Ok(()) + } + + /// Authorize against a manifest: unwrap the branch KEK with `identity` and + /// decrypt the index. Fails `PrivateNoAccess` when no wrap unwraps. + pub(crate) fn open_private( + &self, + branch: &str, + manifest_id: ObjectId, + manifest: BranchManifest, + identity: Option<&SecretKey>, + ) -> Result { + let sk = identity.ok_or_else(|| Error::PrivateNoAccess(branch.to_string()))?; + let my_id = sk.public().recipient_id(); + // Try the caller's own wrap first (cheap exact match), then every + // wrap (a rewrapped manifest may carry stale recipient-id labels). + let mut wraps: Vec<&WrappedKey> = manifest.kek_wraps.iter().collect(); + wraps.sort_by_key(|w| w.recipient_id != my_id.as_str()); + for wk in wraps { + if let Ok(kek) = scl_crypto::unwrap_kek_with(wk, sk) { + let index = BranchIndex::decrypt(&manifest.index_ct, &kek) + .map_err(|_| Error::BadRef("private branch index failed to decrypt".into()))?; + return Ok(PrivateCtx { + manifest_id, + manifest, + kek, + index, + }); + } + } + Err(Error::PrivateNoAccess(branch.to_string())) + } + + /// Resolve + authorize a private branch by name in one step. + pub(crate) fn open_private_branch( + &self, + name: &str, + identity: Option<&SecretKey>, + ) -> Result { + let (mid, manifest) = self + .branch_manifest(name)? + .ok_or_else(|| Error::NotPrivateBranch(name.to_string()))?; + self.open_private(name, mid, manifest, identity) + } + + // ---- inner-object resolution -------------------------------------------- + + /// Fetch an inner object: an index hit resolves through its sealed + /// ciphertext; a miss is a public object read directly. The decoded + /// object's id must equal the requested id — a mismatch means a corrupt + /// (or tampered-with) index entry and is refused loudly. + pub(crate) fn get_inner( + &self, + store: &mut Store, + ctx: &PrivateCtx, + id: &ObjectId, + ) -> Result { + match ctx.index.entries.get(id) { + None => store.get(id).map_err(Error::from), + Some(entry) => { + let sealed = match store.get(&entry.sealed)? { + Object::Sealed(s) => s, + other => { + return Err(Error::BadRef(format!( + "index maps {id} to a non-sealed object ({})", + other.kind_name() + ))) + } + }; + let encoding = scl_crypto::open_object(&sealed.payload, &entry.dek)?; + let obj = Object::decode(&encoding)?; + if obj.id() != *id { + return Err(Error::CorruptObject(*id)); + } + Ok(obj) + } + } + } + + fn get_inner_snapshot( + &self, + store: &mut Store, + ctx: &PrivateCtx, + id: &ObjectId, + ) -> Result { + match self.get_inner(store, ctx, id)? { + Object::Snapshot(s) => Ok(s), + other => Err(Error::BadRef(format!( + "expected snapshot at {id}, found {}", + other.kind_name() + ))), + } + } + + /// Flatten an inner tree to `path -> (inner blob id, mode, perms)`. + pub(crate) fn inner_tree_files( + &self, + store: &mut Store, + ctx: &PrivateCtx, + root: ObjectId, + ) -> Result> { + let mut out = BTreeMap::new(); + let mut stack = vec![(root, String::new())]; + while let Some((tree_id, prefix)) = stack.pop() { + let tree = match self.get_inner(store, ctx, &tree_id)? { + Object::Tree(t) => t, + other => { + return Err(Error::BadRef(format!( + "expected tree at {tree_id}, found {}", + other.kind_name() + ))) + } + }; + for e in tree.entries { + let path = if prefix.is_empty() { + e.name.clone() + } else { + format!("{prefix}/{}", e.name) + }; + match e.kind { + EntryKind::Blob => { + out.insert(path, (e.id, e.mode, e.perms)); + } + EntryKind::Tree => stack.push((e.id, path)), + } + } + } + Ok(out) + } + + /// Every inner id (trees + blobs) reachable in an inner tree. The + /// carried-set authority for the copy-on-write sealing decision: an id in + /// here (and not in the index) is public content reachable from the base + /// by induction, so referencing it never strands a peer. + fn inner_tree_closure( + &self, + store: &mut Store, + ctx: &PrivateCtx, + root: ObjectId, + ) -> Result> { + let mut out = BTreeSet::new(); + let mut stack = vec![root]; + while let Some(tree_id) = stack.pop() { + if !out.insert(tree_id) { + continue; + } + let tree = match self.get_inner(store, ctx, &tree_id)? { + Object::Tree(t) => t, + other => { + return Err(Error::BadRef(format!( + "expected tree at {tree_id}, found {}", + other.kind_name() + ))) + } + }; + for e in tree.entries { + match e.kind { + EntryKind::Blob => { + out.insert(e.id); + } + EntryKind::Tree => stack.push(e.id), + } + } + } + Ok(out) + } + + /// Inner blob bytes via the resolver. + fn inner_blob(&self, store: &mut Store, ctx: &PrivateCtx, id: &ObjectId) -> Result> { + match self.get_inner(store, ctx, id)? { + Object::Blob(b) => Ok(b.to_vec()), + other => Err(Error::BadRef(format!( + "expected blob at {id}, found {}", + other.kind_name() + ))), + } + } + + // ---- creation ------------------------------------------------------------- + + /// Create a private branch at the current (public) tip: mint a KEK, wrap + /// it for creator + recipients + escrow, and point the new ref at a + /// manifest whose index is empty (copy-on-write: nothing is sealed until + /// content actually changes). + pub fn branch_private( + &self, + name: &str, + creator: &SecretKey, + recipients: &[PublicKey], + escrows: &[PublicKey], + ) -> Result { + crate::repo::validate_branch_name(name)?; + if self.layout.ref_path(name).exists() { + return Err(Error::BadRef(format!("branch already exists: {name}"))); + } + if crate::merge_state::in_progress(&self.layout) { + return Err(Error::MergeInProgress); + } + if crate::pick_state::in_progress(&self.layout) { + return Err(Error::PickInProgress); + } + if crate::rebase_state::in_progress(&self.layout) { + return Err(Error::RebaseInProgress); + } + let tip = self.head_tip()?.ok_or(Error::Unborn)?; + if self.manifest_at(&tip)?.is_some() { + return Err(Error::PrivateUnsupported( + "creating a private branch from a private branch".into(), + )); + } + + let kek = scl_crypto::generate_kek(); + // Creator is always wrapped in; recipients + escrow deduped by id. + let mut wrap_keys: Vec = vec![creator.public()]; + for pk in recipients.iter().chain(escrows.iter()) { + if !wrap_keys + .iter() + .any(|k| k.recipient_id() == pk.recipient_id()) + { + wrap_keys.push(pk.clone()); + } + } + let kek_wraps: Vec = wrap_keys + .iter() + .map(|pk| scl_crypto::wrap_kek_for(&kek, pk)) + .collect(); + + let index = BranchIndex { + inner_tip: Some(tip), + entries: BTreeMap::new(), + }; + let manifest = BranchManifest { + base: tip, + prev: None, + anchors: Vec::new(), + closure: Vec::new(), + index_ct: index.encrypt(&kek), + kek_wraps, + }; + let store_arc = self.vfs.store(); + let mid = store_arc.lock().unwrap().put(Object::Manifest(manifest))?; + refs::write_branch_tip(&self.layout, name, &mid)?; + let head = refs::current_branch(&self.layout)?; + crate::oplog::record( + &self.layout, + &format!("branch {name} (private)"), + &head, + &head, + &[(name.to_string(), None, Some(mid))], + )?; + Ok(mid) + } + + // ---- checkout / status / log ---------------------------------------------- + + /// Materialize a private branch's inner tip into the working tree + /// (recipient context — plaintext on disk is the authorized checkout, + /// same posture as a protected-path decrypt). `old_paths` is the file set + /// of the tree being switched away from; anything there that the target + /// doesn't track (or that falls outside `sparse`) is removed. + fn materialize_private( + &self, + store: &mut Store, + ctx: &PrivateCtx, + target_root: ObjectId, + old_paths: &BTreeSet, + protection: &Protection, + identity: &SecretKey, + ) -> Result> { + let sparse = self.sparse_spec()?; + let target = self.inner_tree_files(store, ctx, target_root)?; + for p in old_paths { + if !target.contains_key(p) || !sparse.matches(p) { + let full = worktree::safe_join(&self.layout.root, p)?; + let _ = std::fs::remove_file(full); + } + } + let mut skipped = Vec::new(); + for (path, (blob_id, _mode, perms)) in &target { + if !sparse.matches(path) { + continue; + } + let full = worktree::safe_join(&self.layout.root, path)?; + let bytes = self.inner_blob(store, ctx, blob_id)?; + if perms & PROTECTED != 0 { + // A path-protected file carried from the base: still the P7 + // envelope, decrypted via the inner snapshot's wraps. + match crate::protect::decrypt_with(&bytes, blob_id, &[protection], identity, path) { + Ok(pt) => { + if let Some(parent) = full.parent() { + std::fs::create_dir_all(parent)?; + } + std::fs::write(&full, &pt[..])?; + } + Err(_) => { + // KEK recipient without path access: skip, never + // leave stale plaintext behind (same rule as + // `worktree::materialize`). + match std::fs::remove_file(&full) { + Ok(()) => {} + Err(e) if e.kind() == std::io::ErrorKind::NotFound => {} + Err(e) => return Err(e.into()), + } + skipped.push(path.clone()); + } + } + } else { + if let Some(parent) = full.parent() { + std::fs::create_dir_all(parent)?; + } + std::fs::write(&full, &bytes[..])?; + } + } + Ok(skipped) + } + + /// Switch onto a private branch (called by `switch_with_identity` once it + /// detects a manifest ref). The dirty check against the OLD branch has + /// already run in the caller. + pub(crate) fn switch_to_private( + &self, + name: &str, + manifest_id: ObjectId, + manifest: BranchManifest, + identity: Option<&SecretKey>, + old_paths: &BTreeSet, + ) -> Result> { + let ctx = self.open_private(name, manifest_id, manifest, identity)?; + let sk = identity.expect("open_private succeeded, identity present"); + let head_before = refs::current_branch(&self.layout)?; + let tip_snap = { + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let tip = ctx.inner_tip()?; + self.get_inner_snapshot(&mut store, &ctx, &tip)? + }; + let skipped = { + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + self.materialize_private( + &mut store, + &ctx, + tip_snap.root, + old_paths, + &tip_snap.protection, + sk, + )? + }; + refs::write_head(&self.layout, name)?; + crate::oplog::record( + &self.layout, + &format!("switch {name}"), + &head_before, + name, + &[], + )?; + Ok(skipped) + } + + /// Working-tree status against a private HEAD. Requires a recipient + /// identity (the KEK gates even knowing what the tree contains). + pub fn status_private(&self, identity: Option<&SecretKey>) -> Result { + let (branch, mid, manifest) = self + .head_private()? + .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; + let ctx = self.open_private(&branch, mid, manifest, identity)?; + let sk = identity.expect("open_private succeeded"); + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let tip = ctx.inner_tip()?; + let snap = self.get_inner_snapshot(&mut store, &ctx, &tip)?; + let head = self.inner_tree_files(&mut store, &ctx, snap.root)?; + let sparse = self.sparse_spec()?; + let tracked: BTreeSet = head.keys().cloned().collect(); + let wt: BTreeMap> = worktree::read_worktree(&self.layout, &tracked)? + .into_iter() + .map(|(p, b, _)| (p, b)) + .collect(); + let mut diff = Diff::default(); + for (p, bytes) in &wt { + match head.get(p) { + None => diff.added.push(p.clone()), + Some((hid, _mode, perms)) => { + if *perms & PROTECTED != 0 { + // Carried path-ciphertext: unchanged iff the on-disk + // plaintext decrypts equal. We hold an identity by + // construction; an unresolvable wrap reports modified + // (spurious-but-safe, mirrors the P33 posture). + let ct = self.inner_blob(&mut store, &ctx, hid)?; + match crate::protect::decrypt_with(&ct, hid, &[&snap.protection], sk, p) { + Ok(pt) if pt.as_slice() == bytes.as_slice() => {} + _ => diff.modified.push(p.clone()), + } + } else if Object::blob(bytes.clone()).id() != *hid { + diff.modified.push(p.clone()); + } + } + } + } + for (p, (_hid, _mode, perms)) in &head { + if !wt.contains_key(p) { + if !sparse.matches(p) || *perms & PROTECTED != 0 { + continue; + } + diff.deleted.push(p.clone()); + } + } + Ok(diff) + } + + /// Paths tracked by the private HEAD's inner tip. Needs a recipient + /// identity (the tree structure is sealed). + pub(crate) fn private_tracked_paths( + &self, + identity: Option<&SecretKey>, + ) -> Result> { + let (branch, mid, manifest) = self + .head_private()? + .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; + let ctx = self.open_private(&branch, mid, manifest, identity)?; + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let tip = ctx.inner_tip()?; + let root = self.get_inner_snapshot(&mut store, &ctx, &tip)?.root; + Ok(self + .inner_tree_files(&mut store, &ctx, root)? + .into_keys() + .collect()) + } + + /// Line-level unified diff of the working tree against a private HEAD. + /// Same rendering as `diff_unified`; carried path-protected entries are + /// compared via decryption (we hold an identity by construction) but + /// their content is reported status-only, matching the public rule. + pub fn diff_unified_private(&self, identity: Option<&SecretKey>) -> Result { + let (branch, mid, manifest) = self + .head_private()? + .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; + let ctx = self.open_private(&branch, mid, manifest, identity)?; + let sk = identity.expect("open_private succeeded"); + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let tip = ctx.inner_tip()?; + let snap = self.get_inner_snapshot(&mut store, &ctx, &tip)?; + let head = self.inner_tree_files(&mut store, &ctx, snap.root)?; + let sparse = self.sparse_spec()?; + let tracked: BTreeSet = head.keys().cloned().collect(); + let wt: BTreeMap> = worktree::read_worktree(&self.layout, &tracked)? + .into_iter() + .map(|(p, b, _)| (p, b)) + .collect(); + let mut paths: BTreeSet<&String> = wt.keys().collect(); + paths.extend(head.keys()); + let mut out = String::new(); + for path in paths { + let disk = wt.get(path); + match head.get(path) { + None => { + let bytes = disk.expect("path came from one of the two maps"); + crate::repo::push_file_diff(&mut out, path, &[], bytes); + } + Some((blob_id, _mode, perms)) if *perms & PROTECTED != 0 => { + if let Some(bytes) = disk { + let ct = self.inner_blob(&mut store, &ctx, blob_id)?; + let changed = !matches!( + crate::protect::decrypt_with(&ct, blob_id, &[&snap.protection], sk, path), + Ok(pt) if pt.as_slice() == bytes.as_slice() + ); + if changed { + out.push_str(&format!( + "protected file changed: {path} (content not shown)\n" + )); + } + } + } + Some((blob_id, _mode, _perms)) => { + if disk.is_none() && !sparse.matches(path) { + continue; + } + let old = self.inner_blob(&mut store, &ctx, blob_id)?; + match disk { + None => crate::repo::push_file_diff(&mut out, path, &old, &[]), + Some(bytes) => { + if Object::blob(bytes.clone()).id() != *blob_id { + crate::repo::push_file_diff(&mut out, path, &old, bytes); + } + } + } + } + } + } + Ok(out) + } + + /// History of a private branch: inner snapshots from the inner tip, then + /// straight through into public history past the fork point (the resolver + /// falls through to the store on an index miss). + pub fn log_private(&self, identity: Option<&SecretKey>) -> Result> { + let (branch, mid, manifest) = self + .head_private()? + .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; + let ctx = self.open_private(&branch, mid, manifest, identity)?; + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let mut out = Vec::new(); + let mut next = Some(ctx.inner_tip()?); + while let Some(id) = next { + let snap = self.get_inner_snapshot(&mut store, &ctx, &id)?; + next = snap.parents.first().copied(); + out.push((id, snap)); + } + Ok(out) + } + + // ---- commit ---------------------------------------------------------------- + + /// Commit the working tree onto a private branch: scanner-gated like a + /// public commit, then copy-on-write sealing — only objects genuinely new + /// to the branch are sealed; unchanged content keeps its prior inner id + /// (public or already-sealed). Completes an in-progress merge-from-public + /// exactly like the public commit path (two parents, union policy). + pub fn commit_private( + &self, + author: &str, + message: &str, + identity: Option<&SecretKey>, + ) -> Result { + if crate::rebase_state::in_progress(&self.layout) { + return Err(Error::RebaseInProgress); + } + if crate::pick_state::in_progress(&self.layout) { + return Err(Error::PickInProgress); + } + let (branch, mid, manifest) = self + .head_private()? + .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; + let ctx = self.open_private(&branch, mid, manifest, identity)?; + let sk = identity.expect("open_private succeeded"); + let merge_head = crate::merge_state::read_merge_head(&self.layout)?; + + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let inner_tip = ctx.inner_tip()?; + let prev_snap = self.get_inner_snapshot(&mut store, &ctx, &inner_tip)?; + + // Policy + secrets: tip's, or the union with theirs on merge completion + // (same discipline as `snapshot_files`' merge-completion arm). + let (protection, secrets) = match merge_head { + None => (prev_snap.protection.clone(), prev_snap.secrets.clone()), + Some(mh) => { + let theirs = store.get_snapshot(&mh)?; + let prefixes = crate::protect::merge_prefixes( + &prev_snap.protection.prefixes, + &theirs.protection.prefixes, + ); + let mut wrapped = prev_snap.protection.wrapped.clone(); + for (id, wks) in &theirs.protection.wrapped { + let entry = wrapped.entry(*id).or_default(); + *entry = crate::protect::union_wraps(entry, wks); + } + let base = self + .merge_base_private(&mut store, &ctx, inner_tip, mh)? + .ok_or(Error::NoCommonAncestor)?; + let base_secrets = self.get_inner_snapshot(&mut store, &ctx, &base)?.secrets; + let secs = crate::merge::merge_secrets( + &base_secrets, + &prev_snap.secrets, + &theirs.secrets, + )?; + (Protection { prefixes, wrapped }, secs) + } + }; + + let prior = self.inner_tree_files(&mut store, &ctx, prev_snap.root)?; + let tracked: BTreeSet = prior.keys().cloned().collect(); + let files = worktree::read_worktree(&self.layout, &tracked)?; + let sparse = self.sparse_spec()?; + + // Scanner gate on the plaintext partition (protected-prefix files are + // sealed at publish, exempt here exactly as in a public commit). + let plain_for_scan: Vec<(String, Vec, FileMode)> = files + .iter() + .filter(|(p, _, _)| crate::protect::matching_prefix(&protection, p).is_none()) + .cloned() + .collect(); + let report = self.scan_files(&plain_for_scan)?; + if !report.is_empty() { + return Err(Error::SecretDetected(report)); + } + + // Decide each path: carry the prior entry or mint a new inner blob. + let mut entries: BTreeMap = BTreeMap::new(); + let mut new_blobs: BTreeMap> = BTreeMap::new(); + let disk_paths: BTreeSet = files.iter().map(|(p, _, _)| p.clone()).collect(); + for (path, bytes, mode) in files { + let prior_e = prior.get(&path); + match prior_e { + Some((pid, pmode, perms)) if *perms & PROTECTED != 0 => { + // Carried path-ciphertext: unchanged iff plaintext matches. + let ct = self.inner_blob(&mut store, &ctx, pid)?; + let same = crate::protect::decrypt_with(&ct, pid, &[&protection], sk, &path) + .map(|pt| pt.as_slice() == bytes.as_slice()) + .unwrap_or(false); + if same { + entries.insert(path, (*pid, *pmode, *perms)); + } else { + // Edited: it becomes a plain inner blob; publish will + // re-seal it under the path rule. + let id = Object::blob(bytes.clone()).id(); + new_blobs.entry(id).or_insert(bytes); + entries.insert(path, (id, mode, 0)); + } + } + _ => { + let id = Object::blob(bytes.clone()).id(); + if prior_e.map(|(pid, _, _)| *pid) != Some(id) { + new_blobs.entry(id).or_insert(bytes); + } + entries.insert(path, (id, mode, 0)); + } + } + } + // Carry prior paths that are expected-absent: outside sparse, or + // PROTECTED and skipped on disk (never silently drop protected content). + for (path, e) in &prior { + if entries.contains_key(path) || disk_paths.contains(path) { + continue; + } + if !sparse.matches(path) || e.2 & PROTECTED != 0 { + entries.insert(path.clone(), *e); + } + } + + // Carried-set for the sealing decision: prior tree closure, plus + // theirs' closure on merge completion (content adopted from a public + // merge head is already public — sealing it would only waste space). + let mut carried = self.inner_tree_closure(&mut store, &ctx, prev_snap.root)?; + if let Some(mh) = merge_head { + let theirs_root = store.get_snapshot(&mh)?.root; + carried.extend( + worktree::tree_file_entries_with_perms(&mut store, theirs_root)? + .values() + .map(|(id, _, _)| *id), + ); + carried.extend(public_tree_ids(&mut store, theirs_root)?); + } + + let parents = match merge_head { + None => vec![inner_tip], + Some(mh) => vec![inner_tip, mh], + }; + let mut index = ctx.index.clone(); + let new_tip = self.seal_commit( + &mut store, &ctx, &mut index, &carried, entries, new_blobs, parents, secrets, + protection, author, message, + )?; + + // Carry anchors forward; a merge-completion commit adds the public + // merge head (its objects were carried into the sealed tree). + let anchors = extend_anchors(&ctx.manifest.anchors, merge_head.into_iter()); + let new_manifest = BranchManifest { + base: ctx.manifest.base, + prev: Some(ctx.manifest_id), + anchors, + closure: index.entries.values().map(|e| e.sealed).collect(), + index_ct: index.encrypt(&ctx.kek), + kek_wraps: ctx.manifest.kek_wraps.clone(), + }; + let new_mid = store.put(Object::Manifest(new_manifest))?; + drop(store); + refs::write_branch_tip(&self.layout, &branch, &new_mid)?; + crate::merge_state::clear(&self.layout)?; + let label = if merge_head.is_some() { + "commit (merge)" + } else { + "commit" + }; + let first_line = message.lines().next().unwrap_or(""); + crate::oplog::record( + &self.layout, + &format!("{label}: {first_line}"), + &branch, + &branch, + &[(branch.clone(), Some(ctx.manifest_id), Some(new_mid))], + )?; + Ok(new_tip) + } + + /// Build inner trees + snapshot from a decided file map, seal everything + /// genuinely new (not in the index, not in `carried`), record the new + /// DEKs in `index`, and return the new inner tip id. The shared tail of + /// `commit_private` and `merge_into_private`'s clean path. + #[allow(clippy::too_many_arguments)] + fn seal_commit( + &self, + store: &mut Store, + ctx: &PrivateCtx, + index: &mut BranchIndex, + carried: &BTreeSet, + entries: BTreeMap, + new_blobs: BTreeMap>, + parents: Vec, + secrets: BTreeMap, + mut protection: Protection, + author: &str, + message: &str, + ) -> Result { + // Prune wraps to path-ciphertext blobs actually reachable (commit's + // rebuild discipline). + let reachable: BTreeSet = entries.values().map(|(id, _, _)| *id).collect(); + protection.wrapped.retain(|id, _| reachable.contains(id)); + + let (root, trees) = build_tree_objects(&entries); + let snapshot = Object::Snapshot(Snapshot { + root, + parents, + author: author.to_string(), + timestamp: crate::repo::unix_now(), + message: message.to_string(), + secrets, + protection, + }); + let snap_id = snapshot.id(); + + let mut to_seal: Vec<(ObjectId, Vec)> = Vec::new(); + for (id, bytes) in new_blobs { + if !index.entries.contains_key(&id) && !carried.contains(&id) { + to_seal.push((id, Object::blob(bytes).encode())); + } + } + for t in trees { + let id = t.id(); + if !index.entries.contains_key(&id) && !carried.contains(&id) { + to_seal.push((id, t.encode())); + } + } + to_seal.push((snap_id, snapshot.encode())); + + for (inner_id, encoding) in to_seal { + let (payload, dek) = scl_crypto::seal_object(&encoding); + let sealed_id = store.put(Object::Sealed(SealedObj { + payload: payload.into(), + }))?; + index.entries.insert( + inner_id, + IndexEntry { + sealed: sealed_id, + dek: *dek, + }, + ); + } + index.inner_tip = Some(snap_id); + let _ = ctx; // ctx retained by callers for kek/manifest fields + Ok(snap_id) + } + + /// A lowest common ancestor between an inner snapshot and a public one, + /// walking the mixed graph through the resolver. Always returns a public + /// id when `Some` (a private snapshot can never be an ancestor of a + /// public tip). + fn merge_base_private( + &self, + store: &mut Store, + ctx: &PrivateCtx, + ours_inner: ObjectId, + theirs_public: ObjectId, + ) -> Result> { + let ours_anc = self.inner_ancestors(store, ctx, ours_inner)?; + let mut seen = BTreeSet::new(); + let mut q = VecDeque::new(); + q.push_back(theirs_public); + seen.insert(theirs_public); + while let Some(id) = q.pop_front() { + if ours_anc.contains(&id) { + return Ok(Some(id)); + } + for p in store.get_snapshot(&id)?.parents { + if seen.insert(p) { + q.push_back(p); + } + } + } + Ok(None) + } + + /// All ancestors of an inner snapshot (inclusive), via the resolver — + /// crosses seamlessly into public history at the fork point. + fn inner_ancestors( + &self, + store: &mut Store, + ctx: &PrivateCtx, + id: ObjectId, + ) -> Result> { + let mut set = BTreeSet::new(); + let mut q = VecDeque::new(); + q.push_back(id); + set.insert(id); + while let Some(cur) = q.pop_front() { + for p in self.get_inner_snapshot(store, ctx, &cur)?.parents { + if set.insert(p) { + q.push_back(p); + } + } + } + Ok(set) + } +} + +/// Build the tree objects for a flat `path -> (id, mode, perms)` map without +/// touching any store: returns the root tree id plus every tree object built +/// (the sealing loop decides which of them are new). Pure — the inner world's +/// analogue of `vfs::write_tree_with_perms`. +pub(crate) fn build_tree_objects( + entries: &BTreeMap, +) -> (ObjectId, Vec) { + let mut trees = Vec::new(); + let root = build_subtree(entries, "", &mut trees); + (root, trees) +} + +fn build_subtree( + entries: &BTreeMap, + prefix: &str, + out: &mut Vec, +) -> ObjectId { + let mut files: Vec = Vec::new(); + let mut dirs: BTreeSet = BTreeSet::new(); + for (path, (id, mode, perms)) in entries.range(prefix.to_string()..) { + let rel = match prefix.is_empty() { + true => path.as_str(), + false => match path.strip_prefix(prefix) { + Some(r) => r, + None => break, // sorted map: past the prefix range + }, + }; + match rel.split_once('/') { + None => files.push(TreeEntry { + name: rel.to_string(), + kind: EntryKind::Blob, + id: *id, + mode: *mode, + perms: *perms, + }), + Some((dir, _)) => { + dirs.insert(dir.to_string()); + } + } + } + for dir in dirs { + let sub_prefix = if prefix.is_empty() { + format!("{dir}/") + } else { + format!("{prefix}{dir}/") + }; + let sub_id = build_subtree(entries, &sub_prefix, out); + files.push(TreeEntry { + name: dir, + kind: EntryKind::Tree, + id: sub_id, + // Match vfs's `build_subtree_inner` byte-for-byte: an unchanged + // subtree must hash to the SAME id as its public original, or the + // copy-on-write carry check would spuriously re-seal it. + mode: FileMode(0o755), + perms: 0, + }); + } + let tree = Object::Tree(Tree::new(files)); + let id = tree.id(); + out.push(tree); + id +} + +/// Every tree id reachable under a PUBLIC root (subtree ids only; blob ids +/// come from `tree_file_entries_with_perms`). Helper for the merge-completion +/// carried set. +fn public_tree_ids(store: &mut Store, root: ObjectId) -> Result> { + let mut out = BTreeSet::new(); + let mut stack = vec![root]; + while let Some(id) = stack.pop() { + if !out.insert(id) { + continue; + } + let tree = store.get_tree(&id)?; + for e in tree.entries { + if e.kind == EntryKind::Tree { + stack.push(e.id); + } + } + } + Ok(out) +} + +/// Merge new public reachability anchors into a manifest's existing set, +/// deduped and sorted (canonical). Anchors are cumulative across a branch's +/// history; a redundant anchor (already reachable from `base` or another +/// anchor) is harmless — the reachability walk is idempotent — so no store +/// walk is needed here to prune, and the growth is bounded by the number of +/// merges (a documented manifest-scalability boundary, ADR-0044 deferred). +pub(crate) fn extend_anchors( + existing: &[ObjectId], + new: impl Iterator, +) -> Vec { + let mut set: BTreeSet = existing.iter().copied().collect(); + set.extend(new); + set.into_iter().collect() +} + +/// Escrow/recipient sets must never be empty on a rewrap (seal-to-zero +/// footgun — the same rule `secrets::require_recipients` enforces). +pub(crate) fn require_nonempty_recipients(keys: &[PublicKey], what: &str) -> Result<()> { + if keys.is_empty() { + return Err(Error::InvalidArgument(format!( + "{what} would leave zero recipients able to read the branch; refusing" + ))); + } + Ok(()) +} + +/// Membership helpers shared by grant/revoke: the recipient ids currently +/// wrapped on a manifest. +pub(crate) fn wrapped_ids(manifest: &BranchManifest) -> BTreeSet { + manifest + .kek_wraps + .iter() + .map(|w| w.recipient_id.clone()) + .collect() +} + +impl Repo { + // ---- membership ----------------------------------------------------------- + + /// Grant: wrap the branch KEK for one more recipient. O(1) — one manifest + /// rewrite, no object churn, no index change. + pub fn branch_grant( + &self, + name: &str, + identity: &SecretKey, + new: &PublicKey, + ) -> Result { + self.guard_states()?; + let ctx = self.open_private_branch(name, Some(identity))?; + let mut kek_wraps = ctx.manifest.kek_wraps.clone(); + let new_id = new.recipient_id().to_string(); + kek_wraps.retain(|w| w.recipient_id != new_id); + kek_wraps.push(scl_crypto::wrap_kek_for(&ctx.kek, new)); + let manifest = BranchManifest { + base: ctx.manifest.base, + prev: Some(ctx.manifest_id), + anchors: ctx.manifest.anchors.clone(), + closure: ctx.manifest.closure.clone(), + index_ct: ctx.manifest.index_ct.clone(), + kek_wraps, + }; + self.replace_manifest(name, &ctx, manifest, &format!("branch grant {name}")) + } + + /// Revoke + rewrap, atomically: mint a fresh KEK, re-encrypt the index + /// under it, and wrap it for every remaining recipient (resolved to + /// public keys by the caller) — zero content plaintext, zero object-id + /// churn. `remaining` must cover every currently-wrapped recipient except + /// the revoked one; anything uncoverable is a loud error, never silent + /// access loss. + pub fn branch_revoke( + &self, + name: &str, + identity: &SecretKey, + revoked: &RecipientId, + remaining: &[PublicKey], + ) -> Result { + self.guard_states()?; + let ctx = self.open_private_branch(name, Some(identity))?; + let current = wrapped_ids(&ctx.manifest); + if !current.contains(revoked.as_str()) { + return Err(Error::InvalidArgument(format!( + "recipient {revoked} holds no wrap on branch {name}" + ))); + } + require_nonempty_recipients(remaining, "revoke")?; + let remaining_ids: BTreeSet = remaining + .iter() + .map(|pk| pk.recipient_id().to_string()) + .collect(); + if remaining_ids.contains(revoked.as_str()) { + return Err(Error::InvalidArgument(format!( + "revoked recipient {revoked} is still in the remaining set" + ))); + } + let uncovered: Vec = current + .iter() + .filter(|id| id.as_str() != revoked.as_str() && !remaining_ids.contains(*id)) + .cloned() + .collect(); + if !uncovered.is_empty() { + return Err(Error::InvalidArgument(format!( + "cannot rewrap for current recipient(s) {} — no public key known \ + (add them to .sc/recipients.toml or revoke them too)", + uncovered.join(", ") + ))); + } + + let new_kek = scl_crypto::generate_kek(); + let kek_wraps: Vec = remaining + .iter() + .map(|pk| scl_crypto::wrap_kek_for(&new_kek, pk)) + .collect(); + let manifest = BranchManifest { + base: ctx.manifest.base, + prev: Some(ctx.manifest_id), + anchors: ctx.manifest.anchors.clone(), + closure: ctx.manifest.closure.clone(), + index_ct: ctx.index.encrypt(&new_kek), + kek_wraps, + }; + self.replace_manifest(name, &ctx, manifest, &format!("branch revoke {name}")) + } + + fn replace_manifest( + &self, + name: &str, + ctx: &PrivateCtx, + manifest: BranchManifest, + desc: &str, + ) -> Result { + let store_arc = self.vfs.store(); + let new_mid = store_arc.lock().unwrap().put(Object::Manifest(manifest))?; + refs::write_branch_tip(&self.layout, name, &new_mid)?; + let head = refs::current_branch(&self.layout)?; + crate::oplog::record( + &self.layout, + desc, + &head, + &head, + &[(name.to_string(), Some(ctx.manifest_id), Some(new_mid))], + )?; + Ok(new_mid) + } + + fn guard_states(&self) -> Result<()> { + if crate::merge_state::in_progress(&self.layout) { + return Err(Error::MergeInProgress); + } + if crate::pick_state::in_progress(&self.layout) { + return Err(Error::PickInProgress); + } + if crate::rebase_state::in_progress(&self.layout) { + return Err(Error::RebaseInProgress); + } + Ok(()) + } + + // ---- merge (public -> private) --------------------------------------------- + + /// Merge a PUBLIC branch into the private branch at HEAD (the one legal + /// direction — private → public is refused everywhere and only `publish` + /// crosses it). The three-way itself is the existing `three_way_files`, + /// run against a RAM-only ephemeral store holding the three tree closures + /// (decrypted inner objects hash to their inner ids, so content + /// addressing is consistent and no plaintext ever touches disk). + pub fn merge_into_private( + &self, + branch: &str, + author: &str, + identity: Option<&SecretKey>, + ) -> Result<(ObjectId, Vec)> { + self.guard_states()?; + let (head, mid, manifest) = self + .head_private()? + .ok_or_else(|| Error::NotPrivateBranch("HEAD".into()))?; + let ctx = self.open_private(&head, mid, manifest, identity)?; + let sk = identity.expect("open_private succeeded"); + let dirty = self.status_private(identity)?; + if !dirty.modified.is_empty() || !dirty.deleted.is_empty() { + return Err(Error::InvalidArgument( + "working tree has uncommitted changes; commit before merging".into(), + )); + } + let theirs = refs::resolve_tip(&self.layout, branch)? + .ok_or_else(|| Error::NoSuchBranch(branch.to_string()))?; + if self.manifest_at(&theirs)?.is_some() { + return Err(Error::PrivateIntegration(branch.to_string())); + } + if self.promisor()?.is_some() { + return Err(crate::promisor::partial_clone_unsupported("merge")); + } + + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let inner_tip = ctx.inner_tip()?; + let ours_snap = self.get_inner_snapshot(&mut store, &ctx, &inner_tip)?; + let theirs_snap = store.get_snapshot(&theirs)?; + + // Short-circuits over the mixed ancestry. + let ours_anc = self.inner_ancestors(&mut store, &ctx, inner_tip)?; + if ours_anc.contains(&theirs) { + return Err(Error::UpToDate); + } + if crate::merge::is_ancestor(&mut store, inner_tip, theirs).unwrap_or(false) { + // Fast-forward: only possible while the private branch has no + // private commits yet (a private inner id can never be a public + // ancestor). The branch STAYS private — the index just adopts the + // public tip as its inner tip; nothing needs sealing. + let mut index = ctx.index.clone(); + index.inner_tip = Some(theirs); + // The inner tip is now a public snapshot descended from `base` + // (not reachable from it) — anchor it so the manifest stays + // self-contained and transferable. + let anchors = extend_anchors(&ctx.manifest.anchors, std::iter::once(theirs)); + let new_manifest = BranchManifest { + base: ctx.manifest.base, + prev: Some(ctx.manifest_id), + anchors, + closure: ctx.manifest.closure.clone(), + index_ct: index.encrypt(&ctx.kek), + kek_wraps: ctx.manifest.kek_wraps.clone(), + }; + let new_mid = store.put(Object::Manifest(new_manifest.clone()))?; + let old_paths: BTreeSet = self + .inner_tree_files(&mut store, &ctx, ours_snap.root)? + .into_keys() + .collect(); + let ff_ctx = PrivateCtx { + manifest_id: new_mid, + manifest: new_manifest, + kek: ctx.kek.clone(), + index, + }; + let skipped = self.materialize_private( + &mut store, + &ff_ctx, + theirs_snap.root, + &old_paths, + &theirs_snap.protection, + sk, + )?; + drop(store); + refs::write_branch_tip(&self.layout, &head, &new_mid)?; + crate::oplog::record( + &self.layout, + &format!("merge {branch} (ff)"), + &head, + &head, + &[(head.clone(), Some(ctx.manifest_id), Some(new_mid))], + )?; + return Ok((theirs, skipped)); + } + + let base = self + .merge_base_private(&mut store, &ctx, inner_tip, theirs)? + .ok_or(Error::NoCommonAncestor)?; + let base_snap = self.get_inner_snapshot(&mut store, &ctx, &base)?; + + // Stage the three tree closures in a RAM-only store and run the + // existing three-way. Inner objects decode to plaintext values whose + // ids are exactly the inner ids the trees reference. + let mut ram = Store::with_budget(usize::MAX / 2); + self.copy_closure_inner(&mut store, &ctx, ours_snap.root, &mut ram)?; + copy_closure_public(&mut store, theirs_snap.root, &mut ram)?; + copy_closure_public(&mut store, base_snap.root, &mut ram)?; + let fm = crate::merge::three_way_files( + &mut ram, + Some((base_snap.root, &base_snap.protection)), + (ours_snap.root, &ours_snap.protection), + (theirs_snap.root, &theirs_snap.protection), + identity, + )?; + let secrets = crate::merge::merge_secrets( + &base_snap.secrets, + &ours_snap.secrets, + &theirs_snap.secrets, + )?; + let prefixes = crate::protect::merge_prefixes( + &ours_snap.protection.prefixes, + &theirs_snap.protection.prefixes, + ); + + // Decide the merged file map for the inner world: + // - carried PROTECTED ciphertext keeps its id + wraps; + // - everything else (plain carries, diff3 outputs, decrypted + // protected merges) becomes a plain inner blob — publish re-seals + // protected prefixes into public form. + let mut entries: BTreeMap = BTreeMap::new(); + let mut new_blobs: BTreeMap> = BTreeMap::new(); + let mut wrapped: BTreeMap> = BTreeMap::new(); + for f in &fm.files { + if f.perms & PROTECTED != 0 && !f.needs_encrypt { + let id = Object::blob(f.bytes.clone()).id(); + if let Some(wks) = fm.wrapped_carry.get(&id) { + wrapped.insert(id, wks.clone()); + } + entries.insert(f.path.clone(), (id, f.mode, f.perms)); + } else { + let id = Object::blob(f.bytes.clone()).id(); + new_blobs.entry(id).or_insert_with(|| f.bytes.clone()); + entries.insert(f.path.clone(), (id, f.mode, 0)); + } + } + + if !fm.conflicts.is_empty() { + // Conflict state: everything lands on disk in plaintext (markers, + // sidecars, decrypted protected content — we hold the identity by + // construction). Never through the CAS. Completion is the next + // `commit_private`, which reads MERGE_HEAD. + let prior_paths: BTreeSet = self + .inner_tree_files(&mut store, &ctx, ours_snap.root)? + .into_keys() + .collect(); + drop(store); + for f in &fm.files { + let full = worktree::safe_join(&self.layout.root, &f.path)?; + if let Some(parent) = full.parent() { + std::fs::create_dir_all(parent)?; + } + if f.perms & PROTECTED != 0 && !f.needs_encrypt { + // Carried ciphertext: write the decrypted form for the + // resolver to see; completion re-seals it as needed. + let id = Object::blob(f.bytes.clone()).id(); + let prots = Protection { + prefixes: prefixes.clone(), + wrapped: fm.wrapped_carry.clone(), + }; + match crate::protect::decrypt_with(&f.bytes, &id, &[&prots], sk, &f.path) { + Ok(pt) => std::fs::write(&full, &pt[..])?, + Err(_) => continue, // undecryptable: leave prior state + } + } else { + std::fs::write(&full, &f.bytes)?; + } + } + for (name, bytes) in &fm.sidecars { + let full = worktree::safe_join(&self.layout.root, name)?; + std::fs::write(&full, bytes)?; + } + let merged_paths: BTreeSet = fm.files.iter().map(|f| f.path.clone()).collect(); + for p in prior_paths.difference(&merged_paths) { + let full = worktree::safe_join(&self.layout.root, p)?; + let _ = std::fs::remove_file(full); + } + crate::merge_state::write(&self.layout, &theirs, &fm.conflicts, None)?; + return Err(Error::MergeConflicts(fm.conflicts.len())); + } + + // Clean merge: seal + commit in one step. + let mut carried = self.inner_tree_closure(&mut store, &ctx, ours_snap.root)?; + carried.extend( + worktree::tree_file_entries_with_perms(&mut store, theirs_snap.root)? + .values() + .map(|(id, _, _)| *id), + ); + carried.extend(public_tree_ids(&mut store, theirs_snap.root)?); + carried.extend( + worktree::tree_file_entries_with_perms(&mut store, base_snap.root)? + .values() + .map(|(id, _, _)| *id), + ); + carried.extend(public_tree_ids(&mut store, base_snap.root)?); + + let mut index = ctx.index.clone(); + let new_tip = self.seal_commit( + &mut store, + &ctx, + &mut index, + &carried, + entries, + new_blobs, + vec![inner_tip, theirs], + secrets, + Protection { prefixes, wrapped }, + author, + &format!("merge {branch}"), + )?; + // Anchor the merged-in public tip: its objects were carried into the + // sealed tree as unsealed public references (copy-on-write). + let anchors = extend_anchors(&ctx.manifest.anchors, std::iter::once(theirs)); + let new_manifest = BranchManifest { + base: ctx.manifest.base, + prev: Some(ctx.manifest_id), + anchors, + closure: index.entries.values().map(|e| e.sealed).collect(), + index_ct: index.encrypt(&ctx.kek), + kek_wraps: ctx.manifest.kek_wraps.clone(), + }; + let new_mid = store.put(Object::Manifest(new_manifest.clone()))?; + // Re-lay the working tree from the merged inner tip. + let old_paths: BTreeSet = self + .inner_tree_files(&mut store, &ctx, ours_snap.root)? + .into_keys() + .collect(); + let merged_ctx = PrivateCtx { + manifest_id: new_mid, + manifest: new_manifest, + kek: ctx.kek.clone(), + index, + }; + let merged_root = self + .get_inner_snapshot(&mut store, &merged_ctx, &new_tip)? + .root; + let merged_prot = self + .get_inner_snapshot(&mut store, &merged_ctx, &new_tip)? + .protection; + let skipped = self.materialize_private( + &mut store, + &merged_ctx, + merged_root, + &old_paths, + &merged_prot, + sk, + )?; + drop(store); + refs::write_branch_tip(&self.layout, &head, &new_mid)?; + crate::oplog::record( + &self.layout, + &format!("merge {branch}"), + &head, + &head, + &[(head.clone(), Some(ctx.manifest_id), Some(new_mid))], + )?; + Ok((new_tip, skipped)) + } + + /// Copy an inner tree closure (trees + blobs, decoded plaintext values) + /// into a RAM-only store. Ids are content addresses of the decoded + /// values, so the copy is a faithful CAS fragment. + fn copy_closure_inner( + &self, + store: &mut Store, + ctx: &PrivateCtx, + root: ObjectId, + ram: &mut Store, + ) -> Result<()> { + let mut stack = vec![root]; + while let Some(id) = stack.pop() { + if ram.contains(&id) { + continue; + } + let obj = self.get_inner(store, ctx, &id)?; + if let Object::Tree(t) = &obj { + for e in &t.entries { + stack.push(e.id); + } + } + ram.put(obj)?; + } + Ok(()) + } + + // ---- publish ---------------------------------------------------------------- + + /// Publish a private branch: replay its full private history as plaintext + /// public snapshots (messages/authors/timestamps preserved, parents + /// remapped, fork point unchanged), re-sealing protected-prefix files + /// through the normal P7/P33 path, then move the branch ref to the + /// published tip. The scanner runs over every decrypted plain file BEFORE + /// any public object is written — a secret committed under seal must not + /// sail into plaintext history at the moment of publish. One ref move = + /// the atomic point; the manifest chain becomes garbage for `sc gc`. + pub fn branch_publish(&self, name: &str, identity: &SecretKey) -> Result<(ObjectId, usize)> { + self.guard_states()?; + let ctx = self.open_private_branch(name, Some(identity))?; + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let inner_tip = ctx.inner_tip()?; + + // Nothing private ever committed: the ref just becomes the public tip. + if !ctx.index.entries.contains_key(&inner_tip) { + drop(store); + refs::write_branch_tip(&self.layout, name, &inner_tip)?; + let head = refs::current_branch(&self.layout)?; + crate::oplog::record( + &self.layout, + &format!("branch publish {name}"), + &head, + &head, + &[(name.to_string(), Some(ctx.manifest_id), Some(inner_tip))], + )?; + return Ok((inner_tip, 0)); + } + + // Collect the private snapshot chain (ids in the index), topo-ordered + // parents-first. + let order = { + let mut order: Vec = Vec::new(); + let mut seen: BTreeSet = BTreeSet::new(); + let mut stack = vec![(inner_tip, false)]; + while let Some((id, expanded)) = stack.pop() { + if expanded { + order.push(id); + continue; + } + if !seen.insert(id) { + continue; + } + stack.push((id, true)); + for p in self.get_inner_snapshot(&mut store, &ctx, &id)?.parents { + if ctx.index.entries.contains_key(&p) && !seen.contains(&p) { + stack.push((p, false)); + } + } + } + order + }; + + // Phase 1: build every public object in memory, scanning as we go. + // NOTHING is written until the whole branch passes. + let mut to_put: Vec = Vec::new(); + let mut id_map: BTreeMap = BTreeMap::new(); // inner -> published + let mut tip_cache_records: Vec<(String, Vec, ObjectId)> = Vec::new(); + for inner_id in &order { + let snap = self.get_inner_snapshot(&mut store, &ctx, inner_id)?; + let files = self.inner_tree_files(&mut store, &ctx, snap.root)?; + let mut plain: Vec<(String, Vec, FileMode)> = Vec::new(); + let mut to_encrypt: Vec<(String, Vec, FileMode, Vec<[u8; 32]>)> = Vec::new(); + let mut published: BTreeMap = BTreeMap::new(); + let mut wrapped: BTreeMap> = BTreeMap::new(); + for (path, (id, mode, perms)) in &files { + if perms & PROTECTED != 0 { + // Carried path-ciphertext: already a public object; keep + // the entry + its wraps verbatim. + if let Some(wks) = snap.protection.wrapped.get(id) { + wrapped.insert(*id, wks.clone()); + } + published.insert(path.clone(), (*id, *mode, *perms)); + continue; + } + let bytes = self.inner_blob(&mut store, &ctx, id)?; + match crate::protect::matching_prefix(&snap.protection, path) { + Some(rule) => { + to_encrypt.push((path.clone(), bytes, *mode, rule.granted_keys())) + } + None => plain.push((path.clone(), bytes, *mode)), + } + } + let report = self.scan_files(&plain)?; + if !report.is_empty() { + return Err(Error::SecretDetected(report)); + } + // Keep the pre-seal plaintexts of the TIP snapshot's protected + // files: the cache records below need them so the working tree + // reads clean right after publish (rewrap's discipline). + let seal_plaintexts: BTreeMap> = if *inner_id == inner_tip { + to_encrypt + .iter() + .map(|(p, b, _, _)| (p.clone(), b.clone())) + .collect() + } else { + BTreeMap::new() + }; + let (encrypted, fresh) = crate::protect::encrypt_protected(to_encrypt)?; + for (path, ct, mode, perms) in encrypted { + let blob = Object::blob(ct.clone()); + let bid = blob.id(); + published.insert(path.clone(), (bid, mode, perms)); + to_put.push(blob); + if let Some(b) = seal_plaintexts.get(&path) { + tip_cache_records.push((path.clone(), b.clone(), bid)); + } + } + for (id, wks) in fresh { + wrapped.insert(id, wks); + } + for (path, bytes, mode) in &plain { + let blob = Object::blob(bytes.clone()); + published.insert(path.clone(), (blob.id(), *mode, 0)); + to_put.push(blob); + } + let (root, trees) = build_tree_objects(&published); + to_put.extend(trees); + let parents: Vec = snap + .parents + .iter() + .map(|p| id_map.get(p).copied().unwrap_or(*p)) + .collect(); + let pub_snap = Object::Snapshot(Snapshot { + root, + parents, + author: snap.author.clone(), + timestamp: snap.timestamp, + message: snap.message.clone(), + secrets: snap.secrets.clone(), + protection: Protection { + prefixes: snap.protection.prefixes.clone(), + wrapped, + }, + }); + id_map.insert(*inner_id, pub_snap.id()); + to_put.push(pub_snap); + } + let published_tip = *id_map.get(&inner_tip).expect("tip is in order"); + let resealed = tip_cache_records.len(); + + // Phase 2: everything passed — write the objects, then the ref. + for obj in to_put { + store.put(obj)?; + } + drop(store); + refs::write_branch_tip(&self.layout, name, &published_tip)?; + // Cache records so protected files read clean immediately (best-effort). + if !tip_cache_records.is_empty() { + if let Ok(mut cache) = self.open_protected_cache() { + for (path, plaintext, bid) in &tip_cache_records { + cache.record(path, plaintext, *bid); + } + cache.save_best_effort(); + } + } + let head = refs::current_branch(&self.layout)?; + crate::oplog::record( + &self.layout, + &format!("branch publish {name}"), + &head, + &head, + &[(name.to_string(), Some(ctx.manifest_id), Some(published_tip))], + )?; + Ok((published_tip, resealed)) + } +} + +/// Copy a PUBLIC tree closure (trees + blobs) into the RAM store. +fn copy_closure_public(store: &mut Store, root: ObjectId, ram: &mut Store) -> Result<()> { + let mut stack = vec![root]; + while let Some(id) = stack.pop() { + if ram.contains(&id) { + continue; + } + let obj = store.get(&id)?; + if let Object::Tree(t) = &obj { + for e in &t.entries { + stack.push(e.id); + } + } + ram.put(obj)?; + } + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::repo::Repo; + + fn tmp_root(tag: &str) -> std::path::PathBuf { + let p = std::env::temp_dir().join(format!("scl-repo-priv-{tag}-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&p); + std::fs::create_dir_all(&p).unwrap(); + p + } + + fn write(root: &std::path::Path, rel: &str, content: &str) { + let p = root.join(rel); + if let Some(parent) = p.parent() { + std::fs::create_dir_all(parent).unwrap(); + } + std::fs::write(p, content).unwrap(); + } + + /// A repo with one public commit plus alice/bob keypairs. + fn setup( + tag: &str, + ) -> ( + std::path::PathBuf, + Repo, + (SecretKey, PublicKey), + (SecretKey, PublicKey), + ) { + let root = tmp_root(tag); + let repo = Repo::init(&root).unwrap(); + write(&root, "readme.txt", "public content\n"); + write(&root, "src/lib.rs", "pub fn hello() {}\n"); + repo.commit("tester", "public base").unwrap(); + let alice = scl_crypto::generate_keypair(); + let bob = scl_crypto::generate_keypair(); + (root, repo, alice, bob) + } + + /// Every non-sealed object in the store must be free of `needle`: the + /// honest opacity check (on-disk bytes are zstd-compressed, so a raw + /// byte scan of `.sc/objects` would pass vacuously). + fn assert_opaque(repo: &Repo, needle: &[u8]) { + let store_arc = repo.vfs().store(); + let mut store = store_arc.lock().unwrap(); + let ids = store.all_ids().unwrap(); + for id in ids { + let obj = store.get(&id).unwrap(); + match &obj { + Object::Sealed(_) | Object::Manifest(_) => continue, + _ => {} + } + let enc = obj.encode(); + assert!( + !enc.windows(needle.len()).any(|w| w == needle), + "plaintext {:?} leaked into a public {} object {id}", + String::from_utf8_lossy(needle), + obj.kind_name() + ); + } + } + + #[test] + fn lifecycle_commit_status_log_and_disk_hygiene() { + let (root, repo, (alice_sk, _alice_pk), _bob) = setup("life"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + + write(&root, "fix.txt", "THE-SECRET-FIX\n"); + write(&root, "src/lib.rs", "pub fn hello() { patched(); }\n"); + let st = repo.status_private(Some(&alice_sk)).unwrap(); + assert_eq!(st.added, vec!["fix.txt".to_string()]); + assert_eq!(st.modified, vec!["src/lib.rs".to_string()]); + + repo.commit_private("alice", "private: SECRET-MSG", Some(&alice_sk)) + .unwrap(); + let st = repo.status_private(Some(&alice_sk)).unwrap(); + assert!(st.added.is_empty() && st.modified.is_empty() && st.deleted.is_empty()); + + // Log: private commit first, then straight into public history. + let log = repo.log_private(Some(&alice_sk)).unwrap(); + assert_eq!(log.len(), 2); + assert_eq!(log[0].1.message, "private: SECRET-MSG"); + assert_eq!(log[1].1.message, "public base"); + + // Opacity: no non-sealed object carries the content, the path, or + // the message. + assert_opaque(&repo, b"THE-SECRET-FIX"); + assert_opaque(&repo, b"fix.txt"); + assert_opaque(&repo, b"SECRET-MSG"); + + // Switching to the public branch removes the private plaintext from + // disk and restores the public content. + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + assert!(!root.join("fix.txt").exists()); + assert_eq!( + std::fs::read_to_string(root.join("src/lib.rs")).unwrap(), + "pub fn hello() {}\n" + ); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn non_recipient_is_refused_everywhere() { + let (root, repo, (alice_sk, _), (bob_sk, _bob_pk)) = setup("noaccess"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "sealed\n"); + repo.commit_private("alice", "m", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + + // Bob (not a recipient) and keyless are both refused. + assert!(matches!( + repo.switch_with_identity("fix", Some(&bob_sk)), + Err(Error::PrivateNoAccess(_)) + )); + assert!(matches!( + repo.switch_with_identity("fix", None), + Err(Error::PrivateNoAccess(_)) + )); + assert!(!repo.can_open_private("fix", Some(&bob_sk)).unwrap()); + assert!(repo.can_open_private("fix", Some(&alice_sk)).unwrap()); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn cow_sealing_unchanged_content_is_never_resealed() { + let (root, repo, (alice_sk, _), _bob) = setup("cow"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + // Creation seals nothing (copy-on-write: empty closure). + let (_, m0) = repo.branch_manifest("fix").unwrap().unwrap(); + assert!(m0.closure.is_empty(), "creation must seal nothing"); + + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "v1\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + // One new blob + one changed root tree + one snapshot = 3 sealed. + let (_, m1) = repo.branch_manifest("fix").unwrap().unwrap(); + assert_eq!( + m1.closure.len(), + 3, + "unchanged src/ subtree must be carried" + ); + + // A commit with no working-tree change seals exactly ONE new object + // (the snapshot; even the root tree id is unchanged). + repo.commit_private("alice", "c2 empty", Some(&alice_sk)) + .unwrap(); + let (_, m2) = repo.branch_manifest("fix").unwrap().unwrap(); + assert_eq!( + m2.closure.len(), + 4, + "no-change commit seals only the snapshot" + ); + assert_eq!(m2.prev, Some(Object::Manifest(m1.clone()).id())); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn grant_is_wrap_only_and_revoke_rotates_the_kek() { + let (root, repo, (alice_sk, alice_pk), (bob_sk, bob_pk)) = setup("members"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "v1\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + let (_, before) = repo.branch_manifest("fix").unwrap().unwrap(); + + // Grant: bob can open; closure and index bytes are untouched. + assert!(!repo.can_open_private("fix", Some(&bob_sk)).unwrap()); + repo.branch_grant("fix", &alice_sk, &bob_pk).unwrap(); + assert!(repo.can_open_private("fix", Some(&bob_sk)).unwrap()); + let (_, after_grant) = repo.branch_manifest("fix").unwrap().unwrap(); + assert_eq!(after_grant.closure, before.closure); + assert_eq!(after_grant.index_ct, before.index_ct, "grant is wrap-only"); + + // Revoke bob: fresh KEK, index re-encrypted, object ids untouched. + repo.branch_revoke( + "fix", + &alice_sk, + &bob_pk.recipient_id(), + std::slice::from_ref(&alice_pk), + ) + .unwrap(); + let (_, after_revoke) = repo.branch_manifest("fix").unwrap().unwrap(); + assert!(!repo.can_open_private("fix", Some(&bob_sk)).unwrap()); + assert!(repo.can_open_private("fix", Some(&alice_sk)).unwrap()); + assert_eq!(after_revoke.closure, before.closure, "zero object churn"); + assert_ne!(after_revoke.index_ct, before.index_ct, "index re-encrypted"); + + // Guard rails: revoking with a remaining set that misses a current + // recipient, or an empty one, is a loud error. + repo.branch_grant("fix", &alice_sk, &bob_pk).unwrap(); + assert!(matches!( + repo.branch_revoke("fix", &alice_sk, &bob_pk.recipient_id(), &[]), + Err(Error::InvalidArgument(_)) + )); + let (charlie_sk, charlie_pk) = scl_crypto::generate_keypair(); + let _ = charlie_sk; + repo.branch_grant("fix", &alice_sk, &charlie_pk).unwrap(); + // Remaining covers alice but not charlie -> refuse (never silent loss). + assert!(matches!( + repo.branch_revoke( + "fix", + &alice_sk, + &bob_pk.recipient_id(), + std::slice::from_ref(&alice_pk) + ), + Err(Error::InvalidArgument(_)) + )); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn publish_replays_history_and_the_branch_becomes_public() { + let (root, repo, (alice_sk, _), _bob) = setup("publish"); + let base = repo.head_tip().unwrap().unwrap(); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "v1\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "v2\n"); + repo.commit_private("alice", "c2", Some(&alice_sk)).unwrap(); + + let (published_tip, resealed) = repo.branch_publish("fix", &alice_sk).unwrap(); + assert_eq!(resealed, 0); + // The ref now points at a plaintext snapshot; normal log works and + // history is preserved commit-by-commit down to the unchanged base. + assert!(repo.branch_manifest("fix").unwrap().is_none()); + let log = repo.log().unwrap(); + assert_eq!(log[0].0, published_tip); + assert_eq!(log[0].1.message, "c2"); + assert_eq!(log[1].1.message, "c1"); + assert_eq!(log[2].0, base, "fork-point parent keeps its public id"); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn publish_scanner_gate_aborts_before_any_public_write() { + let (root, repo, (alice_sk, _), _bob) = setup("pubscan"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + // A secret-looking file: allowlist it so the private COMMIT passes, + // then remove the allowlist so PUBLISH must catch it. + let secret = "aws_secret_access_key = \"AKIAIOSFODNN7EXAMPLE99\"\n"; + write(&root, "creds.env", secret); + let blob_id = Object::blob(secret.as_bytes().to_vec()).id(); + let allow = root.join(".sc/scanner-allowlist.toml"); + std::fs::write( + &allow, + format!("[[allow]]\nblob = \"{}\"\n", blob_id.to_hex()), + ) + .unwrap(); + repo.commit_private("alice", "sealed secret", Some(&alice_sk)) + .unwrap(); + std::fs::remove_file(&allow).unwrap(); + + let (mid_before, _) = repo.branch_manifest("fix").unwrap().unwrap(); + let objects_before = { + let store_arc = repo.vfs().store(); + let n = store_arc.lock().unwrap().all_ids().unwrap().len(); + n + }; + assert!(matches!( + repo.branch_publish("fix", &alice_sk), + Err(Error::SecretDetected(_)) + )); + // Nothing public was written and the ref did not move. + let (mid_after, _) = repo.branch_manifest("fix").unwrap().unwrap(); + assert_eq!( + mid_before, mid_after, + "ref must not move on a failed publish" + ); + let objects_after = { + let store_arc = repo.vfs().store(); + let n = store_arc.lock().unwrap().all_ids().unwrap().len(); + n + }; + assert_eq!( + objects_before, objects_after, + "a failed publish must write no objects" + ); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn merge_public_into_private_clean_then_conflicted() { + let (root, repo, (alice_sk, _), _bob) = setup("mergein"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "private line\n"); + repo.commit_private("alice", "private c1", Some(&alice_sk)) + .unwrap(); + + // main moves on with a DISJOINT file: merge-in resolves clean. + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "main-only.txt", "from main\n"); + repo.commit("tester", "main moves").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + let (merged, _skipped) = repo + .merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap(); + assert!(root.join("main-only.txt").exists()); + assert!(root.join("fix.txt").exists()); + // Two parents: prior inner tip + main's public tip. + let log = repo.log_private(Some(&alice_sk)).unwrap(); + assert_eq!(log[0].0, merged); + assert_eq!(log[0].1.parents.len(), 2); + assert_opaque(&repo, b"private line"); + + // Conflicting edits on BOTH sides stop with markers + merge state. + // `whichpub.txt` is a public file added alongside the conflict — the + // conflict-completion path must anchor it just like the clean path. + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "shared.txt", "base\n"); + write(&root, "whichpub.txt", "public-conflict-era\n"); + repo.commit("tester", "add shared").unwrap(); + let pub_blob = Object::blob("public-conflict-era\n".as_bytes().to_vec()).id(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + repo.merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap(); + write(&root, "shared.txt", "private edit\n"); + repo.commit_private("alice", "edit shared", Some(&alice_sk)) + .unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "shared.txt", "public edit\n"); + repo.commit("tester", "edit shared on main").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert!(matches!( + repo.merge_into_private("main", "alice", Some(&alice_sk)), + Err(Error::MergeConflicts(1)) + )); + let marked = std::fs::read_to_string(root.join("shared.txt")).unwrap(); + assert!(marked.contains("<<<<<<<"), "markers on disk: {marked}"); + // Resolve + complete through the private commit path: two parents. + write(&root, "shared.txt", "resolved\n"); + let completed = repo + .commit_private("alice", "resolve merge", Some(&alice_sk)) + .unwrap(); + let log = repo.log_private(Some(&alice_sk)).unwrap(); + assert_eq!(log[0].0, completed); + assert_eq!(log[0].1.parents.len(), 2, "completion records both parents"); + assert!(!crate::merge_state::in_progress(repo.layout())); + // The CONFLICT-completion path anchors merged-in public content too: + // a public file brought in alongside the conflict stays reachable + // from the manifest alone (transferable), same as the clean path. + let (mid, _) = repo.branch_manifest("fix").unwrap().unwrap(); + let reachable = { + let store_arc = repo.vfs().store(); + let mut store = store_arc.lock().unwrap(); + crate::reachable::reachable_objects(&mut *store, &[mid]).unwrap() + }; + assert!( + reachable.contains(&pub_blob), + "conflict-completed merge must anchor its merged-in public blob" + ); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn merged_in_private_branch_is_self_contained_for_transfer() { + // Regression (advisor review): a public commit merged INTO a private + // branch is carried into the sealed tree as an unsealed public + // reference (copy-on-write). Without a reachability anchor on the + // manifest, pushing only the private branch to a peer lacking that + // public commit would strand the sealed tree. The manifest's closure + // walk MUST reach the merged-in public blob. + let (root, repo, (alice_sk, _), _bob) = setup("selfcontained"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "private\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + + // main gains a NEW file after the fork, then merge it in. + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "main-only.txt", "public-after-fork\n"); + repo.commit("tester", "b2").unwrap(); + let main_blob = Object::blob("public-after-fork\n".as_bytes().to_vec()).id(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + repo.merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap(); + + let (mid, _) = repo.branch_manifest("fix").unwrap().unwrap(); + // The crisp discriminator: reachability from the manifest tip ALONE + // (a keyless transfer's want set) must contain the merged-in blob. + let store_arc = repo.vfs().store(); + let reachable = { + let mut store = store_arc.lock().unwrap(); + crate::reachable::reachable_objects(&mut *store, &[mid]).unwrap() + }; + assert!( + reachable.contains(&main_blob), + "merged-in public blob must be reachable from the private manifest \ + (else the branch is non-transferable)" + ); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn recipient_opens_a_merged_branch_cloned_without_the_public_source() { + // The transfer-level twin: clone ONLY the private branch's closure + // into a fresh peer that never received the public branch the merge + // pulled from, and assert a recipient can still switch onto it. Built + // by copying exactly the manifest's reachable set (what a filtered + // want for that ref would transfer) into a fresh store. + let (root, repo, (alice_sk, _), _bob) = setup("mergeclone"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "private\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + write(&root, "main-only.txt", "public-after-fork\n"); + repo.commit("tester", "b2").unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + repo.merge_into_private("main", "alice", Some(&alice_sk)) + .unwrap(); + let (mid, _) = repo.branch_manifest("fix").unwrap().unwrap(); + + // Peer repo: copy the manifest's reachable closure only (no `main` + // ref, no oplog), point `fix` at the manifest, and open it. + let peer_root = tmp_root("mergeclone-peer"); + let peer = Repo::init(&peer_root).unwrap(); + { + let src_arc = repo.vfs().store(); + let mut src = src_arc.lock().unwrap(); + let reachable = crate::reachable::reachable_objects(&mut *src, &[mid]).unwrap(); + let dst_arc = peer.vfs().store(); + let mut dst = dst_arc.lock().unwrap(); + for id in &reachable { + let obj = src.get(id).unwrap(); + dst.put(obj).unwrap(); + } + } + crate::refs::write_branch_tip(peer.layout(), "fix", &mid).unwrap(); + // Leave HEAD on the unborn default (like a fresh clone) so the switch + // is a genuine cross-branch materialize, not a same-branch no-op that + // would dirty-check an unmaterialized tree. + + // The recipient opens the merged branch on a peer that never had `main`. + let skipped = peer + .switch_with_identity("fix", Some(&alice_sk)) + .expect("recipient must open a merged branch cloned without the public source"); + assert!(skipped.is_empty()); + assert_eq!( + std::fs::read_to_string(peer_root.join("main-only.txt")).unwrap(), + "public-after-fork\n", + "the merged-in public file must materialize on the peer" + ); + assert_eq!( + std::fs::read_to_string(peer_root.join("fix.txt")).unwrap(), + "private\n" + ); + + drop(repo); + drop(peer); + std::fs::remove_dir_all(&root).unwrap(); + std::fs::remove_dir_all(&peer_root).unwrap(); + } + + #[test] + fn private_to_public_integration_is_refused() { + let (root, repo, (alice_sk, _), _bob) = setup("valve"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "sealed\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + + assert!(matches!( + repo.merge_with_identity("fix", "tester", None), + Err(Error::PrivateIntegration(_)) + )); + assert!(matches!( + repo.cherry_pick("fix", "tester", None, None), + Err(Error::PrivateIntegration(_)) + )); + assert!(matches!( + repo.rebase("fix", "tester", None), + Err(Error::PrivateIntegration(_)) + )); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn undo_restores_the_previous_manifest() { + let (root, repo, (alice_sk, _), _bob) = setup("undo"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "v1\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + let (mid1, _) = repo.branch_manifest("fix").unwrap().unwrap(); + write(&root, "fix.txt", "v2\n"); + repo.commit_private("alice", "c2", Some(&alice_sk)).unwrap(); + + repo.undo().unwrap(); + let (mid_after, _) = repo.branch_manifest("fix").unwrap().unwrap(); + assert_eq!(mid_after, mid1, "undo restores the prior manifest"); + // The branch still opens and reads correctly. + let log = repo.log_private(Some(&alice_sk)).unwrap(); + assert_eq!(log[0].1.message, "c1"); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn gc_preserves_the_manifest_closure() { + let (root, repo, (alice_sk, _), _bob) = setup("gc"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "survives gc\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + + repo.gc(std::time::Duration::ZERO).unwrap(); + + // Everything still reads after a zero-grace gc. + let log = repo.log_private(Some(&alice_sk)).unwrap(); + assert_eq!(log[0].1.message, "c1"); + let st = repo.status_private(Some(&alice_sk)).unwrap(); + assert!(st.added.is_empty() && st.modified.is_empty() && st.deleted.is_empty()); + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn filtered_clone_excludes_private_branches_full_clone_carries_them() { + let (root, repo, (alice_sk, _), _bob) = setup("clonefilter"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "src/private.rs", "sealed\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + repo.switch_with_identity("main", Some(&alice_sk)).unwrap(); + drop(repo); + + // Full clone: the private ref travels (ciphertext + manifest). + let full_dst = tmp_root("clonefilter-full"); + let full = Repo::clone_to(&root, &full_dst).unwrap(); + assert!(full.branch_manifest("fix").unwrap().is_some()); + full.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + assert_eq!( + std::fs::read_to_string(full_dst.join("src/private.rs")).unwrap(), + "sealed\n" + ); + drop(full); + + // Filtered clone: the private ref is excluded entirely. + let part_dst = tmp_root("clonefilter-part"); + let part = Repo::clone_url_filtered( + &root.display().to_string(), + &part_dst, + Some(&["src/".to_string()]), + ) + .unwrap(); + let names: Vec = part + .branches() + .unwrap() + .into_iter() + .map(|(n, _)| n) + .collect(); + assert!( + !names.contains(&"fix".to_string()), + "filtered clone must exclude private refs" + ); + drop(part); + + std::fs::remove_dir_all(&root).unwrap(); + std::fs::remove_dir_all(&full_dst).unwrap(); + std::fs::remove_dir_all(&part_dst).unwrap(); + } + + #[test] + fn wire_pack_for_a_private_branch_carries_ciphertext_only() { + let (root, repo, (alice_sk, _), _bob) = setup("wire"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "wire-secret.txt", "WIRE-SENTINEL-CONTENT\n"); + repo.commit_private("alice", "WIRE-SENTINEL-MESSAGE", Some(&alice_sk)) + .unwrap(); + let (mid, manifest) = repo.branch_manifest("fix").unwrap().unwrap(); + drop(repo); + + // Build the exact pack a push/fetch of this branch would stream (the + // wire server's GetPack delegates to this same builder), then DECODE + // every record: pack records are zstd-compressed, so asserting on the + // raw stream would be vacuous. Every decoded object must be Sealed, + // the manifest itself, or free of the plaintext markers. + let transport = crate::transport::LocalTransport::open(&root).unwrap(); + let guard = transport.build_pack_tempfile(&[mid], &[], None).unwrap(); + let pack = std::fs::read(guard.path()).unwrap(); + let objects = scl_core::pack::parse_pack(&pack).unwrap(); + assert!( + objects.iter().any(|(id, _)| *id == mid), + "manifest must travel" + ); + for sealed_id in &manifest.closure { + assert!( + objects.iter().any(|(id, _)| id == sealed_id), + "sealed closure object missing from pack" + ); + } + for (id, obj) in &objects { + match obj { + Object::Sealed(_) | Object::Manifest(_) => continue, + _ => {} + } + let enc = obj.encode(); + for needle in [ + b"WIRE-SENTINEL-CONTENT".as_slice(), + b"WIRE-SENTINEL-MESSAGE".as_slice(), + b"wire-secret.txt".as_slice(), + ] { + assert!( + !enc.windows(needle.len()).any(|w| w == needle), + "plaintext leaked in wire object {id}" + ); + } + } + + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn manifest_ancestry_is_prev_chain_and_publish_is_not_a_fast_forward() { + let (root, repo, (alice_sk, _), (_, bob_pk)) = setup("ancestry"); + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + let (m1, _) = repo.branch_manifest("fix").unwrap().unwrap(); + repo.branch_grant("fix", &alice_sk, &bob_pk).unwrap(); + let (m2, _) = repo.branch_manifest("fix").unwrap().unwrap(); + + let store_arc = repo.vfs().store(); + { + let mut store = store_arc.lock().unwrap(); + // Push's ff check runs over the manifest prev chain: the + // superseded manifest is an ancestor of its successor, never the + // reverse (a stale manifest can't ff over a newer one). + assert!(crate::merge::is_ancestor(&mut store, m1, m2).unwrap()); + assert!(!crate::merge::is_ancestor(&mut store, m2, m1).unwrap()); + } + + // Publish is a REWRITE, not a fast-forward: the published snapshot + // shares no structural link with the manifest it replaced (ids differ + // by construction), so the pre-publish manifest is NOT an ancestor of + // the published tip. This is what makes a rollback-republish over a + // newer remote manifest a non-ff (refused) instead of a silent + // clobber of grants/revokes/commits — the Copilot-review data-loss + // hole. A publish-push over a remote still holding the private + // manifest is a genuine non-ff: the remote fetches, or the operator + // force-pushes (same posture as amend/rebase). + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "fix.txt", "v1\n"); + repo.commit_private("alice", "c1", Some(&alice_sk)).unwrap(); + let (m3, _) = repo.branch_manifest("fix").unwrap().unwrap(); + let (published, _) = repo.branch_publish("fix", &alice_sk).unwrap(); + { + let mut store = store_arc.lock().unwrap(); + assert!( + !crate::merge::is_ancestor(&mut store, m3, published).unwrap(), + "publish must not read as a fast-forward over the private manifest" + ); + // And crucially: a ROLLED-BACK older manifest is likewise not an + // ancestor of a published tip that shares its base — the specific + // hole the reviewer flagged. + assert!(!crate::merge::is_ancestor(&mut store, m1, published).unwrap()); + } + + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } + + #[test] + fn protected_paths_survive_a_private_round_trip() { + let (root, repo, (alice_sk, alice_pk), _bob) = setup("protpath"); + // A protected prefix on the PUBLIC branch first. + repo.protect("vault/", std::slice::from_ref(&alice_pk), None) + .unwrap(); + write(&root, "vault/key.pem", "PEM-PLAINTEXT\n"); + repo.commit("tester", "add protected").unwrap(); + + // Private branch: edit the protected file under seal. + repo.branch_private("fix", &alice_sk, &[], &[]).unwrap(); + repo.switch_with_identity("fix", Some(&alice_sk)).unwrap(); + write(&root, "vault/key.pem", "PEM-ROTATED\n"); + repo.commit_private("alice", "rotate pem", Some(&alice_sk)) + .unwrap(); + assert_opaque(&repo, b"PEM-ROTATED"); + + // Publish: the edited file is re-sealed through the P7/P33 path — + // PROTECTED in the published tree, never public plaintext. + let (tip, resealed) = repo.branch_publish("fix", &alice_sk).unwrap(); + assert_eq!(resealed, 1, "one protected file re-sealed at publish"); + assert_opaque(&repo, b"PEM-ROTATED"); + let snap = repo.snapshot(&tip).unwrap(); + let store_arc = repo.vfs().store(); + let mut store = store_arc.lock().unwrap(); + let files = crate::worktree::tree_file_entries_with_perms(&mut store, snap.root).unwrap(); + let (_, _, perms) = files["vault/key.pem"]; + assert!(perms & PROTECTED != 0, "published entry stays protected"); + + drop(store); + drop(repo); + std::fs::remove_dir_all(&root).unwrap(); + } +} diff --git a/crates/repo/src/protect_ops.rs b/crates/repo/src/protect_ops.rs index 82c81341..34f158c2 100644 --- a/crates/repo/src/protect_ops.rs +++ b/crates/repo/src/protect_ops.rs @@ -33,6 +33,7 @@ impl Repo { recipients: &[scl_crypto::PublicKey], _identity: Option<&scl_crypto::SecretKey>, ) -> Result { + self.refuse_on_private("sc protect")?; use scl_core::ProtectPrefix; // `protect`'s first write is a policy-only commit_snapshot, which // (unlike `commit`) has no in-progress guard of its own — the P19-I1 @@ -140,6 +141,7 @@ impl Repo { authorized: &scl_crypto::SecretKey, new: &scl_crypto::PublicKey, ) -> Result { + self.refuse_on_private("sc grant")?; // P21: same in-progress guard as `protect` — see its comment. if crate::merge_state::in_progress(self.layout()) { return Err(Error::MergeInProgress); @@ -219,6 +221,7 @@ impl Repo { /// such prefix. Does not rotate content (a prior holder kept any plaintext /// already checked out — see the secrets-revoke rationale in ADR-0008). pub fn revoke(&self, prefix: &str, recipient_id: &scl_crypto::RecipientId) -> Result { + self.refuse_on_private("sc revoke")?; // P21: same in-progress guard as `protect` — see its comment. if crate::merge_state::in_progress(self.layout()) { return Err(Error::MergeInProgress); @@ -300,6 +303,7 @@ impl Repo { /// List the tip's protected prefixes with every recipient register — /// tombstones included, so a post-merge listing shows revocations holding. pub fn protected_prefixes(&self) -> Result)>> { + self.refuse_on_private("sc protect --list")?; let protection = match self.head_tip()? { Some(t) => self.snapshot(&t)?.protection, None => Protection::default(), diff --git a/crates/repo/src/reachable.rs b/crates/repo/src/reachable.rs index 655e66e4..c0f300e0 100644 --- a/crates/repo/src/reachable.rs +++ b/crates/repo/src/reachable.rs @@ -188,6 +188,37 @@ pub fn reachable_objects_filtered( while let Some(sid) = snapshots.pop_front() { let snap = match src.get(&sid)? { Object::Snapshot(s) => s, + // A private branch's tip (P34, ADR-0044): the manifest's flat + // closure list IS the reachability rule — every sealed id it + // names is included as an opaque leaf (never decoded), the + // public base and the superseded-manifest chain are walked like + // parents. Sealed content cannot be path-filtered (that opacity + // is the feature), so the closure is included whole even under a + // filter; filtered CLONES exclude private refs at the want level + // instead (`filter_manifest_wants`). + Object::Manifest(m) => { + for id in &m.closure { + included.insert(*id); + } + // `base` and every merged-in public `anchor` are walked as + // snapshot roots: the sealed inner trees carry unsealed public + // references to content reachable from these (copy-on-write), + // which a keyless party can't discover by walking sealed + // objects. Without anchoring the merged-in tips, a private + // branch that merged a public branch in would be + // non-transferable to a peer lacking those public commits. + for root in std::iter::once(m.base).chain(m.anchors.iter().copied()) { + if included.insert(root) { + snapshots.push_back(root); + } + } + if let Some(prev) = m.prev { + if included.insert(prev) { + snapshots.push_back(prev); + } + } + continue; + } _ => return Err(Error::BadRef(format!("expected snapshot {sid}"))), }; for p in &snap.parents { @@ -214,6 +245,29 @@ pub fn reachable_objects_filtered( Ok(Reachable { included, gaps }) } +/// Filtered clones exclude private branches entirely (ADR-0044): sealed +/// content cannot be path-filtered, so under an active filter any want that +/// resolves to a branch manifest is dropped before the reachability walk. +/// Both pack builders (local transport + wire server) call this so the rule +/// holds on every transport. With no filter, wants pass through untouched. +pub fn filter_manifest_wants( + src: &mut impl ObjectSource, + wants: &[ObjectId], + filter_active: bool, +) -> Result> { + if !filter_active { + return Ok(wants.to_vec()); + } + let mut out = Vec::with_capacity(wants.len()); + for w in wants { + match src.get(w) { + Ok(Object::Manifest(_)) => continue, + _ => out.push(*w), + } + } + Ok(out) +} + /// Path-tracking tree descent shared by [`walk_tree`] (filter = None) and /// [`reachable_objects_filtered`]. Stack items are `(tree_id, path)`, the /// snapshot root at path `""`. For each entry, `child_path` is `name` at the diff --git a/crates/repo/src/replay.rs b/crates/repo/src/replay.rs index 5af2339e..56eb93eb 100644 --- a/crates/repo/src/replay.rs +++ b/crates/repo/src/replay.rs @@ -331,6 +331,12 @@ impl Repo { identity: Option<&scl_crypto::SecretKey>, mainline: Option, ) -> Result { + self.refuse_on_private("cherry-pick")?; + if let Some(t) = crate::refs::resolve_tip(&self.layout, refname)? { + if self.manifest_at(&t)?.is_some() { + return Err(Error::PrivateIntegration(refname.to_string())); + } + } if crate::merge_state::in_progress(&self.layout) { return Err(Error::MergeInProgress); } @@ -672,6 +678,12 @@ impl Repo { author: &str, identity: Option<&scl_crypto::SecretKey>, ) -> Result { + self.refuse_on_private("rebase")?; + if let Some(t) = crate::refs::resolve_tip(&self.layout, target)? { + if self.manifest_at(&t)?.is_some() { + return Err(Error::PrivateIntegration(target.to_string())); + } + } if crate::merge_state::in_progress(&self.layout) { return Err(Error::MergeInProgress); } diff --git a/crates/repo/src/repo.rs b/crates/repo/src/repo.rs index 95c0c695..764bcf1b 100644 --- a/crates/repo/src/repo.rs +++ b/crates/repo/src/repo.rs @@ -139,6 +139,7 @@ impl Repo { /// Scan the current working tree for plaintext secrets (read-only). pub fn scan_worktree(&self) -> Result { + self.refuse_on_private("sc scan")?; let files = worktree::read_worktree(&self.layout, &self.tracked_paths()?)?; self.scan_files(&files) } @@ -750,6 +751,11 @@ impl Repo { if crate::rebase_state::in_progress(&self.layout) { return Err(Error::RebaseInProgress); } + // A private branch commits through `commit_private` (P34) — it needs + // a recipient identity this signature doesn't carry. + if let Some((branch, _, _)) = self.head_private()? { + return Err(Error::PrivateNoAccess(branch)); + } let head = refs::current_branch(&self.layout)?; let before = refs::read_branch_tip(&self.layout, &head)?; let tip = self.head_tip()?; @@ -866,6 +872,7 @@ impl Repo { /// restores the old tip. No pushed-commit guard — sc has no /// authoritative record of remote observers (ADR-0029). pub fn amend(&self, author: &str, message: Option<&str>) -> Result { + self.refuse_on_private("sc amend")?; if crate::merge_state::in_progress(&self.layout) { return Err(Error::MergeInProgress); } @@ -987,6 +994,11 @@ impl Repo { /// Working-tree status against HEAD, plus merge-in-progress info. pub fn status(&self) -> Result { + // A private HEAD needs a recipient identity even to know what the + // tree contains — `status_private` (P34) is the covering call. + if let Some((branch, _, _)) = self.head_private()? { + return Err(Error::PrivateNoAccess(branch)); + } let (head_root, protection) = match self.head_tip()? { Some(tip) => { let snap = self.snapshot(&tip)?; @@ -1010,6 +1022,10 @@ impl Repo { /// The working-tree file paths (repo-relative, sorted) that `commit` would /// snapshot — the same set `sc protect` encrypts when they match a prefix. pub fn worktree_paths(&self) -> Result> { + // A private branch's tree is sealed — there is no plaintext working + // 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() @@ -1032,6 +1048,9 @@ impl Repo { /// out-of-sparse subtree as deleted. pub fn diff_unified(&self) -> Result { use scl_core::PROTECTED; + if let Some((branch, _, _)) = self.head_private()? { + return Err(Error::PrivateNoAccess(branch)); + } let sparse = self.sparse_spec()?; let head_root = self .head_tip()? @@ -1216,6 +1235,11 @@ impl Repo { author: &str, identity: Option<&scl_crypto::SecretKey>, ) -> Result<(ObjectId, Vec)> { + // On a private branch, merging FROM a public branch is the one legal + // integration direction — route to the private machinery (P34). + if self.head_private()?.is_some() { + return self.merge_into_private(branch, author, identity); + } if crate::merge_state::in_progress(&self.layout) { return Err(Error::MergeInProgress); } @@ -1235,6 +1259,11 @@ impl Repo { let before = refs::read_branch_tip(&self.layout, &head)?; let theirs = refs::resolve_tip(&self.layout, branch)? .ok_or_else(|| Error::NoSuchBranch(branch.to_string()))?; + // Private → public integration is refused unconditionally: the only + // sanctioned path to plaintext is `sc branch publish` (P34). + if self.manifest_at(&theirs)?.is_some() { + return Err(Error::PrivateIntegration(branch.to_string())); + } let ours = match self.head_tip()? { Some(ours) => ours, None => { @@ -1661,6 +1690,9 @@ impl Repo { /// Snapshots from the current tip back through parents (newest first). pub fn log(&self) -> Result> { + if let Some((branch, _, _)) = self.head_private()? { + return Err(Error::PrivateNoAccess(branch)); + } let mut out = Vec::new(); let mut next = self.head_tip()?; while let Some(id) = next { @@ -1693,6 +1725,9 @@ impl Repo { /// Create `name` pointing at the current tip (errors if unborn or exists). pub fn branch(&self, name: &str) -> Result<()> { + // Two branch refs sharing one manifest would fork the private index + // in place — refuse rather than corrupt (P34). + self.refuse_on_private("sc branch (creating a branch from a private tip)")?; validate_branch_name(name)?; if self.layout.ref_path(name).exists() { return Err(Error::BadRef(format!("branch already exists: {name}"))); @@ -1747,7 +1782,13 @@ impl Repo { // The protection-aware dirty check (status) already treats unchanged // decrypted protected files and skipped/absent protected files as clean, // so a reported modification/deletion is a genuine uncommitted change. - let dirty = self.status()?; + // Leaving a PRIVATE branch needs the private status (the identity + // gates even knowing what the tree contains — P34). + let head_priv = self.head_private()?; + let dirty = match &head_priv { + Some(_) => self.status_private(identity)?, + None => self.status()?, + }; if !dirty.modified.is_empty() || !dirty.deleted.is_empty() { return Err(Error::InvalidArgument( "working tree has uncommitted changes; commit before switching".into(), @@ -1755,12 +1796,40 @@ impl Repo { } let target_tip = refs::read_branch_tip(&self.layout, name)? .ok_or_else(|| Error::NoSuchBranch(name.to_string()))?; - let old_root = self.head_root()?; + // Paths tracked by the branch being LEFT (for the removal pass). + let old_paths: std::collections::BTreeSet = match &head_priv { + Some(_) => self.private_tracked_paths(identity)?, + None => self.tracked_paths()?, + }; + // Switching TO a private branch takes the private materialize path. + if let Some(m) = self.manifest_at(&target_tip)? { + return self.switch_to_private(name, target_tip, m, identity, &old_paths); + } + // Public target. When leaving a private branch there is no public + // old root to hand `materialize` — the stale-path removal below uses + // `old_paths` directly instead. + let old_root = match &head_priv { + Some(_) => None, + None => self.head_root()?, + }; let (target_root, target_protection) = { let store_arc = self.vfs.store(); let snap = store_arc.lock().unwrap().get_snapshot(&target_tip)?; (snap.root, snap.protection) }; + if head_priv.is_some() { + // Remove private-tip paths the public target doesn't track, + // BEFORE materialize lays the target down. + let store_arc = self.vfs.store(); + let mut store = store_arc.lock().unwrap(); + let target_paths = worktree::tree_file_entries_with_perms(&mut store, target_root)?; + for p in &old_paths { + if !target_paths.contains_key(p) { + let full = worktree::safe_join(&self.layout.root, p)?; + let _ = std::fs::remove_file(full); + } + } + } // Cache hoisted above the block so its save follows the ref move // (`write_head` below), matching commit/amend's discipline (P33 Task 7). let mut cache = self.open_protected_cache()?; @@ -1822,8 +1891,9 @@ pub(crate) fn unix_now() -> i64 { } /// Append one file's diff to `out`: unified hunks for text, a one-line notice -/// for binary content (NUL byte on either side). -fn push_file_diff(out: &mut String, path: &str, old: &[u8], new: &[u8]) { +/// for binary content (NUL byte on either side). `pub(crate)` so the private- +/// branch diff (P34, `private.rs`) renders identically. +pub(crate) fn push_file_diff(out: &mut String, path: &str, old: &[u8], new: &[u8]) { if old.contains(&0) || new.contains(&0) { out.push_str(&format!("Binary files a/{path} and b/{path} differ\n")); return; diff --git a/crates/repo/src/rewrap.rs b/crates/repo/src/rewrap.rs index 1c57d11c..d635d061 100644 --- a/crates/repo/src/rewrap.rs +++ b/crates/repo/src/rewrap.rs @@ -44,6 +44,7 @@ impl Repo { known_keys: &[PublicKey], dry_run: bool, ) -> Result { + self.refuse_on_private("sc rewrap")?; // Refuse mid-merge/mid-pick: completion unions the OLD wraps back in // over rewrap's cutover, silently resurrecting a stripped recipient // at the tip (final-review I1). Checked even on --dry-run — a diff --git a/crates/repo/src/secrets.rs b/crates/repo/src/secrets.rs index 7bde0150..3d43fbd6 100644 --- a/crates/repo/src/secrets.rs +++ b/crates/repo/src/secrets.rs @@ -91,6 +91,7 @@ impl Repo { value: &[u8], recipients: &[PublicKey], ) -> Result { + self.refuse_on_private("sc secret add")?; // P19-I1: policy ops that move the branch tip via `commit_registry` // had no in-progress guard of their own, letting them silently // discard a stopped merge/pick/rebase's pending resolution. Same @@ -128,6 +129,7 @@ impl Repo { authorized: &SecretKey, new: &PublicKey, ) -> Result { + self.refuse_on_private("sc secret grant")?; // P21: `secret_grant` also moves the branch tip via `commit_registry` // (confirmed by inspection per the P21 brief) — same guard trio as // `secret_add`/`secret_rotate`. @@ -169,6 +171,7 @@ impl Repo { /// Revoke a recipient from `name` (metadata-only re-wrap). pub fn secret_revoke(&self, name: &str, recipient: &RecipientId) -> Result { + self.refuse_on_private("sc secret revoke")?; // P21: `secret_revoke` also moves the branch tip via `commit_registry` // (confirmed by inspection per the P21 brief) — same guard trio as // `secret_add`/`secret_rotate`. @@ -231,6 +234,7 @@ impl Repo { recipients: &[PublicKey], identity: Option<&SecretKey>, ) -> Result { + self.refuse_on_private("sc secret rotate")?; // P21: same in-progress guard as `secret_add` — see its comment. if crate::merge_state::in_progress(self.layout()) { return Err(Error::MergeInProgress); @@ -308,6 +312,7 @@ impl Repo { /// List secrets at HEAD with recipient counts. pub fn secret_list(&self) -> Result> { + self.refuse_on_private("sc secret list")?; let reg = self.registry()?; let mut out = Vec::new(); for (name, id) in reg { @@ -393,6 +398,7 @@ impl Repo { /// stderr warning; a corrupt/tampered secret is a hard error. Returns the /// child's exit code. pub fn run(&self, identity: &SecretKey, cmd: &[String]) -> Result { + self.refuse_on_private("sc run")?; let envs = self.secret_env(identity, false)?; let (exe, args) = cmd .split_first() diff --git a/crates/repo/src/signatures.rs b/crates/repo/src/signatures.rs index 2eea4835..2c241753 100644 --- a/crates/repo/src/signatures.rs +++ b/crates/repo/src/signatures.rs @@ -262,6 +262,9 @@ impl Repo { snapshot: ObjectId, identity: &scl_crypto::Identity, ) -> Result { + if self.manifest_at(&snapshot)?.is_some() { + return Err(Error::PrivateUnsupported("sc sign".into())); + } let signing = identity.signing.as_ref().ok_or_else(|| { Error::InvalidArgument( "identity has no signing half (v1 identity); signing requires a v2 (scl-id-) \ diff --git a/crates/repo/src/sparse.rs b/crates/repo/src/sparse.rs index 86d94750..4543a9c6 100644 --- a/crates/repo/src/sparse.rs +++ b/crates/repo/src/sparse.rs @@ -143,6 +143,7 @@ impl Repo { prefixes: &[String], identity: Option<&scl_crypto::SecretKey>, ) -> Result> { + self.refuse_on_private("sc sparse set")?; if crate::merge_state::in_progress(&self.layout) { return Err(Error::MergeInProgress); } @@ -209,6 +210,7 @@ impl Repo { /// the same reason (the write loop rewrites every in-sparse target entry /// unconditionally). pub fn disable_sparse(&self, identity: Option<&scl_crypto::SecretKey>) -> Result> { + self.refuse_on_private("sc sparse disable")?; if crate::merge_state::in_progress(&self.layout) { return Err(Error::MergeInProgress); } diff --git a/crates/repo/src/sync.rs b/crates/repo/src/sync.rs index 4eb3ded2..d6c0f66f 100644 --- a/crates/repo/src/sync.rs +++ b/crates/repo/src/sync.rs @@ -119,10 +119,28 @@ impl Repo { // Copy branches + HEAD, and seed origin/* remote-tracking refs so // `merge origin/` resolves immediately and `fetch` has a baseline. + // Filtered clones exclude private branches entirely (ADR-0044): the + // server dropped manifest wants from the pack, so a private ref's + // target never landed — skip the ref rather than minting a dangling + // one. Full clones carry private refs (ciphertext + manifest) intact. for (branch, tip) in &remote_refs { + if filter.is_some() && !dst_repo.vfs.store().lock().unwrap().contains(tip) { + continue; + } refs::write_branch_tip(&dst_repo.layout, branch, tip)?; refs::write_remote_tip(&dst_repo.layout, "origin", branch, tip)?; } + // The remote's HEAD branch may itself have been excluded (private): + // fall back to any branch that survived. + let head_branch = if dst_repo.layout.ref_path(&head_branch).exists() { + head_branch + } else { + dst_repo + .branches()? + .first() + .map(|(n, _)| n.clone()) + .unwrap_or(head_branch) + }; refs::write_head(&dst_repo.layout, &head_branch)?; // Record origin. @@ -149,7 +167,13 @@ impl Repo { // Materialize HEAD into the working tree. No identity is available at // clone time, so PROTECTED files are skipped (ciphertext stays in objects // but plaintext is not written to disk — correct for unauthorized clones). + // A private HEAD branch (manifest tip, ADR-0044) is not materialized at + // all: checkout needs a recipient identity — `sc switch + // --identity ` after the clone. if let Some(head_tip) = dst_repo.head_tip()? { + if dst_repo.manifest_at(&head_tip)?.is_some() { + return Ok(dst_repo); + } let store_arc = dst_repo.vfs.store(); let mut store = store_arc.lock().unwrap(); let head_snap = store.get_snapshot(&head_tip)?; diff --git a/crates/repo/src/transcripts.rs b/crates/repo/src/transcripts.rs index 70a1d631..2bb6ebaf 100644 --- a/crates/repo/src/transcripts.rs +++ b/crates/repo/src/transcripts.rs @@ -207,6 +207,9 @@ impl Repo { body: &[u8], recipients: &[scl_crypto::PublicKey], ) -> Result { + if self.manifest_at(&snapshot)?.is_some() { + return Err(Error::PrivateUnsupported("sc transcript attach".into())); + } crate::secrets::require_recipients(recipients)?; // P5 scan-and-WARN (never reject): the body is about to be sealed, diff --git a/crates/repo/src/transport.rs b/crates/repo/src/transport.rs index 44a93918..291d8b4b 100644 --- a/crates/repo/src/transport.rs +++ b/crates/repo/src/transport.rs @@ -130,16 +130,22 @@ impl LocalTransport { // (the remote) store. `haves` the remote doesn't have are skipped. let promisor_filter = filter.map(|prefixes| crate::promisor::Promisor::new(String::new(), prefixes.to_vec())); + // Filtered transfers exclude private branches entirely (ADR-0044): + // a want that resolves to a branch manifest is dropped before the + // walk. Serves both transports — the wire server's GetPack delegates + // here. + let wants = + crate::reachable::filter_manifest_wants(&mut *store, wants, promisor_filter.is_some())?; let mut want_set = match &promisor_filter { Some(pf) => { crate::reachable::reachable_objects_filtered( &mut *store, - wants, + &wants, Some(pf as &dyn crate::reachable::PrefixFilter), )? .included } - None => crate::reachable::reachable_objects(&mut *store, wants)?, + None => crate::reachable::reachable_objects(&mut *store, &wants)?, }; let mut have_set: BTreeSet = BTreeSet::new(); diff --git a/crates/repo/src/wire.rs b/crates/repo/src/wire.rs index 831e79a1..150b3a03 100644 --- a/crates/repo/src/wire.rs +++ b/crates/repo/src/wire.rs @@ -21,8 +21,11 @@ use crate::error::{Error, Result}; /// `filter: Vec` field (empty = no filter, full transfer) — a v2 /// peer's decoder doesn't know to read that trailing field, so the versions /// are wire-incompatible for `GetPack` even though every other verb's -/// encoding is unchanged. -pub const PROTOCOL_VERSION: u32 = 3; +/// encoding is unchanged. Bumped 3 -> 4 in P34 (ADR-0044): two additive +/// object kinds (sealed object, branch manifest) now cross the wire inside +/// packs — a v3 peer's `Object::decode` would hit an unknown tag deep in +/// pack ingest, so the mismatch is refused cleanly at the handshake instead. +pub const PROTOCOL_VERSION: u32 = 4; // Request opcodes (one per Transport verb, plus session control). const OP_HELLO: u8 = 0x01; diff --git a/crates/repo/src/workspace.rs b/crates/repo/src/workspace.rs index a5b2cb46..aab0a11e 100644 --- a/crates/repo/src/workspace.rs +++ b/crates/repo/src/workspace.rs @@ -230,6 +230,7 @@ impl Repo { /// working tree are never touched. One workspace's failure (agent exit, /// scanner rejection, harvest error) never aborts its siblings. pub fn work(&self, opts: WorkOptions) -> Result> { + self.refuse_on_private("sc work")?; if opts.agents == 0 { return Err(Error::InvalidArgument("agents must be >= 1".into())); } diff --git a/crates/repo/src/ws.rs b/crates/repo/src/ws.rs index 3990ff8b..90ea5269 100644 --- a/crates/repo/src/ws.rs +++ b/crates/repo/src/ws.rs @@ -200,6 +200,7 @@ impl Repo { author: &str, identity: Option<&scl_crypto::SecretKey>, ) -> Result { + self.refuse_on_private("sc ws fork")?; if agents == 0 { return Err(Error::InvalidArgument("agents must be >= 1".into())); } diff --git a/demo/run_private_branch_demo.sh b/demo/run_private_branch_demo.sh new file mode 100755 index 00000000..8e545818 --- /dev/null +++ b/demo/run_private_branch_demo.sh @@ -0,0 +1,193 @@ +#!/usr/bin/env bash +# P34 demo: per-branch access control (ADR-0044). Two agents — alice +# (authorized) and mallory (no key) — plus bob, who is granted and then +# revoked. Proves end-to-end: +# +# 1. OPACITY: a private branch travels to a keyless clone as ciphertext + +# manifest only. The clone sees the branch NAME (accepted metadata) but +# every read surface refuses (`switch`, `log`, `status`), nothing +# private is ever materialized to its working tree, and no plaintext +# byte of the content, paths, or commit message appears anywhere in the +# clone. (The stronger structural claim — that every DECODED non-sealed +# object in the store and on the wire is free of private plaintext — is +# pinned by unit tests in crates/repo/src/private.rs, where objects can +# be decoded; a shell grep cannot decode zstd-compressed objects, so +# this script does not pretend that grep alone proves it.) +# 2. MEMBERSHIP: `sc branch grant` admits bob O(1); `sc branch revoke` +# rotates the branch KEK atomically, so a fresh bob clone after the +# revoke cannot open the branch — while bob's PRE-revoke clone still +# can (rotation ≠ erasure, stated honestly, same boundary as ADR-0019). +# 3. THE VALVE: merging main INTO the private branch works (keeping the +# embargo current); merging the private branch into main, and exporting +# it to git, are refused with a publish hint. +# 4. PUBLISH: one atomic command replays the sealed history as public +# commits (messages/authors preserved); a keyless clone taken afterward +# reads everything, and the branch is an ordinary public branch. +# 5. Zero residue: no .sc/tmp leftovers on any repo, either run. +# +# Self-checking: every claim is an assertion; any failure exits non-zero +# before the RESULT line. Run twice to prove repeatability. +set -euo pipefail + +cd "$(dirname "$0")/.." +cargo build --quiet --bin sc +SC="$PWD/target/debug/sc" + +tmp_base="${TMPDIR:-/tmp}" +tmp_base="${tmp_base%/}" + +fail() { echo "FAIL: $1"; exit 1; } + +run_demo() { + local run_label="$1" + local work + work=$(mktemp -d "$tmp_base/sc-private-branch-demo.XXXXXX") + trap 'rm -rf "$work"' RETURN + export HOME="$work/home" # keep ~/.sc and known-host state inside $work + mkdir -p "$HOME" + + local origin="$work/origin" + local SENTINEL="EMBARGOED-FIX-CONTENT-7f3a" + local SENTINEL_MSG="EMBARGOED-COMMIT-MESSAGE-7f3a" + local SENTINEL_PATH="embargoed-fix-7f3a.txt" + + echo + echo "=== [$run_label] 0: setup — public repo, alice + bob identities ===" + mkdir -p "$origin" && cd "$origin" + "$SC" init >/dev/null + echo "public readme" > readme.txt + mkdir -p src && echo "fn main() {}" > src/main.rs + "$SC" commit -m "public base" --author alice >/dev/null + "$SC" keygen --out "$work/alice.key" > "$work/alice.out" 2>&1 + "$SC" keygen --out "$work/bob.key" > "$work/bob.out" 2>&1 + local ALICE_PK BOB_PK BOB_ID + ALICE_PK=$(grep -o 'scl-pk-[0-9a-f]*' "$work/alice.out" | head -1) + BOB_PK=$(grep -o 'scl-pk-[0-9a-f]*' "$work/bob.out" | head -1) + BOB_ID=$(grep 'recipient id:' "$work/bob.out" | awk '{print $3}') + printf '[recipients]\nalice = "%s"\nbob = "%s"\n' "$ALICE_PK" "$BOB_PK" > .sc/recipients.toml + echo "setup done ✔" + + echo + echo "=== [$run_label] 1: alice stages an embargoed fix on a private branch ===" + "$SC" branch hotfix --private --to alice --identity "$work/alice.key" | head -1 + "$SC" switch hotfix --identity "$work/alice.key" >/dev/null + echo "$SENTINEL" > "$SENTINEL_PATH" + echo "fn main() { patched(); }" > src/main.rs + "$SC" commit -m "$SENTINEL_MSG" --author alice --identity "$work/alice.key" >/dev/null + "$SC" log --identity "$work/alice.key" | grep -q "$SENTINEL_MSG" \ + || fail "alice must read her own private log" + echo "private commit landed; alice reads it ✔" + + echo + echo "=== [$run_label] 2: keeping current — merge main INTO the branch ===" + "$SC" switch main --identity "$work/alice.key" >/dev/null + [ ! -f "$SENTINEL_PATH" ] || fail "private file must leave the tree on switch to main" + echo "main keeps moving" > main-moves.txt + "$SC" commit -m "public progress" --author carol >/dev/null + "$SC" switch hotfix --identity "$work/alice.key" >/dev/null + "$SC" merge main --author alice --identity "$work/alice.key" | grep -q merged \ + || fail "merge main into private must succeed" + [ -f main-moves.txt ] || fail "merged-in public file must materialize" + echo "public → private merge works (the one legal direction) ✔" + + echo + echo "=== [$run_label] 3: the valve — private → public is refused everywhere ===" + "$SC" switch main --identity "$work/alice.key" >/dev/null + out=$("$SC" merge hotfix --author alice 2>&1 || true) + echo "$out" | grep -q "sc branch publish" \ + || fail "merge private→public must be refused with a publish hint (got: $out)" + "$SC" switch hotfix --identity "$work/alice.key" >/dev/null + out=$("$SC" export --to "$work/nogit" 2>&1 || true) + echo "$out" | grep -q "cannot be exported" \ + || fail "git export of a private branch must be refused (got: $out)" + "$SC" switch main --identity "$work/alice.key" >/dev/null + echo "integration valve holds ✔" + + echo + echo "=== [$run_label] 4: OPACITY — mallory clones without any key ===" + "$SC" clone "$origin" "$work/mallory" >/dev/null + cd "$work/mallory" + "$SC" branch list | grep -q "hotfix (private, no access)" \ + || fail "mallory must see the branch name with a no-access marker" + out=$("$SC" switch hotfix 2>&1 || true) + echo "$out" | grep -q "private" || fail "mallory switch must refuse (got: $out)" + ( "$SC" switch hotfix >/dev/null 2>&1 ) && fail "mallory switch must exit non-zero" + [ ! -f "$SENTINEL_PATH" ] || fail "private path must never materialize for mallory" + # No plaintext byte of content/path/message anywhere in the clone — + # working tree, refs, or (necessarily-ciphertext) sealed objects. + if grep -r "$SENTINEL" . >/dev/null 2>&1; then fail "content plaintext leaked"; fi + if grep -r "$SENTINEL_MSG" . >/dev/null 2>&1; then fail "message plaintext leaked"; fi + if grep -rl "$SENTINEL_PATH" .sc >/dev/null 2>&1; then fail "path name leaked into .sc"; fi + echo "mallory holds ciphertext only; every read surface refuses ✔" + + echo + echo "=== [$run_label] 5: MEMBERSHIP — grant bob, then revoke (KEK rotation) ===" + cd "$origin" + "$SC" branch grant hotfix --to bob --identity "$work/alice.key" >/dev/null + "$SC" clone "$origin" "$work/bob-before" >/dev/null + cd "$work/bob-before" + "$SC" switch hotfix --identity "$work/bob.key" >/dev/null + grep -q "$SENTINEL" "$SENTINEL_PATH" || fail "granted bob must read the fix" + echo "bob (granted) reads the embargoed fix ✔" + cd "$origin" + "$SC" branch revoke hotfix --recipient-id "$BOB_ID" --identity "$work/alice.key" | head -1 + "$SC" clone "$origin" "$work/bob-after" >/dev/null + cd "$work/bob-after" + "$SC" branch list --identity "$work/bob.key" | grep -q "hotfix (private, no access)" \ + || fail "post-revoke bob must have no access to the rotated manifest" + ( "$SC" switch hotfix --identity "$work/bob.key" >/dev/null 2>&1 ) \ + && fail "post-revoke bob switch must fail" + # Rotation ≠ erasure, stated honestly: bob's PRE-revoke clone still opens + # the OLD manifest it already fetched. + cd "$work/bob-before" + "$SC" log --identity "$work/bob.key" | grep -q "$SENTINEL_MSG" \ + || fail "pre-revoke clone must still read (rotation != erasure)" + echo "revoke locks bob out of everything after the rotation; old clone keeps old reads (documented boundary) ✔" + + echo + echo "=== [$run_label] 6: PUBLISH — one atomic release ===" + cd "$origin" + "$SC" branch publish hotfix --identity "$work/alice.key" | head -1 + "$SC" branch list | grep -q "^..hotfix$" || true + "$SC" branch list | grep "hotfix" | grep -qv "private" \ + || fail "published branch must lose its private marker" + "$SC" switch hotfix >/dev/null # no identity needed anymore + grep -q "$SENTINEL" "$SENTINEL_PATH" || fail "published content must be public" + "$SC" log | grep -q "$SENTINEL_MSG" || fail "published history must be readable" + # A brand-new keyless clone reads everything. + "$SC" clone "$origin" "$work/mallory2" >/dev/null + cd "$work/mallory2" + "$SC" switch hotfix >/dev/null 2>&1 || true + grep -q "$SENTINEL" "$SENTINEL_PATH" || fail "keyless clone must read published content" + # And the branch integrates into main like any public branch. + cd "$origin" && "$SC" switch main >/dev/null + "$SC" merge hotfix --author alice >/dev/null + grep -q "$SENTINEL" "$SENTINEL_PATH" || fail "published branch must merge into main" + echo "publish flips the branch public atomically; everyone reads; merge to main lands ✔" + + echo + echo "=== [$run_label] 7: zero residue ===" + for r in "$origin" "$work/mallory" "$work/bob-before" "$work/bob-after" "$work/mallory2"; do + if [ -d "$r/.sc/tmp" ] && [ -n "$(ls -A "$r/.sc/tmp" 2>/dev/null)" ]; then + fail "$r/.sc/tmp is not empty" + fi + done + echo "no .sc/tmp residue anywhere ✔" + cd "$tmp_base" +} + +run_demo "run 1" +run_demo "run 2 (repeatable, not a one-shot fluke)" + +echo +echo "RESULT: a private branch is fully opaque to non-recipients — the keyless" +echo "clone sees only the branch name, a manifest, and sealed ciphertext" +echo "(content, paths, and commit messages unreadable; every read surface" +echo "refuses cleanly); grant admits a recipient with one wrap, revoke rotates" +echo "the branch KEK so post-revoke fetches are unreadable while pre-revoke" +echo "clones keep what they already had (rotation ≠ erasure, the standing" +echo "ADR-0019 boundary); public → private merges keep the embargo current" +echo "while private → public integration and git export refuse until the one" +echo "sanctioned crossing — sc branch publish — atomically replays the sealed" +echo "history as ordinary public commits. Verified across two independent" +echo "runs, zero residue." diff --git a/docs/THREAT-MODEL.md b/docs/THREAT-MODEL.md index 96ae7689..928e5783 100644 --- a/docs/THREAT-MODEL.md +++ b/docs/THREAT-MODEL.md @@ -81,6 +81,56 @@ too, not just the src-control-side metadata. is rotation (`sc rewrap` on the live tip), not revoke, and history keeps the old wraps regardless. +## Private branches (`sc branch --private` / `sc branch publish`) — ADR-0044 + +- **Defends:** the full content of a private branch — file bytes, **file + paths**, commit messages, authors, timestamps, and inner DAG shape — against + any party lacking a recipient (or escrow) key. Every object a private commit + introduces is individually sealed (`Object::Sealed`, fresh random DEK per + object) under a per-branch KEK; the ref points at a manifest whose only + plaintext is structural (a flat sealed-object id list + the public fork + point). An unauthorized clone, the hosting server, and every transport + (plaintext `sc+http://` included) see **ciphertext only** — verified by + `demo/run_private_branch_demo.sh` and by the wire-pack decode test in + `crates/repo/src/private.rs` (the honest structural check a shell grep + cannot make: it decodes every non-sealed object off the wire and asserts no + private plaintext). +- **Leaks by design (accepted metadata):** a private branch's **existence and + name** (name blandly — `hotfix-CVE-1234` in a ref itself discloses), the + sealed-object **count and sizes**, closure **growth over time** (commit + activity is observable as count deltas), the **recipient ids** (in the + manifest's KEK wraps), the **public fork point**, and **which public commits + were merged in** (both plaintext reachability anchors in the manifest — the + fork point plus the tips of any public branch merged in to keep the embargo + current, so the sealed trees' references stay transferable). No content, + path, or message is among these. +- **Revoke is a KEK rotation, not erasure:** `sc branch revoke` mints a fresh + KEK, re-encrypts the index, and rewraps for the remaining recipients — with + zero content plaintext written and zero sealed-object id churn. A revoked + recipient who **already fetched** the branch keeps the old manifest + old KEK + and can decrypt everything sealed *before* the revoke, forever (the same + rotation ≠ erasure boundary ADR-0019 names for secrets — content addressing + keeps the old manifest reachable in any clone that has it). The rotation + guarantees they can read nothing sealed *after*. Real cutover of already- + disclosed content also means rotating the underlying credential. +- **Escrow holders can read pre-publish:** the branch KEK is wrapped to the + configured escrow set at creation (and re-wrapped on every membership + change), so break-glass holders can open an embargoed branch — the standing + meaning of escrow, applied consistently. +- **Publish makes intermediate commit messages public** and gives every + published commit a **new snapshot id** (a sealed id is BLAKE3(ciphertext), a + public id is BLAKE3(plaintext) — equal ids would be the very oracle P33 + closed), so **published commits start unsigned** (re-sign with `sc sign`). + Publish re-runs the P5 secret scanner over all decrypted content **before** + writing any public object, so a secret committed under seal cannot sail into + plaintext history at release. +- **Does NOT defend:** a malicious *recipient* (any key-holder can read, + commit, grant, or publish); the git bridge carries private branches **not at + all** (export/push refuse unconditionally — `--include-encrypted` does not + apply, ADR-0044 §7); and integrating a private branch *into* a public one is + refused everywhere except `sc branch publish` — the one loudly-named, + atomic, scanner-gated crossing. + ## Signed commits & provenance (`sc commit --sign` / `sc verify`) — ADR-0032 - **Defends:** history rewriting (an `amend`/`rebase`/`merge`-attack in a clone or diff --git a/docs/adr/0044-per-branch-access-control.md b/docs/adr/0044-per-branch-access-control.md new file mode 100644 index 00000000..37bbf542 --- /dev/null +++ b/docs/adr/0044-per-branch-access-control.md @@ -0,0 +1,364 @@ +# ADR-0044: Per-branch access control (private branches) + +- **Status:** Accepted +- **Date:** 2026-07-11 +- **Phase:** 34 +- **Builds on:** ADR-0043 (P33 randomized sealing — the seal primitive private + branches reuse), ADR-0014 (P7 protected paths — the split-envelope + discipline generalized here), ADR-0027 (P17 rewrap mechanics — mirrored at + the KEK level), ADR-0026 (P16 revocation semantics — the rotation ≠ erasure + boundary restated), ADR-0032 (P22 signatures — why published commits start + unsigned), ADR-0037 (P27 partial clone — the filter non-interaction), + ADR-0016/0018 (git bridge — the export refusal) + +## Context + +The one thesis capability not yet built: staging work — canonically an +embargoed security fix — on a branch that is **fully opaque** to +non-authorized readers until a coordinated release, then made public in one +atomic operation. + +Nothing existing composes into this. P7/P33 protection encrypts **blob +content only**: trees (file paths), snapshot messages, authors, timestamps, +and the DAG shape are plaintext CAS objects. The requirement here is +stronger — a non-recipient must not read content, must not confirm the +presence of specific file contents, and must not **enumerate the branch's +file paths**. That demands sealing trees and snapshots too, which is +structurally new. + +The equality-confirmation question the original task brief raised +(convergent vs. randomized encryption) was settled by ADR-0043 before this +phase: all protected sealing has been randomized (fresh DEK + nonce) since +P33. Private branches inherit that primitive; this ADR does not re-litigate +the choice — a deterministic seal for branch objects would reopen exactly +the oracle P33 closed, and is rejected below on those grounds. + +## Decision + +### 1. Unit of sealing: per-object + +Every object a private commit introduces — snapshot, tree, blob — is +individually encrypted into a **sealed object**: a new CAS object kind whose +payload is `nonce(24) ‖ AEAD(canonical encoding of the inner object)` under +a fresh random per-object DEK (the ADR-0043 randomized primitive, applied to +object encodings rather than file bytes). The sealed object's id is +BLAKE3 of the ciphertext, so ids reveal nothing about the plaintext. +Content, paths, messages, and the inner DAG are all hidden; object count and +sizes leak (accepted, §Threat-model delta). + +**Accepted cost — no dedup with public objects.** A private branch forked +from `main` re-seals its **entire closure**, including unchanged content: a +sealed tree referencing a public plaintext blob would announce "this file is +unchanged from public" and pre-seed an equality oracle. A private branch +therefore roughly doubles storage for its closure. For the intended shape — +small, short-lived embargo branches — this is acceptable and deliberate. + +### 2. Closure visibility: a sealed-branch manifest + +A private branch's ref points at a **sealed-branch manifest**, a small CAS +object with plaintext *structure* (its fields are visible; the content they +guard is not): + +- the flat list of every sealed object id in the branch closure, +- the sealed tip snapshot's id, +- the key material (§3). + +Keyless parties get exactly one rule each. **gc:** manifest reachable ⇒ +every listed id reachable. **Transport:** diff the flat list against the +peer's haves; a private branch always travels as a whole closure. No inner +DAG shape is visible — strictly less leakage than per-object edge lists. +The manifest is rewritten on every private commit; it is a growing flat +list, and optimizing it is a **non-goal** at hotfix scale. A non-recipient +can watch the closure count grow over time (commit activity is observable +as count deltas) — accepted metadata leakage, stated here deliberately. + +### 3. Key architecture: two-level envelope + +- Each sealed object's DEK is wrapped under a per-branch **branch KEK**. +- The KEK is wrapped per recipient (X25519 → HKDF → AEAD, reusing + `wrap_dek_for`/`unwrap_dek_with` from `crates/crypto`) **and to the + configured escrow set** — the P17 `granted_keys() + escrow` posture. +- All wraps live **in the manifest, never inside sealed object bytes** (the + P7 split-envelope discipline): membership churn never moves an object id. + +Consequences of the shape: + +- **Grant is O(1):** wrap the KEK for the new recipient; one manifest edit. +- **Revoke is O(closure) in wrap operations only:** mint a fresh KEK, unwrap + each object DEK with the old KEK and rewrap under the new, write the new + recipient wrap list. **Zero content plaintext is produced, zero object ids + change** — revocation rewraps without writing plaintext to the object + store, mirroring P17 mechanics one envelope level up. +- **Rotation ≠ erasure, restated (ADR-0019/0026):** a revoked recipient who + already fetched the branch holds the old manifest and old KEK and can + decrypt everything sealed *before* the revoke, forever. The rewrap + guarantees they cannot read anything sealed *after*. Escrow holders can + read embargoed content pre-publish — the standing meaning of break-glass + escrow, applied consistently; a `--no-escrow` creation flag is recorded in + ROADMAP, not built. + +### 4. Publish: atomic full-history replay into public space + +`sc branch publish ` (recipient identity required): + +1. Decrypts the sealed closure through the manifest. +2. **Re-runs the P5 secret scanner over every decrypted file** before + writing any public object — a secret committed under seal must not sail + into plaintext history at the moment of publish. (The scanner also runs + at private-commit time, where the plaintext is in hand; publish is the + last line.) +3. Replays each private commit as a plaintext public snapshot — message, + author, timestamp, and tree content preserved; parents remapped to the + previously-published ids; the fork-point parent keeps its existing + public id. Files under protected prefixes seal through the normal + P7/P33 commit path, so path protection applies in public space. +4. Moves the branch ref to the published tip — the branch simply **becomes + public**. The single ref move is the atomic commit point; one oplog + record; `sc undo`-able. The manifest is dropped; the sealed closure + becomes unreachable and `sc gc` prunes it under the normal grace window. + +**Object ids change on publish, necessarily.** A sealed id is BLAKE3 of +ciphertext; a public id is BLAKE3 of the plaintext canonical encoding. If +they could be equal, the sealed id would be a deterministic function of the +plaintext — precisely the equality oracle ADR-0043 closed. Publish is +therefore a history rewrite by construction, with the standard P22 +consequences: **signatures over private snapshot ids do not carry over; +published commits start unsigned** (re-sign with `sc sign`, the existing +amend/rebase/merge posture), and peers that fetched the private ref see a +non-fast-forward ref move. + +Intermediate commit **messages become public** at publish — operators +staging embargoed fixes must write messages accordingly. A `--squash` +publish mode is recorded in ROADMAP, not built. + +### 5. Operation policy matrix + +| Actor | Operation | Policy | +|---|---|---| +| Recipient | `switch`, `commit`, `status`, `diff`, `log` on the private branch | Normal semantics; reads decrypt through the manifest, commits seal new objects and rewrite the manifest | +| Recipient | `merge main` *into* the private branch | **Allowed** — public content flows in and is re-sealed (it was already public); keeps a long embargo branch current | +| Anyone | merge / cherry-pick / rebase **from** a private ref while on a public branch | **Refused, always**, with a hint → `sc branch publish`. Decrypt-and-commit-to-public *is* publishing minus the atomicity, the scan, and the deliberate decision; the only path to plaintext goes through the one loudly-named command | +| Non-recipient | `fetch` / `push` / `clone` / `gc` | Work by design — ciphertext + manifest travel and collect without keys | +| Non-recipient | `log` / branch listing | Branch **name** visible with a `(private, no access)` marker | +| Non-recipient | `switch`, `merge`, `diff`, `publish`, everything else | Clean refusal | + +Branch existence and **name** are visible by design; embargo branches +should be named blandly (the demo uses `hotfix-CVE-2026-1234` only as a +worked example — a real embargo would not put the CVE id in the ref name). + +### 6. Creation and membership surface + +- `sc branch --private --to ...` — the existing creation + verb plus flags, matching the house `--to ` idiom (the + task brief's `branch create --private --recipients` spelling is + deliberately not used). Identity required (`--identity` / `SC_IDENTITY` / + `~/.sc/identity`); the **creator is always wrapped in** even if absent + from `--to` (a branch its creator cannot read is a pure foot-gun). +- `sc branch grant --to --identity ` — O(1) KEK wrap. +- `sc branch revoke --recipient-id --identity ` — + **revoke implies immediate, atomic KEK rotation + DEK rewrap** in the same + command. Unlike P7 path revoke (policy-only, rewrap separate), a private + branch revoke that left the old KEK live until a later manual rewrap would + be a standing foot-gun on exactly the branch type where it matters most; + the Q3 architecture makes the rewrap cheap enough to fuse. `--identity` + is required (unwrapping DEKs from the old KEK needs a key). +- Both are ref-moving ops (they rewrite the manifest the ref points at): + in-progress-op guarded, oplogged, `sc undo`-able — the standard treatment. +- **P17 `sc rewrap` does not touch private-branch manifests.** Branch + membership is managed only through the `sc branch` surface; the bulk + cutover command stays scoped to secrets + protected paths rather than + growing a fourth semantics. +- **Local branch names stay flat** (P28 ref grammar unchanged); `/` remains + reserved for remote-tracking refs. Slash-names are an independent naming + feature, recorded in ROADMAP. + +### 7. Git bridge: unconditional exclusion + +`sc export --to ` refuses when the current branch is private +(hint: publish first). `sc push ` refuses a private branch. +Fetch through the bridge can never mint one (git has no source for a +manifest). **`--include-encrypted` does not cover private branches**: that +flag exists for P7 protected *paths*, where the exported git repo stays +structurally coherent (public trees, sealed file contents). A private +branch has no exportable git structure at all — the structure is what's +sealed — so the refusal is unconditional, not flag-gated. `crates/gitio` +never learns the new object kinds exist. + +### 8. Format and wire + +**No existing object's on-disk format changes.** Snapshots, trees, blobs, +secrets, and signatures are untouched; `TAG_SNAPSHOT` stays 4. The two new +object kinds (sealed object, sealed-branch manifest) are **additive tags**. + +Because the new kinds cross the wire on push/fetch/clone, +**`PROTOCOL_VERSION` bumps 3 → 4**: a pre-P34 peer fails cleanly at the +handshake with a version error instead of hitting an unknown object tag +deep in pack ingest. A P34 client cannot talk to a P33 server even about +public branches — zero cost in practice under the same no-deployed-old-peers +reality every prior format-touching phase leaned on, and it fails loud at +the seam. + +**Transport safety is by construction, and pinned by test:** the wire ships +stored bytes, and a private branch's stored bytes are ciphertext + the +plaintext-structure manifest (ids and wraps — no content, no paths, no +messages). The plaintext `sc+http://` transport therefore never carries +private plaintext. A regression test captures the wire bytes of a private +push/fetch and asserts known plaintext markers (file content, paths, commit +message) appear nowhere in the stream. + +### 9. Partial clone and sparse + +Filtering and privacy do not compose: a server cannot path-filter inside +sealed trees — that inability is the feature. So: + +- `sc clone --filter ` **excludes private branches entirely** (no + ref created). +- Everywhere else, a private branch travels as its whole manifest closure + or not at all. +- `sc sparse` on a private branch works normally for a recipient — sparse + is checkout-only (ADR-0034) and applies after decryption. + +No P27 promisor/gap machinery grows manifest-aware cases. + +## Threat-model delta (to be added to docs/THREAT-MODEL.md) + +- **Defends:** content, file paths, commit messages, authors, timestamps, + and DAG shape of a private branch against any party lacking a recipient + (or escrow) key — including the hosting server, all transports (plaintext + `sc+http://` included), and every clone. +- **Does NOT defend / leaks by design:** branch **existence and name**; + sealed object **count and sizes**; closure **growth over time** (commit + activity); who the recipients are is not hidden from manifest holders + (recipient ids are in the wraps). Revoked recipients keep everything + sealed before the revoke (rotation ≠ erasure). Escrow holders can read + pre-publish. Publish makes intermediate commit messages public. + +## Alternatives considered + +- **Whole-branch sealed bundle.** One encrypted object holding the entire + serialized history. Maximal opacity, but every commit re-writes and + re-transfers the whole branch, and every merge/replay path needs a second + codepath outside the CAS grain. Rejected. +- **Reuse per-file protection (`sc protect ""`) + encrypted commit + messages.** Trees stay plaintext, so paths enumerate — fails the core + requirement outright. Rejected. +- **Convergent/deterministic sealing of branch objects** (dedup with public + space, dedup across private branches). Reopens the equality-confirmation + oracle ADR-0043 closed, and against a *sealed tree* it would confirm + whole-subtree equality with public history. Rejected. +- **Per-object plaintext edge lists instead of a manifest** (gc/transport + walk sealed objects like a DAG). Leaks the inner DAG shape — snapshot vs. + tree fanout, parent counts, per-commit structure evolution. The manifest + leaks strictly less and gives keyless parties one dumb rule. Rejected. +- **Flat per-recipient wraps per object (no KEK).** Grant becomes + O(closure) and a true revoke-rewrap forces re-sealing every object (full + id churn). Rejected for the two-level envelope. +- **Keys required for gc/serve.** Breaks the unauthorized-ciphertext-host + story that the rest of the system is built on. Rejected. +- **Opaque sealed refs through the git bridge.** No coherent git structure + to write; the marks map gains a second incompatible meaning; publish + goes permanently stale against the exported artifact; and it hands + operators a one-command path to durably publishing embargo ciphertext on + a public host where it is archived beyond any later rewrap. Rejected. +- **Keep `PROTOCOL_VERSION` 3 (ADR-0043's no-bump posture).** The failure + mode against an old peer is an unknown-tag decode error mid-ingest rather + than a clean handshake refusal. New object kinds crossing the wire is + what a protocol version is for (P27 precedent). Rejected. +- **Publish preserving object ids.** Impossible without making sealed ids a + deterministic function of plaintext — the oracle again. Not a real + alternative; recorded because the task brief asked the question. + +## As built (P34) + +The decision shipped as designed; the concrete shape settled as follows. + +- **Two additive object kinds, no existing-format change.** `TAG_SEALED` (7) + is `nonce(24) ‖ AEAD(inner encoding)` under a per-object random DEK; + `TAG_MANIFEST` (8) carries `base`, `prev` (the superseded manifest — a + meta-history chain the push ff-check and gc walk over like parents), + `closure` (sorted sealed-object ids), `index_ct` (the KEK-encrypted + branch index), and `kek_wraps`. Snapshots/trees/blobs/secrets/signatures + are byte-for-byte unchanged (`TAG_SNAPSHOT` stays 4); `PROTOCOL_VERSION` + bumped 3 → 4 so a v3 peer refuses at the handshake rather than hitting an + unknown tag deep in pack ingest. +- **`crates/crypto/src/private.rs`** owns the three new primitives — + `seal_object`/`open_object` (per-object DEK, distinct AAD domain from P7 + path blobs so a sealed body can never be opened as a path blob), the branch + KEK (`generate_kek`/`wrap_kek_for`/`unwrap_kek_with`, reusing the P2 + X25519 envelope), and `BranchIndex` (the `inner id → (sealed id, DEK)` map + + inner tip, encrypted under the KEK; DEKs zeroize on drop). RustCrypto + stays quarantined. +- **Copy-on-write sealing.** `crates/repo/src/private.rs` resolves inner + objects through the index with a public-store fallback: an inner id absent + from the index is public content read directly, so a private branch forked + from `main` seals nothing at creation and reseals only what actually + changes — an unchanged subtree keeps its public id (the tree builder + matches vfs byte-for-byte, `FileMode(0o755)` on dir entries). Verified by + `cow_sealing_unchanged_content_is_never_resealed`. +- **Grant is one wrap; revoke is KEK rotation.** `branch_grant` appends one + KEK wrap (index + closure untouched). `branch_revoke` mints a fresh KEK, + re-encrypts the index, rewraps for the remaining recipients, and **refuses + loudly** if any still-wrapped recipient has no known public key (never + silent access loss) — zero content plaintext, zero sealed-object churn. +- **Publish is two-phase.** Every public object is built in memory and the P5 + scanner runs over all decrypted plain files **before** the first + `store.put` — a failed publish moves no ref and writes no object + (`publish_scanner_gate_aborts_before_any_public_write`). Protected-prefix + files re-seal through the normal P7/P33 path (stay `PROTECTED` in public + space, `protected_paths_survive_a_private_round_trip`). +- **Refusals are centralized.** `refuse_on_private` guards every + snapshot-assuming ref-moving op (`commit`/`amend`/`branch`/`protect`/ + `secret *`/`rewrap`/`sparse *`/`ws`/`work`/`transcript attach`/`sign`); + `PrivateIntegration` refuses merge/cherry-pick/rebase *from* a private ref; + `commit`/`status`/`diff`/`log`/`merge`/`switch` dispatch to the private + variant when HEAD is private. `undo` restores manifest refs but skips + rematerialize (no identity in hand) — the working tree goes stale until + `sc switch … --identity`. +- **Operator footgun, unchanged from every prior phase:** keep identity + files **outside** the working tree — a key committed under a private + branch is sealed into it and vanishes from disk on the next switch (the + standing [[scanner-false-positive-own-keys]] hazard). +- **Publish is a non-fast-forward, enforced (Copilot review).** An earlier + build gave `is_ancestor` a manifest→snapshot special case ("a manifest is + an ancestor of any snapshot sharing its `base`") so a remote holding the + pre-publish manifest would accept the published tip as a fast-forward. That + was unsound and contradicted this ADR's own Decision §4 ("peers see a + non-fast-forward ref move"): publish severs the id chain by construction, + so the rule couldn't distinguish the legitimate published tip from a + *rollback* — an older manifest (same fork point) re-published to clobber a + newer remote manifest's grants/revokes/commits (silent data loss). The + special case is removed; `is_ancestor` is purely the `prev`-chain walk, so + a publish-push over a remote still holding the private manifest is a + genuine non-ff (the remote fetches, or the operator force-pushes — the same + posture as `amend`/`rebase`). Pinned by + `manifest_ancestry_is_prev_chain_and_publish_is_not_a_fast_forward`. +- **Reachability anchors for merged-in public content (review Critical).** + `merge_into_private` carries the merged public tip's objects into the + sealed inner tree as **unsealed public references** (copy-on-write — they + are already public, resealing them would waste space and fight dedup). But + a keyless party can't walk a sealed tree to discover those references, and + the manifest's `base` only anchors the *fork point* — so a public object + added *after* the fork and merged in was reachable through no root a + transfer or gc could follow. Pushing just the private branch to a peer + lacking those commits would strand the sealed tree (`NotFound` on the + recipient's `switch`). Fixed by a cumulative plaintext `anchors: Vec< + ObjectId>` on the manifest — the merged-in public tips — walked as + reachability roots alongside `base`. Costs one metadata leak (*which* + public commits were merged in), the same class as `base` leaking the fork + point. Pinned by `merged_in_private_branch_is_self_contained_for_transfer` + (reachability from the manifest alone includes the merged blob) and + `recipient_opens_a_merged_branch_cloned_without_the_public_source` (a peer + that never received the public branch still opens the merged private one). + +## Deferred (to ROADMAP.md) + +- `sc branch publish --squash` (hide intermediate history at publish). +- `--no-escrow` at private-branch creation. +- Slash-names for local branches (independent of access control). +- Manifest scalability beyond hotfix-scale closures. +- Sealing the recipient set and fork point (currently plaintext manifest + metadata) — would need an anonymous-recipient KEK-wrap scheme. +- `sc branch grant/revoke`-style rotation of the KEK on a schedule, and a + fetch-time freshness attestation so a stale pre-revoke manifest can't be + re-served (the gittuf-shaped ref-attestation effort ADR-0032 already + defers).