Skip to content

Fix #19294, Ruby NetHttpRequest improvements #20101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix #19294, Ruby NetHttpRequest improvements
  • Loading branch information
mschwager committed Jul 21, 2025
commit 9da94fb880b13f60d087079cafe37184e22242ff
22 changes: 18 additions & 4 deletions ruby/ql/lib/codeql/ruby/frameworks/http_clients/NetHttp.qll
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,22 @@ private import codeql.ruby.DataFlow
/**
* A `Net::HTTP` call which initiates an HTTP request.
* ```ruby
* # one-off request
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file needs to be auto-formatted, using codeql query format --in-place NetHttp.qll.

* Net::HTTP.get("http://example.com/")
* Net::HTTP.post("http://example.com/", "some_data")
* req = Net::HTTP.new("example.com")
* response = req.get("/")
*
* # connection re-use
* Net::HTTP.start("http://example.com") do |http|
* http.get("/")
* end
Comment on lines +20 to +24
Copy link
Author

@mschwager mschwager Jul 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that technically Net::HTTP.new does not open/reuse connections/sessions: https://docs.ruby-lang.org/en/master/Net/HTTP.html#method-c-new

* ```
*/
class NetHttpRequest extends Http::Client::Request::Range instanceof DataFlow::CallNode {
private DataFlow::CallNode request;
private API::Node requestNode;
API::Node requestNode;
API::Node connectionNode;
private boolean returnsResponseBody;

NetHttpRequest() {
Expand All @@ -30,20 +37,27 @@ class NetHttpRequest extends Http::Client::Request::Range instanceof DataFlow::C
|
// Net::HTTP.get(...)
method in ["get", "get_response"] and
requestNode = API::getTopLevelMember("Net").getMember("HTTP").getReturn(method) and
connectionNode = API::getTopLevelMember("Net").getMember("HTTP") and
requestNode = connectionNode.getReturn(method) and
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move requestNode = connectionNode.getReturn(method) up after line 36, and then remove the three duplicated lines.

returnsResponseBody = true
or
// Net::HTTP.post(...).body
method in ["post", "post_form"] and
requestNode = API::getTopLevelMember("Net").getMember("HTTP").getReturn(method) and
connectionNode = API::getTopLevelMember("Net").getMember("HTTP") and
requestNode = connectionNode.getReturn(method) and
returnsResponseBody = false
or
// Net::HTTP.new(..).get(..).body
// Net::HTTP.start(..) do |http| http.get(..) end
method in [
"get", "get2", "request_get", "head", "head2", "request_head", "delete", "put", "patch",
"post", "post2", "request_post", "request"
] and
requestNode = API::getTopLevelMember("Net").getMember("HTTP").getInstance().getReturn(method) and
connectionNode = [
API::getTopLevelMember("Net").getMember("HTTP").getInstance(),
API::getTopLevelMember("Net").getMember("HTTP").getMethod("start").getBlock().getParameter(0)
] and
requestNode = connectionNode.getReturn(method) and
returnsResponseBody = false
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,11 @@ def get(domain, path)
get("example.com", "/").body

Net::HTTP.post(uri, "some_body") # note: response body not accessed

http = Net::HTTP.new("https://example.com")
root_get = Net::HTTP::Get.new("/")
http.request(root_get)
Comment on lines +31 to +33
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case was already detected without the above code modifications, but I wanted to make sure that request objects are also detected correctly.


Net::HTTP.start("https://example.com") do |http|
http.get("/")
end
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy