Skip to content
Merged
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
7 changes: 7 additions & 0 deletions packages/playwright-core/src/server/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`);
Expand Down
34 changes: 27 additions & 7 deletions tests/browsercontext-cookies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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/);
});