AI & Agent Dev Bug Sandbox logo
AI & Agent Dev Bug Sandbox
Back to Radar

Next.Js TestProxy Breaks Fetch To HTTP/2 Servers Due To ALPN Mismatch

When experimental.testProxy is enabled, the global socket interceptor from @mswjs/interceptors patches net.Socket.prototype.connect for all outbound sockets, including fetch/undici. In passthrough mode (no active test context), it opens a real TLS socket copying the original ALPN list. If the server negotiates HTTP/2, the interceptor still writes HTTP/1.1 plaintext and parses the response with an HTTP/1.1 parser, causing fetch failures.

highConfidence 85%Next.jsAffected V16.4.0-Canary.1

Origin Analysis

The socket-interceptor in @mswjs/interceptors (vendored by Next.js for testProxy) assumes HTTP/1.1 framing throughout. When reconstructing a passthrough connection, it copies TLS options including ALPNProtocols. On TLS servers that support h2, the connection negotiates HTTP/2, but the interceptor sends an HTTP/1.1 request and attempts to parse the HTTP/2 response with an HTTP/1.1 parser, leading to SocketError or HTTPParserError.
git clone https://github.com/melkir/testproxy-h2-repro cd testproxy-h2-repro npm install npm test The test proxy is enabled in next.config.ts. The test performs a simple fetch to an HTTPS server that supports HTTP/2. The request fails with TypeError: fetch failed or HTTPParserError.

Fixing Code Block

Edge Case Audit

Forcing HTTP/1.1 globally may break connections to servers that only support HTTP/2 (e.g., some modern APIs). The patch affects all TLS sockets in the process, including non-fetch libraries, potentially altering their behavior. It should only be enabled in test environments and must be removed once the official fix is available. Concurrency is not impacted, but TLS session reuse and ALPN caching could be affected. Rollback: remove the patch and set experimental.testProxy to false.

Ecosystem Topology