GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix(cookies): ignore a numeric expires outside the Date range by xia-chao · Pull Request #5832 · nodejs/undici

Conversation

@xia-chao

@xia-chao xia-chao commented Sep 15, 2026

Copy link
Copy Markdown
Contributor

What this fixes

setCookie produced a broken Expires attribute when expires was a number outside the range a Date can hold:

setCookie(headers, { name: 'Space', value: 'Cat', expires: 1e16 })
// before: Space=Cat; Expires=undefined, undefined undefined NaN undefined:undefined:undefined GMT
// after:  Space=Cat

Fixes #5831.

Why

expires can be a Date or a number, but the check in stringify only understood the Date form:

if (cookie.expires != null && cookie.expires.toString() !== 'Invalid Date') {

A bad Date becomes the text "Invalid Date", so it gets skipped. A number never does - NaN.toString() is "NaN" and (1e16).toString() is "10000000000000000" - so the check always passed and the value reached toIMFDate, which pasted undefined/NaN straight into the header.

The change

Check the number itself, and leave the Date behaviour exactly as it was:

if (cookie.expires != null) {
  const expiresMs = cookie.expires instanceof Date
    ? cookie.expires.getTime()
    : cookie.expires

  if (typeof expiresMs === 'number' && Number.isFinite(expiresMs) && Math.abs(expiresMs) <= 8640000000000000) {
    out.push(`Expires=${toIMFDate(cookie.expires)}`)
  }
}

8640000000000000 is the largest value a Date can hold, so anything passing the check is safe to format. This is the same approach the Max-Age branch a few lines above already uses.

Infinity, -Infinity and NaN are turned into 0 by the converter in lib/web/cookies/index.js before this code runs, so they still serialize as the epoch. That is a separate problem and this change does not touch it.

Tests

Added cases in test/cookie/cookies.js, next to the existing numeric-expires test:

  • out-of-range numbers (8640000000000001, 1e16, Number.MAX_SAFE_INTEGER) omit the attribute
  • the largest valid value 8640000000000000 still serializes
  • Infinity, -Infinity, NaN still serialize as the epoch
  • an invalid Date is still omitted
node --test test/cookie/*.js test/utils/date.js
# 99 passing, 0 failing

Reverting only lib/web/cookies/util.js makes the new cases fail with:

AssertionError: 'Space=Cat; Expires=undefined, undefined undefined NaN undefined:undefined:undefined GMT' == 'Space=Cat'

npx standard lib/web/cookies/util.js test/cookie/cookies.js is clean.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
gijs-martens
gijs-martens approved these changes Sep 17, 2026
View reviewed changes
metcoder95
metcoder95 reviewed Sep 17, 2026
View reviewed changes
Comment thread lib/web/cookies/util.js Outdated
? cookie.expires.getTime()
: cookie.expires

if (typeof expiresMs === 'number' && Number.isFinite(expiresMs) && Math.abs(expiresMs) <= 8640000000000000) {

@metcoder95 metcoder95 Sep 17, 2026

Copy link
Copy Markdown
Member

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.

Let's just add a comment of why that harcoded number

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@codecov-commenter

codecov-commenter commented Sep 17, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.53%. Comparing base (6933139) to head (2bbba02).
⚠️ Report is 26 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #5832      +/-   ##
==========================================
- Coverage   93.54%   93.53%   -0.02%     
==========================================
  Files         110      110              
  Lines       39596    39790     +194     
==========================================
+ Hits        37040    37217     +177     
- Misses       2556     2573      +17     

☔ 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.
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@xia-chao
fix(cookies): ignore a numeric expires outside the Date range
2bbba02
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]>
@xia-chao
xia-chao force-pushed the fix/cookies-numeric-expires-out-of-range branch from cd66bb8 to 2bbba02 Compare September 17, 2026 09:24
metcoder95
metcoder95 approved these changes Sep 24, 2026
View reviewed changes
@mcollina
mcollina merged commit 78aa1a0 into nodejs:main Sep 24, 2026
35 of 38 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot mentioned this pull request Oct 14, 2026
[Release] v8.12.0 #6008
Merged
@mrityunjay-pandey mrityunjay-pandey mentioned this pull request Oct 15, 2026
[v7.x] fix(cookies): backport #5663, #5808 and #5832 #6019
Merged
3 of 7 tasks
mcollina pushed a commit that referenced this pull request Oct 15, 2026
@mrityunjay-pandey @luantaraschi
@metcoder95 @NgoQuocViet2001 @xia-chao
[v7.x] fix(cookies): backport #5663, #5808 and #5832 (#6019)
7d60c7b
* 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]>
@github-actions github-actions Bot mentioned this pull request Oct 15, 2026
[Release] v7.30.1 #6022
Merged
This was referenced Oct 24, 2026
refactor: Bump undici from 6.27.0 to 6.29.1 and 7.28.0 to 7.30.1 parse-community/node-apn#226
Merged
refactor: Bump undici from 6.27.0 to 6.29.1 and 7.28.0 to 7.30.1 parse-community/Parse-SDK-dotNET#436
Merged
refactor: Bump undici from 6.27.0 to 6.29.1 and 7.28.0 to 7.30.1 parse-community/parse-server-example#947
Merged
refactor: Bump undici from 6.28.1 to 6.29.1, 7.29.1 to 7.30.1 and 7.30.0 to 7.30.1 parse-community/parse-server-sqs-mq-adapter#460
Merged
fix: Bump undici from 6.28.1 to 6.29.1 and 7.29.1 to 7.30.1 parse-community/parse-server-push-adapter#636
Merged
refactor: Bump undici from 6.28.1 to 6.29.1, 7.29.1 to 7.30.1 and 7.30.0 to 7.30.1 parse-community/parse-dashboard#3510
Merged
refactor: Bump undici from 7.24.6 to 7.30.1 parse-community/Parse-SDK-Flutter#1179
Merged
@Roshanraj2580 Roshanraj2580 mentioned this pull request Nov 1, 2026
setCookie turns finite out-of-range expires values into a Unix-epoch expiry #6123
Open
@vtxe02023274 vtxe02023274 mentioned this pull request Nov 1, 2026
fix(cookies): ignore finite numeric expires that wrap in unsigned long long #6124
Open
2 of 7 tasks
@waw666waw666 waw666waw666 mentioned this pull request Nov 21, 2026
fix(cookies): do not serialize a finite maxAge outside the long long range #6244
Open
3 of 7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@metcoder95 metcoder95 metcoder95 approved these changes
+1 more reviewer
@gijs-martens gijs-martens gijs-martens approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

setCookie writes a broken Expires header when expires is an out-of-range number

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@xia-chao @codecov-commenter @metcoder95 @gijs-martens @mcollina
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.