Skip to content

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

fix(cookies): make filtering by url work with subdomains (#4989)

Yury Semikhatsky29c34325c9a9
diff --git a/src/server/network.ts b/src/server/network.ts
index 9f97faa0db..e452fae4d3 100644
--- a/src/server/network.ts
+++ b/src/server/network.ts
@@ -26,7 +26,10 @@ export function filterCookies(cookies: types.NetworkCookie[], urls: string[]): t
if (!parsedURLs.length)
return true;
for (const parsedURL of parsedURLs) {
- if (parsedURL.hostname !== c.domain)
+ let domain = c.domain;
+ if (!domain.startsWith('.'))
+ domain = '.' + domain;
+ if (!('.' + parsedURL.hostname).endsWith(domain))
continue;
if (!parsedURL.pathname.startsWith(c.path))
continue;
diff --git a/test/browsercontext-cookies.spec.ts b/test/browsercontext-cookies.spec.ts
index 81504c42b2..3b9d7dac70 100644
--- a/test/browsercontext-cookies.spec.ts
+++ b/test/browsercontext-cookies.spec.ts
@@ -169,3 +169,33 @@ it('should get cookies from multiple urls', async ({context}) => {
sameSite: 'None',
}]);
});
+
+it('should work with subdomain cookie', async ({context, page, server}) => {
+ await context.addCookies([{
+ domain: '.foo.com',
+ path: '/',
+ name: 'doggo',
+ value: 'woofs',
+ secure: true
+ }]);
+ expect(await context.cookies('https://foo.com')).toEqual([{
+ name: 'doggo',
+ value: 'woofs',
+ domain: '.foo.com',
+ path: '/',
+ expires: -1,
+ httpOnly: false,
+ secure: true,
+ sameSite: 'None',
+ }]);
+ expect(await context.cookies('https://sub.foo.com')).toEqual([{
+ name: 'doggo',
+ value: 'woofs',
+ domain: '.foo.com',
+ path: '/',
+ expires: -1,
+ httpOnly: false,
+ secure: true,
+ sameSite: 'None',
+ }]);
+});