Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 42 additions & 7 deletions verifycache/script_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 == "" {
Expand Down Expand Up @@ -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
}
}

Expand Down
12 changes: 12 additions & 0 deletions verifycache/script_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions verifycache/sign_script_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
8 changes: 8 additions & 0 deletions verifycache/sign_script_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
Loading