question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fetch garbles headers when used with a proxy global dispatcher

See original GitHub issue

Bug 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:closed
  • Created a year ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
KhafraDevcommented, Apr 22, 2022
0reactions
RafaelGSScommented, Apr 22, 2022

@RafaelGSS I suspect our recent headers changes might have regressed somewhere with an incorrect spread operator.

Yes, probably. @KhafraDev let me know if you need some help on that

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use a proxy with node-fetch?
In this tutorial we will see how to use a proxy with the node-fetch package. We will also discuss on how to choose...
Read more >
Fix list for IBM WebSphere Application Server V8.5
IBM WebSphere Application Server provides periodic fixes for the base and Network Deployment editions of release V8.5. The following is a complete listing ......
Read more >
GNU Wget 1.21.1-dirty Manual
Wget allows you to define global startup files ( /usr/local/etc/wgetrc by ... Another instance where you'll get a garbled file if you try...
Read more >
Push-to-talk wireless telecommunications system utilizing an ...
An Internet Protocol (IP) network interconnects the SIP Proxy server, the SIP Registrar and ... but are used to describe typical embodiments of...
Read more >
Push-to-talk telecommunications system utilizing an voice-over- ...
An Internet Protocol (IP) network interconnects the SIP Proxy server, the SIP Registrar and ... but are used to describe typical embodiments of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found