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

Command injection false positive with nested system call #1399

Closed
toupeira opened this issue Sep 19, 2019 · 3 comments · Fixed by #1408
Closed

Command injection false positive with nested system call #1399

toupeira opened this issue Sep 19, 2019 · 3 comments · Fixed by #1408

Comments

@toupeira
Copy link

Background

Brakeman version: 4.6.1
Rails version: 5.2.3
Ruby version: 2.6.3

False Positive

Full warning from Brakeman:

Confidence: Medium
Category: Command Injection
Check: Execute
Message: Possible command injection
Code: system(*["foo", "bar", "#{value}"])
File: lib/foo.rb
Line: 5

Relevant code:

system(*%W(foo bar #{value}))

module Foo
  def test1
    system(*%W(foo bar #{value}))
  end

  def test2
    system('foo', 'bar', value)
  end
end

Note: I had to place this file in e.g. lib/foo.rb rather than the toplevel directory for Brakeman to pick it up, even though I passed --only-files foo.rb.

Why might this be a false positive?

The first system call outside of Foo is the same as in test1, but not reported as a warning.

Using a normal literal instead of %W() as in test2 doesn't get reported as a warning either, but this seems unnecessary. I originally thought %W() can result in variables containing whitespace to be interpreted as two arguments, but Ruby is smart enough to protect against that:

%W(#{'foo bar'}) === ['foo bar'] # -> true
@presidentbeef
Copy link
Owner

Hi @toupeira thank you for reporting!

It seems that the string interpolation in test1 is being wrongly treated as dangerous.

@presidentbeef
Copy link
Owner

I saw this fixed some issues in GitLab(-ci/hq) and thought "Huh, looks like someone does use this code pattern" and then I realized 🤣

@toupeira
Copy link
Author

I saw this fixed some issues in GitLab(-ci/hq) and thought "Huh, looks like someone does use this code pattern" and then I realized 🤣

Haha yeah... which reminds me, I still need to finish watching those security training videos with you and Jim 😁

Thanks for the fix! 👍

Repository owner locked and limited conversation to collaborators Jan 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants