Skip to content

Commit ebfc9a4

Browse files
authored
Make the transport proxy throw exceptions on every sw except 0x9000 (#566)
1 parent 1ba263d commit ebfc9a4

5 files changed

Lines changed: 2025 additions & 8 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"@grpc/grpc-js": "^1.13.4",
4343
"@grpc/proto-loader": "^0.7.15",
4444
"@ledgerhq/hw-transport-http": "^6.30.8",
45+
"@ledgerhq/errors": "^6.23.0",
4546
"axios": "^1.11.0",
4647
"axios-retry": "^4.5.0",
4748
"dockerode": "^4.0.7",

pnpm-lock.yaml

Lines changed: 5 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Zemu.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
******************************************************************************* */
1616

1717
import { resolve } from 'node:path'
18+
import { TransportStatusError } from '@ledgerhq/errors'
1819
import type Transport from '@ledgerhq/hw-transport'
1920
import HttpTransport from '@ledgerhq/hw-transport-http'
2021
import axios, { type AxiosResponse } from 'axios'
@@ -66,6 +67,10 @@ import {
6667
} from './types'
6768
import { isTouchDevice, zondaxToggleBlindSigning, zondaxToggleExpertMode, zondaxTouchEnableSpecialMode } from './zondax'
6869

70+
enum ApduError {
71+
NoError = 0x9000,
72+
}
73+
6974
export default class Zemu {
7075
public startOptions!: IStartOptions
7176

@@ -458,6 +463,11 @@ export default class Zemu {
458463
try {
459464
self.lastTransportError = null // Clear previous error
460465
const result = await target.send(cla, ins, p1, p2, data, statusList)
466+
const sw = result.readUInt16BE(result.length - 2)
467+
468+
if (sw !== ApduError.NoError) {
469+
throw new TransportStatusError(sw)
470+
}
461471
return result
462472
} catch (error) {
463473
// Store the error for later checks

tests/error-handling.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ describe('Error Handling', () => {
2929
const validINS = 0x00
3030
const p1 = 0x00
3131
const p2 = 0x00
32+
const data = Buffer.from([])
33+
// Provide a statusList to make sure zemu throws an exception even on accepted status words
34+
const statusList = [0x9000, 0x6e00]
3235

3336
// Start timer to measure how long the error takes
3437
const startTime = Date.now()
3538

3639
try {
3740
// This should fail with CLA_NOT_SUPPORTED (0x6E00)
38-
await transport.send(invalidCLA, validINS, p1, p2)
41+
await transport.send(invalidCLA, validINS, p1, p2, data, statusList)
3942

4043
// If we get here, the test failed - we expected an error
4144
expect.fail('Expected transport.send to throw an error')

0 commit comments

Comments
 (0)