Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
2a8d64d
feat(core)!: verify_chain returns the proof chain's root issuer
Vasanthdev2004 Aug 14, 2026
13a2424
fix(node): gate two Unix-only tests so the crate compiles on Windows
Vasanthdev2004 Aug 14, 2026
1b1c972
feat(node): carry the verified UCAN and its root into the request
Vasanthdev2004 Aug 14, 2026
f8b9cf1
feat(node): add ucan_grants_push, anchored at the repo owner
Vasanthdev2004 Aug 14, 2026
d27f0be
feat(node): honour a delegated git/push capability on the push path
Vasanthdev2004 Aug 14, 2026
d141e59
feat(gl): gl ucan import stores a delegation for the push helper
Vasanthdev2004 Aug 14, 2026
3d81f57
feat(git-remote): send a delegated push capability as X-Ucan
Vasanthdev2004 Aug 14, 2026
8843c4f
test(git-remote): cover pack-POST URL splitting
Vasanthdev2004 Aug 14, 2026
a9e9c48
build: sync Cargo.lock for the git-remote-gitlawb dependencies
Vasanthdev2004 Aug 14, 2026
f455726
fix(core,gl,git-remote): close two review findings on delegated push
Vasanthdev2004 Aug 14, 2026
a2502fc
fix(core,node,gl,git-remote): close the second review round on delega…
Vasanthdev2004 Aug 14, 2026
d503655
fix(gl): restrict the delegations directory, and state the contract
Vasanthdev2004 Aug 14, 2026
5071524
fix(gl,git-remote): make a delegation issued the documented way actua…
Vasanthdev2004 Aug 15, 2026
5dfd8c5
fix(gl): refuse a relative or non-UTF-8 GITLAWB_KEY when deriving the…
Vasanthdev2004 Aug 15, 2026
e15fa73
fix(identity): one resolver for GITLAWB_KEY across gl and the helper
Vasanthdev2004 Aug 16, 2026
05fc52d
test(git-remote): cover delegation_header end to end
Vasanthdev2004 Aug 16, 2026
75aa1a0
fix(gl): finish the identity-path sweep on the write side
Vasanthdev2004 Aug 16, 2026
fad75a6
fix(gl,core): close round five — lazy home, envelope reads, honest re…
Vasanthdev2004 Aug 18, 2026
82f2484
fix(node,gl,git-remote): enforce repo narrowing at the node, honour t…
Vasanthdev2004 Aug 22, 2026
a47ea93
fix(gl): align the MCP ucan_show tool with gl ucan show
Vasanthdev2004 Aug 22, 2026
ec4399c
fix(gl): drop a needless borrow in quickstart's unix key write
Vasanthdev2004 Aug 22, 2026
1ab55e5
fix(node,gl,git-remote): fix a delegation's scope at issuance
Vasanthdev2004 Aug 22, 2026
89fa647
Merge origin/main into feat/ucan-push-authorization
Vasanthdev2004 Aug 27, 2026
9cf48f5
fix(gl): drop an unused import in the unix-only refresh tests
Vasanthdev2004 Aug 27, 2026
04e7941
fix(node): pass the UCAN extension at the last two receive-pack call …
Vasanthdev2004 Aug 27, 2026
f79309b
fix(core,node,gl,git-remote): one definition of a usable push capability
Vasanthdev2004 Sep 8, 2026
4dc62b2
fix(node): drop repo_capability_matches, orphaned by the shared push …
Vasanthdev2004 Sep 8, 2026
1da4251
fix(core,gl): bound every proof-chain walk and make verify report the…
Vasanthdev2004 Sep 8, 2026
fb831b1
docs: state the two gates a delegated push crosses, and finish the sweep
Vasanthdev2004 Sep 8, 2026
8b91f28
fix(gl): store one repository per imported delegation; pin the two-ga…
Vasanthdev2004 Sep 8, 2026
e3314c6
fix(gl,core): sanitize token-derived output, prove the publish step, …
Vasanthdev2004 Sep 8, 2026
1b5cb7d
test(gl): count the MCP tool list at 41 with ucan_import
Vasanthdev2004 Sep 8, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(gl): store one repository per imported delegation; pin the two-ga…
…te composition

