diff --git a/crates/synapse-api/src/lib.rs b/crates/synapse-api/src/lib.rs index 3b1cd02..830e0c9 100644 --- a/crates/synapse-api/src/lib.rs +++ b/crates/synapse-api/src/lib.rs @@ -1146,6 +1146,28 @@ impl SynapseService { &self.root_dir } + pub fn repo_id(&self) -> &RepoId { + &self.repo_id + } + + /// Authorize an export/publication request (`Capability::Publish` at `BoundaryKind::Export`) + /// against this service's repository authority and policy enforcer. + pub fn authorize_export( + &mut self, + operation: &str, + req: &AuthenticatedRequest, + now_ms: u64, + ) -> SynapseResult<()> { + self.auth.authorize( + BoundaryKind::Export, + operation, + &self.repo_id, + Capability::Publish, + req, + now_ms, + ) + } + fn validate_page_request(&self, page: &PageRequest) -> SynapseResult { if page.limit == 0 || page.limit > self.max_page_size { return Err(SynapseError::new( diff --git a/crates/synapse-bench/src/conformance.rs b/crates/synapse-bench/src/conformance.rs index 8e5c2f6..6e214b3 100644 --- a/crates/synapse-bench/src/conformance.rs +++ b/crates/synapse-bench/src/conformance.rs @@ -21,6 +21,7 @@ use std::collections::BTreeMap; use std::path::{Path, PathBuf}; use std::sync::Arc; use synapse_api::{SynapseService, TreeTarget}; +use synapse_auth::{AgentCapabilityGrant, AuthenticatedRequest, Capability}; use synapse_core::{ encode_did_key_ed25519, BlobDigest, ChangesetEnvelope, ChangesetId, FileMode, GitOid, IdempotencyKey, MutationOp, PathMutation, RefusalCode, RepoId, WalSeq, @@ -28,6 +29,7 @@ use synapse_core::{ }; use synapse_git::{ advance_human_branch_fast_forward, materialize_changeset_to_git, GitProjectionIndex, + HumanBranchAdvanceRequest, }; use synapse_radicle::{ load_radicle_state_from_bare_repo, CanonicalPromotionEngine, DelegateSigrefs, @@ -434,11 +436,48 @@ struct GitFixture { base_repo: PathBuf, base_commit: String, svc: SynapseService, + ids: BenchIdentities, c1: ChangesetId, c2: ChangesetId, c3: ChangesetId, } +fn sign_publish_advance( + ids: &BenchIdentities, + label: &str, + branch: &str, + new_commit_oid: GitOid, + now_ms: u64, +) -> Result, String> { + let agent = &ids.agents[0]; + let grant = AgentCapabilityGrant::issue( + &ids.authority_key, + synapse_arm::label_bytes(&format!("publish-grant-{label}")), + ids.repo_id.clone(), + agent.did.clone(), + vec![Capability::Publish], + synapse_arm::BENCH_MAX_BYTES_PER_OP, + u32::MAX, + synapse_arm::BENCH_RATE_WINDOW_MS, + now_ms.saturating_sub(3_600_000), + now_ms + 7 * 24 * 3_600_000, + ); + AuthenticatedRequest::sign_new( + &agent.key, + grant, + ids.repo_id.clone(), + Capability::Publish, + synapse_arm::label_bytes(&format!("publish-nonce-{label}")), + now_ms, + 0, + HumanBranchAdvanceRequest { + branch: branch.to_string(), + new_commit_oid, + }, + ) + .map_err(|e| e.to_string()) +} + fn upsert(path: &str, body: &str, mode: FileMode) -> FileEdit { FileEdit { path: path.to_string(), @@ -567,6 +606,7 @@ fn git_fixture(work: &Path) -> Result { base_repo, base_commit, svc, + ids, c1, c2, c3, @@ -759,7 +799,9 @@ pub fn run_git_suite(work: &Path) -> SuiteResult { "advancing refs/heads/main from the base to the stacked descendant succeeds (positive control)", || { let commit = c2_commit.ok_or("no materialized commit")?; - let r = advance_human_branch_fast_forward(&t1, "main", commit); + let now = crate::unix_now_ms(); + let req = sign_publish_advance(&fx.ids, "ff", "main", commit, now)?; + let r = advance_human_branch_fast_forward(&mut fx.svc, &t1, &req, now); let head = git_line(&t1, &["rev-parse", "refs/heads/main"])?; Ok((r.is_ok() && head == commit.to_hex(), format!("result {:?}; main at {head}", code_of(&r)))) }, @@ -773,7 +815,9 @@ pub fn run_git_suite(work: &Path) -> SuiteResult { let before = git_line(&t1, &["rev-parse", "refs/heads/main"])?; let m3 = materialize_changeset_to_git(&mut fx.svc, &mut index, &t1, fx.c3, None, None) .map_err(|e| e.to_string())?; - let r = advance_human_branch_fast_forward(&t1, "main", m3.commit_oid); + let now = crate::unix_now_ms(); + let req = sign_publish_advance(&fx.ids, "non-ff", "main", m3.commit_oid, now)?; + let r = advance_human_branch_fast_forward(&mut fx.svc, &t1, &req, now); let after = git_line(&t1, &["rev-parse", "refs/heads/main"])?; Ok(( code_of(&r) == Some(RefusalCode::GitRefNonFastForward) && before == after, diff --git a/crates/synapse-bench/tests/crumb_synapse_qualification.rs b/crates/synapse-bench/tests/crumb_synapse_qualification.rs index 45cd61a..83f4a01 100644 --- a/crates/synapse-bench/tests/crumb_synapse_qualification.rs +++ b/crates/synapse-bench/tests/crumb_synapse_qualification.rs @@ -36,8 +36,8 @@ use synapse_evidence::{ ReceiptResult, TestIdentity, MAX_LOG_BYTES, MAX_RECEIPT_BYTES, }; use synapse_git::{ - materialize_changeset_to_git, project_github_pull_request, GitHubPrProjectionRequest, - GitProjectionIndex, + advance_human_branch_fast_forward, materialize_changeset_to_git, project_github_pull_request, + GitHubPrProjectionRequest, GitProjectionIndex, HumanBranchAdvanceRequest, }; use synapse_radicle::{ CanonicalPromotionEngine, DelegateSigrefs, PromotionCandidate, RadicleIdentityDoc, @@ -1352,8 +1352,9 @@ fn s12_exit_rule_passing_run_is_not_publication() { let mut index = GitProjectionIndex::new(); let mat = materialize_changeset_to_git(&mut fx.svc, &mut index, &bare, cs, None, None).unwrap(); - // Gate 1: Capability::Publish. Every other capability, plus a Current Pass receipt in - // hand, is refused. (Neither gate takes a receipt as input: there is no field for one.) + // Gate 1: Capability::Publish (both GitHub PR projection and human branch fast-forward). + // Every other capability, plus a Current Pass receipt in hand, is refused, and refs/heads/main + // is never created or moved. (Neither gate takes a receipt as input: there is no field for one.) let pr = |n| GitHubPrProjectionRequest { pr_number: n, changeset_id: cs, @@ -1361,6 +1362,10 @@ fn s12_exit_rule_passing_run_is_not_publication() { title: "d5".into(), github_merged: false, }; + let adv_branch = || HumanBranchAdvanceRequest { + branch: "main".into(), + new_commit_oid: mat.commit_oid, + }; for signer in [alice(), runner()] { let req = fx.sign_as( &signer, @@ -1376,6 +1381,26 @@ fn s12_exit_rule_passing_run_is_not_publication() { let now = fx.now(); let err = project_github_pull_request(&mut fx.svc, &index, &req, now).unwrap_err(); assert_eq!(err.code, RefusalCode::AuthCapabilityDenied); + + let branch_req = fx.sign_as( + &signer, + vec![ + Capability::Read, + Capability::Propose, + Capability::Evaluate, + Capability::Review, + ], + Capability::Publish, + adv_branch(), + ); + let now = fx.now(); + let err = + advance_human_branch_fast_forward(&mut fx.svc, &bare, &branch_req, now).unwrap_err(); + assert_eq!(err.code, RefusalCode::AuthCapabilityDenied); + assert!( + !bare.join("refs/heads/main").exists(), + "refs/heads/main must not be created or moved without Publish" + ); } // Gate 2: Radicle canonical promotion needs the delegate quorum. Neither the author nor @@ -1423,6 +1448,20 @@ fn s12_exit_rule_passing_run_is_not_publication() { let now = fx.now(); let proj = project_github_pull_request(&mut fx.svc, &index, &req, now).unwrap(); assert_eq!(proj.head_git_oid, mat.commit_oid); + let branch_req = fx.sign_as( + &alice(), + vec![Capability::Publish], + Capability::Publish, + adv_branch(), + ); + let now = fx.now(); + advance_human_branch_fast_forward(&mut fx.svc, &bare, &branch_req, now).unwrap(); + assert_eq!( + fs::read_to_string(bare.join("refs/heads/main")) + .unwrap() + .trim(), + mat.commit_oid.to_hex() + ); let head = engine .verify_and_promote( &candidate, @@ -1433,6 +1472,6 @@ fn s12_exit_rule_passing_run_is_not_publication() { result( "S12", "PASS", - "advisory read (no foreign claims) + Current Pass receipt: GitHub PR projection refused without Publish (AuthCapabilityDenied, author and runner); Radicle promotion refused without delegate quorum (none / runner / author sigrefs); both open only for a Publish grant / delegate sigrefs. AEGIS not integrated (separate repo)", + "advisory read (no foreign claims) + Current Pass receipt: GitHub PR projection and advance_human_branch_fast_forward refused without Publish (AuthCapabilityDenied, author and runner; refs/heads/main untouched); Radicle promotion refused without delegate quorum (none / runner / author sigrefs); all open only for a Publish grant / delegate sigrefs. AEGIS not integrated (separate repo)", ); } diff --git a/crates/synapse-git/src/lib.rs b/crates/synapse-git/src/lib.rs index d133e4c..ed43932 100644 --- a/crates/synapse-git/src/lib.rs +++ b/crates/synapse-git/src/lib.rs @@ -13,7 +13,7 @@ use std::io::Write; use std::path::Path; use std::process::Command; use synapse_api::{SynapseService, TreeTarget, VirtualTreeEntry}; -use synapse_auth::{AuthenticatedRequest, BoundaryKind, Capability}; +use synapse_auth::AuthenticatedRequest; use synapse_core::{ ChangesetId, GitHubPullRequestProjection, GitOid, RefusalCode, SynapseError, SynapseResult, WalSeq, @@ -88,6 +88,14 @@ pub struct GitHubPrProjectionRequest { pub github_merged: bool, } +/// Request payload for advancing a human-facing Git branch (`refs/heads/`) +/// to a fast-forward descendant commit (`Capability::Publish` at `BoundaryKind::Export`). +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub struct HumanBranchAdvanceRequest { + pub branch: String, + pub new_commit_oid: GitOid, +} + /// Compute RFC 1950 Adler-32 checksum over `data`. pub fn adler32(data: &[u8]) -> u32 { const MOD_ADLER: u32 = 65521; @@ -235,8 +243,19 @@ fn write_dir_node_recursive(git_dir: &Path, node: &DirNode) -> SynapseResult SynapseResult<()> { + if !ref_name.starts_with("refs/synapse/") || ref_name.contains("..") { + return Err(SynapseError::new( + RefusalCode::PathInvalid, + format!("invalid or unauthenticated human git ref name: {ref_name}"), + )); + } + write_git_ref_raw(git_dir, ref_name, oid) +} + +fn write_git_ref_raw(git_dir: &Path, ref_name: &str, oid: GitOid) -> SynapseResult<()> { if !ref_name.starts_with("refs/") || ref_name.contains("..") { return Err(SynapseError::new( RefusalCode::PathInvalid, @@ -382,14 +401,36 @@ pub fn materialize_changeset_to_git( }) } -/// Advance a human-facing branch (`refs/heads/`) ONLY if `new_commit_oid` is a strict -/// fast-forward descendant of the current branch head, refusing any non-fast-forward update -/// with `RefusalCode::GitRefNonFastForward`. -pub fn advance_human_branch_fast_forward( +fn validate_human_branch_name(branch: &str) -> SynapseResult<()> { + if branch.is_empty() + || branch.starts_with('/') + || branch.ends_with('/') + || branch.starts_with('.') + || branch.ends_with(".lock") + || branch.contains("..") + || branch.contains("//") + || branch.contains("@{") + || branch.contains('\\') + || branch + .bytes() + .any(|b| b <= b' ' || b == 0x7f || matches!(b, b'~' | b'^' | b':' | b'?' | b'*' | b'[')) + { + return Err(SynapseError::new( + RefusalCode::PathInvalid, + format!("invalid human branch name: {branch}"), + )); + } + Ok(()) +} + +/// Unauthenticated crate-private helper that advances `refs/heads/` only if +/// `new_commit_oid` is a fast-forward descendant of the current branch head. +fn advance_human_branch_fast_forward_unchecked( git_dir: &Path, branch: &str, new_commit_oid: GitOid, ) -> SynapseResult<()> { + validate_human_branch_name(branch)?; let ref_name = format!("refs/heads/{branch}"); let rev_out = Command::new("git") .arg("--git-dir") @@ -421,7 +462,25 @@ pub fn advance_human_branch_fast_forward( } } - write_git_ref(git_dir, &ref_name, new_commit_oid) + write_git_ref_raw(git_dir, &ref_name, new_commit_oid) +} + +/// Advance a human-facing branch (`refs/heads/`) after authorizing `Capability::Publish` +/// at `BoundaryKind::Export` via `SynapseService::authorize_export`, and ONLY if `new_commit_oid` +/// is a fast-forward descendant of the current branch head (refusing non-fast-forward updates +/// with `RefusalCode::GitRefNonFastForward`). +pub fn advance_human_branch_fast_forward( + service: &mut SynapseService, + git_dir: &Path, + req: &AuthenticatedRequest, + now_ms: u64, +) -> SynapseResult<()> { + service.authorize_export("advance_human_branch_fast_forward", req, now_ms)?; + advance_human_branch_fast_forward_unchecked( + git_dir, + &req.payload.branch, + req.payload.new_commit_oid, + ) } /// Build and record a bounded `GitHubPullRequestProjection` (`Capability::Publish` at `BoundaryKind::Export`). @@ -432,21 +491,8 @@ pub fn project_github_pull_request( req: &AuthenticatedRequest, now_ms: u64, ) -> SynapseResult { - let repo_id = service - .store() - .list_changesets(None, None, None)? - .first() - .map(|cs| cs.envelope.repo_id.clone()) - .unwrap_or_else(|| req.target_repo_id.clone()); - - service.auth_mut().authorize( - BoundaryKind::Export, - "project_github_pr", - &repo_id, - Capability::Publish, - req, - now_ms, - )?; + let repo_id = service.repo_id().clone(); + service.authorize_export("project_github_pr", req, now_ms)?; let stored = service .store() @@ -522,7 +568,7 @@ mod tests { use super::*; use ed25519_dalek::SigningKey; use synapse_api::SubmitChangesetPayload; - use synapse_auth::AgentCapabilityGrant; + use synapse_auth::{AgentCapabilityGrant, Capability}; use synapse_core::{ encode_did_key_ed25519, BlobDigest, ChangesetEnvelope, EvidenceAttachment, FileMode, IdempotencyKey, MutationOp, PathMutation, RepoId, SYNAPSE_ENVELOPE_VERSION, @@ -676,7 +722,21 @@ mod tests { assert_eq!(index.changeset_for_commit(mat1.commit_oid), Some(cs1_id)); // Fast-forward human `main` branch to `mat1.commit_oid` - advance_human_branch_fast_forward(&bare_git_dir, "main", mat1.commit_oid).unwrap(); + let adv1_req = AuthenticatedRequest::sign_new( + &agent_key, + grant.clone(), + repo_id.clone(), + Capability::Publish, + [11u8; 32], + 1_500, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat1.commit_oid, + }, + ) + .unwrap(); + advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &adv1_req, 1_500).unwrap(); // 4. Verify `git fsck --full --strict` passes with zero errors or warnings let fsck_out = Command::new("git") @@ -770,9 +830,38 @@ mod tests { // 7. Verify non-fast-forward human branch protection: // Advance human `main` to `mat2.commit_oid` (fast-forward succeeds), // then attempt to move human `main` backwards to `mat1.commit_oid` -> refused with GitRefNonFastForward! - advance_human_branch_fast_forward(&bare_git_dir, "main", mat2.commit_oid).unwrap(); + let adv2_req = AuthenticatedRequest::sign_new( + &agent_key, + grant.clone(), + repo_id.clone(), + Capability::Publish, + [12u8; 32], + 2_500, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat2.commit_oid, + }, + ) + .unwrap(); + advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &adv2_req, 2_500).unwrap(); + let adv_back_req = AuthenticatedRequest::sign_new( + &agent_key, + grant.clone(), + repo_id.clone(), + Capability::Publish, + [13u8; 32], + 2_600, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat1.commit_oid, + }, + ) + .unwrap(); let non_ff_err = - advance_human_branch_fast_forward(&bare_git_dir, "main", mat1.commit_oid).unwrap_err(); + advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &adv_back_req, 2_600) + .unwrap_err(); assert_eq!(non_ff_err.code, RefusalCode::GitRefNonFastForward); // 8. Attach evaluation receipt and build GitHub PR projection (`Capability::Publish`) @@ -811,4 +900,277 @@ mod tests { let _ = fs::remove_dir_all(&work); } + + #[test] + fn test_advance_human_branch_requires_publish_capability_and_rejects_bypass() { + let work = temp_dir("publish-gate"); + let bare_git_dir = work.join("origin.git"); + let svc_dir = work.join("synapse-service"); + + Command::new("git") + .args(["init", "--bare"]) + .arg(&bare_git_dir) + .output() + .unwrap(); + + let repo_id = RepoId::new("aien-spark/synapse").unwrap(); + let mut svc = SynapseService::open( + &svc_dir, + repo_id.clone(), + FsyncPolicy::EveryCommit, + RecoveryPolicy::StrictFailClosed, + StoreLimits::default(), + ) + .unwrap(); + + let auth_key = SigningKey::from_bytes(&[50u8; 32]); + let auth_did = encode_did_key_ed25519(&auth_key.verifying_key().to_bytes()); + svc.register_authority(auth_did); + svc.register_base_tree(GitOid::ZERO, vec![]).unwrap(); + + let author_key = SigningKey::from_bytes(&[51u8; 32]); + let author_did = encode_did_key_ed25519(&author_key.verifying_key().to_bytes()); + let no_publish_grant = AgentCapabilityGrant::issue( + &auth_key, + [21u8; 32], + repo_id.clone(), + author_did.clone(), + vec![ + Capability::Read, + Capability::Propose, + Capability::Evaluate, + Capability::Review, + ], + 1024 * 1024, + 100, + 60_000, + 10, + 100_000, + ); + + let body = b"fn gated() {} +"; + let d_body = BlobDigest::compute(body); + let mut env = ChangesetEnvelope { + version: SYNAPSE_ENVELOPE_VERSION, + repo_id: repo_id.clone(), + base_git_oid: GitOid::ZERO, + parent_changesets: vec![], + author_did: String::new(), + idempotency_key: IdempotencyKey::from_label("publish-gate-cs1"), + created_at_ms: 1_700_000_000_000, + intent_summary: "Propose gated change".to_string(), + mutations: vec![PathMutation { + path: "src/lib.rs".to_string(), + op: MutationOp::Upsert { + mode: FileMode::Regular644, + blob_digest: d_body, + size_bytes: body.len() as u64, + }, + }], + evidence_receipts: vec![], + signature: vec![], + }; + env.sign(&author_key); + let sub_req = AuthenticatedRequest::sign_new( + &author_key, + no_publish_grant.clone(), + repo_id.clone(), + Capability::Propose, + [22u8; 32], + 1_000, + 0, + SubmitChangesetPayload { + envelope: env, + blobs: vec![(d_body, body.to_vec())], + }, + ) + .unwrap(); + let cs_id = match svc.submit(&sub_req, 1_000).unwrap() { + IngestOutcome::Committed { changeset_id, .. } => changeset_id, + _ => unreachable!(), + }; + + // Attach a passing evaluation receipt to verify that passing validation never grants Publish. + svc.store_mut() + .attach_evaluation( + cs_id, + EvidenceAttachment { + gate_name: "cargo-test".to_string(), + passed: true, + artifact_digest: [0xcd; 32], + summary: "all tests passed".to_string(), + }, + ) + .unwrap(); + + let mut index = GitProjectionIndex::new(); + let mat = materialize_changeset_to_git( + &mut svc, + &mut index, + &bare_git_dir, + cs_id, + Some("lane-beta"), + None, + ) + .unwrap(); + + // Direct write_git_ref to refs/heads/main is refused as PathInvalid. + let direct_err = + write_git_ref(&bare_git_dir, "refs/heads/main", mat.commit_oid).unwrap_err(); + assert_eq!(direct_err.code, RefusalCode::PathInvalid); + assert!(!bare_git_dir.join("refs/heads/main").exists()); + + // 1. Grant without Capability::Publish is refused with AuthCapabilityDenied; ref does not move. + let unauth_req = AuthenticatedRequest::sign_new( + &author_key, + no_publish_grant.clone(), + repo_id.clone(), + Capability::Publish, + [23u8; 32], + 1_100, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat.commit_oid, + }, + ) + .unwrap(); + let err = advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &unauth_req, 1_100) + .unwrap_err(); + assert_eq!(err.code, RefusalCode::AuthCapabilityDenied); + assert!(!bare_git_dir.join("refs/heads/main").exists()); + + // 2. Request signed with Capability::Propose instead of Capability::Publish is refused. + let wrong_cap_req = AuthenticatedRequest::sign_new( + &author_key, + no_publish_grant, + repo_id.clone(), + Capability::Propose, + [24u8; 32], + 1_200, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat.commit_oid, + }, + ) + .unwrap(); + let err = advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &wrong_cap_req, 1_200) + .unwrap_err(); + assert_eq!(err.code, RefusalCode::AuthCapabilityDenied); + assert!(!bare_git_dir.join("refs/heads/main").exists()); + + // 3. Cross-repo grant is refused with AuthCrossRepoDenied. + let other_repo = RepoId::new("aien-spark/other").unwrap(); + let cross_grant = AgentCapabilityGrant::issue( + &auth_key, + [25u8; 32], + other_repo.clone(), + author_did.clone(), + vec![Capability::Publish], + 1024 * 1024, + 100, + 60_000, + 10, + 100_000, + ); + let cross_req = AuthenticatedRequest::sign_new( + &author_key, + cross_grant, + other_repo, + Capability::Publish, + [26u8; 32], + 1_300, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat.commit_oid, + }, + ) + .unwrap(); + let err = advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &cross_req, 1_300) + .unwrap_err(); + assert_eq!(err.code, RefusalCode::AuthCrossRepoDenied); + assert!(!bare_git_dir.join("refs/heads/main").exists()); + + // 4. Expired Publish grant is refused with AuthGrantExpired. + let publish_grant = AgentCapabilityGrant::issue( + &auth_key, + [27u8; 32], + repo_id.clone(), + author_did, + vec![Capability::Publish], + 1024 * 1024, + 100, + 60_000, + 10, + 5_000, + ); + let exp_req = AuthenticatedRequest::sign_new( + &author_key, + publish_grant.clone(), + repo_id.clone(), + Capability::Publish, + [28u8; 32], + 6_000, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat.commit_oid, + }, + ) + .unwrap(); + let err = advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &exp_req, 6_000) + .unwrap_err(); + assert_eq!(err.code, RefusalCode::AuthGrantExpired); + assert!(!bare_git_dir.join("refs/heads/main").exists()); + + // 5. Path traversal or malformed branch name is refused even with valid Publish grant. + let bad_branch_req = AuthenticatedRequest::sign_new( + &author_key, + publish_grant.clone(), + repo_id.clone(), + Capability::Publish, + [29u8; 32], + 2_000, + 0, + HumanBranchAdvanceRequest { + branch: "../synapse/agent-head/lane-beta".to_string(), + new_commit_oid: mat.commit_oid, + }, + ) + .unwrap(); + let err = + advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &bad_branch_req, 2_000) + .unwrap_err(); + assert_eq!(err.code, RefusalCode::PathInvalid); + assert!(!bare_git_dir.join("refs/heads/main").exists()); + + // 6. Valid Publish grant succeeds and advances refs/heads/main; replay of the same nonce is refused. + let valid_req = AuthenticatedRequest::sign_new( + &author_key, + publish_grant, + repo_id, + Capability::Publish, + [30u8; 32], + 2_100, + 0, + HumanBranchAdvanceRequest { + branch: "main".to_string(), + new_commit_oid: mat.commit_oid, + }, + ) + .unwrap(); + advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &valid_req, 2_100).unwrap(); + let head = fs::read_to_string(bare_git_dir.join("refs/heads/main")).unwrap(); + assert_eq!(head.trim(), mat.commit_oid.to_hex()); + + let replay_err = + advance_human_branch_fast_forward(&mut svc, &bare_git_dir, &valid_req, 2_101) + .unwrap_err(); + assert_eq!(replay_err.code, RefusalCode::AuthReplayDetected); + + let _ = fs::remove_dir_all(&work); + } }