From c2b2ff2cfaf1152b1bccee463e9872fbe32ccd9d Mon Sep 17 00:00:00 2001 From: Mohamed Meabed Date: Wed, 11 Nov 2026 19:16:02 -0500 Subject: [PATCH] Clamp out-of-range Expires dates instead of throwing MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 #293 (released as part of the 2.0.2 reject behavior in #290; proposed for the next minor). --- src/index.ts | 28 ++++++++++++++++++++++++---- src/stringify-set-cookie.spec.ts | 18 +++++++++++++----- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/src/index.ts b/src/index.ts index 005b07d..ef70b1c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,11 @@ +/** + * Bounds for the `Expires` attribute year, per RFC 6265bis. Dates outside + * these bounds are clamped to the closest bound on serialization, matching + * the year clamping the specification's cookie-date algorithm applies. + */ +const MIN_COOKIE_EXPIRES_DATE = new Date("1601-01-01T00:00:00.000Z"); +const MAX_COOKIE_EXPIRES_DATE = new Date("9999-12-31T23:59:59.999Z"); + /** * RegExp to match cookie-name in RFC 6265 sec 4.1.1 * This refers out to the obsoleted definition of token in RFC 2616 sec 2.2 @@ -208,7 +216,7 @@ export interface SetCookie { maxAge?: number; /** * Specifies the `Date` object to be the value for the [`Expires` `Set-Cookie` attribute](https://tools.ietf.org/html/rfc6265#section-5.2.1). - * When serializing, the UTC year must be between 1601 and 9999 inclusive. + * When serializing, dates with a UTC year outside 1601–9999 are clamped to the nearest bound. * When no expiration is set, clients consider this a "non-persistent cookie" and delete it when the current session is over. * * The [cookie storage model specification](https://tools.ietf.org/html/rfc6265#section-5.3) states that if both `expires` and @@ -326,12 +334,24 @@ export function stringifySetCookie( } if (cookie.expires) { - const year = cookie.expires.getUTCFullYear(); - if (!(year >= 1601 && year <= 9999)) { + const time = cookie.expires.getTime(); + + if (Number.isNaN(time)) { throw new TypeError(`cookie expires is invalid: ${cookie.expires}`); } - str += "; Expires=" + cookie.expires.toUTCString(); + // Clamp out-of-range dates to the closest representable bound instead of + // throwing. This mirrors the year clamping the cookie-date algorithm in + // RFC 6265bis applies and how user agents treat dates they cannot + // represent. See https://github.com/jshttp/cookie/issues/293. + const expires = + time > MAX_COOKIE_EXPIRES_DATE.getTime() + ? MAX_COOKIE_EXPIRES_DATE + : time < MIN_COOKIE_EXPIRES_DATE.getTime() + ? MIN_COOKIE_EXPIRES_DATE + : cookie.expires; + + str += "; Expires=" + expires.toUTCString(); } if (cookie.httpOnly) { diff --git a/src/stringify-set-cookie.spec.ts b/src/stringify-set-cookie.spec.ts index 174e48b..ca81a1a 100644 --- a/src/stringify-set-cookie.spec.ts +++ b/src/stringify-set-cookie.spec.ts @@ -165,17 +165,25 @@ describe("cookie.stringifySetCookie", () => { ).toThrow(/cookie expires is invalid/); }); - it.each([-1, 0, 49, 50, 69, 99, 1600, 10000])( - "should reject expires with UTC year %s outside cookie bounds", + it.each([-1, 0, 49, 50, 69, 99, 1600])( + "should clamp expires with UTC year %s below cookie bounds to 1601-01-01", (year) => { const expires = new Date(0); expires.setUTCFullYear(year); - expect(() => - stringifySetCookie({ name: "foo", value: "bar", expires }), - ).toThrow(/cookie expires is invalid/); + expect(stringifySetCookie({ name: "foo", value: "bar", expires })).toBe( + "foo=bar; Expires=Mon, 01 Jan 1601 00:00:00 GMT", + ); }, ); + it("should clamp expires with UTC year above cookie bounds to 9999-12-31", () => { + const expires = new Date(0); + expires.setUTCFullYear(10000); + expect(stringifySetCookie({ name: "foo", value: "bar", expires })).toBe( + "foo=bar; Expires=Fri, 31 Dec 9999 23:59:59 GMT", + ); + }); + it.each([1601, 1970, 9999])( "should preserve expires with supported UTC year %s", (year) => {