Round eleven of #331, code half.

`gl ucan import` validated every capability up front and then wrote one
file per repository, and `write_private_file` is atomic per file, not
across files: a token naming two repositories could publish the first
delegation, fail on the second, and report failure for both. `gl ucan
delegate` and the MCP tool issue one capability per token, so such a
token is hand-built; import now refuses it with the supported shape in
the message. Several capabilities on one repository are still one file.

`delegated_push_on_a_private_repo_needs_read_visibility_too` pins the
composition the docs now describe, through the middleware production
gives each route: on a private repository a valid delegation is refused
at the `info/refs` advertisement (404) until the owner names the agent
as a reader, clears the receive-pack POST on its own, and a reader with
no delegation is still refused on the push. Making the visibility check
admit everyone reddens the advertisement assertion.
  • Loading branch information
Vasanthdev2004 committed Sep 8, 2026
commit 8b91f2841afe5acb71011738af33c92b1c795a2a
191 changes: 191 additions & 0 deletions crates/gitlawb-node/src/test_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2546,6 +2546,197 @@ mod tests {
);
}

/// The two-gate contract on a PRIVATE repository, end to end.
///
/// A `git push` crosses two independent gates. Gate 1 is read visibility on
/// the `info/refs` advertisement (both services): the owner, anyone on a public
/// repository, or a caller a rule names in `reader_dids`. Gate 2 is push
/// authorization on the `git-receive-pack` POST, the owner gate a delegation
/// clears. A UCAN speaks only to gate 2 and grants no read, so a delegate on a
/// private repository needs BOTH a push delegation AND a reader rule. That
/// composition — not a POST defect — is what "delegated push does not work on
/// my private repo" turns out to be; the sibling test above runs on a public
/// repository and never meets gate 1.
///
/// Status codes discriminate as in the sibling: 404 is gate 1 withholding the
/// advertisement, 403 is gate 2 refusing the push, 500 is a request that cleared
/// every gate and reached git on a repo with no disk backing.
///
/// Each gate is mounted under the middleware production gives it (`server.rs`):
/// `optional_signature` alone on `info/refs`, `require_signature` plus
/// `require_ucan_chain` on the receive-pack POST.
#[sqlx::test]
async fn delegated_push_on_a_private_repo_needs_read_visibility_too(pool: PgPool) {
use gitlawb_core::http_sig::sign_request;
use gitlawb_core::identity::Keypair;
use gitlawb_core::ucan::{caps, Capability, Ucan};
use std::sync::Arc;

let owner = Keypair::generate();
let agent = Keypair::generate();
let owner_did = owner.did().to_string();
let short = owner_did.split(':').next_back().unwrap().to_string();

let mut state = test_state(pool).await;
let mut cfg = (*state.config).clone();
cfg.enforce_owner_push = true;
state.config = Arc::new(cfg);

let mut repo = seed_repo(&owner_did, "priv-deleg");
repo.is_public = false;
state
.db
.create_repo(&repo)
.await
.expect("seed private repo");

let advertisement = || {
Router::new()
.route(
"/{owner}/{repo}/info/refs",
axum::routing::get(crate::api::repos::git_info_refs),
)
.layer(axum::middleware::from_fn(crate::auth::optional_signature))
.with_state(state.clone())
};
let receive_pack = || {
Router::new()
.route(
"/{owner}/{repo}/git-receive-pack",
axum::routing::post(crate::api::repos::git_receive_pack),
)
.layer(axum::middleware::from_fn_with_state(
state.clone(),
crate::auth::require_ucan_chain,
))
.layer(axum::middleware::from_fn(crate::auth::require_signature))
.with_state(state.clone())
};

// A delegation that is valid in every respect the push gate checks.
let hour = chrono::Utc::now() + chrono::Duration::hours(1);
let delegation = Ucan::issue(
&owner,
agent.did(),
vec![Capability::new(
format!("gitlawb://repos/{owner_did}/priv-deleg"),
caps::GIT_PUSH,
)],
Some(hour),
)
.expect("issue delegation");
let invocation = Ucan::delegate(
&agent,
state.node_did.clone(),
delegation.payload.att.clone(),
Some(hour),
&delegation,
)
.expect("wrap invocation")
.encode()
.expect("encode invocation");

let advert_path = format!("/{short}/priv-deleg.git/info/refs?service=git-receive-pack");
let signed_advert = |ucan: Option<&str>| {
let signed = sign_request(&agent, "GET", &advert_path, b"");
let mut req = Request::builder()
.method(Method::GET)
.uri(&advert_path)
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature);
if let Some(token) = ucan {
req = req.header("x-ucan", token);
}
req.body(Body::empty()).expect("request")
};
let push_path = format!("/{short}/priv-deleg.git/git-receive-pack");
let push_body = b"0000".to_vec();
let signed_push = |ucan: Option<&str>| {
let signed = sign_request(&agent, "POST", &push_path, &push_body);
let mut req = Request::builder()
.method(Method::POST)
.uri(&push_path)
.header("content-type", "application/x-git-receive-pack-request")
.header("content-digest", signed.content_digest)
.header("signature-input", signed.signature_input)
.header("signature", signed.signature);
if let Some(token) = ucan {
req = req.header("x-ucan", token);
}
req.body(Body::from(push_body.clone())).expect("request")
};

