fix: make daemon reset conflict safe

This commit is contained in:
proxyctl coordinator
2026-07-17 11:11:56 +08:00
parent 613fe56540
commit 9e2418f45e
6 changed files with 283 additions and 73 deletions
+1 -35
View File
@@ -14,8 +14,6 @@ import (
"path/filepath"
"strings"
"golang.org/x/sys/unix"
"proxyctl/internal/profile"
"proxyctl/internal/securefs"
"proxyctl/internal/target"
@@ -194,41 +192,9 @@ func (a *Adapter) StartupPath() string { return a.configuration.StartupFile
// DiagnosticChecks performs no writes or command execution.
func (a *Adapter) DiagnosticChecks(_ context.Context, _ *profile.Profile) []target.Check {
paths := []string{a.configuration.EnvironmentFile, a.configuration.RecoveryFile}
writable := true
var evidence []string
for _, path := range paths {
ok, detail := writablePath(path)
writable = writable && ok
evidence = append(evidence, detail)
}
return []target.Check{
{Name: "required commands", OK: true, Evidence: "Shell Target requires no external commands"},
{Name: "write authority", OK: writable, Evidence: strings.Join(evidence, "; ")},
}
}
func writablePath(path string) (bool, string) {
candidate := path
for {
info, err := os.Stat(candidate)
if err == nil {
if !info.IsDir() {
candidate = filepath.Dir(candidate)
}
if err := unix.Access(candidate, unix.W_OK); err != nil {
return false, candidate + " is not writable: " + err.Error()
}
return true, candidate + " is writable"
}
if !errors.Is(err, fs.ErrNotExist) {
return false, candidate + " cannot be inspected: " + err.Error()
}
parent := filepath.Dir(candidate)
if parent == candidate {
return false, path + " has no writable existing ancestor"
}
candidate = parent
target.ReadOnlyWriteAuthority(a.configuration.EnvironmentFile, a.configuration.RecoveryFile),
}
}
+65
View File
@@ -6,6 +6,7 @@ import (
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
"testing"
@@ -15,6 +16,70 @@ import (
"proxyctl/internal/target/targettest"
)
func TestShellDiagnosticsRejectSymlinkedPathsWithoutTouchingVictim(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", "shell.env")
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")
const content = "shell-diagnostic-victim"
if err := os.WriteFile(victim, []byte(content), 0o640); err != nil {
t.Fatal(err)
}
before, err := os.Stat(victim)
if err != nil {
t.Fatal(err)
}
environmentPath := test.path(t, root, victim)
a := shell.New(shell.Configuration{Shell: "/bin/bash", EnvironmentFile: environmentPath, RecoveryFile: filepath.Join(root, "state", "shell.json")}, nil, shell.MapEnvironment{})
checks := a.DiagnosticChecks(context.Background(), nil)
if len(checks) != 2 || checks[1].OK || !strings.Contains(checks[1].Evidence, "symbolic link") {
t.Fatalf("Shell 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("diagnostics changed victim: before=%#v after=%#v content=%q", before, after, got)
}
})
}
}
func TestShellAvailabilitySupportsOnlyBashAndZsh(t *testing.T) {
for _, tc := range []struct {
shell string