Class: Makit::Commands::Middleware::Validator
- Inherits:
-
Object
- Object
- Makit::Commands::Middleware::Validator
- Defined in:
- lib/makit/commands/middleware/validator.rb
Overview
Validation middleware that provides input sanitization and security checks for command execution contexts.
This middleware validates and sanitizes:
-
Command strings for shell injection attacks
-
File paths for directory traversal
-
URLs for malicious schemes
-
Environment variables for sensitive data
-
Arguments for dangerous patterns
Constant Summary collapse
- DEFAULT_BLOCKED_COMMANDS =
Default blocked commands that pose security risks
%w[ rm rmdir sudo su chmod chown wget curl eval exec python ruby node ssh scp rsync dd fdisk mount umount iptables ufw systemctl service crontab passwd ].freeze
- DANGEROUS_PATTERNS =
Dangerous shell operators and patterns
[ /[;&|`$(){}]/, # Shell operators and command substitution %r{\.\./}, # Directory traversal %r{/etc/|/proc/|/sys/}, # Sensitive system directories /\$\{[^}]*\}/, # Variable expansion %r{~[^/\s]*}, # User home directory expansion %r{\bfile://|ftp://|https?://[^\s]*\.(sh|py|rb|js|exe|bat)\b}i, # Executable URLs ].freeze
- DEFAULT_LIMITS =
Maximum allowed lengths for various inputs
{ command_length: 1000, argument_length: 500, path_length: 1000, env_var_value_length: 2000, }.freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#call(context) ⇒ Commands::Context
Validate and sanitize the command context.
-
#initialize(options = {}) ⇒ Validator
constructor
Initialize the validator with security options.
Constructor Details
#initialize(options = {}) ⇒ Validator
Initialize the validator with security options
78 79 80 81 82 83 84 85 86 87 |
# File 'lib/makit/commands/middleware/validator.rb', line 78 def initialize( = {}) @options = { allow_shell_operators: false, blocked_commands: DEFAULT_BLOCKED_COMMANDS, limits: DEFAULT_LIMITS, strict_mode: true, allowed_schemes: %w[http https file], allow_environment_access: false, }.merge() end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
68 69 70 |
# File 'lib/makit/commands/middleware/validator.rb', line 68 def @options end |
Instance Method Details
#call(context) ⇒ Commands::Context
Validate and sanitize the command context
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/makit/commands/middleware/validator.rb', line 94 def call(context) validate_command!(context.command) if context.command validate_arguments!(context.arguments) if context.arguments validate_environment!(context.environment) if context.environment validate_working_directory!(context.working_directory) if context.working_directory context rescue StandardError => e raise Commands::SecurityError, "Validation failed: #{e.}" end |