@niwa-labs/proc 0.0.0-stage → 0.0.0
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/LICENSE +21 -0
- package/README.md +45 -2
- package/package.json +18 -4
- package/src/artifact.ts +225 -0
- package/src/box-rules.ts +40 -0
- package/src/flow.ts +146 -0
- package/src/index.ts +316 -0
- package/src/records.ts +184 -0
- package/src/refusal.ts +17 -0
- package/src/supervise.ts +152 -0
- package/test/artifact.test.ts +214 -0
- package/test/budget.test.ts +55 -0
- package/test/cancel.test.ts +81 -0
- package/test/exec.test.ts +154 -0
- package/test/flow.test.ts +107 -0
- package/test/grant.test.ts +136 -0
- package/test/live.test.ts +178 -0
- package/test/records.test.ts +46 -0
- package/test/refusal.test.ts +113 -0
- package/test/run.test.ts +273 -0
- package/test/supervise.test.ts +127 -0
- package/test/support.ts +178 -0
- package/tsconfig.json +6 -0
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// What a grant can say about a command: both actions are called at the real directory they run in,
|
|
2
|
+
// so a rule names a place, and a link cannot name a place the rule does not cover.
|
|
3
|
+
|
|
4
|
+
import { existsSync, realpathSync, rmSync, symlinkSync } from "node:fs";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { afterEach, expect, test } from "bun:test";
|
|
7
|
+
|
|
8
|
+
import { grant, isCallTarget, principalOf } from "@niwa-labs/core";
|
|
9
|
+
import { plugin } from "@niwa-labs/plugin";
|
|
10
|
+
import { connect } from "@niwa-labs/testing";
|
|
11
|
+
import type { NiwaApi } from "@niwa-labs/testing";
|
|
12
|
+
|
|
13
|
+
import { fileTarget, procExec, procRun } from "../src/index.ts";
|
|
14
|
+
import { dirIn, procHost, recordingSandbox } from "./support.ts";
|
|
15
|
+
import type { TestProcOptions } from "./support.ts";
|
|
16
|
+
|
|
17
|
+
const clients: NiwaApi[] = [];
|
|
18
|
+
const hosts: Array<Awaited<ReturnType<typeof procHost>>> = [];
|
|
19
|
+
afterEach(async () => {
|
|
20
|
+
for (const client of clients.splice(0)) await client.close().catch(() => {});
|
|
21
|
+
for (const host of hosts.splice(0)) await host.stop();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
async function served(options: TestProcOptions = { sandbox: recordingSandbox() }) {
|
|
25
|
+
const host = await procHost(options);
|
|
26
|
+
hosts.push(host);
|
|
27
|
+
const as = async (rules: string[]): Promise<NiwaApi> => {
|
|
28
|
+
const token = host.niwa.issueToken(principalOf("session", "caller"), { grant: grant(rules) });
|
|
29
|
+
const client = await connect({ home: host.home, id: "session:caller", token });
|
|
30
|
+
clients.push(client);
|
|
31
|
+
return client;
|
|
32
|
+
};
|
|
33
|
+
return { host, as };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
test("a grant on a directory lets a command run there and in no other directory", async () => {
|
|
37
|
+
const { host, as } = await served();
|
|
38
|
+
const here = realpathSync(dirIn(host.home, "here"));
|
|
39
|
+
const there = realpathSync(dirIn(host.home, "there"));
|
|
40
|
+
const caller = await as([`proc.run@${here}`]);
|
|
41
|
+
expect((await caller.call(procRun, { cmd: ["/usr/bin/true"], cwd: here, timeout: 10_000 })).exit).toBe(0);
|
|
42
|
+
await expect(caller.call(procRun, { cmd: ["/usr/bin/true"], cwd: there, timeout: 10_000 })).rejects.toThrow(/grant/);
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
test("a box wider than the host's default needs the caller's box rules, and the record names the rule that covered it", async () => {
|
|
46
|
+
const { host, as } = await served();
|
|
47
|
+
const here = realpathSync(dirIn(host.home, "here"));
|
|
48
|
+
const data = realpathSync(dirIn(host.home, "data"));
|
|
49
|
+
const box = { allow: ["bun", { read: data }] } as const;
|
|
50
|
+
const narrow = await as([`proc.run@${here}`]);
|
|
51
|
+
const refused = await narrow.call(procRun, { cmd: ["/usr/bin/true"], cwd: here, timeout: 10_000, box }).then(
|
|
52
|
+
() => undefined,
|
|
53
|
+
(error: unknown) => String(error),
|
|
54
|
+
);
|
|
55
|
+
expect(refused).toContain("box.use@bun");
|
|
56
|
+
expect(refused).toContain(`box.read@${data}`);
|
|
57
|
+
// Narrower than the default needs nothing.
|
|
58
|
+
expect((await narrow.call(procRun, { cmd: ["/usr/bin/true"], cwd: here, timeout: 10_000, box: { only: [] } })).exit).toBe(0);
|
|
59
|
+
|
|
60
|
+
const wide = await as([`proc.run@${here}`, "box.use@*", `box.read@${realpathSync(host.home)}/*`]);
|
|
61
|
+
const result = await wide.call(procRun, { cmd: ["/usr/bin/true"], cwd: here, timeout: 10_000, box });
|
|
62
|
+
expect(result.exit).toBe(0);
|
|
63
|
+
expect(result.box.rules).toEqual(["box.use@*", `box.read@${realpathSync(host.home)}/*`]);
|
|
64
|
+
const finished = (await host.niwa.records.read()).filter((row) => row.type === "proc.finished").at(-1);
|
|
65
|
+
expect((finished?.data as { box: { rules: string[] } }).box.rules).toEqual(result.box.rules);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
test("a link to a directory is the directory it leads to: the grant of that place covers it, and the link's own name does not", async () => {
|
|
69
|
+
const { host, as } = await served();
|
|
70
|
+
const other = realpathSync(dirIn(host.home, "other"));
|
|
71
|
+
const link = join(host.home, "link");
|
|
72
|
+
symlinkSync(other, link);
|
|
73
|
+
const byPlace = await as([`proc.run@${other}`]);
|
|
74
|
+
expect((await byPlace.call(procRun, { cmd: ["/usr/bin/true"], cwd: link, timeout: 10_000 })).exit).toBe(0);
|
|
75
|
+
const byLink = await as([`proc.run@${link}`]);
|
|
76
|
+
await expect(byLink.call(procRun, { cmd: ["/usr/bin/true"], cwd: link, timeout: 10_000 })).rejects.toThrow(/grant/);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test("a grant of run does not let a caller exec, and exec needs its own rule", async () => {
|
|
80
|
+
const { host, as } = await served();
|
|
81
|
+
const here = realpathSync(dirIn(host.home, "here"));
|
|
82
|
+
const runner = await as([`proc.run@${here}`]);
|
|
83
|
+
await expect(runner.call(procExec, { cmd: ["/usr/bin/true"], cwd: here, timeout: 10_000 })).rejects.toThrow(/grant/);
|
|
84
|
+
const execer = await as([`proc.exec@${here}`]);
|
|
85
|
+
expect((await execer.call(procExec, { cmd: ["/usr/bin/true"], cwd: here, timeout: 10_000 })).exit).toBe(0);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test("a link that is moved after the grant was checked does not move the command to a place the grant never covered", async () => {
|
|
89
|
+
const { host, as } = await served();
|
|
90
|
+
const here = realpathSync(dirIn(host.home, "here"));
|
|
91
|
+
const there = realpathSync(dirIn(host.home, "there"));
|
|
92
|
+
const link = join(host.home, "link");
|
|
93
|
+
symlinkSync(here, link);
|
|
94
|
+
// A guard runs after the grant was matched and before the command starts: the window of the race.
|
|
95
|
+
host.niwa.use(
|
|
96
|
+
plugin("mover", (api) => {
|
|
97
|
+
api.guard(procRun, () => {
|
|
98
|
+
rmSync(link);
|
|
99
|
+
symlinkSync(there, link);
|
|
100
|
+
return undefined;
|
|
101
|
+
});
|
|
102
|
+
}),
|
|
103
|
+
);
|
|
104
|
+
const caller = await as([`proc.run@${here}`]);
|
|
105
|
+
await expect(caller.call(procRun, { cmd: ["/usr/bin/touch", "ran"], cwd: link, timeout: 10_000 })).rejects.toThrow(/changed/);
|
|
106
|
+
expect(existsSync(join(there, "ran"))).toBe(false);
|
|
107
|
+
expect(existsSync(join(here, "ran"))).toBe(false);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
test("a directory with a space, a percent sign or a colon in its name is a target a rule can name", async () => {
|
|
111
|
+
const { host, as } = await served();
|
|
112
|
+
const plain = realpathSync(dirIn(host.home, "My Project"));
|
|
113
|
+
const odd = realpathSync(dirIn(host.home, "a%b:c@d*e"));
|
|
114
|
+
const other = realpathSync(dirIn(host.home, "other"));
|
|
115
|
+
|
|
116
|
+
const anywhere = await as(["proc.run"]);
|
|
117
|
+
expect((await anywhere.call(procRun, { cmd: ["/usr/bin/true"], cwd: plain, timeout: 10_000 })).exit).toBe(0);
|
|
118
|
+
expect((await anywhere.call(procRun, { cmd: ["/usr/bin/true"], cwd: odd, timeout: 10_000 })).exit).toBe(0);
|
|
119
|
+
|
|
120
|
+
const named = await as([`proc.run@${fileTarget(plain)}`]);
|
|
121
|
+
expect(fileTarget(plain)).toBe(`${realpathSync(host.home)}/My%20Project`);
|
|
122
|
+
expect((await named.call(procRun, { cmd: ["/usr/bin/true"], cwd: plain, timeout: 10_000 })).exit).toBe(0);
|
|
123
|
+
await expect(named.call(procRun, { cmd: ["/usr/bin/true"], cwd: other, timeout: 10_000 })).rejects.toThrow(/grant/);
|
|
124
|
+
await expect(named.call(procRun, { cmd: ["/usr/bin/true"], cwd: odd, timeout: 10_000 })).rejects.toThrow(/grant/);
|
|
125
|
+
|
|
126
|
+
const underHome = await as([`proc.run@${fileTarget(realpathSync(host.home))}/*`]);
|
|
127
|
+
expect((await underHome.call(procRun, { cmd: ["/usr/bin/true"], cwd: odd, timeout: 10_000 })).exit).toBe(0);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
test("a path with only plain segments is its own target, and an encoded one cannot be read as another place", () => {
|
|
131
|
+
expect(fileTarget("/work/repo-1/src.d")).toBe("/work/repo-1/src.d");
|
|
132
|
+
expect(fileTarget("/a b/c")).toBe("/a%20b/c");
|
|
133
|
+
expect(fileTarget("/a%20b")).toBe("/a%2520b");
|
|
134
|
+
expect(fileTarget("/a%20b")).not.toBe(fileTarget("/a b"));
|
|
135
|
+
expect(isCallTarget(fileTarget("/x/{y}/*/z@q:r s"))).toBe(true);
|
|
136
|
+
});
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// proc.run in the real sandboxes.
|
|
2
|
+
//
|
|
3
|
+
// Runs the real srt and nono, so it is behind `live`, and each part is skipped on a machine where
|
|
4
|
+
// that sandbox is not available.
|
|
5
|
+
//
|
|
6
|
+
// What it proves, for nono:
|
|
7
|
+
// 1. a run writes in its cwd and cannot write outside it
|
|
8
|
+
// 2. a run reaches no network
|
|
9
|
+
// 3. a run cannot read the owner token of the host, though the host's home holds it
|
|
10
|
+
// 4. a run cannot write a shared temp area, and the temp files of a command that follows TMPDIR still work
|
|
11
|
+
// 5. the box a run names carries nono's policy: its cwd to write, and neither a temp area nor the home to read
|
|
12
|
+
// 6. a run's HOME is the user's own, which it cannot write, and a program on the PATH under the user's
|
|
13
|
+
// home runs from the toolchains, and not when the call replaces them
|
|
14
|
+
// and for srt: proc refuses its additional writable directories.
|
|
15
|
+
|
|
16
|
+
import { existsSync, readFileSync, readdirSync, realpathSync, writeFileSync } from "node:fs";
|
|
17
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
18
|
+
import { homedir, tmpdir } from "node:os";
|
|
19
|
+
import { join } from "node:path";
|
|
20
|
+
import { describe, expect, test } from "bun:test";
|
|
21
|
+
|
|
22
|
+
import { nono, sandbox as sandboxHost, srt } from "@niwa-labs/sandbox";
|
|
23
|
+
import { live } from "@niwa-labs/testing";
|
|
24
|
+
|
|
25
|
+
import { procRun } from "../src/index.ts";
|
|
26
|
+
import { dirIn, outputOf, procHost } from "./support.ts";
|
|
27
|
+
|
|
28
|
+
for (const sandbox of [nono()]) {
|
|
29
|
+
const present = live && (await sandbox.available());
|
|
30
|
+
describe(`proc.run in ${sandbox.name}`, () => {
|
|
31
|
+
test.skipIf(!present)("writes in its cwd, not outside it, and reaches no network", async () => {
|
|
32
|
+
// A home under the user's home, as the host's is.
|
|
33
|
+
const host = await procHost({ sandbox }, mkdtempSync(join(homedir(), ".niwa-proc-home-")));
|
|
34
|
+
const outside = realpathSync(mkdtempSync(join(tmpdir(), "niwa-proc-out-")));
|
|
35
|
+
try {
|
|
36
|
+
const cwd = realpathSync(dirIn(host.home, "work"));
|
|
37
|
+
const inside = await host.niwa.call(procRun, { cmd: ["/bin/sh", "-c", "echo x > in.txt"], cwd, timeout: 20_000 });
|
|
38
|
+
expect(inside.exit).toBe(0);
|
|
39
|
+
// The box it names carries the policy nono applies: its own directory to write, and no temp area or home to read.
|
|
40
|
+
expect(inside.box.policy.write).toContain(cwd);
|
|
41
|
+
const read = "only" in inside.box.policy.read ? inside.box.policy.read.only : [];
|
|
42
|
+
expect(read).toContain(cwd);
|
|
43
|
+
for (const place of [realpathSync(tmpdir()), homedir(), realpathSync(host.home)]) {
|
|
44
|
+
expect(read.some((path) => place === path || place.startsWith(`${path}/`))).toBe(false);
|
|
45
|
+
}
|
|
46
|
+
expect(readFileSync(join(cwd, "in.txt"), "utf8")).toBe("x\n");
|
|
47
|
+
|
|
48
|
+
const denied = await host.niwa.call(procRun, { cmd: ["/bin/sh", "-c", `echo x > ${outside}/out.txt`], cwd, timeout: 20_000 });
|
|
49
|
+
expect(denied.exit).not.toBe(0);
|
|
50
|
+
expect(existsSync(join(outside, "out.txt"))).toBe(false);
|
|
51
|
+
|
|
52
|
+
const online = await host.niwa.call(procRun, {
|
|
53
|
+
cmd: ["/bin/sh", "-c", "curl -sS -m 8 -o /dev/null -w '%{http_code}' https://example.com"],
|
|
54
|
+
cwd,
|
|
55
|
+
timeout: 30_000,
|
|
56
|
+
});
|
|
57
|
+
expect(online.exit).not.toBe(0);
|
|
58
|
+
} finally {
|
|
59
|
+
rmSync(outside, { recursive: true, force: true });
|
|
60
|
+
await host.stop();
|
|
61
|
+
}
|
|
62
|
+
}, 90_000);
|
|
63
|
+
|
|
64
|
+
test.skipIf(!present)("has no shared temp area, and a command's temp files go inside its cwd", async () => {
|
|
65
|
+
const host = await procHost({ sandbox }, mkdtempSync(join(homedir(), ".niwa-proc-home-")));
|
|
66
|
+
const shared = join(realpathSync(tmpdir()), `niwa-proc-shared-${process.pid}-${Date.now()}`);
|
|
67
|
+
try {
|
|
68
|
+
const cwd = realpathSync(dirIn(host.home, "work"));
|
|
69
|
+
const refused = await host.niwa.call(procRun, { cmd: ["/bin/sh", "-c", `echo x > '${shared}'`], cwd, timeout: 20_000 });
|
|
70
|
+
expect(refused.exit).not.toBe(0);
|
|
71
|
+
expect(existsSync(shared)).toBe(false);
|
|
72
|
+
const made = await host.niwa.call(procRun, { cmd: ["/bin/sh", "-c", 'mktemp "$TMPDIR/file.XXXXXX"'], cwd, timeout: 20_000 });
|
|
73
|
+
expect(made.exit).toBe(0);
|
|
74
|
+
expect(outputOf(host, made.output).trim().startsWith(`${cwd}/`)).toBe(true);
|
|
75
|
+
expect(readdirSync(cwd)).toEqual([]);
|
|
76
|
+
} finally {
|
|
77
|
+
rmSync(shared, { force: true });
|
|
78
|
+
await host.stop();
|
|
79
|
+
}
|
|
80
|
+
}, 60_000);
|
|
81
|
+
|
|
82
|
+
test.skipIf(!present)("has the user's home, which it cannot write, and runs a program on the PATH from the toolchains", async () => {
|
|
83
|
+
const tools = realpathSync(mkdtempSync(join(homedir(), ".niwa-proc-tools-")));
|
|
84
|
+
writeFileSync(join(tools, "niwa-hello"), "#!/bin/sh\necho hello from the toolchain\n", { mode: 0o755 });
|
|
85
|
+
const saved = process.env["PATH"];
|
|
86
|
+
process.env["PATH"] = `${tools}:/usr/bin:/bin`;
|
|
87
|
+
const host = await procHost({ sandbox }, mkdtempSync(join(homedir(), ".niwa-proc-home-")));
|
|
88
|
+
try {
|
|
89
|
+
const cwd = realpathSync(dirIn(host.home, "work"));
|
|
90
|
+
const env = { PATH: `${tools}:/usr/bin:/bin` };
|
|
91
|
+
const own = await host.niwa.call(procRun, {
|
|
92
|
+
cmd: ["/bin/sh", "-c", 'echo "$HOME"; touch "$HOME/.niwa-proc-probe" 2>/dev/null && echo wrote || echo kept; niwa-hello'],
|
|
93
|
+
cwd,
|
|
94
|
+
timeout: 20_000,
|
|
95
|
+
env,
|
|
96
|
+
});
|
|
97
|
+
const [home, wrote, hello] = outputOf(host, own.output).trim().split("\n");
|
|
98
|
+
expect(own.exit).toBe(0);
|
|
99
|
+
expect(home).toBe(homedir());
|
|
100
|
+
expect([wrote, hello]).toEqual(["kept", "hello from the toolchain"]);
|
|
101
|
+
expect(own.box.paths).toContainEqual({ path: tools, mode: "read", from: "toolchains" });
|
|
102
|
+
|
|
103
|
+
// With the toolchains replaced, the same program is closed to it.
|
|
104
|
+
const closed = await host.niwa.call(procRun, { cmd: ["/bin/sh", "-c", "niwa-hello"], cwd, timeout: 20_000, env, box: { only: [] } });
|
|
105
|
+
expect(closed.exit).not.toBe(0);
|
|
106
|
+
expect(outputOf(host, closed.output)).not.toContain("hello from the toolchain");
|
|
107
|
+
} finally {
|
|
108
|
+
process.env["PATH"] = saved;
|
|
109
|
+
await host.stop();
|
|
110
|
+
rmSync(tools, { recursive: true, force: true });
|
|
111
|
+
rmSync(join(homedir(), ".niwa-proc-probe"), { force: true });
|
|
112
|
+
}
|
|
113
|
+
}, 60_000);
|
|
114
|
+
|
|
115
|
+
test.skipIf(!present)("commits in a git worktree, whose git directory lies in the main repository", async () => {
|
|
116
|
+
const home = realpathSync(mkdtempSync(join(tmpdir(), "niwa-proc-home-")));
|
|
117
|
+
const main = dirIn(home, "repo");
|
|
118
|
+
const tree = join(home, "worktrees", "wt-1");
|
|
119
|
+
const boxes = sandboxHost();
|
|
120
|
+
const worktree = { path: tree, repo: main, id: "wt-1" };
|
|
121
|
+
const registered = { ...boxes, plan: (input: Parameters<typeof boxes.plan>[0]) => boxes.plan({ ...input, worktree }) };
|
|
122
|
+
const host = await procHost({ sandbox, boxes: registered }, home);
|
|
123
|
+
try {
|
|
124
|
+
const git = (cwd: string, ...args: string[]) => {
|
|
125
|
+
const done = Bun.spawnSync(["git", "-c", "user.name=niwa", "-c", "user.email=niwa@example.com", ...args], { cwd, stderr: "pipe" });
|
|
126
|
+
if (done.exitCode !== 0) throw new Error(`git ${args.join(" ")}: ${done.stderr.toString()}`);
|
|
127
|
+
};
|
|
128
|
+
git(main, "init", "-q", "-b", "main");
|
|
129
|
+
git(main, "commit", "-q", "--allow-empty", "-m", "first");
|
|
130
|
+
git(main, "worktree", "add", "-q", "-b", "lane", tree);
|
|
131
|
+
const cwd = realpathSync(tree);
|
|
132
|
+
const committed = await host.niwa.call(procRun, {
|
|
133
|
+
cmd: ["/bin/sh", "-c", "echo x > made.txt && git add made.txt && git -c user.name=niwa -c user.email=niwa@example.com commit -q -m made && git rev-parse --abbrev-ref HEAD && git log --format=%s -1"],
|
|
134
|
+
cwd,
|
|
135
|
+
timeout: 30_000,
|
|
136
|
+
});
|
|
137
|
+
expect(outputOf(host, committed.output).trim().split("\n").slice(-2)).toEqual(["lane", "made"]);
|
|
138
|
+
expect(committed.exit).toBe(0);
|
|
139
|
+
expect(committed.box.paths).toContainEqual({ path: join(main, ".git", "worktrees", "wt-1"), mode: "write", from: "repository" });
|
|
140
|
+
// The main checkout itself stays closed to it.
|
|
141
|
+
const peek = await host.niwa.call(procRun, { cmd: ["/bin/sh", "-c", `ls ${main}`], cwd, timeout: 20_000 });
|
|
142
|
+
expect(peek.exit).not.toBe(0);
|
|
143
|
+
} finally {
|
|
144
|
+
await host.stop();
|
|
145
|
+
}
|
|
146
|
+
}, 90_000);
|
|
147
|
+
|
|
148
|
+
test.skipIf(!present)("cannot read the owner token of its host", async () => {
|
|
149
|
+
// A home under the user's home, as the host's is.
|
|
150
|
+
const host = await procHost({ sandbox }, mkdtempSync(join(homedir(), ".niwa-proc-home-")));
|
|
151
|
+
try {
|
|
152
|
+
writeFileSync(join(host.home, "owner.token"), "owner-token-value\n", { mode: 0o600 });
|
|
153
|
+
const cwd = realpathSync(dirIn(host.home, "work"));
|
|
154
|
+
const result = await host.niwa.call(procRun, {
|
|
155
|
+
cmd: ["/bin/sh", "-c", `cat ${join(realpathSync(host.home), "owner.token")}`],
|
|
156
|
+
cwd,
|
|
157
|
+
timeout: 20_000,
|
|
158
|
+
});
|
|
159
|
+
expect(result.exit).not.toBe(0);
|
|
160
|
+
expect(outputOf(host, result.output)).not.toContain("owner-token-value");
|
|
161
|
+
} finally {
|
|
162
|
+
await host.stop();
|
|
163
|
+
}
|
|
164
|
+
}, 60_000);
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const srtPresent = live && (await srt().available());
|
|
169
|
+
|
|
170
|
+
test.skipIf(!srtPresent)("refuses srt's additional writable directories before a run", async () => {
|
|
171
|
+
const host = await procHost({ sandbox: srt() });
|
|
172
|
+
try {
|
|
173
|
+
const cwd = dirIn(host.home, "work");
|
|
174
|
+
await expect(host.niwa.call(procRun, { cmd: ["true"], cwd, timeout: 1000 })).rejects.toThrow(/outside the admitted paths/);
|
|
175
|
+
} finally {
|
|
176
|
+
await host.stop();
|
|
177
|
+
}
|
|
178
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// The declarations of the proc actions: what a call may say, and the class each action claims.
|
|
2
|
+
|
|
3
|
+
import { expect, test } from "bun:test";
|
|
4
|
+
|
|
5
|
+
import { secret } from "@niwa-labs/core";
|
|
6
|
+
|
|
7
|
+
import { procExec, procRun } from "../src/records.ts";
|
|
8
|
+
import { procHost } from "./support.ts";
|
|
9
|
+
|
|
10
|
+
const base = { cmd: ["true"], cwd: "/tmp", timeout: 1000 };
|
|
11
|
+
|
|
12
|
+
test("a run takes plain environment values only: a secret reference is refused", () => {
|
|
13
|
+
expect(procRun.input.safeParse({ ...base, env: { A: "1" } }).success).toBe(true);
|
|
14
|
+
expect(procRun.input.safeParse({ ...base, env: { A: secret("TOKEN") } }).success).toBe(false);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("an exec takes a secret reference where a run does not", () => {
|
|
18
|
+
expect(procExec.input.safeParse({ ...base, env: { A: secret("TOKEN") }, secrets: ["TOKEN"], net: true }).success).toBe(true);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
test("a call needs an absolute directory, a program and a timeout inside the limit", () => {
|
|
22
|
+
expect(procRun.input.safeParse({ ...base, cwd: "relative" }).success).toBe(false);
|
|
23
|
+
expect(procRun.input.safeParse({ ...base, cmd: [] }).success).toBe(false);
|
|
24
|
+
expect(procRun.input.safeParse({ ...base, cmd: [""] }).success).toBe(false);
|
|
25
|
+
expect(procRun.input.safeParse({ ...base, timeout: 0 }).success).toBe(false);
|
|
26
|
+
expect(procRun.input.safeParse({ ...base, timeout: 3_600_001 }).success).toBe(false);
|
|
27
|
+
expect(procExec.input.safeParse({ ...base, extra: 1 }).success).toBe(false);
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
test("a secret reference names a variable-like secret", () => {
|
|
31
|
+
expect(() => secret("has space")).toThrow();
|
|
32
|
+
expect(secret("DEPLOY_KEY")).toEqual({ secret: "DEPLOY_KEY" });
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
test("run is a write in the local world, exec an irreversible one in the external world, as the host describes them", async () => {
|
|
36
|
+
const host = await procHost();
|
|
37
|
+
try {
|
|
38
|
+
const actions = (await host.niwa.describe()).actions.filter((one) => one.name.startsWith("proc."));
|
|
39
|
+
expect(actions.map(({ name, effect, world }) => ({ name, effect, world }))).toEqual([
|
|
40
|
+
{ name: "proc.run", effect: "write", world: "local" },
|
|
41
|
+
{ name: "proc.exec", effect: "irreversible", world: "external" },
|
|
42
|
+
]);
|
|
43
|
+
} finally {
|
|
44
|
+
await host.stop();
|
|
45
|
+
}
|
|
46
|
+
});
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
// A call that could not start a command says so as a refusal, so nobody is left wondering whether
|
|
2
|
+
// the command ran.
|
|
3
|
+
//
|
|
4
|
+
// What it proves, for proc.run:
|
|
5
|
+
// 1. a sandbox that cannot run here, one that cannot express the box, and one whose wrapper
|
|
6
|
+
// cannot be started each refuse the call as a ProcRefusal, which the caller catches and the
|
|
7
|
+
// core does not record as a failure that may have had an effect
|
|
8
|
+
// 2. nothing was started, and no `proc.finished` was written
|
|
9
|
+
// 3. a sandbox whose policy lets a process reach a host, or one that reports no policy, cannot
|
|
10
|
+
// run it, so the promise of the action is never made on its word; a box the sandbox cannot
|
|
11
|
+
// hold is a refusal of its own, `box-refused`
|
|
12
|
+
|
|
13
|
+
import { existsSync, realpathSync } from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { afterEach, expect, test } from "bun:test";
|
|
16
|
+
|
|
17
|
+
import { CatchableError } from "@niwa-labs/core";
|
|
18
|
+
import { BoxRefused, SandboxUnavailable } from "@niwa-labs/sandbox";
|
|
19
|
+
import type { Sandbox } from "@niwa-labs/sandbox";
|
|
20
|
+
|
|
21
|
+
import { ProcRefusal } from "../src/index.ts";
|
|
22
|
+
import { absentSandbox, dirIn, heldPolicy, procActions, recordingSandbox } from "./support.ts";
|
|
23
|
+
|
|
24
|
+
type Bench = Awaited<ReturnType<typeof procActions>>;
|
|
25
|
+
const benches: Bench[] = [];
|
|
26
|
+
afterEach(async () => {
|
|
27
|
+
for (const bench of benches.splice(0)) await bench.stop();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const sh = (script: string) => ["/bin/sh", "-c", script];
|
|
31
|
+
|
|
32
|
+
const unsupported: Sandbox = {
|
|
33
|
+
...recordingSandbox(),
|
|
34
|
+
async launch() {
|
|
35
|
+
throw new BoxRefused([{ code: "adapter", message: "reads are an allow-list there" }], "nono");
|
|
36
|
+
},
|
|
37
|
+
};
|
|
38
|
+
const wrapperGone: Sandbox = {
|
|
39
|
+
...recordingSandbox(),
|
|
40
|
+
async launch() {
|
|
41
|
+
throw new SandboxUnavailable("nono", "the wrapper cannot be started");
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
test("a sandbox whose policy does not keep a run off the network refuses the call before anything starts", async () => {
|
|
46
|
+
const cases: Array<[Sandbox, RegExp]> = [
|
|
47
|
+
[recordingSandbox((spec) => ({ ...heldPolicy(spec), write: [join(spec.cwd, "..", "sibling")] })), /outside the admitted paths/],
|
|
48
|
+
[recordingSandbox(() => undefined), /does not say what a box lets a process reach/],
|
|
49
|
+
[recordingSandbox((spec) => ({ ...heldPolicy(spec), net: ["example.com"] })), /lets a process reach example\.com/],
|
|
50
|
+
];
|
|
51
|
+
for (const [sandbox, reason] of cases) {
|
|
52
|
+
const bench = await procActions({ sandbox });
|
|
53
|
+
benches.push(bench);
|
|
54
|
+
const cwd = realpathSync(dirIn(bench.home, "work"));
|
|
55
|
+
const error = await bench.call("proc.run", { cmd: sh("touch ran"), cwd, timeout: 10_000 }).then(
|
|
56
|
+
() => undefined,
|
|
57
|
+
(thrown: unknown) => thrown,
|
|
58
|
+
);
|
|
59
|
+
expect(error).toBeInstanceOf(ProcRefusal);
|
|
60
|
+
expect((error as ProcRefusal).code).toBe("box-refused");
|
|
61
|
+
expect((error as Error).message).toMatch(reason);
|
|
62
|
+
expect(existsSync(join(cwd, "ran"))).toBe(false);
|
|
63
|
+
expect(bench.emitted).toEqual([]);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test("a box that cannot be had refuses the call before anything starts", async () => {
|
|
68
|
+
for (const [sandbox, message, code] of [
|
|
69
|
+
[absentSandbox, /sandbox nono is not available/, "no-sandbox"],
|
|
70
|
+
[unsupported, /was refused/, "box-refused"],
|
|
71
|
+
[wrapperGone, /the wrapper cannot be started/, "no-sandbox"],
|
|
72
|
+
] as const) {
|
|
73
|
+
const bench = await procActions({ sandbox });
|
|
74
|
+
benches.push(bench);
|
|
75
|
+
const cwd = realpathSync(dirIn(bench.home, "work"));
|
|
76
|
+
const error = await bench.call("proc.run", { cmd: sh("touch ran"), cwd, timeout: 10_000 }).then(
|
|
77
|
+
() => undefined,
|
|
78
|
+
(thrown: unknown) => thrown,
|
|
79
|
+
);
|
|
80
|
+
expect(error).toBeInstanceOf(ProcRefusal);
|
|
81
|
+
expect(error).toBeInstanceOf(CatchableError);
|
|
82
|
+
expect((error as ProcRefusal).code).toBe(code);
|
|
83
|
+
expect((error as Error).message).toMatch(message);
|
|
84
|
+
expect(existsSync(join(cwd, "ran"))).toBe(false);
|
|
85
|
+
expect(bench.emitted).toEqual([]);
|
|
86
|
+
}
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("a manifest too large for the completion record refuses before launch", async () => {
|
|
90
|
+
const adapter = recordingSandbox((spec) => ({
|
|
91
|
+
write: spec.write, devices: [], read: { only: Array.from({ length: 6000 }, (_, i) => `${spec.cwd}/example-fixture-${i}`) }, net: [],
|
|
92
|
+
}));
|
|
93
|
+
const host = await procActions({ sandbox: adapter });
|
|
94
|
+
benches.push(host);
|
|
95
|
+
const cwd = dirIn(host.home, "work");
|
|
96
|
+
await expect(host.call("proc.run", { cmd: ["true"], cwd, timeout: 1000 })).rejects.toThrow(/manifest exceeds/);
|
|
97
|
+
expect(adapter.specs).toHaveLength(0);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
test("an admitted run receives its private temp directory and removes it on completion", async () => {
|
|
102
|
+
const adapter = recordingSandbox();
|
|
103
|
+
const bench = await procActions({ sandbox: adapter });
|
|
104
|
+
benches.push(bench);
|
|
105
|
+
const cwd = realpathSync(dirIn(bench.home, "work"));
|
|
106
|
+
const result = await bench.call("proc.run", { cmd: sh('test -d "$TMPDIR"'), cwd, timeout: 1000 });
|
|
107
|
+
expect(result.exit).toBe(0);
|
|
108
|
+
expect(adapter.specs).toHaveLength(1);
|
|
109
|
+
const temp = adapter.specs[0]!.env["TMPDIR"]!;
|
|
110
|
+
expect(temp.startsWith(cwd + "/")).toBe(true);
|
|
111
|
+
expect(existsSync(temp)).toBe(false);
|
|
112
|
+
expect(bench.emitted).toHaveLength(1);
|
|
113
|
+
});
|