// 1. Delegation, no reader rule. Gate 1 withholds the advertisement even
// though the delegate presents the token there too: a UCAN grants no read.
let resp = advertisement()
.oneshot(signed_advert(Some(&invocation)))
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::NOT_FOUND,
"a push delegation must not open the advertisement of a private repo"
);
// Gate 2, taken on its own, is satisfied by the same delegation: the two
// gates are independent, which is exactly why both must be arranged.
let resp = receive_pack()
.oneshot(signed_push(Some(&invocation)))
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::INTERNAL_SERVER_ERROR,
"the push gate must clear on the delegation alone and reach git"
);

// 2. The owner makes the agent a reader of the whole repository — what
// `gl visibility set / --repo <repo> --readers <agent>` does.
state
.db
.set_visibility_rule(
&repo.id,
"/",
crate::db::VisibilityMode::B,
&[agent.did().to_string()],
&owner_did,
)
.await
.expect("reader rule");
let resp = advertisement()
.oneshot(signed_advert(Some(&invocation)))
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::INTERNAL_SERVER_ERROR,
"a reader clears the advertisement gate and reaches git"
);
let resp = receive_pack()
.oneshot(signed_push(Some(&invocation)))
.await
.unwrap();
assert_eq!(
resp.status(),
StatusCode::INTERNAL_SERVER_ERROR,
"with both gates arranged the push reaches git"
);

// 3. Reader rule, no delegation: reading is not pushing.
let resp = advertisement().oneshot(signed_advert(None)).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::INTERNAL_SERVER_ERROR,
"the reader rule alone still opens the advertisement"
);
let resp = receive_pack().oneshot(signed_push(None)).await.unwrap();
assert_eq!(
resp.status(),
StatusCode::FORBIDDEN,
"a reader with no delegation must still be refused on the push"
);
}

/// A valid delegation clears the owner gate but is still refused on a branch
/// the owner has explicitly protected.
///
Expand Down
101 changes: 101 additions & 0 deletions crates/gl/src/ucan_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,28 @@ async fn cmd_import(token: String, dir: Option<PathBuf>) -> Result<()> {
);
}

