This commit is contained in:
Max Schmitt 2024-07-05 17:28:36 +02:00
parent d6628657da
commit 2647b6d444
2 changed files with 16 additions and 6 deletions

View file

@ -85,13 +85,13 @@ export abstract class Browser extends SdkObject {
async newContext(metadata: CallMetadata, options: channels.BrowserNewContextParams): Promise<BrowserContext> { async newContext(metadata: CallMetadata, options: channels.BrowserNewContextParams): Promise<BrowserContext> {
validateBrowserContextOptions(options, this.options); validateBrowserContextOptions(options, this.options);
let clientCertificateProxy: ClientCertificatesProxy | undefined; let socksServer: ClientCertificatesProxy | undefined;
if (shouldUseMitmSocksProxy(options)) { if (shouldUseMitmSocksProxy(options)) {
clientCertificateProxy = new ClientCertificatesProxy(options); socksServer = new ClientCertificatesProxy(options);
options.proxy = { server: await clientCertificateProxy.listen() }; options.proxy = { server: await socksServer.listen() };
} }
const context = await this.doCreateNewContext(options); const context = await this.doCreateNewContext(options);
context._socksServer = clientCertificateProxy; context._socksServer = socksServer;
if (options.storageState) if (options.storageState)
await context.setStorageState(metadata, options.storageState); await context.setStorageState(metadata, options.storageState);
return context; return context;

View file

@ -21,7 +21,7 @@ import fs from 'fs';
import tls from 'tls'; import tls from 'tls';
import stream from 'stream'; import stream from 'stream';
import { createSocket } from '../utils/happy-eyeballs'; import { createSocket } from '../utils/happy-eyeballs';
import { assert, isUnderTest, urlMatches } from '../utils'; import { assert, globToRegex, isUnderTest } from '../utils';
import type { SocksSocketClosedPayload, SocksSocketDataPayload, SocksSocketRequestedPayload } from '../common/socksProxy'; import type { SocksSocketClosedPayload, SocksSocketDataPayload, SocksSocketRequestedPayload } from '../common/socksProxy';
import { SocksProxy } from '../common/socksProxy'; import { SocksProxy } from '../common/socksProxy';
import type * as channels from '@protocol/channels'; import type * as channels from '@protocol/channels';
@ -168,11 +168,21 @@ export class ClientCertificatesProxy {
} }
} }
const kClientCertificatesGlobRegex = Symbol('kClientCertificatesGlobRegex');
export function clientCertificatesToTLSOptions( export function clientCertificatesToTLSOptions(
clientCertificates: channels.BrowserNewContextOptions['clientCertificates'], clientCertificates: channels.BrowserNewContextOptions['clientCertificates'],
requestURL: string requestURL: string
): Pick<https.RequestOptions, 'pfx' | 'key' | 'passphrase' | 'cert' | 'ca'> | undefined { ): Pick<https.RequestOptions, 'pfx' | 'key' | 'passphrase' | 'cert' | 'ca'> | undefined {
const matchingCerts = clientCertificates?.filter(c => urlMatches(undefined, requestURL, c.url)); const matchingCerts = clientCertificates?.filter(c => {
let regex: RegExp | undefined = (c as any)[kClientCertificatesGlobRegex];
if (!regex) {
regex = globToRegex(c.url);
(c as any)[kClientCertificatesGlobRegex] = regex;
}
regex.lastIndex = 0;
return regex.test(requestURL);
});
if (!matchingCerts || !matchingCerts.length) if (!matchingCerts || !matchingCerts.length)
return; return;
const requestOptions = { const requestOptions = {