Skip to content

fix: handle flat alternating header arrays in cache normalizeHeaders#5536

Open
mcollina wants to merge 3 commits into
mainfrom
fix-cache-normalize-headers-flat-array
Open

fix: handle flat alternating header arrays in cache normalizeHeaders#5536
mcollina wants to merge 3 commits into
mainfrom
fix-cache-normalize-headers-flat-array

Conversation

@mcollina

@mcollina mcollina commented Jul 9, 2026

Copy link
Copy Markdown
Member

Description

The cache interceptor's normalizeHeaders function only handled two header
formats:

  1. Array-of-pairs: [['key', 'val'], ...]
  2. Object: { key: 'val', ... }

However, undici internally uses flat alternating arrays:
['key', 'val', 'key', 'val', ...]. When the cache() interceptor is placed
before dns() in a compose chain (e.g., new Agent().compose(cache(), dns())),
the DNS interceptor converts headers to flat format before passing them to
the cache interceptor, causing normalizeHeaders to throw
'opts.headers is not a valid header map'.

Reproduction

import { Agent, interceptors, request } from "undici";

const { cache, dns } = interceptors;
const cacheThenDns = new Agent().compose(cache(), dns());
const url = "https://www.google.com";
const headers = [];
const r = await request(url, { dispatcher: cacheThenDns, headers });

Fixes #5522

The cache interceptor's normalizeHeaders function only handled two header
formats: array-of-pairs ([[key, val], ...]) and plain objects ({ key: val, ...}).
However, undici internally uses flat alternating arrays ([key, val, key, val, ...]).

When the cache() interceptor is placed before dns() in a compose chain, the DNS
interceptor converts headers to flat alternating format before passing them to
the cache interceptor, causing normalizeHeaders to throw
'opts.headers is not a valid header map'.

Fixes #5522
@codecov-commenter

codecov-commenter commented Jul 9, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 83.67347% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.44%. Comparing base (cb4c2f1) to head (c16c29d).
⚠️ Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
lib/util/cache.js 83.67% 8 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5536   +/-   ##
=======================================
  Coverage   93.44%   93.44%           
=======================================
  Files         110      110           
  Lines       37328    37418   +90     
=======================================
+ Hits        34881    34966   +85     
- Misses       2447     2452    +5     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

normalizeHeaders fails with opts.headers is not a valid header map depending on interceptor ordering

3 participants