Files
zero/internal/sandbox/command_prefix_test.go
beardthelion 6edf9a8b78 fix(sandbox): normalize launcher names before the command-prefix denylist (#934)
* fix(sandbox): retire recorded approvals through a policy bump

Ahead of tightening which command prefixes may be approved, make the
upgrade path safe. A grant already on disk that the narrower rules
reject fails validation on read, and that path rejects the entire grants
file rather than the one entry. Every caller treats a read error as "no
grant" (engine.go LookupCommandPrefix, ApprovedCommandPrefixes, and the
Lookup in Evaluate), so one stale prefix would silently disable the
user's persisted deny grants too, with the error text never reaching
them.

Bump the approval policy version instead and let the store's existing
migration handle it: it backs the file up, keeps deny grants,
invalidates prior approvals, and reports the counts once through the
startup notice that both frontends already print. That is what the
mechanism was built for.

The v1-migration test pinned the policy version as a literal, so it now
derives the expected value from the constant and will not need editing
on the next bump.

* fix(sandbox): normalize launcher names before the command-prefix denylist

unsafeCommandPrefixLauncher matched the program name as written, so
python3.11, python2.7, python.exe, node.exe and git.exe all validated as
ordinary commands and could be persisted as command-prefix grants.
Lookup is a string-prefix match, so a single approval then auto-allowed
every later invocation: granting [python3.11 -c] covers
`python3.11 -c "import os; os.system(...)"` with no further prompt.

internal/agent's commandName already normalizes case and Windows
executable extensions for the allow side of the same decision; the deny
side did not, and the two disagreeing is the bug. Reduce the program to
the launcher it actually runs and match on that: executable extension,
Windows' trailing dots and spaces (python. starts python.exe), CPython
ABI flags, then a trailing version, testing the digit-stripped form as
well so python3.11 and python3 both arrive as python. A name is only
ever narrowed toward an existing entry, so the failure direction is an
extra permission prompt, never a silent grant. Digits without a
separator stay part of the name, which keeps base64, 7z and sha256sum
grantable.

Two Windows spellings cannot be resolved by name at all, so the shape is
refused alongside a path: 8.3 short names truncate the stem, which makes
POWERS~1.EXE unrecognizable, and python.exe::$DATA reaches the same
executable through its default stream. Normalizing the first token
before the banned-suggestion comparison keeps git.exe at parity with
git.

The list also gains launchers of the same class it already covers and
that normalization cannot reach: cmd, wsl, the remaining POSIX shells,
busybox, uv and uvx. Entries normalization now covers (/bin/bash,
/bin/zsh, powershell.exe, python3, pypy3) are gone; the first two were
already unreachable behind the path check above them.

Covers the persisted store, the session-scoped grants the agent takes
during a turn, the legacy-schema migration that prunes such a grant
per entry, and the Windows spellings hermetically.

* fix(sandbox): treat free-threaded CPython as a launcher too

The ABI flags came from the pre-3.13 set, so "python3.13t" and
"python3.13t.exe" normalized to themselves and stayed grantable as
command prefixes while the GIL build "python3.13" was refused. A
free-threaded interpreter runs arbitrary code just the same.

Add "t" to the flags trimmed under the existing digit guard, which is
what keeps ordinary names intact: "zstd" and "cat" have no digit beneath
the trimmed letters, so they keep their own names and stay grantable.

Reported by CodeRabbit on #934.

* fix(sandbox): refuse the remaining spellings of listed launchers

Same defect as the free-threaded build, found by sweeping the families
already on the list for names it cannot reach:

- nodejs is node on Debian and Ubuntu, and was grantable.
- python3-dbg, python3.11-dbg and python3.13t-dbg are interpreters that
  run arbitrary code; so are bash-static, pwsh-preview and
  node-nightly. A closed set of build-channel suffixes now normalizes
  them. The set stays closed deliberately: a general "-word" strip would
  also swallow node-gyp, python3-config and ruby-lsp, which are ordinary
  tools a user should still be able to approve, and those are pinned as
  must-stay-grantable.
- pnpm, yarn and bunx run package scripts exactly as npm and npx do, and
  npm and npx are already refused.
- sudoedit escalates like sudo.

The launcher-wrapper family (winpty, chroot, unshare, nsenter, parallel,
script, unbuffer, taskset, flock) has the same inconsistency against the
env/nohup/timeout/setsid entries already on the list, and is left for a
separate decision rather than widened here.

* test(sandbox): cover the -debug and -beta launcher suffixes

Four of the six build-channel suffixes had regression cases; -debug and
-beta did not, so removing either from the set left every test green.
Both now fail RED when their entry is dropped.

Reported by CodeRabbit on #934.
2026-08-21 12:50:11 +08:00

116 lines
3.9 KiB
Go

