diff --git a/src/server/network.ts b/src/server/network.ts index 9f97faa0db6f9..e452fae4d3a59 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 81504c42b2dc6..3b9d7dac70734 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', + }]); +});