diff --git a/fs/install_root.go b/fs/install_root.go index acc2977..5e3e0d7 100644 --- a/fs/install_root.go +++ b/fs/install_root.go @@ -13,6 +13,8 @@ func InstallRootDeniedMessage() string { "Disallowed locations include:\n" + "- Windows, System32, WinSxS, Program Files, Program Files (x86), ProgramData, WindowsApps, Recycle Bin\n" + "- Drive roots (C:\\)\n" + + "- Folders directly under a drive root (D:\\tools) — runtime files would land on the drive root; use D:\\tools\\nvm\\installs\n" + + "- Existing folders that already contain non-NVM content (other apps would be deleted on uninstall)\n" + "- Your user profile, AppData, Desktop, Documents, Downloads, Pictures, Videos, Music\n" + "- Start Menu, Startup, LocalAppData\\Programs, LocalAppData\\Microsoft\n" + "- The NVM program folder, .shim, .nodejs, and utils\n" + diff --git a/fs/install_root_windows.go b/fs/install_root_windows.go index 99f08ab..886e680 100644 --- a/fs/install_root_windows.go +++ b/fs/install_root_windows.go @@ -19,6 +19,8 @@ type installRootProtect struct { } // CheckInstallRootAllowed refuses Node.js storage in OS, profile, or NVM program folders. +// It also refuses paths whose parent is a drive root (DataRoot would be the drive) and +// existing directories that already hold non-NVM content (uninstall would wipe siblings). func CheckInstallRootAllowed(path string) error { abs, err := normalizeInstallRootPath(path) if err != nil { @@ -27,12 +29,115 @@ func CheckInstallRootAllowed(path string) error { if installRootDenied(abs, defaultInstallRootProtect()) { return fmt.Errorf("%s\n\nRequested path: %s", InstallRootDeniedMessage(), abs) } + // D:\tools → DataRoot D:\ (.shim/proxy next to every other folder on the drive). + // Always refuse — even empty — so runtime never lands on a drive root. + if HasRiskyDataRootLayout(abs) { + return fmt.Errorf("%s\n\nRequested path: %s", InstallRootDeniedMessage(), abs) + } + if err := rejectForeignInstallRootContents(abs); err != nil { + return err + } return nil } -// IsSafeToDeleteInstallRoot reports whether uninstall may DelTree the storage path. +// IsSafeToDeleteInstallRoot reports whether uninstall may DelTree the entire storage path. +// Paths that fail CheckInstallRootAllowed, or that contain non-NVM children, must not be +// wiped wholesale (surgical version-dir removal only). func IsSafeToDeleteInstallRoot(path string) bool { - return CheckInstallRootAllowed(path) == nil + abs, err := normalizeInstallRootPath(path) + if err != nil { + return false + } + if installRootDenied(abs, defaultInstallRootProtect()) { + return false + } + if HasRiskyDataRootLayout(abs) { + return false + } + ok, err := installRootHasOnlyManagedContent(abs) + if err != nil || !ok { + return false + } + return true +} + +// rejectForeignInstallRootContents refuses an existing directory that already holds +// files/folders that are not Node version trees (vX.Y.Z). Missing/empty dirs are fine. +func rejectForeignInstallRootContents(abs string) error { + ok, err := installRootHasOnlyManagedContent(abs) + if err != nil { + return fmt.Errorf("%s\n\nRequested path: %s\nDetail: %v", InstallRootDeniedMessage(), abs, err) + } + if !ok { + return fmt.Errorf("%s\n\nRequested path: %s", InstallRootDeniedMessage(), abs) + } + return nil +} + +// installRootHasOnlyManagedContent is true when path is missing, empty, or only contains +// Node version directories (name starts with "v"). Unmanaged siblings make it false. +func installRootHasOnlyManagedContent(abs string) (bool, error) { + info, err := os.Lstat(abs) + if err != nil { + if os.IsNotExist(err) { + return true, nil + } + return false, err + } + if !info.IsDir() { + return false, nil + } + entries, err := os.ReadDir(abs) + if err != nil { + return false, err + } + for _, entry := range entries { + name := entry.Name() + if name == "." || name == ".." { + continue + } + if isManagedInstallRootEntry(name, entry.IsDir()) { + continue + } + return false, nil + } + return true, nil +} + +func isManagedInstallRootEntry(name string, isDir bool) bool { + if !isDir { + return false + } + // Version trees: v22.0.0, v24.11.1-rc.0, etc. + if len(name) < 2 || (name[0] != 'v' && name[0] != 'V') { + return false + } + return isVersionDirSuffix(name[1:]) +} + +func isVersionDirSuffix(rest string) bool { + if rest == "" { + return false + } + dot := false + digit := false + for i := 0; i < len(rest); i++ { + ch := rest[i] + switch { + case ch >= '0' && ch <= '9': + digit = true + case ch == '.': + dot = true + case ch == '-' || ch == '_' || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'): + // prerelease / build tags after digits started + if !digit { + return false + } + default: + return false + } + } + return digit && (dot || len(rest) >= 1) } func normalizeInstallRootPath(path string) (string, error) { diff --git a/fs/install_root_windows_test.go b/fs/install_root_windows_test.go index 32f6f49..d78078b 100644 --- a/fs/install_root_windows_test.go +++ b/fs/install_root_windows_test.go @@ -3,6 +3,7 @@ package fs import ( + "os" "path/filepath" "strings" "testing" @@ -57,6 +58,8 @@ func TestCheckInstallRootAllowedRejectsProtected(t *testing.T) { filepath.Join(local, "Programs"), filepath.Join(local, "Microsoft"), `C:\`, + `C:\nvm`, + `D:\soft`, ``, `\\server\share\nvm`, } @@ -76,6 +79,56 @@ func TestCheckInstallRootAllowedRejectsProtected(t *testing.T) { } } +func TestCheckInstallRootAllowedNestedUnderDriveOK(t *testing.T) { + // D:\tools\nvm\installs — parent is not a drive root, so DataRoot stays off the drive. + path := `C:\nvm-tools\installs` + if err := CheckInstallRootAllowed(path); err != nil { + t.Fatalf("nested path %q: %v", path, err) + } +} + +func TestCheckInstallRootAllowedRejectsForeignContents(t *testing.T) { + root := t.TempDir() + if err := os.MkdirAll(filepath.Join(root, "Git"), 0o755); err != nil { + t.Fatal(err) + } + if err := CheckInstallRootAllowed(root); err == nil { + t.Fatalf("expected NVM4313 for directory with foreign child") + } + if IsSafeToDeleteInstallRoot(root) { + t.Fatal("foreign contents must not be DelTree-safe") + } +} + +func TestCheckInstallRootAllowedAllowsVersionTreesOnly(t *testing.T) { + root := t.TempDir() + if err := os.MkdirAll(filepath.Join(root, "v22.0.0"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(root, "v24.11.1-rc.0"), 0o755); err != nil { + t.Fatal(err) + } + if err := CheckInstallRootAllowed(root); err != nil { + t.Fatalf("version-only root: %v", err) + } + if !IsSafeToDeleteInstallRoot(root) { + t.Fatal("version-only root should be DelTree-safe") + } +} + +func TestIsSafeToDeleteInstallRootRejectsMixedContents(t *testing.T) { + root := t.TempDir() + if err := os.MkdirAll(filepath.Join(root, "v22.0.0"), 0o755); err != nil { + t.Fatal(err) + } + if err := os.MkdirAll(filepath.Join(root, "IntelliJ IDEA"), 0o755); err != nil { + t.Fatal(err) + } + if IsSafeToDeleteInstallRoot(root) { + t.Fatal("mixed NVM + foreign contents must not be DelTree-safe") + } +} + func TestInstallRootDeniedMessageListsEvent(t *testing.T) { msg := InstallRootDeniedMessage() if !strings.Contains(msg, "Event code: NVM4313") { @@ -84,6 +137,9 @@ func TestInstallRootDeniedMessageListsEvent(t *testing.T) { if !strings.Contains(msg, "Program Files") { t.Fatalf("message missing Program Files list") } + if !strings.Contains(msg, "directly under a drive root") { + t.Fatalf("message missing drive-root-child guidance: %s", msg) + } } func mustKnownFolder(t *testing.T, id *windows.KNOWNFOLDERID, envNames ...string) string {