Skip to content

Commit

Permalink
Allow setting ignoreHostHttpsErrors to true
Browse files Browse the repository at this point in the history
When `ignoreHostHttpsErrors` is set to true, HTTPS errors will be
ignored for all the hosts
  • Loading branch information
ashishmadeti committed Feb 24, 2023
1 parent 72b8484 commit d448ad8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
19 changes: 10 additions & 9 deletions src/rules/websockets/websocket-handler-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ export interface PassThroughWebSocketHandlerOptions {

/**
* A list of hostnames for which server certificate and TLS version errors
* should be ignored (none, by default).
* should be ignored (none, by default). If set to 'true', ignore HTTPS errors
* for all hosts. (WARNING: Use this at your own risk. This can open your
* application to MITM attacks)
*/
ignoreHostHttpsErrors?: string[];
ignoreHostHttpsErrors?: string[] | boolean;

/**
* An array of additional certificates, which should be trusted as certificate
Expand Down Expand Up @@ -99,7 +101,7 @@ export interface SerializedPassThroughWebSocketData {
forwarding?: ForwardingOptions;
lookupOptions?: PassThroughLookupOptions;
proxyConfig?: SerializedProxyConfig;
ignoreHostCertificateErrors?: string[]; // Doesn't match option name, backward compat
ignoreHostCertificateErrors?: string[] | boolean; // Doesn't match option name, backward compat
extraCACertificates?: Array<{ cert: string } | { certPath: string }>;
}

Expand All @@ -111,17 +113,16 @@ export class PassThroughWebSocketHandlerDefinition extends Serializable implemen
public readonly proxyConfig?: ProxyConfig;

public readonly forwarding?: ForwardingOptions;
public readonly ignoreHostHttpsErrors: string[] = [];
public readonly ignoreHostHttpsErrors: string[] | boolean = [];

public readonly extraCACertificates: Array<{ cert: string | Buffer } | { certPath: string }> = [];

constructor(options: PassThroughWebSocketHandlerOptions = {}) {
super();

this.ignoreHostHttpsErrors = options.ignoreHostHttpsErrors ||
[];
if (!Array.isArray(this.ignoreHostHttpsErrors)) {
throw new Error("ignoreHostHttpsErrors must be an array");
this.ignoreHostHttpsErrors = options.ignoreHostHttpsErrors || [];
if (!Array.isArray(this.ignoreHostHttpsErrors) && typeof this.ignoreHostHttpsErrors !== 'boolean') {
throw new Error("ignoreHostHttpsErrors must be an array or a boolean");
}

// If a location is provided, and it's not a bare hostname, it must be parseable
Expand Down Expand Up @@ -227,4 +228,4 @@ export const WsHandlerDefinitionLookup = {
'close-connection': CloseConnectionHandlerDefinition,
'reset-connection': ResetConnectionHandlerDefinition,
'timeout': TimeoutHandlerDefinition
};
};
15 changes: 10 additions & 5 deletions src/rules/websockets/websocket-handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ import { MaybePromise } from '../../util/type-utils';
import { getAgent } from '../http-agents';
import { ProxySettingSource } from '../proxy-config';
import { assertParamDereferenced, RuleParameters } from '../rule-parameters';
import { UPSTREAM_TLS_OPTIONS } from '../passthrough-handling';
import {
UPSTREAM_TLS_OPTIONS,
shouldUseStrictHttps
} from '../passthrough-handling';

import {
EchoWebSocketHandlerDefinition,
Expand Down Expand Up @@ -311,10 +314,12 @@ export class PassThroughWebSocketHandler extends PassThroughWebSocketHandlerDefi
incomingSocket: net.Socket,
head: Buffer
) {
// Skip cert checks if the host or host+port are whitelisted
const parsedUrl = url.parse(wsUrl);
const checkServerCertificate = !_.includes(this.ignoreHostHttpsErrors, parsedUrl.hostname) &&
!_.includes(this.ignoreHostHttpsErrors, parsedUrl.host);
const checkServerCertificate = shouldUseStrictHttps(
parsedUrl.hostname as string,
parsedUrl.port as string,
this.ignoreHostHttpsErrors
);

const trustedCerts = await this.trustedCACertificates();
const caConfig = trustedCerts
Expand Down Expand Up @@ -471,4 +476,4 @@ export const WsHandlerLookup: typeof WsHandlerDefinitionLookup = {
'close-connection': CloseConnectionHandler,
'reset-connection': ResetConnectionHandler,
'timeout': TimeoutHandler
};
};

0 comments on commit d448ad8

Please sign in to comment.