module

Logarithm::Validation

Input validation and sanitization utilities for security and reliability.

This module provides comprehensive validation for all user inputs and configuration parameters. It implements security-first validation to prevent common attacks like path traversal, command injection, and resource exhaustion.

Security Considerations

The validation system protects against:

  • Path Traversal: Prevents ../../../etc/passwd style attacks
  • Command Injection: Blocks shell metacharacters in inputs
  • Resource Exhaustion: Validates numerical ranges to prevent DoS
  • Data Corruption: Ensures input formats match expected patterns

Validation Categories

  • Path Validation: File system paths for logs, models, and configuration
  • Duration Validation: Time specifications for training and monitoring
  • Numerical Validation: Thresholds, sizes, and other numerical parameters

Integration Points

Validation is used throughout the system:

  • CLI: Command-line argument validation
  • Config: Configuration file parsing and validation
  • Pipeline: Runtime parameter validation
  • Ingestion: Log source path validation

Error Handling

All validation failures raise ValidationError with descriptive messages that help users correct their inputs. This provides clear feedback for configuration issues and prevents silent failures.

Usage Examples

# Validate log file paths
Logarithm::Validation.validate_log_paths(["/var/log/auth.log", "/var/log/syslog"])

# Validate training duration
Logarithm::Validation.validate_duration("24h") # Valid
Logarithm::Validation.validate_duration("30x") # Raises ValidationError

# Validate anomaly threshold
Logarithm::Validation.validate_threshold(0.05) # Valid
Logarithm::Validation.validate_threshold(1.5)  # Raises ValidationError

Class methods

validate_duration(duration_str : String)

Validates duration strings used for training and monitoring periods.

Accepts duration formats: Nh (hours), Nd (days), Nm (minutes), Ns (seconds). Validates both format correctness and reasonable value ranges to prevent resource exhaustion attacks.

Parameters:

  • duration_str: Duration string in format "123h", "45m", "90s", etc.

Raises: ValidationError for invalid format or out-of-range values

Valid Ranges:

  • Hours: 1-8760 (1 year)
  • Days: 1-365 (1 year)
  • Minutes: 1-525600 (1 year)
  • Seconds: 1-86400 (1 day)

Examples:

Logarithm::Validation.validate_duration("24h")   # Valid: 24 hours
Logarithm::Validation.validate_duration("30m")   # Valid: 30 minutes
Logarithm::Validation.validate_duration("90s")   # Valid: 90 seconds
Logarithm::Validation.validate_duration("30x")   # Invalid: bad unit
Logarithm::Validation.validate_duration("9999h") # Invalid: too long
Source
validate_log_paths(paths : Array(String))

Validates an array of log file paths for security and accessibility.

Applies path validation to each path in the array, checking for:

  • Directory traversal attempts (.. in paths)
  • Invalid characters that could be used for injection
  • Paths that resolve to directory traversal after expansion

Parameters:

  • paths: Array of file paths to validate

Raises: ValidationError if any path fails validation

Example:

Logarithm::Validation.validate_log_paths([
  "/var/log/auth.log",
  "/var/log/syslog",
])
Source
validate_model_size(size : Int32)

Validates model size parameters to prevent resource exhaustion.

Model size affects memory usage and computational requirements. This validation prevents DoS attacks through excessively large models while ensuring models are large enough for effective anomaly detection.

Parameters:

  • size: Model size parameter (typically vocabulary size or layer dimensions)

Raises: ValidationError if size is outside valid range (1-100000)

Performance Impact:

  • Small models (< 1000): Fast but may miss complex patterns
  • Medium models (1000-10000): Good balance of performance and accuracy
  • Large models (10000-100000): High accuracy but increased resource usage
Source
validate_path(path : String)

Validates a single file system path for security violations.

Performs multiple security checks to prevent path-based attacks:

  1. Directory Traversal: Blocks paths containing .. segments
  2. Invalid Characters: Rejects paths with shell metacharacters <>\|
  3. Path Expansion: Validates the expanded absolute path for traversal

Parameters:

  • path: The file system path to validate

Raises: ValidationError with specific reason for validation failure

Security Notes:

  • Prevents access to files outside intended directories
  • Blocks command injection through path manipulation
  • Ensures paths are safe for file system operations
Source
validate_threshold(threshold : Float64)

Validates anomaly detection threshold values.

The threshold determines sensitivity for anomaly detection. Values closer to 0 are more sensitive (more false positives), while values closer to 1 are less sensitive (more false negatives).

Parameters:

  • threshold: Threshold value between 0.0 and 1.0

Raises: ValidationError if threshold is outside valid range

Typical Ranges:

  • 0.01-0.05: High sensitivity (catches more anomalies)
  • 0.05-0.10: Balanced sensitivity
  • 0.10-0.20: Low sensitivity (fewer false positives)
Source