Skip to content

psbt: fix xpub depth overflow, duplicate check, witness bounds, and taproot sort - #2686

Open
acxxcxx wants to merge 4 commits into
btcsuite:masterfrom
acxxcxx:fix/psbt-validation-sort
Open

acxxcxx wants to merge 4 commits into
btcsuite:masterfrom
acxxcxx:fix/psbt-validation-sort

Conversation

@acxxcxx

@acxxcxx acxxcxx commented Nov 18, 2026 •

Copy link
Copy Markdown

Change Description

Fixes four edge-case bugs in psbt deserialization, sorting, and extraction:

  • ReadXPub (psbt/bip32.go): cast numPathElements (uint8) to int
    before multiplying by uint32Size to avoid uint8 overflow for extended
    keys with Depth() >= 63.
  • NewFromRawBytes (psbt/psbt.go): compare x.ExtendedKey against keydata
    instead of the shadowed keyData (nil) so duplicate global
    PSBT_GLOBAL_XPUB keys are rejected per BIP-174.
  • Extract (psbt/extractor.go): validate
    witCount <= uint64(witnessReader.Len()) before allocating
    make(wire.TxWitness, witCount) to prevent slice allocation panics on
    malformed FinalScriptWitness payloads.
  • TaprootScriptSpendSig.SortBefore (psbt/taproot.go): compare XOnlyPubKey
    and LeafHash lexicographically rather than with && so entries sharing the
    same XOnlyPubKey satisfy strict weak ordering.
  • Adds regression tests in psbt/bip32_test.go, psbt/psbt_test.go, and
    psbt/sort_test.go with both positive controls and error-path assertions for
    all four cases.

Steps to Test

cd psbt && go vet ./... && go test -v -count=1 ./...

Pull Request Checklist

Testing

  • Your PR passes all CI checks.
  • Tests covering the positive and negative (error paths) are included.
  • Bug fixes contain tests triggering the bug to prevent regressions.

Code Style and Documentation

📝 Please see our Contribution Guidelines for further guidance.

In ReadXPub, uint32Size * (numPathElements + 1) was evaluated in uint8
before converting to int, causing expectedSize to wrap around for
extended keys with Depth() >= 63.

Cast numPathElements to int before arithmetic and add a regression test.
In NewFromRawBytes, the duplicate check for PSBT_GLOBAL_XPUB compared
x.ExtendedKey against keyData (from the earlier UnsignedTxType check,
which is always nil) instead of keydata from the loop iteration.

Compare against keydata and add a regression test.
In Extract, witCount was decoded as an unvalidated uint64 varint and
passed directly to make(wire.TxWitness, witCount), allowing malformed
FinalScriptWitness payloads to trigger a slice allocation panic.

Reject witCount values exceeding witnessReader.Len() with
ErrInvalidPsbtFormat and add a regression test.
TaprootScriptSpendSig.SortBefore combined XOnlyPubKey and LeafHash
comparisons with && instead of lexicographical tie-breaking, returning
false whenever two entries shared the same XOnlyPubKey.

Break ties on XOnlyPubKey using LeafHash and add a regression test.
@acxxcxx
acxxcxx force-pushed the fix/psbt-validation-sort branch from 0ba72d0 to a469fa9 Compare November 19, 2026 02:04
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.

1 participant