psbt: Add unit test case for key byte size handling
What changed, and why it matters
This commit only adds a new unit test; it does not change production code. The test checks that parsing a malformed PSBT key with an impossibly small byte-size field returns a proper error instead of crashing. The underlying panic/underflow bug was already fixed in an earlier change, so this patch is a regression test rather than a fix.
No immediate action is required for this commit because it only adds a regression test. Review the prior commit that fixed the underflow to confirm the fix is complete and that similar length checks exist for related PSBT parsing paths.
Security signals we found
integer underflow/panic in PSBT key parsing (described as already fixed)
regression test for malformed PSBT input handling
error-path validation for undersized key length
Evidence from the diff
The diff adds a test in bitcoin/src/psbt/mod.rs named psbt_insufficient_byte_size. It builds a raw key byte sequence (0x02 fd07ff ababababab) where the declared key byte size (2) is smaller than the encoded type compact-size value (3 bytes for the 0xfd07ff compact size). It then calls Key::decode and asserts that an InvalidKey error is returned. The commit message states the panic/underflow issue was already fixed and this test prevents regressions.
Changed components
bitcoin/src/psbt/mod.rsPSBT raw key decoder (super::raw::Key::decode)Inspect captured patch +8 / −0
diff --git a/bitcoin/src/psbt/mod.rs b/bitcoin/src/psbt/mod.rs
index 7465d3e7..5dd2bb2d 100644
--- a/bitcoin/src/psbt/mod.rs
+++ b/bitcoin/src/psbt/mod.rs
@@ -1400,6 +1400,14 @@ mod tests {
assert!(!pk.compressed());
}
+ #[test]
+ fn psbt_insufficient_byte_size() {
+ // construct a key where the key byte size (0x02) is less than even the type value length (in this case, 3)
+ let key_data = hex!("02fd07ffababababab");
+ let got = super::raw::Key::decode(&mut key_data.as_slice()).unwrap_err();
+ assert!(matches!(got, Error::InvalidKey(_)));
+ }
+
#[test]
fn psbt_high_fee_checks() {
let psbt = psbt_with_amounts(Amount::MAX.to_sat(), 1000);
Why this scored 35/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.