Resolve the outbound proxy for HTTParty providers

JA
2h ago
+222-40

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 from ProxyConfigurable#net_http_proxy_args for 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_proxy can'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.
  • The 30 providers and the generator template swap include HTTParty for include ProxiedHttparty.
  • SimpleFIN: PinnedConnectionAdapter now subclasses the new adapter, because SimpleFIN passes that adapter per request. Its explicit http_proxyaddr: nil for plain-http bridges is kept.
  • Docs: docs/hosting/ai.md and docs/llm-guides/providers.md note that provider syncs now honour HTTPS_PROXY and NO_PROXY.
  • An unreadable proxy: with #5487 below, a bad HTTPS_PROXY (no host Sure can read) now raises ProxyConfigurable::InvalidProxyError in 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.rb covers:

    • HTTPS_PROXY alone (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-argument Net::HTTP.new.

  • The contract test also fails on:

    • a bare include HTTParty or include ::HTTParty;
    • a direct HTTParty.get or HTTParty::Request;
    • an app/ HTTParty class whose adapter is missing, a Proc, or not built on ProxiedHttparty::ConnectionAdapter.

    Before the fix, the scan listed the 30 providers and the template.

  • 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 test on 2be62a84a: 18163 runs, 0 failures (the 8 errors are ReleaseNotesWorkflowTest's node-missing environment errors in my runner). The provider tests also pass with HTTPS_PROXY exported (1301 runs), so WebMock stubs still match through a proxy. bin/rubocop and bin/brakeman clean.


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 ProxiedHttparty concern 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_proxyaddr itself keeps its own choice.
  • The 30 providers and the provider generator template swap include HTTParty for include ProxiedHttparty; SimpleFIN's PinnedConnectionAdapter subclasses the new adapter and keeps its explicit http_proxyaddr: nil for plain-http bridges.
  • The outbound proxy contract test now fails on bare include HTTParty, direct HTTParty.get/HTTParty::Request, or an app HTTParty class lacking the adapter.
  • Docs note that provider syncs now honor HTTPS_PROXY, HTTP_PROXY, and NO_PROXY, and that these providers read a scheme-less proxy as http:// 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.