chore(port-forwarding): validate forwarded ports on the client side (#6756)

This commit is contained in:
Max Schmitt 2021-05-26 16:51:38 +02:00 committed by GitHub
parent 792f3d41e9
commit 59d591bce6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 10 additions and 3 deletions

View file

@ -97,7 +97,7 @@ export class BrowserServerLauncherImpl implements BrowserServerLauncher {
portForwardingServer.enablePortForwarding(ports); portForwardingServer.enablePortForwarding(ports);
}); });
const incomingSocksSocketHandler = (socket: SocksInterceptedSocketHandler) => { const incomingSocksSocketHandler = (socket: SocksInterceptedSocketHandler) => {
playwrightDispatcher._dispatchEvent('incomingSocksSocket', { socket: new SocksSocketDispatcher(playwrightDispatcher._scope, socket) }); playwrightDispatcher._dispatchEvent('incomingSocksSocket', { socket: new SocksSocketDispatcher(playwrightDispatcher, socket) });
}; };
portForwardingServer.on('incomingSocksSocket', incomingSocksSocketHandler); portForwardingServer.on('incomingSocksSocket', incomingSocksSocketHandler);

View file

@ -189,6 +189,7 @@ export class BrowserType extends ChannelOwner<channels.BrowserTypeChannel, chann
await playwright._channel.enablePortForwarding({ await playwright._channel.enablePortForwarding({
ports: params._forwardPorts, ports: params._forwardPorts,
}); });
playwright._forwardPorts = params._forwardPorts;
} catch (err) { } catch (err) {
reject(err); reject(err);
return; return;

View file

@ -44,6 +44,7 @@ export class Playwright extends ChannelOwner<channels.PlaywrightChannel, channel
readonly selectors: Selectors; readonly selectors: Selectors;
readonly errors: { TimeoutError: typeof TimeoutError }; readonly errors: { TimeoutError: typeof TimeoutError };
private _selectorsOwner: SelectorsOwner; private _selectorsOwner: SelectorsOwner;
_forwardPorts: number[] = [];
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) { constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.PlaywrightInitializer) {
super(parent, type, guid, initializer); super(parent, type, guid, initializer);

View file

@ -17,6 +17,7 @@
import net from 'net'; import net from 'net';
import * as channels from '../protocol/channels'; import * as channels from '../protocol/channels';
import { Playwright } from './playwright';
import { assert, isLocalIpAddress, isUnderTest } from '../utils/utils'; import { assert, isLocalIpAddress, isUnderTest } from '../utils/utils';
import { ChannelOwner } from './channelOwner'; import { ChannelOwner } from './channelOwner';
@ -28,11 +29,13 @@ export class SocksSocket extends ChannelOwner<channels.SocksSocketChannel, chann
constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.SocksSocketInitializer) { constructor(parent: ChannelOwner, type: string, guid: string, initializer: channels.SocksSocketInitializer) {
super(parent, type, guid, initializer); super(parent, type, guid, initializer);
this._connection.on('disconnect', () => this._socket.end()); assert(parent instanceof Playwright);
assert(parent._forwardPorts.includes(this._initializer.dstPort));
assert(isLocalIpAddress(this._initializer.dstAddr));
if (isUnderTest() && process.env.PW_TEST_PROXY_TARGET) if (isUnderTest() && process.env.PW_TEST_PROXY_TARGET)
this._initializer.dstPort = Number(process.env.PW_TEST_PROXY_TARGET); this._initializer.dstPort = Number(process.env.PW_TEST_PROXY_TARGET);
assert(isLocalIpAddress(this._initializer.dstAddr));
this._socket = net.createConnection(this._initializer.dstPort, this._initializer.dstAddr); this._socket = net.createConnection(this._initializer.dstPort, this._initializer.dstAddr);
this._socket.on('error', (err: Error) => this._channel.error({error: String(err)})); this._socket.on('error', (err: Error) => this._channel.error({error: String(err)}));
@ -50,6 +53,8 @@ export class SocksSocket extends ChannelOwner<channels.SocksSocketChannel, chann
this._socket.write(Buffer.from(data, 'base64')); this._socket.write(Buffer.from(data, 'base64'));
}); });
this._channel.on('close', () => this._socket.end()); this._channel.on('close', () => this._socket.end());
this._connection.on('disconnect', () => this._socket.end());
} }
async write(data: Buffer): Promise<void> { async write(data: Buffer): Promise<void> {