Skip to content

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

fix(windows): quote the grant hint so it survives being pasted

shukieshah759a1972ab6f
diff --git a/cli/beacon/internal/endpoint/writer/acl_windows.go b/cli/beacon/internal/endpoint/writer/acl_windows.go
index 14b07c4e..db956406 100644
--- a/cli/beacon/internal/endpoint/writer/acl_windows.go
+++ b/cli/beacon/internal/endpoint/writer/acl_windows.go
@@ -60,17 +60,41 @@ func grantArgs(dir string) []string {
//
// Empty on platforms with no such grant, which is how callers tell that this remediation does not
// apply rather than printing a Windows command on a Mac.
+//
+// Quoting matters here in a way it does not for the grant itself: applying it goes through argv
+// with no shell involved, while this is read by a person and pasted into whichever shell they have
+// open. The rights string carries parentheses, which PowerShell reads as grouping, so an unquoted
+// hint is a command that fails on paste -- and a remediation that fails on paste is worse than
+// none, because the operator concludes the diagnosis was wrong.
func GrantCommandHint(dir string) string {
- args := grantArgs(dir)
- quoted := make([]string, 0, len(args)+1)
- quoted = append(quoted, "icacls")
- for _, arg := range args {
- if strings.ContainsAny(arg, " \t") {
- arg = `"` + arg + `"`
+ parts := make([]string, 0, len(grantArgs(dir))+1)
+ parts = append(parts, "icacls")
+ for _, arg := range grantArgs(dir) {
+ parts = append(parts, quoteForShellPaste(arg))
+ }
+ return strings.Join(parts, " ")
+}
+
+// quoteForShellPaste wraps anything either Windows shell would not pass through literally.
+//
+// An allowlist rather than a list of metacharacters: cmd.exe and PowerShell disagree about which
+// characters are special, and the set that is safe in both is small and easy to state. Double
+// quotes work in both for everything here, and no argument in this command contains one, so simple
+// wrapping is enough -- if that ever changes this needs the escaping rules too, not just the quotes.
+func quoteForShellPaste(arg string) string {
+ safe := func(r rune) bool {
+ switch {
+ case r >= 'a' && r <= 'z', r >= 'A' && r <= 'Z', r >= '0' && r <= '9':
+ return true
+ }
+ return strings.ContainsRune(`\/.:_-`, r)
+ }
+ for _, r := range arg {
+ if !safe(r) {
+ return `"` + arg + `"`
}
- quoted = append(quoted, arg)
}
- return strings.Join(quoted, " ")
+ return arg
}
// Rights that answer the question doctor is actually asking: can a hook running as the logged-on
diff --git a/cli/beacon/internal/endpoint/writer/acl_windows_test.go b/cli/beacon/internal/endpoint/writer/acl_windows_test.go
index 8e541334..2ef0bba6 100644
--- a/cli/beacon/internal/endpoint/writer/acl_windows_test.go
+++ b/cli/beacon/internal/endpoint/writer/acl_windows_test.go
@@ -176,9 +176,11 @@ func TestGrantCommandHintQuotesThePathItPrints(t *testing.T) {
if !strings.Contains(hint, `"C:\Program Files\Beacon\logs"`) {
t.Fatalf("GrantCommandHint left a path containing a space unquoted: %s", hint)
}
- // The SID rather than a localized name, for the same reason the grant itself uses it.
- if !strings.Contains(hint, "S-1-5-4") {
- t.Fatalf("GrantCommandHint does not name the well-known SID: %s", hint)
+ // The rights string carries parentheses, which PowerShell reads as grouping rather than as
+ // part of an argument. Unquoted, the hint is a command that fails the moment it is pasted.
+ if !strings.Contains(hint, `"*S-1-5-4:(OI)(CI)(M)"`) {
+ t.Fatalf("GrantCommandHint left the rights string unquoted: %s; "+
+ "PowerShell would try to evaluate the parentheses", hint)
}
// Same definition as the applied grant, so the advice cannot drift from the implementation.
for _, arg := range grantArgs(`C:\Program Files\Beacon\logs`)[1:] {
@@ -186,4 +188,31 @@ func TestGrantCommandHintQuotesThePathItPrints(t *testing.T) {
t.Fatalf("GrantCommandHint omits %q from the grant it claims to describe: %s", arg, hint)
}
}
+ // Plain flags stay readable: quoting everything would work but makes the line harder to scan,
+ // and this is read before it is run.
+ if !strings.Contains(hint, " /grant ") || !strings.HasSuffix(hint, " /T") {
+ t.Fatalf("GrantCommandHint quoted arguments that needed no quoting: %s", hint)
+ }
+}
+
+func TestQuoteForShellPasteCoversWhatBothWindowsShellsTreatSpecially(t *testing.T) {
+ // Safe in cmd.exe and PowerShell alike, so left alone.
+ for _, arg := range []string{`/T`, `/grant`, `C:\ProgramData\Beacon\logs`, `icacls`, `S-1-5-4`} {
+ if got := quoteForShellPaste(arg); got != arg {
+ t.Fatalf("quoteForShellPaste(%q) = %q, want it unchanged", arg, got)
+ }
+ }
+ // Each of these is special to at least one of the two shells.
+ for _, arg := range []string{
+ `*S-1-5-4:(OI)(CI)(M)`,
+ `C:\Program Files\Beacon`,
+ `C:\logs&more`,
+ `C:\logs;more`,
+ `C:\logs$more`,
+ `C:\logs'more`,
+ } {
+ if got := quoteForShellPaste(arg); got != `"`+arg+`"` {
+ t.Fatalf("quoteForShellPaste(%q) = %q, want it quoted", arg, got)
+ }
+ }
}