opencode-fs-plugin 1.9.2 → 1.10.1
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/README.md +37 -25
- package/SPEC.md +15 -7
- package/package.json +1 -1
- package/skills/filesystem/SKILL.md +16 -16
- package/src/tools/exec.ts +123 -53
package/README.md
CHANGED
|
@@ -72,7 +72,7 @@ unit, end-to-end and coverage tests.
|
|
|
72
72
|
| `cpio` | `fs.cpio` | `create`/`list`/`extract` (newc), `directory`, `members`, `input`, `member`, `verbose` |
|
|
73
73
|
| `ar` | `fs.ar` | `create`/`list`/`extract`, `directory`, `members` |
|
|
74
74
|
| HTTP(S) | `fs.http_request` | `url`, `method`, `headers`, `body`, `json`, `output`, `checksum`, `maxBytes`, `timeoutMs`, `failOnError`, `followRedirects`, `maxRedirects` |
|
|
75
|
-
| process | `fs.exec` | `command`, `args`, `cwd`, `stdin`, `timeoutMs
|
|
75
|
+
| process | `fs.exec` | `command`, `args`, `cwd`, `stdin`, `timeoutMs`; exact allowlist, procage sandbox, no shell |
|
|
76
76
|
| shell pipe | `fs.pipe` | `stages`, `input`, `output`, `append` |
|
|
77
77
|
|
|
78
78
|
`fs.head.skip` selects line ranges (`sed -n '20,40p'`). `fs.find` can
|
|
@@ -140,24 +140,30 @@ work, fan out with `Promise.all` over `tools.fs.*` (or one multi-path call).
|
|
|
140
140
|
```
|
|
141
141
|
|
|
142
142
|
The workspace (project) is read/write/exec, but **network and external paths are
|
|
143
|
-
denied by default**.
|
|
143
|
+
denied by default**. There are no per-call grants: when the sandbox blocks a
|
|
144
|
+
path or a socket, `fs.exec` asks the matching permission and retries the run.
|
|
145
|
+
|
|
146
|
+
- blocked path → `external_directory` for its containing directory; the sandbox
|
|
147
|
+
then grants read/write there. A missing or `***`-redacted path resolves to the
|
|
148
|
+
nearest existing ancestor.
|
|
149
|
+
- blocked socket → `fs_exec_network`
|
|
150
|
+
|
|
151
|
+
The decision comes from your `opencode.jsonc` rules (`ask`, `allow`, `deny`), the
|
|
152
|
+
same as every other tool. If a path is still blocked after approval, the result
|
|
153
|
+
reports `violations` and a `hint`. `fs.exec` also kills every process it started
|
|
154
|
+
when the run ends, including detached descendants.
|
|
155
|
+
|
|
156
|
+
With network enabled, the policy also opens the resolver sockets used by
|
|
157
|
+
systemd-resolved, nscd, and SSSD (`/run/systemd/resolve`, `/run/nscd`,
|
|
158
|
+
`/var/lib/sss/pipes`), so DNS works across common Linux distributions. Plain
|
|
159
|
+
UDP DNS needs none of them; other Unix sockets stay blocked. On kernels where
|
|
160
|
+
procage cannot scope Unix sockets (T2 below Landlock ABI 9), the policy drops
|
|
161
|
+
them via `no-unix-sockets`; DNS then works only through a plain UDP resolver.
|
|
144
162
|
|
|
145
163
|
```js
|
|
146
|
-
await tools.fs.exec({
|
|
147
|
-
command: "npm",
|
|
148
|
-
args: ["install"],
|
|
149
|
-
network: true,
|
|
150
|
-
allowPaths: [{ path: "/home/you/.npm", mode: "rw" }],
|
|
151
|
-
timeoutMs: 300000,
|
|
152
|
-
})
|
|
164
|
+
await tools.fs.exec({ command: "npm", args: ["install"], timeoutMs: 300000 })
|
|
153
165
|
```
|
|
154
166
|
|
|
155
|
-
`network: true` asserts `fs_exec_network`; every `allowPaths` entry asserts
|
|
156
|
-
`external_directory`. Use `mode: "ro"` for read-only paths. Denied operations
|
|
157
|
-
come back as `violations` and a `hint`; ask the user, then retry with the approved
|
|
158
|
-
paths or network. `fs.exec` also kills every process it started when the run
|
|
159
|
-
ends, including detached descendants.
|
|
160
|
-
|
|
161
167
|
`command` is a bare name, `args` are literal, and `cwd` is sandboxed. Direct
|
|
162
168
|
calls return JSON with `exitCode`, `stdout`, `stderr`, and violations; a pipe
|
|
163
169
|
stage returns raw stdout so it composes with `grep`, `sort`, `tee`, and the rest.
|
|
@@ -262,18 +268,18 @@ Add the registry package directly to the plugin list:
|
|
|
262
268
|
|
|
263
269
|
```jsonc
|
|
264
270
|
{
|
|
265
|
-
"plugins": ["opencode-fs-plugin@1.
|
|
271
|
+
"plugins": ["opencode-fs-plugin@1.10.1"]
|
|
266
272
|
}
|
|
267
273
|
```
|
|
268
274
|
|
|
269
|
-
`opencode plugin add opencode-fs-plugin@1.
|
|
275
|
+
`opencode plugin add opencode-fs-plugin@1.10.1` installs it and writes the config
|
|
270
276
|
entry. A bare `opencode-fs-plugin` follows the `latest` tag.
|
|
271
277
|
|
|
272
278
|
OpenCode manages the package under its cache, so the path is not stable. If you
|
|
273
279
|
also want the packaged skill, install it into a project instead:
|
|
274
280
|
|
|
275
281
|
```bash
|
|
276
|
-
npm install opencode-fs-plugin@1.
|
|
282
|
+
npm install opencode-fs-plugin@1.10.1
|
|
277
283
|
```
|
|
278
284
|
|
|
279
285
|
```jsonc
|
|
@@ -288,7 +294,7 @@ npm install opencode-fs-plugin@1.9.0
|
|
|
288
294
|
Download the release tarball, then install it as a normal npm package:
|
|
289
295
|
|
|
290
296
|
```bash
|
|
291
|
-
VERSION=1.
|
|
297
|
+
VERSION=1.10.1
|
|
292
298
|
curl -L -o "opencode-fs-plugin-${VERSION}.tgz" \
|
|
293
299
|
"https://github.com/masterjg/opencode-fs-plugin/releases/download/v${VERSION}/opencode-fs-plugin-${VERSION}.tgz"
|
|
294
300
|
npm install "./opencode-fs-plugin-${VERSION}.tgz"
|
|
@@ -317,20 +323,20 @@ OpenCode installs it from Git:
|
|
|
317
323
|
|
|
318
324
|
```jsonc
|
|
319
325
|
{
|
|
320
|
-
"plugins": ["github:masterjg/opencode-fs-plugin#v1.
|
|
326
|
+
"plugins": ["github:masterjg/opencode-fs-plugin#v1.10.1"]
|
|
321
327
|
}
|
|
322
328
|
```
|
|
323
329
|
|
|
324
|
-
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.
|
|
330
|
+
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.10.1'` installs the
|
|
325
331
|
same spec and writes the config entry. The equivalent full URL is
|
|
326
|
-
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.
|
|
332
|
+
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.10.1`. A tag is
|
|
327
333
|
checked for updates; use a full commit hash to pin it.
|
|
328
334
|
|
|
329
335
|
Git plugins are installed under OpenCode's cache, so their package path is not
|
|
330
336
|
stable. If you also want the packaged skill, install with npm instead:
|
|
331
337
|
|
|
332
338
|
```bash
|
|
333
|
-
npm install "github:masterjg/opencode-fs-plugin#v1.
|
|
339
|
+
npm install "github:masterjg/opencode-fs-plugin#v1.10.1"
|
|
334
340
|
```
|
|
335
341
|
|
|
336
342
|
Then use `./node_modules/opencode-fs-plugin` in `plugins` and
|
|
@@ -388,17 +394,23 @@ Every tool asserts an action named after the tool (`fs_cp`, `fs_rm`,
|
|
|
388
394
|
external targets first require `external_directory` on the absolute boundary.
|
|
389
395
|
`allow always` persists the same patterns as the built-in tools.
|
|
390
396
|
|
|
391
|
-
`fs.exec` asserts `fs_exec
|
|
392
|
-
|
|
397
|
+
`fs.exec` asserts `fs_exec` for the run, then asks `external_directory` for any
|
|
398
|
+
blocked path and `fs_exec_network` for blocked sockets before retrying:
|
|
393
399
|
|
|
394
400
|
```jsonc
|
|
395
401
|
{
|
|
396
402
|
"permissions": [
|
|
403
|
+
{ "action": "external_directory", "resource": "$HOME/.npm/**", "effect": "ask" },
|
|
397
404
|
{ "action": "fs_exec_network", "resource": "*", "effect": "ask" }
|
|
398
405
|
]
|
|
399
406
|
}
|
|
400
407
|
```
|
|
401
408
|
|
|
409
|
+
Keep both rules explicit. Plugin actions match OpenCode's base
|
|
410
|
+
`{ "action": "*", "resource": "*", "effect": "allow" }` rule, so an unlisted
|
|
411
|
+
`fs_exec_network` or `external_directory` resource is allowed without a prompt,
|
|
412
|
+
not asked.
|
|
413
|
+
|
|
402
414
|
`fs.http_request` is allowed by default, like `fetch`. Its resource is the
|
|
403
415
|
request host (`example.com:8443`), so requests can be restricted per host:
|
|
404
416
|
|
package/SPEC.md
CHANGED
|
@@ -86,13 +86,21 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
|
|
|
86
86
|
Default: `npm`, `npx`, `uv`, `uvx`, `pip`, `pip3`. Override with the plugin
|
|
87
87
|
option `execAllowlist`; an empty list disables execution.
|
|
88
88
|
- The policy is procage `agent_workspace` v3: the project is read/write/exec.
|
|
89
|
-
Network and external paths are denied by default
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
89
|
+
Network and external paths are denied by default; there are no per-call
|
|
90
|
+
grants.
|
|
91
|
+
- When network is enabled, the policy is `ip` (IPv4/IPv6) plus the resolver
|
|
92
|
+
sockets used by systemd-resolved, nscd, and SSSD. Plain UDP DNS needs none;
|
|
93
|
+
other Unix sockets stay blocked. Landlock ignores paths absent on the host.
|
|
94
|
+
On T2 below Landlock ABI 9 the sockets cannot be scoped, so the
|
|
95
|
+
`no-unix-sockets` relaxation drops them and only UDP DNS remains.
|
|
96
|
+
- Denied access is handled by asserting the configured permission and retrying,
|
|
97
|
+
bounded to six attempts: a blocked path asks `external_directory` on its
|
|
98
|
+
containing directory (or the nearest existing ancestor for a missing or
|
|
99
|
+
redacted path), a blocked socket asks `fs_exec_network`. The `allow`/`ask`/`deny`
|
|
100
|
+
decision comes from `opencode.jsonc`, not from the tool call.
|
|
101
|
+
- If a path or socket stays blocked after approval, the result reports
|
|
102
|
+
`violations` plus a `hint`; stderr containing EACCES/EPERM/EROFS marks
|
|
103
|
+
`sandboxBlocked`.
|
|
96
104
|
- `args` are passed verbatim. `cwd` resolves inside the sandbox and asserts
|
|
97
105
|
`external_directory` when it leaves the project; the action is `fs_exec`.
|
|
98
106
|
- Timeout defaults to 120000 ms, capped at 600000. Procage ends the run and kills
|
package/package.json
CHANGED
|
@@ -100,7 +100,7 @@ host drops unknown fields, so a name typo runs with defaults.
|
|
|
100
100
|
| `paths` + optional `keep`/`force`/`stdout`/`level`/`suffix` | `fs.gzip`, `fs.gunzip` |
|
|
101
101
|
| `url` + optional `output` | `fs.http_request` |
|
|
102
102
|
| `stages` + optional `input`/`output` | `fs.pipe` |
|
|
103
|
-
| `command` + `args` + optional `cwd`/`stdin`/`timeoutMs
|
|
103
|
+
| `command` + `args` + optional `cwd`/`stdin`/`timeoutMs` | `fs.exec` |
|
|
104
104
|
|
|
105
105
|
`fs.head` takes `skip` for line ranges: `sed -n '20,40p'` is
|
|
106
106
|
`fs.head({ paths, skip: 19, lines: 21 })`. `fs.find` takes
|
|
@@ -157,23 +157,22 @@ override it with the fs plugin option `execAllowlist` in `opencode.jsonc`:
|
|
|
157
157
|
```
|
|
158
158
|
|
|
159
159
|
The project is read/write/exec. **Network and external paths are denied by
|
|
160
|
-
default
|
|
161
|
-
|
|
160
|
+
default, and there are no per-call grants.** When the sandbox blocks something,
|
|
161
|
+
`fs.exec` asks `external_directory` for the path or `fs_exec_network` for a
|
|
162
|
+
socket, then retries; the decision comes from `opencode.jsonc`:
|
|
162
163
|
|
|
163
164
|
```js
|
|
164
|
-
await tools.fs.exec({
|
|
165
|
-
command: "npm",
|
|
166
|
-
args: ["install"],
|
|
167
|
-
network: true,
|
|
168
|
-
allowPaths: [{ path: "/home/you/.npm", mode: "rw" }],
|
|
169
|
-
timeoutMs: 300000,
|
|
170
|
-
})
|
|
165
|
+
await tools.fs.exec({ command: "npm", args: ["install"], timeoutMs: 300000 })
|
|
171
166
|
```
|
|
172
167
|
|
|
173
|
-
Denied access returns `violations` and a `hint`;
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
168
|
+
Denied access returns `violations` and a `hint`; approval covers the denied
|
|
169
|
+
path's containing directory (or nearest existing ancestor), and a path still
|
|
170
|
+
blocked after approval is reported. With network approved, DNS works through the
|
|
171
|
+
systemd-resolved, nscd, or SSSD sockets where the kernel can scope them; other
|
|
172
|
+
Unix sockets stay blocked and plain UDP DNS works everywhere.
|
|
173
|
+
`command` is a bare name, `args` are literal, and `cwd` is sandboxed. Direct
|
|
174
|
+
calls return JSON with `exitCode`/`stdout`/`stderr` and violations; in `fs.pipe`
|
|
175
|
+
the stage passes raw stdout.
|
|
177
176
|
|
|
178
177
|
```js
|
|
179
178
|
await tools.fs.pipe({
|
|
@@ -476,8 +475,9 @@ in agent rules. `fs.pipe` asserts each stage's action; a denied stage cannot be
|
|
|
476
475
|
hidden inside a pipeline. Do not work around a denied tool with shell.
|
|
477
476
|
|
|
478
477
|
`fs_exec` runs only exact allowlisted names in a procage sandbox. It asserts
|
|
479
|
-
`fs_exec`,
|
|
480
|
-
for
|
|
478
|
+
`fs_exec`, then asks `external_directory` for a blocked path or `fs_exec_network`
|
|
479
|
+
for a blocked socket and retries; there are no per-call grants. There is no
|
|
480
|
+
shell.
|
|
481
481
|
|
|
482
482
|
`fs_http_request` is allowed by default, like `fetch`. Its resource is the
|
|
483
483
|
request host; rules can allow one host and ask or deny the rest. Each redirect
|
package/src/tools/exec.ts
CHANGED
|
@@ -42,37 +42,33 @@ const Schema = Type.Object(
|
|
|
42
42
|
timeoutMs: Type.Optional(
|
|
43
43
|
Type.Integer({ minimum: 100, maximum: 600_000, description: "Timeout in milliseconds. Default: 120000." }),
|
|
44
44
|
),
|
|
45
|
-
network: Type.Optional(
|
|
46
|
-
Type.Boolean({
|
|
47
|
-
description: "Allow IP network after `fs_exec_network` approval. Default: false.",
|
|
48
|
-
}),
|
|
49
|
-
),
|
|
50
|
-
allowPaths: Type.Optional(
|
|
51
|
-
Type.Array(
|
|
52
|
-
Type.Object(
|
|
53
|
-
{
|
|
54
|
-
path: Type.String({ minLength: 1, description: "External path to expose after approval." }),
|
|
55
|
-
mode: Type.Optional(
|
|
56
|
-
Type.Union([Type.Literal("ro"), Type.Literal("rw")], { description: "Read-only or read-write. Default: rw." }),
|
|
57
|
-
),
|
|
58
|
-
},
|
|
59
|
-
{ additionalProperties: false },
|
|
60
|
-
),
|
|
61
|
-
),
|
|
62
|
-
),
|
|
63
45
|
},
|
|
64
46
|
{ additionalProperties: false },
|
|
65
47
|
);
|
|
66
48
|
|
|
67
49
|
type Input = Static<typeof Schema>;
|
|
68
50
|
|
|
69
|
-
interface PolicyDocument {
|
|
51
|
+
export interface PolicyDocument {
|
|
70
52
|
fs: Array<{ access: string; target: { subpath: string } }>;
|
|
71
53
|
net: string;
|
|
54
|
+
ipc: { unix_sockets: string[] };
|
|
72
55
|
[key: string]: unknown;
|
|
73
56
|
}
|
|
74
57
|
|
|
75
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Sockets used by the name-service resolvers on common Linux systems:
|
|
60
|
+
* systemd-resolved, nscd, and SSSD. Plain glibc/musl DNS over UDP needs none.
|
|
61
|
+
* Landlock ignores paths that do not exist on the host.
|
|
62
|
+
*/
|
|
63
|
+
export const RESOLVER_SOCKETS = ["/run/systemd/resolve", "/run/nscd", "/var/lib/sss/pipes"] as const;
|
|
64
|
+
|
|
65
|
+
/** Set the network mode, plus the resolver sockets it needs. */
|
|
66
|
+
export function applyNetworkPolicy(doc: PolicyDocument, network: boolean): void {
|
|
67
|
+
doc.net = network ? "ip" : "none";
|
|
68
|
+
if (network) doc.ipc.unix_sockets = [...RESOLVER_SOCKETS];
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export interface ExecOutcome {
|
|
76
72
|
stdout: Buffer;
|
|
77
73
|
stderr: Buffer;
|
|
78
74
|
exitCode: number;
|
|
@@ -120,34 +116,60 @@ export const execTool: ToolSpec<Input> = {
|
|
|
120
116
|
await ctx.permission.assert([{ target: cwdTarget, boundary: cwdTarget.full }]);
|
|
121
117
|
if (ctx.signal.aborted) throw new FsError("exec interrupted.", "interrupted");
|
|
122
118
|
|
|
123
|
-
if (input.network === true) await ctx.permissionFor("fs_exec_network").assert([]);
|
|
124
|
-
|
|
125
|
-
const allowed: Array<{ full: string; mode: "ro" | "rw" }> = [];
|
|
126
|
-
for (const entry of input.allowPaths ?? []) {
|
|
127
|
-
const target = await ctx.sandbox.resolveExisting(entry.path, { allowRoot: true });
|
|
128
|
-
await ctx.permission.assert([{ target, boundary: target.full }]);
|
|
129
|
-
const mode = entry.mode ?? "rw";
|
|
130
|
-
if (!allowed.some((item) => item.full === target.full && item.mode === mode)) {
|
|
131
|
-
allowed.push({ full: target.full, mode });
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
|
|
135
119
|
const executable = await resolveExecutable(command);
|
|
136
120
|
const tempDir = await fs.mkdtemp(path.join(ctx.tmpdir, "fs-exec-"));
|
|
137
121
|
try {
|
|
138
|
-
const
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
122
|
+
const inputData = io.stdin === undefined
|
|
123
|
+
? input.stdin === undefined ? undefined : Buffer.from(input.stdin)
|
|
124
|
+
: await collect(io.stdin);
|
|
125
|
+
let networkAllowed = false;
|
|
126
|
+
const approved: Array<{ full: string }> = [];
|
|
127
|
+
let outcome: ExecOutcome | undefined;
|
|
128
|
+
|
|
129
|
+
for (let attempt = 0; attempt < 6; attempt++) {
|
|
130
|
+
outcome = await runCaged(ctx, {
|
|
131
|
+
command,
|
|
132
|
+
executable,
|
|
133
|
+
args,
|
|
134
|
+
cwd: cwdTarget.full,
|
|
135
|
+
timeoutMs: input.timeoutMs ?? 120_000,
|
|
136
|
+
network: networkAllowed,
|
|
137
|
+
allowed: approved,
|
|
138
|
+
tempDir,
|
|
139
|
+
input: inputData,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const networkDenied = !networkAllowed && detectsNetworkDenial(outcome);
|
|
143
|
+
const deniedPaths = discoverExternalPaths(ctx, outcome)
|
|
144
|
+
.filter((candidate) => !approved.some((entry) => entry.full === candidate));
|
|
145
|
+
if (!networkDenied && deniedPaths.length === 0) break;
|
|
146
|
+
|
|
147
|
+
if (networkDenied) {
|
|
148
|
+
await ctx.permissionFor("fs_exec_network").assert([]);
|
|
149
|
+
networkAllowed = true;
|
|
150
|
+
continue;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
let added = false;
|
|
154
|
+
for (const candidate of deniedPaths) {
|
|
155
|
+
const target = await existingBoundary(ctx, candidate);
|
|
156
|
+
if (target === undefined) continue;
|
|
157
|
+
/* v8 ignore next */
|
|
158
|
+
const stats = await ctx.sandbox.stat(target.full, candidate).catch(() => undefined);
|
|
159
|
+
/* v8 ignore next */
|
|
160
|
+
if (stats === undefined) continue;
|
|
161
|
+
const boundary = stats.isDirectory() ? target.full : path.dirname(target.full);
|
|
162
|
+
const dirTarget = await ctx.sandbox.resolveExisting(boundary, { allowRoot: true });
|
|
163
|
+
if (approved.some((entry) => entry.full === dirTarget.full)) continue;
|
|
164
|
+
await ctx.permission.assert([{ target: dirTarget, boundary: dirTarget.full }]);
|
|
165
|
+
approved.push({ full: dirTarget.full });
|
|
166
|
+
added = true;
|
|
167
|
+
}
|
|
168
|
+
if (!added) break;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/* v8 ignore next */
|
|
172
|
+
if (outcome === undefined) throw new FsError("exec produced no result.", "io");
|
|
151
173
|
const metadata = {
|
|
152
174
|
command,
|
|
153
175
|
args,
|
|
@@ -159,8 +181,8 @@ export const execTool: ToolSpec<Input> = {
|
|
|
159
181
|
stderrTruncated: outcome.stderrTruncated,
|
|
160
182
|
sandboxBlocked: isSandboxBlocked(outcome),
|
|
161
183
|
violations: outcome.violations.length,
|
|
162
|
-
network:
|
|
163
|
-
|
|
184
|
+
network: networkAllowed,
|
|
185
|
+
externalPaths: approved.map((entry) => entry.full),
|
|
164
186
|
};
|
|
165
187
|
if (io.piped === true) {
|
|
166
188
|
return {
|
|
@@ -179,7 +201,7 @@ export const execTool: ToolSpec<Input> = {
|
|
|
179
201
|
...(sandboxBlocked
|
|
180
202
|
? {
|
|
181
203
|
hint:
|
|
182
|
-
"The sandbox denied an external path or network access.
|
|
204
|
+
"The sandbox denied an external path or network access. Approve the path or network in the permission prompt and retry.",
|
|
183
205
|
}
|
|
184
206
|
: {}),
|
|
185
207
|
};
|
|
@@ -202,7 +224,7 @@ interface CagedInput {
|
|
|
202
224
|
cwd: string;
|
|
203
225
|
timeoutMs: number;
|
|
204
226
|
network: boolean;
|
|
205
|
-
allowed: Array<{ full: string
|
|
227
|
+
allowed: Array<{ full: string }>;
|
|
206
228
|
tempDir: string;
|
|
207
229
|
input: Buffer | undefined;
|
|
208
230
|
}
|
|
@@ -217,7 +239,7 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
217
239
|
}
|
|
218
240
|
|
|
219
241
|
const doc = api.Policy.preset("agent_workspace", 3, { workspace: ctx.sandbox.root }).toJSON() as PolicyDocument;
|
|
220
|
-
doc
|
|
242
|
+
applyNetworkPolicy(doc, input.network);
|
|
221
243
|
addRule(doc, "exec", path.dirname(input.executable.path));
|
|
222
244
|
addRule(doc, "exec", path.dirname(input.executable.real));
|
|
223
245
|
addRule(doc, "exec", path.dirname(process.execPath));
|
|
@@ -235,7 +257,7 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
235
257
|
}
|
|
236
258
|
addRule(doc, "write", input.tempDir);
|
|
237
259
|
addRule(doc, "write", input.cwd);
|
|
238
|
-
for (const allowed of input.allowed) addRule(doc,
|
|
260
|
+
for (const allowed of input.allowed) addRule(doc, "write", allowed.full);
|
|
239
261
|
|
|
240
262
|
let cage: InstanceType<typeof api.Cage>;
|
|
241
263
|
let policy: ReturnType<typeof api.Policy.fromJSON>;
|
|
@@ -244,7 +266,7 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
244
266
|
cage = new api.Cage(policy, {
|
|
245
267
|
violations: true,
|
|
246
268
|
acceptSpawnSnapshot: true,
|
|
247
|
-
relax: ["waive:trust-handoff", "signals-same-user"],
|
|
269
|
+
relax: ["waive:trust-handoff", "signals-same-user", "no-unix-sockets"],
|
|
248
270
|
});
|
|
249
271
|
} catch (error) {
|
|
250
272
|
/* v8 ignore next */
|
|
@@ -313,6 +335,23 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
313
335
|
}
|
|
314
336
|
}
|
|
315
337
|
|
|
338
|
+
/**
|
|
339
|
+
* Resolve a denied path to the nearest existing ancestor, so a missing
|
|
340
|
+
* directory or a redacted temp name still yields an approvable boundary.
|
|
341
|
+
* The filesystem root is never exposed.
|
|
342
|
+
*/
|
|
343
|
+
async function existingBoundary(ctx: RunContext, candidate: string) {
|
|
344
|
+
let current = candidate;
|
|
345
|
+
for (;;) {
|
|
346
|
+
/* v8 ignore next */
|
|
347
|
+
const target = await ctx.sandbox.resolveExisting(current, { allowRoot: true }).catch(() => undefined);
|
|
348
|
+
if (target !== undefined) {
|
|
349
|
+
return target.full === path.parse(target.full).root ? undefined : target;
|
|
350
|
+
}
|
|
351
|
+
current = path.dirname(current);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
|
|
316
355
|
function addRule(doc: PolicyDocument, access: string, target: string): void {
|
|
317
356
|
const subpath = path.resolve(target);
|
|
318
357
|
if (doc.fs.some((rule) => rule.access === access && rule.target.subpath === subpath)) return;
|
|
@@ -326,12 +365,43 @@ export function filterViolations(violations: readonly Violation[]): Violation[]
|
|
|
326
365
|
});
|
|
327
366
|
}
|
|
328
367
|
|
|
329
|
-
function isSandboxBlocked(outcome: ExecOutcome): boolean {
|
|
368
|
+
export function isSandboxBlocked(outcome: ExecOutcome): boolean {
|
|
330
369
|
if (outcome.violations.length > 0) return true;
|
|
331
370
|
if (outcome.exitCode === 0) return false;
|
|
332
371
|
return /EACCES|EPERM|EROFS|Permission denied|Read-only file system/i.test(outcome.stderr.toString("utf8"));
|
|
333
372
|
}
|
|
334
373
|
|
|
374
|
+
export function detectsNetworkDenial(outcome: ExecOutcome): boolean {
|
|
375
|
+
if (outcome.violations.some((violation) => /AF_INET|AF_INET6/.test(violation.target ?? ""))) return true;
|
|
376
|
+
return /EAI_AGAIN|getaddrinfo|ENETUNREACH|ECONNREFUSED/i.test(outcome.stderr.toString("utf8"));
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export function discoverExternalPaths(ctx: RunContext, outcome: ExecOutcome): string[] {
|
|
380
|
+
const found = new Set<string>();
|
|
381
|
+
for (const violation of outcome.violations) {
|
|
382
|
+
if (violation.target?.startsWith("/")) found.add(violation.target);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const stderr = outcome.stderr.toString("utf8");
|
|
386
|
+
if (/EACCES|EPERM|EROFS|Permission denied|Read-only file system|error writing|not written|cannot open|failed to open/i.test(stderr)) {
|
|
387
|
+
for (const line of stderr.split("\n")) {
|
|
388
|
+
for (const match of line.matchAll(/(?:^|[\s'"`(])(\/[^\s'"`),]+)/g)) {
|
|
389
|
+
found.add(match[1]!);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
for (const match of stderr.matchAll(/path:\s*['"]([^'"]+)['"]/g)) {
|
|
394
|
+
found.add(match[1]!);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const root = ctx.sandbox.root;
|
|
398
|
+
return [...found]
|
|
399
|
+
.map((candidate) => path.resolve(candidate))
|
|
400
|
+
.map((candidate) => candidate.replace(/\/\*+$/, ""))
|
|
401
|
+
.filter((candidate) => candidate !== root && !candidate.startsWith(`${root}${path.sep}`))
|
|
402
|
+
.filter((candidate, index, all) => all.indexOf(candidate) === index);
|
|
403
|
+
}
|
|
404
|
+
|
|
335
405
|
async function resolveExecutable(command: string): Promise<{ path: string; real: string }> {
|
|
336
406
|
const directories = (process.env.PATH ?? "").split(path.delimiter).filter(Boolean);
|
|
337
407
|
for (const directory of directories) {
|