Skip to content

Commit

Permalink
More parsing exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
presidentbeef committed Apr 29, 2021
1 parent b9e9536 commit 7ccba40
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
34 changes: 16 additions & 18 deletions lib/brakeman/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,12 @@ def parse_files list
file_path = @app_tree.file_path(file_name)
contents = file_path.read

result = parse_ruby(contents, file_path.relative)

case result
when Exception
result
when Sexp
ASTFile.new(file_name, result)
else
nil
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
Expand All @@ -50,13 +47,14 @@ def read_files list
list.each do |path|
file = @app_tree.file_path(path)

result = yield file, file.read
begin
result = yield file, file.read

case result
when ASTFile
@file_list << result
when Exception
@errors << result
if result
@file_list << result
end
rescue Exception => e
@errors << e
end
end
end
Expand All @@ -71,11 +69,11 @@ def parse_ruby input, path
Brakeman.debug "Parsing #{path}"
RubyParser.new.parse input, path, @timeout
rescue Racc::ParseError => e
e.exception(e.message + "\nCould not parse #{path}")
raise e.exception(e.message + "\nCould not parse #{path}")
rescue Timeout::Error => e
Exception.new("Parsing #{path} took too long (> #{@timeout} seconds). Try increasing the limit with --parser-timeout")
raise Exception.new("Parsing #{path} took too long (> #{@timeout} seconds). Try increasing the limit with --parser-timeout")
rescue => e
e.exception(e.message + "\nWhile processing #{path}")
raise e.exception(e.message + "\nWhile processing #{path}")
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions lib/brakeman/scanner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ def index_call_sites
def parse_ruby_file file
fp = Brakeman::FileParser.new(tracker.app_tree, tracker.options[:parser_timeout])
fp.parse_ruby(file.read, file)
rescue Exception => e
tracker.error(e)
nil
end
end

Expand Down

0 comments on commit 7ccba40

Please sign in to comment.