undici vulnerable to TLS certificate validation bypass via servername reused on cross-origin redirects
Description
Impact
undici's redirect interceptor builds each follow-up request from the options of the previous one. On a cross-origin redirect it reset the origin and path and removed credential headers, but kept the TLS servername, so the redirect target's certificate was validated against the first host's name instead of its own. A party that holds a valid certificate for the first host and can intercept traffic to the redirect target can impersonate the target and read or modify the redirected request and its response.
Applications are affected when they follow redirects with interceptors.redirect() and the first request carries a servername, either passed as a request option (undici 6.8.0 and later) or set by interceptors.dns() composed outside the redirect interceptor, as in agent.compose(interceptors.redirect(), interceptors.dns()) (undici 7.0.0 and later). Requests sent through a ProxyAgent tunnel without requestTls are affected in the same way. fetch() is not affected.
Patches
Upgrade to undici 6.29.2, 7.30.4 or 8.14.1. The redirect interceptor now clears servername on cross-origin redirects, so the next request derives it from the new host. Same-origin redirects keep the caller's value.
Workarounds
Compose the dns interceptor inside the redirect interceptor (agent.compose(interceptors.dns(), interceptors.redirect())), and do not pass a servername on requests that follow redirects. Alternatively, disable automatic redirects and follow them in application code.