Skip to content

Commit 1c70ad7

Browse files
authored
Merge pull request #2580 from Latermedia/main
fix: do not overwrite process.env during API authentication
2 parents 8deed5b + bd14efc commit 1c70ad7

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

src/exec_auth.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export class ExecAuth implements Authenticator {
9595
}
9696
let opts = {};
9797
if (exec.env) {
98-
const env = process.env;
98+
const env = { ...process.env };
9999
exec.env.forEach((elt) => (env[elt.name] = elt.value));
100100
opts = { ...opts, env };
101101
}

src/exec_auth_test.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,4 +538,63 @@ describe('ExecAuth', () => {
538538
const promise = auth.applyAuthentication(user, opts);
539539
await rejects(promise, { name: 'SyntaxError' });
540540
});
541+
542+
it('should not overwrite environment variables in process.env', async () => {
543+
// TODO: fix this test for Windows
544+
if (process.platform === 'win32') {
545+
return;
546+
}
547+
const auth = new ExecAuth();
548+
let optsOut: child_process.SpawnOptions | undefined = {};
549+
(auth as any).execFn = (
550+
command: string,
551+
args?: readonly string[],
552+
options?: child_process.SpawnOptionsWithoutStdio,
553+
): child_process.ChildProcessWithoutNullStreams => {
554+
optsOut = options;
555+
return {
556+
stdout: {
557+
setEncoding: () => {},
558+
on: (_data: string, f: (data: Buffer | string) => void) => {
559+
f(Buffer.from(JSON.stringify({ status: { token: 'foo' } })));
560+
},
561+
},
562+
stderr: {
563+
setEncoding: () => {},
564+
on: () => {},
565+
},
566+
on: (op: string, f: any) => {
567+
if (op === 'close') {
568+
f(0);
569+
}
570+
},
571+
} as unknown as child_process.ChildProcessWithoutNullStreams;
572+
};
573+
574+
process.env.DO_NO_OVERWRITE_ME = 'important';
575+
const opts = {} as https.RequestOptions;
576+
opts.headers = {} as OutgoingHttpHeaders;
577+
578+
await auth.applyAuthentication(
579+
{
580+
name: 'user',
581+
authProvider: {
582+
config: {
583+
exec: {
584+
command: 'echo',
585+
env: [
586+
{
587+
name: 'DO_NO_OVERWRITE_ME',
588+
value: 'in exec',
589+
},
590+
],
591+
},
592+
},
593+
},
594+
},
595+
opts,
596+
);
597+
strictEqual(optsOut.env!.DO_NO_OVERWRITE_ME, 'in exec');
598+
strictEqual(process.env.DO_NO_OVERWRITE_ME, 'important');
599+
});
541600
});

0 commit comments

Comments
 (0)