mirror of
https://github.com/safedep/pmg.git
synced 2026-08-03 07:24:09 +02:00
fix: harden system-install review findings
Require root-owned, non-group/other-writable pmg for --system install; allow remove without that validation. Doctor checks npm resolution for PATH precedence, uses ImpliesInterception instead of message matching, and documents version-manager shadowing. Pass profile bin dir from the shim manager and note that system config ignores per-user files. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -13,9 +13,11 @@ func useSystemPaths(t *testing.T, dir string) {
|
||||
t.Helper()
|
||||
systemBinDirOverride = filepath.Join(dir, "bin")
|
||||
systemProfilePathOverride = filepath.Join(dir, "profile.d", "pmg.sh")
|
||||
systemExecutableOwnershipCheck = false
|
||||
t.Cleanup(func() {
|
||||
systemBinDirOverride = ""
|
||||
systemProfilePathOverride = ""
|
||||
systemExecutableOwnershipCheck = true
|
||||
})
|
||||
}
|
||||
|
||||
@@ -100,15 +102,20 @@ func TestWriteSystemProfileRepairsStalePath(t *testing.T) {
|
||||
0o644,
|
||||
))
|
||||
|
||||
require.NoError(t, writeSystemProfile())
|
||||
binDir := filepath.Join(root, "custom-bin")
|
||||
require.NoError(t, writeSystemProfile(binDir))
|
||||
|
||||
content, err := os.ReadFile(SystemProfilePath())
|
||||
require.NoError(t, err)
|
||||
assert.Contains(t, string(content), SystemBinDir())
|
||||
assert.Contains(t, string(content), binDir)
|
||||
assert.NotContains(t, string(content), "/stale/path")
|
||||
assert.NotContains(t, string(content), SystemBinDir())
|
||||
}
|
||||
|
||||
func TestValidateSystemExecutableRejectsPrivateBinary(t *testing.T) {
|
||||
systemExecutableOwnershipCheck = false
|
||||
t.Cleanup(func() { systemExecutableOwnershipCheck = true })
|
||||
|
||||
privateDir := t.TempDir()
|
||||
privateExecutable := filepath.Join(privateDir, "pmg")
|
||||
require.NoError(t, os.WriteFile(privateExecutable, []byte("binary"), 0o700))
|
||||
@@ -118,3 +125,40 @@ func TestValidateSystemExecutableRejectsPrivateBinary(t *testing.T) {
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "not executable by all users")
|
||||
}
|
||||
|
||||
func TestValidateSystemExecutableRejectsGroupWritable(t *testing.T) {
|
||||
systemExecutableOwnershipCheck = false
|
||||
t.Cleanup(func() { systemExecutableOwnershipCheck = true })
|
||||
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "pmg")
|
||||
require.NoError(t, os.WriteFile(path, []byte("binary"), 0o755))
|
||||
require.NoError(t, os.Chmod(path, 0o775))
|
||||
|
||||
err := validateSystemExecutable(path)
|
||||
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "writable by group or others")
|
||||
}
|
||||
|
||||
func TestValidateSystemExecutableRejectsNonRootOwner(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
path := filepath.Join(dir, "pmg")
|
||||
require.NoError(t, os.WriteFile(path, []byte("binary"), 0o755))
|
||||
|
||||
err := validateSystemExecutable(path)
|
||||
|
||||
require.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "must be owned by root")
|
||||
}
|
||||
|
||||
func TestNewSystemShimManagerForRemoveSkipsValidation(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
useSystemPaths(t, root)
|
||||
systemExecutableOwnershipCheck = true
|
||||
|
||||
mgr, err := NewSystemShimManagerForRemove()
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, mgr.Install())
|
||||
require.NoError(t, mgr.Remove())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user