Skip to content
Merged
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
47 changes: 44 additions & 3 deletions src/key_name.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::*;

static NAME_RE: LazyLock<Regex> =
LazyLock::new(|| Regex::new("^[A-Za-z0-9][A-Za-z0-9._-]*$").unwrap());
static NAME_RE: LazyLock<Regex> = LazyLock::new(|| Regex::new("^[a-z0-9]+(-[a-z0-9]+)*$").unwrap());

#[derive(Clone, Debug, PartialEq)]
pub struct KeyName(String);
Expand All @@ -20,7 +19,7 @@ impl FromStr for KeyName {
type Err = PublicKeyError;

fn from_str(name: &str) -> Result<Self, Self::Err> {
if !NAME_RE.is_match(name) {
if !NAME_RE.is_match(name) || name.len() > 128 {
return Err(public_key_error::NameError { name }.build());
}

Expand All @@ -33,3 +32,45 @@ impl Display for KeyName {
write!(f, "{}", self.0)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn validity() {
#[track_caller]
fn valid(s: &str) {
assert!(s.parse::<KeyName>().is_ok());
}

#[track_caller]
fn invalid(s: &str) {
assert!(s.parse::<KeyName>().is_err());
}

valid("0");
valid("0-0");
valid("foo");
valid("foo-bar");
valid("foo-bar-baz");
valid(&"a".repeat(128));

invalid("");
invalid("-");
invalid("--");
invalid(".");
invalid("..");
invalid("/");
invalid("FOO");
invalid("\\");
invalid("_");
invalid("foo--bar");
invalid("foo--bar");
invalid("foo-bar-");
invalid("foo.");
invalid("foo.bar");
invalid("foo_bar");
invalid(&"a".repeat(129));
}
}