Skip to content

fix(blocksync): validate peer block and status responses - #1355

Merged
lklimek merged 1 commit into
v1.6-devfrom
fix/blocksync-response-validation
Jun 19, 2026
Merged

lklimek merged 1 commit into
v1.6-devfrom
fix/blocksync-response-validation

Conversation

@lklimek

@lklimek lklimek commented Jun 17, 2026

Copy link
Copy Markdown
Collaborator

Summary

Hardens the block-sync path against malformed responses from unauthenticated P2P peers.

Changes

  • Nil-check before field access in BlockResponse.Validate.
  • Reject a block whose height does not match the requested height.
  • Bound peer-reported StatusResponse Base/Height (log + ignore implausible values).

🤖 Generated with Claude Code

Order the nil checks in BlockResponse.Validate before any field access,
reject a fetched block whose height does not match the requested height,
and bound peer-reported StatusResponse base/height to plausible values
(implausible reports are logged and ignored without dropping the peer).

Adds table-driven tests for nil-block rejection, out-of-range status
reports, and block height-mismatch rejection.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@lklimek
lklimek requested a review from QuantumExplorer as a code owner June 17, 2026 13:39
@thepastaclaw

thepastaclaw commented Jun 17, 2026 •

Copy link
Copy Markdown

✅ Review complete (commit 749fab6)

@coderabbitai

coderabbitai Bot commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

Caution

Review failed

An error occurred during the review process. Please try again later.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/blocksync-response-validation

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.

@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

Focused, well-scoped hardening of the blocksync receive path: nil-Block guard in BlockResponse.Validate, requested-height match in the fetch job, and base/height plausibility on StatusResponse. The changes are correctly wired (errBlockFetch propagates to pushBack/RemovePeer/PeerError) and have matching tests. No blocking issues. One stylistic nitpick on log severity for peer-controlled input.

💬 1 nitpick(s)

Comment thread internal/blocksync/p2p_msg_handler.go Resolved

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 the block-sync pipeline against malformed or malicious responses from unauthenticated P2P peers, focusing on avoiding nil dereferences and rejecting implausible or inconsistent peer-reported data.

Changes:

  • Strengthened BlockResponse.Validate() to reject nil blocks/commits and enforce block/commit height consistency.
  • Added defensive validation around StatusResponse (reject negative/illogical heights and cap overly large heights to avoid overflow in downstream arithmetic).
  • Ensured fetched blocks match the requested height, with new unit tests covering these cases.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
internal/blocksync/synchronizer.go Tightens BlockResponse.Validate() with explicit nil checks and strict height matching.
internal/blocksync/p2p_msg_handler.go Adds plausibility bounds for peer-reported status heights to prevent overflow/invalid ranges.
internal/blocksync/p2p_msg_handler_test.go Expands status-response test coverage for valid and invalid peer status values.
internal/blocksync/block_fetch_job.go Rejects peers that return a block at a height different from the requested height.
internal/blocksync/block_fetch_job_test.go Adds tests for height mismatch handling and BlockResponse.Validate() behavior.

@lklimek
lklimek merged commit 9369798 into v1.6-dev Jun 19, 2026
22 of 23 checks passed
@lklimek
lklimek deleted the fix/blocksync-response-validation branch June 19, 2026 09:26
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