fix: harden header iterable checks for prototype-pollution scenarios#4824
Open
fix: harden header iterable checks for prototype-pollution scenarios#4824
Conversation
Treat headers as iterable only when Symbol.iterator is an own property, or when the object has a non-plain prototype with a function iterator.\n\nThis avoids misclassifying plain header objects as iterables when Object.prototype[Symbol.iterator] is polluted, which could otherwise cause silent header loss.\n\nApply the same hardening in request, redirect, and cache header handling, and add regression coverage for polluted Object.prototype[Symbol.iterator] while preserving support for legitimate iterable headers (e.g. Map/Headers).
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #4824 +/- ##
==========================================
+ Coverage 93.17% 93.19% +0.01%
==========================================
Files 109 109
Lines 34187 34200 +13
==========================================
+ Hits 31855 31873 +18
+ Misses 2332 2327 -5 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
metcoder95
approved these changes
Feb 13, 2026
Comment on lines
+225
to
+228
| const prototype = Object.getPrototypeOf(headers) | ||
| const ownIterator = Object.prototype.hasOwnProperty.call(headers, Symbol.iterator) | ||
| const hasIterator = ownIterator || (prototype != null && prototype !== Object.prototype && typeof headers[Symbol.iterator] === 'function') | ||
| const entries = hasIterator ? headers : Object.entries(headers) |
Member
There was a problem hiding this comment.
Shall we maybe create an utility function to share it across?
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.
Summary
This PR hardens header processing against prototype-pollution side effects.
Undici previously used inherited
Symbol.iteratorlookups for some header inputs. IfObject.prototype[Symbol.iterator]was polluted, plain header objects could be misclassified as iterables and silently lose headers.This is not a standalone vulnerability in Undici; it requires a pre-existing prototype-pollution primitive. This change improves resilience in that scenario.
Changes
Updated iterable detection in:
lib/core/request.jslib/handler/redirect-handler.jslib/util/cache.jsNew behavior:
Symbol.iteratoris an own property, orMap,Headers)Object.keys/Object.entries.Tests
test/request.jsObject.prototype[Symbol.iterator]is pollutedtest/interceptors/redirect.jstest/cache-interceptor/cache-utils.js(new)normalizeHeadersworks for polluted plain objectsnormalizeHeadersstill works forMapheadersVerification
npx borp -p "test/request.js"npx borp -p "test/interceptors/redirect.js"npx borp -p "test/cache-interceptor/cache-utils.js"npx borp -p "test/interceptors/cache.js"npm run lint -- lib/core/request.js lib/util/cache.js lib/handler/redirect-handler.js test/request.js test/interceptors/redirect.js test/cache-interceptor/cache-utils.js