fix: anchor profile directory traversal
This commit is contained in:
@@ -111,7 +111,7 @@ func TestFileStoreLockSerializesAcrossProcesses(t *testing.T) {
|
||||
|
||||
func TestOwnedStoreAssignsInvokingUserToDirectoryLockAndReplacement(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "proxyctl", "profiles.json")
|
||||
owner := UserIdentity{Username: "alice", UID: 1001, GID: 1002}
|
||||
owner := UserIdentity{Username: "alice", HomeDir: filepath.Dir(filepath.Dir(path)), UID: 1001, GID: 1002}
|
||||
operations := &fakeFileOperations{}
|
||||
store := newFileStore(path, &owner, operations)
|
||||
if _, err := store.Mutate(func(state State) (State, error) {
|
||||
@@ -146,7 +146,7 @@ func TestOwnedStoreReportsOwnershipFailuresBeforeCommit(t *testing.T) {
|
||||
for _, failure := range []string{"proxyctl", "profiles.json.lock", ".profiles-"} {
|
||||
t.Run(failure, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "proxyctl", "profiles.json")
|
||||
owner := UserIdentity{Username: "alice", UID: 1001, GID: 1002}
|
||||
owner := UserIdentity{Username: "alice", HomeDir: filepath.Dir(filepath.Dir(path)), UID: 1001, GID: 1002}
|
||||
operations := &fakeFileOperations{failContains: failure}
|
||||
store := newFileStore(path, &owner, operations)
|
||||
_, err := store.Mutate(func(state State) (State, error) {
|
||||
@@ -171,7 +171,7 @@ func TestOwnedLoadRejectsConfigurationOwnershipFailure(t *testing.T) {
|
||||
if err := os.WriteFile(path, []byte(`{"profiles":[]}`), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner := UserIdentity{Username: "alice", UID: 1001, GID: 1002}
|
||||
owner := UserIdentity{Username: "alice", HomeDir: filepath.Dir(filepath.Dir(path)), UID: 1001, GID: 1002}
|
||||
operations := &fakeFileOperations{failContains: "profiles.json"}
|
||||
_, err := newFileStore(path, &owner, operations).Load()
|
||||
if err == nil || !strings.Contains(err.Error(), "set Profile configuration owner") {
|
||||
@@ -202,7 +202,7 @@ func TestLoadRejectsProfileConfigurationSymlinkWithoutChangingTarget(t *testing.
|
||||
}
|
||||
}
|
||||
|
||||
func TestMutationRejectsLockSymlinkWithoutChangingTarget(t *testing.T) {
|
||||
func TestLoadRejectsLockSymlinkWithoutChangingTarget(t *testing.T) {
|
||||
directory := filepath.Join(t.TempDir(), "proxyctl")
|
||||
if err := os.MkdirAll(directory, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -215,7 +215,7 @@ func TestMutationRejectsLockSymlinkWithoutChangingTarget(t *testing.T) {
|
||||
if err := os.Symlink(target, path+".lock"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := NewFileStore(path).Mutate(func(state State) (State, error) { return state, nil })
|
||||
_, err := NewFileStore(path).Load()
|
||||
if err == nil || !strings.Contains(err.Error(), "without following links") {
|
||||
t.Fatalf("lock symlink error = %v", err)
|
||||
}
|
||||
@@ -235,6 +235,136 @@ func TestMutationRejectsLockSymlinkWithoutChangingTarget(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRepairsExistingDirectoryConfigurationAndLockOwnership(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
directory := filepath.Join(home, ".config", "proxyctl")
|
||||
if err := os.MkdirAll(directory, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Chmod(directory, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(directory, "profiles.json")
|
||||
if err := os.WriteFile(path, []byte(`{"profiles":[]}`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
lockPath := path + ".lock"
|
||||
if err := os.WriteFile(lockPath, nil, 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
owner := UserIdentity{Username: "alice", HomeDir: home, UID: 1001, GID: 1002}
|
||||
operations := &fakeFileOperations{}
|
||||
_, err := newFileStore(path, &owner, operations).Load()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
for target, mode := range map[string]os.FileMode{directory: 0o700, path: 0o600, lockPath: 0o600} {
|
||||
info, statErr := os.Stat(target)
|
||||
if statErr != nil {
|
||||
t.Fatal(statErr)
|
||||
}
|
||||
if info.Mode().Perm() != mode {
|
||||
t.Errorf("mode for %s = %#o, want %#o", target, info.Mode().Perm(), mode)
|
||||
}
|
||||
}
|
||||
for _, target := range []string{directory, path, lockPath} {
|
||||
found := false
|
||||
for _, call := range operations.calls {
|
||||
if call.path == target && call.uid == 1001 && call.gid == 1002 {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("no invoking-user ownership repair for %s; calls=%#v", target, operations.calls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadRejectsConfigAncestorSymlinkWithoutChangingVictim(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
victim := t.TempDir()
|
||||
victimFile := filepath.Join(victim, "unchanged")
|
||||
if err := os.WriteFile(victimFile, []byte("victim"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(victim, filepath.Join(home, ".config")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(home, ".config", "proxyctl", "profiles.json")
|
||||
owner := UserIdentity{Username: "alice", HomeDir: home, UID: 1001, GID: 1002}
|
||||
_, err := newFileStore(path, &owner, &fakeFileOperations{}).Load()
|
||||
if err == nil || !strings.Contains(err.Error(), "without following links") {
|
||||
t.Fatalf("ancestor symlink error = %v", err)
|
||||
}
|
||||
contents, readErr := os.ReadFile(victimFile)
|
||||
if readErr != nil || string(contents) != "victim" {
|
||||
t.Fatalf("victim changed: contents=%q error=%v", contents, readErr)
|
||||
}
|
||||
if _, statErr := os.Stat(filepath.Join(victim, "proxyctl")); !errors.Is(statErr, os.ErrNotExist) {
|
||||
t.Fatalf("created through ancestor symlink: %v", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMutationRejectsIntermediateXDGAncestorSymlink(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
xdg := filepath.Join(home, "xdg")
|
||||
if err := os.Mkdir(xdg, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
victim := t.TempDir()
|
||||
if err := os.Symlink(victim, filepath.Join(xdg, "intermediate")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
path := filepath.Join(xdg, "intermediate", "proxyctl", "profiles.json")
|
||||
owner := UserIdentity{Username: "alice", HomeDir: home, UID: 1001, GID: 1002}
|
||||
store := &FileStore{path: path, owner: &owner, trustedRoot: xdg, operations: &fakeFileOperations{}}
|
||||
_, err := store.Mutate(func(state State) (State, error) { return state, nil })
|
||||
if err == nil || !strings.Contains(err.Error(), "without following links") {
|
||||
t.Fatalf("intermediate symlink error = %v", err)
|
||||
}
|
||||
entries, readErr := os.ReadDir(victim)
|
||||
if readErr != nil {
|
||||
t.Fatal(readErr)
|
||||
}
|
||||
if len(entries) != 0 {
|
||||
t.Fatalf("victim directory changed through symlink: %#v", entries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestOwnedMutationCreatesMissingDirectoriesAnchoredWithOwnerAndMode(t *testing.T) {
|
||||
home := t.TempDir()
|
||||
path := filepath.Join(home, ".config", "nested", "proxyctl", "profiles.json")
|
||||
owner := UserIdentity{Username: "alice", HomeDir: home, UID: 1001, GID: 1002}
|
||||
operations := &fakeFileOperations{}
|
||||
store := newFileStore(path, &owner, operations)
|
||||
if _, err := store.Mutate(func(state State) (State, error) { return state, nil }); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, directory := range []string{
|
||||
filepath.Join(home, ".config"),
|
||||
filepath.Join(home, ".config", "nested"),
|
||||
filepath.Join(home, ".config", "nested", "proxyctl"),
|
||||
} {
|
||||
info, err := os.Stat(directory)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if info.Mode().Perm() != 0o700 {
|
||||
t.Errorf("created directory %s mode = %#o, want 0700", directory, info.Mode().Perm())
|
||||
}
|
||||
found := false
|
||||
for _, call := range operations.calls {
|
||||
if call.path == directory && call.uid == 1001 && call.gid == 1002 {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Errorf("created directory %s was not assigned invoking-user ownership", directory)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectorySyncFailureReturnsCommittedStateAndRecognizableError(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "proxyctl", "profiles.json")
|
||||
operations := &fakeFileOperations{syncErr: errors.New("injected sync failure")}
|
||||
|
||||
Reference in New Issue
Block a user