Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(client-certificates): reestablish a way to explicitly send no client certificate for an origin (#42547)

Florian Schwalm59672632a225
diff --git a/docs/src/api/params.md b/docs/src/api/params.md
index 2f93cecd67..e5f02b313a 100644
--- a/docs/src/api/params.md
+++ b/docs/src/api/params.md
@@ -651,6 +651,7 @@ Does not enforce fixed viewport, allows resizing window in the headed mode.
- `pfxPath` ?<[path]> Path to the PFX or PKCS12 encoded private key and certificate chain.
- `pfx` ?<[Buffer]> Direct value of the PFX or PKCS12 encoded private key and certificate chain.
- `passphrase` ?<[string]> Passphrase for the private key (PEM or PFX).
+ - `noCertificate` ?<[boolean]> Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
TLS Client Authentication allows the server to request a client certificate and verify it.
@@ -658,7 +659,7 @@ TLS Client Authentication allows the server to request a client certificate and
An array of client certificates to be used. Each certificate object must have either both `certPath` and `keyPath`, a single `pfxPath`, or their corresponding direct value equivalents (`cert` and `key`, or `pfx`). Optionally, `passphrase` property should be provided if the certificate is encrypted. The `origin` property should be provided with an exact match to the request origin that the certificate is valid for.
-Client certificate authentication is only active when at least one client certificate is provided. If you want to reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that does not match any of the domains you plan to visit.
+Client certificate authentication is only active when at least one client certificate is provided. If you want to reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
:::note
When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it work by replacing `localhost` with `local.playwright`.
diff --git a/packages/playwright-client/types/types.d.ts b/packages/playwright-client/types/types.d.ts
index 3a1663b0fd..1aaccdadf3 100644
--- a/packages/playwright-client/types/types.d.ts
+++ b/packages/playwright-client/types/types.d.ts
@@ -11292,8 +11292,10 @@ export interface Browser {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -11339,6 +11341,11 @@ export interface Browser {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
@@ -17592,8 +17599,10 @@ export interface BrowserType<Unused = {}> {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -17639,6 +17648,11 @@ export interface BrowserType<Unused = {}> {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
@@ -19316,8 +19330,10 @@ export interface APIRequest {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -19363,6 +19379,11 @@ export interface APIRequest {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
@@ -25788,8 +25809,10 @@ export interface BrowserContextOptions {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -25835,6 +25858,11 @@ export interface BrowserContextOptions {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
diff --git a/packages/playwright-core/src/client/browserContext.ts b/packages/playwright-core/src/client/browserContext.ts
index a6c0c92c8f..03ae7a5eb1 100644
--- a/packages/playwright-core/src/client/browserContext.ts
+++ b/packages/playwright-core/src/client/browserContext.ts
@@ -627,5 +627,6 @@ export async function toClientCertificatesProtocol(certs?: BrowserContextOptions
key: await bufferizeContent(cert.key, cert.keyPath),
pfx: await bufferizeContent(cert.pfx, cert.pfxPath),
passphrase: cert.passphrase,
+ noCertificate: cert.noCertificate,
})));
}
diff --git a/packages/playwright-core/src/client/channels.d.ts b/packages/playwright-core/src/client/channels.d.ts
index 301b508a57..a645cd61a8 100644
--- a/packages/playwright-core/src/client/channels.d.ts
+++ b/packages/playwright-core/src/client/channels.d.ts
@@ -390,6 +390,7 @@ export type AndroidDeviceLaunchBrowserParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -457,6 +458,7 @@ export type AndroidDeviceLaunchBrowserOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -839,6 +841,7 @@ export type BrowserNewContextParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -909,6 +912,7 @@ export type BrowserNewContextOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -982,6 +986,7 @@ export type BrowserNewContextForReuseParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1052,6 +1057,7 @@ export type BrowserNewContextForReuseOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1164,6 +1170,7 @@ export type BrowserContextInitializer = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1815,6 +1822,7 @@ export type BrowserTypeLaunchPersistentContextParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1898,6 +1906,7 @@ export type BrowserTypeLaunchPersistentContextOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -4633,6 +4642,7 @@ export type PlaywrightNewRequestParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
maxRedirects?: number,
httpCredentials?: HttpCredentials[],
@@ -4660,6 +4670,7 @@ export type PlaywrightNewRequestOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
maxRedirects?: number,
httpCredentials?: HttpCredentials[],
diff --git a/packages/playwright-core/src/client/types.ts b/packages/playwright-core/src/client/types.ts
index 89a610732a..a38339de99 100644
--- a/packages/playwright-core/src/client/types.ts
+++ b/packages/playwright-core/src/client/types.ts
@@ -62,6 +62,7 @@ export type ClientCertificate = {
pfx?: Buffer;
pfxPath?: string;
passphrase?: string;
+ noCertificate?: boolean;
};
export type BrowserContextOptions = Omit<channels.BrowserNewContextOptions, 'viewport' | 'noDefaultViewport' | 'extraHTTPHeaders' | 'clientCertificates' | 'storageState' | 'recordHar' | 'colorScheme' | 'reducedMotion' | 'forcedColors' | 'acceptDownloads' | 'contrast' | 'agent' | 'httpCredentials'> & {
diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.ts
index 42a64cdc70..e510a16e64 100644
--- a/packages/playwright-core/src/server/browserContext.ts
+++ b/packages/playwright-core/src/server/browserContext.ts
@@ -800,6 +800,11 @@ export function verifyClientCertificates(clientCertificates?: types.BrowserConte
for (const cert of clientCertificates) {
if (!cert.origin)
throw new Error(`clientCertificates.origin is required`);
+ if (cert.noCertificate) {
+ if (cert.cert || cert.key || cert.passphrase || cert.pfx)
+ throw new Error('noCertificate is set together with cert, key, passphrase or pfx');
+ continue;
+ }
if (!cert.cert && !cert.key && !cert.passphrase && !cert.pfx)
throw new Error('None of cert, key, passphrase or pfx is specified');
if (cert.cert && !cert.key)
diff --git a/packages/playwright-core/src/server/channels.d.ts b/packages/playwright-core/src/server/channels.d.ts
index 5b58e66f84..f2fc853c42 100644
--- a/packages/playwright-core/src/server/channels.d.ts
+++ b/packages/playwright-core/src/server/channels.d.ts
@@ -391,6 +391,7 @@ export type AndroidDeviceLaunchBrowserParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -458,6 +459,7 @@ export type AndroidDeviceLaunchBrowserOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -840,6 +842,7 @@ export type BrowserNewContextParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -910,6 +913,7 @@ export type BrowserNewContextOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -983,6 +987,7 @@ export type BrowserNewContextForReuseParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1053,6 +1058,7 @@ export type BrowserNewContextForReuseOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1165,6 +1171,7 @@ export type BrowserContextInitializer = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1816,6 +1823,7 @@ export type BrowserTypeLaunchPersistentContextParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -1899,6 +1907,7 @@ export type BrowserTypeLaunchPersistentContextOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
javaScriptEnabled?: boolean,
bypassCSP?: boolean,
@@ -4634,6 +4643,7 @@ export type PlaywrightNewRequestParams = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
maxRedirects?: number,
httpCredentials?: HttpCredentials[],
@@ -4661,6 +4671,7 @@ export type PlaywrightNewRequestOptions = {
key?: Binary,
passphrase?: string,
pfx?: Binary,
+ noCertificate?: boolean,
}[],
maxRedirects?: number,
httpCredentials?: HttpCredentials[],
diff --git a/packages/playwright-core/src/server/socksClientCertificatesInterceptor.ts b/packages/playwright-core/src/server/socksClientCertificatesInterceptor.ts
index 448ebc238f..01aa7a3a14 100644
--- a/packages/playwright-core/src/server/socksClientCertificatesInterceptor.ts
+++ b/packages/playwright-core/src/server/socksClientCertificatesInterceptor.ts
@@ -161,9 +161,8 @@ class SocksProxyConnection {
this._firstPackageReceived = true;
// 0x16 is the TLS "handshake" content type. Only intercept it when the origin has a client
// certificate; otherwise pass the connection through so the browser talks TLS to the server directly.
- const secureContext = data[0] === 0x16 ? this.socksProxy.secureContextMap.get(normalizeOrigin(`https://${this.host}:${this.port}`)) : undefined;
- if (secureContext)
- this._establishTlsTunnel(this._browserEncrypted, data, secureContext);
+ if (data[0] === 0x16 && this.socksProxy.secureContextMap.has(normalizeOrigin(`https://${this.host}:${this.port}`)))
+ this._establishTlsTunnel(this._browserEncrypted, data, this.socksProxy.secureContextMap.get(normalizeOrigin(`https://${this.host}:${this.port}`)));
else
this._establishPlaintextTunnel(this._browserEncrypted);
}
@@ -177,7 +176,7 @@ class SocksProxyConnection {
this._serverEncrypted.pipe(browserEncrypted);
}
- private _establishTlsTunnel(browserEncrypted: stream.Duplex, clientHello: Buffer, secureContext: tls.SecureContext) {
+ private _establishTlsTunnel(browserEncrypted: stream.Duplex, clientHello: Buffer, secureContext?: tls.SecureContext) {
const browserALPNProtocols = parseALPNFromClientHello(clientHello) || ['http/1.1'];
debugLogger.log('client-certificates', `Browser->Proxy ${this.host}:${this.port} offers ALPN ${browserALPNProtocols}`);
@@ -280,7 +279,7 @@ export class ClientCertificatesProxy {
_socksProxy: SocksProxy;
private _connections: Map<string, SocksProxyConnection> = new Map();
ignoreHTTPSErrors: boolean | undefined;
- secureContextMap: Map<string, tls.SecureContext> = new Map();
+ secureContextMap: Map<string, tls.SecureContext | undefined> = new Map();
private _proxy: types.ProxySettings | undefined;
private constructor(
@@ -323,7 +322,7 @@ export class ClientCertificatesProxy {
_initSecureContexts(clientCertificates: types.BrowserContextOptions['clientCertificates']) {
// Step 1. Group certificates by origin.
- const origin2certs = new Map<string, types.BrowserContextOptions['clientCertificates']>();
+ const origin2certs = new Map<string, NonNullable<types.BrowserContextOptions['clientCertificates']>>();
for (const cert of clientCertificates || []) {
const origin = normalizeOrigin(cert.origin);
const certs = origin2certs.get(origin) || [];
@@ -333,6 +332,13 @@ export class ClientCertificatesProxy {
// Step 2. Create secure contexts for each origin.
for (const [origin, certs] of origin2certs) {
+ const noCertificateCount = certs.filter(cert => cert.noCertificate).length;
+ if (noCertificateCount > 0 && noCertificateCount < certs.length)
+ throw new Error(`clientCertificates for origin "${origin}" mix noCertificate with a real certificate`);
+ if (noCertificateCount > 0) {
+ this.secureContextMap.set(origin, undefined);
+ continue;
+ }
try {
this.secureContextMap.set(origin, tls.createSecureContext(convertClientCertificatesToTLSOptions(certs)));
} catch (error) {
diff --git a/packages/playwright-core/types/types.d.ts b/packages/playwright-core/types/types.d.ts
index 3a1663b0fd..1aaccdadf3 100644
--- a/packages/playwright-core/types/types.d.ts
+++ b/packages/playwright-core/types/types.d.ts
@@ -11292,8 +11292,10 @@ export interface Browser {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -11339,6 +11341,11 @@ export interface Browser {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
@@ -17592,8 +17599,10 @@ export interface BrowserType<Unused = {}> {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -17639,6 +17648,11 @@ export interface BrowserType<Unused = {}> {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
@@ -19316,8 +19330,10 @@ export interface APIRequest {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -19363,6 +19379,11 @@ export interface APIRequest {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
@@ -25788,8 +25809,10 @@ export interface BrowserContextOptions {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
@@ -25835,6 +25858,11 @@ export interface BrowserContextOptions {
* Passphrase for the private key (PEM or PFX).
*/
passphrase?: string;
+
+ /**
+ * Explicitly send no client certificate for this origin. Must be the only field set besides `origin`.
+ */
+ noCertificate?: boolean;
}>;
/**
diff --git a/packages/playwright/types/test.d.ts b/packages/playwright/types/test.d.ts
index 26f666c7fb..8d0eef73cc 100644
--- a/packages/playwright/types/test.d.ts
+++ b/packages/playwright/types/test.d.ts
@@ -7377,8 +7377,10 @@ export interface PlaywrightTestOptions {
* with an exact match to the request origin that the certificate is valid for.
*
* Client certificate authentication is only active when at least one client certificate is provided. If you want to
- * reject all client certificates sent by the server, you need to provide a client certificate with an `origin` that
- * does not match any of the domains you plan to visit.
+ * reject all client certificates sent by the server for an origin you visit, set `noCertificate` to `true` for that
+ * origin instead of omitting it: omitting the origin entirely leaves the connection unintercepted, so the server's
+ * own certificate request still reaches the browser and may trigger a native certificate-selection prompt on some
+ * platforms. `noCertificate` forces interception for that origin while still presenting no client certificate.
*
* **NOTE** When using WebKit on macOS, accessing `localhost` will not pick up client certificates. You can make it
* work by replacing `localhost` with `local.playwright`.
diff --git a/packages/protocol/spec/mixins.yml b/packages/protocol/spec/mixins.yml
index 80dcdfe8f4..6ec28578f0 100644
--- a/packages/protocol/spec/mixins.yml
+++ b/packages/protocol/spec/mixins.yml
@@ -131,6 +131,7 @@ ContextOptions:
key: binary?
passphrase: string?
pfx: binary?
+ noCertificate: boolean?
javaScriptEnabled: boolean?
bypassCSP: boolean?
userAgent: string?
diff --git a/packages/protocol/spec/playwright.yml b/packages/protocol/spec/playwright.yml
index b4c9651b2e..4cb7be2bab 100644
--- a/packages/protocol/spec/playwright.yml
+++ b/packages/protocol/spec/playwright.yml
@@ -62,6 +62,7 @@ Playwright:
key: binary?
passphrase: string?
pfx: binary?
+ noCertificate: boolean?
maxRedirects: int?
httpCredentials:
type: array?
diff --git a/packages/protocol/src/validator.ts b/packages/protocol/src/validator.ts
index 6e6185a6cd..21e7cbe603 100644
--- a/packages/protocol/src/validator.ts
+++ b/packages/protocol/src/validator.ts
@@ -158,6 +158,7 @@ scheme.AndroidDeviceLaunchBrowserParams = tObject({
key: tOptional(tBinary),
passphrase: tOptional(tString),
pfx: tOptional(tBinary),
+ noCertificate: tOptional(tBoolean),
}))),
javaScriptEnabled: tOptional(tBoolean),
bypassCSP: tOptional(tBoolean),
@@ -440,6 +441,7 @@ scheme.BrowserNewContextParams = tObject({
key: tOptional(tBinary),
passphrase: tOptional(tString),
pfx: tOptional(tBinary),
+ noCertificate: tOptional(tBoolean),
}))),
javaScriptEnabled: tOptional(tBoolean),
bypassCSP: tOptional(tBoolean),
@@ -513,6 +515,7 @@ scheme.BrowserNewContextForReuseParams = tObject({
key: tOptional(tBinary),
passphrase: tOptional(tString),
pfx: tOptional(tBinary),
+ noCertificate: tOptional(tBoolean),
}))),
javaScriptEnabled: tOptional(tBoolean),
bypassCSP: tOptional(tBoolean),
@@ -608,6 +611,7 @@ scheme.BrowserContextInitializer = tObject({
key: tOptional(tBinary),
passphrase: tOptional(tString),
pfx: tOptional(tBinary),
+ noCertificate: tOptional(tBoolean),
}))),
javaScriptEnabled: tOptional(tBoolean),
bypassCSP: tOptional(tBoolean),
@@ -1008,6 +1012,7 @@ scheme.BrowserTypeLaunchPersistentContextParams = tObject({
key: tOptional(tBinary),
passphrase: tOptional(tString),
pfx: tOptional(tBinary),
+ noCertificate: tOptional(tBoolean),
}))),
javaScriptEnabled: tOptional(tBoolean),
bypassCSP: tOptional(tBoolean),
@@ -2681,6 +2686,7 @@ scheme.PlaywrightNewRequestParams = tObject({
key: tOptional(tBinary),
passphrase: tOptional(tString),
pfx: tOptional(tBinary),
+ noCertificate: tOptional(tBoolean),
}))),
maxRedirects: tOptional(tInt),
httpCredentials: tOptional(tArray(tType('HttpCredentials'))),
diff --git a/tests/library/client-certificates.spec.ts b/tests/library/client-certificates.spec.ts
index 0684a504d3..6340d5d542 100644
--- a/tests/library/client-certificates.spec.ts
+++ b/tests/library/client-certificates.spec.ts
@@ -88,6 +88,13 @@ const kValidationSubTests: [BrowserContextOptions, string][] = [
passphrase: kDummyFileName,
}]
}, 'pfx is specified together with cert, key or passphrase'],
+ [{
+ clientCertificates: [{
+ origin: 'test',
+ noCertificate: true,
+ certPath: kDummyFileName,
+ }]
+ }, 'noCertificate is set together with cert, key, passphrase or pfx'],
];
test.describe('fetch', () => {
@@ -105,6 +112,21 @@ test.describe('fetch', () => {
await request.dispose();
});
+ test('should send no client certificate when noCertificate is set', async ({ playwright, startCCServer }) => {
+ const serverURL = await startCCServer();
+ const request = await playwright.request.newContext({
+ ignoreHTTPSErrors: true,
+ clientCertificates: [{
+ origin: new URL(serverURL).origin,
+ noCertificate: true,
+ }],
+ });
+ const response = await request.get(serverURL);
+ expect(response.status()).toBe(401);
+ expect(await response.text()).toContain('Sorry, but you need to provide a client certificate to continue.');
+ await request.dispose();
+ });
+
test('should keep supporting http', async ({ playwright, server, asset }) => {
const request = await playwright.request.newContext({
ignoreHTTPSErrors: true,
@@ -361,6 +383,41 @@ test.describe('browser', () => {
await page.close();
});
+ test('should intercept TLS and send no client certificate when noCertificate is set for the visited origin', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42546' },
+ }, async ({ browser, startCCServer }) => {
+ const serverURL = await startCCServer();
+ const page = await browser.newPage({
+ ignoreHTTPSErrors: true,
+ clientCertificates: [{
+ origin: new URL(serverURL).origin,
+ noCertificate: true,
+ }],
+ });
+ const response = await page.goto(serverURL);
+ // noCertificate forces interception for this origin, so the browser sees the
+ // proxy's own dummy certificate (CN=localhost) instead of the real
+ // server's certificate.
+ const securityDetails = await response.securityDetails();
+ expect(securityDetails.subjectName).toBe('localhost');
+ // The app layer still receives no client certificate.
+ await expect(page.getByTestId('message')).toHaveText('Sorry, but you need to provide a client certificate to continue.');
+ await page.close();
+ });
+
+ test('should throw when noCertificate and a real certificate are both set for the same origin', async ({ browser, asset }) => {
+ await expect(browser.newContext({
+ clientCertificates: [
+ { origin: 'https://not-matching.com', noCertificate: true },
+ {
+ origin: 'https://not-matching.com',
+ certPath: asset('client-certificates/client/trusted/cert.pem'),
+ keyPath: asset('client-certificates/client/trusted/key.pem'),
+ },
+ ],
+ })).rejects.toThrow('clientCertificates for origin "https://not-matching.com" mix noCertificate with a real certificate');
+ });
+
test('should fail with no client certificates', async ({ browser, startCCServer, asset, browserName, isMac }) => {
const serverURL = await startCCServer({ useFakeLocalhost: browserName === 'webkit' && isMac });
const page = await browser.newPage({
@@ -913,5 +970,20 @@ test.describe('browser', () => {
await page.goto(serverURL);
await expect(page.getByTestId('message')).toHaveText('Hello Alice, your certificate was issued by localhost!');
});
+
+ test('should intercept TLS and send no client certificate when noCertificate is set for the visited origin', async ({ launchPersistent, startCCServer }) => {
+ const serverURL = await startCCServer();
+ const { page } = await launchPersistent({
+ ignoreHTTPSErrors: true,
+ clientCertificates: [{
+ origin: new URL(serverURL).origin,
+ noCertificate: true,
+ }],
+ });
+ const response = await page.goto(serverURL);
+ const securityDetails = await response.securityDetails();
+ expect(securityDetails.subjectName).toBe('localhost');
+ await expect(page.getByTestId('message')).toHaveText('Sorry, but you need to provide a client certificate to continue.');
+ });
});
});