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
feat(git-remote): send a delegated push capability as X-Ucan
Wraps a stored delegation into an invocation (iss=agent, aud=node,
prf=[delegation]) and attaches it to the receive-pack POST, which is what lets a
CI or delegated key push once owner-only push is enforced. Only on the push: a
fetch is gated by read visibility, not by git/push.

Capabilities are copied from the delegation unchanged, so an invocation can
never be broader than what was delegated. No expiry is set on the invocation
itself — the delegation`s own exp still bounds the chain because verify_chain
checks each proof`s expiry as it recurses. A test proves that rather than
asserting it: an already-expired delegation still fails the chain after
wrapping, so an expired grant cannot be laundered into an open-ended one.

The whole path is best-effort. A missing delegation, an unreachable node, or an
unreadable stored token all send the request without the header. The node
decides whether one was required, and a node denial has to reach the user
instead of being pre-empted by a local guess.

delegation_path duplicates gl`s six-line version because gl is not a dependency
of this crate; both carry a pointer to the other.
  • Loading branch information
Vasanthdev2004 committed Aug 16, 2026
commit 3d81f578ee4fc269b3405bbfb6debd8f46f5f9c2
6 changes: 6 additions & 0 deletions crates/git-remote-gitlawb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@ path = "src/main.rs"
gitlawb-core = { path = "../gitlawb-core" }
anyhow = { workspace = true }
reqwest = { workspace = true }
# Reading the node's DID from `GET /` so a delegated push can address its
# invocation to the right executor.
serde_json = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

[dev-dependencies]
mockito = "1"
tempfile = "3"
# Test-only: constructing an already-expired delegation needs a timestamp. The
# helper itself never sets an expiry — the proof's own exp bounds the chain.
chrono = { workspace = true }

[target.'cfg(unix)'.dev-dependencies]
libc = "0.2"
Expand Down
193 changes: 193 additions & 0 deletions crates/git-remote-gitlawb/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,115 @@ fn build_advertisement_request(
/// Public-repo fetch still works anonymously when no keypair is present. The body
/// is signed (content-digest) but NOT attached here, so the caller can move the
/// (possibly large) pack bytes into `.body()` rather than clone them.
/// Where a delegation for `owner_did`/`repo` is stored.
///
/// Must agree with `gl`'s `ucan_cmd::delegation_path`, which writes these files.
/// `gl` is not a dependency of this crate, so the six lines are duplicated rather
/// than shared; change both together. Keyed on the bare base58 key because
/// `did:key:` contains a colon, which Windows rejects in a filename.
fn delegation_path(dir: &std::path::Path, owner_did: &str, repo: &str) -> std::path::PathBuf {
let bare = owner_did.strip_prefix("did:key:").unwrap_or(owner_did);
dir.join("delegations").join(format!("{bare}__{repo}.ucan"))
}

/// Wrap a stored delegation into an invocation addressed to the node.
///
/// `iss=agent, aud=node, prf=[delegation]` is exactly the shape the node's
/// `validate_ucan_chain` expects: it binds `iss` to the request signer and `aud`
/// to its own DID, then walks `prf` to the root.
///
/// Capabilities are copied from the delegation unchanged, so the invocation is
/// never broader than what was delegated and cannot fail attenuation. No expiry
/// is set: the delegation's own `exp` still bounds the chain, because
/// `verify_chain` checks each proof's expiry as it recurses.
fn build_invocation(
agent: &Keypair,
node_did: &gitlawb_core::did::Did,
delegation: &gitlawb_core::ucan::Ucan,
) -> Result<gitlawb_core::ucan::Ucan> {
gitlawb_core::ucan::Ucan::delegate(
agent,
node_did.clone(),
delegation.payload.att.clone(),
None,
delegation,
)
.map_err(|e| anyhow::anyhow!("failed to build UCAN invocation: {e}"))
}

/// Split a receive-pack POST URL into `(origin, owner, repo)`.
///
/// `https://node/zOwner/myrepo.git/git-receive-pack`
/// -> ("https://node", "zOwner", "myrepo")
fn split_pack_post_url(post_url: &str) -> Option<(String, String, String)> {
let path = url_path(post_url);
let origin = post_url.strip_suffix(&path)?.to_string();
let mut segs = path.trim_start_matches('/').split('/');
let owner = segs.next()?;
let repo = segs.next()?;
if owner.is_empty() || repo.is_empty() {
return None;
}
let repo = repo.strip_suffix(".git").unwrap_or(repo);
Some((origin, owner.to_string(), repo.to_string()))
}

