Reject duplicate keys and truncated maps when parsing PSBT maps in sign_psbt
What changed, and why it matters
This commit fixes a bug in how the Ledger Bitcoin app client parses PSBT (Partially Signed Bitcoin Transaction) maps. Previously, if the same key appeared twice in a map, the parser silently kept the last value. A malicious or malformed PSBT could use this to make the device commit to one set of data while the client software saw another, potentially causing the user to sign a transaction different from what they reviewed. The fix also rejects truncated PSBTs and extra trailing data, which could otherwise be silently repaired during reserialization and hide attacks.
Treat this as a security fix and include it in the next release. Users and integrators should update the Python client library. Review any PSBT workflows that previously accepted malformed or duplicate-key PSBTs, as they will now raise errors. No CVE or vendor advisory was supplied; consider requesting a security advisory from Ledger if one is not already published.
Security signals we found
Silent overwrite of duplicate PSBT map keys could create commitment mismatch between client and device
Truncated PSBT maps were silently accepted due to exception swallowing in deserialization loops
Trailing data after PSBT maps was ignored, allowing reserialization to silently normalize malformed inputs
PSBTv2 unknown entries colliding with typed keys could alter the map commitment without affecting typed fields
Evidence from the diff
The patch hardens parse_stream_to_map in client.py and PSBT.deserialize in psbt.py. parse_stream_to_map now raises on duplicate keys instead of overwriting, and uses a strict string deserializer that fails on truncated entries. sign_psbt now checks for trailing bytes after the parsed maps. PSBT.deserialize now raises on truncated maps and trailing data instead of silently breaking. _serialize.py’s deser_string now raises when fewer bytes are available than the compact-size length declares. Tests are added demonstrating duplicate-key rejection, trailing-data rejection, and truncated-PSBT rejection for both bytes and base64 inputs.
Changed components
bitcoin_client/ledger_bitcoin/_serialize.pybitcoin_client/ledger_bitcoin/client.pybitcoin_client/ledger_bitcoin/psbt.pybitcoin_client/tests/test_parse_stream_to_map.pyInspect captured patch +107 / −10
### bitcoin_client/ledger_bitcoin/_serialize.py
@@ -80,7 +80,10 @@ def deser_string(f: Readable) -> bytes:
:returns: The byte string that was serialized
"""
nit = deser_compact_size(f)
- return f.read(nit)
+ s = f.read(nit)
+ if len(s) != nit:
+ raise ValueError("Unexpected end of data while reading a string")
+ return s
def ser_string(s: bytes) -> bytes:
"""
### bitcoin_client/ledger_bitcoin/client.py
@@ -18,24 +18,35 @@
from .merkle import get_merkleized_map_commitment
from .wallet import WalletPolicy, WalletType
from .psbt import PSBT, normalize_psbt
-from ._serialize import deser_string
+from ._serialize import deser_compact_size
from .bip0327 import key_agg, cbytes
+def _deser_string_strict(f: BufferedReader) -> bytes:
+ try:
+ length = deser_compact_size(f)
+ except Exception:
+ raise ValueError("Truncated PSBT map")
+ data = f.read(length)
+ if len(data) != length:
+ raise ValueError("Truncated PSBT map")
+ return data
+
+
def parse_stream_to_map(f: BufferedReader) -> Mapping[bytes, bytes]:
result = {}
while True:
- try:
- key = deser_string(f)
- except Exception:
- break
+ key = _deser_string_strict(f)
# Check for separator
if len(key) == 0:
break
- value = deser_string(f)
+ value = _deser_string_strict(f)
+
+ if key in result:
+ raise ValueError(f"Duplicate key in PSBT map: {key.hex()}")
result[key] = value
return result
@@ -317,6 +328,9 @@ def sign_psbt(self, psbt: Union[PSBT, bytes, str], wallet: WalletPolicy, wallet_
for m in output_maps:
client_intepreter.add_known_mapping(m)
+ if f.read() != b"":
+ raise ValueError("Unexpected trailing data after the PSBT maps")
+
# We also add the Merkle tree of the input (resp. output) map commitments as a known tree
input_commitments = [get_merkleized_map_commitment(m_in) for m_in in input_maps]
output_commitments = [get_merkleized_map_commitment(m_out) for m_out in output_maps]
### bitcoin_client/ledger_bitcoin/psbt.py
@@ -180,7 +180,7 @@ def deserialize(self, f: Readable) -> None:
try:
key = deser_string(f)
except Exception:
- break
+ raise PSBTSerializationError("Truncated PSBT map")
# Check for separator
if len(key) == 0:
@@ -599,7 +599,7 @@ def deserialize(self, f: Readable) -> None:
try:
key = deser_string(f)
except Exception:
- break
+ raise PSBTSerializationError("Truncated PSBT map")
# Check for separator
if len(key) == 0:
@@ -831,7 +831,7 @@ def deserialize(self, psbt: str) -> None:
try:
key = deser_string(f)
except Exception:
- break
+ raise PSBTSerializationError("Truncated PSBT map")
# Check for separator
if len(key) == 0:
@@ -995,6 +995,9 @@ def deserialize(self, psbt: str) -> None:
if len(self.outputs) != output_count:
raise PSBTSerializationError("Outputs provided does not match the number of outputs in transaction")
+ if f.tell() != end:
+ raise PSBTSerializationError("Unexpected trailing data after the PSBT")
+
self.cache_unsigned_tx_pieces()
def serialize(self) -> str:
### bitcoin_client/tests/test_parse_stream_to_map.py
@@ -0,0 +1,77 @@
+import base64
+from io import BytesIO
+
+import pytest
+
+from bitcoin_client.ledger_bitcoin.client import NewClient, parse_stream_to_map
+from bitcoin_client.ledger_bitcoin.errors import PSBTSerializationError
+from bitcoin_client.ledger_bitcoin.psbt import PSBT, PartiallySignedInput, PartiallySignedOutput
+from bitcoin_client.ledger_bitcoin.tx import CTransaction, CTxIn, CTxOut, COutPoint
+from bitcoin_client.ledger_bitcoin.wallet import WalletPolicy
+
+
+def make_psbt_v2() -> PSBT:
+ """A PSBTv2 with one input and one output."""
+ tx = CTransaction()
+ tx.vin = [CTxIn(COutPoint(1, 0), b"", 0)]
+ tx.vout = [CTxOut(1000, b"\x6a")]
+
+ psbt = PSBT(tx)
+ psbt.inputs = [PartiallySignedInput(0)]
+ psbt.outputs = [PartiallySignedOutput(0)]
+ psbt.convert_to_v2()
+ return psbt
+
+
+def parse_output_map(psbt: PSBT):
+ f = BytesIO(base64.b64decode(psbt.serialize()))
+ assert f.read(5) == b"psbt\xff"
+ parse_stream_to_map(f) # global map
+ parse_stream_to_map(f) # input map
+ return parse_stream_to_map(f)
+
+
+def test_parse_stream_to_map():
+ output_map = parse_output_map(make_psbt_v2())
+ assert output_map[b"\x03"] == (1000).to_bytes(8, "little")
+ assert output_map[b"\x04"] == b"\x6a"
+
+
+def test_parse_stream_to_map_rejects_duplicate_keys():
+ # an unknown entry colliding with PSBT_OUT_AMOUNT is serialized as a duplicate key
+ psbt = make_psbt_v2()
+ psbt.outputs[0].unknown[b"\x03"] = (1).to_bytes(8, "little")
+
+ with pytest.raises(ValueError, match="Duplicate key"):
+ parse_output_map(psbt)
+
+
+class NoDeviceTransport:
+ def apdu_exchange(self, *args, **kwargs):
+ raise AssertionError("no APDU must be sent")
+
+
+@pytest.mark.parametrize("as_base64", [False, True])
+def test_sign_psbt_rejects_trailing_data(as_base64: bool):
+ psbt_bytes = base64.b64decode(make_psbt_v2().serialize()) + b"\x00"
+ psbt = base64.b64encode(psbt_bytes).decode() if as_base64 else psbt_bytes
+
+ wallet = WalletPolicy("", "wpkh(@0/**)", ["[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P"])
+ client = NewClient(NoDeviceTransport())
+ with pytest.raises(PSBTSerializationError, match="trailing data"):
+ client.sign_psbt(psbt, wallet, None)
+
+
+# Dropping 1 byte removes the separator of the last output map; dropping 2 bytes also truncates
+# the value of its last entry. Reserializing the PSBT would restore the missing bytes, so these
+# must be rejected while deserializing.
+@pytest.mark.parametrize("as_base64", [False, True])
+@pytest.mark.parametrize("n_dropped", [1, 2])
+def test_sign_psbt_rejects_truncated_psbt(as_base64: bool, n_dropped: int):
+ psbt_bytes = base64.b64decode(make_psbt_v2().serialize())[:-n_dropped]
+ psbt = base64.b64encode(psbt_bytes).decode() if as_base64 else psbt_bytes
+
+ wallet = WalletPolicy("", "wpkh(@0/**)", ["[f5acc2fd/84'/1'/0']tpubDCtKfsNyRhULjZ9XMS4VKKtVcPdVDi8MKUbcSD9MJDyjRu1A2ND5MiipozyyspBT9bg8upEp7a8EAgFxNxXn1d7QkdbL52Ty5jiSLcxPt1P"])
+ client = NewClient(NoDeviceTransport())
+ with pytest.raises((PSBTSerializationError, ValueError)):
+ client.sign_psbt(psbt, wallet, None)Why this scored 72/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.