Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
TypeScript automationchromechromiume2e-testingelectronend-to-end-testingfirefoxjavascriptplaywrighttesttest-automationtestingtesting-toolswebwebkit
fix(client-certificates): auto detect proxy in the browser (#36817)
Max Schmittf512687291e9
diff --git a/packages/playwright-core/src/server/socksClientCertificatesInterceptor.ts b/packages/playwright-core/src/server/socksClientCertificatesInterceptor.tsindex da706fd940..e5dd51bfb0 100644--- a/packages/playwright-core/src/server/socksClientCertificatesInterceptor.ts+++ b/packages/playwright-core/src/server/socksClientCertificatesInterceptor.ts@@ -26,6 +26,7 @@ import { verifyClientCertificates } from './browserContext';import { createProxyAgent } from './utils/network';import { debugLogger } from './utils/debugLogger';import { createSocket, createTLSSocket } from './utils/happyEyeballs';+import { getProxyForUrl } from '../utilsBundle';import type * as types from './types';import type { SocksSocketClosedPayload, SocksSocketDataPayload, SocksSocketRequestedPayload } from './utils/socksProxy';@@ -99,8 +100,9 @@ class SocksProxyConnection {}async connect() {- if (this.socksProxy.proxyAgentFromOptions)- this.target = await this.socksProxy.proxyAgentFromOptions.callback(new EventEmitter() as any, { host: rewriteToLocalhostIfNeeded(this.host), port: this.port, secureEndpoint: false });+ const proxyAgent = this.socksProxy.getProxyAgent(this.host, this.port);+ if (proxyAgent)+ this.target = await proxyAgent.callback(new EventEmitter() as any, { host: rewriteToLocalhostIfNeeded(this.host), port: this.port, secureEndpoint: false });elsethis.target = await createSocket(rewriteToLocalhostIfNeeded(this.host), this.port);@@ -242,7 +244,7 @@ export class ClientCertificatesProxy {ignoreHTTPSErrors: boolean | undefined;secureContextMap: Map<string, tls.SecureContext> = new Map();alpnCache: ALPNCache;- proxyAgentFromOptions: ReturnType<typeof createProxyAgent>;+ private _proxy: types.ProxySettings | undefined;private constructor(contextOptions: Pick<types.BrowserContextOptions, 'clientCertificates' | 'ignoreHTTPSErrors' | 'proxy'>@@ -250,7 +252,7 @@ export class ClientCertificatesProxy {verifyClientCertificates(contextOptions.clientCertificates);this.alpnCache = new ALPNCache();this.ignoreHTTPSErrors = contextOptions.ignoreHTTPSErrors;- this.proxyAgentFromOptions = createProxyAgent(contextOptions.proxy);+ this._proxy = contextOptions.proxy;this._initSecureContexts(contextOptions.clientCertificates);this._socksProxy = new SocksProxy();this._socksProxy.setPattern('*');@@ -274,6 +276,15 @@ export class ClientCertificatesProxy {loadDummyServerCertsIfNeeded();}+ getProxyAgent(host: string, port: number) {+ const proxyFromOptions = createProxyAgent(this._proxy);+ if (proxyFromOptions)+ return proxyFromOptions;+ const proxyFromEnv = getProxyForUrl(`https://${host}:${port}`);+ if (proxyFromEnv)+ return createProxyAgent({ server: proxyFromEnv });+ }+_initSecureContexts(clientCertificates: types.BrowserContextOptions['clientCertificates']) {// Step 1. Group certificates by origin.const origin2certs = new Map<string, types.BrowserContextOptions['clientCertificates']>();diff --git a/tests/library/client-certificates.spec.ts b/tests/library/client-certificates.spec.tsindex 108dde2edc..a3ebbb83a2 100644--- a/tests/library/client-certificates.spec.ts+++ b/tests/library/client-certificates.spec.ts@@ -371,6 +371,49 @@ test.describe('browser', () => {await page.close();});+ test('should pass with matching certificates and when a http proxy is used from env', async ({ browser, startCCServer, asset, browserName, proxyServer, isMac }) => {+ process.env.HTTPS_PROXY = `http://localhost:${proxyServer.PORT}`;+ const serverURL = await startCCServer({ useFakeLocalhost: browserName === 'webkit' && isMac });+ proxyServer.forwardTo(parseInt(new URL(serverURL).port, 10), { allowConnectRequests: true });+ const page = await browser.newPage({+ ignoreHTTPSErrors: true,+ clientCertificates: [{+ origin: new URL(serverURL).origin,+ certPath: asset('client-certificates/client/trusted/cert.pem'),+ keyPath: asset('client-certificates/client/trusted/key.pem'),+ }],+ });+ expect(proxyServer.connectHosts).toEqual([]);+ await page.goto(serverURL);+ const host = browserName === 'webkit' && isMac ? 'localhost' : '127.0.0.1';+ expect([...new Set(proxyServer.connectHosts)]).toEqual([`${host}:${new URL(serverURL).port}`]);+ await expect(page.getByTestId('message')).toHaveText('Hello Alice, your certificate was issued by localhost!');+ await page.close();+ delete process.env.HTTPS_PROXY;+ });++ test('should pass with matching certificates and when a http proxy is used from config but env is there', async ({ browser, startCCServer, asset, browserName, proxyServer, isMac }) => {+ process.env.HTTPS_PROXY = `http://this-should-not-taken-into-account:4242`;+ const serverURL = await startCCServer({ useFakeLocalhost: browserName === 'webkit' && isMac });+ proxyServer.forwardTo(parseInt(new URL(serverURL).port, 10), { allowConnectRequests: true });+ const page = await browser.newPage({+ ignoreHTTPSErrors: true,+ clientCertificates: [{+ origin: new URL(serverURL).origin,+ certPath: asset('client-certificates/client/trusted/cert.pem'),+ keyPath: asset('client-certificates/client/trusted/key.pem'),+ }],+ proxy: { server: `localhost:${proxyServer.PORT}` }+ });+ expect(proxyServer.connectHosts).toEqual([]);+ await page.goto(serverURL);+ const host = browserName === 'webkit' && isMac ? 'localhost' : '127.0.0.1';+ expect([...new Set(proxyServer.connectHosts)]).toEqual([`${host}:${new URL(serverURL).port}`]);+ await expect(page.getByTestId('message')).toHaveText('Hello Alice, your certificate was issued by localhost!');+ await page.close();+ delete process.env.HTTPS_PROXY;+ });+test('should pass with matching certificates and when a socks proxy is used', async ({ browser, startCCServer, asset, browserName, isMac }) => {const serverURL = await startCCServer({ useFakeLocalhost: browserName === 'webkit' && isMac });const serverPort = parseInt(new URL(serverURL).port, 10);