Skip to content

Commit

Permalink
Use Prism parser by default (#1897)
Browse files Browse the repository at this point in the history
Default to using Prism parser if available
  • Loading branch information
presidentbeef authored Dec 30, 2024
1 parent b299ca0 commit ee9de40
Show file tree
Hide file tree
Showing 10 changed files with 20 additions and 24 deletions.
10 changes: 0 additions & 10 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,6 @@ jobs:
<<: *default
docker:
- image: cimg/ruby:3.2
test-with-prism:
<<: *default
steps:
- checkout
- run: bundle check || bundle install
- run:
command: |
gem install prism
TEST_PRISM=true bundle exec rake
upload-coverage:
<<: *default
working_directory: ~/repo
Expand All @@ -65,7 +56,6 @@ workflows:
- default
- test-3-1
- test-3-2
- test-with-prism
- upload-coverage:
requires:
- test-3-1
1 change: 0 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ unless ENV['BM_PACKAGE']
group :test do
gem 'rake'
gem 'minitest'
gem 'prism'
end
end
2 changes: 1 addition & 1 deletion brakeman.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |s|
s.files += Dir['bundle/ruby/*/gems/**/*'].reject do |path|
# Skip unnecessary files in dependencies
path =~ %r{^bundle/ruby/\d\.\d\.\d/gems/[^\/]+/(Rakefile|benchmark|bin|doc|example|man|site|spec|test)} or
path =~ %r{/gems/(io-console|racc|strscan)/}
path =~ %r{/gems/(io-console|prism|racc|strscan)/}
end

# racc is not only a built-in gem, but also has native code which we cannot
Expand Down
2 changes: 1 addition & 1 deletion build.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env ruby
require 'fileutils'
bundle_exclude = %w[io-console racc strscan]
bundle_exclude = %w[io-console prism racc strscan]

puts 'Packaging Brakeman gem...'

Expand Down
1 change: 1 addition & 0 deletions gem_common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def self.extended_dependencies spec
spec.add_dependency "haml", "~>5.1"
spec.add_dependency "slim", ">=1.3.6", "< 5.3"
spec.add_dependency "rexml", "~>3.0"
spec.add_dependency "prism", "~>1.3"
end
end
end
10 changes: 10 additions & 0 deletions lib/brakeman.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ def self.run options
options[:report_progress] = false
end

if options[:use_prism]
begin
require 'prism'
notify '[Notice] Using Prism parser'
rescue LoadError => e
Brakeman.debug "[Notice] Asked to use Prism, but failed to load: #{e}"
end
end

scan options
end

Expand Down Expand Up @@ -196,6 +205,7 @@ def self.default_options
:pager => true,
:parallel_checks => true,
:parser_timeout => 10,
:use_prism => true,
:relative_path => false,
:report_progress => true,
:safe_methods => Set.new,
Expand Down
3 changes: 2 additions & 1 deletion lib/brakeman/file_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ def initialize app_tree, timeout, parallel = true, use_prism = false
if @use_prism
begin
require 'prism'
Brakeman.debug '[Notice] Using Prism parser'
rescue LoadError => e
Brakeman.debug "Asked to use Prism, but failed to load: #{e}"
Brakeman.debug "[Notice] Asked to use Prism, but failed to load: #{e}"
@use_prism = false
end
end
Expand Down
9 changes: 4 additions & 5 deletions lib/brakeman/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,13 @@ def create_option_parser options

opts.on "--[no-]prism", "Use the Prism parser" do |use_prism|
if use_prism
prism_version = '1.0'
min_prism_version = '1.3.0'

begin
# Specifying minimum version here,
# since it can't be in the gem dependency list because it is optional
gem 'prism', ">=#{prism_version}"
gem 'prism', ">=#{min_prism_version}"
require 'prism'
rescue Gem::MissingSpecVersionError, Gem::MissingSpecError, Gem::LoadError => e
$stderr.puts "Please install `prism` version #{prism_version} or newer:"
$stderr.puts "Please install `prism` version #{min_prism_version} or newer:"
raise e
end
end
Expand Down
4 changes: 0 additions & 4 deletions test/tests/rails52.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,6 @@ def test_sql_injection_foreign_key
end

def test_sql_injection_user_input
if ENV['TEST_PRISM']
skip 'Un-skip as soon as Prism >1.2.0 is released'
end

assert_warning :type => :warning,
:warning_code => 0,
:fingerprint => "f7affe2dfe9e3a48f39f1fb86224e150e60555a73f2e78fb499eadd298233625",
Expand Down
2 changes: 1 addition & 1 deletion test/tests/rails8.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class Rails8Tests < Minitest::Test
def report
@@report ||=
Date.stub :today, Date.parse("2024-05-13") do
BrakemanTester.run_scan "rails8", "Rails 8", run_all_checks: true, use_prism: true
BrakemanTester.run_scan "rails8", "Rails 8", run_all_checks: true, use_prism: false
end
end

Expand Down

0 comments on commit ee9de40

Please sign in to comment.