package sandbox
import "testing"
func TestUnsafeCommandPrefixLauncherIgnoresVersionAndExtension(t *testing.T) {
for _, program := range []string{
"python3", "python", "node", "git", "sh", "bash", "sudo",
"python3.11", "python3.12", "python2.7", "python3.13.1",
"python.exe", "python3.exe", "node.exe", "git.exe", "bash.exe",
"PYTHON3.11", "Node.EXE",
"cmd", "cmd.exe", "wsl", "wsl.exe",
// Windows resolves these to the same executable.
"python.", "python3.", "python3.11.", "python.exe.", "PYTHON.",
"python.cmd", "python.bat", "python.com", "python.ps1",
"PYTHON~1.EXE", "POWERS~1.EXE", "python.exe::$DATA",
// Versioned and ABI-suffixed interpreters.
"python3.7m", "python3.6dm", "pythonw3.11", "python-3.11",
// Free-threaded CPython (PEP 703) ships beside the GIL build.
"python3.13t", "python3.13t.exe", "python3.14t", "python3.13td",
// Distribution and build-channel spellings of the same interpreter.
"nodejs", "nodejs.exe", "node-nightly", "python3-dbg", "python3.11-dbg",
"python3.13t-dbg", "python3-debug", "python3.11-debug", "node-beta",
"pwsh-preview", "bash-static", "sudoedit",
// Twins of npm and npx, which the list already refuses.
"pnpm", "yarn", "bunx", "pnpm.cmd", "yarn.exe",
"perl5.36", "ruby3.2", "php8", "lua5.4", "node22.exe", "bash5",
} {
if !unsafeCommandPrefix([]string{program}) {
t.Errorf("unsafeCommandPrefix([%q]) = false, want true", program)
}
}
}
func TestUnsafeCommandPrefixKeepsOrdinaryCommandsGrantable(t *testing.T) {
for _, prefix := range [][]string{
{"cargo", "build"},
{"go", "test"},
{"ls", "-la"},
{"rg", "--json"},
{"7z", "l"},
{"base64", "-d"},
{"kubectl", "get", "pods"},
{"docker", "ps"},
{"sha256sum", "file"},
{"gcc-13", "-c"},
{"node-gyp", "build"},
{"python3-config", "--includes"},
{"s3cmd", "ls"},
{"cat", "file"},
{"zstd", "-d"},
{"sqlite3", "db"},
{"yt-dlp", "url"},
{"perl-doc", "-h"},
{"php-fpm", "-t"},
{"ruby-lsp", "--version"},
{"lua-language-server", "--check"},
{"nodemon", "app.js"},
} {
if unsafeCommandPrefix(prefix) {
t.Errorf("unsafeCommandPrefix(%q) = true, want false", prefix)
}
}
}
func TestNormalizeLauncherName(t *testing.T) {
for _, testCase := range []struct{ in, want string }{
{"python3.11", "python3"},
{"python2.7", "python2"},
{"pypy3", "pypy3"},
{"node.exe", "node"},
{"POWERSHELL.EXE", "powershell"},
{"7z", "7z"},
{"base64", "base64"},
{"sha256sum", "sha256sum"},
{"python.", "python"},
{"python3.11.exe.", "python3"},
{"python3.7m", "python3"},
{"python3.6dm", "python3"},
{"python3.13t", "python3"},
{"python3.13td", "python3"},
{"python3.11-dbg", "python3"},
{"node-nightly", "node"},
{"node-gyp", "node-gyp"},
{"python3-config", "python3-config"},
{"zstd", "zstd"},
{"cat", "cat"},
{"cargo", "cargo"},
{"", ""},
} {
if got := normalizeLauncherName(testCase.in); got != testCase.want {
t.Errorf("normalizeLauncherName(%q) = %q, want %q", testCase.in, got, testCase.want)
}
}
}
// Session-scoped grants are the agent's other entry point (loop.go grants them
// for a single turn without touching the store), so they must refuse the same
// launcher spellings the persisted path does.
func TestGrantCommandPrefixForSessionRefusesLauncherVariants(t *testing.T) {
engine := &Engine{commandPrefixes: newCommandPrefixGrantSet()}
engine.GrantCommandPrefixForSession("bash", []string{"cargo", "build"})
if _, ok := engine.LookupCommandPrefixForSession("bash", []string{"cargo", "build", "--release"}); !ok {
t.Fatal("CONTROL BROKEN: an ordinary session prefix grant did not match")
}
for _, prefix := range [][]string{
{"python3.11", "-c"},
{"python.exe", "-c"},
{"cmd", "/c"},
} {
engine.GrantCommandPrefixForSession("bash", prefix)
command := append(append([]string(nil), prefix...), "whatever")
if _, ok := engine.LookupCommandPrefixForSession("bash", command); ok {
t.Errorf("session grant %q matched %q, want refused", prefix, command)
}
}
}