The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu
Go agent-ai-cliagent-harnessesagent-securitybrowser-extensionciclaude-codecloudcodexcursordetection-engineeringjevknowledge-basememorymobile-device-managementobservabilitysecuritysecurity-information-and-event-managementsecurity-toolstelemetrytraces
fix(hooks): keep the platform of hooks installed under a path with an apostrophe (#1075)
Rudy Mizrahi Celeklib14d151ca2b9
diff --git a/cli/beacon/internal/endpoint/hooks/command_parse_test.go b/cli/beacon/internal/endpoint/hooks/command_parse_test.goindex 90f772c0..3aa22343 100644--- a/cli/beacon/internal/endpoint/hooks/command_parse_test.go+++ b/cli/beacon/internal/endpoint/hooks/command_parse_test.go@@ -51,6 +51,12 @@ func TestCommandFieldsKeepsQuotedPathsWhole(t *testing.T) {command: `C:\beacon\beacon-hooks.exe --log C:\logs\runtime.jsonl`,want: []string{`C:\beacon\beacon-hooks.exe`, "--log", `C:\logs\runtime.jsonl`},},+ {+ // hookCommandQuote writes a single quote inside a value as \' between two quoted runs.+ name: "a single quote escaped the way hookCommandQuote writes it",+ command: `'C:\Users\O'\''Brien\hooks\beacon-hooks.exe' --platform claude`,+ want: []string{`C:\Users\O'Brien\hooks\beacon-hooks.exe`, "--platform", "claude"},+ },{name: "empty",command: " ",@@ -115,6 +121,22 @@ func TestAPlatformNamedWithAnEqualsSignCountsAsNamingOne(t *testing.T) {}}+// A profile directory with an apostrophe (C:\Users\O'Brien) puts \' between two quoted runs in every+// path of the command. Read as an unterminated quote, it swallowed --platform, and the hook matched+// every runtime.+func TestAHookUnderAnApostrophePathKeepsItsPlatform(t *testing.T) {+ for _, home := range []string{`C:\Users\O'Brien`, `/home/o'brien`} {+ command := endpointCommandPrefix("claude", home+`/.beacon/endpoint/hooks/beacon-hooks`,+ home+`/.beacon/endpoint/logs/runtime.jsonl`, home+`/.beacon/endpoint/config.json`)+ if !isEndpointHookCommand(command, "claude") {+ t.Errorf("%s: not matched for claude", command)+ }+ if isEndpointHookCommand(command, "cursor") {+ t.Errorf("%s: a claude hook matched when asked about cursor", command)+ }+ }+}+func TestAPlatformlessBeaconHookStillMatchesAnyRuntime(t *testing.T) {// Written before --platform existed. Repair has to keep finding these or it will leave two.if !isEndpointHookCommand(`/opt/beacon/hooks/beacon-hooks pre-tool`, "cursor") {@@ -192,6 +214,9 @@ func TestEndpointCommandArgsMatchPrefix(t *testing.T) {{name: "no log", binaryPath: "/opt/beacon/hooks/beacon-hooks", configPath: "/etc/beacon/config.json"},{name: "no config", binaryPath: "/opt/beacon/hooks/beacon-hooks", logPath: "/var/log/beacon/runtime.jsonl"},{name: "windows paths with spaces", binaryPath: `C:\Program Files\Beacon\beacon-hooks.exe`, logPath: `C:\ProgramData\Beacon\runtime.jsonl`, configPath: `C:\ProgramData\Beacon\config.json`},+ {name: "apostrophes", binaryPath: `C:\Users\O'Brien\.beacon\beacon-hooks.exe`, logPath: `/home/o'brien/it's/runtime.jsonl`, configPath: `/home/o'brien/'quoted'/config.json'`},+ {name: "shell metacharacters", binaryPath: "/opt/$HOME/`id`/beacon-hooks", logPath: `/var/log/a b;c|d&e/runtime.jsonl`, configPath: `/etc/"dq"/config.json`},+ {name: "unc and non-ascii", binaryPath: `\\server\share\Beacon\beacon-hooks.exe`, logPath: `/Users/zoë/日誌/runtime.jsonl`, configPath: `C:\ProgramData\Beacon\config.json`},}for _, tc := range cases {t.Run(tc.name, func(t *testing.T) {diff --git a/cli/beacon/internal/endpoint/hooks/runtime.go b/cli/beacon/internal/endpoint/hooks/runtime.goindex e87f1028..7af13b10 100644--- a/cli/beacon/internal/endpoint/hooks/runtime.go+++ b/cli/beacon/internal/endpoint/hooks/runtime.go@@ -309,7 +309,8 @@ func commandHasPlatform(command, platform string) bool {// are single-quoted and Windows ones double-quoted -- and this function only has to recover tokens,// not to reproduce a particular shell's semantics. It deliberately does not handle backslash escapes:// on Windows a backslash is a path separator, and treating it as an escape would corrupt every path-// this parses.+// this parses. The one exception is \' outside quotes: hookCommandQuote writes a single quote in a+// value by closing the quoted run, writing \' and reopening it.func commandFields(command string) []string {var (fields []string@@ -324,7 +325,9 @@ func commandFields(command string) []string {started = false}}- for _, r := range command {+ runes := []rune(command)+ for i := 0; i < len(runes); i++ {+ r := runes[i]switch {case quote != 0:if r == quote {@@ -332,6 +335,10 @@ func commandFields(command string) []string {break}current.WriteRune(r)+ case r == '\\' && i+1 < len(runes) && runes[i+1] == '\'':+ current.WriteRune('\'')+ started = true+ i++case r == '"' || r == '\'':// Marks the token as started even if the quotes turn out to be empty, so `--platform ""`// yields an empty value rather than dropping the argument and shifting every token after