From 5fff722fba5abc9d70da9132f5efb0f9ad3d4622 Mon Sep 17 00:00:00 2001 From: Dmitry Kochin Date: Tue, 20 Aug 2024 21:13:52 +0300 Subject: [PATCH] Fix invalid url escaping - replaced url parsing from url.parse to new URL --- src/rules/requests/request-handlers.ts | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/rules/requests/request-handlers.ts b/src/rules/requests/request-handlers.ts index 6418cd84c..5c4262d90 100644 --- a/src/rules/requests/request-handlers.ts +++ b/src/rules/requests/request-handlers.ts @@ -1,5 +1,4 @@ import _ = require('lodash'); -import url = require('url'); import type dns = require('dns'); import net = require('net'); import tls = require('tls'); @@ -405,7 +404,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { // Capture raw request data: let { method, url: reqUrl, rawHeaders } = clientReq as OngoingRequest; - let { protocol, hostname, port, path } = url.parse(reqUrl); + let { protocol, hostname, port, pathname, search } = new URL(reqUrl); // Check if this request is a request loop: if (isSocketLoop(this.outgoingSockets, ( clientReq).socket)) { @@ -430,7 +429,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { hostname, clientReq.remoteIpAddress, getDnsLookupFunction(this.lookupOptions) - ); + ) || ""; if (this.forwarding) { const { targetHost, updateHostHeader } = this.forwarding; @@ -439,7 +438,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { [hostname, port] = targetHost.split(':'); } else { // We're forwarding to a fully specified URL; override the host etc, but never the path. - ({ protocol, hostname, port } = url.parse(targetHost)); + ({ protocol, hostname, port } = new URL(targetHost)); } const hostHeaderName = isH2Downstream ? ':authority' : 'host'; @@ -465,7 +464,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { hostHeader[1] = updateHostHeader; } // Otherwise: falsey means don't touch it. - reqUrl = new URL(`${protocol}//${hostname}${(port ? `:${port}` : '')}${path}`).toString(); + reqUrl = new URL(`${protocol}//${hostname}${(port ? `:${port}` : '')}${pathname}${search}`).toString(); } // Override the request details, if a transform or callback is specified: @@ -645,7 +644,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { // Reparse the new URL, if necessary if (modifiedReq?.url) { if (!isAbsoluteUrl(modifiedReq?.url)) throw new Error("Overridden request URLs must be absolute"); - ({ protocol, hostname, port, path } = url.parse(reqUrl)); + ({ protocol, hostname, port, pathname, search } = new URL(reqUrl)); } rawHeaders = objectHeadersToRaw(headers); @@ -748,7 +747,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { hostname, port, family, - path, + path: pathname + search, headers: shouldTryH2Upstream ? rawHeadersToObjectPreservingCase(rawHeaders) : flattenPairedRawHeaders(rawHeaders) as any, @@ -1110,7 +1109,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition { protocol: protocol!.replace(/:$/, ''), hostname, port, - path, + path: pathname + search, rawHeaders });