am-i-done 0.4.0 → 0.4.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/rules/fixtures/no-raw-temp__paths.spec.ts.fixture +6 -0
- package/src/rules/fixtures/no-raw-temp__raw.spec.ts.fixture +5 -2
- package/src/rules/fixtures/shell-injection__safe.ts.fixture +4 -0
- package/src/rules/fixtures/shell-injection__template.ts.fixture +3 -1
- package/src/rules/no-raw-temp.rule.ts +13 -1
- package/src/rules/shell-injection.rule.ts +4 -3
package/package.json
CHANGED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { expect, it } from "bun:test";
|
|
2
|
+
it("treats /tmp paths as data", () => {
|
|
3
|
+
expect(batchRoot("/tmp/x/om-1")).toEqual({ root: "/tmp/x/om-1" });
|
|
4
|
+
expect(parseArgs(["--out", "/tmp/x"])).toEqual({ out: "/tmp/x" });
|
|
5
|
+
expect(message).toBe('ENOENT reading "/tmp/x/src/z.spec.ts"');
|
|
6
|
+
});
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
// @expect
|
|
1
|
+
// @expect 10
|
|
2
2
|
import { mkdtempSync } from "node:fs";
|
|
3
3
|
import os, { tmpdir as tmp } from "node:os";
|
|
4
4
|
const a = mkdtempSync(join(os.tmpdir(), "x-"));
|
|
5
5
|
const b = await fs.promises.mkdtemp("y");
|
|
6
6
|
using c = fs.mkdtempDisposableSync(tmp());
|
|
7
|
-
|
|
7
|
+
writeFileSync("/tmp/fixed/out.json", "{}");
|
|
8
|
+
await Bun.write(`/tmp/${name}.txt`, data);
|
|
9
|
+
Bun.spawn(["make"], { cwd: "/tmp/build" });
|
|
10
|
+
Bun.spawn(["make"], { env: { TMPDIR: "/tmp" } });
|
|
8
11
|
await $`mktemp -d`;
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
// @expect
|
|
1
|
+
// @expect 8
|
|
2
2
|
execSync(`git log ${ref}`);
|
|
3
3
|
exec( `rm -rf ${dir}`, cb);
|
|
4
4
|
execSync("rm -rf " + dir);
|
|
5
5
|
spawn("sh", ["-c", `ls ${dir}`]);
|
|
6
6
|
Bun.spawn(["bash", "-c", "cat " + file]);
|
|
7
7
|
execFile("/bin/sh", ["-c", `echo ${x}`]);
|
|
8
|
+
cp.execSync(`rm -rf ${dir}`);
|
|
9
|
+
child_process.exec("ls " + dir);
|
|
@@ -6,7 +6,18 @@ import { position, type Rule, snippetAt } from "../engine/rule.ts";
|
|
|
6
6
|
const OWNER = join(import.meta.dir, "..", "tmp.ts");
|
|
7
7
|
const CALLS = /\b(?:mkdtemp|mkdtempSync|mkdtempDisposable|mkdtempDisposableSync|tmpdir)\s*\(/g;
|
|
8
8
|
const ALIAS = /\btmpdir\s+as\s+([A-Za-z_$][\w$]*)/g;
|
|
9
|
-
|
|
9
|
+
/**
|
|
10
|
+
* A `/tmp/...` literal is a temp file only where something is made there: handed to a call that
|
|
11
|
+
* writes, or set as a child's working or temp directory. Elsewhere it is just a path (a test input,
|
|
12
|
+
* an expected value), and nothing is created. A path held in a variable and written later is
|
|
13
|
+
* beyond a pattern.
|
|
14
|
+
*/
|
|
15
|
+
const SINKS =
|
|
16
|
+
"mkdir|mkdirSync|writeFile|writeFileSync|appendFile|appendFileSync|open|openSync|createWriteStream|cp|cpSync|copyFile|copyFileSync|rename|renameSync|symlink|symlinkSync|Bun\\.write";
|
|
17
|
+
const LITERALS = new RegExp(
|
|
18
|
+
`\\b(?:${SINKS})\\s*\\(\\s*["'\`]\\/tmp\\/|\\b(?:cwd|TMPDIR|TMP|TEMP)\\s*:\\s*["'\`]\\/tmp\\b|\\bmktemp\\b`,
|
|
19
|
+
"g",
|
|
20
|
+
);
|
|
10
21
|
|
|
11
22
|
/**
|
|
12
23
|
* Temp directories made by hand leak: a spec that throws before its cleanup, or a run that is
|
|
@@ -25,6 +36,7 @@ export default {
|
|
|
25
36
|
"Code that runs outside specs uses the same helper; what a scope misses goes at exit, and a killed process's leftovers on the next run.",
|
|
26
37
|
'Add `preload = ["am-i-done/tmp/preload"]` under [test] in bunfig.toml to sweep at the start and end of every test run.',
|
|
27
38
|
"A child process that needs a temp dir gets one passed in (its TMPDIR set to `dir.path`), not a hard-coded /tmp path.",
|
|
39
|
+
"A project with a temp helper of its own exempts that one file: // doing-it-wrong-ignore-file no-raw-temp: <why> (#123)",
|
|
28
40
|
],
|
|
29
41
|
check(ctx) {
|
|
30
42
|
if (ctx.file.path === OWNER) return ctx.skipped();
|
|
@@ -5,10 +5,11 @@ export default {
|
|
|
5
5
|
type: "pattern",
|
|
6
6
|
category: "conventions",
|
|
7
7
|
// A command string built from a value, by interpolation or `+`, handed to `exec`/`execSync`, or to
|
|
8
|
-
// a shell's `-c` through `spawn`, `execFile` or `Bun.spawn`.
|
|
9
|
-
//
|
|
8
|
+
// a shell's `-c` through `spawn`, `execFile` or `Bun.spawn`. `exec` as a method is someone else's
|
|
9
|
+
// (`db.exec`, `regex.exec`) unless the receiver is child_process by a usual name. A string built
|
|
10
|
+
// earlier and passed by name is beyond a pattern.
|
|
10
11
|
pattern:
|
|
11
|
-
|
|
12
|
+
/(?:(?<![.\w$])|(?<=\b(?:child_process|childProcess|cp)\.))(?:exec|execSync)\s*\(\s*(?:`[^`]*\$\{|["'][^"'\n]*["']\s*\+)|\b(?:spawn|spawnSync|execFile|execFileSync|Bun\.spawn|Bun\.spawnSync)\s*\(\s*\[?\s*["'](?:\/bin\/)?(?:ba|z|da)?sh["']\s*,\s*\[?\s*["']-c["']\s*,\s*(?:`[^`]*\$\{|["'][^"'\n]*["']\s*\+)/g,
|
|
12
13
|
scope: "all",
|
|
13
14
|
scold: "A value interpolated into a shell command string runs as shell if it contains shell syntax.",
|
|
14
15
|
guidance: [
|