Skip to content

feat(cookie): export/import chips cookies #36168

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions docs/src/api/class-browsercontext.md
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,7 @@ await context.AddCookiesAsync(new[] { cookie1, cookie2 });
- `httpOnly` ?<[boolean]> Optional.
- `secure` ?<[boolean]> Optional.
- `sameSite` ?<[SameSiteAttribute]<"Strict"|"Lax"|"None">> Optional.
- `partitionKey` ?<[string]> For partitioned third-party cookies (aka [CHIPS](https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies)), the partition key. Optional.

## async method: BrowserContext.addInitScript
* since: v1.8
Expand Down Expand Up @@ -602,6 +603,7 @@ The default browser context cannot be closed.
- `httpOnly` <[boolean]>
- `secure` <[boolean]>
- `sameSite` <[SameSiteAttribute]<"Strict"|"Lax"|"None">>
- `partitionKey` ?<[string]>

If no URLs are specified, this method returns all cookies. If URLs are specified, only cookies that affect those URLs
are returned.
Expand Down Expand Up @@ -1504,6 +1506,7 @@ Whether to emulate network being offline for the browser context.
- `httpOnly` <[boolean]>
- `secure` <[boolean]>
- `sameSite` <[SameSiteAttribute]<"Strict"|"Lax"|"None">>
- `partitionKey` ?<[string]>
- `origins` <[Array]<[Object]>>
- `origin` <[string]>
- `localStorage` <[Array]<[Object]>>
Expand Down
11 changes: 11 additions & 0 deletions packages/playwright-client/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8830,6 +8830,13 @@ export interface BrowserContext {
* Optional.
*/
sameSite?: "Strict"|"Lax"|"None";

/**
* For partitioned third-party cookies (aka
* [CHIPS](https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies)), the
* partition key. Optional.
*/
partitionKey?: string;
}>): Promise<void>;

/**
Expand Down Expand Up @@ -9304,6 +9311,8 @@ export interface BrowserContext {
secure: boolean;

sameSite: "Strict"|"Lax"|"None";

partitionKey?: string;
}>;

origins: Array<{
Expand Down Expand Up @@ -22501,6 +22510,8 @@ export interface Cookie {
secure: boolean;

sameSite: "Strict"|"Lax"|"None";

partitionKey?: string;
}

interface PageWaitForSelectorOptions {
Expand Down
4 changes: 4 additions & 0 deletions packages/playwright-core/src/protocol/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ scheme.SetNetworkCookie = tObject({
httpOnly: tOptional(tBoolean),
secure: tOptional(tBoolean),
sameSite: tOptional(tEnum(['Strict', 'Lax', 'None'])),
partitionKey: tOptional(tString),
_crHasCrossSiteAncestor: tOptional(tBoolean),
});
scheme.NetworkCookie = tObject({
name: tString,
Expand All @@ -146,6 +148,8 @@ scheme.NetworkCookie = tObject({
httpOnly: tBoolean,
secure: tBoolean,
sameSite: tEnum(['Strict', 'Lax', 'None']),
partitionKey: tOptional(tString),
_crHasCrossSiteAncestor: tOptional(tBoolean),
});
scheme.NameValue = tObject({
name: tString,
Expand Down
55 changes: 46 additions & 9 deletions packages/playwright-core/src/server/chromium/crBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -381,19 +381,56 @@ export class CRBrowserContext extends BrowserContext {
async doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]> {
const { cookies } = await this._browser._session.send('Storage.getCookies', { browserContextId: this._browserContextId });
return network.filterCookies(cookies.map(c => {
const copy: any = { sameSite: 'Lax', ...c };
delete copy.size;
delete copy.priority;
delete copy.session;
delete copy.sameParty;
delete copy.sourceScheme;
delete copy.sourcePort;
return copy as channels.NetworkCookie;
const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;
const copy: channels.NetworkCookie = {
name,
value,
domain,
path,
expires,
httpOnly,
secure,
sameSite: sameSite ?? 'Lax',
};
// If hasCrossSiteAncestor is false, the cookie is a partitioned first party cookie,
// this is Chromium specific, see https://chromestatus.com/feature/5144832583663616
// and https://github.com/explainers-by-googlers/CHIPS-spec.
if (c.partitionKey) {
copy._crHasCrossSiteAncestor = c.partitionKey.hasCrossSiteAncestor;
copy.partitionKey = c.partitionKey.topLevelSite;
}
return copy;
}), urls);
}

async addCookies(cookies: channels.SetNetworkCookie[]) {
await this._browser._session.send('Storage.setCookies', { cookies: network.rewriteCookies(cookies), browserContextId: this._browserContextId });
function toChromiumCookie(cookie: channels.SetNetworkCookie) {
const { name, value, url, domain, path, expires, httpOnly, secure, sameSite, partitionKey, _crHasCrossSiteAncestor } = cookie;
const copy: Protocol.Network.CookieParam = {
name,
value,
url,
domain,
path,
expires,
httpOnly,
secure,
sameSite
};
if (partitionKey) {
copy.partitionKey = {
topLevelSite: partitionKey,
// _crHasCrossSiteAncestor is non-standard, set it true by default if the cookie is partitioned.
hasCrossSiteAncestor: _crHasCrossSiteAncestor ?? true,
};
}
return copy;
}

await this._browser._session.send('Storage.setCookies', {
cookies: network.rewriteCookies(cookies).map(toChromiumCookie),
browserContextId: this._browserContextId
});
}

async doClearCookies() {
Expand Down
33 changes: 25 additions & 8 deletions packages/playwright-core/src/server/firefox/ffBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,18 +297,35 @@ export class FFBrowserContext extends BrowserContext {
async doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]> {
const { cookies } = await this._browser.session.send('Browser.getCookies', { browserContextId: this._browserContextId });
return network.filterCookies(cookies.map(c => {
const copy: any = { ... c };
delete copy.size;
delete copy.session;
return copy as channels.NetworkCookie;
const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;
return {
name,
value,
domain,
path,
expires,
httpOnly,
secure,
sameSite,
};
}), urls);
}

async addCookies(cookies: channels.SetNetworkCookie[]) {
const cc = network.rewriteCookies(cookies).map(c => ({
...c,
expires: c.expires === -1 ? undefined : c.expires,
}));
const cc = network.rewriteCookies(cookies).map(c => {
const { name, value, url, domain, path, expires, httpOnly, secure, sameSite } = c;
return {
name,
value,
url,
domain,
path,
expires: expires === -1 ? undefined : expires,
httpOnly,
secure,
sameSite
};
});
await this._browser.session.send('Browser.setCookies', { browserContextId: this._browserContextId, cookies: cc });
}

Expand Down
36 changes: 27 additions & 9 deletions packages/playwright-core/src/server/webkit/wkBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,19 +255,37 @@ export class WKBrowserContext extends BrowserContext {
async doGetCookies(urls: string[]): Promise<channels.NetworkCookie[]> {
const { cookies } = await this._browser._browserSession.send('Playwright.getAllCookies', { browserContextId: this._browserContextId });
return network.filterCookies(cookies.map((c: channels.NetworkCookie) => {
const copy: any = { ... c };
copy.expires = c.expires === -1 ? -1 : c.expires / 1000;
delete copy.session;
return copy as channels.NetworkCookie;
const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;
const copy: channels.NetworkCookie = {
name,
value,
domain,
path,
expires: expires === -1 ? -1 : expires / 1000,
httpOnly,
secure,
sameSite,
};
return copy;
}), urls);
}

async addCookies(cookies: channels.SetNetworkCookie[]) {
const cc = network.rewriteCookies(cookies).map(c => ({
...c,
session: c.expires === -1 || c.expires === undefined,
expires: c.expires && c.expires !== -1 ? c.expires * 1000 : c.expires,
})) as Protocol.Playwright.SetCookieParam[];
const cc = network.rewriteCookies(cookies).map(c => {
const { name, value, domain, path, expires, httpOnly, secure, sameSite } = c;
const copy: Protocol.Playwright.SetCookieParam = {
name,
value,
domain: domain!,
path: path!,
expires: expires && expires !== -1 ? expires * 1000 : expires,
httpOnly,
secure,
sameSite,
session: expires === -1 || expires === undefined,
};
return copy;
});
await this._browser._browserSession.send('Playwright.setCookies', { cookies: cc, browserContextId: this._browserContextId });
}

Expand Down
11 changes: 11 additions & 0 deletions packages/playwright-core/types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8830,6 +8830,13 @@ export interface BrowserContext {
* Optional.
*/
sameSite?: "Strict"|"Lax"|"None";

