diff --git a/sdk/src/signature.rs b/sdk/src/signature.rs index 278e2ca65d..2e63bf90bb 100644 --- a/sdk/src/signature.rs +++ b/sdk/src/signature.rs @@ -58,6 +58,11 @@ impl Keypair { } } +/// Number of bytes in a signature +pub const SIGNATURE_BYTES: usize = 64; +/// Maximum string length of a base58 encoded signature +const MAX_BASE58_SIGNATURE_LEN: usize = 88; + #[repr(transparent)] #[derive( Serialize, Deserialize, Clone, Copy, Default, Eq, PartialEq, Ord, PartialOrd, Hash, AbiExample, @@ -138,6 +143,9 @@ impl FromStr for Signature { type Err = ParseSignatureError; fn from_str(s: &str) -> Result { + if s.len() > MAX_BASE58_SIGNATURE_LEN { + return Err(ParseSignatureError::WrongSize); + } let bytes = bs58::decode(s) .into_vec() .map_err(|_| ParseSignatureError::Invalid)?; @@ -521,6 +529,16 @@ mod tests { signature_base58_str.parse::(), Err(ParseSignatureError::Invalid) ); + + // too long input string + // longest valid encoding + let mut too_long = bs58::encode(&[255u8; SIGNATURE_BYTES]).into_string(); + // and one to grow on + too_long.push('1'); + assert_eq!( + too_long.parse::(), + Err(ParseSignatureError::WrongSize) + ); } #[test]