Resolve the outbound proxy for HTTParty providers
Closes #5469
Fork pre-flight: jaysbeekay/sure#932 (same commits). Stacked on #5487 (for #5192), which adds ProxyConfigurable.
Problem
Every provider that did include HTTParty (30 under app/models/provider, plus the provider generator's template) set no proxy. HTTParty therefore built Net::HTTP.new(host, port), and Net::HTTP's own ENV lookup reads only HTTP_PROXY, even for an HTTPS target. With only HTTPS_PROXY set, every one of those bank, brokerage and crypto providers connected directly.
Change
ProxiedHttparty(app/models/concerns/proxied_httparty.rb) includes HTTParty and sets a connection adapter. Unless the request already sets:http_proxyaddr, the adapter fills the four proxy options fromProxyConfigurable#net_http_proxy_argsfor the URL that connection opens, at request time.- Per-request hosts, redirects, hosts read from ENV, server-chosen hosts and loopback are therefore each decided per host. A class-level
http_proxycan't do that: 16 of the 30 classes have no fixed host. - When the resolver says go direct, all four options are set to
nil, which also stops Net::HTTP's own lookup.
- Per-request hosts, redirects, hosts read from ENV, server-chosen hosts and loopback are therefore each decided per host. A class-level
- The 30 providers and the generator template swap
include HTTPartyforinclude ProxiedHttparty. - SimpleFIN:
PinnedConnectionAdapternow subclasses the new adapter, because SimpleFIN passes that adapter per request. Its explicithttp_proxyaddr: nilfor plain-http bridges is kept. - Docs:
docs/hosting/ai.mdanddocs/llm-guides/providers.mdnote that provider syncs now honourHTTPS_PROXYandNO_PROXY. - An unreadable proxy: with #5487 below, a bad
HTTPS_PROXY(no host Sure can read) now raisesProxyConfigurable::InvalidProxyErrorin every HTTParty provider sync, as well as the AI calls, instead of connecting directly.
Not in this PR
Faraday clients (market data, SnapTrade) still use Faraday's own lookup; they're the next step. The Trade Republic WebSocket is still unproxied (see the PR below this one).
Validation
-
test/models/concerns/proxied_httparty_test.rbcovers:HTTPS_PROXYalone (Provider::Kraken);NO_PROXY=*;- per-host resolution at request time;
- loopback;
- no variables set (and no ENV fallback either);
- a proxy without a scheme;
- encoded credentials and an IPv6 proxy host;
- SimpleFIN pinning with and without the proxy.
The
HTTPS_PROXY,NO_PROXY=*and per-host tests failed before the fix with a two-argumentNet::HTTP.new. -
The contract test also fails on:
- a bare
include HTTPartyorinclude ::HTTParty; - a direct
HTTParty.getorHTTParty::Request; - an
app/HTTParty class whose adapter is missing, a Proc, or not built onProxiedHttparty::ConnectionAdapter.
Before the fix, the scan listed the 30 providers and the template.
- a bare
-
Mutations: 7, each failing a test:
- dropping the adapter;
- dropping the explicit-option guard;
- leaving direct options unset;
- reverting a provider to
include ::HTTParty; - re-parenting SimpleFIN's adapter;
- removing the Class guard;
- narrowing the scan.
-
Full
bin/rails teston 2be62a84a: 18163 runs, 0 failures (the 8 errors areReleaseNotesWorkflowTest'snode-missing environment errors in my runner). The provider tests also pass withHTTPS_PROXYexported (1301 runs), so WebMock stubs still match through a proxy.bin/rubocopandbin/brakemanclean.
Summary by cubic
Makes the 30 HTTParty provider clients honor Sure's outbound proxy resolution, so provider syncs route through HTTPS_PROXY and honor NO_PROXY like the AI clients do, instead of connecting directly whenever only HTTPS_PROXY is set (Net::HTTP's own env lookup reads only HTTP_PROXY).
- New
ProxiedHttpartyconcern includes HTTParty and sets a connection adapter that resolves the proxy per URL at request time, covering per-request hosts, redirects, env-configured hosts, and loopback. A request that sets:http_proxyaddritself keeps its own choice. - The 30 providers and the provider generator template swap
include HTTPartyforinclude ProxiedHttparty; SimpleFIN'sPinnedConnectionAdaptersubclasses the new adapter and keeps its explicithttp_proxyaddr: nilfor plain-http bridges. - The outbound proxy contract test now fails on bare
include HTTParty, directHTTParty.get/HTTParty::Request, or an appHTTPartyclass lacking the adapter. - Docs note that provider syncs now honor
HTTPS_PROXY,HTTP_PROXY, andNO_PROXY, and that these providers read a scheme-less proxy ashttp://and ignore an unreadable one with a warning. - Faraday-based clients and the Trade Republic WebSocket keep their own lookups and are the next step.
Summary by CodeRabbit
-
New Features
- Provider connections now honor outbound proxy settings, including proxy exclusions for specified hosts.
- SimpleFIN HTTPS connections retain their pinned connection behavior when using a proxy.
-
Documentation
- Added guidance on configuring outbound proxies for provider syncs and integrating proxy support in provider clients.