Skip to content

Ruby: try/try! as code execution #11022

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

Merged
merged 2 commits into from
Nov 7, 2022
Merged
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
4 changes: 4 additions & 0 deletions ruby/ql/lib/change-notes/2022-10-28-try-code-execution.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* The `ActiveSupport` extensions `Object#try` and `Object#try!` are now recognised as code executions.
23 changes: 23 additions & 0 deletions ruby/ql/lib/codeql/ruby/frameworks/ActiveSupport.qll
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,29 @@ module ActiveSupport {
preservesValue = true
}
}

/**
* A call to `Object#try`, which may execute its first argument as a Ruby
* method call.
* ```rb
* x = "abc"
* x.try(:upcase) # => "ABC"
* y = nil
* y.try(:upcase) # => nil
* ```
* `Object#try!` behaves similarly but raises `NoMethodError` if the
* receiver is not `nil` and does not respond to the method.
*/
class TryCallCodeExecution extends CodeExecution::Range, DataFlow::CallNode {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
class TryCallCodeExecution extends CodeExecution::Range, DataFlow::CallNode {
class TryCallCodeExecution extends CodeExecution::Range instanceof DataFlow::CallNode {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

👍 I'll open a new PR for this change to avoid having to ask for re-review on this one.

TryCallCodeExecution() {
this.asExpr().getExpr() instanceof UnknownMethodCall and
this.getMethodName() = ["try", "try!"]
}

override DataFlow::Node getCode() { result = this.getArgument(0) }

override predicate runsArbitraryCode() { none() }
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ constantizeCalls
loggerInstantiations
| active_support.rb:6:1:6:33 | call to new |
| active_support.rb:7:1:7:40 | call to new |
codeExecutions
| active_support.rb:1:1:1:22 | call to constantize |
| active_support.rb:3:1:3:13 | call to constantize |
| active_support.rb:4:1:4:18 | call to safe_constantize |
| active_support.rb:296:5:296:18 | call to try |
| active_support.rb:297:5:297:17 | call to try |
| active_support.rb:298:5:298:19 | call to try! |
| active_support.rb:298:5:298:35 | call to try! |
| active_support.rb:299:5:299:18 | call to try! |
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import codeql.ruby.frameworks.ActiveSupport
import codeql.ruby.DataFlow
import codeql.ruby.frameworks.stdlib.Logger
import codeql.ruby.Concepts

query DataFlow::Node constantizeCalls(ActiveSupport::CoreExtensions::String::Constantize c) {
result = c.getCode()
}

query predicate loggerInstantiations(Logger::LoggerInstantiation l) { any() }

query predicate codeExecutions(CodeExecution c) { any() }
Original file line number Diff line number Diff line change
Expand Up @@ -290,3 +290,11 @@ def m_deep_dup
x = source "a"
sink x.deep_dup # $hasValueFlow=a
end

def m_try(method)
x = "abc"
x.try(:upcase)
x.try(method)
x.try!(:upcase).try!(:downcase)
x.try!(method)
end