Skip to content

Commit a29fe44

Browse files
Merge pull request #2014 from FFederi/fix-polymorphic-name-false-positive
Fix polymorphic_name false positive
2 parents f65d077 + 61150cf commit a29fe44

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

lib/brakeman/checks/check_sql.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,8 @@ def check_string_arg exp
600600
:sanitize_sql_for_assignment, :sanitize_sql_for_conditions, :sanitize_sql_hash,
601601
:sanitize_sql_hash_for_assignment, :sanitize_sql_hash_for_conditions,
602602
:to_sql, :sanitize, :primary_key, :table_name_prefix, :table_name_suffix,
603-
:where_values_hash, :foreign_key, :uuid, :escape, :escape_string
603+
:where_values_hash, :foreign_key, :uuid, :escape, :escape_string,
604+
:polymorphic_name
604605
]
605606

606607
def ignore_methods_in_sql

test/apps/rails5.2/app/models/user.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ def foreign_key_thing
2323

2424
User.joins("INNER JOIN <complex join involving custom SQL and #{foreign_key} interpolation>")
2525
end
26+
27+
def polymorphic_name_joins
28+
MediaFile.joins(
29+
"JOIN #{table_name}
30+
ON media_files.parent_type = '#{polymorphic_name}'
31+
AND media_files.parent_id = #{table_name}.id"
32+
)
33+
end
2634
end

test/tests/rails52.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ def test_sql_injection_foreign_key
103103
:user_input => s(:call, s(:call, nil, :reflect_on_association, s(:lit, :foos)), :foreign_key)
104104
end
105105

106+
def test_sql_injection_polymorphic_name
107+
assert_no_warning :type => :warning,
108+
:warning_code => 0,
109+
:fingerprint => "76aa7211f677f4341babbadbed9d1c64c1cf3073303e94d565fe6cdb0124e4dc",
110+
:warning_type => "SQL Injection",
111+
:line => 30,
112+
:message => /^Possible\ SQL\ injection/,
113+
:confidence => 2,
114+
:relative_path => "app/models/user.rb",
115+
:code => s(:call, s(:const, :MediaFile), :joins, s(:dstr, "JOIN ", s(:evstr, s(:call, nil, :table_name)), s(:str, "\n ON media_files.parent_type = '"), s(:evstr, s(:call, nil, :polymorphic_name)), s(:str, "'\n AND media_files.parent_id = "), s(:evstr, s(:call, nil, :table_name)), s(:str, ".id"))),
116+
:user_input => s(:call, nil, :polymorphic_name)
117+
end
118+
106119
def test_sql_injection_user_input
107120
assert_warning :type => :warning,
108121
:warning_code => 0,

0 commit comments

Comments
 (0)