Skip to content

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

hooks: ignore forged HOME and ungated BEACON_CLOUD_* (#913) (#1184)

Vamsi Krishna2f9ea89afb68
diff --git a/cli/beacon-hooks/cmd/share_auto_test.go b/cli/beacon-hooks/cmd/share_auto_test.go
index 9fd3aa27..8c02da41 100644
--- a/cli/beacon-hooks/cmd/share_auto_test.go
+++ b/cli/beacon-hooks/cmd/share_auto_test.go
@@ -8,6 +8,7 @@ import (
"github.com/asymptote-labs/agent-beacon/cli/beacon-hooks/internal/logging"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/accounthome"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/activesession"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/shareauto"
)
@@ -120,10 +121,21 @@ func TestAutoShareOffWritesNothing(t *testing.T) {
}
}
+// setAccountHome stands in for the account's own home, so these tests never read the real
+// ~/.beacon/endpoint/share.json of whoever runs them.
+func setAccountHome(t *testing.T, home string) {
+ t.Helper()
+ prev := accounthome.Dir
+ accounthome.Dir = func() (string, error) { return home, nil }
+ t.Cleanup(func() { accounthome.Dir = prev })
+}
+
// The hook's environment can come from the repository, so the settings-file override must not point
// it at a file that turns auto-share on.
func TestAutoShareIgnoresRepoSuppliedSettingsPath(t *testing.T) {
logPath := shareAutoHookEnv(t, nil)
+ account := t.TempDir()
+ setAccountHome(t, account)
setShareSettingsPath(t, shareauto.HookPath)
home := t.TempDir()
t.Setenv("HOME", home)
@@ -140,6 +152,28 @@ func TestAutoShareIgnoresRepoSuppliedSettingsPath(t *testing.T) {
}
}
+// A forged HOME with auto-share enabled under it must not publish sessions: HookPath reads the
+// account's home, which has no settings here.
+func TestAutoShareIgnoresForgedHome(t *testing.T) {
+ logPath := shareAutoHookEnv(t, nil)
+ account := t.TempDir()
+ forged := t.TempDir()
+ setAccountHome(t, account)
+ setShareSettingsPath(t, shareauto.HookPath)
+ t.Setenv("HOME", forged)
+ t.Setenv("USERPROFILE", forged)
+ if err := shareauto.Save(filepath.Join(forged, ".beacon", "endpoint", shareauto.FileName), shareauto.Settings{
+ Auto: shareauto.Auto{Enabled: true, Visibility: "public"},
+ }); err != nil {
+ t.Fatal(err)
+ }
+
+ runClaudeSessionHooks(t, "sess-a")
+ if n := len(shareRequests(t, logPath)); n != 0 {
+ t.Fatalf("share requests from a forged HOME = %d", n)
+ }
+}
+
func TestAutoShareHonorsProjectOptOut(t *testing.T) {
logPath := shareAutoHookEnv(t, &shareauto.Settings{
Auto: shareauto.Auto{Enabled: true},
diff --git a/cli/beacon-hooks/internal/cloudshuttle/shuttle.go b/cli/beacon-hooks/internal/cloudshuttle/shuttle.go
index 0553059f..bfc0dcb5 100644
--- a/cli/beacon-hooks/internal/cloudshuttle/shuttle.go
+++ b/cli/beacon-hooks/internal/cloudshuttle/shuttle.go
@@ -138,21 +138,33 @@ func inSandbox(label, p, name string) string {
return p
}
}
- if strings.TrimSpace(os.Getenv("BEACON_ORIGIN")) == "cloud" {
+ if inCloudSandbox() {
fmt.Fprintf(os.Stderr, "beacon-hooks: cloud shuttle ignores %s %s outside %s; using %s\n", label, p, SandboxDir, fallback)
}
return fallback
}
+// inCloudSandbox reports whether the hook says it runs in a cloud sandbox (BEACON_ORIGIN=cloud),
+// the only place the shuttle acts. The marker comes from the same environment as BEACON_CLOUD_*,
+// so it keeps a local install that happens to carry those variables from uploading; it does not
+// stop a repository that sets both. That case is bounded by inSandbox, which keeps the shuttle to
+// the sandbox's own files.
+func inCloudSandbox() bool {
+ return strings.TrimSpace(os.Getenv("BEACON_ORIGIN")) == "cloud"
+}
+
func MaybeUpload(ctx context.Context, force bool) error {
+ if !inCloudSandbox() {
+ return nil
+ }
return Upload(ctx, ConfigFromEnv(), force)
}
func ResetFromEnv() error {
- cfg := ConfigFromEnv()
- if strings.TrimSpace(os.Getenv("BEACON_ORIGIN")) != "cloud" {
+ if !inCloudSandbox() {
return nil
}
+ cfg := ConfigFromEnv()
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
release, err := lockUpload(ctx, cfg.StatePath)
diff --git a/cli/beacon-hooks/internal/cloudshuttle/shuttle_test.go b/cli/beacon-hooks/internal/cloudshuttle/shuttle_test.go
index 847ccfa5..2098bd39 100644
--- a/cli/beacon-hooks/internal/cloudshuttle/shuttle_test.go
+++ b/cli/beacon-hooks/internal/cloudshuttle/shuttle_test.go
@@ -56,6 +56,33 @@ func TestResetFromEnvPreservesLockFiles(t *testing.T) {
}
}
+// Stop and SessionEnd call MaybeUpload whenever BEACON_CLOUD_* is set. Without the cloud
+// sandbox marker those variables must not open a network connection.
+func TestMaybeUploadWithoutCloudOriginSendsNothing(t *testing.T) {
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ t.Errorf("unexpected upload without BEACON_ORIGIN=cloud: %s %s", r.Method, r.URL)
+ }))
+ defer server.Close()
+ dir := t.TempDir()
+ useSandbox(t, dir)
+ logPath := filepath.Join(dir, "runtime.jsonl")
+ if err := os.WriteFile(logPath, []byte("{\"event\":\"session\"}\n"), 0o644); err != nil {
+ t.Fatal(err)
+ }
+ t.Setenv("BEACON_ORIGIN", "")
+ t.Setenv("BEACON_CLOUD_UPLOAD", "s3")
+ t.Setenv("BEACON_CLOUD_S3_BUCKET", "bucket")
+ t.Setenv("BEACON_CLOUD_S3_ENDPOINT", server.URL)
+ t.Setenv("AWS_ACCESS_KEY_ID", "AKIATEST")
+ t.Setenv("AWS_SECRET_ACCESS_KEY", "secret")
+ t.Setenv("BEACON_RUN_ID", "run")
+ t.Setenv("BEACON_ENDPOINT_LOG", logPath)
+ t.Setenv("BEACON_CLOUD_SHUTTLE_STATE", filepath.Join(dir, "state.json"))
+ if err := MaybeUpload(context.Background(), true); err != nil {
+ t.Fatalf("MaybeUpload returned error: %v", err)
+ }
+}
+
// A target the environment supplies cannot select a log outside the sandbox directory, so a
// repository cannot have a host's runtime log, every project's sessions, uploaded to its bucket.
func TestMaybeUploadRefusesALogOutsideTheSandbox(t *testing.T) {
@@ -115,6 +142,7 @@ func TestMaybeUploadWithoutCredentialsSendsNothing(t *testing.T) {
t.Fatal(err)
}
statePath := filepath.Join(dir, "state.json")
+ t.Setenv("BEACON_ORIGIN", "cloud")
t.Setenv("BEACON_ENDPOINT_LOG", logPath)
t.Setenv("BEACON_CLOUD_SHUTTLE_STATE", statePath)
t.Setenv("BEACON_RUN_ID", "run")
diff --git a/cli/beacon/cmd/share_auto_test.go b/cli/beacon/cmd/share_auto_test.go
index d4270893..9761333d 100644
--- a/cli/beacon/cmd/share_auto_test.go
+++ b/cli/beacon/cmd/share_auto_test.go
@@ -12,6 +12,7 @@ import (
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/managedprivacy"
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/testenv"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/accounthome"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/shareauto"
)
@@ -33,8 +34,11 @@ func runShareAuto(t *testing.T, args ...string) (string, string, error) {
func shareAutoTestEnv(t *testing.T, connected bool, mode string) string {
t.Helper()
env := newShareEnv(t)
+ // The CLI and the hooks both use the account's home, not HOME; stand the test home in for it.
+ prevHome := accounthome.Dir
+ accounthome.Dir = func() (string, error) { return env.home, nil }
+ t.Cleanup(func() { accounthome.Dir = prevHome })
path := filepath.Join(env.home, ".beacon", "endpoint", shareauto.FileName)
- t.Setenv(shareauto.PathEnv, path)
prev := shareAutoCloud
shareAutoCloud = func() (bool, string) { return connected, mode }
t.Cleanup(func() { shareAutoCloud = prev })
diff --git a/docs/changelog/cli.mdx b/docs/changelog/cli.mdx
index b9e02845..412e9ef3 100644
--- a/docs/changelog/cli.mdx
+++ b/docs/changelog/cli.mdx
@@ -13,6 +13,8 @@ Stay up to date with the latest changes to the Agent Beacon CLI.
Changes on `main` since v1.3.58. They ship in the next release.
+- **A repository cannot turn auto-share on through the hook environment** · The hooks read the auto-share setting from `~/.beacon/endpoint/share.json` in the home of the account they run as, looked up from the account (the passwd entry, or the Windows process token), not from `$HOME` or `USERPROFILE`, so a repository that sets `HOME` for its hooks cannot plant a `share.json` that publishes its sessions. On Linux, an account that is not in `/etc/passwd` (LDAP, SSSD) still falls back to `$HOME`. `beacon share auto` writes the same file, and the restricted-log spool also moved to the account's home. The cloud-sandbox shuttle now uploads on Stop and SessionEnd only with `BEACON_ORIGIN=cloud`, which the cloud agent setups already set; `BEACON_CLOUD_*` without it does nothing.
+
- **Fleet package installs turn the local dashboard off** · An unattended first install of the `.deb` or `.rpm`, the macOS `.pkg` or the Windows MSI (Salt, Ansible, an MDM, a silent `msiexec`) now turns the local dashboard off for every account on the machine, root included, as `BEACON_DASHBOARD=disabled` already did: that install sets up the machine for the people who use it, and they should not be able to open a view of its telemetry unless an administrator says so. A person who installs the package on their own machine (`sudo` at a terminal, including the one-line `install.sh`, Installer.app, or the MSI's full interface) keeps the dashboard, by the same test that adds them to `beacon-log`. Upgrades, and reinstalls over a kept config or log, keep the setting an endpoint already has, so set `BEACON_DASHBOARD=disabled` to turn it off on machines installed from an earlier release. `BEACON_DASHBOARD=enabled` keeps it on an unattended install, and `sudo beacon endpoint repair --system --dashboard enabled` turns it back on. Installs from the `beacon` CLI are unchanged. See [Turning the dashboard off](/cli/dashboard#turning-the-dashboard-off).
## November 2026
diff --git a/pkg/asymptoteobserve/accounthome/accounthome.go b/pkg/asymptoteobserve/accounthome/accounthome.go
new file mode 100644
index 00000000..61d9ba00
--- /dev/null
+++ b/pkg/asymptoteobserve/accounthome/accounthome.go
@@ -0,0 +1,9 @@
+// Package accounthome resolves the home directory of the account this process runs as, from the
+// account itself rather than $HOME or USERPROFILE. The environment can come from somewhere else: a
+// repository can supply a hook's environment, and a process started under other credentials can
+// inherit its parent's. Use it where a path under the home decides what Beacon trusts or where an
+// account's own files go; it cannot be redirected by changing the environment.
+package accounthome
+
+// Dir is the home directory of the account this process runs as. Tests replace it.
+var Dir = lookup
diff --git a/pkg/asymptoteobserve/accounthome/accounthome_other.go b/pkg/asymptoteobserve/accounthome/accounthome_other.go
new file mode 100644
index 00000000..d0352be1
--- /dev/null
+++ b/pkg/asymptoteobserve/accounthome/accounthome_other.go
@@ -0,0 +1,19 @@
+//go:build !windows
+
+package accounthome
+
+import (
+ "os"
+ "os/user"
+ "strings"
+)
+
+// lookup reads the home directory from the account database (the passwd entry). Beacon builds
+// without cgo, so on Linux Go reads /etc/passwd only: an account that is listed only in a directory
+// service (LDAP, SSSD) or not at all falls back to $HOME, as does any lookup failure.
+func lookup() (string, error) {
+ if u, err := user.Current(); err == nil && strings.TrimSpace(u.HomeDir) != "" {
+ return u.HomeDir, nil
+ }
+ return os.UserHomeDir()
+}
diff --git a/pkg/asymptoteobserve/accounthome/accounthome_windows.go b/pkg/asymptoteobserve/accounthome/accounthome_windows.go
new file mode 100644
index 00000000..d8da6617
--- /dev/null
+++ b/pkg/asymptoteobserve/accounthome/accounthome_windows.go
@@ -0,0 +1,18 @@
+//go:build windows
+
+package accounthome
+
+import (
+ "os"
+
+ "golang.org/x/sys/windows"
+)
+
+// lookup reads the profile directory from the process token. USERPROFILE is the fallback when the
+// token has no profile loaded.
+func lookup() (string, error) {
+ if dir, err := windows.GetCurrentProcessToken().GetUserProfileDirectory(); err == nil && dir != "" {
+ return dir, nil
+ }
+ return os.UserHomeDir()
+}
diff --git a/pkg/asymptoteobserve/localingest/deliver.go b/pkg/asymptoteobserve/localingest/deliver.go
index 06aeb2c5..5d460ae8 100644
--- a/pkg/asymptoteobserve/localingest/deliver.go
+++ b/pkg/asymptoteobserve/localingest/deliver.go
@@ -12,14 +12,17 @@ import (
"time"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/accounthome"
)
// spoolDirInHome is where a writer's spools live, under its account's home directory.
const spoolDirInHome = ".beacon/endpoint/spool"
-// DefaultSpoolDir is the current account's spool directory, or "" when it has no home.
+// DefaultSpoolDir is the current account's spool directory, or "" when it has no home. The home
+// comes from the account, not the environment: a process started under other credentials can
+// inherit its parent's HOME or USERPROFILE, which would point the spool at a home it cannot write.
func DefaultSpoolDir() string {
- home, err := accountHome()
+ home, err := accounthome.Dir()
if err != nil || home == "" {
return ""
}
diff --git a/pkg/asymptoteobserve/localingest/deliver_unix_test.go b/pkg/asymptoteobserve/localingest/deliver_unix_test.go
index 63e56517..6d055be6 100644
--- a/pkg/asymptoteobserve/localingest/deliver_unix_test.go
+++ b/pkg/asymptoteobserve/localingest/deliver_unix_test.go
@@ -8,6 +8,7 @@ import (
"io/fs"
"net"
"os"
+ "os/user"
"path/filepath"
"sync"
"testing"
@@ -463,10 +464,16 @@ func TestWriteOutcome(t *testing.T) {
}
}
-func TestDefaultSpoolDir(t *testing.T) {
- home := t.TempDir()
- t.Setenv("HOME", home)
- if got, want := DefaultSpoolDir(), filepath.Join(home, ".beacon", "endpoint", "spool"); got != want {
- t.Fatalf("DefaultSpoolDir = %q, want %q", got, want)
+// The spool is under the home of the account the process runs as, whatever HOME says: a process
+// started under other credentials (sudo without -H) can inherit its parent's HOME, and a spool there
+// is one this account should not write.
+func TestTheSpoolIsInTheAccountsHomeNotHOME(t *testing.T) {
+ u, err := user.Current()
+ if err != nil || u.HomeDir == "" {
+ t.Skipf("this account has no home in the account database: %v", err)
+ }
+ t.Setenv("HOME", t.TempDir())
+ if got, want := DefaultSpoolDir(), filepath.Join(u.HomeDir, ".beacon", "endpoint", "spool"); got != want {
+ t.Fatalf("DefaultSpoolDir = %q, want %q under the account's home", got, want)
}
}
diff --git a/pkg/asymptoteobserve/localingest/privilege_unix.go b/pkg/asymptoteobserve/localingest/privilege_unix.go
index 94c4ad84..999bc9ca 100644
--- a/pkg/asymptoteobserve/localingest/privilege_unix.go
+++ b/pkg/asymptoteobserve/localingest/privilege_unix.go
@@ -6,6 +6,3 @@ import "os"
// processPrivileged reports whether this process is root.
func processPrivileged() bool { return os.Geteuid() == 0 }
-
-// accountHome is the home directory the spool lives under.
-func accountHome() (string, error) { return os.UserHomeDir() }
diff --git a/pkg/asymptoteobserve/localingest/privilege_windows.go b/pkg/asymptoteobserve/localingest/privilege_windows.go
index 031ba429..f9384b23 100644
--- a/pkg/asymptoteobserve/localingest/privilege_windows.go
+++ b/pkg/asymptoteobserve/localingest/privilege_windows.go
@@ -2,11 +2,7 @@
package localingest
-import (
- "os"
-
- "golang.org/x/sys/windows"
-)
+import "golang.org/x/sys/windows"
// processPrivileged reports whether this process may write the restricted log itself: it runs as
// SYSTEM, or its token has Administrators enabled. Those are the accounts the log's ACL gives
@@ -28,14 +24,3 @@ func processPrivileged() bool {
member, err := windows.Token(0).IsMember(admins)
return err == nil && member
}
-
-// accountHome is the profile directory of the account this process runs as, read from its token
-// rather than USERPROFILE: a process started under other credentials can inherit its parent's
-// environment (Start-Process -Credential does), which would point the spool at a profile this
-// account cannot write. USERPROFILE is the fallback when the token has no profile loaded.
-func accountHome() (string, error) {
- if dir, err := windows.GetCurrentProcessToken().GetUserProfileDirectory(); err == nil && dir != "" {
- return dir, nil
- }
- return os.UserHomeDir()
-}
diff --git a/pkg/asymptoteobserve/localingest/transport_other.go b/pkg/asymptoteobserve/localingest/transport_other.go
index 11e79cb4..2373cae9 100644
--- a/pkg/asymptoteobserve/localingest/transport_other.go
+++ b/pkg/asymptoteobserve/localingest/transport_other.go
@@ -5,7 +5,6 @@ package localingest
import (
"errors"
"net"
- "os"
"time"
)
@@ -37,6 +36,3 @@ func temporaryAcceptError(error) bool { return false }
// processPrivileged is never true here, where there is no ingest channel to bypass.
func processPrivileged() bool { return false }
-
-// accountHome is the home directory the spool lives under.
-func accountHome() (string, error) { return os.UserHomeDir() }
diff --git a/pkg/asymptoteobserve/shareauto/shareauto.go b/pkg/asymptoteobserve/shareauto/shareauto.go
index 89986a6d..8a7d974a 100644
--- a/pkg/asymptoteobserve/shareauto/shareauto.go
+++ b/pkg/asymptoteobserve/shareauto/shareauto.go
@@ -23,6 +23,8 @@ import (
"path/filepath"
"runtime"
"strings"
+
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/accounthome"
)
const (
@@ -121,11 +123,12 @@ func DefaultPath() string {
return HookPath()
}
-// HookPath is the user file the hooks read: ~/.beacon/endpoint/share.json. It ignores
-// BEACON_SHARE_SETTINGS_FILE, because a hook's environment can come from the repository it runs
-// in, and a repository must never be able to turn auto-share on for itself.
+// HookPath is the user file the hooks read: ~/.beacon/endpoint/share.json, in the home of the
+// account the hook runs as. It ignores BEACON_SHARE_SETTINGS_FILE and $HOME, because a hook's
+// environment can come from the repository it runs in, and a repository must never be able to turn
+// auto-share on for itself.
func HookPath() string {
- home, err := os.UserHomeDir()
+ home, err := accounthome.Dir()
if err != nil || home == "" {
return ""
}
diff --git a/pkg/asymptoteobserve/shareauto/shareauto_test.go b/pkg/asymptoteobserve/shareauto/shareauto_test.go
index cad2d907..a238fa1a 100644
--- a/pkg/asymptoteobserve/shareauto/shareauto_test.go
+++ b/pkg/asymptoteobserve/shareauto/shareauto_test.go
@@ -53,6 +53,21 @@ func TestResolve(t *testing.T) {
}
}
+// A repository can put HOME in the hook environment. HookPath follows the account, so moving HOME
+// (or USERPROFILE) does not move it, and a share.json planted under the forged home is never read.
+func TestHookPathIgnoresForgedHome(t *testing.T) {
+ before := HookPath()
+ if before == "" {
+ t.Skip("this account has no home directory")
+ }
+ forged := t.TempDir()
+ t.Setenv("HOME", forged)
+ t.Setenv("USERPROFILE", forged)
+ if got := HookPath(); got != before || strings.HasPrefix(got, forged) {
+ t.Fatalf("HookPath with a forged HOME = %q, want %q", got, before)
+ }
+}
+
func TestForHookAndSave(t *testing.T) {
path := filepath.Join(t.TempDir(), "endpoint", FileName)
if d := ForHook(path, "/x"); d.Enabled {