Fetch garbles headers when used with a proxy global dispatcher
See original GitHub issueBug Description
When a ProxyAgent is set as the global dispatcher, the headers sent in fetch requests are completely broken: they’re transformed from header-name: header-value
into 1: header-name
, 2: header-value
, basically flattening them into an array and using the indexes as the header names.
Reproducible By
Use any proxy, for quick testing I’d suggest launching tinyproxy on port 8888 locally via Docker:
docker run -it --rm -p 8888:8888 monokal/tinyproxy ANY
Then use Undici with that proxy:
let Undici = require('undici');
// Use the proxy as the global dispatcher:
Undici.setGlobalDispatcher(new Undici.ProxyAgent('http://localhost:8888'))
// Send a fetch request to httpbin, which echoes the incoming headers:
Undici.fetch('http://httpbin.org/get', {
headers: { 'Test-header': 'value' }
}).then(r => r.text()).then(console.log)
This prints:
{
"args": {},
"headers": {
"0": "accept",
"1": "*/*",
"10": "user-agent",
"11": "undici",
"2": "accept-encoding",
"3": "gzip, deflate",
"4": "accept-language",
"5": "*",
"6": "sec-fetch-mode",
"7": "cors",
"8": "test-header",
"9": "value",
"Host": "httpbin.org",
"X-Amzn-Trace-Id": "Root=1-6170a3-4c398f08ae255c83"
},
"origin": "ip.ip.ip.ip",
"url": "http://httpbin.org/get"
}
I’ve tested this with a couple of different proxies and looked at the traffic in wireshark. This isn’t being broken on the proxy site - those are the real headers that Undici is sending to the proxy itself.
Expected Behavior
The headers should be sent in their original format. Undici.request
does have the correct behaviour:
// (after setting the global dispatcher as above)
Undici.request('http://httpbin.org/get', {
headers: { 'Test-header': 'value' }
}).then(r => r.body.pipe(process.stdout))
That correct prints:
{
"args": {},
"headers": {
"Host": "httpbin.org",
"Test-Header": "value",
"X-Amzn-Trace-Id": "Root=1-62624e-c8afe258cd33244f8"
},
"origin": "ip.ip.ip.ip",
"url": "http://httpbin.org/get"
}
Using Undici.fetch
without a proxy also works correctly, it’s only when a global dispatcher is configured that this breaks.
Environment
Ubuntu 20.04.4, Node 16.14.2, Undici v5.0.0
Issue Analytics
- State:
- Created a year ago
- Comments:8 (8 by maintainers)
It’s exactly that, noticed it pretty quickly 😄 https://github.com/nodejs/undici/blob/1dc2977c94583efbecd5b6791b5a091d18a2ceeb/lib/proxy-agent.js#L26
Yes, probably. @KhafraDev let me know if you need some help on that