Skip to content

fix(proxy): use undici's own fetch (real fix for proxy 502 / UND_ERR_INVALID_ARG) - #44

Open
twilson63 wants to merge 1 commit into
mainfrom
fix/proxy-use-undici-fetch
Open

fix(proxy): use undici's own fetch (real fix for proxy 502 / UND_ERR_INVALID_ARG)#44
twilson63 wants to merge 1 commit into
mainfrom
fix/proxy-use-undici-fetch

Conversation

@twilson63

Copy link
Copy Markdown
Owner

Root cause (the real one)

The proxy passed an npm-undici Agent as the dispatcher to Node's built-in global fetch. Built-in fetch validates the dispatcher against its own bundled undici's Dispatcher class. When the npm undici version differs from Node's bundled undici, the Agent is a different class and is rejected with UND_ERR_INVALID_ARGbefore any of our lookup/connector code runs.

This explains everything we saw:

Fix

Call undici's own fetch (import { fetch as undiciFetch } from 'undici') so the dispatcher and fetch come from the same undici instance. One line of behavior change; SSRF validation and connector-based IP pinning are unchanged.

Why this is verified, unlike the prior two

proxy.test.ts doesn't mock fetch — it only covers validation/SSRF/allowlist paths that return before the network call, so it never exercised a real proxied request (which is why it stayed green through the regression). This fix was verified with a real end-to-end test: undici fetch + the pinned dispatcher + the exact response-handling path (body.getReader(), headers.forEach) → 200 for example.com/api.github.com; SSRF targets (169.254.169.254, 127.0.0.1, [::1], 10.0.0.1) still blocked.

  • tsc --noEmit clean, npm run build clean, proxy.test.ts 52/52.

Follow-up (not in this PR)

Consider pinning the Node version in render.yaml/engines so the bundled-undici drift can't bite other global-fetch usage.

🤖 Generated with Claude Code

Real root cause of the proxy 502s (UND_ERR_INVALID_ARG): the code passed
an npm-undici `Agent` as the `dispatcher` to Node's **built-in** global
`fetch`. Built-in fetch validates the dispatcher against its own bundled
undici's Dispatcher class; an Agent from the npm `undici` package is a
different class when the two undici versions differ, so it is rejected
with UND_ERR_INVALID_ARG — before any lookup/connector code runs. That is
why #42 (lookup forms) and #43 (connector override) both failed with the
exact same error, and why it worked locally (local Node's bundled undici
matches npm undici 6.26).

Fix: call undici's own `fetch` (imported from the same package as Agent),
so dispatcher and fetch are guaranteed the same version. SSRF validation
and connector IP-pinning are unchanged.

Verified locally end-to-end (undici fetch + pinned dispatcher + full
response body read): https/http public hosts return 200; SSRF targets
remain blocked. tsc + build clean; proxy.test.ts 52/52.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant