Skip to content

fix(consensus): validate vote validator index against the active validator set - #1329

Merged
lklimek merged 3 commits into
v1.6-devfrom
fix/consensus-vote-index-bounds
Jun 12, 2026
Merged

lklimek merged 3 commits into
v1.6-devfrom
fix/consensus-vote-index-bounds

Conversation

@lklimek

@lklimek lklimek commented Jun 11, 2026

Copy link
Copy Markdown
Collaborator

Issue being fixed or feature implemented

Vote processing looked up a validator by the ValidatorIndex carried in an incoming vote before confirming that index is within the current validator set. ValidatorSet.GetByIndex returns no validator for an out-of-range index, and that result was then used without a nil check.

Vote.ValidateBasic only 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:

  1. addVoteValidateVoteMw — reject ValidatorIndex < 0 || int(ValidatorIndex) >= stateData.Validators.Size() before any lookup.
  2. addVoteVerifyVoteExtensionMw — nil-check the GetByIndex result before using val.PubKey. This site resolves against stateData.state.Validators, which can differ from the round's stateData.Validators, so this guard is load-bearing, not redundant with rename repo to dashevo/tenderdash #1.
  3. 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?

  • New table-driven test 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.
  • A focused test exercising guard crypto: add BLS12381 validator key type #2 in isolation, constructing the round Validators (size 2) and state.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/..., and go 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:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have made corresponding changes to the documentation

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

🤖 Co-authored by Claudius the Magnificent AI Agent

…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]>
@coderabbitai

coderabbitai Bot commented Jun 11, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

Warning

Review limit reached

@lklimek, we couldn't start this review because you've reached your PR review rate limit.

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 @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

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 configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: cde8a910-0444-4710-bdd7-814f44a83624

📥 Commits

Reviewing files that changed from the base of the PR and between 297c65c and 095f893.

📒 Files selected for processing (2)
  • internal/consensus/state_add_vote.go
  • internal/consensus/state_add_vote_test.go
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/consensus-vote-index-bounds

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@lklimek
lklimek changed the base branch from v1.5-dev to v1.6-dev June 11, 2026 09:34
@lklimek
lklimek marked this pull request as ready for review June 11, 2026 09:34
@lklimek
lklimek requested a review from QuantumExplorer as a code owner June 11, 2026 09:34
@thepastaclaw

thepastaclaw commented Jun 11, 2026 •

Copy link
Copy Markdown

✅ Review complete (commit 60ea883)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 addVoteValidateVoteMw to reject votes with ValidatorIndex < 0 or ValidatorIndex >= Validators.Size().
  • Added a nil-guard in addVoteVerifyVoteExtensionMw before using val.PubKey when resolving against stateData.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.

Comment thread internal/consensus/state_add_vote.go Resolved

@thepastaclaw thepastaclaw left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@lklimek
lklimek merged commit 8ab6adb into v1.6-dev Jun 12, 2026
14 of 15 checks passed
@lklimek
lklimek deleted the fix/consensus-vote-index-bounds branch June 12, 2026 09:14
lklimek added a commit that referenced this pull request Jun 12, 2026
…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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants