From 669b3532519966b57c80a6afa0b22afb47878456 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Tue, 28 May 2024 12:25:56 -0400 Subject: [PATCH] added max=32 validation to contract_addr_len --- contracts/main/voice/src/contract.rs | 6 ++++ contracts/main/voice/src/error.rs | 3 ++ contracts/main/voice/src/suite_tests/suite.rs | 7 +++- contracts/main/voice/src/suite_tests/tests.rs | 32 +++++++++++++++++++ 4 files changed, 47 insertions(+), 1 deletion(-) diff --git a/contracts/main/voice/src/contract.rs b/contracts/main/voice/src/contract.rs index ea90146..15996fb 100644 --- a/contracts/main/voice/src/contract.rs +++ b/contracts/main/voice/src/contract.rs @@ -41,6 +41,9 @@ pub fn instantiate( if contract_addr_len == 0 { return Err(ContractError::ContractAddrLenCantBeZero); } + if contract_addr_len > 32 { + return Err(ContractError::ContractAddrLenCantBeGreaterThan32); + } PROXY_CODE_ID.save(deps.storage, &msg.proxy_code_id.u64())?; BLOCK_MAX_GAS.save(deps.storage, &msg.block_max_gas.u64())?; @@ -222,6 +225,9 @@ pub fn migrate(deps: DepsMut, _env: Env, msg: MigrateMsg) -> Result 32 { + return Err(ContractError::ContractAddrLenCantBeGreaterThan32); + } // update the proxy code ID, block max gas, and contract addr len PROXY_CODE_ID.save(deps.storage, &proxy_code_id.u64())?; diff --git a/contracts/main/voice/src/error.rs b/contracts/main/voice/src/error.rs index 6e65290..920501a 100644 --- a/contracts/main/voice/src/error.rs +++ b/contracts/main/voice/src/error.rs @@ -27,4 +27,7 @@ pub enum ContractError { #[error("Contract address length can't be zero")] ContractAddrLenCantBeZero, + + #[error("Contract address length can't be greater than 32")] + ContractAddrLenCantBeGreaterThan32, } diff --git a/contracts/main/voice/src/suite_tests/suite.rs b/contracts/main/voice/src/suite_tests/suite.rs index c899d7f..9518d90 100644 --- a/contracts/main/voice/src/suite_tests/suite.rs +++ b/contracts/main/voice/src/suite_tests/suite.rs @@ -73,6 +73,11 @@ impl SuiteBuilder { self.instantiate.proxy_code_id = code_id; self } + + pub fn with_contract_addr_len(mut self, len: Option) -> Self { + self.instantiate.contract_addr_len = len; + self + } } impl Suite { @@ -120,7 +125,7 @@ impl Suite { &MigrateMsg::WithUpdate { proxy_code_id: contract_code_id.into(), block_max_gas: block_max_gas.into(), - contract_addr_len: contract_addr_len, + contract_addr_len, }, self.voice_code, ) diff --git a/contracts/main/voice/src/suite_tests/tests.rs b/contracts/main/voice/src/suite_tests/tests.rs index df2e276..68d9e95 100644 --- a/contracts/main/voice/src/suite_tests/tests.rs +++ b/contracts/main/voice/src/suite_tests/tests.rs @@ -79,6 +79,22 @@ fn test_gas_validation() { .build(); } +#[test] +#[should_panic] +fn test_contract_addr_len_min_validation() { + SuiteBuilder::default() + .with_contract_addr_len(Some(0)) + .build(); +} + +#[test] +#[should_panic] +fn test_contract_addr_len_max_validation() { + SuiteBuilder::default() + .with_contract_addr_len(Some(33)) + .build(); +} + #[test] fn test_migrate_validation() { let mut suite = SuiteBuilder::default().build(); @@ -98,4 +114,20 @@ fn test_migrate_validation() { .unwrap(); assert_eq!(err, ContractError::GasLimitsMismatch); + + let err = suite + .update(Addr::unchecked(CREATOR_ADDR), 1, 110_000, 0) + .unwrap_err() + .downcast::() + .unwrap(); + + assert_eq!(err, ContractError::ContractAddrLenCantBeZero); + + let err = suite + .update(Addr::unchecked(CREATOR_ADDR), 1, 110_000, 33) + .unwrap_err() + .downcast::() + .unwrap(); + + assert_eq!(err, ContractError::ContractAddrLenCantBeGreaterThan32); }