Skip to content

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

Windows: keep credential files private through MSI uninstall and malformed config

Claude5e51c08cad36
diff --git a/cli/beacon/internal/endpoint/config/config.go b/cli/beacon/internal/endpoint/config/config.go
index e3784405..ca4d6044 100644
--- a/cli/beacon/internal/endpoint/config/config.go
+++ b/cli/beacon/internal/endpoint/config/config.go
@@ -2,6 +2,7 @@ package config
import (
"encoding/json"
+ "errors"
"fmt"
"os"
"path/filepath"
@@ -276,17 +277,41 @@ func saveOTLPForwardSecrets(cfg Config) error {
// 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
- }
+ secret := wasPrivate || rawConfigHoldsCredential(data)
if secret {
return writeSecretFile(false, path, data)
}
return atomicfile.Write(path, data, 0644)
}
+// rawConfigHoldsCredential reports whether config.json text holds a HEC token. It reads only the
+// token fields, so a field elsewhere of an unexpected type (a hand-edited "grpc_port": "4317")
+// cannot hide one, and text it cannot parse at all counts as holding one: the private write is the
+// safe one to fall back to.
+func rawConfigHoldsCredential(data []byte) bool {
+ var partial struct {
+ Destinations struct {
+ SplunkHEC struct {
+ Token json.RawMessage `json:"token"`
+ } `json:"splunk_hec"`
+ FalconHEC struct {
+ Token json.RawMessage `json:"token"`
+ } `json:"falcon_hec"`
+ } `json:"destinations"`
+ }
+ if err := json.Unmarshal(data, &partial); err != nil {
+ var typeErr *json.UnmarshalTypeError
+ if !errors.As(err, &typeErr) {
+ return true
+ }
+ }
+ present := func(raw json.RawMessage) bool {
+ text := strings.TrimSpace(string(raw))
+ return text != "" && text != "null" && text != `""`
+ }
+ return present(partial.Destinations.SplunkHEC.Token) || present(partial.Destinations.FalconHEC.Token)
+}
+
// 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 5f624cdd..2de1866e 100644
--- a/cli/beacon/internal/endpoint/config/config_test.go
+++ b/cli/beacon/internal/endpoint/config/config_test.go
@@ -355,6 +355,10 @@ func TestRewriteSystemConfigKeepsCredentialsPrivate(t *testing.T) {
{"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},
+ // A wrong-typed field elsewhere must not hide the token.
+ {"token beside a bad field", `{"collector":{"grpc_port":"4317"},"destinations":{"falcon_hec":{"token":"t"}}}`, false, 0600},
+ {"destinations of the wrong shape", `{"destinations":"oops"}`, false, 0644},
+ {"unparseable", `{"destinations":`, false, 0600},
} {
path := filepath.Join(dir, strings.ReplaceAll(tc.name, " ", "-")+".json")
if err := RewriteSystemConfig(path, []byte(tc.body), tc.wasPrivate); err != nil {
diff --git a/packaging/windows/uninstall-endpoint.ps1 b/packaging/windows/uninstall-endpoint.ps1
index 73253a6c..39d86e5f 100644
--- a/packaging/windows/uninstall-endpoint.ps1
+++ b/packaging/windows/uninstall-endpoint.ps1
@@ -39,6 +39,32 @@ if (-not (Test-Path -LiteralPath $beaconBin)) {
$configDir = Join-Path $env:ProgramData 'Beacon\Endpoint'
$retained = @('config.json', 'otelcol.yaml', 'otlp-forward-headers.json')
+# A file `beacon endpoint install --system` wrote with a protected DACL (SYSTEM and Administrators
+# only, because it holds a credential: a HEC token, the OTLP forward's headers) has to come back with
+# that DACL. Copy-Item would give the copy what the destination directory passes on, which every
+# user can read. So such a file is copied into a new file created with the original descriptor --
+# set by the creation itself, so there is no moment anyone else could open it -- and renamed into
+# place. Other files keep the plain copy.
+$protected = @{}
+function Copy-Retained([string]$source, [string]$destination, [string]$sddl) {
+ if (-not $sddl) {
+ Copy-Item -LiteralPath $source -Destination $destination -Force
+ return
+ }
+ $security = New-Object System.Security.AccessControl.FileSecurity
+ $security.SetSecurityDescriptorSddlForm($sddl)
+ $partial = "$destination.restoring"
+ Remove-Item -LiteralPath $partial -Force -ErrorAction SilentlyContinue
+ $bytes = [System.IO.File]::ReadAllBytes($source)
+ $stream = [System.IO.File]::Create($partial, 4096, [System.IO.FileOptions]::None, $security)
+ try {
+ $stream.Write($bytes, 0, $bytes.Length)
+ } finally {
+ $stream.Close()
+ }
+ Move-Item -LiteralPath $partial -Destination $destination -Force
+}
+
$stash = $null
if ($purge -ne '1') {
$stash = Join-Path ([System.IO.Path]::GetTempPath()) ("beacon-uninstall-" + [Guid]::NewGuid().ToString('N'))
@@ -46,7 +72,11 @@ if ($purge -ne '1') {
foreach ($name in $retained) {
$source = Join-Path $configDir $name
if (Test-Path -LiteralPath $source) {
- Copy-Item -LiteralPath $source -Destination (Join-Path $stash $name) -Force
+ $acl = Get-Acl -LiteralPath $source
+ if ($acl.AreAccessRulesProtected) {
+ $protected[$name] = $acl.Sddl
+ }
+ Copy-Retained $source (Join-Path $stash $name) $protected[$name]
}
}
Write-Output "beacon: removing the endpoint, keeping configuration and logs (set BEACON_PURGE=1 to remove them)"
@@ -76,7 +106,7 @@ if ($null -ne $stash) {
$saved = Join-Path $stash $name
if (Test-Path -LiteralPath $saved) {
New-Item -ItemType Directory -Force -Path $configDir | Out-Null
- Copy-Item -LiteralPath $saved -Destination (Join-Path $configDir $name) -Force
+ Copy-Retained $saved (Join-Path $configDir $name) $protected[$name]
$restored++
}
}