get psbt inputs with bounds check

This commit is contained in:
Riccardo Casatta 2021-05-06 15:55:58 +02:00
parent 7961ae7f8e
commit 898dfe6cf1
No known key found for this signature in database
GPG Key ID: FD986A969E450397
2 changed files with 24 additions and 3 deletions

View File

@ -41,6 +41,7 @@ impl PsbtUtils for PSBT {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::bitcoin::consensus::deserialize; use crate::bitcoin::consensus::deserialize;
use crate::bitcoin::TxIn;
use crate::psbt::PSBT; use crate::psbt::PSBT;
use crate::wallet::test::{get_funded_wallet, get_test_wpkh}; use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
use crate::wallet::AddressIndex; use crate::wallet::AddressIndex;
@ -51,7 +52,7 @@ mod test {
#[test] #[test]
#[should_panic(expected = "InputIndexOutOfRange")] #[should_panic(expected = "InputIndexOutOfRange")]
fn test_psbt_malformed_legacy() { fn test_psbt_malformed_psbt_input_legacy() {
let psbt_bip: PSBT = deserialize(&base64::decode(PSBT_STR).unwrap()).unwrap(); let psbt_bip: PSBT = deserialize(&base64::decode(PSBT_STR).unwrap()).unwrap();
let (wallet, _, _) = get_funded_wallet(get_test_wpkh()); let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
let send_to = wallet.get_address(AddressIndex::New).unwrap(); let send_to = wallet.get_address(AddressIndex::New).unwrap();
@ -68,7 +69,7 @@ mod test {
#[test] #[test]
#[should_panic(expected = "InputIndexOutOfRange")] #[should_panic(expected = "InputIndexOutOfRange")]
fn test_psbt_malformed_segwit() { fn test_psbt_malformed_psbt_input_segwit() {
let psbt_bip: PSBT = deserialize(&base64::decode(PSBT_STR).unwrap()).unwrap(); let psbt_bip: PSBT = deserialize(&base64::decode(PSBT_STR).unwrap()).unwrap();
let (wallet, _, _) = get_funded_wallet(get_test_wpkh()); let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
let send_to = wallet.get_address(AddressIndex::New).unwrap(); let send_to = wallet.get_address(AddressIndex::New).unwrap();
@ -82,4 +83,20 @@ mod test {
}; };
let _ = wallet.sign(&mut psbt, options).unwrap(); let _ = wallet.sign(&mut psbt, options).unwrap();
} }
#[test]
#[should_panic(expected = "InputIndexOutOfRange")]
fn test_psbt_malformed_tx_input() {
let (wallet, _, _) = get_funded_wallet(get_test_wpkh());
let send_to = wallet.get_address(AddressIndex::New).unwrap();
let mut builder = wallet.build_tx();
builder.add_recipient(send_to.script_pubkey(), 10_000);
let (mut psbt, _) = builder.finish().unwrap();
psbt.global.unsigned_tx.input.push(TxIn::default());
let options = SignOptions {
trust_witness_utxo: true,
assume_height: None,
};
let _ = wallet.sign(&mut psbt, options).unwrap();
}
} }

View File

@ -61,6 +61,7 @@ use crate::descriptor::{
}; };
use crate::error::Error; use crate::error::Error;
use crate::psbt::PsbtUtils; use crate::psbt::PsbtUtils;
use crate::signer::SignerError;
use crate::types::*; use crate::types::*;
const CACHE_ADDR_BATCH_SIZE: u32 = 100; const CACHE_ADDR_BATCH_SIZE: u32 = 100;
@ -927,7 +928,10 @@ where
let mut finished = true; let mut finished = true;
for (n, input) in tx.input.iter().enumerate() { for (n, input) in tx.input.iter().enumerate() {
let psbt_input = &psbt.inputs[n]; let psbt_input = &psbt
.inputs
.get(n)
.ok_or(Error::Signer(SignerError::InputIndexOutOfRange))?;
if psbt_input.final_script_sig.is_some() || psbt_input.final_script_witness.is_some() { if psbt_input.final_script_sig.is_some() || psbt_input.final_script_witness.is_some() {
continue; continue;
} }