@snyk/cli-interface
Version:
Snyk CLI interface definitions
71 lines • 3.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.cleanSnykEnv = exports.restoreSystemEnv = void 0;
const SNYK_VAR_PATTERN = /^SNYK_/i;
// This list must match the variables the Go CLI saves as SNYK_SYSTEM_* in snyk/cli
// cliv2/internal/cliv2/cliv2.go ("preserve original proxy settings"). Add a variable there, add it here too.
// Each SNYK_SYSTEM_* variable holds the user's own value of the variable it restores.
// The flag marks targets that also get an all-lowercase copy. OPENSSL_CONF has no lowercase convention.
const SYSTEM_RESTORES = [
['SNYK_SYSTEM_HTTP_PROXY', 'HTTP_PROXY', true],
['SNYK_SYSTEM_HTTPS_PROXY', 'HTTPS_PROXY', true],
['SNYK_SYSTEM_NO_PROXY', 'NO_PROXY', true],
['SNYK_SYSTEM_OPENSSL_CONF', 'OPENSSL_CONF', false],
];
/**
* Returns a copy of `env` with the user's own proxy and OpenSSL settings restored.
* SNYK_* variables are kept, so use this for Snyk's own child processes.
*
* The CLI replaces HTTP_PROXY, HTTPS_PROXY, NO_PROXY and OPENSSL_CONF, and saves the
* user's values as SNYK_SYSTEM_<name>. Each saved value is read from `systemEnv`.
* For HTTP_PROXY, HTTPS_PROXY and NO_PROXY, every casing of the name is removed and the
* value is written under both the uppercase and the all-lowercase name. An empty value
* leaves both deleted. OPENSSL_CONF is written under its uppercase name only.
* A missing saved value changes nothing.
*
* `systemEnv` is where the SNYK_SYSTEM_* values are read from, normally `process.env`.
*/
function restoreSystemEnv(env, systemEnv) {
const result = Object.assign({}, env);
for (const [systemKey, targetKey, withLowercase] of SYSTEM_RESTORES) {
const value = systemEnv[systemKey];
if (value === undefined) {
continue;
}
// Env names are case-insensitive on Windows, so drop every casing of the target first.
for (const key of Object.keys(result)) {
if (key.toUpperCase() === targetKey) {
delete result[key];
}
}
if (value !== '') {
result[targetKey] = value;
// curl, and so git, reads only lowercase http_proxy for http:// URLs, and some tools
// read only one casing of the others, so write both. Windows picks either casing;
// both hold the same value, so the pick does not matter.
if (withLowercase) {
result[targetKey.toLowerCase()] = value;
}
}
}
return result;
}
exports.restoreSystemEnv = restoreSystemEnv;
/**
* Returns a copy of `env` that is safe for third-party child processes such as build
* tools. It calls restoreSystemEnv to restore the user's own proxy and OpenSSL settings,
* then removes every SNYK_* variable (any casing), so no Snyk credentials reach the child.
*
* `systemEnv` is where the SNYK_SYSTEM_* values are read from, normally `process.env`.
*/
function cleanSnykEnv(env, systemEnv) {
const result = restoreSystemEnv(env, systemEnv);
for (const key of Object.keys(result)) {
if (SNYK_VAR_PATTERN.test(key)) {
delete result[key];
}
}
return result;
}
exports.cleanSnykEnv = cleanSnykEnv;
//# sourceMappingURL=snyk-env.js.map