Skip to content

Clamp out-of-range Expires dates instead of throwing - #294

Closed
meabed wants to merge 1 commit into
jshttp:masterfrom
meabed:fix/clamp-out-of-range-expires
Closed

meabed wants to merge 1 commit into
jshttp:masterfrom
meabed:fix/clamp-out-of-range-expires

Conversation

@meabed

@meabed meabed commented Nov 12, 2026

Copy link
Copy Markdown

Clamp out-of-range Expires dates instead of throwing

Follow-up to #290 (released in 2.0.2), discussed in #293.

Summary

  • stringifySetCookie now clamps valid-but-out-of-range expires dates to the closest representable bound (1601-01-01 / 9999-12-31) instead of throwing a TypeError.
  • Invalid Date objects (new Date(NaN)) still throw — that remains a programming error.
  • No new options or API surface.

Why

Throwing turned a previously-silent edge case into a hard crash when it shipped in the 2.0.2 patch. Real-world case from #293: an app multiplied an already-milliseconds timestamp into a year-~58000 date and passed it through stringifySetCookie; ≤ 2.0.1 serialized it harmlessly (browsers clamp), 2.0.2 threw from inside a login success handler and silently broke the post-login redirect for every user.

This also matches the spec's own approach: RFC 6265bis's cookie-date algorithm clamps parsed years to the 1601–9999 range rather than rejecting them, and user agents likewise clamp dates they cannot represent. Serializing with the same semantics keeps stringifySetCookie symmetric with what actually happens to the cookie.

Behavior

stringifySetCookie({
  name: 'jwt',
  value: 'token',
  path: '/',
  expires: new Date(Date.now() + 1.794e15), // year ~58000
})
// 2.0.1: "jwt=token; Path=/; Expires=<far-future date>"
// 2.0.2: TypeError: cookie expires is invalid
// this PR: "jwt=token; Path=/; Expires=Fri, 31 Dec 9999 23:59:59 GMT"

Since this reverts part of 2.0.2's observable behavior, it's intended for the next minor release — with the current throw kept only for genuinely invalid dates.

Test plan

  • Updated stringify-set-cookie.spec.ts: below-bounds years (−1, 0, 49, 50, 69, 99, 1600) clamp to Mon, 01 Jan 1601 00:00:00 GMT; year 10000 clamps to Fri, 31 Dec 9999 23:59:59 GMT; in-bounds years (1601, 1970, 9999) unchanged; new Date(NaN) still throws.
  • Full suite: 63,752 tests passing locally.

RFC 6265bis applies year clamping (1601–9999) in its own cookie-date
algorithm, and user agents clamp dates they cannot represent. Serialize
out-of-range but valid dates to the closest bound instead of throwing,
so a bad expires value degrades to a long-lived or immediately-expired
cookie rather than crashing the caller. Invalid Date objects still
throw.

Fixes jshttp#293 (released as part of the 2.0.2 reject behavior in jshttp#290;
proposed for the next minor).
@blakeembrey

Copy link
Copy Markdown
Member

Clamping isn't a desirable fix. Either it's going to remain re RFC compliance or revert back to non-RFC compliance. It seems like it unearthed a bug in your own system so it may be desirable to keep it rather than silently altering inputs.

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.

2 participants