fix(bump): validate subtree count varint to bound parser allocations (#66)#129
Merged
Conversation
…66) The block-binary parser fed two untrusted varints — subtreeCount and cbBUMPLen — directly into make() without validating them. PR#107 capped the body at 1 GiB but the in-band counts could still claim ~2^64 elements, forcing a multi-petabyte preallocation before the LimitReader ever ran out of bytes. Reject implausible subtree counts (>10M, the documented Teranode-class headroom) and counts that cannot physically fit in the remaining body. Apply the same body-capacity sanity check to cbBUMPLen, falling back to the existing "no coinbase BUMP" path so the subtree hashes are still returned. Adds tests covering huge varints, the body-capacity boundary, and the cbBUMPLen sibling path. Closes #66
The /block binary response prefixes the payload with two metadata varints (txCount, sizeBytes) that parseBlockBinary currently reads only to advance the cursor. They are not used for allocation today, so they are not part of the F-008 exploit surface — but a future refactor could begin to consume them, and the cost of bounding them now is one branch per parse. Cap txCount at 1e9 (well above any plausible Teranode block) and sizeBytes at 1 TiB (a logical block-size limit, not a response-body limit — the body itself is enforced separately by DefaultMaxBlockBytes). Adds two tests asserting that oversized varints are rejected. Defense-in-depth follow-up to #66 / F-008.
0d8e9e4 to
00d4ae2
Compare
bytes.Reader.Len() is documented to return the number of unread bytes, which is always non-negative. gosec G115 flags the int -> uint64 cast defensively; add inline //nolint annotations with rationale, matching the project's existing nolint:gosec style in datahub_test.go.
Contributor
There was a problem hiding this comment.
Pull request overview
Hardens the DataHub /block/<hash> binary parser to treat in-band varints as untrusted input, preventing attacker-controlled counts/lengths from triggering extreme allocations or unrealistic parsing work, while preserving the existing best-effort behavior for returning subtree hashes when the coinbase tail is malformed.
Changes:
- Added explicit upper bounds for
txCount,sizeBytes, andsubtreeCount, and validatedsubtreeCountagainst remaining-body capacity before preallocation. - Added a remaining-body capacity check for
cbBUMPLen, falling back to the existing “no coinbase BUMP” path on oversize. - Added tests covering oversize varints and “plausible-but-impossible” body-capacity cases.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
bump/datahub.go |
Adds max caps and remaining-body checks to prevent untrusted varints from driving large allocations in parseBlockBinary. |
bump/datahub_test.go |
Adds regression tests for oversized varints and body-capacity validation behavior (F-008). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
subtreeCountagainst a documentedmaxSubtreeCount = 10_000_000cap before allocating; reject counts that cannot fit in the remaining body (32 bytes per hash). Closes F-008 ([F-008] untrusted block metadata drives large parser allocations #66).cbBUMPLen, falling back to the existing "no coinbase BUMP" path so subtree hashes are still returned when the coinbase tail is bogus.txCountandsizeBytesmetadata varints too. These are read but currently only consume bytes to advance the cursor — bounding them now (1e9 and 1 TiB respectively) costs one branch per parse and prevents a future refactor from creating a new allocation path on untrusted input.io.LimitReaderran out of bytes.Choice of caps
subtreeCount:DefaultMaxBlockBytes / 32≈ 33.5M is the absolute hard ceiling implied by the body cap. 10,000,000 is comfortably above any plausible Teranode-class block (millions of subtrees) while leaving 3x headroom.txCount: 1,000,000,000 — well above any plausible block tx count.sizeBytes: 1 TiB — a logical block-size limit, not a response-body limit (the body itself is already enforced byDefaultMaxBlockBytes).Rebase notes (2026-05-27)
Rebased onto current
main(40ac42e). The original commit (subtree/cbBUMP fix) replays cleanly against the currentparseBlockBinary; the only conflict was in the constants region ofbump/datahub.go, wheremainhad since added theBlockDataValidatortype — resolved by keeping both declarations side-by-side. The defense-in-depthtxCount/sizeBytesadditions are a follow-up commit on top.Test plan
go build ./...go vet ./...go test ./bump/... -count=1 -racesubtreeCountrejected via fetcher, plausible-but-impossiblesubtreeCountrejected by body-capacity check, zero-count happy path still works, oversizecbBUMPLenshort-circuits to nil coinbase BUMP, oversizetxCountrejected, oversizesizeBytesrejected.