Skip to content

Commit

Permalink
Low confidence warning if not a class
Browse files Browse the repository at this point in the history
  • Loading branch information
presidentbeef committed Sep 29, 2023
1 parent f2331d4 commit 51ec9c2
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion lib/brakeman/checks/check_ransack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ def check_ransack_calls
# If an allow list is defined anywhere in the
# class or super classes, consider it safe
class_name = result[:chain].first

next if ransackable_allow_list?(class_name)

if input = has_immediate_user_input?(arg)
confidence = if result[:location][:file].relative.include? 'admin'
confidence = if tracker.find_class(class_name).nil?
confidence = :low
elsif result[:location][:file].relative.include? 'admin'
confidence = :medium
else
confidence = :high
Expand Down
3 changes: 3 additions & 0 deletions test/apps/rails7/app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def search
def search_books
# Should not warn - search limited appropriately
Book.ransack(params[:q])

# Low confidence because no idea what `some_book` is
some_book.things.ransack(params[:q])
end

class << self
Expand Down
16 changes: 15 additions & 1 deletion test/tests/rails7.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def expected
:controller => 0,
:model => 0,
:template => 0,
:warning => 25
:warning => 26
}
end

Expand Down Expand Up @@ -448,4 +448,18 @@ def test_missing_authorization_ransack_2
confidence: 0,
relative_path: "app/controllers/users_controller.rb"
end

def test_missing_authorization_ransack_low
assert_warning check_name: "Ransack",
type: :warning,
warning_code: 129,
fingerprint: "50e236d8fbc9db0f67e0011941b92b08d0ece176ce4b8caea89d372f007a4873",
warning_type: "Missing Authorization",
line: 49,
message: /^Unrestricted\ search\ using\ `ransack`\ libr/,
confidence: 2,
relative_path: "app/controllers/users_controller.rb",
code: s(:call, s(:call, s(:call, nil, :some_book), :things), :ransack, s(:call, s(:params), :[], s(:lit, :q))),
user_input: s(:call, s(:params), :[], s(:lit, :q))
end
end

0 comments on commit 51ec9c2

Please sign in to comment.