Skip to content

fix(network): match empty string filters in clearCookies - #43114

Closed
Lazizbek Ergashev (lazerg) wants to merge 1 commit into
microsoft:mainfrom
lazerg:fix-43112
Closed

Lazizbek Ergashev (lazerg) wants to merge 1 commit into
microsoft:mainfrom
lazerg:fix-43112

Conversation

@lazerg

@lazerg Lazizbek Ergashev (lazerg) commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • cookieMatchesClearFilter() skipped a filter with if (!value), so clearCookies({ name: '' }) matched every cookie and removed all of them instead of only the nameless ones.
  • Skip the filter only when it is undefined, which is what hasClearCookiesFilter() already checks. This applies to both BrowserContext.clearCookies() and APIRequestContext.clearCookies(). The test uses APIRequestContext, since browsers do not store or expire nameless cookies consistently.

Fixes #43112

@alok-108 Alok Pandey (alok-108) left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for picking up #43112 so quickly Lazizbek Ergashev (@lazerg)! The fix is concise and directly resolves the bug.

export function cookieMatchesClearFilter(cookie: channels.NetworkCookie, options: ClearCookiesOptions): boolean {
const matches = (prop: 'name' | 'domain' | 'path', value: string | RegExp | undefined) => {
if (!value)
if (value === undefined)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This cleanly aligns cookieMatchesClearFilter with hasClearCookiesFilter on line 124, which already checked options[prop] !== undefined.

{ name: 'session', value: '2', domain: 'one.com', path: '/' },
]);
await request.clearCookies({ name: '' });
expect((await request.cookies()).map(c => c.value)).toEqual(['2']);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional nit: It might be helpful to assert both name and value to explicitly verify that the named session cookie was retained:

expect((await request.cookies()).map(c => ({ name: c.name, value: c.value }))).toEqual([
  { name: 'session', value: '2' }
]);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept the value-only check to match the test above. Each cookie has a unique value, so ['2'] can only be session.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: clearCookies({ name: "" }) unintentionally clears all cookies instead of only nameless cookies

3 participants