Skip to content

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

fix: do not send proxy credentials to website auth challenges (#43342)

Yury Semikhatsky8cc52881a45f
diff --git a/packages/playwright-core/src/server/bidi/bidiBrowser.ts b/packages/playwright-core/src/server/bidi/bidiBrowser.ts
index c7f09fd9aa..c5fc3983c4 100644
--- a/packages/playwright-core/src/server/bidi/bidiBrowser.ts
+++ b/packages/playwright-core/src/server/bidi/bidiBrowser.ts
@@ -225,9 +225,7 @@ export class BidiBrowserContext extends BrowserContext {
constructor(browser: BidiBrowser, browserContextId: string | undefined, options: types.BrowserContextOptions) {
super(browser, options, browserContextId);
- if (browser._bidiOptions.proxyAuthentication === 'credentials')
- this.authenticateProxyViaCredentials();
- else
+ if (browser._bidiOptions.proxyAuthentication === 'header')
this.authenticateProxyViaHeader();
}
diff --git a/packages/playwright-core/src/server/bidi/bidiNetworkManager.ts b/packages/playwright-core/src/server/bidi/bidiNetworkManager.ts
index 3076948af4..b7bb9e3559 100644
--- a/packages/playwright-core/src/server/bidi/bidiNetworkManager.ts
+++ b/packages/playwright-core/src/server/bidi/bidiNetworkManager.ts
@@ -38,6 +38,7 @@ 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;
@@ -199,15 +200,21 @@ export class BidiNetworkManager {
private _onAuthRequired(params: bidi.Network.AuthRequiredParameters) {
// Don't look at params.response.authChallenges: Chromium does not report them.
- const credentials = findMatchingHttpCredentials(this._page.browserContext._options.httpCredentials, params.request.url);
+ // 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);
if (credentials) {
- if (this._attemptedAuthentications.has(params.request.request)) {
+ if (this._attemptedAuthentications.has(attemptKey)) {
this._session.sendMayFail('network.continueWithAuth', {
request: params.request.request,
action: 'cancel',
});
} else {
- this._attemptedAuthentications.add(params.request.request);
+ this._attemptedAuthentications.add(attemptKey);
this._session.sendMayFail('network.continueWithAuth', {
request: params.request.request,
action: 'provideCredentials',
@@ -228,7 +235,8 @@ export class BidiNetworkManager {
_deleteRequest(requestId: string) {
this._requests.delete(requestId);
- this._attemptedAuthentications.delete(requestId);
+ this._attemptedAuthentications.delete(authAttemptKey(requestId, true));
+ this._attemptedAuthentications.delete(authAttemptKey(requestId, false));
}
async setRequestInterception(value: boolean) {
@@ -236,13 +244,14 @@ export class BidiNetworkManager {
await this._updateProtocolRequestInterception();
}
- async setCredentials(credentials: HttpCredentials[] | undefined) {
+ async setCredentials(credentials: HttpCredentials[] | undefined, proxyCredentials: HttpCredentials | undefined) {
this._credentials = credentials;
+ this._proxyCredentials = proxyCredentials;
await this._updateProtocolRequestInterception();
}
async _updateProtocolRequestInterception(initial?: boolean) {
- const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length;
+ const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length || !!this._proxyCredentials;
if (enabled === this._protocolRequestInterceptionEnabled)
return;
this._protocolRequestInterceptionEnabled = enabled;
@@ -437,3 +446,7 @@ 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.ts
index 25dbd07722..910713af3b 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);
+ await this._networkManager.setCredentials(this._browserContext._options.httpCredentials, this._browserContext.proxyCredentials());
}
async updateFileChooserInterception() {
diff --git a/packages/playwright-core/src/server/browserContext.ts b/packages/playwright-core/src/server/browserContext.ts
index 08abd03d0d..0bbb26ef1f 100644
--- a/packages/playwright-core/src/server/browserContext.ts
+++ b/packages/playwright-core/src/server/browserContext.ts
@@ -465,7 +465,6 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
const 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,
@@ -474,13 +473,11 @@ export abstract class BrowserContext<EM extends EventMap = EventMap> extends Sdk
}
}
- protected authenticateProxyViaCredentials() {
+ proxyCredentials(): HttpCredentials | undefined {
const proxy = this._options.proxy || this._browser.options.proxy;
- if (!proxy)
+ if (!proxy?.username)
return;
- const { username, password } = proxy;
- if (username)
- this._options.httpCredentials = [{ username, password: password || '' }];
+ return { username: proxy.username, password: proxy.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.ts
index 2bf9475749..1f7c05d7c7 100644
--- a/packages/playwright-core/src/server/chromium/crBrowser.ts
+++ b/packages/playwright-core/src/server/chromium/crBrowser.ts
@@ -352,7 +352,6 @@ 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.ts
index fbecd21355..8396c7e194 100644
--- a/packages/playwright-core/src/server/chromium/crNetworkManager.ts
+++ b/packages/playwright-core/src/server/chromium/crNetworkManager.ts
@@ -47,7 +47,8 @@ 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[] | null = null;
+ private _credentials: HttpCredentials[] | undefined;
+ private _proxyCredentials: HttpCredentials | undefined;
private _attemptedAuthentications = new Set<string>();
private _userRequestInterceptionEnabled = false;
private _protocolRequestInterceptionEnabled = false;
@@ -122,8 +123,9 @@ export class CRNetworkManager {
}));
}
- async authenticate(credentials: HttpCredentials[] | null) {
+ async authenticate(credentials: HttpCredentials[] | undefined, proxyCredentials: HttpCredentials | undefined) {
this._credentials = credentials;
+ this._proxyCredentials = proxyCredentials;
await this._updateProtocolRequestInterception();
}
@@ -155,7 +157,7 @@ export class CRNetworkManager {
}
async _updateProtocolRequestInterception() {
- const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length;
+ const enabled = this._userRequestInterceptionEnabled || !!this._credentials?.length || !!this._proxyCredentials;
if (enabled === this._protocolRequestInterceptionEnabled)
return;
this._protocolRequestInterceptionEnabled = enabled;
@@ -227,12 +229,15 @@ export class CRNetworkManager {
_onAuthRequired(sessionInfo: SessionInfo, event: Protocol.Fetch.authRequiredPayload) {
let response: 'Default' | 'CancelAuth' | 'ProvideCredentials' = 'Default';
- const credentials = findMatchingHttpCredentials(this._credentials || undefined, event.request.url);
- if (this._attemptedAuthentications.has(event.requestId)) {
+ // 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)) {
response = 'CancelAuth';
} else if (credentials) {
response = 'ProvideCredentials';
- this._attemptedAuthentications.add(event.requestId);
+ this._attemptedAuthentications.add(attemptKey);
}
const { username, password } = credentials || { username: undefined, password: undefined };
sessionInfo.session._sendMayFail('Fetch.continueWithAuth', {
@@ -428,8 +433,10 @@ export class CRNetworkManager {
_deleteRequest(request: InterceptableRequest) {
this._requestIdToRequest.delete(request._requestId);
- if (request._interceptionId)
- this._attemptedAuthentications.delete(request._interceptionId);
+ if (request._interceptionId) {
+ this._attemptedAuthentications.delete(authAttemptKey(request._interceptionId, true));
+ this._attemptedAuthentications.delete(authAttemptKey(request._interceptionId, false));
+ }
}
_handleRequestRedirect(request: InterceptableRequest, responsePayload: Protocol.Network.Response, timestamp: number, hasExtraInfo: boolean) {
@@ -936,3 +943,7 @@ function toResourceType(type: Protocol.Network.ResourceType): network.ResourceTy
return '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.ts
index 1510bd07df..68501601f9 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 || null);
+ await this._networkManager.authenticate(this._browserContext._options.httpCredentials, this._browserContext.proxyCredentials());
}
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.ts
index abd6f58ad7..962bac4cbd 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 || null).catch(() => {});
+ await this._networkManager?.authenticate(this.browserContext._options.httpCredentials, this.browserContext.proxyCredentials()).catch(() => {});
}
async updateExtraHTTPHeaders(): Promise<void> {
diff --git a/tests/bidi/expectations/bidi-chromium-library.txt b/tests/bidi/expectations/bidi-chromium-library.txt
index 0d104126b4..738907e510 100644
--- a/tests/bidi/expectations/bidi-chromium-library.txt
+++ b/tests/bidi/expectations/bidi-chromium-library.txt
@@ -495,6 +495,7 @@ 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.ts
index db6e6e9038..3370274b40 100644
--- a/tests/library/proxy.spec.ts
+++ b/tests/library/proxy.spec.ts
@@ -141,7 +141,9 @@ for (const host of ['localhost', '127.0.0.1']) {
});
}
-it('should authenticate', async ({ browserType, server }) => {
+it('should authenticate', {
+ annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43326' },
+}, async ({ browserType, server }) => {
server.setRoute('/target.html', async (req, res) => {
const auth = req.headers['proxy-authorization'];
if (!auth) {
@@ -153,12 +155,25 @@ it('should authenticate', async ({ browserType, server }) => {
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();
});