Skip to content

Commit c699afd

Browse files
committed
Ruby: instantiate NetHttpRequest even if body is not accessed
1 parent 504a0f8 commit c699afd

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

ruby/ql/lib/codeql/ruby/frameworks/http_clients/NetHttp.qll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ private import codeql.ruby.dataflow.internal.DataFlowImplForHttpClientLibraries
2121
*/
2222
class NetHttpRequest extends Http::Client::Request::Range, DataFlow::CallNode {
2323
private DataFlow::CallNode request;
24-
private DataFlow::Node responseBody;
2524
private API::Node requestNode;
25+
private boolean returnsResponseBody;
2626

2727
NetHttpRequest() {
2828
exists(string method |
@@ -32,20 +32,20 @@ class NetHttpRequest extends Http::Client::Request::Range, DataFlow::CallNode {
3232
// Net::HTTP.get(...)
3333
method = "get" and
3434
requestNode = API::getTopLevelMember("Net").getMember("HTTP").getReturn(method) and
35-
responseBody = request
35+
returnsResponseBody = true
3636
or
3737
// Net::HTTP.post(...).body
3838
method in ["post", "post_form"] and
3939
requestNode = API::getTopLevelMember("Net").getMember("HTTP").getReturn(method) and
40-
responseBody = requestNode.getAMethodCall(["body", "read_body", "entity"])
40+
returnsResponseBody = false
4141
or
4242
// Net::HTTP.new(..).get(..).body
4343
method in [
4444
"get", "get2", "request_get", "head", "head2", "request_head", "delete", "put", "patch",
4545
"post", "post2", "request_post", "request"
4646
] and
4747
requestNode = API::getTopLevelMember("Net").getMember("HTTP").getInstance().getReturn(method) and
48-
responseBody = requestNode.getAMethodCall(["body", "read_body", "entity"])
48+
returnsResponseBody = false
4949
)
5050
}
5151

@@ -64,7 +64,11 @@ class NetHttpRequest extends Http::Client::Request::Range, DataFlow::CallNode {
6464
)
6565
}
6666

67-
override DataFlow::Node getResponseBody() { result = responseBody }
67+
override DataFlow::Node getResponseBody() {
68+
if returnsResponseBody = true
69+
then result = this
70+
else result = requestNode.getAMethodCall(["body", "read_body", "entity"])
71+
}
6872

6973
/** Gets the value that controls certificate validation, if any. */
7074
DataFlow::Node getCertificateValidationControllingValue() {

ruby/ql/test/library-tests/frameworks/http_clients/HttpClients.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,9 @@ httpRequests
4343
| NetHttp.rb:13:6:13:17 | call to get |
4444
| NetHttp.rb:14:6:14:18 | call to post |
4545
| NetHttp.rb:15:6:15:17 | call to put |
46+
| NetHttp.rb:16:6:16:19 | call to patch |
4647
| NetHttp.rb:24:3:24:33 | call to get |
48+
| NetHttp.rb:29:1:29:32 | call to post |
4749
| OpenURI.rb:3:9:3:41 | call to open |
4850
| OpenURI.rb:6:9:6:34 | call to open |
4951
| OpenURI.rb:9:9:9:38 | call to open |
@@ -110,7 +112,9 @@ getFramework
110112
| NetHttp.rb:13:6:13:17 | call to get | Net::HTTP |
111113
| NetHttp.rb:14:6:14:18 | call to post | Net::HTTP |
112114
| NetHttp.rb:15:6:15:17 | call to put | Net::HTTP |
115+
| NetHttp.rb:16:6:16:19 | call to patch | Net::HTTP |
113116
| NetHttp.rb:24:3:24:33 | call to get | Net::HTTP |
117+
| NetHttp.rb:29:1:29:32 | call to post | Net::HTTP |
114118
| OpenURI.rb:3:9:3:41 | call to open | OpenURI |
115119
| OpenURI.rb:6:9:6:34 | call to open | OpenURI |
116120
| OpenURI.rb:9:9:9:38 | call to open | OpenURI |
@@ -259,8 +263,11 @@ getAUrlPart
259263
| NetHttp.rb:14:6:14:18 | call to post | NetHttp.rb:14:15:14:17 | "/" |
260264
| NetHttp.rb:15:6:15:17 | call to put | NetHttp.rb:11:21:11:41 | "https://example.com" |
261265
| NetHttp.rb:15:6:15:17 | call to put | NetHttp.rb:15:14:15:16 | "/" |
266+
| NetHttp.rb:16:6:16:19 | call to patch | NetHttp.rb:11:21:11:41 | "https://example.com" |
267+
| NetHttp.rb:16:6:16:19 | call to patch | NetHttp.rb:16:16:16:18 | "/" |
262268
| NetHttp.rb:24:3:24:33 | call to get | NetHttp.rb:24:17:24:22 | domain |
263269
| NetHttp.rb:24:3:24:33 | call to get | NetHttp.rb:24:29:24:32 | path |
270+
| NetHttp.rb:29:1:29:32 | call to post | NetHttp.rb:29:16:29:18 | uri |
264271
| OpenURI.rb:3:9:3:41 | call to open | OpenURI.rb:3:21:3:40 | "http://example.com" |
265272
| OpenURI.rb:6:9:6:34 | call to open | OpenURI.rb:6:14:6:33 | "http://example.com" |
266273
| OpenURI.rb:9:9:9:38 | call to open | OpenURI.rb:9:18:9:37 | "http://example.com" |

0 commit comments

Comments
 (0)