Class: Sxn::Security::SecurePathValidator
- Inherits:
-
Object
- Object
- Sxn::Security::SecurePathValidator
- Defined in:
- lib/sxn/security/secure_path_validator.rb
Overview
SecurePathValidator provides security controls for file system path operations. It prevents directory traversal attacks, validates paths stay within project boundaries, checks for dangerous symlinks, and ensures no “..” components in paths.
Instance Attribute Summary collapse
-
#project_root ⇒ String
readonly
Returns the project root path.
Instance Method Summary collapse
-
#initialize(project_root) ⇒ SecurePathValidator
constructor
A new instance of SecurePathValidator.
-
#validate_file_operation(source, destination, allow_creation: true) ⇒ Array<String>
Validates a source and destination pair for file operations.
-
#validate_path(path, allow_creation: false) ⇒ String
Validates that a path is safe and within project boundaries.
-
#within_boundaries?(path) ⇒ Boolean
Checks if a path is within the project boundaries without full validation.
Constructor Details
#initialize(project_root) ⇒ SecurePathValidator
Returns a new instance of SecurePathValidator.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/sxn/security/secure_path_validator.rb', line 19 def initialize(project_root) raise ArgumentError, "Project root cannot be nil or empty" if project_root.nil? || project_root.empty? # Resolve relative paths to absolute paths absolute_root = if Pathname.new(project_root).absolute? project_root else File.(project_root) end @project_root = File.realpath(absolute_root) @project_root_pathname = Pathname.new(@project_root) rescue Errno::ENOENT raise PathValidationError, "Project root does not exist: #{project_root}" end |
Instance Attribute Details
#project_root ⇒ String (readonly)
Returns the project root path
107 108 109 |
# File 'lib/sxn/security/secure_path_validator.rb', line 107 def project_root @project_root end |
Instance Method Details
#validate_file_operation(source, destination, allow_creation: true) ⇒ Array<String>
Validates a source and destination pair for file operations
79 80 81 82 83 84 85 86 87 |
# File 'lib/sxn/security/secure_path_validator.rb', line 79 def validate_file_operation(source, destination, allow_creation: true) validated_source = validate_path(source, allow_creation: false) validated_destination = validate_path(destination, allow_creation: allow_creation) # Additional checks for file operations raise PathValidationError, "Source cannot be a directory: #{source}" if File.exist?(validated_source) && File.directory?(validated_source) [validated_source, validated_destination] end |
#validate_path(path, allow_creation: false) ⇒ String
Validates that a path is safe and within project boundaries
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/sxn/security/secure_path_validator.rb', line 41 def validate_path(path, allow_creation: false) raise ArgumentError, "Path cannot be nil or empty" if path.nil? || (path.respond_to?(:empty?) && path.empty?) raise ArgumentError, "Path must be a string" unless path.is_a?(String) # Check for dangerous patterns in the raw path validate_path_components!(path) # Convert to absolute path relative to project root absolute_path = if Pathname.new(path).absolute? path else File.join(@project_root, path) end # Normalize the path and check boundaries normalized_path = if File.exist?(absolute_path) File.realpath(absolute_path) elsif allow_creation # For non-existent paths, we need to validate the normalized path manually normalize_path_manually(absolute_path) else # If file doesn't exist and creation isn't allowed, use realpath which will fail File.realpath(absolute_path) end validate_within_boundaries!(normalized_path) validate_symlink_safety!(normalized_path) if File.exist?(normalized_path) normalized_path end |
#within_boundaries?(path) ⇒ Boolean
Checks if a path is within the project boundaries without full validation
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/sxn/security/secure_path_validator.rb', line 93 def within_boundaries?(path) return false if path.nil? || path.empty? begin validate_path(path, allow_creation: true) true rescue PathValidationError false end end |