-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcheckpoint_vote_verification_bug_test.go
More file actions
204 lines (173 loc) · 6.29 KB
/
Copy pathcheckpoint_vote_verification_bug_test.go
File metadata and controls
204 lines (173 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package nara
import (
"testing"
"time"
"github.com/eljojo/nara/identity"
"github.com/eljojo/nara/types"
)
// TestCheckpoint_VoteSignatureVerificationBug verifies that verifyVoteSignature works correctly
// Fixed: Uses Go embedding to access vote.Attestation.Signature, and uses ID-based lookup
func TestCheckpoint_VoteSignatureVerificationBug(t *testing.T) {
tc := NewTestCoordinator(t)
verifier := tc.AddNara("verifier", WithoutServer())
voter := tc.AddNara("alice", WithoutServer())
proposer := tc.AddNara("proposer", WithoutServer())
tc.Connect("verifier", "alice")
tc.Connect("verifier", "proposer")
asOfTime := time.Now().Unix()
attestation := Attestation{
Version: 1,
Subject: proposer.Me.Name,
SubjectID: proposer.ID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: voter.Me.Name,
AttesterID: voter.ID,
AsOfTime: asOfTime,
}
attestation.Signature = identity.SignContent(&attestation, voter.Keypair)
vote := &CheckpointVote{
Attestation: attestation,
ProposalTS: asOfTime,
Round: 1,
Approved: true,
}
ledger := NewSyncLedger(1000)
service := NewCheckpointService(verifier.Network, ledger, verifier)
isValid := service.verifyVoteSignature(vote)
if !isValid {
t.Error("Expected legitimate vote to pass verification, but it failed!")
} else {
t.Logf("✓ Vote verification passed for attester %s (%s)", voter.Me.Name, voter.ID)
t.Log(" ✓ Fixed: Now uses ID-based lookup (getPublicKeyForNaraID)")
t.Log(" Comment explains embedding behavior for maintainability")
}
}
// TestCheckpoint_VoteSignatureNameSpoofing verifies protection against name spoofing
// Fixed: verifyVoteSignature now looks up public key by AttesterID (not Attester name)
func TestCheckpoint_VoteSignatureNameSpoofing(t *testing.T) {
tc := NewTestCoordinator(t)
verifier := tc.AddNara("verifier", WithoutServer())
victim := tc.AddNara("alice", WithoutServer())
attacker := tc.AddNara("attacker", WithoutServer())
proposer := tc.AddNara("proposer", WithoutServer())
tc.Connect("verifier", "alice")
tc.Connect("verifier", "proposer")
asOfTime := time.Now().Unix()
attestation := Attestation{
Version: 1,
Subject: proposer.Me.Name,
SubjectID: proposer.ID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: victim.Me.Name, // SPOOFED name
AttesterID: attacker.ID, // Actual signer
AsOfTime: asOfTime,
}
attestation.Signature = identity.SignContent(&attestation, attacker.Keypair)
vote := &CheckpointVote{
Attestation: attestation,
ProposalTS: asOfTime,
Round: 1,
Approved: true,
}
ledger := NewSyncLedger(1000)
service := NewCheckpointService(verifier.Network, ledger, verifier)
isValid := service.verifyVoteSignature(vote)
t.Log("Attacker spoofed Attester name as:", victim.Me.Name)
t.Log("Actual AttesterID:", attacker.ID)
t.Log("Verification result:", isValid)
if !isValid {
t.Log("✓ Attack failed - signature verification correctly rejected")
t.Log(" ✓ Fixed: Uses ID-based lookup (getPublicKeyForNaraID)")
t.Log(" Attacker's signature doesn't match because we look up by AttesterID")
}
}
// TestCheckpoint_VoteNameVsIDLookup verifies that ID-based lookup handles name changes
// Fixed: Now works correctly even when nara changes name or we have stale name data
func TestCheckpoint_VoteNameVsIDLookup(t *testing.T) {
// Scenario: Voter "alice" created a vote, but we only know her by ID in our network
// (maybe she changed names, or we have her indexed differently)
tc := NewTestCoordinator(t)
verifier := tc.AddNara("verifier", WithoutServer())
voter := tc.AddNara("alice-renamed", WithoutServer())
proposer := tc.AddNara("proposer", WithoutServer())
tc.Connect("verifier", "alice-renamed")
tc.Connect("verifier", "proposer")
oldName := types.NaraName("alice")
asOfTime := time.Now().Unix()
attestation := Attestation{
Version: 1,
Subject: proposer.Me.Name,
SubjectID: proposer.ID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: oldName, // Vote says "alice"
AttesterID: voter.ID,
AsOfTime: asOfTime,
}
attestation.Signature = identity.SignContent(&attestation, voter.Keypair)
vote := &CheckpointVote{
Attestation: attestation,
ProposalTS: asOfTime,
Round: 1,
Approved: true,
}
ledger := NewSyncLedger(1000)
service := NewCheckpointService(verifier.Network, ledger, verifier)
// FIXED: verifyVoteSignature now looks up by vote.AttesterID (stable)
// so it works even though we only have "alice-renamed" in our network
isValid := service.verifyVoteSignature(vote)
if !isValid {
t.Error("Expected vote to pass (ID lookup succeeds), but it failed!")
} else {
t.Log("✓ Bug is fixed: Valid vote accepted despite name change")
t.Log(" Fixed: getPublicKeyForNaraID(vote.AttesterID) succeeds - ID is stable")
t.Log(" Old bug: getPublicKeyForNara(vote.Attester) would have failed - no 'alice' in network")
}
}
// TestCheckpoint_ProposalSignatureVerification verifies proposal signature verification is fixed
func TestCheckpoint_ProposalSignatureVerification(t *testing.T) {
tc := NewTestCoordinator(t)
verifier := tc.AddNara("verifier", WithoutServer())
proposer := tc.AddNara("proposer", WithoutServer())
tc.Connect("verifier", "proposer")
asOfTime := time.Now().Unix()
attestation := Attestation{
Version: 1,
Subject: proposer.Me.Name,
SubjectID: proposer.ID,
Observation: NaraObservation{
Restarts: 100,
TotalUptime: 50000,
StartTime: 1624066568,
},
Attester: proposer.Me.Name,
AttesterID: proposer.ID,
AsOfTime: asOfTime,
}
attestation.Signature = identity.SignContent(&attestation, proposer.Keypair)
proposal := &CheckpointProposal{
Attestation: attestation,
Round: 1,
}
ledger := NewSyncLedger(1000)
service := NewCheckpointService(verifier.Network, ledger, verifier)
isValid := service.verifyProposalSignature(proposal)
if !isValid {
t.Error("Expected legitimate proposal to pass, but it failed!")
} else {
t.Log("✓ Proposal verification passed (uses Go embedding to verify attestation)")
t.Log(" ✓ Fixed: Now uses ID-based lookup (getPublicKeyForNaraID)")
t.Log(" Comment explains embedding behavior for maintainability")
}
}