Skip to content

fix(server): clearCookies({name}) should not transiently delete other cookies#40955

Open
adityasingh2400 wants to merge 4 commits into
microsoft:mainfrom
adityasingh2400:fix-clear-cookies-transient-delete-40953
Open

fix(server): clearCookies({name}) should not transiently delete other cookies#40955
adityasingh2400 wants to merge 4 commits into
microsoft:mainfrom
adityasingh2400:fix-clear-cookies-transient-delete-40953

Conversation

@adityasingh2400
Copy link
Copy Markdown
Contributor

Fixes #40953.

BrowserContext.clearCookies(options) currently wipes every cookie via doClearCookies() and then re-adds the ones that did not match the filter. Pages that subscribe to the cookieStore.change API observe a transient deletion of the kept cookies during the gap between the wipe and the readd, which is enough to trip route-guards, useSyncExternalStore-style auth state machines, and similar listeners. With cookieStore now Baseline 2025, this race window is observable from user code.

When a filter (name, domain, or path) is set, this PR expires only the matching cookies in place by calling addCookies with expires: 0; the no-filter path still delegates to doClearCookies() as before, so no per-browser code is touched.

Credit to @jasikpark for the full root-cause analysis and the proposed fix shape in the issue.

Added a Chromium-only test in tests/library/browsercontext-clearcookies.spec.ts that adds two cookies, subscribes to cookieStore.change via the page, then calls clearCookies({ name: 'delete_me' }) and asserts the kept cookie never appears in a deletion event.

… cookies

BrowserContext.clearCookies(options) currently wipes every cookie via
doClearCookies() and then re-adds the ones that did not match the
filter. Pages that subscribe to cookieStore.change observe a transient
deletion of the kept cookies during the gap between the wipe and the
readd, which is enough to trip route-guards, useSyncExternalStore-style
auth state machines, and similar.

When a filter (name, domain, or path) is set, expire only the matching
cookies in place by calling addCookies with expires=0; the no-filter
path still delegates to doClearCookies() as before. No per-browser code
is changed.

Reported and diagnosed by @jasikpark in microsoft#40953.
@adityasingh2400
Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

@github-actions
Copy link
Copy Markdown
Contributor

Test results for "MCP"

7181 passed, 1113 skipped


Merge workflow run.

@github-actions
Copy link
Copy Markdown
Contributor

Test results for "tests 1"

3 flaky ⚠️ [chromium-library] › library/video.spec.ts:337 › screencast › should work for popups `@ubuntu-22.04-chromium-tip-of-tree`
⚠️ [webkit-library] › library/browsertype-connect.spec.ts:792 › launchServer › should upload a folder `@webkit-ubuntu-22.04-node20`
⚠️ [playwright-test] › ui-mode-trace.spec.ts:812 › should update state on subsequent run `@windows-latest-node20`

42071 passed, 854 skipped


Merge workflow run.

@adityasingh2400
Copy link
Copy Markdown
Contributor Author

@microsoft-github-policy-service agree

Skn0tt added 3 commits May 26, 2026 15:09
…ned cookies

Adds coverage for two cases the new filtered-clearCookies path in microsoft#40955
does not handle: __Secure- prefixed cookies (rejected by Chromium because
the expire payload omits secure:true) and partitioned (CHIPS) cookies
(silently survive because partitionKey is dropped from the expire payload).
Same root cause as the __Secure- regression: without secure/sameSite in
the expire payload, Chromium rejects the cookie with "Invalid cookie
fields".
…arCookies

Spread the original cookie into the expire payload instead of picking
name/domain/path. Without secure/sameSite/partitionKey/_crHasCrossSiteAncestor,
Chromium rejects __Secure- and __Host- prefixed cookies with "Invalid cookie
fields" and silently leaves partitioned (CHIPS) cookies in place.
@Skn0tt
Copy link
Copy Markdown
Member

Skn0tt commented May 26, 2026

Code looked good, I found some regressions that I covered with tests + fixed. @yury-s could you take a look since you last touched Cookie around CHIPS?

@Skn0tt Skn0tt requested a review from yury-s May 26, 2026 13:22
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]: context.clearCookies({name}) transiently deletes non-matching cookies (observable via cookieStore.change)

2 participants