From 635a87a4f486c6ff4d0f310e9b84fbf1eaec822a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lucas=20Ca=C3=B1ete?= <93189480+LexLuthorPrimero@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:33:46 -0300 Subject: [PATCH] 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> --- scripts/lib/package-command-owner.mts | 22 ++++++++++++++++ scripts/lib/release-package-canary.mjs | 6 ++--- scripts/verify-package-pack.mjs | 5 ++-- .../process/packageNpmInvocation.test.ts | 26 ++++++++++++------- .../process/packagePackIsolation.test.ts | 1 + .../process/releaseArtifactCanary.test.ts | 2 ++ 6 files changed, 46 insertions(+), 16 deletions(-) diff --git a/scripts/lib/package-command-owner.mts b/scripts/lib/package-command-owner.mts index db0ddeb90b..e0cad128d8 100644 --- a/scripts/lib/package-command-owner.mts +++ b/scripts/lib/package-command-owner.mts @@ -22,6 +22,11 @@ export interface PackageVerifierCommands extends Pick< arguments_: readonly string[], options?: VerifierCommandOptions, ): Promise; + installWithoutScripts( + operation: "ci" | "install", + arguments_: readonly string[], + options?: VerifierCommandOptions, + ): Promise; 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 { + // 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[], diff --git a/scripts/lib/release-package-canary.mjs b/scripts/lib/release-package-canary.mjs index c86c37064f..40af46586f 100644 --- a/scripts/lib/release-package-canary.mjs +++ b/scripts/lib/release-package-canary.mjs @@ -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", diff --git a/scripts/verify-package-pack.mjs b/scripts/verify-package-pack.mjs index 5a3d7d6e24..1b2d9c5022 100644 --- a/scripts/verify-package-pack.mjs +++ b/scripts/verify-package-pack.mjs @@ -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) { diff --git a/tests/boundary/process/packageNpmInvocation.test.ts b/tests/boundary/process/packageNpmInvocation.test.ts index 823742e859..c9b9c5146d 100644 --- a/tests/boundary/process/packageNpmInvocation.test.ts +++ b/tests/boundary/process/packageNpmInvocation.test.ts @@ -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, }); }, diff --git a/tests/boundary/process/packagePackIsolation.test.ts b/tests/boundary/process/packagePackIsolation.test.ts index 871c97e7a0..c241c0773e 100644 --- a/tests/boundary/process/packagePackIsolation.test.ts +++ b/tests/boundary/process/packagePackIsolation.test.ts @@ -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, diff --git a/tests/boundary/process/releaseArtifactCanary.test.ts b/tests/boundary/process/releaseArtifactCanary.test.ts index 89c0775715..9febdcf998 100644 --- a/tests/boundary/process/releaseArtifactCanary.test.ts +++ b/tests/boundary/process/releaseArtifactCanary.test.ts @@ -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), }, );