Skip to content

Commit

Permalink
Fix invalid url escaping - replaced url parsing from url.parse to new…
Browse files Browse the repository at this point in the history
… URL
  • Loading branch information
dukei committed Aug 20, 2024
1 parent 4bbab59 commit 5fff722
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions src/rules/requests/request-handlers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import _ = require('lodash');
import url = require('url');
import type dns = require('dns');
import net = require('net');
import tls = require('tls');
Expand Down Expand Up @@ -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, (<any> clientReq).socket)) {
Expand All @@ -430,7 +429,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
hostname,
clientReq.remoteIpAddress,
getDnsLookupFunction(this.lookupOptions)
);
) || "";

if (this.forwarding) {
const { targetHost, updateHostHeader } = this.forwarding;
Expand All @@ -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';
Expand All @@ -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:
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -748,7 +747,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
hostname,
port,
family,
path,
path: pathname + search,
headers: shouldTryH2Upstream
? rawHeadersToObjectPreservingCase(rawHeaders)
: flattenPairedRawHeaders(rawHeaders) as any,
Expand Down Expand Up @@ -1110,7 +1109,7 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
protocol: protocol!.replace(/:$/, ''),
hostname,
port,
path,
path: pathname + search,
rawHeaders
});

Expand Down

0 comments on commit 5fff722

Please sign in to comment.