Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

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

Max Schmittb2e3357613f1
diff --git a/packages/playwright-core/src/server/network.ts b/packages/playwright-core/src/server/network.ts
index 800788e19e..af892d53b8 100644
--- a/packages/playwright-core/src/server/network.ts
+++ b/packages/playwright-core/src/server/network.ts
@@ -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}"`);
diff --git a/tests/browsercontext-cookies.spec.ts b/tests/browsercontext-cookies.spec.ts
index ae9d439371..22a173557d 100644
--- a/tests/browsercontext-cookies.spec.ts
+++ b/tests/browsercontext-cookies.spec.ts
@@ -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/);
});