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
Revert "fix: do not send proxy credentials to website auth challenges (#43342)" (#43406)
Yury Semikhatsky4844ec6dd05c
diff --git a/packages/playwright-core/src/server/bidi/bidiBrowser.ts b/packages/playwright-core/src/server/bidi/bidiBrowser.tsindex c5fc3983c4..c7f09fd9aa 100644--- a/packages/playwright-core/src/server/bidi/bidiBrowser.ts+++ b/packages/playwright-core/src/server/bidi/bidiBrowser.ts@@ -225,7 +225,9 @@ export class BidiBrowserContext extends BrowserContext {constructor(browser: BidiBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {super(browser, options, browserContextId);- if (browser._bidiOptions.proxyAuthentication === 'header')+ if (browser._bidiOptions.proxyAuthentication === 'credentials')+ this.authenticateProxyViaCredentials();+ elsethis.authenticateProxyViaHeader();}diff --git a/packages/playwright-core/src/server/bidi/bidiNetworkManager.ts b/packages/playwright-core/src/server/bidi/bidiNetworkManager.tsindex f352e03c58..4bd1a9d9b6 100644--- a/packages/playwright-core/src/server/bidi/bidiNetworkManager.ts+++ b/packages/playwright-core/src/server/bidi/bidiNetworkManager.ts@@ -38,7 +38,6 @@ export class BidiNetworkManager {private _userRequestInterceptionEnabled: boolean = false;private _protocolRequestInterceptionEnabled: boolean = false;private _credentials: HttpCredentials[] | undefined;- private _proxyCredentials: HttpCredentials | undefined;private _attemptedAuthentications = new Set<string>();private _intercepId: bidi.Network.Intercept | undefined;@@ -200,21 +199,15 @@ export class BidiNetworkManager {private _onAuthRequired(params: bidi.Network.AuthRequiredParameters) {// Don't look at params.response.authChallenges: Chromium does not report them.- // Proxy credentials only answer the proxy's challenge, never the website's.- // Chromium does not report the status either, so the challenge may come from the proxy or the website.- const siteCredentials = findMatchingHttpCredentials(this._credentials, params.request.url);- const status = params.response.status;- const isProxy = status === 407 || (status !== 401 && !siteCredentials);- const credentials = isProxy ? this._proxyCredentials : siteCredentials;- const attemptKey = authAttemptKey(params.request.request, isProxy);+ const credentials = findMatchingHttpCredentials(this._page.browserContext._options.httpCredentials, params.request.url);if (credentials) {- if (this._attemptedAuthentications.has(attemptKey)) {+ if (this._attemptedAuthentications.has(params.request.request)) {this._session.sendMayFail('network.continueWithAuth', {request: params.request.request,action: 'cancel',});} else {- this._attemptedAuthentications.add(attemptKey);+ this._attemptedAuthentications.add(params.request.request);this._session.sendMayFail('network.continueWithAuth', {request: params.request.request,action: 'provideCredentials',@@ -235,8 +228,7 @@ export class BidiNetworkManager {_deleteRequest(requestId: string) {this._requests.delete(requestId);- this._attemptedAuthentications.delete(authAttemptKey(requestId, true));- this._attemptedAuthentications.delete(authAttemptKey(requestId, false));+ this._attemptedAuthentications.delete(requestId);}async setRequestInterception(value: boolean) {@@ -244,14 +236,13 @@ export class BidiNetworkManager {await this._updateProtocolRequestInterception();}- async setCredentials(credentials: HttpCredentials[] | undefined, proxyCredentials: HttpCredentials | undefined) {+ async setCredentials(credentials: HttpCredentials[] | undefined) {this._credentials = credentials;- this._proxyCredentials = proxyCredentials;await this._updateProtocolRequestInterception();}async _updateProtocolRequestInterception(initial?: boolean) {- const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length || !!this._proxyCredentials;+ const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length;if (enabled === this._protocolRequestInterceptionEnabled)return;this._protocolRequestInterceptionEnabled = enabled;@@ -446,7 +437,3 @@ function resourceTypeFromBidi(requestDestination: string, requestInitiatorType:default: return 'other';}}--function authAttemptKey(requestId: string, isProxy: boolean) {- return `${requestId}:${isProxy ? 'proxy' : 'server'}`;-}diff --git a/packages/playwright-core/src/server/bidi/bidiPage.ts b/packages/playwright-core/src/server/bidi/bidiPage.tsindex 910713af3b..25dbd07722 100644--- a/packages/playwright-core/src/server/bidi/bidiPage.ts+++ b/packages/playwright-core/src/server/bidi/bidiPage.ts@@ -416,7 +416,7 @@ export class BidiPage implements PageDelegate {}async updateHttpCredentials() {- await this._networkManager.setCredentials(this._browserContext._options.httpCredentials, this._browserContext.proxyCredentials());+ await this._networkManager.setCredentials(this._browserContext._options.httpCredentials);}async updateFileChooserInterception() {diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.tsindex 0bbb26ef1f..08abd03d0d 100644--- a/packages/playwright-core/src/server/browserContext.ts+++ b/packages/playwright-core/src/server/browserContext.ts@@ -465,6 +465,7 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdkconst proxy = this._options.proxy || this._browser.options.proxy || { username: undefined, password: undefined };const { username, password } = proxy;if (username) {+ this._options.httpCredentials = [{ username, password: password! }];const token = Buffer.from(`${username}:${password}`).toString('base64');this._options.extraHTTPHeaders = network.mergeHeaders([this._options.extraHTTPHeaders,@@ -473,11 +474,13 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk}}- proxyCredentials(): HttpCredentials | undefined {+ protected authenticateProxyViaCredentials() {const proxy = this._options.proxy || this._browser.options.proxy;- if (!proxy?.username)+ if (!proxy)return;- return { username: proxy.username, password: proxy.password || '' };+ const { username, password } = proxy;+ if (username)+ this._options.httpCredentials = [{ username, password: password || '' }];}async addInitScript(progress: Progress, source: string): Promise<InitScript> {diff --git a/packages/playwright-core/src/server/chromium/crBrowser.ts b/packages/playwright-core/src/server/chromium/crBrowser.tsindex 1f7c05d7c7..2bf9475749 100644--- a/packages/playwright-core/src/server/chromium/crBrowser.ts+++ b/packages/playwright-core/src/server/chromium/crBrowser.ts@@ -352,6 +352,7 @@ export class CRBrowserContext extends BrowserContext<CREventsMap> {constructor(browser: CRBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {super(browser, options, browserContextId);+ this.authenticateProxyViaCredentials();}override async initialize() {diff --git a/packages/playwright-core/src/server/chromium/crNetworkManager.ts b/packages/playwright-core/src/server/chromium/crNetworkManager.tsindex 8396c7e194..fbecd21355 100644--- a/packages/playwright-core/src/server/chromium/crNetworkManager.ts+++ b/packages/playwright-core/src/server/chromium/crNetworkManager.ts@@ -47,8 +47,7 @@ export class CRNetworkManager {private _serviceWorker: CRServiceWorker | null;private _requestIdToRequest = new Map<string, InterceptableRequest>();private _requestIdToRequestWillBeSentEvent = new Map<string, { sessionInfo: SessionInfo, event: Protocol.Network.requestWillBeSentPayload }>();- private _credentials: HttpCredentials[] | undefined;- private _proxyCredentials: HttpCredentials | undefined;+ private _credentials: HttpCredentials[] | null = null;private _attemptedAuthentications = new Set<string>();private _userRequestInterceptionEnabled = false;private _protocolRequestInterceptionEnabled = false;@@ -123,9 +122,8 @@ export class CRNetworkManager {}));}- async authenticate(credentials: HttpCredentials[] | undefined, proxyCredentials: HttpCredentials | undefined) {+ async authenticate(credentials: HttpCredentials[] | null) {this._credentials = credentials;- this._proxyCredentials = proxyCredentials;await this._updateProtocolRequestInterception();}@@ -157,7 +155,7 @@ export class CRNetworkManager {}async _updateProtocolRequestInterception() {- const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length || !!this._proxyCredentials;+ const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length;if (enabled === this._protocolRequestInterceptionEnabled)return;this._protocolRequestInterceptionEnabled = enabled;@@ -229,15 +227,12 @@ export class CRNetworkManager {_onAuthRequired(sessionInfo: SessionInfo, event: Protocol.Fetch.authRequiredPayload) {let response: 'Default' | 'CancelAuth' | 'ProvideCredentials' = 'Default';- // Proxy credentials only answer the proxy's challenge, never the website's.- const isProxy = event.authChallenge.source === 'Proxy';- const credentials = isProxy ? this._proxyCredentials : findMatchingHttpCredentials(this._credentials, event.request.url);- const attemptKey = authAttemptKey(event.requestId, isProxy);- if (this._attemptedAuthentications.has(attemptKey)) {+ const credentials = findMatchingHttpCredentials(this._credentials || undefined, event.request.url);+ if (this._attemptedAuthentications.has(event.requestId)) {response = 'CancelAuth';} else if (credentials) {response = 'ProvideCredentials';- this._attemptedAuthentications.add(attemptKey);+ this._attemptedAuthentications.add(event.requestId);}const { username, password } = credentials || { username: undefined, password: undefined };sessionInfo.session._sendMayFail('Fetch.continueWithAuth', {@@ -433,10 +428,8 @@ export class CRNetworkManager {_deleteRequest(request: InterceptableRequest) {this._requestIdToRequest.delete(request._requestId);- if (request._interceptionId) {- this._attemptedAuthentications.delete(authAttemptKey(request._interceptionId, true));- this._attemptedAuthentications.delete(authAttemptKey(request._interceptionId, false));- }+ if (request._interceptionId)+ this._attemptedAuthentications.delete(request._interceptionId);}_handleRequestRedirect(request: InterceptableRequest, responsePayload: Protocol.Network.Response, timestamp: number, hasExtraInfo: boolean) {@@ -943,7 +936,3 @@ function toResourceType(type: Protocol.Network.ResourceType): network.ResourceTyreturn 'other';}}--function authAttemptKey(interceptionId: string, isProxy: boolean) {- return `${interceptionId}:${isProxy ? 'proxy' : 'server'}`;-}diff --git a/packages/playwright-core/src/server/chromium/crPage.ts b/packages/playwright-core/src/server/chromium/crPage.tsindex 68501601f9..1510bd07df 100644--- a/packages/playwright-core/src/server/chromium/crPage.ts+++ b/packages/playwright-core/src/server/chromium/crPage.ts@@ -197,7 +197,7 @@ export class CRPage implements PageDelegate {}async updateHttpCredentials(): Promise<void> {- await this._networkManager.authenticate(this._browserContext._options.httpCredentials, this._browserContext.proxyCredentials());+ await this._networkManager.authenticate(this._browserContext._options.httpCredentials || null);}async updateEmulatedViewportSize(preserveWindowBoundaries?: boolean): Promise<void> {diff --git a/packages/playwright-core/src/server/chromium/crServiceWorker.ts b/packages/playwright-core/src/server/chromium/crServiceWorker.tsindex 962bac4cbd..abd6f58ad7 100644--- a/packages/playwright-core/src/server/chromium/crServiceWorker.ts+++ b/packages/playwright-core/src/server/chromium/crServiceWorker.ts@@ -89,7 +89,7 @@ export class CRServiceWorker extends Worker {async updateHttpCredentials(): Promise<void> {if (!this._isNetworkInspectionEnabled())return;- await this._networkManager?.authenticate(this.browserContext._options.httpCredentials, this.browserContext.proxyCredentials()).catch(() => {});+ await this._networkManager?.authenticate(this.browserContext._options.httpCredentials || null).catch(() => {});}async updateExtraHTTPHeaders(): Promise<void> {diff --git a/tests/bidi/expectations/bidi-chromium-library.txt b/tests/bidi/expectations/bidi-chromium-library.txtindex 738907e510..0d104126b4 100644--- a/tests/bidi/expectations/bidi-chromium-library.txt+++ b/tests/bidi/expectations/bidi-chromium-library.txt@@ -495,7 +495,6 @@ library/popup.spec.ts › should not throttle rAF in the opener page [timeout]library/popup.spec.ts › should respect routes from browser context [fail]library/popup.spec.ts › should respect routes from browser context when using window.open [fail]library/popup.spec.ts › should use viewport size from window features [timeout]-library/proxy.spec.ts › should authenticate [fail]library/proxy.spec.ts › should proxy local network requests › by default › link-local [flaky]library/resource-timing.spec.ts › should work when serving from memory cache [fail]library/role-utils.spec.ts › wpt accname #0 [flaky]diff --git a/tests/library/proxy.spec.ts b/tests/library/proxy.spec.tsindex 3370274b40..db6e6e9038 100644--- a/tests/library/proxy.spec.ts+++ b/tests/library/proxy.spec.ts@@ -141,9 +141,7 @@ for (const host of ['localhost', '127.0.0.1']) {});}-it('should authenticate', {- annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43326' },-}, async ({ browserType, server }) => {+it('should authenticate', async ({ browserType, server }) => {server.setRoute('/target.html', async (req, res) => {const auth = req.headers['proxy-authorization'];if (!auth) {@@ -155,25 +153,12 @@ it('should authenticate', {res.end(`<html><title>${auth}</title></html>`);}});- server.setRoute('/protected.html', async (req, res) => {- if (!req.headers['proxy-authorization']) {- res.writeHead(407, { 'Proxy-Authenticate': 'Basic realm="Access to internal site"' });- res.end();- return;- }- res.writeHead(401, { 'WWW-Authenticate': 'Basic realm="site"' });- res.end(req.headers['authorization'] || 'no credentials');- });const browser = await browserType.launch({proxy: { server: server.HOST, username: 'user', password: 'secret' }});const page = await browser.newPage();await page.goto('http://non-existent.com/target.html');expect(await page.title()).toBe('Basic ' + Buffer.from('user:secret').toString('base64'));- // Proxy credentials must not be sent to the website's own auth challenge.- const response = await page.goto('http://non-existent.com/protected.html');- expect(response.status()).toBe(401);- expect(await page.locator('body').textContent()).toBe('no credentials');await browser.close();});