Reject duplicate keys and trailing data when parsing a PSBT in the JS client
What changed, and why it matters
This commit fixes the JavaScript PSBT (Partially Signed Bitcoin Transaction) parser in Ledger's Bitcoin app so it now rejects malformed PSBTs that contain duplicate keys or extra trailing bytes. Previously, such malformed data could be accepted, potentially causing the parser to behave inconsistently or mix up data from one PSBT with another. The fix also clears internal state before parsing, so reusing the same parser object does not accidentally keep old data.
Review whether the fixed parser behavior could have allowed malformed PSBTs to reach downstream signing logic, and consider whether a security advisory is warranted if prior versions could be induced to sign or display incorrect transaction data. Update dependent packages and notify users to upgrade the JS client.
Security signals we found
BIP-174 non-compliance: duplicate keys and trailing data were previously accepted
State reuse bug: deserializing multiple PSBTs in the same instance could merge maps
Input validation gap in client-side PSBT parsing
Potential for parser confusion or transaction misinterpretation
Evidence from the diff
The patch updates bitcoin_client_js/src/lib/psbtv2.ts in three ways: (1) it resets globalMap, inputMaps, and outputMaps at the start of deserialize() so repeated deserialization on the same PsbtV2 instance does not retain prior state; (2) it throws 'Duplicate key in PSBT map' when readKeyPair encounters a key already present in the current map; and (3) it throws 'Unexpected trailing data after the PSBT' if bytes remain after the PSBT structure is fully parsed. Tests are added for duplicate keys, trailing data, and reuse of a single parser instance.
Changed components
bitcoin_client_js/src/lib/psbtv2.tsbitcoin_client_js/src/__tests__/psbtv2.test.tsLedger Bitcoin app JavaScript client PSBT parserInspect captured patch +40 / −0
### bitcoin_client_js/src/__tests__/psbtv2.test.ts
@@ -67,4 +67,33 @@ describe("PsbtV2", () => {
"PsbtV0 contains fields that are only allowed in PsbtV2"
);
});
+
+ it("rejects a psbt with a duplicate key", async () => {
+ // PSBT_IN_SIGHASH_TYPE = 3, twice with different values
+ const duplicateSighash = Buffer.from("010304" + "01000000" + "010304" + "02000000", "hex");
+
+ const psbt = new PsbtV2();
+ expect(() => psbt.deserialize(makePsbtV0(0, duplicateSighash))).toThrow(
+ "Duplicate key in PSBT map"
+ );
+ });
+
+ it("can deserialize multiple psbts with the same instance", async () => {
+ const psbt = new PsbtV2();
+ psbt.deserialize(makePsbtV0(800000));
+ const first = psbt.serialize();
+
+ psbt.deserialize(makePsbtV0(800000));
+ expect(psbt.serialize()).toEqual(first);
+
+ psbt.deserialize(makePsbtV0(700000));
+ expect(psbt.getGlobalFallbackLocktime()).toEqual(700000);
+ });
+
+ it("rejects a psbt with trailing data", async () => {
+ const psbt = new PsbtV2();
+ expect(() => psbt.deserialize(Buffer.concat([makePsbtV0(0), Buffer.from([0x00])]))).toThrow(
+ "Unexpected trailing data after the PSBT"
+ );
+ });
});
### bitcoin_client_js/src/lib/psbtv2.ts
@@ -371,6 +371,10 @@ export class PsbtV2 {
if (!buf.readSlice(5).equals(PSBT_MAGIC_BYTES)) {
throw new Error('Invalid magic bytes');
}
+ // discard any previous content, so that it does not mix with the new PSBT's
+ this.globalMap = new Map();
+ this.inputMaps = [];
+ this.outputMaps = [];
while (this.readKeyPair(this.globalMap, buf));
let psbtVersion: number;
@@ -405,6 +409,10 @@ export class PsbtV2 {
while (this.readKeyPair(this.outputMaps[i], buf));
}
+ if (buf.offset !== psbt.length) {
+ throw new Error('Unexpected trailing data after the PSBT');
+ }
+
this.normalizeToV2();
}
normalizeToV2() {
@@ -556,6 +564,9 @@ export class PsbtV2 {
const keyType = buf.readUInt8();
const keyData = buf.readSlice(keyLen - 1);
const value = buf.readVarSlice();
+ if (map.has(new Key(keyType, keyData).toString())) {
+ throw new Error('Duplicate key in PSBT map');
+ }
set(map, keyType, keyData, value);
return true;Why this scored 40/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.