Class: Brakeman::FileParser

Inherits:
Object
  • Object
show all
Defined in:
lib/brakeman/file_parser.rb

Overview

This class handles reading and parsing files.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app_tree, timeout, parallel = true, use_prism = false) ⇒ FileParser

Returns a new instance of FileParser.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/brakeman/file_parser.rb', line 10

def initialize app_tree, timeout, parallel = true, use_prism = false
  @use_prism = use_prism

  if @use_prism
    begin
      require 'prism'
    rescue LoadError => e
      Brakeman.debug "Asked to use Prism, but failed to load: #{e}"
      @use_prism = false
    end
  end

  @app_tree = app_tree
  @timeout = timeout
  @file_list = []
  @errors = []
  @parallel = parallel
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



8
9
10
# File 'lib/brakeman/file_parser.rb', line 8

def errors
  @errors
end

#file_listObject (readonly)

Returns the value of attribute file_list.



8
9
10
# File 'lib/brakeman/file_parser.rb', line 8

def file_list
  @file_list
end

Instance Method Details

#parse_files(list) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/brakeman/file_parser.rb', line 29

def parse_files list
  if @parallel
    parallel_options = {}
  else
    # Disable parallelism
    parallel_options = { in_threads: 0 }
  end

  # Parse the files in parallel.
  # By default, the parsing will be in separate processes.
  # So we map the result to ASTFiles and/or Exceptions
  # then partition them into ASTFiles and Exceptions
  # and add the Exceptions to @errors
  #
  # Basically just a funky way to deal with two possible
  # return types that are returned from isolated processes.
  #
  # Note this method no longer uses read_files
  @file_list, new_errors = Parallel.map(list, parallel_options) do |file_name|
    file_path = @app_tree.file_path(file_name)
    contents = file_path.read

    begin
      if ast = parse_ruby(contents, file_path.relative)
        ASTFile.new(file_name, ast)
      end
    rescue Exception => e
      e
    end
  end.compact.partition do |result|
    result.is_a? ASTFile
  end

  errors.concat new_errors
end

#parse_ruby(input, path) ⇒ Object

path can be a string or a Brakeman::FilePath



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/brakeman/file_parser.rb', line 82

def parse_ruby input, path
  if path.is_a? Brakeman::FilePath
    path = path.relative
  end

  Brakeman.debug "Parsing #{path}"

  if @use_prism
    begin
      parse_with_prism input, path
    rescue => e
      Brakeman.debug "Prism failed to parse #{path}: #{e}"

      parse_with_ruby_parser input, path
    end
  else
    parse_with_ruby_parser input, path
  end
end

#read_files(list) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/brakeman/file_parser.rb', line 65

def read_files list
  list.each do |path|
    file = @app_tree.file_path(path)

    begin
      result = yield file, file.read

      if result
        @file_list << result
      end
    rescue Exception => e
      @errors << e
    end
  end
end