/// Build the `X-Ucan` value for a delegated push, or `None` when this push does
/// not need one.
///
/// Entirely best-effort. A missing delegation, an unreachable node, or an
/// unreadable stored token all yield `None` and the push proceeds without the
/// header — the node decides whether one was required, and a node denial must
/// reach the user rather than being pre-empted by a local guess.
fn delegation_header(
client: &reqwest::blocking::Client,
post_url: &str,
keypair: &Keypair,
) -> Option<String> {
let (origin, owner, repo) = split_pack_post_url(post_url)?;

// The owner pushes on their own authority; no delegation is involved.
let bare = |d: &str| d.strip_prefix("did:key:").unwrap_or(d).to_string();
if bare(&keypair.did().to_string()) == bare(&owner) {
return None;
}

let dir = resolve_key_path().parent()?.to_path_buf();
let path = delegation_path(&dir, &owner, &repo);
let raw = std::fs::read_to_string(&path).ok()?;

let delegation = match gitlawb_core::ucan::Ucan::decode(raw.trim()) {
Ok(u) => u,
Err(e) => {
tracing::warn!("stored delegation at {path:?} is unreadable: {e}");
return None;
}
};

// The invocation must be addressed to the node that will execute it.
let node_did: gitlawb_core::did::Did = client
.get(&origin)
.header("User-Agent", USER_AGENT)
.send()
.ok()
.and_then(|r| r.json::<serde_json::Value>().ok())
.and_then(|v| v.get("did")?.as_str().map(str::to_owned))
.or_else(|| {
tracing::warn!("could not read the node DID from {origin}; pushing without X-Ucan");
None
})?
.parse()
.ok()?;
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

match build_invocation(keypair, &node_did, &delegation) {
Ok(inv) => inv.encode().ok(),
Err(e) => {
tracing::warn!("could not build the UCAN invocation: {e}");
None
}
}
}

fn build_pack_post_request(
client: &reqwest::blocking::Client,
post_url: &str,
Expand All @@ -376,6 +485,15 @@ fn build_pack_post_request(
.header("Signature-Input", signed.signature_input)
.header("Signature", signed.signature);
tracing::debug!("signed {service} POST (DID: {})", kp.did());

// A non-owner pushing under a delegation presents it here. Only on the
// push: a fetch is gated by read visibility, not by git/push.
if service == "git-receive-pack" {
if let Some(token) = delegation_header(client, post_url, kp) {
tracing::debug!("attaching a delegated push capability");
req = req.header("X-Ucan", token);
}
}
} else if service == "git-receive-pack" {
tracing::warn!("no identity keypair found, push will be unsigned (v0.1 local alpha only)");
}
Expand Down Expand Up @@ -2170,3 +2288,78 @@ mod tests {
);
}
}

#[cfg(test)]
mod delegated_push_tests {
use super::*;
use gitlawb_core::ucan::{caps, Capability, Ucan};

#[test]
fn invocation_wraps_the_delegation_and_targets_the_node() {
let owner = Keypair::generate();
let agent = Keypair::generate();
let node = Keypair::generate();
let delegation = Ucan::issue(
&owner,
agent.did(),
vec![Capability::new("gitlawb://repos/zowner/r", caps::GIT_PUSH)],
None,
)
.expect("issue");

let invocation = build_invocation(&agent, &node.did(), &delegation).expect("wrap");

assert_eq!(invocation.payload.iss, agent.did(), "the agent invokes");
assert_eq!(invocation.payload.aud, node.did(), "the node executes");
assert_eq!(
invocation.payload.prf.len(),
1,
"exactly one proof: chains are linear"
);
assert_eq!(
invocation.verify_chain().expect("must verify"),
owner.did(),
"the chain must still root at the owner after wrapping"
);
}

/// The invocation deliberately carries no expiry of its own. That is only safe
/// because `verify_chain` recurses into the proof and checks the delegation's
/// expiry there — so an expired delegation cannot be laundered into an
/// open-ended push capability by wrapping it.
#[test]
fn an_expired_delegation_cannot_be_laundered_by_wrapping_it() {
let owner = Keypair::generate();
let agent = Keypair::generate();
let node = Keypair::generate();
let expired = Ucan::issue(
&owner,
agent.did(),
vec![Capability::new("gitlawb://repos/zowner/r", caps::GIT_PUSH)],
Some(chrono::Utc::now() - chrono::Duration::hours(1)),
)
.expect("issue expired");

let invocation = build_invocation(&agent, &node.did(), &expired).expect("wrap");

assert!(
invocation.payload.exp.is_none(),
"the invocation sets no expiry of its own"
);
let err = invocation
.verify_chain()
.expect_err("an expired proof must fail the chain");
assert!(
err.to_string().contains("expired"),
"the failure must name expiry, got: {err}"
);
}

#[test]
fn delegation_path_matches_the_gl_layout() {
let base = std::path::Path::new("/tmp/id");
let expected = base.join("delegations").join("z6MkAbc__myrepo.ucan");
assert_eq!(delegation_path(base, "did:key:z6MkAbc", "myrepo"), expected);
assert_eq!(delegation_path(base, "z6MkAbc", "myrepo"), expected);
}
}