undici icon indicating copy to clipboard operation
undici copied to clipboard

DNS interceptor breaks headers of type `[string, string][]`

Open peterhirn opened this issue 1 year ago • 10 comments

Bug Description

Unable to use headers of type [string, string][], using Record<string, string> works (see below).

Reproducible By

import http from "node:http";
import { Agent, interceptors, request } from "undici";

const port = process.env.PORT ?? 3001;

const { cache, dns } = interceptors;

const good = new Agent().compose(cache());

const fail = new Agent().compose(cache(), dns());

http
  .createServer((req, res) => {
    console.log(req.headers);
    res.end();
  })
  .listen(port, async () => {
    const headers = [["foo", "bar"]];

    await request(`http://localhost:${port}`, {
      dispatcher: good,
      headers,
    });

    await request(`http://localhost:${port}`, {
      dispatcher: fail,
      headers,
    });

    const headersAsRecord = { foo: "bar" };
    await request(`http://localhost:${port}`, {
      dispatcher: fail,
      headers: headersAsRecord,
    });
  });
node index.js
{ host: 'localhost:3001', connection: 'keep-alive', foo: 'bar' }
{ '0': 'foo, bar', host: 'localhost:3001', connection: 'keep-alive' }
{ host: 'localhost:3001', connection: 'keep-alive', foo: 'bar' }

peterhirn avatar Aug 22 '25 08:08 peterhirn

The problem seems to be here: https://github.com/nodejs/undici/blob/main/lib/interceptor/dns.js#L416

Maybe I should switch all my code to use IncomingHttpHeaders (which still allows multiple values)

export type IncomingHttpHeaders = Record<string, string | string[] | undefined>

Which type do you recommend for headers?

peterhirn avatar Aug 22 '25 16:08 peterhirn

I agree with you.. there should be proper handling of headers. I think we have some functions which takes care of headers being processed properly. we just need to use them in the interceptor.

Uzlopak avatar Aug 22 '25 17:08 Uzlopak

I agree with you.. there should be proper handling of headers. I think we have some functions which takes care of headers being processed properly. we just need to use them in the interceptor.

Or just normalize the headers into one normal view and use this structure everywhere after the request, in each interceptor to check the type and format of the headers so-so

https://github.com/nodejs/undici/issues/4336

PandaWorker avatar Aug 23 '25 09:08 PandaWorker

We dont normalize headers for performance reasons.

Uzlopak avatar Aug 23 '25 09:08 Uzlopak

In any case, the headers are normalized when sending requests.

https://github.com/nodejs/undici/blob/aec609b0fc595b9d99079dfce0bb0010f7a9ca7f/lib/core/request.js#L165

PandaWorker avatar Aug 23 '25 10:08 PandaWorker

@Uzlopak could you elaborate? If the overhead isnt too bad I would be +1 for closing #4336 as well. If not we can just make changes to the dns interceptor

FelixVaughan avatar Aug 24 '25 19:08 FelixVaughan

Let's make adjustments to the dns interceptor; it should handle the headers in any way this is possibly set, not break them

metcoder95 avatar Aug 29 '25 06:08 metcoder95

We found another constellation that can lead to this problem. If you use dns in combination with redirect you can get the same issues if a redirect happens during the request regardless of the provided header format. This seems to happen due to a header cleanup inside of the RedirectHandler logic.

mlegenhausen avatar Jan 16 '26 13:01 mlegenhausen

I opened a fix for this here: https://github.com/nodejs/undici/pull/4863\n\nIt keeps DNS host-header behavior but preserves tuple/iterable header inputs by normalizing them before redispatch, and includes a regression test in .

theamodhshetty avatar Mar 05 '26 04:03 theamodhshetty

Small correction to my previous comment (shell escaped it badly):\n\nThe PR is https://github.com/nodejs/undici/pull/4863.\n\nIt preserves tuple/iterable header inputs while still setting the original DNS host header, and adds a regression test in dns interceptor tests.

theamodhshetty avatar Mar 05 '26 04:03 theamodhshetty