Class: Sxn::Security::SecurePathValidator

Inherits:
Object
  • Object
show all
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.

Examples:

validator = SecurePathValidator.new("/path/to/project")
validator.validate_path("config/database.yml")  # => "/path/to/project/config/database.yml"
validator.validate_path("../etc/passwd")        # => raises PathValidationError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_root) ⇒ SecurePathValidator

Returns a new instance of SecurePathValidator.

Parameters:

  • project_root (String)

    The absolute path to the project root directory

Raises:

  • (ArgumentError)

    if project_root is nil, empty, or not an absolute path



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.expand_path(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_rootString (readonly)

Returns the project root path

Returns:

  • (String)

    The absolute 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

Parameters:

  • source (String)

    The source path

  • destination (String)

    The destination path

  • allow_creation (Boolean) (defaults to: true)

    Whether to allow validation of non-existent destination

Returns:

  • (Array<String>)

    Array containing [validated_source, validated_destination]

Raises:



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

Parameters:

  • path (String)

    The path to validate (can be relative or absolute)

  • allow_creation (Boolean) (defaults to: false)

    Whether to allow validation of non-existent paths

Returns:

  • (String)

    The absolute, validated path

Raises:



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

Parameters:

  • path (String)

    The path to check

Returns:

  • (Boolean)

    true if the path appears to be within boundaries



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