Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: configurable rejectUnauthorized #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,15 @@ Use [`roarr-cli`](https://github.com/gajus/roarr-cli) program to pretty-print th
* @property environmentVariableNamespace Defines namespace of `HTTP_PROXY`, `HTTPS_PROXY` and `NO_PROXY` environment variables. (Default: `GLOBAL_AGENT_`)
* @property forceGlobalAgent Forces to use `global-agent` HTTP(S) agent even when request was explicitly constructed with another agent. (Default: `true`)
* @property socketConnectionTimeout Destroys socket if connection is not established within the timeout. (Default: `60000`)
* @property rejectUnauthorized `false` - all invalid SSL certificates are ignored and no error is thrown.
* `true` - an error is thrown when an invalid SSL certificate is detected.
* (Default: `undefined`)
*/
type ProxyAgentConfigurationInputType = {|
+environmentVariableNamespace?: string,
+forceGlobalAgent?: boolean,
+socketConnectionTimeout?: number,
+rejectUnauthorized?: boolean,
|};

(configurationInput: ProxyAgentConfigurationInputType) => ProxyAgentConfigurationType;
Expand Down
18 changes: 5 additions & 13 deletions src/classes/Agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
import {
serializeError,
} from 'serialize-error';
import {
boolean,
} from 'boolean';
import Logger from '../Logger';
import type {
AgentType,
Expand Down Expand Up @@ -34,6 +31,8 @@ class Agent {

getUrlProxy: GetUrlProxyMethodType;

rejectUnauthorized: boolean;

socketConnectionTimeout: number;

constructor (
Expand All @@ -42,12 +41,14 @@ class Agent {
getUrlProxy: GetUrlProxyMethodType,
fallbackAgent: AgentType,
socketConnectionTimeout: number,
rejectUnauthorized: boolean,
) {
this.fallbackAgent = fallbackAgent;
this.isProxyConfigured = isProxyConfigured;
this.mustUrlUseProxy = mustUrlUseProxy;
this.getUrlProxy = getUrlProxy;
this.socketConnectionTimeout = socketConnectionTimeout;
this.rejectUnauthorized = rejectUnauthorized;
}

addRequest (request: *, configuration: *) {
Expand Down Expand Up @@ -146,21 +147,12 @@ class Agent {
key: configuration.key,
passphrase: configuration.passphrase,
pfx: configuration.pfx,
rejectUnauthorized: configuration.rejectUnauthorized,
rejectUnauthorized: configuration.rejectUnauthorized === undefined ? this.rejectUnauthorized : configuration.rejectUnauthorized,
secureOptions: configuration.secureOptions,
secureProtocol: configuration.secureProtocol,
servername: configuration.servername || connectionConfiguration.host,
sessionIdContext: configuration.sessionIdContext,
};

// This is not ideal because there is no way to override this setting using `tls` configuration if `NODE_TLS_REJECT_UNAUTHORIZED=0`.
// However, popular HTTP clients (such as https://github.com/sindresorhus/got) come with pre-configured value for `rejectUnauthorized`,
// which makes it impossible to override that value globally and respect `rejectUnauthorized` for specific requests only.
//
// eslint-disable-next-line no-process-env
if (typeof process.env.NODE_TLS_REJECT_UNAUTHORIZED === 'string' && boolean(process.env.NODE_TLS_REJECT_UNAUTHORIZED) === false) {
connectionConfiguration.tls.rejectUnauthorized = false;
}
}

// $FlowFixMe It appears that Flow is missing the method description.
Expand Down
3 changes: 3 additions & 0 deletions src/factories/createGlobalProxyAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const createConfiguration = (configurationInput: ProxyAgentConfigurationInputTyp
const defaultConfiguration = {
environmentVariableNamespace: typeof environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE === 'string' ? environment.GLOBAL_AGENT_ENVIRONMENT_VARIABLE_NAMESPACE : 'GLOBAL_AGENT_',
forceGlobalAgent: typeof environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT === 'string' ? parseBoolean(environment.GLOBAL_AGENT_FORCE_GLOBAL_AGENT) : true,
rejectUnauthorized: typeof environment.NODE_TLS_REJECT_UNAUTHORIZED === 'string' ? parseBoolean(environment.NODE_TLS_REJECT_UNAUTHORIZED) : undefined,
socketConnectionTimeout: typeof environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT === 'string' ? Number.parseInt(environment.GLOBAL_AGENT_SOCKET_CONNECTION_TIMEOUT, 10) : defaultConfigurationInput.socketConnectionTimeout,
};

Expand Down Expand Up @@ -132,6 +133,7 @@ export default (configurationInput: ProxyAgentConfigurationInputType = defaultCo
getUrlProxy(getHttpProxy),
http.globalAgent,
configuration.socketConnectionTimeout,
configuration.rejectUnauthorized,
);
}
};
Expand All @@ -152,6 +154,7 @@ export default (configurationInput: ProxyAgentConfigurationInputType = defaultCo
getUrlProxy(getHttpsProxy),
https.globalAgent,
configuration.socketConnectionTimeout,
configuration.rejectUnauthorized,
);
}
};
Expand Down
2 changes: 2 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,12 @@ export type ProxyAgentConfigurationInputType = {|
+environmentVariableNamespace?: string,
+forceGlobalAgent?: boolean,
+socketConnectionTimeout?: number,
+rejectUnauthorized?: boolean,
|};

export type ProxyAgentConfigurationType = {|
+environmentVariableNamespace: string,
+forceGlobalAgent: boolean,
+socketConnectionTimeout: number,
+rejectUnauthorized?: boolean,
|};