Skip to content
Closed
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
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,8 @@ export function parseSetCookie(str: string, options?: ParseOptions): SetCookie {
case "expires":
if (!val) break;
const date = new Date(val);
if (Number.isFinite(date.valueOf())) setCookie.expires = date;
const year = date.getUTCFullYear();
if (year >= 1601 && year <= 9999) setCookie.expires = date;
break;
case "priority":
if (!val) break;
Expand Down
36 changes: 36 additions & 0 deletions src/parse-set-cookie.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,42 @@ describe("cookie.parseSetCookie", () => {
value: "value",
});
});

it.each([1500, 1600, 10000])(
"should ignore expires outside cookie year bounds: %s",
(year) => {
const expires = new Date(0);
expires.setUTCFullYear(year);
expect(
parseSetCookie(`key=value; Expires=${expires.toUTCString()}; Path=/`),
).toEqual({ name: "key", value: "value", path: "/" });
},
);

it.each([1500, 1600, 10000])(
"should keep a valid expires when a later year %s is invalid",
(year) => {
const invalid = new Date(0);
invalid.setUTCFullYear(year);
const expires = new Date("Wed, 21 Oct 2015 07:28:00 GMT");
expect(
parseSetCookie(
`key=value; Expires=${expires.toUTCString()}; Expires=${invalid.toUTCString()}`,
),
).toEqual({ name: "key", value: "value", expires });
},
);

it.each([1601, 9999])(
"should preserve expires at the supported year boundary %s",
(year) => {
const expires = new Date(0);
expires.setUTCFullYear(year);
expect(
parseSetCookie(`key=value; Expires=${expires.toUTCString()}`),
).toEqual({ name: "key", value: "value", expires });
},
);
});

describe('with "max-age"', () => {
Expand Down