Class: Omamori::StaticAnalysers::BrakemanRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/omamori/static_analysers/brakeman_runner.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ BrakemanRunner

Returns a new instance of BrakemanRunner.



6
7
8
# File 'lib/omamori/static_analysers/brakeman_runner.rb', line 6

def initialize(options = {})
  @options = options
end

Instance Method Details

#runObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/omamori/static_analysers/brakeman_runner.rb', line 10

def run
  puts "Running Brakeman..."
  # Determine Brakeman command based on options
  # Use --force to run scan even if it's not a Rails application
  # Use -f json for JSON output
  # Include options passed during initialization
  # Convert options hash to command line arguments string
  options_string = @options.map do |key, value|
    if value.is_a?(TrueClass)
      key.to_s
    elsif value.is_a?(FalseClass)
      "" # Don't include false flags
    else
      "#{key} #{value}"
    end
  end.join(" ").strip

  brakeman_command = "brakeman -f json . --force #{options_string}".strip # strip again in case options_string is empty

  begin
    # Execute the Brakeman command and capture output
    brakeman_output = `#{brakeman_command}`

    # Parse the JSON output
    # Note: JSON.parse is called here. If the test expects it to be called only once,
    # the test setup might be causing it to be called multiple times or the mock is misconfigured.
    JSON.parse(brakeman_output)
  rescue Errno::ENOENT
    puts "Error: Brakeman command not found. Is Brakeman installed?"
    nil
  rescue JSON::ParserError
    puts "Error: Failed to parse Brakeman JSON output."
    puts "Raw output:\n#{brakeman_output}"
    nil
  rescue => e
    puts "An error occurred during Brakeman execution: #{e.message}"
    nil
  end
end