From 11e627b58c30462555b8701b65faed08f88cefdd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 5 Apr 2022 17:47:29 +0100 Subject: [PATCH 1/3] Changed AccountId validation to check for minimum number of bytes --- cosmrs/src/base.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmrs/src/base.rs b/cosmrs/src/base.rs index 0930326f..4d7c745b 100644 --- a/cosmrs/src/base.rs +++ b/cosmrs/src/base.rs @@ -72,7 +72,7 @@ impl FromStr for AccountId { fn from_str(s: &str) -> Result { let (hrp, bytes) = bech32::decode(s).wrap_err("failed to decode bech32")?; - if bytes.len() == tendermint::account::LENGTH { + if bytes.len() >= tendermint::account::LENGTH { Ok(Self { bech32: s.to_owned(), hrp_length: hrp.len(), From b8f047935c5226827028cf6c742fef4b95ac8901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Thu, 7 Apr 2022 09:58:33 +0100 Subject: [PATCH 2/3] Updated AccountId parsing to more closely follow cosmos-sdk logic --- cosmrs/src/base.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cosmrs/src/base.rs b/cosmrs/src/base.rs index 4d7c745b..cfe5bb03 100644 --- a/cosmrs/src/base.rs +++ b/cosmrs/src/base.rs @@ -6,6 +6,9 @@ use serde::{de, de::Error as _, ser, Deserialize, Serialize}; use std::{fmt, str::FromStr}; use subtle_encoding::bech32; +/// Maximum allowed length (in bytes) for an address. +pub const MAX_ADDRESS_LENGTH: usize = 255; + /// Account identifiers #[derive(Clone, Eq, PartialEq, PartialOrd, Ord)] pub struct AccountId { @@ -72,7 +75,7 @@ impl FromStr for AccountId { fn from_str(s: &str) -> Result { let (hrp, bytes) = bech32::decode(s).wrap_err("failed to decode bech32")?; - if bytes.len() >= tendermint::account::LENGTH { + if bytes.len() <= MAX_ADDRESS_LENGTH { Ok(Self { bech32: s.to_owned(), hrp_length: hrp.len(), @@ -80,8 +83,8 @@ impl FromStr for AccountId { } else { Err(Error::AccountId { id: s.to_owned() }).wrap_err_with(|| { format!( - "account ID should be at least {} bytes long, but was {} bytes long", - tendermint::account::LENGTH, + "account ID should be at most {} bytes long, but was {} bytes long", + MAX_ADDRESS_LENGTH, bytes.len() ) }) From 2695cebead7b2da84646fd48ba1dee18a1d70425 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C4=99drzej=20Stuczy=C5=84ski?= Date: Tue, 12 Apr 2022 10:52:08 +0100 Subject: [PATCH 3/3] Applying suggestion to disallow empty addresses --- cosmrs/src/base.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cosmrs/src/base.rs b/cosmrs/src/base.rs index cfe5bb03..bd3819b3 100644 --- a/cosmrs/src/base.rs +++ b/cosmrs/src/base.rs @@ -75,7 +75,7 @@ impl FromStr for AccountId { fn from_str(s: &str) -> Result { let (hrp, bytes) = bech32::decode(s).wrap_err("failed to decode bech32")?; - if bytes.len() <= MAX_ADDRESS_LENGTH { + if matches!(bytes.len(), 1..=MAX_ADDRESS_LENGTH) { Ok(Self { bech32: s.to_owned(), hrp_length: hrp.len(),