Skip to content

Commit 0e11465

Browse files
committed
fix: mitm session ports were getting reused and conflicting
1 parent d12eae7 commit 0e11465

File tree

4 files changed

+27
-13
lines changed

4 files changed

+27
-13
lines changed

core/lib/Session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export default class Session extends TypedEventEmitter<{
8585
if (options.locale) this.browserEmulator.locale = options.locale;
8686

8787
if (!this.browserEmulator.canPolyfill) {
88-
log.warn('BrowserEmulators.PolyfillNotSupported', {
88+
log.info('BrowserEmulators.PolyfillNotSupported', {
8989
sessionId: this.id,
9090
browserEmulatorId: this.browserEmulatorId,
9191
userAgentString: this.browserEmulator.userAgentString,

mitm/handlers/RequestSession.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ import ResourceState from '../interfaces/ResourceState';
2222
const { log } = Log(module);
2323

2424
export default class RequestSession extends TypedEventEmitter<IRequestSessionEvents> {
25-
public static sessions: { [sessionId: string]: RequestSession } = {};
26-
public static proxyPortSessionIds: { [port: number]: string } = {};
25+
public static sessionById: { [sessionId: string]: RequestSession } = {};
26+
public static sessionIdByPort: { [port: number]: string } = {};
27+
public static portsBySessionId: { [sessionId: number]: Set<number> } = {};
2728

2829
public websocketBrowserResourceIds: {
2930
[headersHash: string]: IResolvablePromise<string>;
@@ -63,7 +64,7 @@ export default class RequestSession extends TypedEventEmitter<IRequestSessionEve
6364
readonly networkInterceptorDelegate: INetworkInterceptorDelegate = { http: {} },
6465
) {
6566
super();
66-
RequestSession.sessions[sessionId] = this;
67+
RequestSession.sessionById[sessionId] = this;
6768
this.logger = log.createChild(module, {
6869
sessionId,
6970
});
@@ -209,7 +210,18 @@ export default class RequestSession extends TypedEventEmitter<IRequestSessionEve
209210
this.dns.close();
210211

211212
// give it a second for lingering requests to finish
212-
setTimeout(() => delete RequestSession.sessions[this.sessionId], 1e3).unref();
213+
setTimeout(
214+
sessionId => {
215+
const ports = RequestSession.portsBySessionId[sessionId] || [];
216+
for (const port of ports) {
217+
delete RequestSession.sessionIdByPort[port];
218+
}
219+
delete RequestSession.portsBySessionId[sessionId];
220+
delete RequestSession.sessionById[sessionId];
221+
},
222+
1e3,
223+
this.sessionId,
224+
).unref();
213225
}
214226

215227
public shouldBlockRequest(url: string): boolean {
@@ -303,7 +315,7 @@ export default class RequestSession extends TypedEventEmitter<IRequestSessionEve
303315
}
304316

305317
public static async close(): Promise<void> {
306-
await Promise.all(Object.values(RequestSession.sessions).map(x => x.close()));
318+
await Promise.all(Object.values(RequestSession.sessionById).map(x => x.close()));
307319
}
308320

309321
public static readSessionId(
@@ -312,7 +324,7 @@ export default class RequestSession extends TypedEventEmitter<IRequestSessionEve
312324
): string {
313325
const authHeader = requestHeaders['proxy-authorization'] as string;
314326
if (!authHeader) {
315-
return RequestSession.proxyPortSessionIds[remotePort];
327+
return RequestSession.sessionIdByPort[remotePort];
316328
}
317329

318330
const [, sessionId] = Buffer.from(authHeader.split(' ')[1], 'base64').toString().split(':');
@@ -321,7 +333,9 @@ export default class RequestSession extends TypedEventEmitter<IRequestSessionEve
321333

322334
public static registerProxySession(loopbackProxySocket: net.Socket, sessionId: string): void {
323335
// local port is the side that originates from our http server
324-
this.proxyPortSessionIds[loopbackProxySocket.localPort] = sessionId;
336+
this.portsBySessionId[sessionId] = this.portsBySessionId[sessionId] || new Set();
337+
this.portsBySessionId[sessionId].add(loopbackProxySocket.localPort);
338+
this.sessionIdByPort[loopbackProxySocket.localPort] = sessionId;
325339
}
326340

327341
public static sendNeedsAuth(socket: net.Socket): void {

mitm/lib/MitmProxy.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,11 @@ export default class MitmProxy {
109109
return RequestSession.sendNeedsAuth(proxyToClientResponse.socket);
110110
}
111111

112-
const requestSession = RequestSession.sessions[sessionId];
112+
const requestSession = RequestSession.sessionById[sessionId];
113113
if (requestSession?.isClosing) return;
114114

115115
if (!requestSession) {
116-
log.warn('MitmProxy.RequestWithoutSessionId', {
116+
log.warn('MitmProxy.RequestWithoutSession', {
117117
sessionId,
118118
isSSL,
119119
host: clientToProxyRequest.headers.host ?? clientToProxyRequest.headers[':authority'],
@@ -150,11 +150,11 @@ export default class MitmProxy {
150150
if (!sessionId) {
151151
return RequestSession.sendNeedsAuth(socket);
152152
}
153-
const requestSession = RequestSession.sessions[sessionId];
153+
const requestSession = RequestSession.sessionById[sessionId];
154154
if (requestSession?.isClosing) return;
155155

156156
if (!requestSession) {
157-
log.warn('MitmProxy.UpgradeRequestWithoutSessionId', {
157+
log.warn('MitmProxy.UpgradeRequestWithoutSession', {
158158
sessionId,
159159
isSSL,
160160
host: clientToProxyRequest.headers.host,

puppet-chrome/lib/Page.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,8 +374,8 @@ export class Page extends TypedEventEmitter<IPuppetPageEvents> implements IPuppe
374374
deviceScaleFactor: viewport.deviceScaleFactor ?? 1,
375375
positionX: viewport.positionX,
376376
positionY: viewport.positionY,
377-
screenHeight: viewport.screenHeight,
378377
screenWidth: viewport.screenWidth,
378+
screenHeight: viewport.screenHeight,
379379
mobile: false,
380380
});
381381
}

0 commit comments

Comments
 (0)