Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed detection of UnscopedFind if optional: true is defined in the model and is not self-reference association #1764

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/brakeman/checks/check_unscoped_find.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def run_check

associated_model_names = active_record_models.keys.select do |name|
if belongs_to = active_record_models[name].associations[:belongs_to]
not optional_belongs_to? belongs_to
not self_referential_association? belongs_to, name
else
false
end
Expand Down Expand Up @@ -52,12 +52,16 @@ def process_result result
:cwe_id => [285]
end

def optional_belongs_to? exp
def self_referential_association? exp, name
return false unless exp.is_a? Array

exp.each do |e|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what we actually want is to check if there is any belongs_to relationship that is not optional?

Is the self-referential piece actually that important?

if hash? e and true? hash_access(e, :optional)
return true
hash_iterate e do |key, value|
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is class_name going to show up more than once as a key? Couldn't this also use hash_access(:class_name) instead of iterating?

if key[1] == :class_name and name == value[1].to_sym
return true
end
end
end
end

Expand Down
6 changes: 6 additions & 0 deletions test/apps/rails7/app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class GroupsController < ApplicationController
def show
@group = Group.find(params[:id])
@user = User.find(params[:id])
end
end
3 changes: 3 additions & 0 deletions test/apps/rails7/app/models/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Group < ApplicationRecord
belongs_to :user, optional: true
end
1 change: 1 addition & 0 deletions test/apps/rails7/app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
class User < ApplicationRecord
belongs_to :matched_user, class_name: 'User', optional: true
end
30 changes: 29 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 => 23
:warning => 24
}
end

Expand Down Expand Up @@ -396,4 +396,32 @@ def test_redirect_back_or_to
code: s(:call, nil, :redirect_back_or_to, s(:call, s(:params), :[], s(:lit, :x))),
user_input: s(:call, s(:params), :[], s(:lit, :x))
end

def test_unscoped_find
assert_warning check_name: "UnscopedFind",
type: :warning,
warning_code: 82,
fingerprint: "e84705527089566771bd3ae5b04f9c82529a0f388a4864eb4a91f7ad468541da",
warning_type: "Unscoped Find",
line: 3,
message: /^Unscoped\ call\ to\ `Group\#find`/,
confidence: 2,
relative_path: "app/controllers/groups_controller.rb",
code: s(:call, s(:const, :Group), :find, s(:call, s(:params), :[], s(:lit, :id))),
user_input: s(:call, s(:params), :[], s(:lit, :id))
end

def test_unscoped_find_2
assert_no_warning check_name: "UnscopedFind",
type: :warning,
warning_code: 82,
fingerprint: "f2ba23dcceff3cefef2d9fd08a0fe9f87b5936226ec883a73e3a93629e172387",
warning_type: "Unscoped Find",
line: 4,
message: /^Unscoped\ call\ to\ `User\#find`/,
confidence: 2,
relative_path: "app/controllers/groups_controller.rb",
code: s(:call, s(:const, :User), :find, s(:call, s(:params), :[], s(:lit, :id))),
user_input: s(:call, s(:params), :[], s(:lit, :id))
end
end