fix(cli): validate runtimes before uninstall

This commit is contained in:
ZacharyZhang-NY committed 2026-07-19 16:40:05 -04:00
1 parent 7bf84a35e0
commit 3488ab778c
7 files changed
+250 -87

No files matched your search

+15
View File
@@ -126,3 +126,18 @@ func RequirePhysicalDirectory(path string) (bool, error) {
}
return true, nil
}
// RequirePhysicalFile reports whether a path is an existing physical regular file.
func RequirePhysicalFile(path string) (bool, error) {
info, err := os.Lstat(path)
if os.IsNotExist(err) {
return false, nil
}
if err != nil {
return false, fmt.Errorf("inspect agent runtime file %s: %w", path, err)
}
if info.Mode()&os.ModeSymlink != 0 || !info.Mode().IsRegular() {
return false, fmt.Errorf("agent runtime config is not a physical file: %s", path)
}
return true, nil
}
+16
View File
@@ -93,3 +93,19 @@ func TestResolveAgentRuntimeDirectoryRejectsSymlinkChild(t *testing.T) {
t.Fatalf("ResolveAgentRuntimeDirectory() error = %v", err)
}
}
func TestRequirePhysicalFileRejectsSymlink(t *testing.T) {
setRuntimeTestHome(t)
target := filepath.Join(t.TempDir(), "target.json")
if err := os.WriteFile(target, []byte("{}"), 0600); err != nil {
t.Fatalf("WriteFile() error = %v", err)
}
link := filepath.Join(t.TempDir(), "config.json")
if err := os.Symlink(target, link); err != nil {
t.Skipf("file symbolic links unavailable: %v", err)
}
_, err := RequirePhysicalFile(link)
if err == nil || !strings.Contains(err.Error(), "physical file") {
t.Fatalf("RequirePhysicalFile() error = %v", err)
}
}