diff options
Diffstat (limited to 'crates/core/c8y_smartrest/src/operations.rs')
-rw-r--r-- | crates/core/c8y_smartrest/src/operations.rs | 51 |
1 files changed, 39 insertions, 12 deletions
diff --git a/crates/core/c8y_smartrest/src/operations.rs b/crates/core/c8y_smartrest/src/operations.rs index 87e6aee9..ea0202b7 100644 --- a/crates/core/c8y_smartrest/src/operations.rs +++ b/crates/core/c8y_smartrest/src/operations.rs @@ -48,6 +48,16 @@ pub struct Operations { operations_by_trigger: HashMap<String, usize>, } +/// depending on which editor you use, temporary files could be created that contain the name of +/// the file. +/// this `operation_name_is_valid` fn will ensure that only files that do not contain +/// any special characters are allowed. +pub fn is_valid_operation_name(operation: &str) -> bool { + operation + .chars() + .all(|c| c.is_ascii_alphabetic() || c.is_numeric() || c.eq(&'_')) +} + impl Operations { pub fn add_operation(&mut self, operation: Operation) { if self.operations.iter().any(|o| o.name.eq(&operation.name)) { @@ -106,23 +116,28 @@ fn get_operations(dir: impl AsRef<Path>, cloud_name: &str) -> Result<Operations, .collect::<Vec<PathBuf>>(); for path in dir_entries { - let mut details = match fs::read(&path) { - Ok(bytes) => toml::from_slice::<Operation>(bytes.as_slice()) - .map_err(|e| OperationsError::TomlError(path.to_path_buf(), e))?, + if let Some(file_name) = path.file_name().and_then(|file_name| file_name.to_str()) { + if !is_valid_operation_name(file_name) { + continue; + } + + let mut details = match fs::read(&path) { + Ok(bytes) => toml::from_slice::<Operation>(bytes.as_slice()) + .map_err(|e| OperationsError::TomlError(path.to_path_buf(), e))?, - Err(err) => return Err(OperationsError::FromIo(err)), - }; + Err(err) => return Err(OperationsError::FromIo(err)), + }; - details.name = path - .file_name() - .and_then(|filename| filename.to_str()) - .ok_or_else(|| OperationsError::InvalidOperationName(path.to_owned()))? - .to_owned(); - operations.add_operation(details); + details.name = path + .file_name() + .and_then(|filename| filename.to_str()) + .ok_or_else(|| OperationsError::InvalidOperationName(path.to_owned()))? + .to_owned(); + operations.add_operation(details); + } } Ok(operations) } - pub fn get_operation(path: PathBuf) -> Result<Operation, OperationsError> { let mut details = match fs::read(&path) { Ok(bytes) => toml::from_slice::<Operation>(bytes.as_slice()) @@ -231,4 +246,16 @@ mod tests { assert_eq!(operations.operations.len(), ops_count); } + + #[test_case("file_a?", false)] + #[test_case("~file_b", false)] + #[test_case("c8y_Command", true)] + #[test_case("c8y_CommandA~", false)] + #[test_case(".c8y_CommandB", false)] + #[test_case("c8y_CommandD?", false)] + #[test_case("c8y_CommandE?!£$%^&*(", false)] + #[test_case("?!£$%^&*(c8y_CommandF?!£$%^&*(", false)] + fn operation_name_should_contain_only_alphabetic_chars(operation: &str, expected_result: bool) { + assert_eq!(is_valid_operation_name(operation), expected_result) + } } |