Skip to content

fix: ignore an Expires with a time or year out of range - #99

Open
bardrop wants to merge 1 commit into
nfriedly:masterfrom
bardrop-oss:fix-expires-out-of-range
Open

bardrop wants to merge 1 commit into
nfriedly:masterfrom
bardrop-oss:fix-expires-out-of-range

Conversation

@bardrop

@bardrop bardrop commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

What is wrong

parseCookieDate reads all the parts of these dates, sees a value out of range, and then passes the string to new Date() anyway. new Date() does not reject them:

parseSetCookie("a=1; Expires=Wed, 21 Oct 2015 24:00:00 GMT")[0].expires
// actual:   2015-10-22T00:00:00.000Z (next day)
parseSetCookie("a=1; Expires=Wed, 21 Oct 2015 07:28:60 GMT")[0].expires
// actual:   2015-10-21T07:28:00.000Z (seconds dropped)
parseSetCookie("a=1; Expires=Wed, 21 Oct 1600 07:28:00 GMT")[0].expires
// actual:   1600-10-21T07:28:00.000Z

Expected: no expires, same as 31 Feb today. rfc6265 5.1.1 step 5 says to fail when hour > 23, minute > 59, second > 59, day is not 1-31, or year < 1601. tough-cookie's parseDate returns undefined for all three.

Cause

lib/set-cookie.js, parseCookieDate: the range checks sit in the same if as the "found all parts" check, so a failed range check falls through to return new Date(str). Only the "day doesn't exist in that month" case returned Invalid Date.

Fix

If the time, day, month or year is missing, still fall back to new Date(str) as before. Once all four are found, any value out of range returns Invalid Date, so the attribute is ignored with the usual "failed to parse expires time" error.

I fuzzed 200k generated dates against tough-cookie's parseDate. After the fix the only cases where we return a date and tough-cookie does not are ones with a missing part (the new Date() fallback), and the only cases the other way are days like 31 Feb, which tough-cookie rolls over to March.

Test

Added "should ignore an Expires with a time or year out of range" with the three dates above. It fails on main and passes with the fix. npm test (build, lint, mocha, tsd): 83 passing.

The cookie-date algorithm rejected these values, but they then went to
new Date(), which reads 24:00:00 as the next day, drops the seconds from
07:28:60 and keeps the year 1600. rfc6265 5.1.1 says to ignore them.
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.

1 participant