Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
639 changes: 412 additions & 227 deletions handshake.go

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,11 @@ type Runtime interface {
// for it and falls back to IsTrusted when the adapter does not provide
// it, so existing Runtime implementations keep compiling unchanged.
//
// pubKeyB64 is the base64 Ed25519 key the peer proved possession of on
// this handshake, or "" when no authenticated key is in scope (the
// registry-relayed path). Implementations that carry a pinned key for
// pubKeyB64 is the base64 Ed25519 key the registry confirmed for the
// peer: the key it signed a direct request with, or, for a request that
// came over the registry relay, the key a registry lookup returned. It
// is never empty: without a confirmed key no auto-accept rule runs.
// Implementations that carry a pinned key for
// the node MUST require an exact match and MUST treat "" as a miss;
// entries with no pin answer the node-ID-only question, matching
// IsTrusted.
Expand Down
16 changes: 6 additions & 10 deletions zz_ceiling_branches_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestProcessRelayedRequest_AutoApproveWithRegistryFiresRespond(t *testing.T)
rt.mu.Unlock()

rt.registry.setLookup(1, map[string]interface{}{"networks": []interface{}{}})
rt.registry.setLookup(5555, map[string]interface{}{"networks": []interface{}{}})
rt.registry.setLookup(5555, map[string]interface{}{"networks": []interface{}{}, "public_key": bindKeyA})

hm.processRelayedRequest(5555, "")

Expand Down Expand Up @@ -173,7 +173,7 @@ func TestProcessRelayedRequest_MutualWithRegistryFiresRespond(t *testing.T) {
rt.mu.Unlock()
hm.outgoing[6666] = time.Now()

rt.registry.setLookup(6666, map[string]interface{}{"networks": []interface{}{}})
rt.registry.setLookup(6666, map[string]interface{}{"networks": []interface{}{}, "public_key": bindKeyA})

hm.processRelayedRequest(6666, "")

Expand Down Expand Up @@ -276,7 +276,7 @@ func TestHandleAccept_RecentlyRevokedDrops(t *testing.T) {
Type: HandshakeAccept,
NodeID: 60,
Timestamp: time.Now().Unix(),
})
}, true)

hm.mu.RLock()
_, trusted := hm.trusted[60]
Expand Down Expand Up @@ -310,7 +310,7 @@ func TestHandleAccept_ExpiredRevokeAllowsTrustAndFiresReport(t *testing.T) {
NodeID: 70,
PublicKey: "pk70",
Timestamp: time.Now().Unix(),
})
}, true)

hm.mu.RLock()
_, trusted := hm.trusted[70]
Expand Down Expand Up @@ -549,12 +549,8 @@ func TestProcessMessage_ReplaySetFullStillDispatches(t *testing.T) {
}
hm.replayMu.Unlock()

msg := &HandshakeMsg{
Type: HandshakeReject,
NodeID: 77,
Reason: "test",
Timestamp: time.Now().Unix(),
}
msg := signedMsg(deterministicIdentity(77), HandshakeReject, 77, hm.rt.NodeID())
msg.Reason = "test"
// Seed an outgoing entry so the dispatch is observable.
hm.outgoing[77] = time.Now()
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 77}}, msg)
Expand Down
20 changes: 7 additions & 13 deletions zz_findings_hardening_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestHandleAccept_WithoutOutgoingRequestDropped(t *testing.T) {
NodeID: 4242,
PublicKey: "pk4242",
Timestamp: time.Now().Unix(),
})
}, true)

hm.mu.RLock()
_, trusted := hm.trusted[4242]
Expand Down Expand Up @@ -74,7 +74,7 @@ func TestHandleAccept_ConsumesOutgoingEntryOnce(t *testing.T) {
PublicKey: "pk4444",
Timestamp: time.Now().Unix(),
}
hm.handleAccept(msg)
hm.handleAccept(msg, true)

hm.mu.RLock()
_, trusted := hm.trusted[peer]
Expand All @@ -91,7 +91,7 @@ func TestHandleAccept_ConsumesOutgoingEntryOnce(t *testing.T) {
hm.mu.Lock()
delete(hm.trusted, peer)
hm.mu.Unlock()
hm.handleAccept(msg)
hm.handleAccept(msg, true)

hm.mu.RLock()
_, reTrusted := hm.trusted[peer]
Expand Down Expand Up @@ -165,11 +165,8 @@ func TestProcessMessage_RejectOverMatchingStreamCancels(t *testing.T) {
hm.outgoing[peer] = time.Now()
hm.mu.Unlock()

hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}}, &HandshakeMsg{
Type: HandshakeReject,
NodeID: peer,
Timestamp: time.Now().Unix(),
})
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}},
signedMsg(deterministicIdentity(72), HandshakeReject, peer, hm.rt.NodeID()))

