test: listen always on 127.0.0.1 for client certificate tests (#32677)

This commit is contained in:
Max Schmitt 2024-09-18 17:09:08 +02:00 committed by GitHub
parent 523ec83cad
commit d4eecafa8a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 15 deletions

View file

@ -263,6 +263,7 @@ export abstract class APIRequestContext extends SdkObject {
try { try {
return await this._sendRequest(progress, url, options, postData); return await this._sendRequest(progress, url, options, postData);
} catch (e) { } catch (e) {
e = rewriteOpenSSLErrorIfNeeded(e);
if (maxRetries === 0) if (maxRetries === 0)
throw e; throw e;
if (i === maxRetries || (options.deadline && monotonicTime() + backoff > options.deadline)) if (i === maxRetries || (options.deadline && monotonicTime() + backoff > options.deadline))
@ -475,7 +476,7 @@ export abstract class APIRequestContext extends SdkObject {
body.on('data', chunk => chunks.push(chunk)); body.on('data', chunk => chunks.push(chunk));
body.on('end', notifyBodyFinished); body.on('end', notifyBodyFinished);
}); });
request.on('error', error => reject(rewriteOpenSSLErrorIfNeeded(error))); request.on('error', reject);
const disposeListener = () => { const disposeListener = () => {
reject(new Error('Request context disposed.')); reject(new Error('Request context disposed.'));

View file

@ -57,14 +57,14 @@ export class TestProxy {
this._prependHandler('request', (req: IncomingMessage) => { this._prependHandler('request', (req: IncomingMessage) => {
this.requestUrls.push(req.url); this.requestUrls.push(req.url);
const url = new URL(req.url); const url = new URL(req.url);
url.host = `localhost:${port}`; url.host = `127.0.0.1:${port}`;
req.url = url.toString(); req.url = url.toString();
}); });
this._prependHandler('connect', (req: IncomingMessage) => { this._prependHandler('connect', (req: IncomingMessage) => {
if (!options?.allowConnectRequests) if (!options?.allowConnectRequests)
return; return;
this.connectHosts.push(req.url); this.connectHosts.push(req.url);
req.url = `localhost:${port}`; req.url = `127.0.0.1:${port}`;
}); });
} }
@ -138,10 +138,10 @@ export async function setupSocksForwardingServer({
connections.get(payload.uid)?.destroy(); connections.get(payload.uid)?.destroy();
connections.delete(payload.uid); connections.delete(payload.uid);
}); });
await socksProxy.listen(port, 'localhost'); await socksProxy.listen(port, '127.0.0.1');
return { return {
closeProxyServer: () => socksProxy.close(), closeProxyServer: () => socksProxy.close(),
proxyServerAddr: `socks5://localhost:${port}`, proxyServerAddr: `socks5://127.0.0.1:${port}`,
connectHosts, connectHosts,
}; };
} }

View file

