Check for unsafe SQL when two arguments are passed to AR methods - #1936
Conversation
|
This pull request involves a minor modification to Brakeman's SQL injection detection logic, adjusting the method argument length condition to potentially capture a broader range of method call scenarios for improved vulnerability scanning. 💭 Unconfirmed Findings (1)
All finding details can be found in the DryRun Security Dashboard. |
I think this conditional is meant to
1. Always check the first and last arguments.
2. Avoid checking the argument twice when there's only one argument.
The structure of `call` appears to be:
* 0: the symbol `:call`
* 1: the receiver of the method call
* 2: the method being called
* 3: the first argument
* 4: the second argument
* 5: the third argument (and so on)
So `call.length > 5` will be true only if there are at least 3
arguments.
Before this change, the behavior was
```ruby
User.count("#{params[:input]}") # triggers warning
User.count("#{params[:input]}", :foo) # doesn't trigger warning
User.count("#{params[:input]}", :foo, :bar) # triggers warning
```
After this change, the two-argument version will trigger the warning.
Another option would be to use `if call.arglist.length > 1`. This is
more explicit, but it involves an additional method call, and I don't
know what the performance implications of that are.
4ee9000 to
38a3005
Compare
|
I'm going to have to figure out what the original intent was here... it's been a while since those methods even accepted more than one argument. (But I think you are right) |
|
@patbl Can you update to use the more explicit |
|
@presidentbeef I made that change. |
|
No security concerns detected in this pull request. All finding details can be found in the DryRun Security Dashboard. |
|
This change seems to have triggered a LOT of warnings across many of our applications that use a https://www.rubydoc.info/gems/dogstatsd-ruby/3.0.0/Datadog/Statsd#count-instance_method Is there a way this check might be better-targeted to avoid this deluge of false positives? |
|
@soulcutter can you provide an example of the exact warning from Brakeman? Does it say |
The |
|
Maybe Brakeman can just ignore calls that look like ActiveRecord methods but are on globals 🤔 |
A global variable such as It would be nice if there were a way to tell Brakeman "this [ |
|
Writing down for myself... for common methods like |
In versions of Rails starting in 5.0, these methods only take one argument. Also reduce false positives reported in #1936
I think this conditional is meant to
The structure of
callappears to be::callSo
call.length > 5will be true only if there are at least 3 arguments.Before this change, the behavior was
After this change, the two-argument version will trigger the warning.
Another option would be to use
if call.arglist.length > 1. This is more explicit, but it involves an additional method call, and I don't know what the performance implications of that are.