chore: throw error if setCookies expires value is not valid (#12470)

This commit is contained in:
Max Schmitt 2022-03-02 23:10:40 +01:00 committed by GitHub
parent a413c0f94c
commit b2e3357613
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 7 deletions

View File

@ -44,12 +44,19 @@ export function filterCookies(cookies: types.NetworkCookie[], urls: string[]): t
});
}
// Rollover to 5-digit year:
// 253402300799 == Fri, 31 Dec 9999 23:59:59 +0000 (UTC)
// 253402300800 == Sat, 1 Jan 1000 00:00:00 +0000 (UTC)
const kMaxCookieExpiresDateInSeconds = 253402300799;
export function rewriteCookies(cookies: types.SetNetworkCookieParam[]): types.SetNetworkCookieParam[] {
return cookies.map(c => {
assert(c.name, 'Cookie should have a name');
assert(c.url || (c.domain && c.path), 'Cookie should have a url or a domain/path pair');
assert(!(c.url && c.domain), 'Cookie should have either url or domain');
assert(!(c.url && c.path), 'Cookie should have either url or path');
assert(!(c.expires && c.expires < 0 && c.expires !== -1), 'Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed');
assert(!(c.expires && c.expires > 0 && c.expires > kMaxCookieExpiresDateInSeconds), 'Cookie should have a valid expires, only -1 or a positive number for the unix timestamp in seconds is allowed');
const copy = { ...c };
if (copy.url) {
assert(copy.url !== 'about:blank', `Blank page can not have cookie "${c.name}"`);

View File

@ -263,10 +263,8 @@ it('should return secure cookies based on HTTP(S) protocol', async ({ context, b
}]);
});
it('should add cookies with an expiration', async ({ context, browserName, platform }) => {
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/12226' });
it.fixme(browserName === 'webkit' && platform === 'linux', 'Protocol error');
const expires = Date.now() + 3600;
it('should add cookies with an expiration', async ({ context }) => {
const expires = Math.floor((Date.now() / 1000)) + 3600;
await context.addCookies([{
url: 'https://foo.com',
name: 'doggo',
@ -276,9 +274,6 @@ it('should add cookies with an expiration', async ({ context, browserName, platf
}]);
const cookies = await context.cookies(['https://foo.com']);
expect(cookies.length).toBe(1);
if (browserName === 'chromium')
// Chromium returns them sometimes as floats: https://crbug.com/1300178
cookies[0].expires = Math.round(cookies[0].expires);
expect(cookies).toEqual([{
name: 'doggo',
value: 'woofs',
@ -289,4 +284,29 @@ it('should add cookies with an expiration', async ({ context, browserName, platf
secure: true,
sameSite: 'None',
}]);
{
// Rollover to 5-digit year
await context.addCookies([{
url: 'https://foo.com',
name: 'doggo',
value: 'woofs',
sameSite: 'None',
expires: 253402300799, // Fri, 31 Dec 9999 23:59:59 +0000 (UTC)
}]);
await expect(context.addCookies([{
url: 'https://foo.com',
name: 'doggo',
value: 'woofs',
sameSite: 'None',
expires: 253402300800, // Sat, 1 Jan 1000 00:00:00 +0000 (UTC)
}])).rejects.toThrow(/Cookie should have a valid expires/);
}
await expect(context.addCookies([{
url: 'https://foo.com',
name: 'doggo',
value: 'woofs',
sameSite: 'None',
expires: -42,
}])).rejects.toThrow(/Cookie should have a valid expires/);
});