@ -28,7 +28,6 @@ const { createHttpsServer, createHttp2Server } = require('../../packages/playwri
type TestOptions = { type TestOptions = {
startCCServer(options?: { startCCServer(options?: {
host?: string;
http2?: boolean; http2?: boolean;
enableHTTP1FallbackWhenUsingHttp2?: boolean; enableHTTP1FallbackWhenUsingHttp2?: boolean;
useFakeLocalhost?: boolean; useFakeLocalhost?: boolean;
@ -68,8 +67,8 @@ const test = base.extend<TestOptions>({
} }
res.end(parts.map(({ key, value }) => `<div data-testid="${key}">${value}</div>`).join('')); res.end(parts.map(({ key, value }) => `<div data-testid="${key}">${value}</div>`).join(''));
}); });
await new Promise<void>(f => server.listen(0, options?.host ?? 'localhost', () => f())); await new Promise<void>(f => server.listen(0, '127.0.0.1', () => f()));
const host = options?.useFakeLocalhost ? 'local.playwright' : 'localhost'; const host = options?.useFakeLocalhost ? 'local.playwright' : '127.0.0.1';
return `https://${host}:${(server.address() as net.AddressInfo).port}/`; return `https://${host}:${(server.address() as net.AddressInfo).port}/`;
}); });
if (server) if (server)
@ -195,7 +194,7 @@ test.describe('fetch', () => {
}); });
test('pass with trusted client certificates and when a socks proxy is used', async ({ playwright, startCCServer, asset }) => { test('pass with trusted client certificates and when a socks proxy is used', async ({ playwright, startCCServer, asset }) => {
const serverURL = await startCCServer({ host: '127.0.0.1' }); const serverURL = await startCCServer();
const serverPort = parseInt(new URL(serverURL).port, 10); const serverPort = parseInt(new URL(serverURL).port, 10);
const { proxyServerAddr, closeProxyServer, connectHosts } = await setupSocksForwardingServer({ const { proxyServerAddr, closeProxyServer, connectHosts } = await setupSocksForwardingServer({
port: test.info().workerIndex + 2048 + 2, port: test.info().workerIndex + 2048 + 2,
@ -366,13 +365,13 @@ test.describe('browser', () => {
}); });
expect(proxyServer.connectHosts).toEqual([]); expect(proxyServer.connectHosts).toEqual([]);
await page.goto(serverURL); await page.goto(serverURL);
expect([...new Set(proxyServer.connectHosts)]).toEqual([`localhost:${new URL(serverURL).port}`]); expect([...new Set(proxyServer.connectHosts)]).toEqual([`127.0.0.1:${new URL(serverURL).port}`]);
await expect(page.getByTestId('message')).toHaveText('Hello Alice, your certificate was issued by localhost!'); await expect(page.getByTestId('message')).toHaveText('Hello Alice, your certificate was issued by localhost!');
await page.close(); await page.close();
}); });
test('should pass with matching certificates and when a socks proxy is used', async ({ browser, startCCServer, asset, browserName }) => { test('should pass with matching certificates and when a socks proxy is used', async ({ browser, startCCServer, asset, browserName }) => {
const serverURL = await startCCServer({ useFakeLocalhost: browserName === 'webkit' && process.platform === 'darwin', host: '127.0.0.1' }); const serverURL = await startCCServer({ useFakeLocalhost: browserName === 'webkit' && process.platform === 'darwin' });
const serverPort = parseInt(new URL(serverURL).port, 10); const serverPort = parseInt(new URL(serverURL).port, 10);
const { proxyServerAddr, closeProxyServer, connectHosts } = await setupSocksForwardingServer({ const { proxyServerAddr, closeProxyServer, connectHosts } = await setupSocksForwardingServer({
port: test.info().workerIndex + 2048 + 2, port: test.info().workerIndex + 2048 + 2,
@ -390,7 +389,7 @@ test.describe('browser', () => {
}); });
expect(connectHosts).toEqual([]); expect(connectHosts).toEqual([]);
await page.goto(serverURL); await page.goto(serverURL);
expect(connectHosts).toEqual([`localhost:${serverPort}`]); expect(connectHosts).toEqual([`127.0.0.1:${serverPort}`]);
await expect(page.getByTestId('message')).toHaveText('Hello Alice, your certificate was issued by localhost!'); await expect(page.getByTestId('message')).toHaveText('Hello Alice, your certificate was issued by localhost!');
await page.close(); await page.close();
await closeProxyServer(); await closeProxyServer();
@ -625,7 +624,7 @@ test.describe('browser', () => {
}); });
test('should pass with matching certificates on context APIRequestContext instance', async ({ browser, startCCServer, asset, browserName }) => { test('should pass with matching certificates on context APIRequestContext instance', async ({ browser, startCCServer, asset, browserName }) => {
const serverURL = await startCCServer({ host: '127.0.0.1' }); const serverURL = await startCCServer();
const baseOptions = { const baseOptions = {
certPath: asset('client-certificates/client/trusted/cert.pem'), certPath: asset('client-certificates/client/trusted/cert.pem'),
keyPath: asset('client-certificates/client/trusted/key.pem'), keyPath: asset('client-certificates/client/trusted/key.pem'),
@ -688,7 +687,7 @@ test.describe('browser', () => {
}], }],
}); });
{ {
await page.goto(serverURL.replace('localhost', 'local.playwright')); await page.goto(serverURL.replace('127.0.0.1', 'local.playwright'));
await expect(page.getByTestId('message')).toHaveText('Sorry, but you need to provide a client certificate to continue.'); await expect(page.getByTestId('message')).toHaveText('Sorry, but you need to provide a client certificate to continue.');
await expect(page.getByTestId('alpn-protocol')).toHaveText('h2'); await expect(page.getByTestId('alpn-protocol')).toHaveText('h2');
await expect(page.getByTestId('servername')).toHaveText('local.playwright'); await expect(page.getByTestId('servername')).toHaveText('local.playwright');
@ -714,7 +713,7 @@ test.describe('browser', () => {
}], }],
}); });
{ {
await page.goto(serverURL.replace('localhost', 'local.playwright')); await page.goto(serverURL.replace('127.0.0.1', 'local.playwright'));
await expect(page.getByTestId('message')).toHaveText('Sorry, but you need to provide a client certificate to continue.'); await expect(page.getByTestId('message')).toHaveText('Sorry, but you need to provide a client certificate to continue.');
await expect(page.getByTestId('alpn-protocol')).toHaveText('http/1.1'); await expect(page.getByTestId('alpn-protocol')).toHaveText('http/1.1');
} }