Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,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 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
Expand Down Expand Up @@ -325,7 +326,12 @@ export function stringifySetCookie(
}

if (cookie.expires) {
if (!Number.isFinite(cookie.expires.valueOf())) {
const year = cookie.expires.getUTCFullYear();
if (
!Number.isFinite(cookie.expires.valueOf()) ||
year < 1601 ||
year > 9999
) {
throw new TypeError(`option expires is invalid: ${cookie.expires}`);
}

Expand Down
22 changes: 22 additions & 0 deletions src/stringify-set-cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,28 @@ describe("cookie.stringifySetCookie", () => {
).toThrow(/option expires is invalid/);
});

it.each([-1, 0, 49, 50, 69, 99, 1600, 10000])(
"should reject expires with UTC year %s outside cookie bounds",
(year) => {
const expires = new Date(0);
expires.setUTCFullYear(year);
expect(() =>
stringifySetCookie({ name: "foo", value: "bar", expires }),
).toThrow(/option expires is invalid/);
},
);

it.each([1601, 1970, 9999])(
"should preserve expires with supported UTC year %s",
(year) => {
const expires = new Date(0);
expires.setUTCFullYear(year);
expect(stringifySetCookie({ name: "foo", value: "bar", expires })).toBe(
`foo=bar; Expires=${expires.toUTCString()}`,
);
},
);

it("should set expires to given date", () => {
expect(
stringifySetCookie({
Expand Down
Loading