Skip to content

Clear proxy credentials when p_no_proxy excludes the host - #386

Open
mame wants to merge 1 commit into
ruby:masterfrom
mame:no-proxy-credentials
Open

mame wants to merge 1 commit into
ruby:masterfrom
mame:no-proxy-credentials

Conversation

@mame

@mame mame commented Nov 29, 2026

Copy link
Copy Markdown
Member

Net::HTTP.new dropped the proxy address and port when the host matched p_no_proxy, but kept p_user and p_pass, so the request to the host still carried a Proxy-Authorization header. Clear them as well, as is already the case when the proxy comes from the environment.

Reported-by: Claude (Anthropic) and Stephan Lipp (Ada Logics)

Net::HTTP.new dropped the proxy address and port when the host matched
p_no_proxy, but kept p_user and p_pass, so the request to the host still
carried a Proxy-Authorization header. Clear them as well, as is already
the case when the proxy comes from the environment.

Reported-by: Claude (Anthropic) and Stephan Lipp (Ada Logics)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mame
mame force-pushed the no-proxy-credentials branch from 670efaa to f11486e Compare December 1, 2026 04:48

@shioimm shioimm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The commit message says "the request to the host still carried a Proxy-Authorization header", and I'd like to confirm the situation in which this happens.

Proxy-Authorization is added only at L1742, and only when the connection is HTTPS and proxy? is true.
However, when the destination host matches p_no_proxy (so the proxy is not used for it), p_addr becomes nil at L1117.
Then @proxy_address is set to nil at L1122, and proxy? returns false.

Wouldn't the situation described in the commit message occur only when the caller explicitly runs http.proxy_address = ... on the same object (or something similar), so that proxy? returns true?

If that is the premise, I think the fix itself is reasonable, but is the commit message appropriate as written?

@mame

mame commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

@shioimm Thanks for the review. The Proxy-Authorization header in question is added here:

net-http/lib/net/http.rb

Lines 2492 to 2494 in ab56ebd

if proxy_user()
req.proxy_basic_auth proxy_user(), proxy_pass() unless use_ssl?
end

I hadn't noticed the proxy? predicate. As far as I can see, this is the only place that doesn't check proxy?, so changing the condition to if proxy? && proxy_user() might be another option.

@shioimm shioimm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks, you're right. I missed the proxy_basic_auth call in request.
It adds Proxy-Authorization whenever proxy_user is set and the connection is not HTTPS, and it does not check proxy?. So the commit message is accurate as written. Sorry for the noise.

And I think checking proxy? in that condition would also prevent other cases where proxy_user is set but no proxy is used, for example Net::HTTP.new(host, port, nil, nil, 'user', 'pass'). So I think it is fine to add it to this PR.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants