Skip to content

Commit 1f594e5

Browse files
Explicitly clear Node.js runtime environment variables when spawning child processes
1 parent 41b9cc3 commit 1f594e5

File tree

4 files changed

+34
-17
lines changed

4 files changed

+34
-17
lines changed

src/npm-config.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { promises as fs } from "fs";
44
import { EOL } from "os";
55
import { dirname } from "path";
66
import { NormalizedOptions } from "./normalize-options";
7+
import { getNpmEnvironment } from "./npm-env";
78

89
/**
910
* Sets/updates the NPM config based on the options.
@@ -49,11 +50,14 @@ function updateConfig(config: string, { registry, debug }: NormalizedOptions): s
4950
/**
5051
* Gets the path of the NPM config file.
5152
*/
52-
async function getNpmConfigPath({ debug }: NormalizedOptions): Promise<string> {
53+
async function getNpmConfigPath(options: NormalizedOptions): Promise<string> {
5354
try {
54-
debug("Running command: npm config get userconfig");
55+
// Get the environment variables to pass to NPM
56+
let env = getNpmEnvironment(options);
5557

56-
let process = await ezSpawn.async("npm", "config", "get", "userconfig");
58+
options.debug("Running command: npm config get userconfig");
59+
60+
let process = await ezSpawn.async("npm", "config", "get", "userconfig", { env });
5761
return process.stdout.trim();
5862
}
5963
catch (error) {

src/npm-env.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NormalizedOptions } from "./normalize-options";
2+
3+
/**
4+
* Returns the environment variables that should be passed to NPM, based on the given options.
5+
*/
6+
export function getNpmEnvironment(options: NormalizedOptions): NodeJS.ProcessEnv {
7+
/* eslint-disable @typescript-eslint/naming-convention */
8+
let env: NodeJS.ProcessEnv = {
9+
// Copy all the host's environment variables
10+
...process.env,
11+
12+
// Don't pass Node.js runtime variables to NPM
13+
NODE_ENV: "",
14+
NODE_OPTIONS: "",
15+
};
16+
17+
// Determine if we need to set the NPM token
18+
let needsToken = Boolean(options.token && process.env.INPUT_TOKEN !== options.token);
19+
20+
if (needsToken) {
21+
env.INPUT_TOKEN = options.token;
22+
}
23+
24+
return env;
25+
}

src/npm.ts

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dirname, resolve } from "path";
55
import { SemVer } from "semver";
66
import { NormalizedOptions } from "./normalize-options";
77
import { setNpmConfig } from "./npm-config";
8+
import { getNpmEnvironment } from "./npm-env";
89
import { Manifest } from "./read-manifest";
910

1011
/**
@@ -70,17 +71,3 @@ export const npm = {
7071
}
7172
},
7273
};
73-
74-
75-
/**
76-
* Returns the environment variables that should be passed to NPM, based on the given options.
77-
*/
78-
function getNpmEnvironment(options: NormalizedOptions): NodeJS.ProcessEnv | undefined {
79-
// Determine if we need to set the NPM token
80-
let needsToken = Boolean(options.token && process.env.INPUT_TOKEN !== options.token);
81-
82-
if (needsToken) {
83-
// eslint-disable-next-line @typescript-eslint/naming-convention
84-
return { ...process.env, INPUT_TOKEN: options.token };
85-
}
86-
}

test/utils/exec.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ module.exports = {
1717
...options,
1818
env: {
1919
...process.env,
20+
NODE_OPTIONS: "",
2021
INPUT_REGISTRY: "https://registry.npmjs.org/",
2122
INPUT_PACKAGE: "package.json",
2223
"INPUT_CHECK-VERSION": "true",

0 commit comments

Comments
 (0)