fix: make daemon reset conflict safe
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -98,6 +99,69 @@ func TestDoctorReturnsFailureWhenAnyCheckFails(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoctorStorageAuthorityRejectsSymlinkedPathsWithoutTouchingVictim(t *testing.T) {
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
path func(t *testing.T, root, victim string) string
|
||||
}{
|
||||
{
|
||||
name: "final path",
|
||||
path: func(t *testing.T, root, victim string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(root, "config", "profiles.json")
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(victim, path); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "ancestor path",
|
||||
path: func(t *testing.T, root, victim string) string {
|
||||
t.Helper()
|
||||
link := filepath.Join(root, "config")
|
||||
if err := os.Symlink(filepath.Dir(victim), link); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return filepath.Join(link, filepath.Base(victim))
|
||||
},
|
||||
},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
victim := filepath.Join(t.TempDir(), "victim.json")
|
||||
const content = `{"profiles":[]}`
|
||||
if err := os.WriteFile(victim, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
before, err := os.Stat(victim)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
configuration := profile.Configuration{Path: test.path(t, root, victim), Owner: profile.UserIdentity{UID: os.Getuid(), GID: os.Getgid()}}
|
||||
|
||||
checks := diagnosticChecks(configuration)
|
||||
if len(checks) != 2 || checks[1].OK || !strings.Contains(checks[1].Evidence, "symbolic link") {
|
||||
t.Fatalf("Profile storage diagnostics accepted symlink: %#v", checks)
|
||||
}
|
||||
after, err := os.Stat(victim)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := os.ReadFile(victim)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(got) != content || !reflect.DeepEqual(before.Sys(), after.Sys()) {
|
||||
t.Fatalf("storage diagnostics changed victim: before=%#v after=%#v content=%q", before, after, got)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoctorStoragePermissionCheckIsReadOnly(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "proxyctl", "profiles.json")
|
||||
writeConfig(t, path, `{"profiles":[]}`)
|
||||
|
||||
Reference in New Issue
Block a user