|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package main |
| 4 | + |
| 5 | +import ( |
| 6 | + "errors" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/pilot-protocol/common/badgeverify" |
| 10 | +) |
| 11 | + |
| 12 | +// TestVerifyForNodeRejectsCrossNodeBadge is the pilotctl-layer adversarial |
| 13 | +// guard for badge replay across addresses. `pilotctl verify status` decides |
| 14 | +// verified=true ONLY when badgeverify.VerifyForNode(badge, sig, nodeID) |
| 15 | +// returns nil — see cmdVerifyStatus. The registry transport is untrusted, so a |
| 16 | +// malicious registry (or a MITM) can return ANY node's badge in a lookup for |
| 17 | +// our node. The binding check inside VerifyForNode is what stops that badge |
| 18 | +// from being credited to a different address. |
| 19 | +// |
| 20 | +// We craft a canonical badge bound to NodeID A and present it for NodeID B. |
| 21 | +// VerifyForNode MUST return a non-nil error (fail-closed) — never nil. We do |
| 22 | +// not need a pinned issuer signature to prove this: a cross-node badge must be |
| 23 | +// rejected whether the rejection comes from the node-mismatch check or the |
| 24 | +// signature check, because either way the pilotctl layer must not render it as |
| 25 | +// "verified". |
| 26 | +func TestVerifyForNodeRejectsCrossNodeBadge(t *testing.T) { |
| 27 | + t.Parallel() |
| 28 | + |
| 29 | + const nodeA = uint32(0x0A0A0A) |
| 30 | + const nodeB = uint32(0x0B0B0B) |
| 31 | + |
| 32 | + badgeForA, err := badgeverify.Canonical(badgeverify.Badge{ |
| 33 | + NodeID: nodeA, |
| 34 | + Provider: "github", |
| 35 | + VerifiedAt: 1700000000, |
| 36 | + Exp: 0, |
| 37 | + Kid: "bdg-v1", |
| 38 | + Subject: "victim", |
| 39 | + }) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("Canonical: %v", err) |
| 42 | + } |
| 43 | + // An attacker-supplied signature; it cannot be a valid pinned-issuer sig |
| 44 | + // here, which is itself part of the point — the layer must fail closed. |
| 45 | + const attackerSig = "Zm9yZ2VkLXNpZ25hdHVyZQ==" |
| 46 | + |
| 47 | + // Present node A's badge while authenticated/looked-up as node B. |
| 48 | + if _, verr := badgeverify.VerifyForNode(badgeForA, attackerSig, nodeB); verr == nil { |
| 49 | + t.Fatal("VerifyForNode accepted a badge bound to a DIFFERENT node — cross-node replay not rejected") |
| 50 | + } |
| 51 | + |
| 52 | + // Sanity: presenting it for its own node A must ALSO fail closed here, |
| 53 | + // because the forged signature does not verify against the pinned issuer |
| 54 | + // key. This proves the gate is genuinely fail-closed, not merely a |
| 55 | + // node-id string compare that an attacker could satisfy. |
| 56 | + _, selfErr := badgeverify.VerifyForNode(badgeForA, attackerSig, nodeA) |
| 57 | + if selfErr == nil { |
| 58 | + t.Fatal("VerifyForNode accepted a badge with a non-pinned (forged) signature") |
| 59 | + } |
| 60 | + // The self-node failure must be a signature/key failure, NOT a node |
| 61 | + // mismatch — confirming the binding check and the crypto check are |
| 62 | + // independent gates. |
| 63 | + if errors.Is(selfErr, badgeverify.ErrNodeMismatch) { |
| 64 | + t.Fatalf("same-node badge failed with ErrNodeMismatch, want a signature failure: %v", selfErr) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// verifyStatusVerdict mirrors the exact decision cmdVerifyStatus makes: a node |
| 69 | +// is rendered "verified" iff VerifyForNode returns nil for the looked-up |
| 70 | +// nodeID. Kept as a tiny pure helper so the adversarial truth-table is testable |
| 71 | +// without standing up a registry + daemon. |
| 72 | +func verifyStatusVerdict(badge, sig string, nodeID uint32) bool { |
| 73 | + if badge == "" { |
| 74 | + return false |
| 75 | + } |
| 76 | + _, err := badgeverify.VerifyForNode(badge, sig, nodeID) |
| 77 | + return err == nil |
| 78 | +} |
| 79 | + |
| 80 | +// TestVerifyStatusVerdictCrossNodeNotVerified pins that the status verdict the |
| 81 | +// CLI prints is "not verified" for a cross-node badge, i.e. the rejection above |
| 82 | +// actually flows through to a user-visible false. |
| 83 | +func TestVerifyStatusVerdictCrossNodeNotVerified(t *testing.T) { |
| 84 | + t.Parallel() |
| 85 | + |
| 86 | + const nodeA = uint32(0x0A0A0A) |
| 87 | + const nodeB = uint32(0x0B0B0B) |
| 88 | + badgeForA, err := badgeverify.Canonical(badgeverify.Badge{ |
| 89 | + NodeID: nodeA, Provider: "github", VerifiedAt: 1700000000, Kid: "bdg-v1", |
| 90 | + }) |
| 91 | + if err != nil { |
| 92 | + t.Fatalf("Canonical: %v", err) |
| 93 | + } |
| 94 | + if verifyStatusVerdict(badgeForA, "Zm9yZ2Vk", nodeB) { |
| 95 | + t.Fatal("status verdict reported verified=true for a cross-node badge") |
| 96 | + } |
| 97 | + if verifyStatusVerdict("", "", nodeB) { |
| 98 | + t.Fatal("status verdict reported verified=true with no badge") |
| 99 | + } |
| 100 | +} |
0 commit comments