Skip to content

Commit

Permalink
Merge pull request #225 from hasnain-db/master
Browse files Browse the repository at this point in the history
Validate namespace names before setting them
  • Loading branch information
nicklan authored Jan 8, 2024
2 parents f72c16a + 33ff590 commit 9b8643d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,29 @@ impl Env {
}
}

// a lowercase RFC 1123 label must consist of lower case alphanumeric characters or '-', and must start and end with an alphanumeric character. Max length is 63.
fn validate_rfc_1123_label(label: &str) -> bool {
if label.is_empty() || label.len() > 63 {
return false;
}
label
.chars()
.all(|c| (c.is_ascii_lowercase() && c.is_ascii_alphanumeric()) || c == '-')
&& label.chars().next().unwrap().is_ascii_alphanumeric()
&& label.chars().last().unwrap().is_ascii_alphanumeric()
}

pub fn set_namespace(&mut self, namespace: Option<&str>) {
let mut do_clear = false;
if let Some(ns) = namespace {
if !Env::validate_rfc_1123_label(ns) {
clickwriteln!(
io::stderr(),
"Invalid namespace name. Namespaces must be valid RFC 1123 labels (less than 64 characters, lowercase alphanumeric or '-', and start and end with an alphanumeric character)"
);
return;
}
}
if let (Some(my_ns), Some(new_ns)) = (&self.namespace, namespace) {
if my_ns.as_str() != new_ns {
do_clear = true; // need to use bool since self is borrowed here
Expand Down

0 comments on commit 9b8643d

Please sign in to comment.