diff --git a/verifycache/script_cache.go b/verifycache/script_cache.go index 2389303..cc91e93 100644 --- a/verifycache/script_cache.go +++ b/verifycache/script_cache.go @@ -17,9 +17,9 @@ const scriptCacheBucket = "scripts" var ErrScriptCacheMiss = errors.New("delegated script cache entry missing") // SignVersionScripts records TPM-signed hashes for top-level .cmd/.bat launchers, -// package-manager JS entrypoints (npm-cli.js / npx-cli.js), Authenticode -// verify-cache entries for top-level .exe helpers, and Yarn Classic / pnpm -// global bin launchers outside the version directory. +// package-manager JS entrypoints (npm-cli.js / npx-cli.js / pnpm.mjs / yarn.js), +// Authenticode verify-cache entries for top-level .exe helpers, and Yarn Classic +// / pnpm global bin launchers outside the version directory. func SignVersionScripts(versionDir string) error { dataRoot, err := dataRootFromVersionDir(versionDir) if err != nil { @@ -28,10 +28,10 @@ func SignVersionScripts(versionDir string) error { return signVersionScripts(dataRoot, versionDir) } -// SignScript force-resigns one delegated .cmd/.bat/.js launcher, bypassing the -// TrustedModules gate. In-process callers (nvm firewall trust module) use this -// directly. The --sign-script CLI flag is gated separately: signed proxy parent -// plus an inherited pipe. Do not treat argv as a trust source. +// SignScript force-resigns one delegated .cmd/.bat/.js/.cjs/.mjs launcher, +// bypassing the TrustedModules gate. In-process callers (nvm firewall trust +// module) use this directly. The --sign-script CLI flag is gated separately: +// signed proxy parent plus an inherited pipe. Do not treat argv as a trust source. func SignScript(scriptPath string) error { scriptPath = strings.TrimSpace(scriptPath) if scriptPath == "" { @@ -107,6 +107,41 @@ func packageManagerCliRelPaths() []string { "node_modules/npm/bin/npm-cli.js", "node_modules/npm/bin/npx-cli.js", "node_modules/corepack/dist/corepack.js", + // Prefer order matches proxy resolvePackageModuleEntry (mjs → cjs → js). + "node_modules/pnpm/bin/pnpm.mjs", + "node_modules/pnpm/bin/pnpm.cjs", + "node_modules/pnpm/bin/pnpm.js", + "node_modules/yarn/bin/yarn.js", + "node_modules/yarn/bin/yarn.cjs", + "node_modules/yarn/bin/yarn.mjs", + } +} + +// PackageManagerCliRelPathsFor returns the install-relative JS/CJS/MJS +// entrypoints that proxy may launch for the given package-manager stem +// (e.g. "pnpm", "yarn", "npm"). Empty when the stem has no nested CLI path. +func PackageManagerCliRelPathsFor(name string) []string { + switch strings.ToLower(strings.TrimSpace(name)) { + case "npm": + return []string{"node_modules/npm/bin/npm-cli.js"} + case "npx": + return []string{"node_modules/npm/bin/npx-cli.js"} + case "corepack": + return []string{"node_modules/corepack/dist/corepack.js"} + case "pnpm": + return []string{ + "node_modules/pnpm/bin/pnpm.mjs", + "node_modules/pnpm/bin/pnpm.cjs", + "node_modules/pnpm/bin/pnpm.js", + } + case "yarn", "yarnpkg": + return []string{ + "node_modules/yarn/bin/yarn.js", + "node_modules/yarn/bin/yarn.cjs", + "node_modules/yarn/bin/yarn.mjs", + } + default: + return nil } } diff --git a/verifycache/script_cache_test.go b/verifycache/script_cache_test.go index eedd8e8..efd9183 100644 --- a/verifycache/script_cache_test.go +++ b/verifycache/script_cache_test.go @@ -22,12 +22,24 @@ func TestSignVersionScriptsSignsPackageManagerCliEntrypoints(t *testing.T) { t.Fatalf("WriteFile(npm-cli.js): %v", err) } + pnpmDir := filepath.Join(versionDir, "node_modules", "pnpm", "bin") + if err := os.MkdirAll(pnpmDir, 0o755); err != nil { + t.Fatalf("MkdirAll(pnpmDir): %v", err) + } + pnpmPath := filepath.Join(pnpmDir, "pnpm.mjs") + if err := os.WriteFile(pnpmPath, []byte("export {}\n"), 0o644); err != nil { + t.Fatalf("WriteFile(pnpm.mjs): %v", err) + } + if err := signVersionScripts(dataRoot, versionDir); err != nil { t.Fatalf("signVersionScripts(): %v", err) } if err := verifyDelegatedScript(dataRoot, cliPath); err != nil { t.Fatalf("verifyDelegatedScript(npm-cli.js): %v", err) } + if err := verifyDelegatedScript(dataRoot, pnpmPath); err != nil { + t.Fatalf("verifyDelegatedScript(pnpm.mjs): %v", err) + } } func TestVerifyDelegatedScriptRejectsPlantedScript(t *testing.T) { diff --git a/verifycache/sign_script_auth.go b/verifycache/sign_script_auth.go index 626b030..efd2606 100644 --- a/verifycache/sign_script_auth.go +++ b/verifycache/sign_script_auth.go @@ -91,16 +91,16 @@ func ReadSignScriptPath(r io.Reader) (string, error) { return scriptPath, nil } -// IsDelegatedLauncherPath is true for .cmd/.bat/.js files NVM proxy would -// launch: under the installs root, Yarn Classic global bin, pnpm home bin, -// or the roaming npm prefix. +// IsDelegatedLauncherPath is true for .cmd/.bat/.js/.cjs/.mjs files NVM proxy +// would launch: under the installs root, Yarn Classic global bin, pnpm home +// bin, or the roaming npm prefix. func IsDelegatedLauncherPath(scriptPath string) bool { scriptPath = filepath.Clean(strings.TrimSpace(scriptPath)) if scriptPath == "" || scriptPath == "." { return false } ext := strings.ToLower(filepath.Ext(scriptPath)) - if ext != ".cmd" && ext != ".bat" && ext != ".js" { + if ext != ".cmd" && ext != ".bat" && ext != ".js" && ext != ".cjs" && ext != ".mjs" { return false } installRoot := filepath.Clean(strings.TrimSpace(settings.Global().Root)) diff --git a/verifycache/sign_script_auth_test.go b/verifycache/sign_script_auth_test.go index 8d254dc..534a959 100644 --- a/verifycache/sign_script_auth_test.go +++ b/verifycache/sign_script_auth_test.go @@ -137,12 +137,20 @@ func TestIsDelegatedLauncherPath(t *testing.T) { versionDir := filepath.Join(dataRoot, "installs", "v24.0.0") ok := filepath.Join(versionDir, "vue.cmd") nested := filepath.Join(versionDir, "node_modules", "npm", "bin", "npm-cli.js") + pnpmMjs := filepath.Join(versionDir, "node_modules", "pnpm", "bin", "pnpm.mjs") + yarnCjs := filepath.Join(versionDir, "node_modules", "yarn", "bin", "yarn.cjs") if !IsDelegatedLauncherPath(ok) { t.Fatal("version-dir cmd must pass") } if !IsDelegatedLauncherPath(nested) { t.Fatal("nested js under installs must pass") } + if !IsDelegatedLauncherPath(pnpmMjs) { + t.Fatal("nested pnpm.mjs under installs must pass") + } + if !IsDelegatedLauncherPath(yarnCjs) { + t.Fatal("nested yarn.cjs under installs must pass") + } if IsDelegatedLauncherPath(`C:\Temp\evil.cmd`) { t.Fatal("temp cmd must fail") }