Skip to content

Commit 80ae017

Browse files
committed
Ruby: Track flow into ActiveRecord scopes
1 parent 4177c38 commit 80ae017

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

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/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)