@niwa-labs/sandbox 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.
@@ -0,0 +1,367 @@
1
+ // How a box gets its access: profiles, requests, and the plan they resolve to.
2
+ //
3
+ // What it proves:
4
+ // 1. a profile is paths, or a function of the machine; `bin` finds a program, its real path and its package
5
+ // 2. the catalog has git, node, bun, npm, cargo and uv; a host adds or replaces profiles by name
6
+ // 3. a box gets the paths of the profiles it takes; every other profile's paths are closed to it,
7
+ // programs excepted, and a profile two installations share stays open to both
8
+ // 4. a path asked for never opens a place another profile keeps: it is refused, naming the profile
9
+ // 5. the closed list (keys, niwa's own) is closed unless a place in it is asked for by name
10
+ // 6. `allow` adds to the host's default and `only` replaces it; what goes beyond the default needs a rule
11
+ // 7. a box that reaches no network refuses a host asked of it and leaves out the hosts of its profiles
12
+ // 8. `admit` refuses with every reason at once: unknown names, missing rules, and what the sandbox cannot hold
13
+ // 9. nono grants a file as a file; srt refuses a place it is to open inside a closed one
14
+ // 10. the manifest records the request, the paths with their origins, the rules and the policy; env by name only
15
+
16
+ import { afterAll, beforeAll, expect, test } from "bun:test";
17
+
18
+ import { mkdirSync, mkdtempSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs";
19
+ import { homedir, tmpdir } from "node:os";
20
+ import { join } from "node:path";
21
+
22
+ import { BoxRefused, admit, boxManifest, boxSpec, itemOf, nono, resolveProfile, sandbox, srt } from "@niwa-labs/sandbox";
23
+ import type { BoxRefusal, Sandbox } from "@niwa-labs/sandbox";
24
+ import { catalog } from "@niwa-labs/sandbox/profiles";
25
+
26
+ let root = "";
27
+ let savedPath: string | undefined;
28
+
29
+ beforeAll(() => {
30
+ root = realpathSync(mkdtempSync(join(tmpdir(), "niwa-access-")));
31
+ savedPath = process.env["PATH"];
32
+ // A PATH of one directory with one program in a package, so `bin` and `toolchains` are known.
33
+ const pkg = join(root, "lib", "node_modules", "@acme", "tool");
34
+ mkdirSync(join(pkg, "bin"), { recursive: true });
35
+ writeFileSync(join(pkg, "bin", "tool.js"), "#!/bin/sh\n", { mode: 0o755 });
36
+ mkdirSync(join(root, "bin"));
37
+ symlinkSync(join(pkg, "bin", "tool.js"), join(root, "bin", "tool"));
38
+ process.env["PATH"] = join(root, "bin");
39
+ mkdirSync(join(root, "work"));
40
+ mkdirSync(join(root, "codex"));
41
+ writeFileSync(join(root, "codex", "auth.json"), "{}");
42
+ mkdirSync(join(root, "pi"));
43
+ });
44
+
45
+ afterAll(() => {
46
+ process.env["PATH"] = savedPath;
47
+ rmSync(root, { recursive: true, force: true });
48
+ });
49
+
50
+ const at = (...parts: string[]) => join(root, ...parts);
51
+ const allows = (plan: { paths: readonly { path: string; mode: string }[] }, path: string, mode?: string) =>
52
+ plan.paths.some((need) => need.path === path && (mode === undefined || need.mode === mode));
53
+ const closes = (plan: { closed: readonly { path: string }[] }, path: string) => plan.closed.some((one) => one.path === path);
54
+ const codes = (reasons: readonly BoxRefusal[]) => reasons.map((reason) => reason.code);
55
+
56
+ /** A host with a tool and two harness installations whose homes are under the test's root. */
57
+ function host() {
58
+ const made = sandbox({ profiles: { tool: ({ bin }) => ({ read: [...bin("tool"), at("tool.conf")], write: [at("tool-cache")] }) } });
59
+ made.install("codex-luna", { read: [], write: [at("codex")], net: ["api.openai.com"] });
60
+ made.install("codex-sol", { write: [at("codex")], net: ["api.openai.com"] });
61
+ made.install("pi", { write: [at("pi")] });
62
+ return made;
63
+ }
64
+
65
+ test("a profile is paths or a function of the machine, and bin finds a program, its real path and its package", () => {
66
+ const plain = resolveProfile({ read: ["~/.gitconfig"], write: ["/x/cache"] }, "plain", { home: "/home/u", env: {} });
67
+ expect(plain).toEqual({ read: ["/home/u/.gitconfig"], write: ["/x/cache"], net: [], programs: [] });
68
+ const found = resolveProfile(({ bin }) => ({ read: bin("tool") }), "found", { home: "/home/u", env: { PATH: at("bin") } });
69
+ const pkg = at("lib", "node_modules", "@acme", "tool");
70
+ expect(found.read).toEqual([at("bin", "tool"), join(pkg, "bin", "tool.js"), pkg]);
71
+ expect(found.programs).toEqual(found.read);
72
+ expect(resolveProfile(({ bin }) => ({ read: bin("absent") }), "absent", { home: "/h", env: { PATH: at("bin") } }).read).toEqual([]);
73
+ expect(() => resolveProfile({ read: ["relative/place"] }, "bad", { home: "/h", env: {} })).toThrow(/neither absolute nor under ~/);
74
+ });
75
+
76
+ test("a program reached through a directory link keeps both paths without granting the parent", () => {
77
+ const parent = at("programs");
78
+ symlinkSync(at("bin"), parent);
79
+ const named = join(parent, "tool");
80
+ const made = sandbox();
81
+ made.install("runner", ({ bin }) => ({ read: bin(named) }));
82
+ const plan = made.plan({ cwd: at("work"), defaults: { allow: [] }, owner: "runner", network: false });
83
+ expect(allows(plan, named, "read")).toBe(true);
84
+ expect(allows(plan, at("bin", "tool"), "read")).toBe(true);
85
+ expect(allows(plan, parent)).toBe(false);
86
+ expect(allows(plan, at("bin"))).toBe(false);
87
+ });
88
+
89
+ test("the catalog holds git, node, bun, npm, cargo and uv; a host adds and replaces profiles, with names it can check", () => {
90
+ expect(Object.keys(catalog).sort()).toEqual(["bun", "cargo", "git", "node", "npm", "uv"]);
91
+ expect(sandbox().names()).toEqual(["git", "node", "bun", "npm", "cargo", "uv"]);
92
+ const own = sandbox({ profiles: { pnpm: { write: ["~/Library/pnpm/store"] }, git: { read: ["/etc/gitconfig"] } } });
93
+ expect(own.names()).toContain("pnpm");
94
+ const plan = own.plan({ cwd: at("work"), defaults: { allow: ["git", "pnpm"] }, network: false });
95
+ expect(allows(plan, join(homedir(), "Library", "pnpm", "store"), "write")).toBe(true);
96
+ expect(allows(plan, "/private/etc/gitconfig") || allows(plan, "/etc/gitconfig")).toBe(true);
97
+ expect(() => sandbox({ profiles: { "Bad Name": {} } })).toThrow(/profile name/);
98
+ });
99
+
100
+ test("a box gets the paths of what it takes, and every other profile's paths but programs are closed to it", () => {
101
+ const made = host();
102
+ const plan = made.plan({ cwd: at("work"), defaults: { allow: ["project", "tool"] }, owner: "pi", network: true });
103
+ expect(allows(plan, at("work"), "write")).toBe(true);
104
+ expect(plan.paths.find((need) => need.path === at("tool-cache"))).toEqual({ path: at("tool-cache"), mode: "write", from: "profile:tool" });
105
+ expect(plan.paths.find((need) => need.path === at("pi"))).toEqual({ path: at("pi"), mode: "write", from: "harness:pi" });
106
+ // The codex installations are not this box's: their home is closed, under the name of each.
107
+ expect(plan.closed).toContainEqual({ path: at("codex"), from: "harness:codex-luna" });
108
+ expect(plan.closed).toContainEqual({ path: at("codex"), from: "harness:codex-sol" });
109
+ expect(plan.net).toEqual([]);
110
+
111
+ const other = made.plan({ cwd: at("work"), defaults: { allow: ["project"] }, owner: "codex-luna", network: true });
112
+ // A box that does not take the tool finds its places closed, but not its program.
113
+ expect(closes(other, at("tool-cache"))).toBe(true);
114
+ expect(closes(other, at("tool.conf"))).toBe(true);
115
+ expect(closes(other, at("bin", "tool"))).toBe(false);
116
+ // Two installations share a home: what one takes, the other's profile does not close.
117
+ expect(closes(other, at("codex"))).toBe(false);
118
+ expect(closes(other, at("pi"))).toBe(true);
119
+ expect(other.net).toEqual([{ host: "api.openai.com", from: "harness:codex-luna" }]);
120
+ });
121
+
122
+ test("a path asked for never opens a place another profile keeps: the box is refused, naming whose place it is", () => {
123
+ const made = host();
124
+ const plan = made.plan({ cwd: at("work"), defaults: { allow: [] }, request: { allow: [{ read: at("codex", "auth.json") }, { write: at("tool-cache", "x") }] }, owner: "pi", network: false });
125
+ expect(codes(plan.refusals)).toEqual(["closed", "closed"]);
126
+ expect(plan.refusals[0]?.message).toContain("harness:codex-luna");
127
+ expect(plan.refusals[1]?.message).toContain("take the profile tool");
128
+ // A tree that holds a closed place is no refusal of the plan: it is the sandbox's to hold or refuse.
129
+ const wide = made.plan({ cwd: at("work"), defaults: { allow: [{ read: root }] }, owner: "pi", network: false });
130
+ expect(wide.refusals).toEqual([]);
131
+ expect(allows(wide, root, "read")).toBe(true);
132
+ expect(closes(wide, at("codex"))).toBe(true);
133
+ });
134
+
135
+ test("the closed list is closed unless a place in it is asked for by name, and niwa's own places are added to it", () => {
136
+ const made = sandbox({ closed: [at("vault")] });
137
+ const ssh = realpathSync(homedir()) + "/.ssh";
138
+ const plain = made.plan({ cwd: at("work"), defaults: { allow: [{ read: "~" }] }, closed: [{ path: at("niwa", "journal.db"), from: "niwa" }], network: false });
139
+ expect(plain.closed).toContainEqual({ path: ssh, from: "closed" });
140
+ expect(plain.closed).toContainEqual({ path: at("vault"), from: "closed" });
141
+ expect(plain.closed).toContainEqual({ path: at("niwa", "journal.db"), from: "niwa" });
142
+ // Named, it opens; it is never the default's to give, so a call needs the rule even under `~`.
143
+ const asked = made.plan({ cwd: at("work"), defaults: { allow: [{ read: "~" }] }, request: { allow: [{ read: "~/.ssh" }] }, network: false });
144
+ expect(closes(asked, ssh)).toBe(false);
145
+ expect(asked.rules).toEqual([`box.read@${ssh}`]);
146
+ });
147
+
148
+ test("allow adds to the host's default and only replaces it; what goes beyond the default names the rule it needs", () => {
149
+ const made = host();
150
+ const defaults = { allow: ["project", "tool", { read: at("data") }] } as const;
151
+ const within = made.plan({ cwd: at("work"), defaults, request: { allow: [{ read: at("data", "set") }, { write: at("work", "out") }, "tool"] }, network: false });
152
+ expect(within.rules).toEqual([]);
153
+ expect(within.request.items.map((one) => one.from)).toEqual(["host", "host", "host", "call", "call", "call"]);
154
+ const beyond = made.plan({ cwd: at("work"), defaults, request: { allow: ["npm", { write: at("data") }, { read: at("my place") }, "toolchains"] }, network: false });
155
+ expect(beyond.rules).toEqual(["box.use@npm", `box.write@${at("data")}`, `box.read@${at("my%20place")}`, "box.use@toolchains"]);
156
+ const only = made.plan({ cwd: at("work"), defaults, request: { only: [{ read: "." }] }, network: false });
157
+ expect(only.request).toEqual({ mode: "only", items: [{ item: { read: "." }, from: "call" }] });
158
+ expect(only.rules).toEqual([]);
159
+ expect(allows(only, at("work"), "read")).toBe(true);
160
+ expect(allows(only, at("work"), "write")).toBe(false);
161
+ expect(allows(only, at("tool-cache"))).toBe(false);
162
+ expect(closes(only, at("tool-cache"))).toBe(true);
163
+ });
164
+
165
+ test("a relative path is read from the working directory, and a link is followed before anything is decided", () => {
166
+ const made = host();
167
+ symlinkSync(at("codex"), at("work", "alias"));
168
+ try {
169
+ const plan = made.plan({ cwd: at("work"), defaults: { allow: [] }, request: { allow: [{ write: "../artifacts" }, { read: "alias/auth.json" }] }, owner: "pi", network: false });
170
+ expect(allows(plan, at("artifacts"), "write")).toBe(true);
171
+ expect(codes(plan.refusals)).toEqual(["closed"]);
172
+ } finally {
173
+ rmSync(at("work", "alias"));
174
+ }
175
+ });
176
+
177
+ test("toolchains reads the PATH and the catalog's programs, and leaves out what holds the home or a closed place", () => {
178
+ const made = host();
179
+ const plan = made.plan({ cwd: at("work"), defaults: { allow: ["toolchains"] }, network: false });
180
+ expect(plan.paths).toContainEqual({ path: at("bin"), mode: "read", from: "toolchains" });
181
+ expect(plan.paths).toContainEqual({ path: at("lib", "node_modules", "@acme", "tool"), mode: "read", from: "toolchains" });
182
+ process.env["PATH"] = `${at("bin")}:${homedir()}:${at("codex")}`;
183
+ try {
184
+ const skipped = made.plan({ cwd: at("work"), defaults: { allow: ["toolchains"] }, owner: "pi", network: false }).skipped;
185
+ expect(skipped.map((one) => one.place)).toEqual([realpathSync(homedir()), at("codex")]);
186
+ } finally {
187
+ process.env["PATH"] = at("bin");
188
+ }
189
+ });
190
+
191
+ test("a box with no network refuses a host asked of it, and leaves out the hosts of its profiles, saying so", () => {
192
+ const made = host();
193
+ const plan = made.plan({ cwd: at("work"), defaults: { allow: [] }, request: { allow: [{ net: "example.com" }] }, owner: "codex-luna", network: false });
194
+ expect(codes(plan.refusals)).toEqual(["no-network"]);
195
+ expect(plan.net).toEqual([]);
196
+ expect(plan.skipped).toContainEqual({ place: "api.openai.com", from: "harness:codex-luna", why: "this box reaches no network" });
197
+ const reaching = made.plan({ cwd: at("work"), defaults: { allow: [] }, request: { allow: [{ net: "example.com" }] }, network: true });
198
+ expect(reaching.net).toEqual([{ host: "example.com", from: "call" }]);
199
+ expect(reaching.rules).toEqual(["box.net@example.com"]);
200
+ });
201
+
202
+ test("an unknown name is refused with the names a box may ask for", () => {
203
+ const plan = host().plan({ cwd: at("work"), defaults: { allow: [] }, request: { allow: ["yarn" as "npm"] }, network: false });
204
+ expect(codes(plan.refusals)).toEqual(["unknown-profile"]);
205
+ expect(plan.refusals[0]?.message).toContain("bun, cargo, git, node, npm, tool, uv");
206
+ });
207
+
208
+ test("admit refuses with every reason at once: the plan's own, each missing rule, and what the sandbox cannot hold", () => {
209
+ const made = host();
210
+ const plan = made.plan({
211
+ cwd: at("work"),
212
+ defaults: { allow: [] },
213
+ request: { allow: ["yarn" as "npm", "npm", { read: root }] },
214
+ owner: "pi",
215
+ network: false,
216
+ });
217
+ let refused: BoxRefused | undefined;
218
+ try {
219
+ admit(plan, nono(), () => undefined);
220
+ } catch (error) {
221
+ refused = error as BoxRefused;
222
+ }
223
+ expect(refused).toBeInstanceOf(BoxRefused);
224
+ expect(codes(refused?.reasons ?? [])).toEqual(["unknown-profile", "not-granted", "not-granted", "adapter", "adapter", "adapter"]);
225
+ expect(refused?.reasons.filter((reason) => reason.code === "not-granted").map((reason) => reason.rule)).toEqual(["box.use@npm", `box.read@${root}`]);
226
+ expect(refused?.message).toContain(`${at("codex")} closed inside ${root}`);
227
+ // The grant's rules that cover what the plan needs are the ones the box took.
228
+ const granted = made.plan({ cwd: at("work"), defaults: { allow: [] }, request: { allow: ["npm"] }, network: false });
229
+ const held = admit(granted, nono(), (rule) => (rule.startsWith("box.use@") ? "box.use@*" : undefined));
230
+ expect(held.rules).toEqual(["box.use@*"]);
231
+ });
232
+
233
+ test("nono grants a file as a file and a directory as a directory, and refuses a grant inside a closed directory", () => {
234
+ const made = nono().manifest(boxSpec({ cwd: at("work"), read: [at("codex", "auth.json"), at("bin")], write: [at("codex", "auth.json"), at("pi")], closed: [at("other")] }));
235
+ expect(() => nono().manifest(boxSpec({ cwd: at("codex", "child"), closed: [at("codex")] }))).toThrow(BoxRefused);
236
+ expect(made.flags).toEqual(expect.arrayContaining(["--read-file", at("codex", "auth.json"), "--read", at("bin"), "--allow-file", at("codex", "auth.json"), "--allow", at("pi")]));
237
+ });
238
+
239
+ test("srt closes a place to reading and writing, and refuses to open a place inside a closed one", () => {
240
+ const settings = srt().manifest(boxSpec({ cwd: at("work"), write: [at("work")], closed: [at("work", "secret")] })).settings;
241
+ expect(settings.filesystem.denyRead).toEqual([{ path: at("work", "secret"), literal: true }]);
242
+ expect(settings.filesystem.denyWrite).toEqual([{ path: at("work", "secret"), literal: true }]);
243
+ expect(() => srt().manifest(boxSpec({ cwd: at("work"), read: [at("codex", "auth.json")], write: [at("codex", "x")], closed: [at("codex")] }))).toThrow(
244
+ expect.objectContaining({ reasons: [expect.objectContaining({ code: "adapter" }), expect.objectContaining({ code: "adapter" })] }),
245
+ );
246
+ });
247
+
248
+ test("the manifest records the request, the paths with their origins, the rules taken and the policy, the environment by name", () => {
249
+ const made = host();
250
+ const plan = made.plan({ cwd: at("work"), defaults: { allow: ["project"] }, request: { allow: ["tool"] }, owner: "pi", network: false });
251
+ const adapter: Sandbox = srt();
252
+ const admitted = admit(plan, adapter, () => "box.use@tool", { env: { HOME: homedir(), SECRET_TOKEN: "value" } });
253
+ const manifest = boxManifest(plan, admitted.rules, admitted.manifest);
254
+ expect(manifest.sandbox).toBe("srt");
255
+ expect(manifest.request.items).toEqual([{ item: "project", from: "host" }, { item: "tool", from: "call" }]);
256
+ expect(manifest.paths).toContainEqual({ path: at("pi"), mode: "write", from: "harness:pi" });
257
+ expect(manifest.rules).toEqual(["box.use@tool"]);
258
+ expect(manifest.env.sort()).toEqual(["HOME", "PATH", "SECRET_TOKEN"]);
259
+ expect(JSON.stringify(manifest)).not.toContain("value");
260
+ expect("except" in manifest.policy.read).toBe(true);
261
+ });
262
+
263
+ test("a command line item is read as a mode and a place, or as a name", () => {
264
+ expect(itemOf("bun")).toBe("bun");
265
+ expect(itemOf("write:../artifacts")).toEqual({ write: "../artifacts" });
266
+ expect(itemOf("read:.")).toEqual({ read: "." });
267
+ expect(itemOf("net:crates.io")).toEqual({ net: "crates.io" });
268
+ });
269
+
270
+ test("a runtime or a package a profile names through bin stays open to a box whose project holds it", () => {
271
+ const made = sandbox();
272
+ const pkg = at("repo", "node_modules", "@vendor", "sdk");
273
+ mkdirSync(pkg, { recursive: true });
274
+ made.install("claude", ({ bin }) => ({ read: [...bin(pkg)], write: [at("claude-home")] }));
275
+ const plan = made.plan({ cwd: at("repo"), defaults: { allow: ["project"] }, network: false });
276
+ expect(closes(plan, pkg)).toBe(false);
277
+ expect(closes(plan, at("claude-home"))).toBe(true);
278
+ });
279
+
280
+ test("a program found through a link keeps the link's name in the box beside its target", () => {
281
+ const plan = host().plan({ cwd: at("work"), defaults: { allow: ["tool"] }, network: false });
282
+ expect(allows(plan, at("bin", "tool"), "read")).toBe(true);
283
+ expect(allows(plan, at("lib", "node_modules", "@acme", "tool", "bin", "tool.js"), "read")).toBe(true);
284
+ });
285
+
286
+ test("a box whose working directory nobody chose is not given it to write, and says why", () => {
287
+ const plan = host().plan({ cwd: at("work"), defaults: { allow: ["project"] }, noProject: "the session names no working directory", network: false });
288
+ expect(allows(plan, at("work"), "write")).toBe(false);
289
+ expect(plan.skipped).toEqual([{ place: at("work"), from: "project", why: "the session names no working directory" }]);
290
+ });
291
+
292
+ test("a box that writes its directory gets what git keeps of it outside: a worktree's own git directory, and the shared objects and refs", () => {
293
+ const main = at("main");
294
+ const tree = at("trees", "wt1");
295
+ mkdirSync(main);
296
+ const git = (args: string[]) => {
297
+ const made = Bun.spawnSync(["git", "-c", "user.name=Fixture", "-c", "user.email=fixture@example.invalid", ...args], {
298
+ cwd: main, env: { ...process.env, PATH: savedPath, GIT_CONFIG_GLOBAL: "/dev/null", GIT_CONFIG_NOSYSTEM: "1" }, stderr: "pipe",
299
+ });
300
+ if (made.exitCode !== 0) throw new Error(made.stderr.toString());
301
+ };
302
+ git(["init", "-q"]);
303
+ git(["commit", "-q", "--allow-empty", "-m", "fixture"]);
304
+ git(["worktree", "add", "-qb", "fixture", tree]);
305
+ const input = { cwd: tree, defaults: { allow: ["project"] }, network: false } as const;
306
+ const worktree = { id: "wt1", path: tree, repo: main };
307
+ for (const binding of [undefined, { ...worktree, id: "unrelated" }]) {
308
+ const denied = host().plan({ ...input, ...(binding && { worktree: binding }) });
309
+ expect(denied.refusals[0]?.code).toBe("not-granted");
310
+ expect(denied.paths.some((need) => need.from === "repository")).toBe(false);
311
+ }
312
+ const explicit = host().plan({ ...input, request: { allow: [{ write: join(main, ".git") }] } });
313
+ expect(explicit.refusals).toEqual([]);
314
+ expect(explicit.paths.some((need) => need.from === "repository")).toBe(false);
315
+ expect(() => admit(explicit, nono(), () => undefined)).toThrow(BoxRefused);
316
+ const plan = host().plan({ ...input, worktree });
317
+ expect(plan.refusals).toEqual([]);
318
+ expect(plan.paths).toContainEqual({ path: join(main, ".git", "worktrees", "wt1"), mode: "write", from: "repository" });
319
+ expect(plan.paths).toContainEqual({ path: join(main, ".git"), mode: "read", from: "repository" });
320
+ for (const part of ["objects", "refs", "logs"]) expect(plan.paths).toContainEqual({ path: join(main, ".git", part), mode: "write", from: "repository" });
321
+ // Its configuration and hooks are read, never written.
322
+ expect(allows(plan, join(main, ".git"), "write")).toBe(false);
323
+ // A box that only reads its directory gets nothing of the repository.
324
+ const reading = host().plan({ cwd: tree, worktree, defaults: { allow: [{ read: "." }] }, network: false });
325
+ expect(reading.paths.some((need) => need.from === "repository")).toBe(false);
326
+ // A plain subdirectory carries no independent grant on its parent.
327
+ mkdirSync(join(main, "pkg"));
328
+ const inner = host().plan({ cwd: join(main, "pkg"), defaults: { allow: ["project"] }, network: false });
329
+ expect(inner.paths).not.toContainEqual({ path: join(main, ".git"), mode: "write", from: "repository" });
330
+ });
331
+
332
+ test("the toolchains read what each profile of the catalog reads, and nothing it writes, nor anything of a host's profile", () => {
333
+ const home = realpathSync(homedir());
334
+ const plan = host().plan({ cwd: at("work"), defaults: { allow: ["toolchains"] }, owner: "pi", network: false });
335
+ expect(plan.paths).toContainEqual({ path: join(home, ".gitconfig"), mode: "read", from: "toolchains" });
336
+ expect(closes(plan, join(home, ".gitconfig"))).toBe(false);
337
+ // What a tool writes stays its own: npm's configuration and git's stored credentials may hold tokens.
338
+ expect(closes(plan, join(home, ".npmrc"))).toBe(true);
339
+ expect(closes(plan, join(home, ".git-credentials"))).toBe(true);
340
+ expect(closes(plan, join(home, ".config", "git", "credentials"))).toBe(true);
341
+ // A host's own profile is not the toolchains' to open, nor is an installation's.
342
+ expect(closes(plan, at("tool.conf"))).toBe(true);
343
+ expect(closes(plan, at("codex"))).toBe(true);
344
+ const bare = host().plan({ cwd: at("work"), defaults: { allow: [] }, owner: "pi", network: false });
345
+ expect(closes(bare, join(home, ".gitconfig"))).toBe(true);
346
+ });
347
+
348
+ test("the cwd and required writes respect installation boundaries", () => {
349
+ const cwd = at("pi", "workspace");
350
+ const input = { cwd, defaults: { allow: [] }, network: false, writes: [{ path: cwd, from: "cwd" }] };
351
+ expect(host().plan({ ...input, owner: "pi" }).refusals).toEqual([]);
352
+ expect(host().plan({ ...input, owner: "codex-sol" }).refusals[0]?.code).toBe("closed");
353
+ });
354
+
355
+
356
+ test("opening a closed path for reading preserves its write restriction", () => {
357
+ const path = at("archive");
358
+ const boxes = sandbox({ closed: [path] });
359
+ const input = { cwd: path, defaults: { allow: [{ read: path }] }, writes: [{ path, from: "cwd" }], network: false };
360
+ expect(boxes.plan(input).refusals[0]?.code).toBe("closed");
361
+ expect(boxes.plan({ ...input, defaults: { allow: [{ write: path }] } }).refusals).toEqual([]);
362
+ });
363
+
364
+ test("admission checks the host socket against the resolved profile boundaries", () => {
365
+ const plan = host().plan({ cwd: at("work"), defaults: { allow: [] }, network: false });
366
+ expect(() => admit(plan, srt({ platform: "darwin" }), () => undefined, { sockets: [at("pi", "socket")] })).toThrow(BoxRefused);
367
+ });