Skip to content

Commit

Permalink
Fix panic in from_base58 fn
Browse files Browse the repository at this point in the history
  • Loading branch information
SWvheerden committed Jul 19, 2024
1 parent 47e3761 commit b1dea76
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 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,7 @@ 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
26 changes: 24 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,24 @@ 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
3 changes: 2 additions & 1 deletion common/src/build/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ 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

0 comments on commit b1dea76

Please sign in to comment.