Allow environment proxies to handle localhost/loopback destinations via an explicit opt-in
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 38/100
Research direction
Start with URI::Generic#find_proxy in the linked ruby/uri implementation and trace how Net::HTTP obtains environment proxy settings. Compare the localhost example with the current default behavior, then define an explicit opt-in that proxies loopback destinations while preserving the existing bypass by default; completion should cover the requested API or setting and its behavior.
Written by the indexing model from the issue text.
Description
Net::HTTP automatically bypasses environment-configured proxies when the destination resolves to a loopback address.
Could an explicit opt-in be added that allows Net::HTTP to use an environment proxy for localhost and other loopback destinations while preserving the current default behavior?
Current behavior
Given:
ENV["http_proxy"] = "http://127.0.0.1:8888"
require "net/http"
http = Net::HTTP.new("localhost", 7700)
p http.proxy_from_env? # true
p http.proxy? # false
Net::HTTP discovers proxies through URI::Generic#find_proxy. That method resolves the destination hostname and unconditionally returns nil for addresses matching 127.* or ::1:
https://github.com/ruby/uri/blob/master/lib/uri/generic.rb
This happens independently of NO_PROXY. An empty NO_PROXY therefore cannot request that loopback traffic use the proxy.
Supplying the proxy explicitly to Net::HTTP.new works, but that requires applications and libraries to replace normal environment-based proxy discovery.
Use case
I encountered this in an OpenAI Codex sandbox on macOS.
The sandbox injects an HTTP proxy into the process environment and permits network access through that proxy. Direct socket connections are restricted by the sandbox policy, including direct connections to a local service in this configuration.
The result was:
curl http://localhost:7700/health
# succeeds through the injected proxy
Net::HTTP.get(URI("http://localhost:7700/health"))
# Errno::EPERM: Operation not permitted - connect(2) for 127.0.0.1:7700
The local destination was allowed by the sandbox configuration and was reachable through its proxy broker. However, Ruby detected that localhost resolved to loopback, bypassed the environment proxy, and attempted the prohibited direct connection.
This also affects libraries that use Net::HTTP internally. They may not expose a way to pass an explicit proxy, while environment variables are the standard integration point provided by sandboxed and containerized environments.
curl/libcurl behaves differently: it uses the configured proxy for localhost unless NO_PROXY or CURLOPT_NOPROXY excludes it.
Behavior in other HTTP libraries
There is no universal standard for proxying loopback destinations, but several implementations either make the behavior configurable or rely entirely on the normal proxy exclusion configuration.
- Go
net/http:ProxyFromEnvironmentalso bypasseslocalhostand loopback addresses unconditionally. There is no environment variable that reverses this decision. Applications can override it by configuringTransport.Proxy, for example withhttp.ProxyURL. - Java: the default
http.nonProxyHostsvalue excludeslocalhost,127.*, and[::1]. Unlike Ruby’s hardcoded check, this property can be replaced or cleared when loopback traffic needs to use the proxy. - .NET: local bypass behavior is controlled by
WebProxy.BypassProxyOnLocal. Applications can set it tofalseto send local destinations through the proxy. - curl/libcurl: loopback destinations are not implicitly excluded. They use the configured proxy unless
NO_PROXYorCURLOPT_NOPROXYexplicitly matches the destination. - Python
urllib.request: when using environment proxy settings, bypasses are generally controlled throughNO_PROXY. Without a matching exclusion, localhost may use the proxy, although system proxy settings can affect behavior on macOS and Windows.
Ruby is therefore not alone in bypassing loopback by default. However, unlike Java and .NET, its environment-proxy behavior does not expose a switch to disable that bypass. Go has a similar unconditional rule in ProxyFromEnvironment, but provides a replaceable proxy-selection callback at the HTTP transport level. curl/libcurl takes the opposite approach and leaves loopback behavior entirely to the proxy exclusion configuration.
Requested behavior
It would be useful to have an explicit, backwards-compatible way to disable the automatic loopback bypass when proxy configuration comes from the environment.
For example, this could be:
- a
Net::HTTPoption; - an option passed to
URI::Generic#find_proxy; or - a dedicated environment setting such as
NET_HTTP_PROXY_LOOPBACK=1.
The exact API is less important than retaining the current default behavior while allowing controlled environments to opt into proxying loopback destinations.
NO_PROXY cannot express this because it only adds destinations that bypass the proxy; it cannot remove Ruby’s built-in loopback exclusion.
Workaround
Applications can parse the proxy environment variables themselves and pass the proxy explicitly:
proxy = URI(ENV.fetch("http_proxy"))
http = Net::HTTP.new(
"localhost",
7700,
proxy.host,
proxy.port,
proxy.user,
proxy.password
)
This works, but duplicates proxy parsing and bypass behavior, does not help libraries that instantiate Net::HTTP internally, and couples application code to sandbox infrastructure.
Versions
ruby 4.0.5 (2026-05-20 revision 64336ffd0e) +PRISM [arm64-darwin25]
net-http 0.9.1
uri 1.1.1
I realize that the actual loopback check currently belongs to URI::Generic#find_proxy. I am filing this here because it surfaces through Net::HTTP’s environment-proxy behavior, but I am happy to move the request to ruby/uri if that is the preferred location.
- Dominant language
- Ruby
- Stars
- 148
- Forks
- 95
- Avg merge
- 10h 54m
- Merged PRs (30d)
- 4
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from ruby/net-http
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
Difficulty 4/5 3-5 days Newbie friendliness 55/100
-
bug documentation
Difficulty 4/5 3-5 days Newbie friendliness 45/100
Similar issues
-
user-reported
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
Kong/developer.konghq.com#7316 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
TheOdinProject/curriculum#31408 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
bensheldon/good_job#1816 · 5 comments ·
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
notch8/utk_knapsack#148 ·
-
Difficulty 1/5 Under an hour Newbie friendliness 78/100
Homebrew/homebrew-cask#288729 · 1 comment ·