Skip to content

Commit 219cd4e

Browse files
authored
Merge pull request #14426 from hmac/hmac-ar-scopes
Ruby: Track flow into ActiveRecord scopes
2 parents f812422 + 7e479e3 commit 219cd4e

File tree

10 files changed

+113
-5
lines changed

10 files changed

+113
-5
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Data flow is now tracked through `ActiveRecord` scopes.

ruby/ql/lib/codeql/ruby/dataflow/internal/DataFlowDispatch.qll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,20 @@ private Callable viableSourceCallableInit(RelevantCall call) { result = getIniti
429429
/** Holds if `call` may resolve to the returned source-code method. */
430430
private DataFlowCallable viableSourceCallable(DataFlowCall call) {
431431
result = viableSourceCallableNonInit(call) or
432-
result.asCfgScope() = viableSourceCallableInit(call.asCall())
432+
result.asCfgScope() = viableSourceCallableInit(call.asCall()) or
433+
result = any(AdditionalCallTarget t).viableTarget(call.asCall())
434+
}
435+
436+
/**
437+
* A unit class for adding additional call steps.
438+
*
439+
* Extend this class to add additional call steps to the data flow graph.
440+
*/
441+
class AdditionalCallTarget extends Unit {
442+
/**
443+
* Gets a viable target for `call`.
444+
*/
445+
abstract DataFlowCallable viableTarget(CfgNodes::ExprNodes::CallCfgNode call);
433446
}
434447

435448
/** Holds if `call` may resolve to the returned summarized library method. */

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,3 +765,30 @@ private class ActiveRecordCollectionProxyModelInstantiation extends ActiveRecord
765765
result = this.(ActiveRecordCollectionProxyMethodCall).getAssociation().getTargetClass()
766766
}
767767
}
768+
769+
/**
770+
* An additional call step for calls to ActiveRecord scopes. For example, in the following code:
771+
*
772+
* ```rb
773+
* class User < ActiveRecord::Base
774+
* scope :with_role, ->(role) { where(role: role) }
775+
* end
776+
*
777+
* User.with_role(r)
778+
* ```
779+
*
780+
* the call to `with_role` targets the lambda, and argument `r` flows to the parameter `role`.
781+
*/
782+
class ActiveRecordScopeCallTarget extends AdditionalCallTarget {
783+
override DataFlowCallable viableTarget(ExprNodes::CallCfgNode scopeCall) {
784+
exists(DataFlow::ModuleNode model, string scopeName |
785+
model = activeRecordBaseClass().getADescendentModule() and
786+
exists(DataFlow::CallNode scope |
787+
scope = model.getAModuleLevelCall("scope") and
788+
scope.getArgument(0).getConstantValue().isStringlikeValue(scopeName) and
789+
scope.getArgument(1).asCallable().asCallableAstNode() = result.asCfgScope()
790+
) and
791+
scopeCall = model.getAnImmediateReference().getAMethodCall(scopeName).asExpr()
792+
)
793+
}
794+
}

ruby/ql/lib/codeql/ruby/frameworks/core/Array.qll

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1855,7 +1855,8 @@ module Enumerable {
18551855

18561856
override predicate propagatesFlow(string input, string output, boolean preservesValue) {
18571857
input = "Argument[self].Element[any]" and
1858-
output = "Argument[block].Parameter[0]" and
1858+
// For `Hash#map`, the value flows to parameter 1
1859+
output = "Argument[block].Parameter[0, 1]" and
18591860
preservesValue = true
18601861
or
18611862
input = "Argument[block].ReturnValue" and

ruby/ql/lib/codeql/ruby/frameworks/core/Hash.qll

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,15 @@ private class ValuesSummary extends SimpleSummarizedCallable {
523523
preservesValue = true
524524
}
525525
}
526+
527+
// We don't (yet) track data flow through hash keys, but this is still useful in cases where a
528+
// whole hash(like) object is tainted, such as `ActionController#params`.
529+
private class KeysSummary extends SimpleSummarizedCallable {
530+
KeysSummary() { this = "keys" }
531+
532+
override predicate propagatesFlow(string input, string output, boolean preservesValue) {
533+
input = "Argument[self]" and
534+
output = "ReturnValue.Element[?]" and
535+
preservesValue = false
536+
}
537+
}

ruby/ql/test/library-tests/dataflow/hash-flow/hash-flow.expected

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,13 @@ edges
10891089
| hash_flow.rb:994:30:994:40 | call to taint | hash_flow.rb:994:14:994:47 | ...[...] [element :b] | provenance | |
10901090
| hash_flow.rb:996:14:996:15 | h2 [element :b] | hash_flow.rb:996:14:996:19 | ...[...] | provenance | |
10911091
| hash_flow.rb:998:14:998:15 | h2 [element :b] | hash_flow.rb:998:14:998:18 | ...[...] | provenance | |
1092+
| hash_flow.rb:1011:5:1011:5 | h [element :a] | hash_flow.rb:1012:5:1012:5 | h [element :a] | provenance | |
1093+
| hash_flow.rb:1011:9:1011:45 | call to [] [element :a] | hash_flow.rb:1011:5:1011:5 | h [element :a] | provenance | |
1094+
| hash_flow.rb:1011:14:1011:24 | call to taint | hash_flow.rb:1011:9:1011:45 | call to [] [element :a] | provenance | |
1095+
| hash_flow.rb:1012:5:1012:5 | h [element :a] | hash_flow.rb:1012:15:1012:15 | k | provenance | |
1096+
| hash_flow.rb:1012:5:1012:5 | h [element :a] | hash_flow.rb:1012:18:1012:18 | v | provenance | |
1097+
| hash_flow.rb:1012:15:1012:15 | k | hash_flow.rb:1014:14:1014:14 | k | provenance | |
1098+
| hash_flow.rb:1012:18:1012:18 | v | hash_flow.rb:1013:14:1013:14 | v | provenance | |
10921099
nodes
10931100
| hash_flow.rb:10:5:10:8 | hash [element 0] | semmle.label | hash [element 0] |
10941101
| hash_flow.rb:10:5:10:8 | hash [element :a] | semmle.label | hash [element :a] |
@@ -2251,6 +2258,14 @@ nodes
22512258
| hash_flow.rb:996:14:996:19 | ...[...] | semmle.label | ...[...] |
22522259
| hash_flow.rb:998:14:998:15 | h2 [element :b] | semmle.label | h2 [element :b] |
22532260
| hash_flow.rb:998:14:998:18 | ...[...] | semmle.label | ...[...] |
2261+
| hash_flow.rb:1011:5:1011:5 | h [element :a] | semmle.label | h [element :a] |
2262+
| hash_flow.rb:1011:9:1011:45 | call to [] [element :a] | semmle.label | call to [] [element :a] |
2263+
| hash_flow.rb:1011:14:1011:24 | call to taint | semmle.label | call to taint |
2264+
| hash_flow.rb:1012:5:1012:5 | h [element :a] | semmle.label | h [element :a] |
2265+
| hash_flow.rb:1012:15:1012:15 | k | semmle.label | k |
2266+
| hash_flow.rb:1012:18:1012:18 | v | semmle.label | v |
2267+
| hash_flow.rb:1013:14:1013:14 | v | semmle.label | v |
2268+
| hash_flow.rb:1014:14:1014:14 | k | semmle.label | k |
22542269
subpaths
22552270
hashLiteral
22562271
| hash_flow.rb:10:12:21:5 | call to [] |
@@ -2324,6 +2339,7 @@ hashLiteral
23242339
| hash_flow.rb:946:13:950:5 | call to [] |
23252340
| hash_flow.rb:971:9:971:38 | ...[...] |
23262341
| hash_flow.rb:994:14:994:47 | ...[...] |
2342+
| hash_flow.rb:1011:9:1011:45 | call to [] |
23272343
#select
23282344
| hash_flow.rb:22:10:22:17 | ...[...] | hash_flow.rb:11:15:11:24 | call to taint | hash_flow.rb:22:10:22:17 | ...[...] | $@ | hash_flow.rb:11:15:11:24 | call to taint | call to taint |
23292345
| hash_flow.rb:24:10:24:17 | ...[...] | hash_flow.rb:13:12:13:21 | call to taint | hash_flow.rb:24:10:24:17 | ...[...] | $@ | hash_flow.rb:13:12:13:21 | call to taint | call to taint |
@@ -2569,3 +2585,5 @@ hashLiteral
25692585
| hash_flow.rb:975:10:975:13 | ...[...] | hash_flow.rb:971:23:971:31 | call to taint | hash_flow.rb:975:10:975:13 | ...[...] | $@ | hash_flow.rb:971:23:971:31 | call to taint | call to taint |
25702586
| hash_flow.rb:996:14:996:19 | ...[...] | hash_flow.rb:994:30:994:40 | call to taint | hash_flow.rb:996:14:996:19 | ...[...] | $@ | hash_flow.rb:994:30:994:40 | call to taint | call to taint |
25712587
| hash_flow.rb:998:14:998:18 | ...[...] | hash_flow.rb:994:30:994:40 | call to taint | hash_flow.rb:998:14:998:18 | ...[...] | $@ | hash_flow.rb:994:30:994:40 | call to taint | call to taint |
2588+
| hash_flow.rb:1013:14:1013:14 | v | hash_flow.rb:1011:14:1011:24 | call to taint | hash_flow.rb:1013:14:1013:14 | v | $@ | hash_flow.rb:1011:14:1011:24 | call to taint | call to taint |
2589+
| hash_flow.rb:1014:14:1014:14 | k | hash_flow.rb:1011:14:1011:24 | call to taint | hash_flow.rb:1014:14:1014:14 | k | $@ | hash_flow.rb:1011:14:1011:24 | call to taint | call to taint |

ruby/ql/test/library-tests/dataflow/hash-flow/hash-flow.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import codeql.ruby.AST
66
import codeql.ruby.CFG
77
import TestUtilities.InlineFlowTest
8-
import ValueFlowTest<DefaultFlowConfig>
8+
import DefaultFlowTest
99
import ValueFlow::PathGraph
1010

1111
query predicate hashLiteral(CfgNodes::ExprNodes::HashLiteralCfgNode n) { any() }

ruby/ql/test/library-tests/dataflow/hash-flow/hash_flow.rb

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def m3()
5959
x = {a: taint(3.2), b: 1}
6060
hash2 = Hash[x]
6161
sink(hash2[:a]) # $ hasValueFlow=3.2
62-
sink(hash2[:b])
62+
sink(hash2[:b]) # $ hasTaintFlow=3.2
6363

6464
hash3 = Hash[[[:a, taint(3.3)], [:b, 1]]]
6565
sink(hash3[:a]) # $ hasValueFlow=3.3
@@ -75,7 +75,7 @@ def m3()
7575

7676
hash6 = Hash[{"a" => taint(3.6), "b" => 1}]
7777
sink(hash6["a"]) # $ hasValueFlow=3.6
78-
sink(hash6["b"])
78+
sink(hash6["b"]) # $ hasTaintFlow=3.6
7979
end
8080

8181
m3()
@@ -1000,3 +1000,17 @@ def m54(i)
10001000
end
10011001

10021002
M54.new.m54(:b)
1003+
1004+
def m55
1005+
h = taint(55.1)
1006+
keys = h.keys
1007+
sink(keys[f()]) # $ hasTaintFlow=55.1
1008+
end
1009+
1010+
def m56
1011+
h = { a: taint(56.1), taint(56.2) => :b }
1012+
h.map do |k, v|
1013+
sink(v) # $ hasValueFlow=56.1
1014+
sink(k) # $ MISSING: hasValueFlow=56.2 SPURIOUS: hasValueFlow=56.1
1015+
end
1016+
end

ruby/ql/test/query-tests/security/cwe-089/ActiveRecordInjection.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,14 @@ def show
204204
Regression.connection.execute("SELECT * FROM users WHERE id = #{permitted_params[:user_id]}")
205205
end
206206
end
207+
208+
class User
209+
scope :with_role, ->(role) { where("role = #{role}") }
210+
end
211+
212+
class UsersController < ActionController::Base
213+
def index
214+
# BAD: user input passed to scope which uses it without sanitization.
215+
@users = User.with_role(params[:role])
216+
end
217+
end

ruby/ql/test/query-tests/security/cwe-089/SqlInjection.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ edges
7676
| ActiveRecordInjection.rb:203:77:203:102 | ...[...] | ActiveRecordInjection.rb:203:43:203:104 | "SELECT * FROM users WHERE id ..." | provenance | |
7777
| ActiveRecordInjection.rb:204:69:204:84 | call to permitted_params | ActiveRecordInjection.rb:204:69:204:94 | ...[...] | provenance | |
7878
| ActiveRecordInjection.rb:204:69:204:94 | ...[...] | ActiveRecordInjection.rb:204:35:204:96 | "SELECT * FROM users WHERE id ..." | provenance | |
79+
| ActiveRecordInjection.rb:209:24:209:27 | role | ActiveRecordInjection.rb:209:38:209:53 | "role = #{...}" | provenance | |
80+
| ActiveRecordInjection.rb:215:29:215:34 | call to params | ActiveRecordInjection.rb:215:29:215:41 | ...[...] | provenance | |
81+
| ActiveRecordInjection.rb:215:29:215:41 | ...[...] | ActiveRecordInjection.rb:209:24:209:27 | role | provenance | |
7982
| ArelInjection.rb:4:5:4:8 | name | ArelInjection.rb:6:20:6:61 | "SELECT * FROM users WHERE nam..." | provenance | |
8083
| ArelInjection.rb:4:5:4:8 | name | ArelInjection.rb:7:39:7:80 | "SELECT * FROM users WHERE nam..." | provenance | |
8184
| ArelInjection.rb:4:12:4:17 | call to params | ArelInjection.rb:4:12:4:29 | ...[...] | provenance | |
@@ -201,6 +204,10 @@ nodes
201204
| ActiveRecordInjection.rb:204:35:204:96 | "SELECT * FROM users WHERE id ..." | semmle.label | "SELECT * FROM users WHERE id ..." |
202205
| ActiveRecordInjection.rb:204:69:204:84 | call to permitted_params | semmle.label | call to permitted_params |
203206
| ActiveRecordInjection.rb:204:69:204:94 | ...[...] | semmle.label | ...[...] |
207+
| ActiveRecordInjection.rb:209:24:209:27 | role | semmle.label | role |
208+
| ActiveRecordInjection.rb:209:38:209:53 | "role = #{...}" | semmle.label | "role = #{...}" |
209+
| ActiveRecordInjection.rb:215:29:215:34 | call to params | semmle.label | call to params |
210+
| ActiveRecordInjection.rb:215:29:215:41 | ...[...] | semmle.label | ...[...] |
204211
| ArelInjection.rb:4:5:4:8 | name | semmle.label | name |
205212
| ArelInjection.rb:4:12:4:17 | call to params | semmle.label | call to params |
206213
| ArelInjection.rb:4:12:4:29 | ...[...] | semmle.label | ...[...] |
@@ -257,6 +264,7 @@ subpaths
257264
| ActiveRecordInjection.rb:194:37:194:41 | query | ActiveRecordInjection.rb:199:5:199:10 | call to params | ActiveRecordInjection.rb:194:37:194:41 | query | This SQL query depends on a $@. | ActiveRecordInjection.rb:199:5:199:10 | call to params | user-provided value |
258265
| ActiveRecordInjection.rb:203:43:203:104 | "SELECT * FROM users WHERE id ..." | ActiveRecordInjection.rb:199:5:199:10 | call to params | ActiveRecordInjection.rb:203:43:203:104 | "SELECT * FROM users WHERE id ..." | This SQL query depends on a $@. | ActiveRecordInjection.rb:199:5:199:10 | call to params | user-provided value |
259266
| ActiveRecordInjection.rb:204:35:204:96 | "SELECT * FROM users WHERE id ..." | ActiveRecordInjection.rb:199:5:199:10 | call to params | ActiveRecordInjection.rb:204:35:204:96 | "SELECT * FROM users WHERE id ..." | This SQL query depends on a $@. | ActiveRecordInjection.rb:199:5:199:10 | call to params | user-provided value |
267+
| ActiveRecordInjection.rb:209:38:209:53 | "role = #{...}" | ActiveRecordInjection.rb:215:29:215:34 | call to params | ActiveRecordInjection.rb:209:38:209:53 | "role = #{...}" | This SQL query depends on a $@. | ActiveRecordInjection.rb:215:29:215:34 | call to params | user-provided value |
260268
| ArelInjection.rb:6:20:6:61 | "SELECT * FROM users WHERE nam..." | ArelInjection.rb:4:12:4:17 | call to params | ArelInjection.rb:6:20:6:61 | "SELECT * FROM users WHERE nam..." | This SQL query depends on a $@. | ArelInjection.rb:4:12:4:17 | call to params | user-provided value |
261269
| ArelInjection.rb:7:39:7:80 | "SELECT * FROM users WHERE nam..." | ArelInjection.rb:4:12:4:17 | call to params | ArelInjection.rb:7:39:7:80 | "SELECT * FROM users WHERE nam..." | This SQL query depends on a $@. | ArelInjection.rb:4:12:4:17 | call to params | user-provided value |
262270
| PgInjection.rb:14:15:14:18 | qry1 | PgInjection.rb:6:12:6:17 | call to params | PgInjection.rb:14:15:14:18 | qry1 | This SQL query depends on a $@. | PgInjection.rb:6:12:6:17 | call to params | user-provided value |

0 commit comments

Comments
 (0)