ConfigValidator

Struct ConfigValidator 

Source
pub struct ConfigValidator;
Expand description

Security validation utilities for task configuration

Implementations§

Source§

impl ConfigValidator

Source

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 has leading or trailing whitespace
  • Command length exceeds maximum allowed length
§Examples
use tcrm_task::tasks::validator::ConfigValidator;

let valid_command = "echo";
assert!(ConfigValidator::validate_command(valid_command).is_ok());

let invalid_command = "";
assert!(ConfigValidator::validate_command(invalid_command).is_err());
Source

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());
Source

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());
Source

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());
Source

pub fn validate_ready_indicator(indicator: &str) -> Result<(), TaskError>

Validates ready indicator string

§Arguments
  • indicator - The ready indicator string to validate
§Returns
  • Ok(()) if the indicator is valid
  • Err(TaskError::InvalidConfiguration) if the indicator is invalid
§Errors

Returns TaskError::InvalidConfiguration if:

  • Indicator is empty
§Examples
use tcrm_task::tasks::validator::ConfigValidator;

let valid_indicator = "ready";
assert!(ConfigValidator::validate_ready_indicator(valid_indicator).is_ok());

let invalid_indicator = "";
assert!(ConfigValidator::validate_ready_indicator(invalid_indicator).is_err());
Source

pub fn validate_timeout(timeout: &u64) -> Result<(), TaskError>

Validates timeout value.

Ensures that the timeout value is greater than 0. A timeout of 0 would mean immediate timeout, which is not useful for task execution.

§Arguments
  • timeout - The timeout value in milliseconds to validate
§Returns
  • Ok(()) - If timeout is valid (greater than 0)
  • Err(TaskError) - If timeout is 0
§Example
use tcrm_task::tasks::validator::ConfigValidator;

// Valid timeout
assert!(ConfigValidator::validate_timeout(&5000).is_ok());

// Invalid timeout (0)
assert!(ConfigValidator::validate_timeout(&0).is_err());
Source

pub fn contains_obvious_injection(input: &str) -> bool

Checks for obvious injection attempts while allowing normal shell features.

This method identifies clearly malicious patterns without blocking legitimate shell functionality. It focuses on patterns that are rarely used in normal command execution.

§Arguments
  • input - The command string to check for injection patterns.
§Returns

true if obvious injection patterns are detected, false otherwise.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.