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

fix: panic in from_base58 fn #6414

Merged
merged 2 commits into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion applications/minotari_merge_mining_proxy/src/proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,11 @@ impl InnerService {
.iter()
.position(|x| x == &last_used_url)
.unwrap_or(0);
let (left, right) = self.config.monerod_url.split_at(pos);
let (left, right) = self
.config
.monerod_url
.split_at_checked(pos)
.ok_or(MmProxyError::ConversionError("Invalid utf 8 url".to_string()))?;
let left = left.to_vec();
let right = right.to_vec();
let iter = right.iter().chain(left.iter()).chain(right.iter()).chain(left.iter());
Expand Down
4 changes: 2 additions & 2 deletions base_layer/common_types/src/tari_address/dual_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,8 +162,8 @@ impl DualAddress {
if hex_str.len() != 90 && hex_str.len() != 91 {
return Err(TariAddressError::InvalidSize);
}
let (first, rest) = hex_str.split_at(2);
let (network, features) = first.split_at(1);
let (first, rest) = hex_str.split_at_checked(2).ok_or(TariAddressError::InvalidCharacter)?;
let (network, features) = first.split_at_checked(1).ok_or(TariAddressError::InvalidCharacter)?;
let mut result = bs58::decode(network)
.into_vec()
.map_err(|_| TariAddressError::CannotRecoverNetwork)?;
Expand Down
27 changes: 25 additions & 2 deletions base_layer/common_types/src/tari_address/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ pub enum TariAddressError {
InvalidChecksum,
#[error("Invalid emoji character")]
InvalidEmoji,
#[error("Invalid text character")]
InvalidCharacter,
#[error("Cannot recover public key")]
CannotRecoverPublicKey,
#[error("Cannot recover network")]
Expand Down Expand Up @@ -246,8 +248,8 @@ impl TariAddress {
if hex_str.len() < 47 {
return Err(TariAddressError::InvalidSize);
}
let (first, rest) = hex_str.split_at(2);
let (network, features) = first.split_at(1);
let (first, rest) = hex_str.split_at_checked(2).ok_or(TariAddressError::InvalidCharacter)?;
let (network, features) = first.split_at_checked(1).ok_or(TariAddressError::InvalidCharacter)?;
let mut result = bs58::decode(network)
.into_vec()
.map_err(|_| TariAddressError::CannotRecoverNetwork)?;
Expand Down Expand Up @@ -820,4 +822,25 @@ mod test {
Err(TariAddressError::CannotRecoverPublicKey)
);
}

#[test]
/// Test invalid emoji
fn invalid_utf8_char() {
// This emoji string contains an invalid utf8 character
let emoji_string = "🦊 | 🦊 | 🦊 | 🦊 | 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | \
🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | 🦊| 🦊 | \
🦊| 🦊 | 🦊| 🦊 | 🦊";
assert_eq!(
TariAddress::from_base58(emoji_string),
Err(TariAddressError::InvalidCharacter)
);
assert_eq!(
TariAddress::from_emoji_string(emoji_string),
Err(TariAddressError::InvalidSize)
);
assert_eq!(
TariAddress::from_str(emoji_string),
Err(TariAddressError::InvalidAddressString)
);
}
}
4 changes: 2 additions & 2 deletions base_layer/common_types/src/tari_address/single_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ impl SingleAddress {
if hex_str.len() != 46 && hex_str.len() != 47 && hex_str.len() != 48 {
return Err(TariAddressError::InvalidSize);
}
let (first, rest) = hex_str.split_at(2);
let (network, features) = first.split_at(1);
let (first, rest) = hex_str.split_at_checked(2).ok_or(TariAddressError::InvalidCharacter)?;
let (network, features) = first.split_at_checked(1).ok_or(TariAddressError::InvalidCharacter)?;
let mut result = bs58::decode(network)
.into_vec()
.map_err(|_| TariAddressError::CannotRecoverNetwork)?;
Expand Down
6 changes: 5 additions & 1 deletion base_layer/wallet_ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,11 @@ impl From<TariAddressError> for LibWalletError {
message: format!("{:?}", e),
},
TariAddressError::InvalidAddressString => Self {
code: 706,
code: 707,
message: format!("{:?}", e),
},
TariAddressError::InvalidCharacter => Self {
code: 708,
message: format!("{:?}", e),
},
}
Expand Down
5 changes: 4 additions & 1 deletion common/src/build/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ fn get_commit() -> Result<String, anyhow::Error> {
let repo = git2::Repository::open(git_root)?;
let head = repo.revparse_single("HEAD")?;
let id = format!("{:?}", head.id());
id.split_at(7).0.to_string();
id.split_at_checked(7)
.ok_or(anyhow::anyhow!("invalid utf8 in commit id"))?
.0
.to_string();
Ok(id)
}

Expand Down
Loading