hm.mu.RLock()
_, stillOutgoing := hm.outgoing[peer]
Expand Down Expand Up @@ -212,11 +209,8 @@ func TestProcessMessage_UnverifiedMessageDoesNotConsumeReplayCapacity(t *testing
hm.mu.Lock()
hm.outgoing[901] = time.Now()
hm.mu.Unlock()
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 901}}, &HandshakeMsg{
Type: HandshakeReject,
NodeID: 901,
Timestamp: time.Now().Unix(),
})
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 901}},
signedMsg(deterministicIdentity(91), HandshakeReject, 901, hm.rt.NodeID()))

hm.replayMu.Lock()
size = len(hm.replaySet)
Expand Down
31 changes: 14 additions & 17 deletions zz_handshake_accept_request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func TestHandleAcceptAddsPeerToTrustedAndRemovesOutgoing(t *testing.T) {
PublicKey: "some-pubkey",
Timestamp: time.Now().Unix(),
}
hm.handleAccept(msg)
hm.handleAccept(msg, true)

if _, ok := hm.outgoing[77]; ok {
t.Fatal("outgoing[77] should have been cleared after accept")
Expand Down Expand Up @@ -93,8 +93,9 @@ func TestHandleRequestAlreadyTrustedSendsAcceptWithoutDuplicating(t *testing.T)
}

