Skip to content

Commit

Permalink
fix: remove cookies that expire at epoch time of 0
Browse files Browse the repository at this point in the history
The logic for checking and removing expired cookies fails when the cookie has an expiry date set to the epoch time of `0` (i.e.; `Thu, 01 Jan 1970 00:00:00 GMT`). This is because the branch check that triggers cookie removal was testing `expiryTime` which is `falsy` for a value of `0` and that short-circuits the logic.

This PR updates the logic to narrow the type of `expiryTime` from `number | undefined` to `number` using `expiryTime != undefined` which allows the `0` value to proceed to the subsequent condition and trigger the cookie removal.

Fixes #455
  • Loading branch information
colincasey committed Sep 16, 2024
1 parent 1a71340 commit 99dc62a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions lib/__tests__/cookieJar.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,16 @@ describe('validation errors invoke callbacks', () => {
})
})

it('issue #455 - should expire a cookie with epoch zero', async () => {
const cookieJar = new CookieJar()
await cookieJar.setCookie(
'OptionsTest=FooBar; Expires=Thu, 01 Jan 1970 00:00:00 GMT;',
'http://example.com',
)
const cookies = await cookieJar.getCookies('http://example.com')
expect(cookies.length).toBe(0)
})

function createCookie(
cookieString: string,
options: {
Expand Down
2 changes: 1 addition & 1 deletion lib/cookie/cookieJar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ export class CookieJar {
// deferred from S5.3
// non-RFC: allow retention of expired cookies by choice
const expiryTime = c.expiryTime()
if (expireCheck && expiryTime && expiryTime <= now) {
if (expireCheck && expiryTime != undefined && expiryTime <= now) {
store.removeCookie(c.domain, c.path, c.key, () => {}) // result ignored
return false
}
Expand Down

0 comments on commit 99dc62a

Please sign in to comment.