Skip to content

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

Description

@noahjoeris

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

// 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)?

  • None / not backend-related (e.g. bdk_chain, bdk_core)

Is this blocking production use?

  • No

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions