Skip to content

Commit

Permalink
restriction of chain-id values
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Sep 12, 2023
1 parent 87bad44 commit dfc1d88
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,9 @@ __Z_INLINE parser_error_t parser_screenPrint(const parser_context_t *ctx,

//Translate output, cpy to tmp to assure it ends in \0
MEMZERO(tmp, tmp_len);
if(container->screen.contentPtr == NULL) {
return parser_unexpected_value;
}
MEMCPY(tmp, container->screen.contentPtr, container->screen.contentLen);
CHECK_PARSER_ERR(tx_display_translation(out, sizeof(out), tmp,container->screen.contentLen))

Expand All @@ -353,6 +356,9 @@ __Z_INLINE parser_error_t parser_screenPrint(const parser_context_t *ctx,
}

MEMZERO(ctx->tx_obj->tx_text.tmpBuffer, sizeof(ctx->tx_obj->tx_text.tmpBuffer));
if(container->screen.titlePtr == NULL) {
return parser_unexpected_value;
}
MEMCPY(tmp, container->screen.titlePtr, container->screen.titleLen);
MEMCPY(tmp + container->screen.titleLen,": ",2);
MEMCPY(tmp + container->screen.titleLen + 2, out, sizeof(out) - container->screen.titleLen -2);
Expand Down Expand Up @@ -447,6 +453,16 @@ __Z_INLINE parser_error_t parser_getTextualItem(const parser_context_t *ctx,
container.screen.expert = false;
CHECK_PARSER_ERR(parser_getScreenInfo(ctx, &container, displayIdx))

// title and content can be Null depending on the screen for chain id they cant be null
if (container.screen.titlePtr != NULL && container.screen.contentPtr != NULL) {
if (!strncmp(container.screen.titlePtr, "Chain id", container.screen.titleLen)){
if(!strncmp(container.screen.contentPtr, "0", container.screen.contentLen) ||
!strncmp(container.screen.contentPtr, "1", container.screen.contentLen)) {
return parser_unexpected_chain;
}
}
}

if (!app_mode_expert()) {
CHECK_PARSER_ERR(parser_getNextNonExpert(ctx, &container, displayIdx))
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/tx_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ __Z_INLINE parser_error_t calculate_is_default_chainid() {
// If we don't match the default chainid, switch to expert mode
display_cache.is_default_chain = true;
zemu_log_stack("DEFAULT Chain ");
} else if (strcmp(outVal, "0") == 0 || strcmp(outVal, "1") == 0) {

This comment has been minimized.

Copy link
@ftheirs

ftheirs Sep 13, 2023

Contributor

Since we are using just one byte, I think that we can compare directly with the ascii value (0x30 = "0" | 0x31 = "1") to avoid adding the len check.

zemu_log_stack("Not Allowed chain");
return parser_unexpected_chain;
} else {
zemu_log_stack("Chain is NOT DEFAULT");
}
Expand Down
15 changes: 14 additions & 1 deletion tests/tx_parse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ namespace {
}

TEST(TxParse, Count_Minimal) {
auto transaction = R"({"account_number":"0"})";
auto transaction = R"({"account_number":"3"})";

parser_tx_obj.tx_json.tx = transaction;
parser_tx_obj.tx_json.flags.cache_valid = false;
Expand All @@ -140,6 +140,19 @@ namespace {
EXPECT_EQ(1, numItems) << "Wrong number of items";
}

TEST(TxParse, Tx_Chain_not_Allowed) {
auto transaction = R"({"account_number":"0","chain_id":"0","fee":{"amount":[{"amount":"5","denom":"photon"}],"gas":"10000"},"memo":"testmemo","msgs":[{"inputs":[{"address":"cosmosaccaddr1d9h8qat5e4ehc5","coins":[{"amount":"10","denom":"atom"}]}],"outputs":[{"address":"cosmosaccaddr1da6hgur4wse3jx32","coins":[{"amount":"10","denom":"atom"}]}]}],"sequence":"1"})";

parser_tx_obj.tx_json.tx = transaction;
parser_tx_obj.tx_json.flags.cache_valid = false;
parser_error_t err = JSON_PARSE(&parser_tx_obj.tx_json.json, parser_tx_obj.tx_json.tx);
EXPECT_EQ(err, parser_ok);

uint8_t numItems;
parser_error_t err2 = tx_display_numItems(&numItems);
EXPECT_EQ(err2, parser_unexpected_chain);
}

TEST(TxParse, Tx_Page_Count) {
auto transaction = R"({"account_number":"0","chain_id":"test-chain-1","fee":{"amount":[{"amount":"5","denom":"photon"}],"gas":"10000"},"memo":"testmemo","msgs":[{"inputs":[{"address":"cosmosaccaddr1d9h8qat5e4ehc5","coins":[{"amount":"10","denom":"atom"}]}],"outputs":[{"address":"cosmosaccaddr1da6hgur4wse3jx32","coins":[{"amount":"10","denom":"atom"}]}]}],"sequence":"1"})";

Expand Down

0 comments on commit dfc1d88

Please sign in to comment.