Skip to content

Commit f00b39d

Browse files
committed
Fix hackatom address assumptions
1 parent b17169a commit f00b39d

File tree

2 files changed

+19
-24
lines changed

2 files changed

+19
-24
lines changed

contracts/hackatom/src/contract.rs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -195,22 +195,8 @@ fn do_user_errors_in_api_calls(api: &dyn Api) -> Result<Response, HackError> {
195195
}
196196
}
197197

198-
let too_long =
199-
"bn9hhssomeltvhzgvuqkwjkpwxojfuigltwedayzxljucefikuieillowaticksoistqoynmgcnj219aewfwefwwegwg";
200-
match api.addr_canonicalize(too_long).unwrap_err() {
201-
StdError::GenericErr { .. } => {}
202-
err => {
203-
return Err(StdError::generic_err(format!(
204-
"Unexpected error in do_user_errors_in_api_calls: {err:?}"
205-
))
206-
.into())
207-
}
208-
}
209-
210-
// Humanize
211-
212-
let empty: CanonicalAddr = vec![].into();
213-
match api.addr_humanize(&empty).unwrap_err() {
198+
let invalid = "bn9hhssomeltvhzgvuqkwjkpwxoj";
199+
match api.addr_canonicalize(invalid).unwrap_err() {
214200
StdError::GenericErr { .. } => {}
215201
err => {
216202
return Err(StdError::generic_err(format!(
@@ -220,8 +206,8 @@ fn do_user_errors_in_api_calls(api: &dyn Api) -> Result<Response, HackError> {
220206
}
221207
}
222208

223-
let too_short: CanonicalAddr = vec![0xAA, 0xBB, 0xCC].into();
224-
match api.addr_humanize(&too_short).unwrap_err() {
209+
let too_long = "cosmwasm1qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqehqqkz";
210+
match api.addr_canonicalize(too_long).unwrap_err() {
225211
StdError::GenericErr { .. } => {}
226212
err => {
227213
return Err(StdError::generic_err(format!(
@@ -231,8 +217,9 @@ fn do_user_errors_in_api_calls(api: &dyn Api) -> Result<Response, HackError> {
231217
}
232218
}
233219

234-
let wrong_length: CanonicalAddr = vec![0xA6; 17].into();
235-
match api.addr_humanize(&wrong_length).unwrap_err() {
220+
// Humanize
221+
let empty: CanonicalAddr = vec![].into();
222+
match api.addr_humanize(&empty).unwrap_err() {
236223
StdError::GenericErr { .. } => {}
237224
err => {
238225
return Err(StdError::generic_err(format!(

packages/std/src/testing/mock.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ impl Api for MockApi {
129129
if let Ok((prefix, decoded, Variant::Bech32)) = decode(input) {
130130
if prefix == self.bech32_prefix {
131131
if let Ok(bytes) = Vec::<u8>::from_base32(&decoded) {
132+
validate_length(&bytes)?;
132133
return Ok(bytes.into());
133134
}
134135
}
@@ -137,6 +138,7 @@ impl Api for MockApi {
137138
}
138139

139140
fn addr_humanize(&self, canonical: &CanonicalAddr) -> StdResult<Addr> {
141+
validate_length(canonical.as_ref())?;
140142
let Ok(encoded) = encode(
141143
self.bech32_prefix,
142144
canonical.as_slice().to_base32(),
@@ -249,6 +251,14 @@ impl MockApi {
249251
}
250252
}
251253

254+
/// Does basic validation of the number of bytes in a canonical address
255+
fn validate_length(bytes: &[u8]) -> StdResult<()> {
256+
if !(1..=255).contains(&bytes.len()) {
257+
return Err(StdError::generic_err("Invalid canonical address length"));
258+
}
259+
Ok(())
260+
}
261+
252262
/// Returns a default enviroment with height, time, chain_id, and contract address
253263
/// You can submit as is to most contracts, or modify height/time if you want to
254264
/// test for expiration.
@@ -1225,13 +1235,11 @@ mod tests {
12251235
}
12261236

12271237
#[test]
1238+
#[should_panic(expected = "Invalid canonical address length")]
12281239
fn addr_humanize_input_length() {
12291240
let api = MockApi::default();
12301241
let input = CanonicalAddr::from(vec![]);
1231-
assert_eq!(
1232-
api.addr_humanize(&input).unwrap(),
1233-
Addr::unchecked("cosmwasm1pj90vm")
1234-
);
1242+
api.addr_humanize(&input).unwrap();
12351243
}
12361244

12371245
// Basic "works" test. Exhaustive tests on VM's side (packages/vm/src/imports.rs)

0 commit comments

Comments
 (0)