]> Untitled Git - bdk/commitdiff
get psbt inputs with bounds check
authorRiccardo Casatta <riccardo@casatta.it>
Thu, 6 May 2021 13:55:58 +0000 (15:55 +0200)
committerRiccardo Casatta <riccardo@casatta.it>
Thu, 6 May 2021 14:10:11 +0000 (16:10 +0200)
src/psbt/mod.rs
src/wallet/mod.rs

index 2490952f2dbdc79ec977317b2e5ed4d6c471480d..d83f18a3628b674ed4f3e6f95111ad5755fbfeb8 100644 (file)
@@ -41,6 +41,7 @@ impl PsbtUtils for PSBT {
 #[cfg(test)]
 mod test {
     use crate::bitcoin::consensus::deserialize;
+    use crate::bitcoin::TxIn;
     use crate::psbt::PSBT;
     use crate::wallet::test::{get_funded_wallet, get_test_wpkh};
     use crate::wallet::AddressIndex;
@@ -51,7 +52,7 @@ mod test {
 
     #[test]
     #[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 (wallet, _, _) = get_funded_wallet(get_test_wpkh());
         let send_to = wallet.get_address(AddressIndex::New).unwrap();
@@ -68,7 +69,7 @@ mod test {
 
     #[test]
     #[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 (wallet, _, _) = get_funded_wallet(get_test_wpkh());
         let send_to = wallet.get_address(AddressIndex::New).unwrap();
@@ -82,4 +83,20 @@ mod test {
         };
         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();
+    }
 }
index cb1cc75420448ec4c64aac53b9e34882b8758da2..ac32cb639c035ae860101e1794cbe65e60e4d57f 100644 (file)
@@ -61,6 +61,7 @@ use crate::descriptor::{
 };
 use crate::error::Error;
 use crate::psbt::PsbtUtils;
+use crate::signer::SignerError;
 use crate::types::*;
 
 const CACHE_ADDR_BATCH_SIZE: u32 = 100;
@@ -927,7 +928,10 @@ where
         let mut finished = true;
 
         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() {
                 continue;
             }