Skip to content

Commit

Permalink
fix: copy process environment before changing it to avoid side effects (
Browse files Browse the repository at this point in the history
#107)



---------

Co-authored-by: Jason Luong <jason.luong@snyk.io>
  • Loading branch information
PeterSchafer and j-luong authored Mar 28, 2023
1 parent 30edf37 commit c851b60
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sub-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export function execute(command: string,
args: string[],
options?: { cwd?: string, env?: any },
shell: boolean = false): Promise<string> {
const spawnOptions: childProcess.SpawnOptions = { shell, env: process.env };
const spawnOptions: childProcess.SpawnOptions = { shell, env: {...process.env} };
if (options?.cwd) {
spawnOptions.cwd = options.cwd;
}
Expand Down
35 changes: 35 additions & 0 deletions test/sub-process.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { test } from 'tap';
import { execute } from '../lib/sub-process';

test('sub-process', (t) => {
let preTestEnv;
t.beforeEach(async () => {
preTestEnv = { ...process.env };
});

t.afterEach(async () => {
process.env = { ...preTestEnv };
});

t.test('restore proxy environment', async (t) => {
process.env.SNYK_SYSTEM_HTTPS_PROXY = "http://1.1.1.1";
process.env.SNYK_SYSTEM_HTTP_PROXY = "http://2.2.2.2";
process.env.SNYK_SYSTEM_NO_PROXY = "snyk.com";

process.env.HTTPS_PROXY = "http://127.0.0.1";
process.env.HTTP_PROXY = "http://127.0.0.1";
process.env.NO_PROXY = "example.com";

const result = await execute('env', []);

t.contains(result, 'HTTPS_PROXY=http://1.1.1.1');
t.contains(result, 'HTTP_PROXY=http://2.2.2.2');
t.contains(result, 'NO_PROXY=snyk.com');

t.equals(process.env.HTTPS_PROXY, 'http://127.0.0.1');
t.equals(process.env.HTTP_PROXY, 'http://127.0.0.1');
t.equals(process.env.NO_PROXY, 'example.com');
});

t.end();
});

0 comments on commit c851b60

Please sign in to comment.