// A request for an already-trusted node ID that carries a different key
// is a different key holder behind a recycled node ID. The record is
// dropped and the request falls through to the normal approval flow.
// the registry confirmed is a different key holder behind a recycled
// node ID. The record is dropped and the request falls through to the
// normal approval flow.
func TestHandleRequestTrustedNodeWithDifferentKeyDropsRecord(t *testing.T) {
t.Parallel()
hm, _ := hsTestManager(t, false)
Expand All @@ -112,7 +113,7 @@ func TestHandleRequestTrustedNodeWithDifferentKeyDropsRecord(t *testing.T) {
PublicKey: "reclaimed-key",
Timestamp: time.Now().Unix(),
}
hm.handleRequest(nil, msg, false)
hm.handleRequest(nil, msg, true) // the registry lists reclaimed-key for 42

hm.mu.RLock()
_, stillTrusted := hm.trusted[42]
Expand All @@ -128,9 +129,9 @@ func TestHandleRequestTrustedNodeWithDifferentKeyDropsRecord(t *testing.T) {
}

// A record persisted before keys were bound (or one whose registry
// backfill never landed) keeps working, and adopts the first key it
// sees.
func TestHandleRequestUnboundLegacyRecordAdoptsKey(t *testing.T) {
// backfill never landed) keeps working, and takes the key of a request
// the registry confirmed.
func TestHandleRequestUnboundLegacyRecordAdoptsConfirmedKey(t *testing.T) {
t.Parallel()
hm, _ := hsTestManager(t, false)
defer waitForGoRPCDrain()
Expand All @@ -146,7 +147,7 @@ func TestHandleRequestUnboundLegacyRecordAdoptsKey(t *testing.T) {
PublicKey: "first-seen-key",
Timestamp: time.Now().Unix(),
}
hm.handleRequest(nil, msg, false)
hm.handleRequest(nil, msg, true)

hm.mu.RLock()
rec := hm.trusted[42]
Expand Down Expand Up @@ -208,7 +209,7 @@ func TestHandleRequestAutoApproveWhenTrustAutoApproveEnabled(t *testing.T) {
PublicKey: "key-55",
Timestamp: time.Now().Unix(),
}
hm.handleRequest(nil, msg, false)
hm.handleRequest(nil, msg, true) // the registry confirmed key-55

hm.mu.RLock()
rec, ok := hm.trusted[55]
Expand Down Expand Up @@ -371,18 +372,14 @@ func TestProcessMessageDispatchesHandshakeAccept(t *testing.T) {
hm := newTestHM(t, "")
t.Cleanup(hm.Stop)

// Omit PublicKey so processMessage skips the signature-required check
// and dispatches through the Type switch to handleAccept. The accept
// completes an outgoing request, so one has to be in flight.
// A signed accept dispatches through the Type switch to
// handleAccept. The accept completes an outgoing request, so one has
// to be in flight.
hm.mu.Lock()
hm.outgoing[44] = time.Now()
hm.mu.Unlock()

msg := &HandshakeMsg{
Type: HandshakeAccept,
NodeID: 44,
Timestamp: time.Now().Unix(),
}
msg := signedMsg(deterministicIdentity(44), HandshakeAccept, 44, hm.rt.NodeID())
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 44}}, msg)

hm.mu.RLock()
Expand Down
24 changes: 6 additions & 18 deletions zz_handshake_connection_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,9 @@ func TestHandleConnectionValidMsgDispatchesToProcessMessage(t *testing.T) {
t.Cleanup(hm.Stop)
hm.outgoing[321] = time.Now()

msg := HandshakeMsg{
Type: HandshakeReject,
NodeID: 321,
Reason: "nope",
Timestamp: time.Now().Unix(),
}
raw, _ := json.Marshal(&msg)
msg := signedMsg(deterministicIdentity(32), HandshakeReject, 321, hm.rt.NodeID())
msg.Reason = "nope"
raw, _ := json.Marshal(msg)

stream := newMockStreamFrom(321, raw)

Expand Down Expand Up @@ -190,12 +186,8 @@ func TestProcessMessageReplayCaughtOnSecondDispatch(t *testing.T) {
t.Cleanup(hm.Stop)
hm.outgoing[33] = time.Now()

msg := &HandshakeMsg{
Type: HandshakeReject,
NodeID: 33,
Reason: "replay-test",
Timestamp: time.Now().Unix(),
}
msg := signedMsg(deterministicIdentity(33), HandshakeReject, 33, hm.rt.NodeID())
msg.Reason = "replay-test"

stream := &addrStream{addr: coreapi.Addr{Node: 33}}

Expand Down Expand Up @@ -331,11 +323,7 @@ func TestProcessMessageRevokeDispatchRemovesTrusted(t *testing.T) {
t.Cleanup(hm.Stop)
hm.trusted[99] = &TrustRecord{NodeID: 99}

msg := &HandshakeMsg{
Type: HandshakeRevoke,
NodeID: 99,
Timestamp: time.Now().Unix(),
}
msg := signedMsg(deterministicIdentity(99), HandshakeRevoke, 99, hm.rt.NodeID())
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 99}}, msg)

hm.mu.RLock()
Expand Down
1 change: 1 addition & 0 deletions zz_handshake_registry_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func TestApproveHandshakeMovesPendingToTrustedAndEmitsWebhook(t *testing.T) {
hm.pending[77] = &PendingHandshake{
NodeID: 77,
PublicKey: "pending-pubkey",
KeyConfirmed: true, // the registry listed this key when the request came in
Justification: "please",
ReceivedAt: time.Now(),
}
Expand Down
25 changes: 14 additions & 11 deletions zz_handshake_relayed_trust_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ func TestProcessRelayedRequestAlreadyTrustedShortCircuits(t *testing.T) {

func TestProcessRelayedRequestMutualAutoApproves(t *testing.T) {
t.Parallel()
hm := newTestHM(t, "")
t.Cleanup(hm.Stop)
hm, rt := newFakeHM(t)
rt.registry.setLookup(99, map[string]interface{}{"public_key": bindKeyA})
hm.mu.Lock()
hm.outgoing[99] = time.Now()
hm.mu.Unlock()

hm.processRelayedRequest(99, "")

Expand All @@ -64,20 +66,19 @@ func TestProcessRelayedRequestMutualAutoApproves(t *testing.T) {
if stillOutgoing {
t.Fatal("outgoing[99] should have been cleared")
}
// PublicKey is NOT populated by the relay path (comes from backfillPeerKey goRPC).
if rec.PublicKey != "" {
t.Fatalf("PublicKey should be empty on relay mutual path (got %q)", rec.PublicKey)
// The relay carries no key: the record is bound to the registry's.
if rec.PublicKey != bindKeyA {
t.Fatalf("PublicKey = %q, want the registry's key", rec.PublicKey)
}
}

// --- processRelayedRequest: TrustAutoApprove ---

func TestProcessRelayedRequestAutoApproveMarksNonMutual(t *testing.T) {
t.Parallel()
rt := newTestRuntime()
hm, rt := newFakeHM(t)
rt.trustAutoApprove = true
hm := NewManager(rt)
t.Cleanup(hm.Stop)
rt.registry.setLookup(55, map[string]interface{}{"public_key": bindKeyA})

hm.processRelayedRequest(55, "from-relay")

Expand All @@ -91,6 +92,9 @@ func TestProcessRelayedRequestAutoApproveMarksNonMutual(t *testing.T) {
if rec.Mutual {
t.Fatal("rec.Mutual should be false (auto_approve, not mutual)")
}
if rec.PublicKey != bindKeyA {
t.Fatalf("PublicKey = %q, want the registry's key", rec.PublicKey)
}
if isPending {
t.Fatal("pending[55] should NOT be set when auto-approved")
}
Expand Down Expand Up @@ -131,10 +135,9 @@ func TestProcessRelayedRequestTrustedAgentAutoApproves(t *testing.T) {
// NOT parallel — setTestTrustedAgents mutates a package-level map.
setTestTrustedAgents(t, []testAgent{{NodeID: 77, Hostname: "crtsh.example"}})

rt := newTestRuntime()
hm, rt := newFakeHM(t)
rt.trustChecker = testTrustChecker{}
hm := NewManager(rt)
t.Cleanup(hm.Stop)
rt.registry.setLookup(77, map[string]interface{}{"public_key": bindKeyA})

hm.processRelayedRequest(77, "service-agent reply handshake")

Expand Down
2 changes: 1 addition & 1 deletion zz_outgoing_lifetime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestOutgoing_AcceptanceHoursLaterIsHonoured(t *testing.T) {
hm.mu.Unlock()
hm.reapOutgoingAndRevoked() // the reaper must not drop it

hm.handleAccept(&HandshakeMsg{Type: HandshakeAccept, NodeID: 77, PublicKey: "k", Timestamp: time.Now().Unix()})
hm.handleAccept(&HandshakeMsg{Type: HandshakeAccept, NodeID: 77, PublicKey: "k", Timestamp: time.Now().Unix()}, true)

hm.mu.RLock()
defer hm.mu.RUnlock()
Expand Down
9 changes: 6 additions & 3 deletions zz_pending_full_auto_accept_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestFullPendingQueueDoesNotBlockAutoAccept(t *testing.T) {
t.Run("trust-auto-approve, direct", func(t *testing.T) {
m, _ := hsTestManager(t, true)
fillPending(m)
m.handleRequest(nil, &HandshakeMsg{NodeID: 5000, PublicKey: "k"}, false)
m.handleRequest(nil, &HandshakeMsg{NodeID: 5000, PublicKey: "k"}, true)
m.mu.RLock()
defer m.mu.RUnlock()
if _, ok := m.trusted[5000]; !ok {
Expand All @@ -35,7 +35,9 @@ func TestFullPendingQueueDoesNotBlockAutoAccept(t *testing.T) {
})

t.Run("trust-auto-approve, relayed", func(t *testing.T) {
m, _ := hsTestManager(t, true)
m, rt := newFakeHM(t)
rt.trustAutoApprove = true
rt.registry.setLookup(6000, map[string]interface{}{"public_key": bindKeyA})
fillPending(m)
m.processRelayedRequest(6000, "join")
m.mu.RLock()
Expand All @@ -46,7 +48,8 @@ func TestFullPendingQueueDoesNotBlockAutoAccept(t *testing.T) {
})

t.Run("mutual request, relayed", func(t *testing.T) {
m, _ := hsTestManager(t, false)
m, rt := newFakeHM(t)
rt.registry.setLookup(7000, map[string]interface{}{"public_key": bindKeyA})
fillPending(m)
m.mu.Lock()
m.outgoing[7000] = time.Now()
Expand Down
12 changes: 2 additions & 10 deletions zz_ppa001_accept_revoke_identity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ func TestPPA001_HonestAcceptOverMatchingStreamTrusts(t *testing.T) {
hm.outgoing[peer] = time.Now()
hm.mu.Unlock()

msg := &HandshakeMsg{
Type: HandshakeAccept,
NodeID: peer,
Timestamp: time.Now().Unix(),
}
msg := signedMsg(deterministicIdentity(50), HandshakeAccept, peer, 1)
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}}, msg)

hm.mu.RLock()
Expand Down Expand Up @@ -127,11 +123,7 @@ func TestPPA001_HonestRevokeOverMatchingStreamRevokes(t *testing.T) {
hm.trusted[peer] = &TrustRecord{NodeID: peer, ApprovedAt: time.Now()}
hm.mu.Unlock()

msg := &HandshakeMsg{
Type: HandshakeRevoke,
NodeID: peer,
Timestamp: time.Now().Unix(),
}
msg := signedMsg(deterministicIdentity(80), HandshakeRevoke, peer, 1)
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: peer}}, msg)

hm.mu.RLock()
Expand Down
7 changes: 5 additions & 2 deletions zz_ppa008_replay_key_timestamp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,11 @@ func TestPPA008_DistinctSendersGetDistinctReplayKeys(t *testing.T) {
hm.mu.Unlock()

ts := time.Now().Unix()
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 11}}, &HandshakeMsg{Type: HandshakeReject, NodeID: 11, Timestamp: ts})
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 22}}, &HandshakeMsg{Type: HandshakeReject, NodeID: 22, Timestamp: ts})
from11 := signedMsg(deterministicIdentity(11), HandshakeReject, 11, hm.rt.NodeID())
from22 := signedMsg(deterministicIdentity(22), HandshakeReject, 22, hm.rt.NodeID())
from11.Timestamp, from22.Timestamp = ts, ts
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 11}}, from11)
hm.processMessage(&addrStream{addr: coreapi.Addr{Node: 22}}, from22)

hm.mu.RLock()
_, out11 := hm.outgoing[11]
Expand Down
Loading
Loading