From 95d8b329a233b0fb7be7e74cb1daebe47b2c78d6 Mon Sep 17 00:00:00 2001 From: Elizabeth Engelman <4752801+elizabethengelman@users.noreply.github.com> Date: Mon, 23 Dec 2024 09:47:44 -0500 Subject: [PATCH] Limit key name length --- cmd/soroban-cli/src/config/address.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/cmd/soroban-cli/src/config/address.rs b/cmd/soroban-cli/src/config/address.rs index 36cc90f42..dadd8853f 100644 --- a/cmd/soroban-cli/src/config/address.rs +++ b/cmd/soroban-cli/src/config/address.rs @@ -28,7 +28,11 @@ pub enum Error { Secret(#[from] secret::Error), #[error("Address cannot be used to sign {0}")] CannotSign(xdr::MuxedAccount), - #[error("Invalid key name: {0}\n only alphanumeric characters, `_`and `-` are allowed")] + #[error("Invalid key name: {0}\n only alphanumeric characters, underscores (_), and hyphens (-) are allowed.")] + InvalidKeyNameCharacters(String), + #[error("Invalid key name: {0}\n keys cannot exceed 250 characters")] + InvalidKeyNameLength(String), + #[error("Invalid key name: {0}\n keys cannot be the word \"ledger\"")] InvalidKeyName(String), } @@ -93,11 +97,14 @@ impl std::str::FromStr for KeyName { type Err = Error; fn from_str(s: &str) -> Result { if !s.chars().all(allowed_char) { - return Err(Error::InvalidKeyName(s.to_string())); + return Err(Error::InvalidKeyNameCharacters(s.to_string())); } if s == "ledger" { return Err(Error::InvalidKeyName(s.to_string())); } + if s.len() > 250 { + return Err(Error::InvalidKeyNameLength(s.to_string())); + } Ok(KeyName(s.to_string())) } }