Resolve the outbound proxy for plain Net::HTTP calls

JA
4h ago
+396-10

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 like SslConfigurable (extend ProxyConfigurable). Its rules are unchanged from #4773: HTTPS_PROXY, then HTTP_PROXY, whatever the scheme; NO_PROXY with CIDR blocks and *; loopback direct.
  • Two methods for extending classes, both prefixed: proxy_url_for(url) and net_http_proxy_args(url). The resolution helpers stay private to the module, so they can't clash with a class's own self.parse.
  • net_http_proxy_args returns 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 all nil to go direct, which also stops Net::HTTP's own ENV lookup.
  • The three Net::HTTP clients pass *self.class.net_http_proxy_args(uri.to_s): Assistant::External::ModelCatalog, Eval::Langfuse::Client and Provider::TradeRepublicSession. Assistant::External::Client#build_http is left as #6123 made it: it already takes its proxy from Ai::Transport::Http.net_http_proxy.
  • Proxy values: one without a scheme (proxy.internal:8888) is read as http://, as curl reads it. One that still can't be read is logged as a warning and the call goes direct, as Ai::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, as main already does for the AI clients. The warning never carries the value, and docs/hosting/ai.md says to check the logs for it.
  • Ai::Transport::Http is unchanged: its resolver from #6123 still serves the AI clients. The http:// default and the host check are ProxyConfigurable'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, and docs/llm-guides/ai-pipeline.md names ProxyConfigurable as the resolver for plain Net::HTTP clients outside Ai::Transport::Http.

Not in this PR

  • Follow-up: the Trade Republic WebSocket opens a raw Socket.tcp and isn't proxied yet; the docs now say so. That's #4695's tunnel.
  • Declined: CIDR NO_PROXY DNS lookup. For CIDR entries, the resolver resolves a host name to its address, as Ruby's own URI#find_proxy does. 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's user and password to Net::HTTP.new undecoded, so percent-encoded proxy credentials don't work there. This PR leaves that code as main has it.

Validation

  • Resolver tests in test/models/concerns/proxy_configurable_test.rb (the AI resolver keeps its own in test/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.
  • Each call site goes through HTTPS_PROXY when HTTP_PROXY is unset, and NO_PROXY=* sends it direct. A loopback agent is never proxied. Before the fix, Net::HTTP.new received :ENV as the proxy host.
  • test/architecture/outbound_proxy_contract_test.rb fails when a file under app/ or lib/ calls Net::HTTP.new, .start, .get, .get_response, .get_print, .post, .post_form or Net::HTTP::Proxy but never calls net_http_proxy_args (or Ai::Transport::Http.net_http_proxy, which Assistant::External::Client uses). Before the fix it listed exactly the three files. lib/llm_pricing_updater.rb is allowed: bin/update_llm_pricing runs it on a developer machine, never the app.
  • Mutations: each failing a test:
    • reverting a call site;
    • reading only HTTP_PROXY;
    • passing raw credentials;
    • host instead of hostname;
    • 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 test on 78789aec4 (main at b9713e538 merged in): 21170 runs, 0 failures. The 8 errors are ReleaseNotesWorkflowTest, which needs node, 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_PROXY rules 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.