fix: CLI honors secrets.extra_patterns; SaveState uses a unique temp file
Some checks failed
CI / vet-build-test (push) Has been cancelled

- add loadGuard(dir) helper in cmd/devbox/main.go; replaces all 4
  secret.New(nil) calls in publish/mount/restore/deploy so user's
  [secrets] extra_patterns from config.toml are enforced on CLI upload
  paths (was only enforced by the daemon — security leak)
- SaveState: switch from fixed .tmp suffix to os.CreateTemp(.state-*.tmp)
  mirroring SaveDaemon, eliminating concurrent-write corruption
This commit is contained in:
shoemoney 2026-06-30 19:26:59 -05:00
parent df33b5facd
commit a8c32c4a23
4 changed files with 62 additions and 6 deletions

View file

@ -169,7 +169,7 @@ func publishCmd() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
guard, err := secret.New(nil) guard, err := loadGuard(dir)
if err != nil { if err != nil {
return err return err
} }
@ -235,7 +235,7 @@ func mountCmd() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
guard, err := secret.New(nil) guard, err := loadGuard(dir)
if err != nil { if err != nil {
return err return err
} }
@ -278,6 +278,13 @@ func mountCmd() *cobra.Command {
return cmd return cmd
} }
// loadGuard builds the secret guard with the user's extra_patterns from config.toml,
// so CLI upload paths block the same secrets the daemon does. Missing settings → defaults only.
func loadGuard(dir string) (*secret.Guard, error) {
s, _ := config.LoadSettings(dir) // absent/parse-error → zero Settings → default patterns
return secret.New(s.Secrets.ExtraPatterns)
}
// splitShare splits "share/sub/path" into ("share", "sub/path"); a bare "share" // splitShare splits "share/sub/path" into ("share", "sub/path"); a bare "share"
// yields an empty sub-path. // yields an empty sub-path.
func splitShare(arg string) (share, subpath string) { func splitShare(arg string) (share, subpath string) {
@ -475,7 +482,7 @@ func restoreCmd() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
guard, err := secret.New(nil) guard, err := loadGuard(dir)
if err != nil { if err != nil {
return err return err
} }
@ -683,7 +690,7 @@ func deployCmd() *cobra.Command {
if err != nil { if err != nil {
return err return err
} }
guard, err := secret.New(nil) guard, err := loadGuard(dir)
if err != nil { if err != nil {
return err return err
} }

View file

@ -14,6 +14,31 @@ import (
"github.com/shoemoney/devbox/internal/identity" "github.com/shoemoney/devbox/internal/identity"
) )
// TestLoadGuard proves loadGuard picks up extra_patterns from config.toml so CLI
// upload paths honour the user's secret list (Bug 1 regression check).
func TestLoadGuard(t *testing.T) {
cfgBase := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", cfgBase)
dir := filepath.Join(cfgBase, "devbox")
if err := os.MkdirAll(dir, 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "config.toml"),
[]byte("[secrets]\nextra_patterns=[\"*.secret\"]\n"), 0o600); err != nil {
t.Fatal(err)
}
g, err := loadGuard(dir)
if err != nil {
t.Fatalf("loadGuard: %v", err)
}
if !g.Blocked("foo.secret") {
t.Error("foo.secret must be blocked by extra_patterns")
}
if g.Blocked("main.go") {
t.Error("main.go must NOT be blocked")
}
}
// TestStatusJSON proves `status --json` emits valid JSON and reports not-joined // TestStatusJSON proves `status --json` emits valid JSON and reports not-joined
// on a fresh config dir (the scriptable path for fleet monitoring). // on a fresh config dir (the scriptable path for fleet monitoring).
func TestStatusJSON(t *testing.T) { func TestStatusJSON(t *testing.T) {

View file

@ -147,8 +147,17 @@ func SaveState(dir string, m map[string]string) error {
if err != nil { if err != nil {
return err return err
} }
tmp := statePath(dir) + ".tmp" f, err := os.CreateTemp(dir, ".state-*.tmp")
if err := os.WriteFile(tmp, b, 0o600); err != nil { if err != nil {
return err
}
tmp := f.Name()
defer os.Remove(tmp) // no-op after rename
if _, err := f.Write(b); err != nil {
f.Close()
return err
}
if err := f.Close(); err != nil {
return err return err
} }
return os.Rename(tmp, statePath(dir)) return os.Rename(tmp, statePath(dir))

View file

@ -32,3 +32,18 @@ func TestLoadDaemonMissingIsEmpty(t *testing.T) {
t.Fatalf("expected empty daemon for missing file, got %+v", d) t.Fatalf("expected empty daemon for missing file, got %+v", d)
} }
} }
func TestStateRoundTrip(t *testing.T) {
dir := t.TempDir()
in := map[string]string{"share\x00\x00/local": "snap123"}
if err := SaveState(dir, in); err != nil {
t.Fatal(err)
}
got, err := LoadState(dir)
if err != nil {
t.Fatal(err)
}
if got["share\x00\x00/local"] != "snap123" {
t.Fatalf("round-trip mismatch: %v", got)
}
}