Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed AccountId validation to check for maximum number of bytes #197

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions cosmrs/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -72,16 +75,16 @@ impl FromStr for AccountId {
fn from_str(s: &str) -> Result<Self> {
let (hrp, bytes) = bech32::decode(s).wrap_err("failed to decode bech32")?;

if bytes.len() == tendermint::account::LENGTH {
if bytes.len() <= MAX_ADDRESS_LENGTH {
Copy link
Member

Choose a reason for hiding this comment

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

It looks like this allows empty addresses. How about:

Suggested change
if bytes.len() == tendermint::account::LENGTH {
if bytes.len() <= MAX_ADDRESS_LENGTH {
if matches!(bytes.len(), 1..=MAX_ADDRESS_LENGTH) {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I love it

Ok(Self {
bech32: s.to_owned(),
hrp_length: hrp.len(),
})
} 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()
)
})
Expand Down