Skip to content

Commit

Permalink
fix: bug in the CLI where envVars were not being restored correctly […
Browse files Browse the repository at this point in the history
…HEAD-218] (#257)
  • Loading branch information
j-luong committed Mar 28, 2023
1 parent efa6ec9 commit 60feee5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/sub-process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,34 @@ const debugLogging = debugModule('snyk-gradle-plugin');
export function execute(
command: string,
args: string[],
options: { cwd?: string },
options: { cwd?: string; env?: NodeJS.ProcessEnv },
perLineCallback?: (s: string) => Promise<void>,
): Promise<string> {
const spawnOptions: childProcess.SpawnOptions = { shell: true };
if (options && options.cwd) {
const spawnOptions: childProcess.SpawnOptions = {
shell: true,
env: { ...process.env },
};
if (options?.cwd) {
spawnOptions.cwd = options.cwd;
}
if (options?.env) {
spawnOptions.env = { ...process.env, ...options.env };
}

args = quoteAll(args, spawnOptions);

// Before spawning an external process, we look if we need to restore the system proxy configuration,
// which overides the cli internal proxy configuration.
if (process.env.SNYK_SYSTEM_HTTP_PROXY !== undefined) {
spawnOptions.env.HTTP_PROXY = process.env.SNYK_SYSTEM_HTTP_PROXY;
}
if (process.env.SNYK_SYSTEM_HTTPS_PROXY !== undefined) {
spawnOptions.env.HTTPS_PROXY = process.env.SNYK_SYSTEM_HTTPS_PROXY;
}
if (process.env.SNYK_SYSTEM_NO_PROXY !== undefined) {
spawnOptions.env.NO_PROXY = process.env.SNYK_SYSTEM_NO_PROXY;
}

return new Promise((resolve, reject) => {
let stdout = '';
let stderr = '';
Expand Down
34 changes: 34 additions & 0 deletions test/functional/sub-process.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { execute } from '../../lib/sub-process';

describe('sub-process', () => {
let preTestEnv: NodeJS.ProcessEnv;

beforeEach(() => {
preTestEnv = { ...process.env };
});

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

it('restores proxy environment', async () => {
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', [], {});

expect(result).toContain('HTTPS_PROXY=http://1.1.1.1');
expect(process.env.HTTPS_PROXY).toStrictEqual('http://127.0.0.1');

expect(result).toContain('HTTP_PROXY=http://2.2.2.2');
expect(process.env.HTTP_PROXY).toStrictEqual('http://127.0.0.1');

expect(result).toContain('NO_PROXY=snyk.com');
expect(process.env.NO_PROXY).toStrictEqual('example.com');
});
});

0 comments on commit 60feee5

Please sign in to comment.