chore: throw error if setCookies expires value is not valid (#12470)
This commit is contained in:
parent
a413c0f94c
commit
b2e3357613
|
|
@ -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[] {
|
export function rewriteCookies(cookies: types.SetNetworkCookieParam[]): types.SetNetworkCookieParam[] {
|
||||||
return cookies.map(c => {
|
return cookies.map(c => {
|
||||||
assert(c.name, 'Cookie should have a name');
|
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 && 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.domain), 'Cookie should have either url or domain');
|
||||||
assert(!(c.url && c.path), 'Cookie should have either url or path');
|
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 };
|
const copy = { ...c };
|
||||||
if (copy.url) {
|
if (copy.url) {
|
||||||
assert(copy.url !== 'about:blank', `Blank page can not have cookie "${c.name}"`);
|
assert(copy.url !== 'about:blank', `Blank page can not have cookie "${c.name}"`);
|
||||||
|
|
|
||||||
|
|
@ -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('should add cookies with an expiration', async ({ context }) => {
|
||||||
it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/12226' });
|
const expires = Math.floor((Date.now() / 1000)) + 3600;
|
||||||
it.fixme(browserName === 'webkit' && platform === 'linux', 'Protocol error');
|
|
||||||
const expires = Date.now() + 3600;
|
|
||||||
await context.addCookies([{
|
await context.addCookies([{
|
||||||
url: 'https://foo.com',
|
url: 'https://foo.com',
|
||||||
name: 'doggo',
|
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']);
|
const cookies = await context.cookies(['https://foo.com']);
|
||||||
expect(cookies.length).toBe(1);
|
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([{
|
expect(cookies).toEqual([{
|
||||||
name: 'doggo',
|
name: 'doggo',
|
||||||
value: 'woofs',
|
value: 'woofs',
|
||||||
|
|
@ -289,4 +284,29 @@ it('should add cookies with an expiration', async ({ context, browserName, platf
|
||||||
secure: true,
|
secure: true,
|
||||||
sameSite: 'None',
|
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/);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue