fix(core/bitcoin): reject SPV proofs with unconsumed leaf-index bits#431
Open
dsmfa10 wants to merge 1 commit into
Open
fix(core/bitcoin): reject SPV proofs with unconsumed leaf-index bits#431dsmfa10 wants to merge 1 commit into
dsmfa10 wants to merge 1 commit into
Conversation
`verify_spv_proof` shifted `idx` right once per sibling but never checked that all index bits were consumed. Any high bits beyond `siblings.len()` levels were silently dropped, so leaf index `i` and `i + (1 << siblings.len())` verified against the same root — proof malleability. Require `idx == 0` after the fold so the index must address exactly the tree depth the proof provides. Existing single/two-tx proof tests still pass. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
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.
Problem
verify_spv_proofwalks the Merkle siblings, consuming one bit of the leafindexper level viaidx >>= 1, but never checks that all index bits wereconsumed:
After folding
siblings.len()levels, any high bits remaining inidxaresilently discarded. So leaf index
iandi + (1 << siblings.len())produce thesame fold and verify against the same root — proof malleability (multiple
distinct
indexvalues accepted for one proof). This contradicts thecanonical-encoding hardening already applied to
SpvProof::from_bytes(issue#181 Finding 3), which rejects trailing garbage for exactly this reason.
Fix
Require
idx == 0after the fold, so the index must address exactly the treedepth the proof provides.
A valid Merkle proof's leaf index is
< 2^depthwheredepth == siblings.len(),so
idxshifts down to 0 — legitimate proofs pass. The single-tx (emptysiblings) case correctly requires
index == 0.Verification
cargo test -p dsm --lib bitcoin::spv— all 5 tests pass (single-tx, two-tx,roundtrip, trailing-garbage rejection, header extraction).
Notes
A related, separate observation (not fixed here):
verify_spv_proofhas noguard against an interior 64-byte node being passed as a "txid"; in practice the
consumer (
limbo_vault) independently deserializes the raw tx, so it's coveredthere. Left as-is to avoid changing accept behavior.
CI gates & coverage
Full verification for this PR runs in CI — Rust (
cargo fmt --check,clippy -D warnings, workspace tests), Frontend, Android Unit Tests,Coverage, SPDX headers, CodeQL (see the PR's Checks tab). The local
check noted above is a subset; the broader mandated gates (full workspace test
suite, codegen/scan, Android, Frontend) run in CI, not locally — none is
silently skipped.