Skip to content

Commit

Permalink
fix: validate account Ids from args (#703)
Browse files Browse the repository at this point in the history
* fix: validate account Ids from args

* feat: use borshDeserializer for account id validation

* chore: do not validate default (empty) account id while deserializing

* fix: make the test panic in case of invalid account id

* fix: clippy error

---------

Co-authored-by: Oleksandr Anyshchenko <oleksandr.anyshchenko@aurora.dev>
Co-authored-by: Joshua J. Bouw <joshua@aurora.dev>
  • Loading branch information
3 people authored and birchmd committed Apr 5, 2023
1 parent b9d5da0 commit 6a9d961
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
8 changes: 8 additions & 0 deletions engine-tests/src/tests/contract_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ fn setup_test() -> (AuroraRunner, Signer, Address, Tester) {
(runner, signer, token, tester)
}

#[test]
#[should_panic]
fn test_deploy_erc20_token_with_invalid_account_id() {
let mut runner = AuroraRunner::new();
let invalid_nep141 = "_";
runner.deploy_erc20_token(invalid_nep141);
}

#[test]
fn hello_world_solidity() {
let (mut runner, mut signer, _token, tester) = setup_test();
Expand Down
20 changes: 16 additions & 4 deletions engine-types/src/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
//!
//! Inspired by: `https://github.com/near/nearcore/tree/master/core/account-id`
use crate::{fmt, str, str::FromStr, Box, String, Vec};
use borsh::{BorshDeserialize, BorshSerialize};
use crate::{fmt, str, str::FromStr, Box, String, ToString, Vec};
use borsh::{maybestd::io, BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};

pub const MIN_ACCOUNT_ID_LEN: usize = 2;
Expand All @@ -13,11 +13,10 @@ pub const MAX_ACCOUNT_ID_LEN: usize = 64;
///
/// This guarantees all properly constructed `AccountId`'s are valid for the NEAR network.
#[derive(
Default,
BorshSerialize,
BorshDeserialize,
Serialize,
Deserialize,
Default,
Eq,
Ord,
Hash,
Expand Down Expand Up @@ -96,6 +95,19 @@ impl AccountId {
}
}

impl BorshDeserialize for AccountId {
fn deserialize(buf: &mut &[u8]) -> io::Result<Self> {
let account: String = BorshDeserialize::deserialize(buf)?;

// It's for saving backward compatibility.
if account.is_empty() {
return Ok(Self::default());
}

Self::new(&account).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e.to_string()))
}
}

impl TryFrom<String> for AccountId {
type Error = ParseAccountError;

Expand Down

0 comments on commit 6a9d961

Please sign in to comment.