/**
* For partitioned third-party cookies (aka
* [CHIPS](https://developer.mozilla.org/en-US/docs/Web/Privacy/Guides/Privacy_sandbox/Partitioned_cookies)), the
* partition key. Optional.
*/
partitionKey?: string;
}>): Promise<void>;

/**
Expand Down Expand Up @@ -9304,6 +9311,8 @@ export interface BrowserContext {
secure: boolean;

sameSite: "Strict"|"Lax"|"None";

partitionKey?: string;
}>;

origins: Array<{
Expand Down Expand Up @@ -22501,6 +22510,8 @@ export interface Cookie {
secure: boolean;

sameSite: "Strict"|"Lax"|"None";

partitionKey?: string;
}

interface PageWaitForSelectorOptions {
Expand Down
4 changes: 4 additions & 0 deletions packages/protocol/src/channels.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,8 @@ export type SetNetworkCookie = {
httpOnly?: boolean,
secure?: boolean,
sameSite?: 'Strict' | 'Lax' | 'None',
partitionKey?: string,
_crHasCrossSiteAncestor?: boolean,
};

export type NetworkCookie = {
Expand All @@ -271,6 +273,8 @@ export type NetworkCookie = {
httpOnly: boolean,
secure: boolean,
sameSite: 'Strict' | 'Lax' | 'None',
partitionKey?: string,
_crHasCrossSiteAncestor?: boolean,
};

export type NameValue = {
Expand Down
4 changes: 4 additions & 0 deletions packages/protocol/src/protocol.yml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ SetNetworkCookie:
- Strict
- Lax
- None
partitionKey: string?
_crHasCrossSiteAncestor: boolean?


NetworkCookie:
Expand All @@ -241,6 +243,8 @@ NetworkCookie:
- Strict
- Lax
- None
partitionKey: string?
_crHasCrossSiteAncestor: boolean?


NameValue:
Expand Down
Loading
Loading