python client: reject PSBTv2-only fields in a PSBTv0
What changed, and why it matters
This commit tightens input validation in the Python helper library that prepares Bitcoin transaction data for Ledger hardware wallets. It now rejects PSBT files that claim to be the older version 0 format but contain fields that only belong to the newer version 2 format, and it makes sure lock-time values are taken from the original transaction when converting an old PSBT to the new format. The change brings the Python client in line with the existing hardware-side (HS) and Rust clients. It is a defensive consistency fix rather than a confirmed exploit patch.
Review whether any downstream code or tests relied on accepting v2-only fields in v0 PSBTs, and update them if necessary. Consider whether additional v2-only global fields need similar rejection. No urgent deployment action is indicated by the diff alone.
Security signals we found
Input-validation hardening: stricter parsing of PSBT version 0 to reject v2-only key types
Consistency fix across clients: aligns Python client with HS and Rust client behavior
Locktime normalization during v0-to-v2 conversion to prevent per-input locktime from overriding transaction locktime
New unit tests covering rejected fields and locktime preservation
Evidence from the diff
The patch adds deserialization checks in PartiallySignedInput.deserialize and PartiallySignedOutput.deserialize: if the PSBT version is 0, the presence of any PSBTv2-only input or output key type raises PSBTSerializationError. It also clears per-input time_locktime and height_locktime in PSBT.convert_to_v2(), because for a PSBTv0 the locktime is defined by the unsigned transaction’s nLockTime. A new test file verifies that each forbidden v2-only field is rejected and that converting a v0 PSBT preserves the transaction locktime.
Changed components
bitcoin_client/ledger_bitcoin/psbt.pybitcoin_client/tests/test_psbt_v0_v2_only_fields.pyInspect captured patch +92 / −0
### bitcoin_client/ledger_bitcoin/psbt.py
@@ -416,6 +416,18 @@ def deserialize(self, f: Readable) -> None:
key_lookup.add(key)
+ # Make sure no PSBTv2-only fields are present in PSBTv0
+ if self.version == 0:
+ for key_type in [
+ PartiallySignedInput.PSBT_IN_PREVIOUS_TXID,
+ PartiallySignedInput.PSBT_IN_OUTPUT_INDEX,
+ PartiallySignedInput.PSBT_IN_SEQUENCE,
+ PartiallySignedInput.PSBT_IN_REQUIRED_TIME_LOCKTIME,
+ PartiallySignedInput.PSBT_IN_REQUIRED_HEIGHT_LOCKTIME,
+ ]:
+ if ser_compact_size(key_type) in key_lookup:
+ raise PSBTSerializationError(f"Input field of type {key_type:#04x} is not allowed in PSBTv0")
+
# Make sure required PSBTv2 fields are present
if self.version >= 2:
if len(self.prev_txid) == 0:
@@ -688,6 +700,12 @@ def deserialize(self, f: Readable) -> None:
key_lookup.add(key)
+ # Make sure no PSBTv2-only fields are present in PSBTv0
+ if self.version == 0:
+ for key_type in [PartiallySignedOutput.PSBT_OUT_AMOUNT, PartiallySignedOutput.PSBT_OUT_SCRIPT]:
+ if ser_compact_size(key_type) in key_lookup:
+ raise PSBTSerializationError(f"Output field of type {key_type:#04x} is not allowed in PSBTv0")
+
# Make sure required PSBTv2 fields are present
if self.version >= 2:
if self.amount is None:
@@ -1188,6 +1206,10 @@ def convert_to_v2(self) -> None:
if self.version == 0:
# make sure fields that PSBT version 0 fields are not present
self.setup_from_tx(self.tx)
+ # the locktime of a PSBTv0 is the one of the unsigned transaction
+ for psbt_in in self.inputs:
+ psbt_in.time_locktime = None
+ psbt_in.height_locktime = None
self._convert_version(2)
### bitcoin_client/tests/test_psbt_v0_v2_only_fields.py
@@ -0,0 +1,70 @@
+import base64
+
+import pytest
+
+from bitcoin_client.ledger_bitcoin._serialize import ser_string
+from bitcoin_client.ledger_bitcoin.errors import PSBTSerializationError
+from bitcoin_client.ledger_bitcoin.psbt import PSBT
+from bitcoin_client.ledger_bitcoin.tx import CTransaction, CTxIn, CTxOut, COutPoint
+
+
+LOCKTIME = 800000
+
+
+def make_psbt_v0(input_entries: bytes = b"", output_entries: bytes = b"") -> str:
+ """A serialized PSBTv0 with one input and one output, with the given (raw) map entries."""
+ tx = CTransaction()
+ tx.nVersion = 2
+ tx.nLockTime = LOCKTIME
+ tx.vin = [CTxIn(COutPoint(1, 0), b"", 0xfffffffe)]
+ tx.vout = [CTxOut(1000, b"\x6a")]
+
+ psbt = (
+ b"psbt\xff"
+ + ser_string(b"\x00") + ser_string(tx.serialize_without_witness()) # PSBT_GLOBAL_UNSIGNED_TX
+ + b"\x00" # end of global map
+ + input_entries + b"\x00" # end of input map
+ + output_entries + b"\x00" # end of output map
+ )
+ return base64.b64encode(psbt).decode()
+
+
+def entry(key_type: int, value: bytes) -> bytes:
+ return ser_string(bytes([key_type])) + ser_string(value)
+
+
+@pytest.mark.parametrize("input_entry", [
+ entry(0x0e, bytes(32)), # PSBT_IN_PREVIOUS_TXID
+ entry(0x0f, (0).to_bytes(4, "little")), # PSBT_IN_OUTPUT_INDEX
+ entry(0x10, (0).to_bytes(4, "little")), # PSBT_IN_SEQUENCE
+ entry(0x11, (900000000).to_bytes(4, "little")), # PSBT_IN_REQUIRED_TIME_LOCKTIME
+ entry(0x12, (900000).to_bytes(4, "little")), # PSBT_IN_REQUIRED_HEIGHT_LOCKTIME
+])
+def test_psbt_v0_rejects_v2_only_input_fields(input_entry: bytes):
+ psbt = PSBT()
+ with pytest.raises(PSBTSerializationError, match="not allowed in PSBTv0"):
+ psbt.deserialize(make_psbt_v0(input_entries=input_entry))
+
+
+@pytest.mark.parametrize("output_entry", [
+ entry(0x03, (1000).to_bytes(8, "little")), # PSBT_OUT_AMOUNT
+ entry(0x04, b"\x6a"), # PSBT_OUT_SCRIPT
+])
+def test_psbt_v0_rejects_v2_only_output_fields(output_entry: bytes):
+ psbt = PSBT()
+ with pytest.raises(PSBTSerializationError, match="not allowed in PSBTv0"):
+ psbt.deserialize(make_psbt_v0(output_entries=output_entry))
+
+
+def test_psbt_v0_to_v2_keeps_locktime():
+ psbt = PSBT()
+ psbt.deserialize(make_psbt_v0())
+
+ # required locktimes set programmatically on a PSBTv0 are ignored by the conversion
+ psbt.inputs[0].height_locktime = 900000
+ psbt.convert_to_v2()
+
+ psbt_v2 = PSBT()
+ psbt_v2.deserialize(psbt.serialize())
+ assert psbt_v2.version == 2
+ assert psbt_v2.get_unsigned_tx().nLockTime == LOCKTIMEWhy this scored 47/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.