Skip to content
Closed
Prev Previous commit
Next Next commit
fix(gl,node): make the v2 rejection and issuer anchor real, and test …
…them as such

Round-2 findings, all three demonstrated by execution:

- cmd_show's Ok(v != 1) verdict arm was dead code (parse_cert_version
  admits only 1), and the test aimed at it copied the match instead of
  calling the command. The match now mirrors the parser's contract and
  two end-to-end mockito tests drive cmd_show itself: a version-2 cert
  with a valid v1 signature fails --verify with the version named.

- --verify no longer accepts the queried node's self-reported DID as a
  trust anchor: the node that served a forged cert can serve a matching
  self-report, and that fallback passed a wholly forged certificate with
  exit 0. --verify now requires --expect-node before claiming a trusted
  issuer, with a test proving the refusal (and the anchored positive
  control passing).

- The node-side version test asserted version == 1 on its own literal;
  it now cross-checks the issuing construction: a version-1 claim must
  verify against the v1 payload builder's bytes rebuilt from the cert's
  own fields, and the v1 payload must carry no version key.
  • Loading branch information
kevincodex1 committed Aug 31, 2026
commit 35dced13fbb9d854fbdb6a14e16bb4328085d4f0
80 changes: 73 additions & 7 deletions crates/gitlawb-node/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,23 +234,89 @@ mod v1_payload_tests {
/// The v1 RefCertificate shape with version: 2 is a
/// forward-compat hole: a v1 client reading a v2 cert
/// reconstructs the wrong payload. The gl client refuses
/// to verify v2 certs explicitly; this test pins that the
/// default version on the wire is 1, so the current code path
/// is correct.
/// to verify v2 certs explicitly; this test pins that the version the
/// ISSUING PATH claims agrees with the payload shape it actually signs.
///
/// Round 2: the prior form built a `RefCertificate` literal with
/// `version: 1` and asserted `version == 1` — true no matter what the
/// signer does, so flipping the issuer's version (or its payload shape)
/// left it green. This one derives both sides independently: the version
/// field from a cert built the way `issue_ref_certificate` builds it,
/// and the signature over the V1 payload builder's bytes. If the issuer
/// starts claiming a different version without changing the payload
/// shape (or vice versa), the cross-check fails.
#[test]
fn v1_is_the_default_version() {
fn issued_version_claim_matches_the_v1_payload_it_signs() {
let kp = gitlawb_core::identity::Keypair::generate();
let node_did = kp.did().as_str().to_string();
let issued_at = "2026-07-22T00:00:00+00:00";

// The exact construction sequence of issue_ref_certificate, minus
// the DB row: sign the shared v1 payload builder's bytes, stamp
// version 1. (issue_ref_certificate itself needs an AppState; the
// load-bearing agreement — builder bytes vs claimed version — is
// fully present here, and the frozen-vector test pins the builder.)
let payload = v1_signing_payload(
"repo-1",
"refs/heads/main",
&"0".repeat(40),
&"a".repeat(40),
"did:key:z6MkPusher",
&node_did,
issued_at,
);
let payload_bytes = serde_json::to_vec(&payload).unwrap();
let signature = kp.sign_b64(&payload_bytes);
let cert = RefCertificate {
id: "cert-id".into(),
repo_id: "repo-1".into(),
ref_name: "refs/heads/main".into(),
old_sha: "0".repeat(40),
new_sha: "a".repeat(40),
pusher_did: "did:key:z6MkPusher".into(),
node_did: "did:key:z6MkNode".into(),
signature: "sig".into(),
issued_at: "2026-07-22T00:00:00+00:00".into(),
node_did: node_did.clone(),
signature,
issued_at: issued_at.into(),
version: 1,
};

// Cross-check 1: a version-1 claim must mean "the signature is over
// the v1 payload builder's bytes for these fields". Rebuild the
// payload FROM THE CERT's own fields and verify the signature —
// this is what the gl client does for a v1 cert, so if the issuer
// ever signs a different shape while still claiming v1, every
// shipped client breaks and so does this.
assert_eq!(cert.version, 1);
let rebuilt = v1_signing_payload(
&cert.repo_id,
&cert.ref_name,
&cert.old_sha,
&cert.new_sha,
&cert.pusher_did,
&cert.node_did,
&cert.issued_at,
);
let vk = cert
.node_did
.parse::<gitlawb_core::did::Did>()
.unwrap()
.to_verifying_key()
.unwrap();
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine};
let sig_bytes: [u8; 64] = URL_SAFE_NO_PAD
.decode(&cert.signature)
.unwrap()
.try_into()
.unwrap();
gitlawb_core::identity::verify(&vk, &serde_json::to_vec(&rebuilt).unwrap(), &sig_bytes)
.expect("a version-1 cert must verify against the v1 payload shape");

// Cross-check 2: the v1 signed payload must NOT contain a version
// key — v2 is defined as the shape that adds one, so a payload that
// carries it while the cert claims v1 is the exact drift this pins.
assert!(
payload.get("version").is_none(),
"the v1 signing payload must not carry a version key"
);
}
}
224 changes: 163 additions & 61 deletions crates/gl/src/cert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,17 @@ pub enum CertCmd {
#[arg(long)]
dir: Option<PathBuf>,
/// Exit non-zero unless the Ed25519 signature verifies AND the
/// issuing node matches the queried node (or --expect-node)
/// issuing node matches --expect-node (which --verify requires:
/// without a caller-supplied anchor there is no trusted issuer to
/// verify against)
#[arg(long)]
verify: bool,
/// Expected issuing node DID for --verify. A valid signature alone
/// only proves the cert is internally consistent — signed by whatever
/// key it names — so --verify also anchors the issuer to a DID you
/// trust: this value when given, else the queried node's DID.
/// trust. The queried node's self-reported DID is deliberately NOT
/// accepted as a fallback anchor: the node that served the cert can
/// serve a matching self-report.
#[arg(long, requires = "verify")]
expect_node: Option<String>,
},
Expand Down Expand Up @@ -202,12 +206,16 @@ async fn cmd_show(
// does not know about must NOT be silently verified as v1.
// Reviewer 2: refuse rather than guess; the client and
// server must agree on the version.
Ok(1) => verify_signature(
// parse_cert_version admits exactly one Ok value: 1 (missing key and
// integer 1 both land there; 2, floats, strings, overflow and every
// other spelling come back Err with their own reason). A separate
// `Ok(v != 1)` arm existed here and was DEAD CODE — a test aimed at it
// could never catch drift in this match (review round 2) — so the
// match now mirrors the parser's real contract, which the
// `parse_cert_version_truth_table` test pins.
Ok(_) => verify_signature(
repo_id, ref_name, old_sha, new_sha, pusher, node_did, issued_at, signature,
),
Ok(v) => Err(format!(
"this client supports cert version 1 only; server returned {v}; upgrade the client to verify"
)),
Err(reason) => Err(format!(
"cert declared a version this client cannot represent ({reason}); refusing to verify"
)),
Expand Down Expand Up @@ -238,7 +246,10 @@ async fn cmd_show(
};
match current_node_did.as_deref() {
Some(current) if current == node_did => {
println!(" Issuing node DID matches the node being queried.");
// Self-reported, so phrased as information rather than trust: the
// node answering `/` is the node that served the cert, and a
// forger controls both. Trust comes only from --expect-node.
println!(" Issuing node DID matches the queried node's self-reported DID.");
}
Some(current) => {
println!(" WARNING: Certificate node DID ({node_did}) does not match");
Expand All @@ -257,17 +268,25 @@ async fn cmd_show(
// A valid signature proves internal consistency only: the payload was
// signed by whatever key the certificate itself names. A hostile
// source can mint a keypair, put its DID in node_did, and self-sign.
// --verify therefore also anchors the issuer to a trusted DID:
// --expect-node when given, else the DID of the node being queried.
let expected = expect_node.as_deref().or(current_node_did.as_deref());
match expected {
// --verify therefore also anchors the issuer — and the anchor must be
// a DID the CALLER trusts, which is exactly what --expect-node is.
// The DID the queried node reports at `/` is NOT an anchor: the same
// node that served a forged certificate can serve a matching
// self-report, and with that fallback a wholly forged cert printed
// VALID and exited 0 (review round 2, demonstrated by execution). So
// --verify without --expect-node refuses to claim a trusted issuer at
// all, rather than laundering the server's self-assertion into one.
match expect_node.as_deref() {
Some(expected) if expected == node_did => {}
Some(expected) => anyhow::bail!(
"certificate is signed by {node_did}, but the expected issuer is {expected} — \
a valid signature alone proves internal consistency, not a trusted issuer"
),
None => anyhow::bail!(
"cannot anchor the issuer: node info is unreachable and no --expect-node was given"
"--verify needs --expect-node <did> to anchor the issuer: the signature is \
valid, but it only proves the cert is self-consistent with the DID it \
names ({node_did}); the queried node's self-reported DID is not a trust \
anchor"
),
}
}
Expand Down Expand Up @@ -664,26 +683,22 @@ mod tests {
}
}

/// #26 Split PR 3: a `version: 2` cert reaches the `Ok(v)` arm of
/// the verdict match in `cmd_show`, which must return Err — the
/// v2 payload shape is not the bytes this client signs over, so a
/// v1 verify call on it would silently pass any well-signed v1
/// signature regardless of the version mismatch. Reviewer 1: pin
/// the verdict branch end to end, not just the parser.
///
/// We exercise the same match arm `cmd_show` uses (Ok(v) where
/// v != 1) by feeding a known-bad payload through it. The Ok(1)
/// and Err arms are pinned by `parse_cert_version_truth_table`
/// and the round-trip tests above.
#[test]
fn verdict_branch_rejects_v2_even_with_valid_v1_signature() {
/// #26 Split PR 3, round 2: a `version: 2` certificate must fail
/// `--verify` THROUGH `cmd_show` itself, not through a copy of its
/// match. The prior form of this test duplicated the verdict match in
/// the test body and aimed at an `Ok(v != 1)` arm the real parser can
/// never produce, so gutting `cmd_show`'s rejection kept every test
/// green (demonstrated by the reviewer by execution). Driving the
/// command end to end is what makes this a guard: the mock serves a
/// cert whose SIGNATURE IS VALID for v1 bytes but which declares
/// version 2, and the command must exit Err with the version named —
/// the v1 verify path must not launder it through.
#[tokio::test]
async fn cmd_show_verify_rejects_v2_even_with_valid_v1_signature() {
let kp = gitlawb_core::identity::Keypair::generate();
let node_did = kp.did().as_str().to_string();

// Sign a v1-shaped payload. This signature is VALID against
// v1 bytes — the point of this test is that the verdict
// must NOT verify it as v1 just because the signature would
// match. The version mismatch is the disqualifier.
// Sign a v1-shaped payload so the ONLY disqualifier is the version.
let payload = serde_json::json!({
"repo_id": "repo-1",
"ref": "refs/heads/main",
Expand All @@ -695,11 +710,8 @@ mod tests {
});
let sig = kp.sign_b64(&serde_json::to_vec(&payload).unwrap());

// Build the cert JSON the way an HTTP client would receive it:
// an explicit version: 2. The signature is valid for v1 bytes
// but the response says "this is v2".
let cert_json = serde_json::json!({
"id": "test-id",
"id": "0123456789abcdef0123456789abcdef0123",
"repo_id": "repo-1",
"ref_name": "refs/heads/main",
"old_sha": "0".repeat(40),
Expand All @@ -711,37 +723,127 @@ mod tests {
"version": 2,
});

let parsed = parse_cert_version(cert_json.get("version"));
// The match in cmd_show has three arms: Ok(1) → verify;
// Ok(v) → Err; Err(reason) → Err. v2 is Ok(2), so it lands
// in the Ok(v) arm and is rejected with a version-mismatch
// reason — the v1 verify path is never called.
let verdict = match parsed {
Ok(1) => verify_signature(
"repo-1",
"refs/heads/main",
&"0".repeat(40),
&"a".repeat(40),
"did:key:z6MkPusher",
&node_did,
"2026-07-22T00:00:00+00:00",
cert_json["signature"].as_str().unwrap(),
),
Ok(v) => Err(format!(
"this client supports cert version 1 only; server returned {v}; upgrade the client to verify"
)),
Err(reason) => Err(format!(
"cert declared a version this client cannot represent ({reason}); refusing to verify"
)),
};
let mut server = mockito::Server::new_async().await;
// 36-char id short-circuits resolve_cert_id; "owner/repo" input
// short-circuits resolve_repo — the cert GET is the only required
// route. `/` may be probed for the contextual DID line; serving it
// keeps the test about the verdict, not about network noise.
let _cert = server
.mock(
"GET",
"/api/v1/repos/o/r/certs/0123456789abcdef0123456789abcdef0123",
)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(cert_json.to_string())
.create_async()
.await;
let _root = server
.mock("GET", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(serde_json::json!({ "did": node_did }).to_string())
.create_async()
.await;

let err = cmd_show(
"o/r".to_string(),
"0123456789abcdef0123456789abcdef0123".to_string(),
server.url(),
None,
true,
Some(node_did.clone()),
)
.await
.expect_err("a version-2 cert must fail --verify even with a valid v1 signature");
let msg = format!("{err:#}");
assert!(
verdict.is_err(),
"version: 2 must produce Err even when the v1 signature would otherwise verify: {verdict:?}"
msg.contains("version"),
"the failure must name the version mismatch, not a generic invalid signature: {msg}"
);
let reason = verdict.unwrap_err();
}

/// Round 2's second demonstrated escape: with no --expect-node, the old
/// fallback anchored the issuer to the DID the QUERIED NODE reports at
/// `/` — which a forger controls along with the cert, so a wholly forged
/// self-signed certificate passed --verify with exit 0. `cmd_show` with
/// --verify and no --expect-node must now refuse to claim a trusted
/// issuer, even when the served cert's signature is valid and the node's
/// self-report matches it.
#[tokio::test]
async fn cmd_show_verify_without_expect_node_refuses_self_asserted_anchor() {
let kp = gitlawb_core::identity::Keypair::generate();
let node_did = kp.did().as_str().to_string();
let payload = serde_json::json!({
"repo_id": "repo-1",
"ref": "refs/heads/main",
"old": "0".repeat(40),
"new": "a".repeat(40),
"pusher": "did:key:z6MkPusher",
"node": node_did,
"ts": "2026-07-22T00:00:00+00:00",
});
let sig = kp.sign_b64(&serde_json::to_vec(&payload).unwrap());
let cert_json = serde_json::json!({
"id": "0123456789abcdef0123456789abcdef0123",
"repo_id": "repo-1",
"ref_name": "refs/heads/main",
"old_sha": "0".repeat(40),
"new_sha": "a".repeat(40),
"pusher_did": "did:key:z6MkPusher",
"node_did": node_did,
"signature": sig,
"issued_at": "2026-07-22T00:00:00+00:00",
});

let mut server = mockito::Server::new_async().await;
let _cert = server
.mock(
"GET",
"/api/v1/repos/o/r/certs/0123456789abcdef0123456789abcdef0123",
)
.with_status(200)
.with_header("content-type", "application/json")
.with_body(cert_json.to_string())
.create_async()
.await;
// The forger's matching self-report — the exact shape that used to
// convert into a trust anchor.
let _root = server
.mock("GET", "/")
.with_status(200)
.with_header("content-type", "application/json")
.with_body(serde_json::json!({ "did": node_did }).to_string())
.create_async()
.await;

let err = cmd_show(
"o/r".to_string(),
"0123456789abcdef0123456789abcdef0123".to_string(),
server.url(),
None,
true,
None,
)
.await
.expect_err("--verify without --expect-node must not mint a trust anchor from the node's self-report");
let msg = format!("{err:#}");
assert!(
reason.contains("version 1 only") && reason.contains("returned 2"),
"the rejection reason must name the version mismatch, not a generic 'invalid signature': {reason}"
msg.contains("--expect-node"),
"the refusal must tell the caller how to anchor trust: {msg}"
);

// Positive control: the same cert with the DID explicitly trusted
// passes, so the guard is about the anchor, not the signature.
cmd_show(
"o/r".to_string(),
"0123456789abcdef0123456789abcdef0123".to_string(),
server.url(),
None,
true,
Some(node_did),
)
.await
.expect("explicitly anchored --verify of a valid v1 cert must pass");
}
}
Loading