summaryrefslogtreecommitdiffhomepage
path: root/mullvad-cli/src
diff options
context:
space:
mode:
authorJoakim Hulthe <joakim@hulthe.net>2024-06-12 14:42:47 +0200
committerOskar Nyberg <oskar@mullvad.net>2024-07-04 15:07:26 +0200
commit771e2506eaa9fe26dcac0258e717397f69d42314 (patch)
treea3de05406d376de209d9b65cb3995e928b3588c2 /mullvad-cli/src
parent3638342c117109f00b29bd0f43c889f8dd152b9d (diff)
downloadmullvadvpn-771e2506eaa9fe26dcac0258e717397f69d42314.tar.xz
mullvadvpn-771e2506eaa9fe26dcac0258e717397f69d42314.zip
Trim custom list name and limit len to 30 in cli
Diffstat (limited to 'mullvad-cli/src')
-rw-r--r--mullvad-cli/src/cmds/custom_list.rs19
1 files changed, 19 insertions, 0 deletions
diff --git a/mullvad-cli/src/cmds/custom_list.rs b/mullvad-cli/src/cmds/custom_list.rs
index 47e48c0c5f..edc6b4b0e2 100644
--- a/mullvad-cli/src/cmds/custom_list.rs
+++ b/mullvad-cli/src/cmds/custom_list.rs
@@ -6,11 +6,15 @@ use mullvad_types::{
constraints::Constraint, relay_constraints::GeographicLocationConstraint, relay_list::RelayList,
};
+/// Custom list length, expressed as a number of UTF8 codepoints (i.e. chars).
+pub const CUSTOM_LIST_MAX_LEN: usize = 30;
+
#[derive(Subcommand, Debug)]
pub enum CustomList {
/// Create a new custom list
New {
/// A name for the new custom list
+ #[clap(value_parser = parse_custom_list_name)]
name: String,
},
@@ -55,7 +59,9 @@ pub enum EditCommand {
Rename {
/// Current name of the custom list
name: String,
+
/// A new name for the custom list
+ #[clap(value_parser = parse_custom_list_name)]
new_name: String,
},
}
@@ -259,3 +265,16 @@ pub async fn find_list_by_name(
.find(|list| list.name == name)
.ok_or(anyhow!("List not found"))
}
+
+/// Trim the string and validate the length against [CUSTOM_LIST_MAX_LEN].
+// NOTE: should only be used when *creating* custom lists, as we don't want to make it impossible
+// to reference any custom lists created before the max length and whitespace restrictions were put
+// in place.
+fn parse_custom_list_name(s: &str) -> Result<String> {
+ let s = s.trim();
+ let length = s.chars().count();
+ if length > CUSTOM_LIST_MAX_LEN {
+ bail!("Provided name is too long, {length}/{CUSTOM_LIST_MAX_LEN} characters.");
+ }
+ Ok(s.to_string())
+}