Module: OllamaChat::Utils::PathValidator

Defined in:
lib/ollama_chat/utils/path_validator.rb

Overview

Utility module that centralises path‑validation logic for OllamaChat tools.

The module is deliberately kept private – it is mixed into tool classes that need to validate file paths against a whitelist. By extracting the logic into a reusable mixin we avoid duplication and make the intent of each tool explicit.

Instance Method Summary collapse

Instance Method Details

#assert_valid_path(path, allowed) ⇒ Pathname

Validates that a supplied path is located inside one of the allowed directories.

The method performs the following steps:

  1. Canonicalises the supplied path using ‘Pathname#expand_path` and `cleanpath(true)` – this resolves symlinks and removes `..` components.

  2. Normalises each entry in allowed into an absolute, cleaned ‘Pathname` (an empty or `nil` allowed list results in an empty array, which causes every path to be rejected – the safest default).

  3. Checks that the canonicalised path starts with any of the allowed directories.

  4. If the check fails, raises an ‘OllamaChat::InvalidPathError` that carries the offending path for debugging and error reporting.

Parameters:

  • path (String, Pathname)

    The file or directory path to validate.

  • allowed (String, Array<String>, nil)

    A list of directory paths that are considered safe. Each entry is expanded to an absolute ‘Pathname`. Passing `nil` or an empty array will reject all paths.

Returns:

  • (Pathname)

    The canonicalised absolute path if validation passes.

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ollama_chat/utils/path_validator.rb', line 33

def assert_valid_path(path, allowed)
  target_path = Pathname.new(path).expand_path.cleanpath(true)

  allowed_dirs = Array(allowed).map do |p|
    Pathname.new(p).expand_path.cleanpath
  end

  valid_path = allowed_dirs.any? do |allowed_dir|
    target_path.to_s.start_with?(allowed_dir.to_s)
  end

  unless valid_path
    error = OllamaChat::InvalidPathError.new(
      "Path #{path} is not within allowed directories: " \
      "#{allowed_dirs&.join(', ') || ''}"
    )
    error.path = path
    raise error
  end

  target_path
end