GeezerVison is showing github.com — fix(cookies): keep a cookie named __proto__ in getCookies by luantaraschi · Pull Request #5663 · nodejs/undici
Conversation
luantaraschi
commented
Aug 7, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
This relates to...
No open issue. Found while reading lib/web/cookies/.
Rationale
getCookies() builds its result on a plain object literal:
const out = {}
for (const piece of cookie.split(';')) {
const [name, ...value] = piece.split('=')
out[name.trim()] = value.join('=')
}
__proto__ is a valid cookie name (it is a token per RFC 6265), but assigning it on a plain object reaches the Object.prototype setter instead of creating an own property. The setter only accepts an object or null, and the assigned value here is always a string, so it does nothing and the cookie is dropped:
const headers = new Headers()
headers.set('Cookie', '__proto__=foo; bar=baz')
getCookies(headers) // { bar: 'baz' }, the first cookie is gone
getCookies(headers).__proto__ // Object.prototype, not the string 'foo'
The second line is the part that bothered me more than the missing key. getCookies is typed Record<string, string>, so a caller that indexes it by an attacker-influenced name gets an object back where the types promise a string.
There is no prototype pollution here: the setter refuses a string, so Object.prototype is untouched. The bug is silent data loss plus a broken type contract.
Changes
getCookies now builds its record with { __proto__: null }, which is already the pattern used in lib/core/util.js, lib/util/runtime-features.js, lib/web/eventsource/eventsource.js and lib/web/fetch/formdata.js.
The existing tests all use assert.deepEqual, which does not compare prototypes, so none of them needed changing. All 92 tests under test/cookie/ pass.
Features
N/A
Bug Fixes
A cookie named __proto__ is now returned by getCookies() like any other name.
Breaking Changes and Deprecations
The returned object no longer inherits from Object.prototype, so calling cookies.hasOwnProperty(name) on it stops working. Object.hasOwn(cookies, name) and name in cookies still work, as does every form of indexing and iteration.
Status
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
getCookies built its result on a plain object literal, so assigning the
__proto__ key reached the Object.prototype setter instead of creating an
own property. The assigned value is a string, so the setter is a no-op
and the cookie is dropped from the returned record.
Reading that key back also returned Object.prototype, which breaks the
documented Record<string, string> contract.
Use a null-prototype object, as lib/ already does in six other places.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
(However, I think this is a bad idea. We should throw hard when proto appears).
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Aug 8, 2026
luantaraschi
commented
Aug 8, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for the review. I checked the three failing jobs, and none of them exercises this change, which only touches lib/web/cookies/.
- The two Node 24 Ubuntu jobs fail in
test/http2-request-never-settles.js. The same test failed on main in run 31085361670 on 6 August, before this branch existed.
- The Node 22 macOS job times out in
test/fetch/content-length.js at 180006 ms, matching the 180-second limit rather than an assertion failure.
On __proto__, I followed the behavior already used in two places:
Both preserve keys received from the wire with Object.defineProperty instead of rejecting them. This PR uses the same approach in getCookies.
__proto__ is also a valid cookie name under the token grammar in RFC 6265 section 4.1.1. Throwing from getCookies would therefore let an incoming cookie break its caller. If the project prefers rejecting this key, I think that policy should be applied consistently to all three parsing paths rather than only to cookies. I am happy to help with that as a separate change.
A rerun should be enough for this PR. I can rebase if you would prefer a fresh run that way.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Thanks for the review. One thing to flag, and it is on me: about a minute after you approved I merged main into the branch to get the old red run out of the way, and the new head now reports no checks at all.
I cannot start workflow runs in this repository myself, so it needs someone with write access to kick them off before this can land. Sorry for the extra step, the timing was clumsy.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.46%. Comparing base (692c02b) to head (90d7914).
⚠️ Report is 3 commits behind head on main.
Additional details and impacted files
@@ Coverage Diff @@
## main #5663 +/- ##
=======================================
Coverage 93.46% 93.46%
=======================================
Files 110 110
Lines 38777 38779 +2
=======================================
+ Hits 36244 36246 +2
Misses 2533 2533
☔ 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.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
CI is red on two jobs, and neither failure comes from this change.
Node 26 on macOS failed in test/issue-5087.js. The test itself passed, then late activity after it ended surfaced read ECONNRESET, which the runner reported as a file level failure. Node 25 on ubuntu with WASM SIMD disabled failed in test/http2-request-never-settles.js. Neither file touches cookies, and Test unit runs borp -p "test/*.js", which does not cover test/cookie/*.js.
Locally on this branch that same step passes: 1510 tests, no failures (1506 passed, 4 skipped). npm run test:cookies passes 92 of 92.
The flakiness is not specific to this branch either. Of the last ten CI runs on main, six failed the Node 24 coverage job and one failed the same Node 25 WASM SIMD job. test/issue-5087.js still has no guard for late session errors, unlike the h2 tests hardened in #5674 and #5686.
I can rebase onto current main if a fresh run would help.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This was referenced Sep 21, 2026
mcollina
pushed a commit
that referenced
this pull request
Oct 15, 2026
* fix(cookies): keep a cookie named __proto__ in getCookies (#5663)
getCookies built its result on a plain object literal, so assigning the
__proto__ key reached the Object.prototype setter instead of creating an
own property. The assigned value is a string, so the setter is a no-op
and the cookie is dropped from the returned record.
Reading that key back also returned Object.prototype, which breaks the
documented Record<string, string> contract.
Use a null-prototype object, as lib/ already does in six other places.
Co-authored-by: Carlos Fuentes <[email protected]>
(cherry picked from commit b73952a)
* fix(cookies): keep an Expires of 0 when serializing a cookie (#5808)
The Expires branch tested cookie.expires for truthiness, so a numeric 0 -- the
Unix epoch, and the canonical way to say "expire this cookie now" -- dropped
the attribute entirely. expires is typed Date | number, and the Max-Age check
directly above already tests the type rather than truthiness for the same
reason.
(cherry picked from commit 6933139)
* fix(cookies): ignore a numeric expires outside the Date range (#5832)
stringify guarded expires with a string comparison that only works for the
Date form:
if (cookie.expires != null && cookie.expires.toString() !== 'Invalid Date')
A Date whose time value is NaN stringifies to "Invalid Date" and is skipped,
but a number never does: NaN.toString() is "NaN" and 1e16.toString() is
"10000000000000000". The guard therefore always passes for a number outside
the range representable by Date, and toIMFDate reads NaN/undefined from every
getter and splices those tokens into the header value:
setCookie(headers, { name: 'Space', value: 'Cat', expires: 1e16 })
// Space=Cat; Expires=undefined, undefined undefined NaN undefined:undefined:undefined GMT
The emitted value is not a valid IMF-fixdate. The Max-Age branch a few lines
above already tests the type rather than a stringified value.
Treat a number as absent only when it is not a valid time value, keeping the
existing Date behaviour. Name the upper bound so it is clear it is the largest
time value a Date can represent rather than a magic number.
Non-finite numbers reach stringify already coerced to 0 by the unsigned long
long converter in lib/web/cookies/index.js, so they keep serializing as the
epoch; that coercion is a separate concern.
Signed-off-by: Xia Chao <[email protected]>
Co-authored-by: Xia Chao <[email protected]>
(cherry picked from commit 78aa1a0)
---------
Signed-off-by: Xia Chao <[email protected]>
Co-authored-by: Luan Taraschi <[email protected]>
Co-authored-by: Carlos Fuentes <[email protected]>
Co-authored-by: Ngo Quoc Viet <[email protected]>
Co-authored-by: Xia Chao <[email protected]>
Co-authored-by: Xia Chao <[email protected]>
This was referenced Oct 24, 2026
Reviewers
mcollina
mcollina approved these changes
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|