fix: normalizeHeaders throws on flat header arrays from dns re-dispatch#5535
Open
GiHoon1123 wants to merge 1 commit into
Open
fix: normalizeHeaders throws on flat header arrays from dns re-dispatch#5535GiHoon1123 wants to merge 1 commit into
GiHoon1123 wants to merge 1 commit into
Conversation
normalizeHeaders() in lib/util/cache.js (shared by the cache and deduplicate interceptors) only recognized headers as a plain object or an iterable of [key, value] pairs. It didn't recognize the flat, alternating [key, value, key, value, ...] array that the dns interceptor's withHostHeader() produces when it re-dispatches after resolving an address -- the same low-level shape core/util.js and dispatcher/client-h1.js already consume. Because compose() wraps arguments innermost-first, .compose(cache(), dns()) makes dns the outer interceptor and cache its "next", so dns's post-resolution re-dispatch lands back in cache's normalizeHeaders() with headers already in that shape, misread as a broken pairs array. Detect the flat-array shape by checking whether the first element is itself an array. Also widened the value check (both the new branch and the existing pairs branch) to accept string[] as well as string, matching what client-h1.js already accepts for repeated headers like Set-Cookie -- the stricter string-only check would have thrown again on that case.
mcollina
reviewed
Jul 9, 2026
| return true | ||
| } | ||
|
|
||
| return Array.isArray(val) && val.every(v => typeof v === 'string') |
Member
There was a problem hiding this comment.
use a proper for loop, .every() is slow.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #5535 +/- ##
=======================================
Coverage 93.44% 93.45%
=======================================
Files 110 110
Lines 37329 37352 +23
=======================================
+ Hits 34883 34908 +25
+ Misses 2446 2444 -2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This relates to...
Fixes #5522
Rationale
normalizeHeaders()inlib/util/cache.js(shared by thecacheanddeduplicateinterceptors) only recognized headers as a plain object or an iterable of[key, value]pairs. It didn't recognize the flat, alternating[key, value, key, value, ...]array that thednsinterceptor'swithHostHeader()produces when it re-dispatches after resolving an address -- the same low-level shapecore/util.jsanddispatcher/client-h1.jsalready consume on the way to the wire.Because
compose()wraps arguments innermost-first,.compose(cache(), dns())makesdnsthe outer interceptor andcacheits "next" -- sodns's post-resolution re-dispatch lands back incache's (ordeduplicate's)normalizeHeaders()with headers already in that shape, which got misread as a malformed pairs array and threw.Changes
normalizeHeaders(): detect the flat-array shape (first element is not itself an array) before falling through to the existing pairs-array handling.string[]as well asstring, matching whatclient-h1.jsalready accepts for repeated headers likeSet-Cookie-- a plain string-only check would have thrown again on that case.normalizeHeaders's JSDoc@param/@returnsto reference the actual accepted input (DispatchOptions, whose.headersisUndiciHeaders-- already covers object/array/iterable) instead of the staleRecord<string, string[] | string>.normalizeHeadersdirectly (test/cache-interceptor/cache-utils.js): flat array, empty flat array, flat array with an array value, tuple-style array with an array value, and two invalid-value cases that should still throw.test/interceptors/dns.jsreproducing the exactdns -> cacheanddns -> deduplicatere-dispatch path with a local server and mockedlookup(no real DNS/network needed).Features
N/A
Bug Fixes
normalizeHeadersfails withopts.headers is not a valid header mapdepending on interceptor ordering #5522 (Error: opts.headers is not a valid header map) when composing thecacheordeduplicateinterceptor "after"dns(e.g..compose(cache(), dns())).Breaking Changes and Deprecations
N/A -- this only widens what
normalizeHeaders()accepts; nothing that previously worked changes shape or behavior.Status