Skip to content

Commit 3206384

Browse files
authored
Merge pull request #7824 from github/hmac/constantize
2 parents b2419d6 + c65ca8f commit 3206384

File tree

5 files changed

+48
-0
lines changed

5 files changed

+48
-0
lines changed

ruby/ql/lib/codeql/ruby/Frameworks.qll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ private import codeql.ruby.frameworks.ActionController
66
private import codeql.ruby.frameworks.ActiveRecord
77
private import codeql.ruby.frameworks.ActiveStorage
88
private import codeql.ruby.frameworks.ActionView
9+
private import codeql.ruby.frameworks.ActiveSupport
910
private import codeql.ruby.frameworks.GraphQL
1011
private import codeql.ruby.frameworks.Rails
1112
private import codeql.ruby.frameworks.StandardLibrary
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Modeling for `ActiveSupport`, which is a utility gem that ships with Rails.
3+
* https://rubygems.org/gems/activesupport
4+
*/
5+
6+
import codeql.ruby.Concepts
7+
import codeql.ruby.DataFlow
8+
import codeql.ruby.frameworks.StandardLibrary
9+
10+
/**
11+
* Modeling for `ActiveSupport`.
12+
*/
13+
module ActiveSupport {
14+
/**
15+
* Extensions to core classes
16+
*/
17+
module CoreExtensions {
18+
/**
19+
* Extensions to the `String` class
20+
*/
21+
module String {
22+
/**
23+
* A call to `String#constantize`, which tries to find a declared constant with the given name.
24+
* Passing user input to this method may result in instantiation of arbitrary Ruby classes.
25+
*/
26+
class Constantize extends CodeExecution::Range, DataFlow::CallNode {
27+
// We treat this an `UnknownMethodCall` in order to match every call to `constantize` that isn't overridden.
28+
// We can't (yet) rely on API Graphs or dataflow to tell us that the receiver is a String.
29+
Constantize() {
30+
this.asExpr().getExpr().(UnknownMethodCall).getMethodName() = "constantize"
31+
}
32+
33+
override DataFlow::Node getCode() { result = this.getReceiver() }
34+
}
35+
}
36+
}
37+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| active_support.rb:1:1:1:22 | call to constantize | active_support.rb:1:1:1:10 | "Foo::Bar" |
2+
| active_support.rb:3:1:3:13 | call to constantize | active_support.rb:3:1:3:1 | call to a |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import codeql.ruby.frameworks.ActiveSupport
2+
3+
query DataFlow::Node constantizeCalls(ActiveSupport::CoreExtensions::String::Constantize c) {
4+
result = c.getCode()
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"Foo::Bar".constantize
2+
3+
a.constantize

0 commit comments

Comments
 (0)