Class: SimpleCovMcp::PredicateEvaluator

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov_mcp/predicate_evaluator.rb

Overview

Evaluates coverage predicates from either Ruby code strings or files. Used by the validate subcommand, validate MCP tool, and library API.

Security Warning: Predicates execute as arbitrary Ruby code with full system privileges. Only use predicates from trusted sources.

Class Method Summary collapse

Class Method Details

.evaluate_code(code, model) ⇒ Boolean

Evaluate a predicate from a code string

Parameters:

  • code (String)

    Ruby code that returns a callable (lambda, proc, or object with #call)

  • model (CoverageModel)

    The coverage model to pass to the predicate

Returns:

  • (Boolean)

    The result of calling the predicate with the model

Raises:

  • (RuntimeError)

    If the code doesn’t return a callable or has syntax errors



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/simplecov_mcp/predicate_evaluator.rb', line 17

def self.evaluate_code(code, model)
  # WARNING: The predicate code executes with full Ruby privileges.
  # It has unrestricted access to the file system, network, and system commands.
  # Only use predicate code from trusted sources.
  #
  # We evaluate in a fresh Object context to prevent accidental access to
  # internals, but this provides NO security isolation.
  evaluation_context = Object.new
  predicate = evaluation_context.instance_eval(code, '<predicate>', 1)

  validate_callable(predicate)
  predicate.call(model)
rescue SyntaxError => e
  raise "Syntax error in predicate code: #{e.message}"
end

.evaluate_file(path, model) ⇒ Boolean

Evaluate a predicate from a file

Parameters:

  • path (String)

    Path to Ruby file containing predicate code

  • model (CoverageModel)

    The coverage model to pass to the predicate

Returns:

  • (Boolean)

    The result of calling the predicate with the model

Raises:

  • (RuntimeError)

    If the file doesn’t exist, doesn’t return a callable, or has syntax errors



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/simplecov_mcp/predicate_evaluator.rb', line 39

def self.evaluate_file(path, model)
  unless File.exist?(path)
    raise "Predicate file not found: #{path}"
  end

  content = File.read(path)

  # WARNING: The predicate code executes with full Ruby privileges.
  # It has unrestricted access to the file system, network, and system commands.
  # Only use predicate files from trusted sources.
  #
  # We evaluate in a fresh Object context to prevent accidental access to
  # internals, but this provides NO security isolation.
  evaluation_context = Object.new
  predicate = evaluation_context.instance_eval(content, path, 1)

  validate_callable(predicate)
  predicate.call(model)
rescue SyntaxError => e
  raise "Syntax error in predicate file: #{e.message}"
end