Repository navigation
fix(consensus): validate vote validator index against the active validator set - #1329
Conversation
…t lookups Votes carrying a validator index outside the active validator set are now rejected with a validation error before any lookup or key access. Adds defensive nil handling around the validator metrics lookup. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
|
Warning Review limit reached
More reviews will be available in 23 minutes and 26 seconds. Learn how PR review limits work. Your organization has run out of usage credits. Purchase more credits in the billing tab to continue. ⌛ How to resolve this issue?After more reviews become available, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans include higher PR review limits than trial, open-source, and free plans. In all cases, reviews become available again over time. During sustained high-volume PR review activity, CodeRabbit may temporarily slow when the next review becomes available. Please see our Fair Usage Limits Policy for further information. ℹ️ Review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
✅ Review complete (commit 60ea883) |
There was a problem hiding this comment.
Pull request overview
This PR hardens consensus vote handling by ensuring a vote’s ValidatorIndex is validated against the active validator set before any validator lookup/dereference occurs, preventing nil dereferences and aligning malformed-vote handling with existing validation behavior.
Changes:
- Added a bounds check in
addVoteValidateVoteMwto reject votes withValidatorIndex < 0orValidatorIndex >= Validators.Size(). - Added a nil-guard in
addVoteVerifyVoteExtensionMwbefore usingval.PubKeywhen resolving againststateData.state.Validators. - Added tests covering out-of-range indices and the “RoundState validators vs committed state validators” mismatch case.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
internal/consensus/state_add_vote.go |
Adds validator-index range validation and nil guards to prevent nil deref during vote processing/extension verification. |
internal/consensus/state_add_vote_test.go |
Adds targeted unit tests for validator-index bounds and validator-set mismatch scenarios. |
thepastaclaw
left a comment
There was a problem hiding this comment.
Code Review
Both Claude and Codex independently found no in-scope defects in PR #1329 at head 60ea883. Independent verification against the worktree confirms the three layered guards (bounds check at addVoteValidateVoteMw:286, nil check at addVoteVerifyVoteExtensionMw:257, defensive nil check at addVoteToVoteSetFunc:96) are correctly placed and the middleware ordering ensures the bounds check runs before any state-mutating dispatch. No CodeRabbit context was provided for this manual-priority run.
…idation (#1331) PR #1329 (commit 8ab6adb) switched addVoteToVoteSetFunc to look up the voter against stateData.Validators (RoundState) instead of stateData.state.Validators. When RoundState.Validators is unset, that turned GetByIndex into a method call on a nil *ValidatorSet, and the len(vals.Validators) bounds check in GetByIndex dereferenced the nil receiver, panicking before the new `if val != nil` guard could help: panic: runtime error: invalid memory address or nil pointer dereference types/validator_set.go:371 internal/consensus/state_add_vote.go:96 internal/consensus/state_add_vote_test.go:162 GetByIndex already documents that it returns nil for an out-of-range index, and its sibling methods (Copy, Equals, IsNilOrEmpty, ...) all guard a nil receiver. This extends GetByIndex to honor the same contract on a nil receiver, returning nil instead of panicking. The nil-check that #1329 added at every call site then handles the result correctly, and all other callers are strictly safer. Adds a regression test asserting GetByIndex never panics on a nil receiver for any index. Co-authored-by: Claude Opus 4.8 (1M context) <[email protected]>
Issue being fixed or feature implemented
Vote processing looked up a validator by the
ValidatorIndexcarried in an incoming vote before confirming that index is within the current validator set.ValidatorSet.GetByIndexreturns no validator for an out-of-range index, and that result was then used without a nil check.Vote.ValidateBasiconly rejects a negative index (the validator-set size isn't known at that layer), so a vote carrying an index>= Validators.Size()reached the lookup path and produced an invalid (empty) result that was used directly. This change adds the missing range and nil guards so such votes are rejected with a plain validation error before any lookup or key access — bringing vote handling in line with how other malformed votes are treated.What was done?
Three layered guards in
internal/consensus/state_add_vote.go, none of which change behavior for valid votes:addVoteValidateVoteMw— rejectValidatorIndex < 0 || int(ValidatorIndex) >= stateData.Validators.Size()before any lookup.addVoteVerifyVoteExtensionMw— nil-check theGetByIndexresult before usingval.PubKey. This site resolves againststateData.state.Validators, which can differ from the round'sstateData.Validators, so this guard is load-bearing, not redundant with rename repo to dashevo/tenderdash #1.addVoteToVoteSetFunc— defensive nil-check around the validator metrics lookup.A rejected vote flows through the existing error middleware (logged and dropped), exactly like any other invalid vote, and is not recorded as conflicting-vote evidence (verified: that path is gated on
*types.ErrVoteConflictingVotes, which these plain validation errors are not).How Has This Been Tested?
internal/consensus/state_add_vote_test.go::TestAddVoteValidatorIndexBounds: precommit with index ∈ {set size, size+1, 2_000_000_000} → vote not added, error returned; a valid index is still processed; plus a prevote case and a local-validator (verify-skipped) case.Validators(size 2) andstate.Validators(size 1) so an index that passes rename repo to dashevo/tenderdash #1 must be caught by crypto: add BLS12381 validator key type #2.gofmt,go build ./internal/consensus/...,go vet ./internal/consensus/..., andgo test -run TestAddVoteValidatorIndexBounds ./internal/consensus/all pass. (The flake-prone full consensus suite was intentionally not run.)Breaking Changes
None. Behavior is unchanged for valid votes; only out-of-range indices that were previously mishandled are now rejected with a validation error.
Checklist:
For repository code-owners and collaborators only
🤖 Co-authored by Claudius the Magnificent AI Agent