Skip to content

fix(api): honor proxy config for direct API calls - #1543

Merged
Benjamin Barslev Nielsen (barslev) merged 1 commit into
v1.xfrom
barslev/fix-apifetch-proxy-support
Sep 17, 2026
Merged

Benjamin Barslev Nielsen (barslev) merged 1 commit into
v1.xfrom
barslev/fix-apifetch-proxy-support

Conversation

@barslev

@barslev Benjamin Barslev Nielsen (barslev) commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

LLM Description written by Claude Code:Claude Opus 5

setupSdk builds an hpagent proxy agent from SOCKET_CLI_API_PROXY, falling back to HTTPS_PROXY / https_proxy / HTTP_PROXY / http_proxy, and hands it to socket-sdk-js. apiFetch built a plain https.Agent and connected straight out, so every call that bypasses the typed SDK ignored the proxy entirely:

  • scan report data (socket scan create --report, socket scan report)
  • socket scan view
  • socket scan diff
  • socket package score
  • socket threat-feed
  • tier1 reachability finalize

That split isn't accidental — those endpoints skip the SDK because they aren't in the OpenAPI spec, as the comment in finalize-tier1-scan.mts says. The second stack was added for a good reason and silently never inherited the proxy handling.

Why this went unnoticed since #526

Setting a proxy env var doesn't mean direct egress is blocked. In most setups the proxy is a policy preference and direct outbound still works, so apiFetch ignoring it is invisible — the request just goes direct and succeeds. It only fails where the proxy is the sole route out. There, socket scan create --report dies with getaddrinfo EAI_AGAIN api.socket.dev — a DNS failure, meaning the request never left the machine — while the org-security-policy request issued concurrently in the same Promise.all through the SDK succeeds.

This is a different failure class from #1124 and the equivalent coana fix from April 2026, which both addressed CA trust behind intercepting proxies (unable to get local issuer certificate, packet length too long). In those environments DNS and direct connections work and only the certificate is wrong, so routing was never addressed.

The change

getHttpsAgent() now builds the same proxy agent setupSdk does. apiFetch needs no change — it already routes everything through _httpsRequestFetch with whatever explicit agent that function returns, so this is a single seam covering all six commands. getDefaultProxyUrl is imported from sdk.mts rather than reimplemented, so the two stacks can't drift on which env vars they read.

Also adds tests pinning proxy routing, since the absence of any such test is why this stayed invisible. They were checked against the unpatched tree — both fail without the change.

Worth a look

  • I used proxyRequestOptions for the CA on the proxy hop, while sdk.mts passes proxyConnectOptions. hpagent 1.2.0 destructures only proxyRequestOptions, so the sdk.mts value is silently dropped, and sdk.test.mts pins the wrong key against a mocked constructor so it passes regardless. Impact there is narrow — top-level ca still covers the destination handshake, so only the CA for connecting to an https:// proxy is lost. Left alone to keep this to one behaviour change; happy to fold it in.
  • NO_PROXY is still unhonored (hpagent doesn't implement it, and setupSdk has the same gap), and api.mts still has no retry logic. Both deliberate non-goals.

Note

Medium Risk
Changes outbound networking for all non-SDK API traffic; behavior is limited to environments with proxy env vars set and aligns with existing SDK proxy handling.

Overview
Direct apiFetch calls now honor the same proxy settings as the Socket SDK, fixing failures in proxy-only networks where commands like scan report/view/diff bypassed the SDK.

getHttpsAgent() in api.mts reads getDefaultProxyUrl() from sdk.mts and builds an hpagent HttpsProxyAgent when a proxy is set; otherwise it keeps the existing plain HttpsAgent behavior. When extra CA certs are configured, both the destination TLS handshake and the proxy hop (proxyRequestOptions.ca) get those certs.

Unit tests mock hpagent and assert proxy routing, CA wiring on proxy + destination, and the no-proxy fallback.

Reviewed by Cursor Bugbot for commit c277e7b. Configure here.

setupSdk builds an hpagent proxy agent from SOCKET_CLI_API_PROXY, falling
back to HTTPS_PROXY/HTTP_PROXY, but apiFetch built a plain https.Agent and
connected directly. Every call that bypasses the SDK ignored the proxy:
scan report data, scan view, scan diff, package score, threat-feed, and
tier1 reachability finalize.

Where a proxy is merely preferred this is invisible, because the direct
connection succeeds anyway. Where the proxy is the only egress route it
fails outright with `getaddrinfo EAI_AGAIN api.socket.dev`, while SDK
requests issued concurrently still succeed.

Build the same proxy agent in getHttpsAgent so both stacks agree. The
destination CA is passed as before; the CA for the hop to an https:// proxy
goes in proxyRequestOptions, which is the option hpagent actually reads.
@barslev
Benjamin Barslev Nielsen (barslev) merged commit 737c752 into v1.x Sep 17, 2026
5 checks passed
@barslev
Benjamin Barslev Nielsen (barslev) deleted the barslev/fix-apifetch-proxy-support branch September 17, 2026 11:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants