Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions packages/playwright-core/src/tools/backend/cookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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!));

Expand Down Expand Up @@ -160,6 +165,10 @@ const cookieClear = defineTool({
},
});

function normalizeDomain(domain: string): string {
return domain.replace(/^\./, '').toLowerCase();
}

export default [
cookieList,
cookieGet,
Expand Down
45 changes: 45 additions & 0 deletions tests/mcp/cookies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,51 @@ test('browser_cookie_list filters by domain', async ({ startClient, server }) =>
});
});

test('browser_cookie_list domain filter matches the domain and its subdomains', { annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43308' } }, async ({ startClient, server }) => {
const { client } = await startClient({
config: { capabilities: ['storage'] },
});

await client.callTool({
name: 'browser_navigate',
arguments: { url: server.EMPTY_PAGE },
});

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 },
});
}

for (const domain of ['example.com', 'EXAMPLE.COM', '.example.com']) {
const result = await client.callTool({
name: 'browser_cookie_list',
arguments: { domain },
});
expect(result).toHaveResponse({
result: expect.stringContaining('apex=value'),
});
expect(result).toHaveResponse({
result: expect.stringContaining('sub=value'),
});
expect(result).toHaveResponse({
result: expect.not.stringContaining('other=value'),
});
}

const subResult = await client.callTool({
name: 'browser_cookie_list',
arguments: { domain: 'sub.example.com' },
});
expect(subResult).toHaveResponse({
result: expect.stringContaining('sub=value'),
});
expect(subResult).toHaveResponse({
result: expect.not.stringContaining('apex=value'),
});
});

test('browser_cookie_get returns not found for missing cookie', async ({ startClient, server }) => {
const { client } = await startClient({
config: { capabilities: ['storage'] },
Expand Down