Skip to content

Commit 6b71e8b

Browse files
committed
Fix bug where implicit ports weren't used to match passthrough options
Specifically ignoreHostCertificateErrors and clientCertificateHostMap, which match against both hostname and hostname:port keys. In practice, this meant that matching against example.com:443 did not match HTTPS to that hostname, for example, because the 443 is implicit in the URL.
1 parent cb4f91c commit 6b71e8b

File tree

3 files changed

+17
-12
lines changed

3 files changed

+17
-12
lines changed

src/rules/passthrough-handling.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ export function getContentLengthAfterModification(
257257
// based on the given config
258258
export function shouldUseStrictHttps(
259259
hostname: string,
260-
port: string,
260+
port: number,
261261
ignoreHostHttpsErrors: string[] | boolean
262262
) {
263263
let skipHttpsErrors = false;

src/rules/requests/request-handlers.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,18 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
639639
rawHeaders = objectHeadersToRaw(headers);
640640
}
641641

642-
const strictHttpsChecks = shouldUseStrictHttps(hostname!, port!, this. ignoreHostHttpsErrors);
642+
const effectivePort = !!port
643+
? parseInt(port, 10)
644+
: (protocol === 'https:' ? 443 : 80);
645+
646+
const strictHttpsChecks = shouldUseStrictHttps(
647+
hostname!,
648+
effectivePort!,
649+
this.ignoreHostHttpsErrors
650+
);
643651

644652
// Use a client cert if it's listed for the host+port or whole hostname
645-
const hostWithPort = `${hostname}:${port}`;
653+
const hostWithPort = `${hostname}:${effectivePort}`;
646654
const clientCert = this.clientCertificateHostMap[hostWithPort] ||
647655
this.clientCertificateHostMap[hostname!] ||
648656
{};
@@ -656,10 +664,6 @@ export class PassThroughHandler extends PassThroughHandlerDefinition {
656664
// and we can't use ALPN to detect HTTP/2 support cleanly.
657665
let shouldTryH2Upstream = isH2Downstream && protocol === 'https:';
658666

659-
const effectivePort = !!port
660-
? parseInt(port, 10)
661-
: (protocol === 'https:' ? 443 : 80);
662-
663667
let family: undefined | 4 | 6;
664668
if (hostname === 'localhost') {
665669
// Annoying special case: some localhost servers listen only on either ipv4 or ipv6.

src/rules/websockets/websocket-handlers.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,14 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
292292
head: Buffer
293293
) {
294294
const parsedUrl = url.parse(wsUrl);
295+
296+
const effectivePort = !!parsedUrl.port
297+
? parseInt(parsedUrl.port, 10)
298+
: parsedUrl.protocol == 'wss:' ? 443 : 80;
299+
295300
const checkServerCertificate = shouldUseStrictHttps(
296301
parsedUrl.hostname!,
297-
parsedUrl.port!,
302+
effectivePort,
298303
this.ignoreHostHttpsErrors
299304
);
300305

@@ -303,10 +308,6 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
303308
? { ca: trustedCerts }
304309
: {};
305310

306-
const effectivePort = !!parsedUrl.port
307-
? parseInt(parsedUrl.port, 10)
308-
: parsedUrl.protocol == 'wss:' ? 443 : 80;
309-
310311
const proxySettingSource = assertParamDereferenced(this.proxyConfig) as ProxySettingSource;
311312

312313
const agent = await getAgent({

0 commit comments

Comments
 (0)