From 260bd42edbb4049a7e6ab033d2674d42738d8cd4 Mon Sep 17 00:00:00 2001 From: Hasnain Lakhani Date: Fri, 5 Jan 2024 15:07:15 -0800 Subject: [PATCH 1/2] working --- src/env.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/env.rs b/src/env.rs index fe59f3d..17ce3d6 100644 --- a/src/env.rs +++ b/src/env.rs @@ -189,8 +189,30 @@ 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)" + ); + io::stdout().flush().expect("Could not flush stdout"); + 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 From 33ff590207a2e554f1e1651fdd12a538e15b3bee Mon Sep 17 00:00:00 2001 From: Hasnain Lakhani Date: Fri, 5 Jan 2024 15:32:47 -0800 Subject: [PATCH 2/2] don't need to flush stdout --- src/env.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/env.rs b/src/env.rs index 17ce3d6..2e70413 100644 --- a/src/env.rs +++ b/src/env.rs @@ -209,7 +209,6 @@ impl Env { 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)" ); - io::stdout().flush().expect("Could not flush stdout"); return; } }