Skip to content

Commit 0626d69

Browse files
committed
Ruby: Recognise rack applications
This is a basic first step in modelling rack apps. We recognise classes that look like rack applications and then treat the argument to `call` in the same way that we treat `request.env` in ActionController classes. This finds a TP in CVE-2021-43840.
1 parent ce06df3 commit 0626d69

File tree

7 files changed

+120
-17
lines changed

7 files changed

+120
-17
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private import codeql.ruby.frameworks.ActiveSupport
1616
private import codeql.ruby.frameworks.Archive
1717
private import codeql.ruby.frameworks.Arel
1818
private import codeql.ruby.frameworks.GraphQL
19+
private import codeql.ruby.frameworks.Rack
1920
private import codeql.ruby.frameworks.Rails
2021
private import codeql.ruby.frameworks.Railties
2122
private import codeql.ruby.frameworks.Stdlib

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,9 @@ class CallableNode extends ExprNode {
10021002
/** Gets the `n`th positional parameter. */
10031003
ParameterNode getParameter(int n) { this.getParameterPosition(result).isPositional(n) }
10041004

1005+
/** Gets the number of parameters of this callable. */
1006+
final int getNumberOfParameters() { result = count(this.getParameter(_)) }
1007+
10051008
/** Gets the keyword parameter of the given name. */
10061009
ParameterNode getKeywordParameter(string name) {
10071010
this.getParameterPosition(result).isKeyword(name)

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

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -301,27 +301,39 @@ private module Request {
301301
override Http::Server::RequestInputKind getKind() { result = Http::Server::bodyInputKind() }
302302
}
303303

304-
/**
305-
* A method call on `request` which returns the rack env.
306-
* This is a hash containing all the information about the request. Values
307-
* under keys starting with `HTTP_` are user-controlled.
308-
*/
309-
private class EnvCall extends RequestMethodCall {
310-
EnvCall() { this.getMethodName() = ["env", "filtered_env"] }
311-
}
304+
private module Env {
305+
abstract private class Env extends DataFlow::LocalSourceNode { }
306+
307+
/**
308+
* A method call on `request` which returns the rack env.
309+
* This is a hash containing all the information about the request. Values
310+
* under keys starting with `HTTP_` are user-controlled.
311+
*/
312+
private class RequestEnvCall extends DataFlow::CallNode, Env {
313+
RequestEnvCall() { this.getMethodName() = ["env", "filtered_env"] }
314+
}
312315

313-
/**
314-
* A read of a user-controlled parameter from the request env.
315-
*/
316-
private class EnvHttpAccess extends DataFlow::CallNode, Http::Server::RequestInputAccess::Range {
317-
EnvHttpAccess() {
318-
this = any(EnvCall c).getAMethodCall("[]") and
319-
this.getArgument(0).getConstantValue().getString().regexpMatch("^HTTP_.+")
316+
private import codeql.ruby.frameworks.Rack
317+
318+
private class RackEnv extends Env {
319+
RackEnv() { this = any(Rack::AppCandidate app).getEnv().getALocalUse() }
320320
}
321321

322-
override Http::Server::RequestInputKind getKind() { result = Http::Server::headerInputKind() }
322+
/**
323+
* A read of a user-controlled parameter from the request env.
324+
*/
325+
private class EnvHttpAccess extends DataFlow::CallNode, Http::Server::RequestInputAccess::Range {
326+
EnvHttpAccess() {
327+
this = any(Env c).getAMethodCall("[]") and
328+
exists(string key | key = this.getArgument(0).getConstantValue().getString() |
329+
key.regexpMatch("^HTTP_.+") or key = "PATH_INFO"
330+
)
331+
}
323332

324-
override string getSourceType() { result = "ActionDispatch::Request#env[]" }
333+
override Http::Server::RequestInputKind getKind() { result = Http::Server::headerInputKind() }
334+
335+
override string getSourceType() { result = "ActionDispatch::Request#env[]" }
336+
}
325337
}
326338
}
327339

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Provides modeling for the Rack library.
3+
*/
4+
5+
private import codeql.ruby.controlflow.CfgNodes::ExprNodes
6+
private import codeql.ruby.DataFlow
7+
8+
/**
9+
* Provides modeling for the Rack library.
10+
*/
11+
module Rack {
12+
/**
13+
* A class that may be a rack application.
14+
* This is a class that has a `call` method that takes a single argument
15+
* (traditionally called `env`) and returns a rack-compatible response.
16+
*/
17+
class AppCandidate extends DataFlow::ClassNode {
18+
private DataFlow::MethodNode call;
19+
20+
AppCandidate() {
21+
call = this.getInstanceMethod("call") and
22+
call.getNumberOfParameters() = 1 and
23+
isRackResponse(call.getAReturningNode())
24+
}
25+
26+
DataFlow::ParameterNode getEnv() { result = call.getParameter(0) }
27+
}
28+
29+
private predicate isRackResponse(DataFlow::Node r) {
30+
// [status, headers, body]
31+
exists(ArrayLiteralCfgNode arr | arr.getNumberOfArguments() = 3 |
32+
r.asExpr() = arr
33+
or
34+
exists(DataFlow::LocalSourceNode n | n.asExpr() = arr | n.flowsTo(r))
35+
)
36+
}
37+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| rack.rb:1:1:5:3 | HelloWorld | rack.rb:2:12:2:14 | env |
2+
| rack.rb:7:1:16:3 | Proxy | rack.rb:12:12:12:18 | the_env |
3+
| rack.rb:18:1:31:3 | Logger | rack.rb:24:12:24:14 | env |
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
private import codeql.ruby.frameworks.Rack
2+
private import codeql.ruby.DataFlow
3+
4+
query predicate rackApps(Rack::AppCandidate c, DataFlow::ParameterNode env) { env = c.getEnv() }
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class HelloWorld
2+
def call(env)
3+
[200, {'Content-Type' => 'text/plain'}, ['Hello World']]
4+
end
5+
end
6+
7+
class Proxy
8+
def initialize(app)
9+
@app = app
10+
end
11+
12+
def call(the_env)
13+
status, headers, body = @app.call(the_env)
14+
[status, headers, body]
15+
end
16+
end
17+
18+
class Logger
19+
def initialize(app, logger = nil)
20+
@app = app
21+
@logger = logger
22+
end
23+
24+
def call(env)
25+
began_at = Utils.clock_time
26+
status, header, body = @app.call(env)
27+
header = Utils::HeaderHash.new(header)
28+
body = BodyProxy.new(body) { log(env, status, header, began_at) }
29+
[status, header, body]
30+
end
31+
end
32+
33+
class Foo
34+
def not_call(env)
35+
[1, 2, 3]
36+
end
37+
end
38+
39+
class Bar
40+
def call(env)
41+
nil
42+
end
43+
end

0 commit comments

Comments
 (0)