From d910f366ab392a1f319789a9bff45d071dac3802 Mon Sep 17 00:00:00 2001 From: Nikolay Vitkov Date: Thu, 4 Apr 2024 11:48:48 +0200 Subject: [PATCH] fix: disabling of authentication --- packages/puppeteer-core/src/api/Page.ts | 12 ++++++++++-- packages/puppeteer-core/src/cdp/NetworkManager.ts | 13 +++---------- packages/puppeteer-core/src/cdp/Page.ts | 5 +++-- test/src/network.spec.ts | 5 +---- 4 files changed, 17 insertions(+), 18 deletions(-) diff --git a/packages/puppeteer-core/src/api/Page.ts b/packages/puppeteer-core/src/api/Page.ts index b3d65103720cf..2fcd4691109e3 100644 --- a/packages/puppeteer-core/src/api/Page.ts +++ b/packages/puppeteer-core/src/api/Page.ts @@ -32,7 +32,7 @@ import type {HTTPResponse} from '../api/HTTPResponse.js'; import type {Accessibility} from '../cdp/Accessibility.js'; import type {Coverage} from '../cdp/Coverage.js'; import type {DeviceRequestPrompt} from '../cdp/DeviceRequestPrompt.js'; -import type {Credentials, NetworkConditions} from '../cdp/NetworkManager.js'; +import type {NetworkConditions} from '../cdp/NetworkManager.js'; import type {Tracing} from '../cdp/Tracing.js'; import type {ConsoleMessage} from '../common/ConsoleMessage.js'; import type { @@ -512,6 +512,14 @@ export interface PageEvents extends Record { [PageEvent.WorkerDestroyed]: WebWorker; } +/** + * @public + */ +export interface Credentials { + username: string; + password: string; +} + /** * @public */ @@ -1417,7 +1425,7 @@ export abstract class Page extends EventEmitter { * @remarks * To disable authentication, pass `null`. */ - abstract authenticate(credentials: Credentials): Promise; + abstract authenticate(credentials: Credentials | null): Promise; /** * The extra HTTP headers will be sent with every request the page initiates. diff --git a/packages/puppeteer-core/src/cdp/NetworkManager.ts b/packages/puppeteer-core/src/cdp/NetworkManager.ts index 4fd61116d2d18..33df5d2053f24 100644 --- a/packages/puppeteer-core/src/cdp/NetworkManager.ts +++ b/packages/puppeteer-core/src/cdp/NetworkManager.ts @@ -8,6 +8,7 @@ import type {Protocol} from 'devtools-protocol'; import {CDPSessionEvent, type CDPSession} from '../api/CDPSession.js'; import type {Frame} from '../api/Frame.js'; +import type {Credentials} from '../api/Page.js'; import {EventEmitter, EventSubscription} from '../common/EventEmitter.js'; import { NetworkManagerEvent, @@ -24,14 +25,6 @@ import { type FetchRequestId, } from './NetworkEventManager.js'; -/** - * @public - */ -export interface Credentials { - username: string; - password: string; -} - /** * @public */ @@ -72,7 +65,7 @@ export class NetworkManager extends EventEmitter { #frameManager: FrameProvider; #networkEventManager = new NetworkEventManager(); #extraHTTPHeaders?: Record; - #credentials?: Credentials; + #credentials: Credentials | null = null; #attemptedAuthentications = new Set(); #userRequestInterceptionEnabled = false; #protocolRequestInterceptionEnabled = false; @@ -135,7 +128,7 @@ export class NetworkManager extends EventEmitter { this.#clients.delete(client); } - async authenticate(credentials?: Credentials): Promise { + async authenticate(credentials: Credentials | null): Promise { this.#credentials = credentials; const enabled = this.#userRequestInterceptionEnabled || !!this.#credentials; if (enabled === this.#protocolRequestInterceptionEnabled) { diff --git a/packages/puppeteer-core/src/cdp/Page.ts b/packages/puppeteer-core/src/cdp/Page.ts index d8158c82b4b59..37204ee110150 100644 --- a/packages/puppeteer-core/src/cdp/Page.ts +++ b/packages/puppeteer-core/src/cdp/Page.ts @@ -15,6 +15,7 @@ import type {Frame, WaitForOptions} from '../api/Frame.js'; import type {HTTPRequest} from '../api/HTTPRequest.js'; import type {HTTPResponse} from '../api/HTTPResponse.js'; import type {JSHandle} from '../api/JSHandle.js'; +import type {Credentials} from '../api/Page.js'; import { Page, PageEvent, @@ -71,7 +72,7 @@ import {FrameManagerEvent} from './FrameManagerEvents.js'; import {CdpKeyboard, CdpMouse, CdpTouchscreen} from './Input.js'; import {MAIN_WORLD} from './IsolatedWorlds.js'; import {releaseObject} from './JSHandle.js'; -import type {Credentials, NetworkConditions} from './NetworkManager.js'; +import type {NetworkConditions} from './NetworkManager.js'; import type {CdpTarget} from './Target.js'; import type {TargetManager} from './TargetManager.js'; import {TargetManagerEvent} from './TargetManager.js'; @@ -735,7 +736,7 @@ export class CdpPage extends Page { this.#bindings.delete(name); } - override async authenticate(credentials: Credentials): Promise { + override async authenticate(credentials: Credentials | null): Promise { return await this.#frameManager.networkManager.authenticate(credentials); } diff --git a/test/src/network.spec.ts b/test/src/network.spec.ts index c6f51a3412f4c..d194ffab702c4 100644 --- a/test/src/network.spec.ts +++ b/test/src/network.spec.ts @@ -759,10 +759,7 @@ describe('network', function () { }); let response = (await page.goto(server.EMPTY_PAGE))!; expect(response.status()).toBe(200); - await page.authenticate({ - username: '', - password: '', - }); + await page.authenticate(null); // Navigate to a different origin to bust Chrome's credential caching. try { response = (await page.goto(