// One repository per token. `gl ucan delegate` and the MCP tool issue one
// capability per token, so anything naming several repositories was built by
// hand. The store publishes one file per repository and `write_private_file`
// is atomic per file, not across files: a failure on the second write would
// leave the first published while the command reports failure. Refusing is
// honest where a half-applied import is not. Several capabilities on the SAME
// repository (`git/push` plus `repo/admin`, say) are one file and are fine.
push_caps.sort();
push_caps.dedup();
if push_caps.len() > 1 {
anyhow::bail!(
"this delegation names {} repositories ({}), and `gl ucan import` stores one \
repository per token. Ask the owner for one delegation per repository.",
push_caps.len(),
push_caps
.iter()
.map(|(owner, repo)| format!("{owner}/{repo}"))
.collect::<Vec<_>>()
.join(", ")
);
}

// The identity directory is a private-data contract, not a public one: it
// already holds `identity.pem`, whose disclosure is strictly worse than a
// delegation's. `create_private_dir` and `write_private_file` below carry the
Expand Down Expand Up @@ -1551,6 +1573,85 @@ mod chain_scope_tests {
use super::delegation_store_tests::{owned_token, seed_identity};
use super::*;

/// A hand-built token naming two repositories is refused before anything is
/// written. `write_private_file` is atomic per file, not across files, so
/// applying such a token could publish one repository's delegation and then
/// fail on the other while reporting failure for both.
#[tokio::test]
async fn import_refuses_a_token_naming_two_repositories() {
let dir = tempfile::tempdir().unwrap();
let me = seed_identity(dir.path());
let owner = gitlawb_core::identity::Keypair::generate();
let bare = owner
.did()
.to_string()
.strip_prefix("did:key:")
.unwrap()
.to_string();
let token = Ucan::issue(
&owner,
me.did(),
vec![
Capability::new(format!("gitlawb://repos/{bare}/first"), caps::GIT_PUSH),
Capability::new(format!("gitlawb://repos/{bare}/second"), caps::GIT_PUSH),
],
Some(chrono::Utc::now() + chrono::Duration::hours(1)),
)
.unwrap()
.encode()
.unwrap();

let err = cmd_import(token, Some(dir.path().to_path_buf()))
.await
.expect_err("two repositories in one token must be refused");
assert!(
err.to_string().contains("one repository per token"),
"the refusal must say what the supported shape is: {err}"
);
for repo in ["first", "second"] {
assert!(
!delegation_path(dir.path(), &bare, repo).exists(),
"nothing may be written for a refused import ({repo})"
);
}
}

/// The refusal is about distinct repositories, not distinct capabilities: two
/// push-class actions on one repository are one stored file.
#[tokio::test]
async fn import_accepts_two_capabilities_on_one_repository() {
let dir = tempfile::tempdir().unwrap();
let me = seed_identity(dir.path());
let owner = gitlawb_core::identity::Keypair::generate();
let bare = owner
.did()
.to_string()
.strip_prefix("did:key:")
.unwrap()
.to_string();
let resource = format!("gitlawb://repos/{bare}/only");
let token = Ucan::issue(
&owner,
me.did(),
vec![
Capability::new(&resource, caps::GIT_PUSH),
Capability::new(&resource, caps::REPO_ADMIN),
],
Some(chrono::Utc::now() + chrono::Duration::hours(1)),
)
.unwrap()
.encode()
.unwrap();

cmd_import(token.clone(), Some(dir.path().to_path_buf()))
.await
.expect("two capabilities on one repository are one delegation");
assert_eq!(
std::fs::read_to_string(delegation_path(dir.path(), &bare, "only")).unwrap(),
token
);
}

/// The round-9 P2: a leaf that names the repository sitting on a proof that is
/// `*`. Attenuation accepts it, the root matches, and the leaf-only check that
/// import used to apply stored it — after which the node's full-chain walk
Expand Down
Loading