pub struct ConfigValidator;
Expand description
Security validation utilities for task configuration
Implementations§
Source§impl ConfigValidator
impl ConfigValidator
Sourcepub fn validate_command(command: &str) -> Result<(), TaskError>
pub fn validate_command(command: &str) -> Result<(), TaskError>
Validates command name.
§Arguments
command
- The command string to validate.
§Returns
Ok(())
if the command is valid.Err(TaskError::InvalidConfiguration)
if the command is invalid.
§Errors
Returns a TaskError::InvalidConfiguration
if:
- Command is empty or contains only whitespace
- Command contains null bytes or obvious injection patterns
- Command has leading or trailing whitespace
- Command length exceeds maximum allowed length
§Examples
use tcrm_task::tasks::validator::ConfigValidator;
let valid_command = "echo Hello";
assert!(ConfigValidator::validate_command(valid_command).is_ok());
let invalid_command = "\0";
assert!(ConfigValidator::validate_command(invalid_command).is_err());
Sourcepub fn validate_args(args: &[String]) -> Result<(), TaskError>
pub fn validate_args(args: &[String]) -> Result<(), TaskError>
Validates arguments.
§Arguments
args
- A slice of argument strings to validate.
§Returns
Ok(())
if all arguments are valid.Err(TaskError::InvalidConfiguration)
if any argument is invalid.
§Errors
Returns a TaskError::InvalidConfiguration
if:
- any argument contains null bytes
- any argument is an empty string
- any argument has leading or trailing whitespace
- any argument exceeds maximum length
§Examples
use tcrm_task::tasks::validator::ConfigValidator;
let valid_args = vec!["arg1".to_string(), "arg2".to_string()];
assert!(ConfigValidator::validate_args(&valid_args).is_ok());
let invalid_args = vec!["arg1".to_string(), "\0".to_string()];
assert!(ConfigValidator::validate_args(&invalid_args).is_err());
Sourcepub fn validate_working_dir(dir: &str) -> Result<(), TaskError>
pub fn validate_working_dir(dir: &str) -> Result<(), TaskError>
Validates the working directory path.
§Arguments
dir
- The directory path to validate.
§Returns
Ok(())
if the directory is valid.Err(TaskError::InvalidConfiguration)
if the directory is invalid.
§Errors
Returns a TaskError::InvalidConfiguration
if:
- Directory does not exist
- Path exists but is not a directory
- Directory has leading or trailing whitespace
- Directory path exceeds maximum length
§Examples
use tcrm_task::tasks::validator::ConfigValidator;
use std::env;
// Test with current directory (should exist)
let current_dir = env::current_dir().unwrap();
let valid_dir = current_dir.to_str().unwrap();
assert!(ConfigValidator::validate_working_dir(valid_dir).is_ok());
// Test with nonexistent directory
let invalid_dir = "nonexistent_dir_12345";
assert!(ConfigValidator::validate_working_dir(invalid_dir).is_err());
Sourcepub fn validate_env_vars(env: &HashMap<String, String>) -> Result<(), TaskError>
pub fn validate_env_vars(env: &HashMap<String, String>) -> Result<(), TaskError>
Validates environment variables.
§Arguments
env
- A hashmap of environment variable key-value pairs to validate.
§Returns
Ok(())
if all environment variables are valid.Err(TaskError::InvalidConfiguration)
if any environment variable is invalid.
§Errors
Returns a TaskError::InvalidConfiguration
if:
- Any environment variable key contains spaces, ‘=’, or null bytes
- Any environment variable value contains null bytes
- Any environment variable key/value exceeds maximum length
§Examples
use tcrm_task::tasks::validator::ConfigValidator;
use std::collections::HashMap;
let mut valid_env = HashMap::new();
valid_env.insert("KEY".to_string(), "VALUE".to_string());
assert!(ConfigValidator::validate_env_vars(&valid_env).is_ok());
let mut invalid_env = HashMap::new();
invalid_env.insert("KEY\0".to_string(), "VALUE".to_string());
assert!(ConfigValidator::validate_env_vars(&invalid_env).is_err());
Sourcepub fn validate_command_strict(command: &str) -> Result<(), TaskError>
pub fn validate_command_strict(command: &str) -> Result<(), TaskError>
Validates command with strict security rules for untrusted input sources.
This is an alternative to validate_command
that blocks all shell features
§Arguments
command
- The command string to validate strictly.
§Returns
Ok(())
if the command passes strict validation.Err(TaskError::InvalidConfiguration)
if the command contains potentially dangerous patterns.
§Errors
Returns a TaskError::InvalidConfiguration
if:
- Command is empty or contains only whitespace
- Command contains shell metacharacters or redirection operators
§Examples
use tcrm_task::tasks::validator::ConfigValidator;
// Simple command should pass
let simple_command = "echo";
assert!(ConfigValidator::validate_command_strict(simple_command).is_ok());
// Command with shell features should fail
let shell_command = "echo hello; rm -rf /";
assert!(ConfigValidator::validate_command_strict(shell_command).is_err());
Auto Trait Implementations§
impl Freeze for ConfigValidator
impl RefUnwindSafe for ConfigValidator
impl Send for ConfigValidator
impl Sync for ConfigValidator
impl Unpin for ConfigValidator
impl UnwindSafe for ConfigValidator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more