Resolve the outbound proxy for plain Net::HTTP calls
JA
4h ago+396+396+012345678901234567890123456789-10-10-01234567890123456789
Closes #5192
Fork pre-flight: jaysbeekay/sure#931 (same commits). Bottom of a native stack; the HTTParty providers follow in #5488 above it (for #5469). Related: #4695 keeps only the Trade Republic WebSocket tunnel on top of this, as agreed there.
Problem
Three clients built a plain Net::HTTP connection with no proxy, so Net::HTTP fell back to its own ENV lookup, which reads only HTTP_PROXY, even for an HTTPS target. With only HTTPS_PROXY set, external assistant model discovery, Langfuse exports and Trade Republic session calls went direct while the AI clients beside them used the proxy. Sure's correct resolver (#4773) lived under Ai::Transport::Http, where non-AI code couldn't reasonably use it.
Change
- New
ProxyConfigurable(app/models/concerns/proxy_configurable.rb), used likeSslConfigurable(extend ProxyConfigurable). Its rules are unchanged from #4773:HTTPS_PROXY, thenHTTP_PROXY, whatever the scheme;NO_PROXYwith CIDR blocks and*; loopback direct. - Two methods for extending classes, both prefixed:
proxy_url_for(url)andnet_http_proxy_args(url). The resolution helpers stay private to the module, so they can't clash with a class's ownself.parse. net_http_proxy_argsreturns Net::HTTP's four proxy arguments, decoded as Net::HTTP's own lookup decodes them: user and password percent-decoded, IPv6 host unbracketed. It returns allnilto go direct, which also stops Net::HTTP's own ENV lookup.- The three
Net::HTTPclients pass*self.class.net_http_proxy_args(uri.to_s):Assistant::External::ModelCatalog,Eval::Langfuse::ClientandProvider::TradeRepublicSession.Assistant::External::Client#build_httpis left as #6123 made it: it already takes its proxy fromAi::Transport::Http.net_http_proxy. - Proxy values: one without a scheme (
proxy.internal:8888) is read ashttp://, as curl reads it. One that still can't be read is logged as a warning and the call goes direct, asAi::Transport::Http.proxy_or_warn(#6123) treats a proxy URL it can't parse. This fails open: a mistyped proxy sends these calls direct, outside the proxy, asmainalready does for the AI clients. The warning never carries the value, anddocs/hosting/ai.mdsays to check the logs for it. Ai::Transport::Httpis unchanged: its resolver from #6123 still serves the AI clients. Thehttp://default and the host check areProxyConfigurable's alone, so they apply to agent discovery, Langfuse exports and Trade Republic sessions only.- Docs:
docs/hosting/ai.md"Outbound proxy" lists the newly covered calls, anddocs/llm-guides/ai-pipeline.mdnamesProxyConfigurableas the resolver for plain Net::HTTP clients outsideAi::Transport::Http.
Not in this PR
- Follow-up: the Trade Republic WebSocket opens a raw
Socket.tcpand isn't proxied yet; the docs now say so. That's #4695's tunnel. - Declined: CIDR
NO_PROXYDNS lookup. For CIDR entries, the resolver resolves a host name to its address, as Ruby's ownURI#find_proxydoes. That's the existing behaviour, unchanged and out of scope here. - HTTParty providers come in the next PR in this stack; Faraday clients come later.
- Follow-up: proxy credentials in the external assistant client.
Assistant::External::Client#build_http(from #6123) passes the proxy URI'suserandpasswordtoNet::HTTP.newundecoded, so percent-encoded proxy credentials don't work there. This PR leaves that code asmainhas it.
Validation
- Resolver tests in
test/models/concerns/proxy_configurable_test.rb(the AI resolver keeps its own intest/models/ai/transport/http_test.rb), with new cases:- the four Net::HTTP arguments, or all
nil; - percent-encoded credentials and an IPv6 proxy;
- a proxy without a scheme;
- an unreadable proxy is warned about and goes direct, without the value in the warning;
- an extending class gains only the two prefixed methods.
- the four Net::HTTP arguments, or all
- Each call site goes through
HTTPS_PROXYwhenHTTP_PROXYis unset, andNO_PROXY=*sends it direct. A loopback agent is never proxied. Before the fix,Net::HTTP.newreceived:ENVas the proxy host. test/architecture/outbound_proxy_contract_test.rbfails when a file underapp/orlib/callsNet::HTTP.new,.start,.get,.get_response,.get_print,.post,.post_formorNet::HTTP::Proxybut never callsnet_http_proxy_args(orAi::Transport::Http.net_http_proxy, whichAssistant::External::Clientuses). Before the fix it listed exactly the three files.lib/llm_pricing_updater.rbis allowed:bin/update_llm_pricingruns it on a developer machine, never the app.- Mutations: each failing a test:
- reverting a call site;
- reading only
HTTP_PROXY; - passing raw credentials;
hostinstead ofhostname;- dropping the scheme default;
- dropping the warning;
- a proxy only when non-nil;
- narrowing the contract regex, twice;
- raising on an unreadable proxy instead of warning and going direct.
- Full
bin/rails teston 78789aec4 (mainat b9713e538 merged in): 21170 runs, 0 failures. The 8 errors areReleaseNotesWorkflowTest, which needsnode, and my runner has none.
Summary by CodeRabbit
- New Features
- AI model discovery, Langfuse exports, and Trade Republic login and session requests now support configured outbound proxies.
- Proxy exclusions are respected, including
NO_PROXYrules and loopback addresses. Proxy addresses without a scheme are treated as HTTP; invalid proxy settings are ignored with a warning.
- Documentation
- Updated hosting and AI pipeline guides with proxy coverage and notes on which connections are not proxied.