Skip to content

Commit b389d50

Browse files
committed
Ruby: Identify safe_constantize
1 parent ca279f4 commit b389d50

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

ruby/ql/lib/codeql/ruby/frameworks/ActiveSupport.qll

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,17 @@ module ActiveSupport {
2424
*/
2525
module String {
2626
/**
27-
* A call to `String#constantize`, which tries to find a declared constant with the given name.
28-
* Passing user input to this method may result in instantiation of arbitrary Ruby classes.
27+
* A call to `String#constantize` or `String#safe_constantize`, which
28+
* tries to find a declared constant with the given name.
29+
* Passing user input to this method may result in instantiation of
30+
* arbitrary Ruby classes.
2931
*/
3032
class Constantize extends CodeExecution::Range, DataFlow::CallNode {
3133
// We treat this an `UnknownMethodCall` in order to match every call to `constantize` that isn't overridden.
3234
// We can't (yet) rely on API Graphs or dataflow to tell us that the receiver is a String.
3335
Constantize() {
34-
this.asExpr().getExpr().(UnknownMethodCall).getMethodName() = "constantize"
36+
this.asExpr().getExpr().(UnknownMethodCall).getMethodName() =
37+
["constantize", "safe_constantize"]
3538
}
3639

3740
override DataFlow::Node getCode() { result = this.getReceiver() }
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
constantizeCalls
22
| active_support.rb:1:1:1:22 | call to constantize | active_support.rb:1:1:1:10 | "Foo::Bar" |
33
| active_support.rb:3:1:3:13 | call to constantize | active_support.rb:3:1:3:1 | call to a |
4+
| active_support.rb:4:1:4:18 | call to safe_constantize | active_support.rb:4:1:4:1 | call to a |
45
loggerInstantiations
5-
| active_support.rb:5:1:5:33 | call to new |
6-
| active_support.rb:6:1:6:40 | call to new |
6+
| active_support.rb:6:1:6:33 | call to new |
7+
| active_support.rb:7:1:7:40 | call to new |

ruby/ql/test/library-tests/frameworks/active_support/active_support.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"Foo::Bar".constantize
22

33
a.constantize
4+
a.safe_constantize
45

56
ActiveSupport::Logger.new(STDOUT)
67
ActiveSupport::TaggedLogging.new(STDOUT)

0 commit comments

Comments
 (0)