Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

Windows: keep config.json private when the auto-update mode is rewritten

Claudebed5901e0d0c
diff --git a/cli/beacon/cmd/endpoint_update_service.go b/cli/beacon/cmd/endpoint_update_service.go
index 5620116b..5058fa5c 100644
--- a/cli/beacon/cmd/endpoint_update_service.go
+++ b/cli/beacon/cmd/endpoint_update_service.go
@@ -7,12 +7,12 @@ import (
"math/rand"
"os"
"path/filepath"
+ "runtime"
"strconv"
"time"
"github.com/spf13/cobra"
- "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/atomicfile"
endpointconfig "github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/config"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/lifecycle"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/selfupdate"
@@ -97,15 +97,15 @@ func setConfigAutoUpdateModeAt(path, mode string) error {
if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
return err
}
- return atomicfile.Write(path, data, 0644)
+ return endpointconfig.RewriteSystemConfig(path, data, false)
}
var raw map[string]json.RawMessage
if err := json.Unmarshal(data, &raw); err != nil {
return err
}
- perm := os.FileMode(0644)
- if info, err := os.Stat(path); err == nil {
- perm = info.Mode().Perm()
+ private := false
+ if info, err := os.Stat(path); err == nil && runtime.GOOS != "windows" {
+ private = info.Mode().Perm()&0o077 == 0
}
autoUpdate, err := json.Marshal(endpointconfig.AutoUpdate{Mode: mode})
if err != nil {
@@ -116,7 +116,7 @@ func setConfigAutoUpdateModeAt(path, mode string) error {
if err != nil {
return err
}
- return atomicfile.Write(path, out, perm)
+ return endpointconfig.RewriteSystemConfig(path, out, private)
}
func requireRootForUpdater() error {
diff --git a/cli/beacon/internal/endpoint/config/config.go b/cli/beacon/internal/endpoint/config/config.go
index 0956b9ff..377f0c5a 100644
--- a/cli/beacon/internal/endpoint/config/config.go
+++ b/cli/beacon/internal/endpoint/config/config.go
@@ -262,6 +262,23 @@ func saveOTLPForwardSecrets(cfg Config) error {
return writeSecretFile(cfg.UserMode, path, data)
}
+// RewriteSystemConfig replaces the system config.json with data, for the callers that edit its raw
+// JSON in place (the auto-update mode) rather than going through Save. It is written the way Save
+// would write it: SYSTEM and Administrators only on Windows, 0600 on POSIX, when it holds a
+// credential or was already private, and otherwise readable like any config.json. Writing it with
+// atomicfile.Write alone gave a Windows file the tree's DACL, so every user could read a HEC token.
+func RewriteSystemConfig(path string, data []byte, wasPrivate bool) error {
+ var cfg Config
+ secret := wasPrivate
+ if json.Unmarshal(data, &cfg) == nil && hasSecretConfigFields(cfg) {
+ secret = true
+ }
+ if secret {
+ return writeSecretFile(false, path, data)
+ }
+ return atomicfile.Write(path, data, 0644)
+}
+
// writeSecretFile writes a file holding a credential. A system install's files sit in the
// machine-wide tree, whose Windows DACL lets every user read what is below it and ignores the
// mode, so they are created with SYSTEM and Administrators only (sysacl.WriteSecretFile); per-user
diff --git a/cli/beacon/internal/endpoint/config/config_test.go b/cli/beacon/internal/endpoint/config/config_test.go
index 78e9070f..8c943484 100644
--- a/cli/beacon/internal/endpoint/config/config_test.go
+++ b/cli/beacon/internal/endpoint/config/config_test.go
@@ -340,3 +340,32 @@ func TestValidateDestinationsRejectsBadOTLPForward(t *testing.T) {
})
}
}
+
+// Callers that edit config.json's raw JSON (the auto-update mode) write it as Save would: private
+// when it holds a credential or already was, readable otherwise. On Windows the private case is the
+// SYSTEM/Administrators-only DACL (sysacl.WriteSecretFile), which TestWriteSecretFile covers there.
+func TestRewriteSystemConfigKeepsCredentialsPrivate(t *testing.T) {
+ testenv.RequirePOSIXFileModes(t)
+ dir := t.TempDir()
+ for _, tc := range []struct {
+ name, body string
+ wasPrivate bool
+ want os.FileMode
+ }{
+ {"hec token", `{"destinations":{"splunk_hec":{"endpoint":"https://splunk.example","token":"t"}}}`, false, 0600},
+ {"already private", `{"auto_update":{"mode":"off"}}`, true, 0600},
+ {"nothing secret", `{"auto_update":{"mode":"off"}}`, false, 0644},
+ } {
+ path := filepath.Join(dir, strings.ReplaceAll(tc.name, " ", "-")+".json")
+ if err := RewriteSystemConfig(path, []byte(tc.body), tc.wasPrivate); err != nil {
+ t.Fatalf("%s: %v", tc.name, err)
+ }
+ info, err := os.Stat(path)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if got := info.Mode().Perm(); got != tc.want {
+ t.Errorf("%s: permissions = %o, want %o", tc.name, got, tc.want)
+ }
+ }
+}
diff --git a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
index 4d026b6b..a32381c3 100644
--- a/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
+++ b/cli/beacon/internal/endpoint/lifecycle/lifecycle.go
@@ -5,6 +5,7 @@ import (
"fmt"
"os"
"path/filepath"
+ "runtime"
"strings"
"time"
@@ -1026,11 +1027,14 @@ func setAutoUpdateModeInConfigFile(path, mode string) error {
if err != nil {
return err
}
- perm := os.FileMode(0644)
- if info, err := os.Stat(path); err == nil {
- perm = info.Mode().Perm()
- }
- return atomicfile.Write(path, out, perm)
+ return endpointconfig.RewriteSystemConfig(path, out, configIsPrivate(path))
+}
+
+// configIsPrivate reports whether the file at path is readable by its owner only. Only POSIX modes
+// say so; on Windows RewriteSystemConfig decides from the content.
+func configIsPrivate(path string) bool {
+ info, err := os.Stat(path)
+ return err == nil && runtime.GOOS != "windows" && info.Mode().Perm()&0o077 == 0
}
func GetStatus(userMode bool, logPath string) Status {