Skip to content

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

fix(mcp): match cookie list domain filter on domain boundaries (#43316)

Pavel Feldman55a367e01cba
diff --git a/packages/playwright-core/src/tools/backend/cookies.ts b/packages/playwright-core/src/tools/backend/cookies.ts
index 62e44b88f7..2b85fda0bb 100644
--- a/packages/playwright-core/src/tools/backend/cookies.ts
+++ b/packages/playwright-core/src/tools/backend/cookies.ts
@@ -36,8 +36,13 @@ const cookieList = defineTool({
const browserContext = await context.currentBrowserContext();
let cookies = await browserContext.cookies();
- if (params.domain)
- cookies = cookies.filter(c => c.domain.includes(params.domain!));
+ if (params.domain) {
+ const domain = normalizeDomain(params.domain);
+ cookies = cookies.filter(c => {
+ const cookieDomain = normalizeDomain(c.domain);
+ return cookieDomain === domain || cookieDomain.endsWith('.' + domain);
+ });
+ }
if (params.path)
cookies = cookies.filter(c => c.path.startsWith(params.path!));
@@ -166,6 +171,10 @@ const cookieClear = defineTool({
},
});
+function normalizeDomain(domain: string): string {
+ return domain.replace(/^\./, '').toLowerCase();
+}
+
export default [
cookieList,
cookieGet,
diff --git a/tests/mcp/cookies.spec.ts b/tests/mcp/cookies.spec.ts
index efcb810495..101c897da9 100644
--- a/tests/mcp/cookies.spec.ts
+++ b/tests/mcp/cookies.spec.ts
@@ -154,6 +154,27 @@ test('browser_cookie_list filters by domain', async ({ startClient, server }) =>
expect(noResult).toHaveResponse({
result: 'No cookies found',
});
+
+ // The domain matches itself and its subdomains, not other domains that end with it.
+ for (const [name, domain] of [['apex', 'example.com'], ['sub', 'sub.example.com'], ['other', 'my-example.com']]) {
+ await client.callTool({
+ name: 'browser_cookie_set',
+ arguments: { name, value: 'value', domain },
+ });
+ }
+ const exampleResult = await client.callTool({
+ name: 'browser_cookie_list',
+ arguments: { domain: 'example.com' },
+ });
+ expect(exampleResult).toHaveResponse({
+ result: expect.stringContaining('apex=value'),
+ });
+ expect(exampleResult).toHaveResponse({
+ result: expect.stringContaining('sub=value'),
+ });
+ expect(exampleResult).toHaveResponse({
+ result: expect.not.stringContaining('other=value'),
+ });
});
test('browser_cookie_get returns not found for missing cookie', async ({ startClient, server }) => {