Skip to content

fix: ignore an unparseable Set-Cookie Expires attribute#5488

Merged
mcollina merged 1 commit into
nodejs:mainfrom
spokodev:w32/undici-cookie-expires-invalid
Jul 4, 2026
Merged

fix: ignore an unparseable Set-Cookie Expires attribute#5488
mcollina merged 1 commit into
nodejs:mainfrom
spokodev:w32/undici-cookie-expires-invalid

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

Description

parseSetCookie assigns an Invalid Date to the parsed cookie's expires when the Set-Cookie Expires attribute fails to parse, instead of ignoring the attribute.

getSetCookies(new Headers({ 'set-cookie': 'id=a3fWa; Expires=not-a-date' }))
// [{ name: 'id', value: 'a3fWa', expires: Invalid Date }]
// should be: [{ name: 'id', value: 'a3fWa' }]

RFC 6265bis section 5.4.1 step 2 says that if the attribute-value fails to parse as a cookie date, the user agent must ignore the cookie-av. The code comment at that spot already states this, but the assignment ran unconditionally, leaking an Invalid Date into the returned object.

Fix

Only set expires when the parsed date is valid.

     // 2. If the attribute-value failed to parse as a cookie date, ignore
     //    the cookie-av.
-
-    cookieAttributeList.expires = expiryTime
+    if (!Number.isNaN(expiryTime.getTime())) {
+      cookieAttributeList.expires = expiryTime
+    }

Valid Expires values are unchanged.

Test

Added a case to test/cookie/cookies.js for an unparseable Expires. It fails before the change (expires: Invalid Date present) and passes after (attribute omitted). Full cookie suite stays green.

parseSetCookie assigned an Invalid Date to cookieAttributeList.expires when
the Expires attribute failed to parse, instead of ignoring the attribute.
RFC 6265bis 5.4.1 step 2 says to ignore the cookie-av when the date does not
parse, which the adjacent code comment already stated. Only set expires when
the parsed date is valid.
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.45%. Comparing base (782ad38) to head (368572c).
⚠️ Report is 9 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5488   +/-   ##
=======================================
  Coverage   93.45%   93.45%           
=======================================
  Files         110      110           
  Lines       37147    37148    +1     
=======================================
+ Hits        34716    34718    +2     
+ Misses       2431     2430    -1     

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

@mcollina mcollina merged commit e529cab into nodejs:main Jul 4, 2026
38 checks passed
@github-actions github-actions Bot mentioned this pull request Jul 4, 2026
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.

5 participants