Skip to content

ERC-20: forbid using invalid NEP141 AccountID for mapping#179

Merged
joshuajbouw merged 3 commits into
developfrom
erc20-mapping
Jul 30, 2021
Merged

ERC-20: forbid using invalid NEP141 AccountID for mapping#179
joshuajbouw merged 3 commits into
developfrom
erc20-mapping

Conversation

@sept-en
Copy link
Copy Markdown
Contributor

@sept-en sept-en commented Jul 14, 2021

Fixes #173.

@sept-en sept-en added the C-enhancement Category: New feature or request label Jul 14, 2021
@birchmd
Copy link
Copy Markdown
Member

birchmd commented Jul 14, 2021

The code itself looks good, but I'd like to understand the context here.

It was mentioned on the Monday call that this is meant to fix a phishing attack where someone tries to spoof a token using different capitalization. But I thought @mfornet said this attack wouldn't be possible if the front-end interfaces were sufficiently good.

It's also unclear to me what this attack would accomplish. Could you spell out in more detail what could happen from a user's perspective?

@sept-en
Copy link
Copy Markdown
Contributor Author

sept-en commented Jul 14, 2021

@birchmd it's not only about the attack itself (which needed to be really complex and I don't expect this to happen in the nearest future), but for the code consistency and right conceptual structure.
Without this fix deploying some address: 123abc..cba321.factory.bridge.near and 123Abc..cba321.factory.bridge.near (one letter is uppercase) will deploy 2 ERC-20 tokens for the same NEP-141 address. So we might end up with many different ERC-20 tokens deployed for the same NEP-141 account.

Copy link
Copy Markdown
Member

@birchmd birchmd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, NEAR account IDs must be lowercase anyways, so it makes sense that we always ensure lowercase is used.

@sept-en
Copy link
Copy Markdown
Contributor Author

sept-en commented Jul 14, 2021

@birchmd yes, exactly. I guess this was explicitly made to eliminate even the possibility of such issues. For our case, it's expected that we can receive this as an input because users may specify ERC-20 checksummed address which contains both upper- and lower- case letters. But we need all those versions to point to the same ERC-20 on Aurora.

Created #180 for the purpose of checking the validity of all used AccountIds

@joshuajbouw
Copy link
Copy Markdown
Contributor

Still in draft, do you want that review?

Comment thread src/engine.rs Outdated
@sept-en sept-en marked this pull request as ready for review July 14, 2021 15:31
Comment thread src/engine.rs Outdated
Comment thread src/engine.rs Outdated
Comment thread src/engine.rs Outdated
Comment thread src/engine.rs Outdated
@joshuajbouw
Copy link
Copy Markdown
Contributor

In general, there needs to be better data handling that doesn't involve copying borrowed data first.

* Engine: Add `GetErc20FromNep141Error`, `RegisterTokenError` error types.
* Engine: `register_token()` forbid using invalid NEP-141 AccountId as
  input.
* Engine: `get_erc20_from_nep141()` forbid using invalid NEP-141 AccountId as
  input.
* Engine: `register_token()` add logs.
* Other minor improvements.
@sept-en
Copy link
Copy Markdown
Contributor Author

sept-en commented Jul 15, 2021

@birchmd @joshuajbouw @mfornet
Gents, please check the update! This contains custom error types and forbids providing invalid NEP-141 AccountID.

Copy link
Copy Markdown
Member

@birchmd birchmd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

I wonder if it would be nicer to validate the input closer to the boundary of the system and not have errors bubbling up from core logic. I think the PR is ok as-is, but just something that I thought I'd mention to see what others think.

With the aforementioned model the code would look something like

/// Can only be constructed with `ValidAccountId::from_bytes` which does validation.
/// Therefore if a function takes `ValidAccountId` it does not need to worry about invalid accounts.
struct ValidAccountId<'a>(&'a [u8]);
impl ValidAccountId {
    fn from_bytes(bytes: &[u8]) -> Option<Self> {
        if is_valid_account(bytes)  { Some(Self(bytes)) }
        else { None }
    }
}

pub fn register_token(&mut self, erc20_token: &[u8], nep141_token: ValidAccountId) { ... }
pub fn get_erc20_from_nep141(nep141_token: ValidAccountId) -> Option<Vec<u8>> { ... }

