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): do not match sibling paths in cookie-list path filter (#43368)

Alok Pandey6c9fa08b32c5
diff --git a/packages/playwright-core/src/tools/backend/cookies.ts b/packages/playwright-core/src/tools/backend/cookies.ts
index 2b85fda0bb..e84082f2a4 100644
--- a/packages/playwright-core/src/tools/backend/cookies.ts
+++ b/packages/playwright-core/src/tools/backend/cookies.ts
@@ -44,7 +44,7 @@ const cookieList = defineTool({
});
}
if (params.path)
- cookies = cookies.filter(c => c.path.startsWith(params.path!));
+ cookies = cookies.filter(c => pathMatches(c.path, params.path!));
if (cookies.length === 0)
response.addTextResult('No cookies found');
@@ -175,6 +175,13 @@ function normalizeDomain(domain: string): string {
return domain.replace(/^\./, '').toLowerCase();
}
+function pathMatches(cookiePath: string, filterPath: string): boolean {
+ if (cookiePath === filterPath)
+ return true;
+ const prefix = filterPath.endsWith('/') ? filterPath : filterPath + '/';
+ return cookiePath.startsWith(prefix);
+}
+
export default [
cookieList,
cookieGet,
diff --git a/tests/mcp/cookies.spec.ts b/tests/mcp/cookies.spec.ts
index 101c897da9..dc90425b2c 100644
--- a/tests/mcp/cookies.spec.ts
+++ b/tests/mcp/cookies.spec.ts
@@ -177,6 +177,38 @@ test('browser_cookie_list filters by domain', async ({ startClient, server }) =>
});
});
+test('browser_cookie_list filters by path', async ({ startClient, server }) => {
+ const { client } = await startClient({
+ config: { capabilities: ['storage'] },
+ });
+
+ await client.callTool({
+ name: 'browser_navigate',
+ arguments: { url: server.EMPTY_PAGE },
+ });
+
+ // The path matches itself and its sub-paths, not other paths that start with it.
+ for (const [name, path] of [['exact', '/api'], ['sub', '/api/users'], ['other', '/apikeys']]) {
+ await client.callTool({
+ name: 'browser_cookie_set',
+ arguments: { name, value: 'value', path },
+ });
+ }
+ const apiResult = await client.callTool({
+ name: 'browser_cookie_list',
+ arguments: { path: '/api' },
+ });
+ expect(apiResult).toHaveResponse({
+ result: expect.stringContaining('exact=value'),
+ });
+ expect(apiResult).toHaveResponse({
+ result: expect.stringContaining('sub=value'),
+ });
+ expect(apiResult).toHaveResponse({
+ result: expect.not.stringContaining('other=value'),
+ });
+});
+
test('browser_cookie_get returns not found for missing cookie', async ({ startClient, server }) => {
const { client } = await startClient({
config: { capabilities: ['storage'] },