mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
fix(verify): scrub ambient npm install-policy from verifier child npm environments (#10474)
* fix(verify): scrub ambient npm install-policy from verifier child npm environments * fix(verify): keep caller npm policy for script-enabled commands --------- Co-authored-by: LexLuthorPrimero <1@voidmail.io> Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
6 files changed
+46
-16
No files matched your search
@@ -22,6 +22,11 @@ export interface PackageVerifierCommands extends Pick<
|
||||
arguments_: readonly string[],
|
||||
options?: VerifierCommandOptions,
|
||||
): Promise<VerifierCommandOutput>;
|
||||
installWithoutScripts(
|
||||
operation: "ci" | "install",
|
||||
arguments_: readonly string[],
|
||||
options?: VerifierCommandOptions,
|
||||
): Promise<VerifierCommandOutput>;
|
||||
run(
|
||||
command: string,
|
||||
arguments_: readonly string[],
|
||||
@@ -78,6 +83,23 @@ class PackageCommands implements PackageVerifierCommands {
|
||||
return this.exec(invocation.command, invocation.arguments, options);
|
||||
}
|
||||
|
||||
installWithoutScripts(
|
||||
operation: "ci" | "install",
|
||||
arguments_: readonly string[],
|
||||
options: VerifierCommandOptions = {},
|
||||
): Promise<VerifierCommandOutput> {
|
||||
// npm exports this setting to scripts, but rejects it in nested project installs.
|
||||
const environment = Object.fromEntries(
|
||||
Object.entries(options.env ?? process.env).filter(
|
||||
([key]) => key.toLowerCase() !== "npm_config_allow_scripts",
|
||||
),
|
||||
);
|
||||
return this.execNpm([operation, "--ignore-scripts", ...arguments_], {
|
||||
...options,
|
||||
env: environment,
|
||||
});
|
||||
}
|
||||
|
||||
async run(
|
||||
command: string,
|
||||
arguments_: readonly string[],
|
||||
|
||||
@@ -16,11 +16,9 @@ export async function installCanaryPackage(
|
||||
commands,
|
||||
) {
|
||||
const prefix = join(workspace, "installed");
|
||||
await commands.exec(
|
||||
"npm",
|
||||
await commands.installWithoutScripts(
|
||||
"install",
|
||||
[
|
||||
"install",
|
||||
"--ignore-scripts",
|
||||
"--no-save",
|
||||
"--no-package-lock",
|
||||
"--no-audit",
|
||||
|
||||
@@ -61,8 +61,9 @@ export async function verifyPackagePack({ root, workspace, commands }) {
|
||||
`${JSON.stringify(shrinkwrap, null, 2)}\n`,
|
||||
);
|
||||
// Materialize exactly the locked runtime tree without executing installers.
|
||||
await commands.execNpm(
|
||||
["ci", "--omit=dev", "--ignore-scripts", "--no-audit", "--no-fund"],
|
||||
await commands.installWithoutScripts(
|
||||
"ci",
|
||||
["--omit=dev", "--no-audit", "--no-fund"],
|
||||
{ cwd: stagingDirectory },
|
||||
);
|
||||
if (packageJson.dependencies?.["@lydell/node-pty"] !== undefined) {
|
||||
|
||||
@@ -6,20 +6,24 @@ import { expect } from "vitest";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { processTest } from "../../support/process/processFixture.js";
|
||||
|
||||
processTest(
|
||||
"runs the caller-selected npm entry through Node without rewriting its inputs",
|
||||
async ({ processes }) => {
|
||||
processTest.for([
|
||||
{
|
||||
command: "pack",
|
||||
args: ["value with spaces", "--literal=$HOME", "quote'and\"double"],
|
||||
},
|
||||
{
|
||||
command: "install",
|
||||
args: ["--no-save", "--no-audit", "local package.tgz"],
|
||||
},
|
||||
])(
|
||||
"preserves the selected npm entry, arguments, and lifecycle policy for $command",
|
||||
async ({ command, args }, { processes }) => {
|
||||
const arguments_ = [command, ...args];
|
||||
const root = await createTestTempDirectory("rea-npm-invocation-");
|
||||
const npmEntry = join(root, "selected npm entry.mjs");
|
||||
const arguments_ = [
|
||||
"pack",
|
||||
"value with spaces",
|
||||
"--literal=$HOME",
|
||||
"quote'and\"double",
|
||||
];
|
||||
await writeFile(
|
||||
npmEntry,
|
||||
`process.stdout.write(JSON.stringify({ arguments: process.argv.slice(2), cwd: process.cwd(), marker: process.env.REA_NPM_TEST_MARKER, ambient: process.env.REA_NPM_TEST_AMBIENT ?? null }));\n`,
|
||||
`process.stdout.write(JSON.stringify({ arguments: process.argv.slice(2), cwd: process.cwd(), marker: process.env.REA_NPM_TEST_MARKER, policy: process.env.npm_config_allow_scripts, ambient: process.env.REA_NPM_TEST_AMBIENT ?? null }));\n`,
|
||||
);
|
||||
const helper = pathToFileURL(
|
||||
resolve("scripts/lib/verify-package-commands.mjs"),
|
||||
@@ -41,6 +45,7 @@ processTest(
|
||||
...(process.platform === "win32" ? { SystemRoot: process.env.SystemRoot } : {}),
|
||||
npm_execpath: ${JSON.stringify(npmEntry)},
|
||||
REA_NPM_TEST_MARKER: "caller-selected",
|
||||
npm_config_allow_scripts: "caller-selected-package",
|
||||
},
|
||||
}),
|
||||
[() => commands.close()],
|
||||
@@ -58,6 +63,7 @@ processTest(
|
||||
arguments: arguments_,
|
||||
cwd: root,
|
||||
marker: "caller-selected",
|
||||
policy: "caller-selected-package",
|
||||
ambient: null,
|
||||
});
|
||||
},
|
||||
|
||||
@@ -84,6 +84,7 @@ process.stdout.write(JSON.stringify({ failure }));
|
||||
env: {
|
||||
...process.env,
|
||||
npm_config_cache: npmCache,
|
||||
npm_config_allow_scripts: "rea-test-control",
|
||||
TEMP: npmCache,
|
||||
TMP: npmCache,
|
||||
TMPDIR: npmCache,
|
||||
|
||||
@@ -87,6 +87,7 @@ processTest.skipIf(process.platform === "win32")(
|
||||
version,
|
||||
type: "module",
|
||||
files: ["scripts"],
|
||||
scripts: { preinstall: 'node -e "process.exit(42)"' },
|
||||
}),
|
||||
);
|
||||
await copyFile(
|
||||
@@ -134,6 +135,7 @@ if (process.argv[2] === "mcp") {
|
||||
],
|
||||
{
|
||||
cwd: controller,
|
||||
env: { ...process.env, npm_config_allow_scripts: "rea-test-control" },
|
||||
timeoutMs: Math.min(2_147_483_647, preparationBudget + 30_000),
|
||||
},
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user