bitcoindevkit / bitcoindevkit/bdk_wallet

`Psbt::sign` panics on `non_witness_utxo` with out-of-range vout (upstream fix pending in bitcoin 0.32.x)

Open
#554 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Rust
Stars
59
Forks
105
Avg merge
10d 9h
Merged PRs (30d)
1

Description

**Describe the bug**

`bitcoin::Psbt::spend_utxo` in the `bitcoin` 0.32.x line indexes `non_witness_utxo.output[vout]` without a bounds check, so `Psbt::sign` (and `sighash_ecdsa`) panic on a PSBT input whose `non_witness_utxo` has fewer outputs than `previous_output.vout` (e.g. a wrong or truncated previous transaction). This was fixed upstream on master in rust-bitcoin/rust-bitcoin#5500 (returns `MissingSpendUtxo` instead), but the fix is not in the 0.32.x branches as of `0.32.8`.

`Wallet::sign_with_signers` is not affected, since it validates `non_witness_utxo` against the outpoint before signing. However, since #505 the recommended flow in docs and examples is to call `bitcoin::Psbt::sign` directly, which does reach the panic when given a malformed PSBT.

This issue tracks picking up the upstream fix (backport to 0.32.x or bump to a release that includes it).

This issue was found by AI.

**To Reproduce**

```rust
// psbt built by the wallet with a single wpkh input, then corrupted:
let mut psbt = builder.finish().unwrap();
psbt.inputs[0].witness_utxo = None;
psbt.inputs[0].non_witness_utxo = Some(Transaction {
version: transaction::Version::TWO,
lock_time: absolute::LockTime::ZERO,
input: vec![],
output: vec![], // no outputs
});
psbt.sign(&xpriv, wallet.secp_ctx()); // panics: index out of bounds at bitcoin-0.32.8/src/psbt/mod.rs:626
```

**Expected behavior**

Signing a PSBT with an inconsistent `non_witness_utxo` should return an error rather than panic.

**Build environment**
- BDK tag/commit: `6fc68462` (master), `bitcoin` 0.32.8

**Which backend(s) are relevant (if any)?**
- [x] None / not backend-related (e.g. `bdk_chain`, `bdk_core`)

**Is this blocking production use?**
- [x] No

Contributor guide

Open the contributing guide

Research direction

Start with bitcoin-0.32.8/src/psbt/mod.rs around spend_utxo and the Psbt::sign and sighash_ecdsa entry points. Compare the upstream rust-bitcoin#5500 fix and decide whether to backport it or update the bitcoin release. Done means malformed non_witness_utxo inputs return an error instead of panicking, with a regression test covering an out-of-range vout.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
cryptography
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.