opencode-fs-plugin 1.10.1 → 2.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/README.md +42 -24
- package/SPEC.md +20 -13
- package/package.json +2 -2
- package/skills/filesystem/SKILL.md +15 -12
- package/src/core/types.ts +2 -0
- package/src/index.ts +7 -1
- package/src/tools/exec.ts +109 -33
package/README.md
CHANGED
|
@@ -139,26 +139,42 @@ work, fan out with `Promise.all` over `tools.fs.*` (or one multi-path call).
|
|
|
139
139
|
}
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
The workspace (project) is read/write/exec
|
|
143
|
-
|
|
144
|
-
|
|
142
|
+
The workspace (project) is read/write/exec. **Network is proxy-only by
|
|
143
|
+
default**: every run gets its own egress proxy, no direct route, and every host
|
|
144
|
+
must be approved through `fs_exec_network`. There is no separate allowlist
|
|
145
|
+
option; the permission rules are the allowlist:
|
|
145
146
|
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
147
|
+
```jsonc
|
|
148
|
+
{
|
|
149
|
+
"permissions": [
|
|
150
|
+
{ "action": "fs_exec_network", "resource": "*", "effect": "deny" },
|
|
151
|
+
{ "action": "fs_exec_network", "resource": "registry.npmjs.org", "effect": "allow" },
|
|
152
|
+
{ "action": "fs_exec_network", "resource": "pypi.org", "effect": "allow" }
|
|
153
|
+
]
|
|
154
|
+
}
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
A host the proxy refuses asks `fs_exec_network` with that host, and the run
|
|
158
|
+
retries with it added. With no rule (base allow), that happens silently; with a
|
|
159
|
+
`*` deny the host fails closed instead. `execNetworkMode: "ip"` opts into all-IP
|
|
160
|
+
networking after the same approval instead. External paths are separate: a
|
|
161
|
+
blocked path asks `external_directory` for its containing directory (or the
|
|
162
|
+
nearest existing ancestor), and the sandbox grants read/write there.
|
|
150
163
|
|
|
151
164
|
The decision comes from your `opencode.jsonc` rules (`ask`, `allow`, `deny`), the
|
|
152
|
-
same as every other tool. If a path
|
|
165
|
+
same as every other tool. If a path stays blocked after approval, the result
|
|
153
166
|
reports `violations` and a `hint`. `fs.exec` also kills every process it started
|
|
154
167
|
when the run ends, including detached descendants.
|
|
155
168
|
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
169
|
+
Proxy mode needs Linux T1 (user namespaces) or macOS. On Linux T2 there is no
|
|
170
|
+
network namespace, so `fs.exec` falls back to all-IP: a socket denial asks
|
|
171
|
+
`fs_exec_network` and retries with `ip`. In `ip` mode the policy also opens the
|
|
172
|
+
resolver sockets used by systemd-resolved, nscd, and SSSD
|
|
173
|
+
(`/run/systemd/resolve`, `/run/nscd`, `/var/lib/sss/pipes`), so DNS works across
|
|
174
|
+
common Linux distributions. Plain UDP DNS needs none of them; other Unix sockets
|
|
175
|
+
stay blocked. On kernels where procage cannot scope Unix sockets (T2 below
|
|
176
|
+
Landlock ABI 9), the policy drops them via `no-unix-sockets`; DNS then works only
|
|
177
|
+
through a plain UDP resolver.
|
|
162
178
|
|
|
163
179
|
```js
|
|
164
180
|
await tools.fs.exec({ command: "npm", args: ["install"], timeoutMs: 300000 })
|
|
@@ -268,18 +284,18 @@ Add the registry package directly to the plugin list:
|
|
|
268
284
|
|
|
269
285
|
```jsonc
|
|
270
286
|
{
|
|
271
|
-
"plugins": ["opencode-fs-plugin@
|
|
287
|
+
"plugins": ["opencode-fs-plugin@2.0.0"]
|
|
272
288
|
}
|
|
273
289
|
```
|
|
274
290
|
|
|
275
|
-
`opencode plugin add opencode-fs-plugin@
|
|
291
|
+
`opencode plugin add opencode-fs-plugin@2.0.0` installs it and writes the config
|
|
276
292
|
entry. A bare `opencode-fs-plugin` follows the `latest` tag.
|
|
277
293
|
|
|
278
294
|
OpenCode manages the package under its cache, so the path is not stable. If you
|
|
279
295
|
also want the packaged skill, install it into a project instead:
|
|
280
296
|
|
|
281
297
|
```bash
|
|
282
|
-
npm install opencode-fs-plugin@
|
|
298
|
+
npm install opencode-fs-plugin@2.0.0
|
|
283
299
|
```
|
|
284
300
|
|
|
285
301
|
```jsonc
|
|
@@ -294,7 +310,7 @@ npm install opencode-fs-plugin@1.10.1
|
|
|
294
310
|
Download the release tarball, then install it as a normal npm package:
|
|
295
311
|
|
|
296
312
|
```bash
|
|
297
|
-
VERSION=
|
|
313
|
+
VERSION=2.0.0
|
|
298
314
|
curl -L -o "opencode-fs-plugin-${VERSION}.tgz" \
|
|
299
315
|
"https://github.com/masterjg/opencode-fs-plugin/releases/download/v${VERSION}/opencode-fs-plugin-${VERSION}.tgz"
|
|
300
316
|
npm install "./opencode-fs-plugin-${VERSION}.tgz"
|
|
@@ -323,20 +339,20 @@ OpenCode installs it from Git:
|
|
|
323
339
|
|
|
324
340
|
```jsonc
|
|
325
341
|
{
|
|
326
|
-
"plugins": ["github:masterjg/opencode-fs-plugin#
|
|
342
|
+
"plugins": ["github:masterjg/opencode-fs-plugin#v2.0.0"]
|
|
327
343
|
}
|
|
328
344
|
```
|
|
329
345
|
|
|
330
|
-
`opencode plugin add 'github:masterjg/opencode-fs-plugin#
|
|
346
|
+
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v2.0.0'` installs the
|
|
331
347
|
same spec and writes the config entry. The equivalent full URL is
|
|
332
|
-
`git+https://github.com/masterjg/opencode-fs-plugin.git#
|
|
348
|
+
`git+https://github.com/masterjg/opencode-fs-plugin.git#v2.0.0`. A tag is
|
|
333
349
|
checked for updates; use a full commit hash to pin it.
|
|
334
350
|
|
|
335
351
|
Git plugins are installed under OpenCode's cache, so their package path is not
|
|
336
352
|
stable. If you also want the packaged skill, install with npm instead:
|
|
337
353
|
|
|
338
354
|
```bash
|
|
339
|
-
npm install "github:masterjg/opencode-fs-plugin#
|
|
355
|
+
npm install "github:masterjg/opencode-fs-plugin#v2.0.0"
|
|
340
356
|
```
|
|
341
357
|
|
|
342
358
|
Then use `./node_modules/opencode-fs-plugin` in `plugins` and
|
|
@@ -395,13 +411,15 @@ external targets first require `external_directory` on the absolute boundary.
|
|
|
395
411
|
`allow always` persists the same patterns as the built-in tools.
|
|
396
412
|
|
|
397
413
|
`fs.exec` asserts `fs_exec` for the run, then asks `external_directory` for any
|
|
398
|
-
blocked path and `fs_exec_network` for
|
|
414
|
+
blocked path and `fs_exec_network` for a refused host (the resource is the host,
|
|
415
|
+
or `*` in ip mode) before retrying:
|
|
399
416
|
|
|
400
417
|
```jsonc
|
|
401
418
|
{
|
|
402
419
|
"permissions": [
|
|
403
420
|
{ "action": "external_directory", "resource": "$HOME/.npm/**", "effect": "ask" },
|
|
404
|
-
{ "action": "fs_exec_network", "resource": "*", "effect": "
|
|
421
|
+
{ "action": "fs_exec_network", "resource": "*", "effect": "deny" },
|
|
422
|
+
{ "action": "fs_exec_network", "resource": "registry.npmjs.org", "effect": "allow" }
|
|
405
423
|
]
|
|
406
424
|
}
|
|
407
425
|
```
|
package/SPEC.md
CHANGED
|
@@ -86,21 +86,28 @@ 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; there are no
|
|
90
|
-
grants.
|
|
91
|
-
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
89
|
+
Network is proxy-only and external paths are denied by default; there are no
|
|
90
|
+
per-call grants.
|
|
91
|
+
- Network is proxy-only by default: the cage gets a procage proxy whose
|
|
92
|
+
allowlist is the hosts approved through `fs_exec_network` in this run. A
|
|
93
|
+
refused host asks with the host as the resource, then the run retries with it
|
|
94
|
+
added. The permission rules are the only allowlist; there is no plugin option
|
|
95
|
+
for it.
|
|
96
|
+
- `execNetworkMode: "ip"` opts into all-IP networking after `fs_exec_network`
|
|
97
|
+
approval. In `ip` mode the policy also opens the resolver sockets used by
|
|
98
|
+
systemd-resolved, nscd, and SSSD. Plain UDP DNS needs none; other Unix
|
|
99
|
+
sockets stay blocked. Landlock ignores paths absent on the host. On T2 below
|
|
100
|
+
Landlock ABI 9 the sockets cannot be scoped, so `no-unix-sockets` drops them
|
|
101
|
+
and only UDP DNS remains.
|
|
102
|
+
- Proxy mode needs Linux T1 or macOS. On T2 the cage fails with the `net-proxy`
|
|
103
|
+
gap; `fs.exec` retries in `ip` mode and asks on a socket denial.
|
|
104
|
+
- Denied paths ask `external_directory` on their containing directory (or the
|
|
105
|
+
nearest existing ancestor for a missing or redacted path). Access handling is
|
|
106
|
+
bounded to twelve attempts.
|
|
101
107
|
- If a path or socket stays blocked after approval, the result reports
|
|
102
108
|
`violations` plus a `hint`; stderr containing EACCES/EPERM/EROFS marks
|
|
103
|
-
`sandboxBlocked`.
|
|
109
|
+
`sandboxBlocked`. Results carry `network`, `networkMode`, `networkHosts`, and
|
|
110
|
+
`externalPaths`.
|
|
104
111
|
- `args` are passed verbatim. `cwd` resolves inside the sandbox and asserts
|
|
105
112
|
`external_directory` when it leaves the project; the action is `fs_exec`.
|
|
106
113
|
- Timeout defaults to 120000 ms, capped at 600000. Procage ends the run and kills
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-fs-plugin",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"engines": {
|
|
6
6
|
"node": ">=22.12"
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"@sinclair/typebox": "^0.34.52",
|
|
37
37
|
"diff": "^9.0.0",
|
|
38
38
|
"picomatch": "^4.0.7",
|
|
39
|
-
"procage": "
|
|
39
|
+
"procage": "https://github.com/masterjg/procage/releases/download/node-proxy-v1/procage-0.8.0.tgz",
|
|
40
40
|
"tar-stream": "^3.2.2",
|
|
41
41
|
"yauzl": "^3.4.0",
|
|
42
42
|
"yazl": "^3.3.1"
|
|
@@ -156,18 +156,21 @@ override it with the fs plugin option `execAllowlist` in `opencode.jsonc`:
|
|
|
156
156
|
{ "package": "./repos/opencode-fs-plugin", "options": { "execAllowlist": ["npm", "uv"] } }
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
-
The project is read/write/exec. **Network
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
159
|
+
The project is read/write/exec. **Network is proxy-only by default, and there
|
|
160
|
+
are no per-call grants.** The run gets its own egress proxy with no direct
|
|
161
|
+
route; every host must be approved through `fs_exec_network`, which asks with
|
|
162
|
+
the host as the resource and retries with it added. The permission rules are
|
|
163
|
+
the allowlist: a `*` deny plus host allows fails closed for everything else.
|
|
164
|
+
`execNetworkMode: "ip"` opts into all-IP after approval instead. On Linux T2
|
|
165
|
+
(no network namespace) the run falls back to `ip`.
|
|
163
166
|
|
|
164
167
|
```js
|
|
165
168
|
await tools.fs.exec({ command: "npm", args: ["install"], timeoutMs: 300000 })
|
|
166
169
|
```
|
|
167
170
|
|
|
168
|
-
Denied
|
|
169
|
-
|
|
170
|
-
|
|
171
|
+
Denied paths ask `external_directory` and approval covers the denied path's
|
|
172
|
+
containing directory (or nearest existing ancestor). Denied access returns
|
|
173
|
+
`violations` and a `hint`. In `ip` mode DNS works through the
|
|
171
174
|
systemd-resolved, nscd, or SSSD sockets where the kernel can scope them; other
|
|
172
175
|
Unix sockets stay blocked and plain UDP DNS works everywhere.
|
|
173
176
|
`command` is a bare name, `args` are literal, and `cwd` is sandboxed. Direct
|
|
@@ -419,8 +422,8 @@ await tools.fs.http_request({
|
|
|
419
422
|
- Linux only; there is no Windows branch.
|
|
420
423
|
- `fs.sed` supports `s<delim>pattern<delim>replacement<delim>flags` with `g`,
|
|
421
424
|
`i` and `p`. It is not a full sed interpreter.
|
|
422
|
-
- `fs.exec` requires procage: Linux x64/arm64, glibc 2.28+, Node 22.12+.
|
|
423
|
-
|
|
425
|
+
- `fs.exec` requires procage: Linux x64/arm64, glibc 2.28+, Node 22.12+.
|
|
426
|
+
Network is proxy-only by default; external paths are denied until approved.
|
|
424
427
|
- `fs.find` has no arbitrary `-exec`; use `action: "delete"`/`"trash"` for the
|
|
425
428
|
common cleanup case, or `fs.rm`/`fs.trash` separately.
|
|
426
429
|
- No `xz`, `zstd`, `7z`, `rar`, `chattr`, ACLs, xattrs, or `rsync`.
|
|
@@ -475,9 +478,9 @@ in agent rules. `fs.pipe` asserts each stage's action; a denied stage cannot be
|
|
|
475
478
|
hidden inside a pipeline. Do not work around a denied tool with shell.
|
|
476
479
|
|
|
477
480
|
`fs_exec` runs only exact allowlisted names in a procage sandbox. It asserts
|
|
478
|
-
`fs_exec`,
|
|
479
|
-
|
|
480
|
-
shell.
|
|
481
|
+
`fs_exec`, asks `external_directory` for a blocked path, and asks
|
|
482
|
+
`fs_exec_network` with the refused host (or `*` in `ip` mode), then retries.
|
|
483
|
+
There are no per-call grants and no shell.
|
|
481
484
|
|
|
482
485
|
`fs_http_request` is allowed by default, like `fetch`. Its resource is the
|
|
483
486
|
request host; rules can allow one host and ask or deny the rest. Each redirect
|
package/src/core/types.ts
CHANGED
|
@@ -32,6 +32,8 @@ export interface RunContext {
|
|
|
32
32
|
readonly tmpdir: string;
|
|
33
33
|
readonly maxOutputBytes: number;
|
|
34
34
|
readonly execAllowlist: readonly string[];
|
|
35
|
+
/** `proxy` forces egress through the run's allowlist proxy; `ip` is all-IP. */
|
|
36
|
+
readonly execNetworkMode: "proxy" | "ip";
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
export interface ToolSpec<I> {
|
package/src/index.ts
CHANGED
|
@@ -30,7 +30,11 @@ import { diffTool } from "./tools/diff.js";
|
|
|
30
30
|
import { duTool } from "./tools/du.js";
|
|
31
31
|
import { fileTool } from "./tools/file.js";
|
|
32
32
|
import { findTool } from "./tools/find.js";
|
|
33
|
-
import {
|
|
33
|
+
import {
|
|
34
|
+
execTool,
|
|
35
|
+
normalizeExecAllowlist,
|
|
36
|
+
normalizeExecNetworkMode,
|
|
37
|
+
} from "./tools/exec.js";
|
|
34
38
|
import { findmntTool } from "./tools/findmnt.js";
|
|
35
39
|
import { foldTool } from "./tools/fold.js";
|
|
36
40
|
import { grepTool } from "./tools/grep.js";
|
|
@@ -199,6 +203,7 @@ export default Plugin.define({
|
|
|
199
203
|
typeof ctx.options?.concurrency === "number" ? ctx.options.concurrency : undefined,
|
|
200
204
|
);
|
|
201
205
|
const execAllowlist = normalizeExecAllowlist(ctx.options?.execAllowlist);
|
|
206
|
+
const execNetworkMode = normalizeExecNetworkMode(ctx.options?.execNetworkMode);
|
|
202
207
|
const maxOutputBytes =
|
|
203
208
|
typeof ctx.options?.maxOutputBytes === "number" && ctx.options.maxOutputBytes > 0
|
|
204
209
|
? Math.trunc(ctx.options.maxOutputBytes)
|
|
@@ -252,6 +257,7 @@ export default Plugin.define({
|
|
|
252
257
|
tmpdir: os.tmpdir(),
|
|
253
258
|
maxOutputBytes,
|
|
254
259
|
execAllowlist,
|
|
260
|
+
execNetworkMode,
|
|
255
261
|
},
|
|
256
262
|
parsed,
|
|
257
263
|
{},
|
package/src/tools/exec.ts
CHANGED
|
@@ -7,7 +7,7 @@ import * as fs from "node:fs/promises";
|
|
|
7
7
|
import { constants as fsConstants } from "node:fs";
|
|
8
8
|
import * as path from "node:path";
|
|
9
9
|
import { Type, type Static } from "@sinclair/typebox";
|
|
10
|
-
import type { Violation } from "procage";
|
|
10
|
+
import type { Relaxation, Violation } from "procage";
|
|
11
11
|
import { FsError } from "../core/errors.js";
|
|
12
12
|
import { collect } from "../core/stream.js";
|
|
13
13
|
import { textResult, type RunContext, type ToolSpec } from "../core/types.js";
|
|
@@ -62,10 +62,71 @@ export interface PolicyDocument {
|
|
|
62
62
|
*/
|
|
63
63
|
export const RESOLVER_SOCKETS = ["/run/systemd/resolve", "/run/nscd", "/var/lib/sss/pipes"] as const;
|
|
64
64
|
|
|
65
|
-
/**
|
|
66
|
-
export
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
/** The proxy allowlist must not be empty; this name never resolves. */
|
|
66
|
+
export const PROXY_SENTINEL = "procage.invalid";
|
|
67
|
+
|
|
68
|
+
export type ExecNetworkMode = "proxy" | "ip";
|
|
69
|
+
|
|
70
|
+
export function normalizeExecNetworkMode(value: unknown): ExecNetworkMode {
|
|
71
|
+
if (value === undefined) return "proxy";
|
|
72
|
+
if (value === "proxy" || value === "ip") return value;
|
|
73
|
+
throw new FsError('execNetworkMode must be "proxy" or "ip".', "invalid");
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** `ip` adds all-IP networking; both modes get the local resolver sockets. */
|
|
77
|
+
export function applyNetworkPolicy(doc: PolicyDocument, mode: ExecNetworkMode): void {
|
|
78
|
+
doc.ipc.unix_sockets = [...RESOLVER_SOCKETS];
|
|
79
|
+
if (mode === "ip") doc.net = "ip";
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** A host the run's proxy refused. */
|
|
83
|
+
export interface ProxyHost {
|
|
84
|
+
/** Host without a port, for the permission resource. */
|
|
85
|
+
host: string;
|
|
86
|
+
/** `host:port`, for the allowlist entry. */
|
|
87
|
+
entry: string;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/** The refused host in a proxy violation, if this violation is one. */
|
|
91
|
+
export function proxyHostOf(violation: Violation): ProxyHost | undefined {
|
|
92
|
+
if (violation.operation !== "network-outbound") return undefined;
|
|
93
|
+
const match = /^(\[[^\]]+\]|[^\s:]+)(?::(\d+))? \(not on the allowlist\)$/.exec(violation.target ?? "");
|
|
94
|
+
if (match === null) return undefined;
|
|
95
|
+
const host = match[1]!;
|
|
96
|
+
return { host, entry: match[2] === undefined ? host : `${host}:${match[2]}` };
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Ask `fs_exec_network` for every host the run's proxy refused, and add the
|
|
101
|
+
* approved ones to `hosts`. Returns whether the list grew.
|
|
102
|
+
*/
|
|
103
|
+
export async function approveProxyHosts(
|
|
104
|
+
ctx: Pick<RunContext, "permissionFor">,
|
|
105
|
+
outcome: ExecOutcome,
|
|
106
|
+
hosts: string[],
|
|
107
|
+
): Promise<boolean> {
|
|
108
|
+
if (outcome.networkMode !== "proxy") return false;
|
|
109
|
+
let added = false;
|
|
110
|
+
for (const violation of outcome.violations) {
|
|
111
|
+
const host = proxyHostOf(violation);
|
|
112
|
+
if (host === undefined || hosts.includes(host.entry)) continue;
|
|
113
|
+
await ctx.permissionFor("fs_exec_network").assertTrusted([host.host]);
|
|
114
|
+
hosts.push(host.entry);
|
|
115
|
+
added = true;
|
|
116
|
+
}
|
|
117
|
+
return added;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Whether the host cannot keep a run to a proxy-only route (Linux T2). */
|
|
121
|
+
export function isProxyGap(error: unknown, api: typeof import("procage")): boolean {
|
|
122
|
+
return error instanceof api.CapabilityGapError && error.gaps.some((gap) => gap.construct === "net-proxy");
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Why a blocked run failed, and what to do about it. */
|
|
126
|
+
export function blockedHint(mode: ExecNetworkMode): string {
|
|
127
|
+
return mode === "proxy"
|
|
128
|
+
? 'The run\'s network is proxy-only and a program tried to bypass it. Use a proxy-aware client, or set execNetworkMode to "ip".'
|
|
129
|
+
: "The sandbox denied an external path or network access. Approve the path or network in the permission prompt and retry.";
|
|
69
130
|
}
|
|
70
131
|
|
|
71
132
|
export interface ExecOutcome {
|
|
@@ -77,6 +138,8 @@ export interface ExecOutcome {
|
|
|
77
138
|
aborted: boolean;
|
|
78
139
|
stdoutTruncated: boolean;
|
|
79
140
|
stderrTruncated: boolean;
|
|
141
|
+
/** The mode the sandbox actually ran: proxy may fall back to ip. */
|
|
142
|
+
networkMode: ExecNetworkMode;
|
|
80
143
|
violations: Violation[];
|
|
81
144
|
}
|
|
82
145
|
|
|
@@ -91,8 +154,8 @@ export const execTool: ToolSpec<Input> = {
|
|
|
91
154
|
name: "exec",
|
|
92
155
|
description:
|
|
93
156
|
"Run an allowlisted executable in a procage sandbox without a shell. Direct calls return JSON; " +
|
|
94
|
-
"pipe stages pass raw stdout. Default allowlist: npm, npx, uv, uvx, pip, pip3. Network
|
|
95
|
-
"
|
|
157
|
+
"pipe stages pass raw stdout. Default allowlist: npm, npx, uv, uvx, pip, pip3. Network is forced " +
|
|
158
|
+
"through the run's allowlist proxy by default; a refused host asks fs_exec_network.",
|
|
96
159
|
schema: Schema,
|
|
97
160
|
pipeable: "filter",
|
|
98
161
|
mutates: true,
|
|
@@ -122,35 +185,37 @@ export const execTool: ToolSpec<Input> = {
|
|
|
122
185
|
const inputData = io.stdin === undefined
|
|
123
186
|
? input.stdin === undefined ? undefined : Buffer.from(input.stdin)
|
|
124
187
|
: await collect(io.stdin);
|
|
125
|
-
let
|
|
188
|
+
let networkMode = ctx.execNetworkMode;
|
|
189
|
+
const proxyHosts: string[] = [];
|
|
190
|
+
let ipNetwork = false;
|
|
126
191
|
const approved: Array<{ full: string }> = [];
|
|
127
192
|
let outcome: ExecOutcome | undefined;
|
|
128
193
|
|
|
129
|
-
for (let attempt = 0; attempt <
|
|
194
|
+
for (let attempt = 0; attempt < 12; attempt++) {
|
|
130
195
|
outcome = await runCaged(ctx, {
|
|
131
196
|
command,
|
|
132
197
|
executable,
|
|
133
198
|
args,
|
|
134
199
|
cwd: cwdTarget.full,
|
|
135
200
|
timeoutMs: input.timeoutMs ?? 120_000,
|
|
136
|
-
|
|
201
|
+
networkMode,
|
|
202
|
+
proxyHosts,
|
|
203
|
+
ipNetwork,
|
|
137
204
|
allowed: approved,
|
|
138
205
|
tempDir,
|
|
139
206
|
input: inputData,
|
|
140
207
|
});
|
|
208
|
+
networkMode = outcome.networkMode;
|
|
141
209
|
|
|
142
|
-
|
|
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) {
|
|
210
|
+
if (networkMode === "ip" && !ipNetwork && detectsNetworkDenial(outcome)) {
|
|
148
211
|
await ctx.permissionFor("fs_exec_network").assert([]);
|
|
149
|
-
|
|
212
|
+
ipNetwork = true;
|
|
150
213
|
continue;
|
|
151
214
|
}
|
|
152
215
|
|
|
153
|
-
let added =
|
|
216
|
+
let added = await approveProxyHosts(ctx, outcome, proxyHosts);
|
|
217
|
+
const deniedPaths = discoverExternalPaths(ctx, outcome)
|
|
218
|
+
.filter((candidate) => !approved.some((entry) => entry.full === candidate));
|
|
154
219
|
for (const candidate of deniedPaths) {
|
|
155
220
|
const target = await existingBoundary(ctx, candidate);
|
|
156
221
|
if (target === undefined) continue;
|
|
@@ -181,7 +246,9 @@ export const execTool: ToolSpec<Input> = {
|
|
|
181
246
|
stderrTruncated: outcome.stderrTruncated,
|
|
182
247
|
sandboxBlocked: isSandboxBlocked(outcome),
|
|
183
248
|
violations: outcome.violations.length,
|
|
184
|
-
network:
|
|
249
|
+
network: networkMode === "proxy" || ipNetwork,
|
|
250
|
+
networkMode,
|
|
251
|
+
networkHosts: proxyHosts,
|
|
185
252
|
externalPaths: approved.map((entry) => entry.full),
|
|
186
253
|
};
|
|
187
254
|
if (io.piped === true) {
|
|
@@ -198,12 +265,7 @@ export const execTool: ToolSpec<Input> = {
|
|
|
198
265
|
stdout: outcome.stdout.toString("utf8"),
|
|
199
266
|
stderr: outcome.stderr.toString("utf8"),
|
|
200
267
|
violations: outcome.violations,
|
|
201
|
-
...(sandboxBlocked
|
|
202
|
-
? {
|
|
203
|
-
hint:
|
|
204
|
-
"The sandbox denied an external path or network access. Approve the path or network in the permission prompt and retry.",
|
|
205
|
-
}
|
|
206
|
-
: {}),
|
|
268
|
+
...(sandboxBlocked ? { hint: blockedHint(networkMode) } : {}),
|
|
207
269
|
};
|
|
208
270
|
return textResult(JSON.stringify(body, null, 2), metadata, outcome.exitCode === 0 ? 0 : 1);
|
|
209
271
|
} finally {
|
|
@@ -223,7 +285,9 @@ interface CagedInput {
|
|
|
223
285
|
args: string[];
|
|
224
286
|
cwd: string;
|
|
225
287
|
timeoutMs: number;
|
|
226
|
-
|
|
288
|
+
networkMode: ExecNetworkMode;
|
|
289
|
+
proxyHosts: readonly string[];
|
|
290
|
+
ipNetwork: boolean;
|
|
227
291
|
allowed: Array<{ full: string }>;
|
|
228
292
|
tempDir: string;
|
|
229
293
|
input: Buffer | undefined;
|
|
@@ -239,7 +303,7 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
239
303
|
}
|
|
240
304
|
|
|
241
305
|
const doc = api.Policy.preset("agent_workspace", 3, { workspace: ctx.sandbox.root }).toJSON() as PolicyDocument;
|
|
242
|
-
applyNetworkPolicy(doc, input.
|
|
306
|
+
applyNetworkPolicy(doc, input.networkMode === "ip" && !input.ipNetwork ? "proxy" : input.networkMode);
|
|
243
307
|
addRule(doc, "exec", path.dirname(input.executable.path));
|
|
244
308
|
addRule(doc, "exec", path.dirname(input.executable.real));
|
|
245
309
|
addRule(doc, "exec", path.dirname(process.execPath));
|
|
@@ -259,16 +323,25 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
259
323
|
addRule(doc, "write", input.cwd);
|
|
260
324
|
for (const allowed of input.allowed) addRule(doc, "write", allowed.full);
|
|
261
325
|
|
|
326
|
+
const cageOptions = {
|
|
327
|
+
violations: true,
|
|
328
|
+
acceptSpawnSnapshot: true,
|
|
329
|
+
relax: ["waive:trust-handoff", "signals-same-user", "no-unix-sockets"] as Relaxation[],
|
|
330
|
+
...(input.networkMode === "proxy"
|
|
331
|
+
? { proxy: { allowlist: [...input.proxyHosts, PROXY_SENTINEL] } }
|
|
332
|
+
: {}),
|
|
333
|
+
};
|
|
334
|
+
|
|
262
335
|
let cage: InstanceType<typeof api.Cage>;
|
|
263
336
|
let policy: ReturnType<typeof api.Policy.fromJSON>;
|
|
264
337
|
try {
|
|
265
338
|
policy = api.Policy.fromJSON(doc);
|
|
266
|
-
cage = new api.Cage(policy,
|
|
267
|
-
violations: true,
|
|
268
|
-
acceptSpawnSnapshot: true,
|
|
269
|
-
relax: ["waive:trust-handoff", "signals-same-user", "no-unix-sockets"],
|
|
270
|
-
});
|
|
339
|
+
cage = new api.Cage(policy, cageOptions);
|
|
271
340
|
} catch (error) {
|
|
341
|
+
/* v8 ignore next 3 */
|
|
342
|
+
if (input.networkMode === "proxy" && isProxyGap(error, api)) {
|
|
343
|
+
return runCaged(ctx, { ...input, networkMode: "ip" });
|
|
344
|
+
}
|
|
272
345
|
/* v8 ignore next */
|
|
273
346
|
throw new FsError(`exec policy rejected: ${String(error)}`, "unsupported");
|
|
274
347
|
}
|
|
@@ -310,10 +383,11 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
310
383
|
aborted,
|
|
311
384
|
stdoutTruncated: stdout.truncated,
|
|
312
385
|
stderrTruncated: stderr.truncated,
|
|
386
|
+
networkMode: input.networkMode,
|
|
313
387
|
violations,
|
|
314
388
|
};
|
|
315
389
|
} catch (error) {
|
|
316
|
-
/* v8 ignore next
|
|
390
|
+
/* v8 ignore next 15 */
|
|
317
391
|
if (aborted || ctx.signal.aborted) {
|
|
318
392
|
return {
|
|
319
393
|
stdout: Buffer.alloc(0),
|
|
@@ -324,6 +398,7 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
324
398
|
aborted: true,
|
|
325
399
|
stdoutTruncated: false,
|
|
326
400
|
stderrTruncated: false,
|
|
401
|
+
networkMode: input.networkMode,
|
|
327
402
|
violations: [],
|
|
328
403
|
};
|
|
329
404
|
}
|
|
@@ -360,6 +435,7 @@ function addRule(doc: PolicyDocument, access: string, target: string): void {
|
|
|
360
435
|
|
|
361
436
|
export function filterViolations(violations: readonly Violation[]): Violation[] {
|
|
362
437
|
return violations.filter((violation) => {
|
|
438
|
+
if (proxyHostOf(violation) !== undefined) return true;
|
|
363
439
|
if (violation.target?.startsWith("/")) return true;
|
|
364
440
|
return /AF_INET|AF_INET6/.test(violation.target ?? "");
|
|
365
441
|
});
|