From f75f1ed359ad9c5b86a8f3aa6bd254e315e33266 Mon Sep 17 00:00:00 2001 From: PeterSchafer <101886095+PeterSchafer@users.noreply.github.com> Date: Mon, 25 Sep 2023 09:45:29 +0200 Subject: [PATCH] fix: changing global proxy setting (#223) --- lib/dependencies/sub-process.ts | 2 +- test/unit/sub-process.spec.ts | 92 +++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 test/unit/sub-process.spec.ts diff --git a/lib/dependencies/sub-process.ts b/lib/dependencies/sub-process.ts index 20078ef6..7e1ed785 100644 --- a/lib/dependencies/sub-process.ts +++ b/lib/dependencies/sub-process.ts @@ -15,7 +15,7 @@ function makeSpawnOptions(options?: ProcessOptions) { spawnOptions.cwd = options.cwd; } if (options && options.env) { - spawnOptions.env = options.env; + spawnOptions.env = { ...options.env }; } // Before spawning an external process, we look if we need to restore the system proxy configuration, diff --git a/test/unit/sub-process.spec.ts b/test/unit/sub-process.spec.ts new file mode 100644 index 00000000..50f6d09b --- /dev/null +++ b/test/unit/sub-process.spec.ts @@ -0,0 +1,92 @@ +import { executeSync } from '../../lib/dependencies/sub-process'; + +describe('Test sub-process.ts', () => { + it('test restoring proxy setting in executeSync()', async () => { + const expectedProxy = 'proxy'; + const expectedProxyHTTPS = 'proxy2'; + const expectedNoProxy = 'no-proxy'; + + process.env.SNYK_SYSTEM_HTTP_PROXY = 'http://127.0.0.1:3128'; + process.env.SNYK_SYSTEM_HTTPS_PROXY = 'http://127.0.0.1:3129'; + process.env.SNYK_SYSTEM_NO_PROXY = 'something.com'; + + const options = { + env: { + HTTP_PROXY: expectedProxy, + HTTPS_PROXY: expectedProxyHTTPS, + NO_PROXY: expectedNoProxy, + }, + }; + + let output = executeSync( + 'python3', + ['-c', "import os; print(os.environ['HTTP_PROXY'])"], + options + ); + expect(output.stdout.toString().trim()).toEqual( + process.env.SNYK_SYSTEM_HTTP_PROXY + ); + + output = executeSync( + 'python3', + ['-c', "import os; print(os.environ['HTTPS_PROXY'])"], + options + ); + expect(output.stdout.toString().trim()).toEqual( + process.env.SNYK_SYSTEM_HTTPS_PROXY + ); + + output = executeSync( + 'python3', + ['-c', "import os; print(os.environ['NO_PROXY'])"], + options + ); + expect(output.stdout.toString().trim()).toEqual( + process.env.SNYK_SYSTEM_NO_PROXY + ); + + // ensure that options remain unchanged + expect(options.env.HTTP_PROXY).toEqual(expectedProxy); + expect(options.env.HTTPS_PROXY).toEqual(expectedProxyHTTPS); + expect(options.env.NO_PROXY).toEqual(expectedNoProxy); + + delete process.env.SNYK_SYSTEM_HTTP_PROXY; + delete process.env.SNYK_SYSTEM_HTTPS_PROXY; + delete process.env.SNYK_SYSTEM_NO_PROXY; + }); + + it('test executeSync()', async () => { + const expectedProxy = 'proxy'; + const expectedProxyHTTPS = 'proxy2'; + const expectedNoProxy = 'no-proxy'; + + const options = { + env: { + HTTP_PROXY: expectedProxy, + HTTPS_PROXY: expectedProxyHTTPS, + NO_PROXY: expectedNoProxy, + }, + }; + + let output = executeSync( + 'python3', + ['-c', "import os; print(os.environ['HTTP_PROXY'])"], + options + ); + expect(output.stdout.toString().trim()).toEqual(expectedProxy); + + output = executeSync( + 'python3', + ['-c', "import os; print(os.environ['HTTPS_PROXY'])"], + options + ); + expect(output.stdout.toString().trim()).toEqual(expectedProxyHTTPS); + + output = executeSync( + 'python3', + ['-c', "import os; print(os.environ['NO_PROXY'])"], + options + ); + expect(output.stdout.toString().trim()).toEqual(expectedNoProxy); + }); +});