Skip to content

Commit ce7675e

Browse files
committed
Ruby: Identify domain in Net::HTTP requests
1 parent 2d05ea3 commit ce7675e

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ private import codeql.ruby.DataFlow
2121
class NetHttpRequest extends HTTP::Client::Request::Range {
2222
private DataFlow::CallNode request;
2323
private DataFlow::Node responseBody;
24+
private API::Node requestNode;
2425

2526
NetHttpRequest() {
26-
exists(API::Node requestNode, string method |
27+
exists(string method |
2728
request = requestNode.getAnImmediateUse() and
2829
this = request.asExpr().getExpr()
2930
|
@@ -48,10 +49,19 @@ class NetHttpRequest extends HTTP::Client::Request::Range {
4849
}
4950

5051
/**
51-
* Gets the node representing the URL of the request.
52-
* Currently unused, but may be useful in future, e.g. to filter out certain requests.
52+
* Gets a node that contributes to the URL of the request.
5353
*/
54-
override DataFlow::Node getAUrlPart() { result = request.getArgument(0) }
54+
override DataFlow::Node getAUrlPart() {
55+
result = request.getArgument(0)
56+
or
57+
// Net::HTTP.new(...).get(...)
58+
exists(API::Node new |
59+
new = API::getTopLevelMember("Net").getMember("HTTP").getInstance() and
60+
requestNode = new.getReturn(_)
61+
|
62+
result = new.getAnImmediateUse().(DataFlow::CallNode).getArgument(0)
63+
)
64+
}
5565

5666
override DataFlow::Node getResponseBody() { result = responseBody }
5767

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,13 @@
4848
| NetHttp.rb:6:8:6:50 | call to post | Net::HTTP | NetHttp.rb:6:23:6:36 | call to parse | NetHttp.rb:7:1:7:9 | call to body |
4949
| NetHttp.rb:6:8:6:50 | call to post | Net::HTTP | NetHttp.rb:6:23:6:36 | call to parse | NetHttp.rb:8:1:8:14 | call to read_body |
5050
| NetHttp.rb:6:8:6:50 | call to post | Net::HTTP | NetHttp.rb:6:23:6:36 | call to parse | NetHttp.rb:9:1:9:11 | call to entity |
51+
| NetHttp.rb:13:6:13:17 | call to get | Net::HTTP | NetHttp.rb:11:21:11:41 | "https://example.com" | NetHttp.rb:18:1:18:7 | call to body |
5152
| NetHttp.rb:13:6:13:17 | call to get | Net::HTTP | NetHttp.rb:13:14:13:16 | "/" | NetHttp.rb:18:1:18:7 | call to body |
53+
| NetHttp.rb:14:6:14:18 | call to post | Net::HTTP | NetHttp.rb:11:21:11:41 | "https://example.com" | NetHttp.rb:19:1:19:12 | call to read_body |
5254
| NetHttp.rb:14:6:14:18 | call to post | Net::HTTP | NetHttp.rb:14:15:14:17 | "/" | NetHttp.rb:19:1:19:12 | call to read_body |
55+
| NetHttp.rb:15:6:15:17 | call to put | Net::HTTP | NetHttp.rb:11:21:11:41 | "https://example.com" | NetHttp.rb:20:1:20:9 | call to entity |
5356
| NetHttp.rb:15:6:15:17 | call to put | Net::HTTP | NetHttp.rb:15:14:15:16 | "/" | NetHttp.rb:20:1:20:9 | call to entity |
57+
| NetHttp.rb:24:3:24:33 | call to get | Net::HTTP | NetHttp.rb:24:17:24:22 | domain | NetHttp.rb:27:1:27:28 | call to body |
5458
| NetHttp.rb:24:3:24:33 | call to get | Net::HTTP | NetHttp.rb:24:29:24:32 | path | NetHttp.rb:27:1:27:28 | call to body |
5559
| OpenURI.rb:3:9:3:41 | call to open | OpenURI | OpenURI.rb:3:21:3:40 | "http://example.com" | OpenURI.rb:4:1:4:10 | call to read |
5660
| OpenURI.rb:6:9:6:34 | call to open | OpenURI | OpenURI.rb:6:14:6:33 | "http://example.com" | OpenURI.rb:7:1:7:15 | call to readlines |

0 commit comments

Comments
 (0)