Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate namespace names before setting them #225

Merged
merged 2 commits into from
Jan 8, 2024
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
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 @@
}
}

// 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 Expand Up @@ -298,7 +319,7 @@
let range_str = if range.is_empty() {
"Empty range".to_string()
} else {
let mut r = format!("{} {}", range.len(), range.get(0).unwrap().type_str());

Check failure on line 322 in src/env.rs

View workflow job for this annotation

GitHub Actions / Clippy

accessing first element with `range.get(0)`
if range.len() > 1 {
r.push('s');
}
Expand Down
Loading