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
Open
fix(proxy): use undici's own fetch (real fix for proxy 502 / UND_ERR_INVALID_ARG)#44twilson63 wants to merge 1 commit into
twilson63 wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause (the real one)
The proxy passed an npm-
undiciAgentas thedispatcherto Node's built-in globalfetch. Built-in fetch validates the dispatcher against its own bundled undici'sDispatcherclass. When the npm undici version differs from Node's bundled undici, the Agent is a different class and is rejected withUND_ERR_INVALID_ARG— before any of our lookup/connector code runs.This explains everything we saw:
UND_ERR_INVALID_ARG, because the failure happens at dispatcher validation, upstream of that code.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.tsdoesn'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: undicifetch+ the pinned dispatcher + the exact response-handling path (body.getReader(),headers.forEach) → 200 forexample.com/api.github.com; SSRF targets (169.254.169.254,127.0.0.1,[::1],10.0.0.1) still blocked.tsc --noEmitclean,npm run buildclean,proxy.test.ts52/52.Follow-up (not in this PR)
Consider pinning the Node version in
render.yaml/enginesso the bundled-undici drift can't bite other global-fetch usage.🤖 Generated with Claude Code