Skip to content

Commit 91352af

Browse files
Improve error-handling of chain ID when parsing APDUs
1 parent 0f9bec8 commit 91352af

File tree

5 files changed

+28
-8
lines changed

5 files changed

+28
-8
lines changed

src/network.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,15 @@ const char *get_displayable_ticker(const uint64_t *chain_id) {
144144
}
145145
return ticker;
146146
}
147+
148+
/**
149+
* Checks wether the app can support the given chain ID
150+
*
151+
* - If the given chain ID is the same as the app's one
152+
* - If both chain IDs are present in the array of Ethereum-compatible networks
153+
*/
154+
bool app_compatible_with_chain_id(const uint64_t *chain_id) {
155+
return ((chainConfig->chainId == *chain_id) ||
156+
(chain_is_ethereum_compatible(&chainConfig->chainId) &&
157+
chain_is_ethereum_compatible(chain_id)));
158+
}

src/network.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@
33
#include <stdint.h>
44
#include <stdbool.h>
55

6+
#define UNSUPPORTED_CHAIN_ID_MSG(id) \
7+
do { \
8+
PRINTF("Unsupported chain ID: %u (app: %u)\n", id, chainConfig->chainId); \
9+
} while (0)
10+
611
const char *get_network_name_from_chain_id(const uint64_t *chain_id);
712
const char *get_network_ticker_from_chain_id(const uint64_t *chain_id);
813

914
bool chain_is_ethereum_compatible(const uint64_t *chain_id);
15+
bool app_compatible_with_chain_id(const uint64_t *chain_id);
1016

1117
uint64_t get_tx_chain_id(void);
1218

src_features/provideErc20TokenInformation/cmd_provideTokenInfo.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "public_keys.h"
44
#include "common_ui.h"
55
#include "os_io_seproxyhal.h"
6+
#include "network.h"
67

78
#ifdef HAVE_CONTRACT_NAME_IN_DESCRIPTOR
89

@@ -111,7 +112,7 @@ void handleProvideErc20TokenInformation(uint8_t p1,
111112
UNUSED(tx);
112113
uint32_t offset = 0;
113114
uint8_t tickerLength;
114-
uint32_t chainId;
115+
uint64_t chain_id;
115116
uint8_t hash[INT256_LENGTH];
116117
cx_ecfp_public_key_t tokenKey;
117118

@@ -141,12 +142,13 @@ void handleProvideErc20TokenInformation(uint8_t p1,
141142
memmove(token->address, workBuffer + offset, 20);
142143
offset += 20;
143144
dataLength -= 20;
145+
// TODO: Handle 64-bit long chain IDs
144146
token->decimals = U4BE(workBuffer, offset);
145147
offset += 4;
146148
dataLength -= 4;
147-
chainId = U4BE(workBuffer, offset);
148-
if ((chainConfig->chainId != ETHEREUM_MAINNET_CHAINID) && (chainConfig->chainId != chainId)) {
149-
PRINTF("ChainId token mismatch: %d vs %d\n", chainConfig->chainId, chainId);
149+
chain_id = U4BE(workBuffer, offset);
150+
if (!app_compatible_with_chain_id(&chain_id)) {
151+
UNSUPPORTED_CHAIN_ID_MSG(chain_id);
150152
THROW(0x6A80);
151153
}
152154
offset += 4;

src_features/provideNFTInformation/cmd_provideNFTInfo.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ void handleProvideNFTInformation(uint8_t p1,
130130
// this prints raw data, so to have a more meaningful print, display
131131
// the buffer before the endianness swap
132132
PRINTF("ChainID: %.*H\n", sizeof(chain_id), (workBuffer + offset));
133-
if (!chain_is_ethereum_compatible(&chain_id)) {
134-
PRINTF("Unsupported chain ID!\n");
133+
if (!app_compatible_with_chain_id(&chain_id)) {
134+
UNSUPPORTED_CHAIN_ID_MSG(chain_id);
135135
THROW(APDU_RESPONSE_INVALID_DATA);
136136
}
137137
offset += CHAIN_ID_SIZE;

src_features/setPlugin/cmd_setPlugin.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,8 @@ void handleSetPlugin(uint8_t p1,
159159
// this prints raw data, so to have a more meaningful print, display
160160
// the buffer before the endianness swap
161161
PRINTF("ChainID: %.*H\n", sizeof(chain_id), (workBuffer + offset));
162-
if (!chain_is_ethereum_compatible(&chain_id)) {
163-
PRINTF("Unsupported chain ID!\n");
162+
if (!app_compatible_with_chain_id(&chain_id)) {
163+
UNSUPPORTED_CHAIN_ID_MSG(chain_id);
164164
THROW(APDU_RESPONSE_INVALID_DATA);
165165
}
166166
offset += CHAIN_ID_SIZE;

0 commit comments

Comments
 (0)