// lib.rs
fn deploy_erc20_token() {
    ...
    let nep141_token = ValidAccountId::from_bytes(&args.nep141).sdk_expect(ERR_INVALID_NEP141_ACCOUNT_ID);
    engine.register_token(address.as_bytes(), nep141_token).sdk_unwrap();
    ....
}

@sept-en
Copy link
Copy Markdown
Contributor Author

sept-en commented Jul 15, 2021

@birchmd thanks, this is a good suggestion! I wanted to do something similar in the scope of PR that will fix #180.
I agree with you as the model you mentioned will simplify the code, its usage, and readability.

@sept-en sept-en changed the title ERC-20: use lowercase NEP141 AccountID for mapping ERC-20: forbid using invalid NEP141 AccountID for mapping Jul 16, 2021
@joshuajbouw
Copy link
Copy Markdown
Contributor

@sept-en when you get back, please fix the conflicts. Thanks buddy.

@joshuajbouw joshuajbouw merged commit f8a4ee6 into develop Jul 30, 2021
@joshuajbouw joshuajbouw deleted the erc20-mapping branch July 30, 2021 14:13
artob added a commit that referenced this pull request Jul 30, 2021
* JSON: fix bugs and add unit tests. (#141)
* Add storage layout debug support for `EvmErc20.sol`. (#178)
* ERC-20: forbid using invalid NEP-141 AccountID for mapping. (#179)
* Add EIP-2930 support. (#181, #182)
* Migrate all workflows to self-hosted runners. (#185)
* Speed up the workflow using build caching. (#189)
* Use the new math API host functions. (#190)
* Fix `clippy::enum_variant_names` warning. (#192)
* Add different networks to the Makefile. (#193)
* Update the network status in the README. (#194)
* Remove the toolchain installation step in workflows. (#195)
* Run all tests for all networks in CI. (#196)
* Optimize for performance instead of code size. (#197)
* Parallelize the test suites. (#198)
* Add build-caching to the testing workflow. (#201)
* Refactor tests to use Signer. (#203)
* Add options to the bench profile. (#204)
* Remove a duplicate test. (#205)
* Add a sanity test for access list handling. (#206)
* Update nearcore to the latest branch.
* Add feature gates to the SDK's new host functions.

Co-authored-by: Ahmed Ali <ahmed@aurora.dev>
Co-authored-by: Dmitry Strokov <dmitry@aurora.dev>
Co-authored-by: Evgeny Ukhanov <evgeny@aurora.dev>
Co-authored-by: Joshua J. Bouw <joshua@aurora.dev>
Co-authored-by: Kirill <kirill@aurora.dev>
Co-authored-by: Michael Birch <michael@aurora.dev>
artob added a commit that referenced this pull request Aug 14, 2021
* ERC-20: forbid using invalid NEP141 AccountID for mapping (#179)
* Timestamp should be in milliseconds for Ethereum compatibility (#208)
* feat(engine): Blockhash definition (#213)
* Update etc/state-migration-test/Cargo.lock (#211)
* Include cost of access list in intrinsic gas (#219)
* Bump tar from 4.4.13 to 4.4.15 in /etc/eth-contracts (#217)
* Feat(engine): Relayer payment (#215)
* Scheduled lint is supposed to run nightly clippy (#214)
* Return actual status of a transaction (#218)
* Added parser for Integer types (#183)
* Update to latest nightly (#221)
* Fix(engine): do not panic when user has insufficient balance to cover gas (#223)
* Update lock files (#224)
* Method to fix balance of aurora account on testnet (#225)
* Use math api host functions on mainnet (#228)
* NEP-141 compliance correctness (#202)
* Adapt workflows to dockerized runners (#231)
* Move block height to the end of hashed data. (#233)
* Ensure solidity artifacts are always recompiled (#234)
* Prevent test binary from deploying (#237)
* Add removal of eth-contracts to `make clean`
* Remove deploy_code feature gate

Co-authored-by: Dmitry Strokov <dmitry@aurora.dev>
Co-authored-by: Evgeny Ukhanov <evgeny@aurora.dev>
Co-authored-by: Joshua J. Bouw <joshua@aurora.dev>
Co-authored-by: Kirill <kirill@aurora.dev>
Co-authored-by: Michael Birch <michael@aurora.dev>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-enhancement Category: New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants