opencode-fs-plugin 1.8.0 → 1.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +49 -17
- package/SPEC.md +22 -6
- package/package.json +5 -1
- package/skills/filesystem/SKILL.md +30 -14
- package/src/tools/exec.ts +253 -156
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`; exact allowlist, no shell |
|
|
75
|
+
| process | `fs.exec` | `command`, `args`, `cwd`, `stdin`, `timeoutMs`, `network`, `allowPaths`; 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
|
|
@@ -128,9 +128,9 @@ work, fan out with `Promise.all` over `tools.fs.*` (or one multi-path call).
|
|
|
128
128
|
|
|
129
129
|
## Exec
|
|
130
130
|
|
|
131
|
-
`fs.exec` runs one allowlisted executable
|
|
132
|
-
allowlist is `npm`, `npx`, `uv`, `uvx`, `pip`,
|
|
133
|
-
options:
|
|
131
|
+
`fs.exec` runs one allowlisted executable inside a procage sandbox with
|
|
132
|
+
`shell: false`. The default allowlist is `npm`, `npx`, `uv`, `uvx`, `pip`,
|
|
133
|
+
`pip3`; override it in the plugin options:
|
|
134
134
|
|
|
135
135
|
```jsonc
|
|
136
136
|
{
|
|
@@ -139,12 +139,30 @@ options:
|
|
|
139
139
|
}
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
+
The workspace (project) is read/write/exec, but **network and external paths are
|
|
143
|
+
denied by default**. Approve them per call:
|
|
144
|
+
|
|
145
|
+
```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
|
+
})
|
|
153
|
+
```
|
|
154
|
+
|
|
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
|
+
|
|
142
161
|
`command` is a bare name, `args` are literal, and `cwd` is sandboxed. Direct
|
|
143
|
-
calls return JSON with `exitCode`, `stdout`,
|
|
144
|
-
raw stdout so it composes with `grep`, `sort`, `tee`, and the rest.
|
|
162
|
+
calls return JSON with `exitCode`, `stdout`, `stderr`, and violations; a pipe
|
|
163
|
+
stage returns raw stdout so it composes with `grep`, `sort`, `tee`, and the rest.
|
|
145
164
|
|
|
146
165
|
```js
|
|
147
|
-
await tools.fs.exec({ command: "npm", args: ["run", "build"], timeoutMs: 300000 })
|
|
148
166
|
await tools.fs.pipe({
|
|
149
167
|
stages: [
|
|
150
168
|
{ tool: "exec", args: { command: "npm", args: ["test"] } },
|
|
@@ -153,8 +171,11 @@ await tools.fs.pipe({
|
|
|
153
171
|
})
|
|
154
172
|
```
|
|
155
173
|
|
|
156
|
-
The command asserts `fs_exec`,
|
|
157
|
-
|
|
174
|
+
The command asserts `fs_exec`, caps stdout/stderr, and treats a non-zero exit as
|
|
175
|
+
a normal result. Procage requires Linux x64/arm64, glibc 2.28+, and Node 22.12+.
|
|
176
|
+
On kernels before Landlock ABI 6, signal isolation relaxes to same-user. The
|
|
177
|
+
trust-handoff class is waived so package tools can update workspace files such as
|
|
178
|
+
`.git/hooks`; the executable allowlist remains the trust boundary.
|
|
158
179
|
|
|
159
180
|
## Performance and concurrency
|
|
160
181
|
|
|
@@ -241,18 +262,18 @@ Add the registry package directly to the plugin list:
|
|
|
241
262
|
|
|
242
263
|
```jsonc
|
|
243
264
|
{
|
|
244
|
-
"plugins": ["opencode-fs-plugin@1.
|
|
265
|
+
"plugins": ["opencode-fs-plugin@1.9.0"]
|
|
245
266
|
}
|
|
246
267
|
```
|
|
247
268
|
|
|
248
|
-
`opencode plugin add opencode-fs-plugin@1.
|
|
269
|
+
`opencode plugin add opencode-fs-plugin@1.9.0` installs it and writes the config
|
|
249
270
|
entry. A bare `opencode-fs-plugin` follows the `latest` tag.
|
|
250
271
|
|
|
251
272
|
OpenCode manages the package under its cache, so the path is not stable. If you
|
|
252
273
|
also want the packaged skill, install it into a project instead:
|
|
253
274
|
|
|
254
275
|
```bash
|
|
255
|
-
npm install opencode-fs-plugin@1.
|
|
276
|
+
npm install opencode-fs-plugin@1.9.0
|
|
256
277
|
```
|
|
257
278
|
|
|
258
279
|
```jsonc
|
|
@@ -267,7 +288,7 @@ npm install opencode-fs-plugin@1.5.1
|
|
|
267
288
|
Download the release tarball, then install it as a normal npm package:
|
|
268
289
|
|
|
269
290
|
```bash
|
|
270
|
-
VERSION=1.
|
|
291
|
+
VERSION=1.9.0
|
|
271
292
|
curl -L -o "opencode-fs-plugin-${VERSION}.tgz" \
|
|
272
293
|
"https://github.com/masterjg/opencode-fs-plugin/releases/download/v${VERSION}/opencode-fs-plugin-${VERSION}.tgz"
|
|
273
294
|
npm install "./opencode-fs-plugin-${VERSION}.tgz"
|
|
@@ -296,20 +317,20 @@ OpenCode installs it from Git:
|
|
|
296
317
|
|
|
297
318
|
```jsonc
|
|
298
319
|
{
|
|
299
|
-
"plugins": ["github:masterjg/opencode-fs-plugin#v1.
|
|
320
|
+
"plugins": ["github:masterjg/opencode-fs-plugin#v1.9.0"]
|
|
300
321
|
}
|
|
301
322
|
```
|
|
302
323
|
|
|
303
|
-
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.
|
|
324
|
+
`opencode plugin add 'github:masterjg/opencode-fs-plugin#v1.9.0'` installs the
|
|
304
325
|
same spec and writes the config entry. The equivalent full URL is
|
|
305
|
-
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.
|
|
326
|
+
`git+https://github.com/masterjg/opencode-fs-plugin.git#v1.9.0`. A tag is
|
|
306
327
|
checked for updates; use a full commit hash to pin it.
|
|
307
328
|
|
|
308
329
|
Git plugins are installed under OpenCode's cache, so their package path is not
|
|
309
330
|
stable. If you also want the packaged skill, install with npm instead:
|
|
310
331
|
|
|
311
332
|
```bash
|
|
312
|
-
npm install "github:masterjg/opencode-fs-plugin#v1.
|
|
333
|
+
npm install "github:masterjg/opencode-fs-plugin#v1.9.0"
|
|
313
334
|
```
|
|
314
335
|
|
|
315
336
|
Then use `./node_modules/opencode-fs-plugin` in `plugins` and
|
|
@@ -367,6 +388,17 @@ Every tool asserts an action named after the tool (`fs_cp`, `fs_rm`,
|
|
|
367
388
|
external targets first require `external_directory` on the absolute boundary.
|
|
368
389
|
`allow always` persists the same patterns as the built-in tools.
|
|
369
390
|
|
|
391
|
+
`fs.exec` asserts `fs_exec`; `network: true` also asserts `fs_exec_network`, and
|
|
392
|
+
each `allowPaths` entry asserts `external_directory`:
|
|
393
|
+
|
|
394
|
+
```jsonc
|
|
395
|
+
{
|
|
396
|
+
"permissions": [
|
|
397
|
+
{ "action": "fs_exec_network", "resource": "*", "effect": "ask" }
|
|
398
|
+
]
|
|
399
|
+
}
|
|
400
|
+
```
|
|
401
|
+
|
|
370
402
|
`fs.http_request` is allowed by default, like `fetch`. Its resource is the
|
|
371
403
|
request host (`example.com:8443`), so requests can be restricted per host:
|
|
372
404
|
|
package/SPEC.md
CHANGED
|
@@ -80,19 +80,35 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
|
|
|
80
80
|
|
|
81
81
|
## Process execution
|
|
82
82
|
|
|
83
|
-
- `fs.exec` is the only
|
|
84
|
-
|
|
83
|
+
- `fs.exec` is the only tool that starts processes, through `procage`; it never
|
|
84
|
+
uses `child_process`, `spawn`, or a shell.
|
|
85
85
|
- `command` must be a bare executable name and must match the allowlist exactly.
|
|
86
86
|
Default: `npm`, `npx`, `uv`, `uvx`, `pip`, `pip3`. Override with the plugin
|
|
87
87
|
option `execAllowlist`; an empty list disables execution.
|
|
88
|
+
- The policy is procage `agent_workspace` v3: the project is read/write/exec.
|
|
89
|
+
Network and external paths are denied by default.
|
|
90
|
+
- `network: true` asserts `fs_exec_network` and sets the policy network to IP.
|
|
91
|
+
`allowPaths` entries assert `external_directory`; `mode: "ro"` adds a read
|
|
92
|
+
rule, `mode: "rw"` adds a write rule.
|
|
93
|
+
- Denied file or network access is reported as `violations` plus a `hint`, and
|
|
94
|
+
stderr containing EACCES/EPERM/EROFS marks `sandboxBlocked`. Ask the user, then
|
|
95
|
+
retry with the approved paths or network.
|
|
88
96
|
- `args` are passed verbatim. `cwd` resolves inside the sandbox and asserts
|
|
89
97
|
`external_directory` when it leaves the project; the action is `fs_exec`.
|
|
90
|
-
- Timeout defaults to 120000 ms, capped at 600000.
|
|
91
|
-
|
|
98
|
+
- Timeout defaults to 120000 ms, capped at 600000. Procage ends the run and kills
|
|
99
|
+
every process it started, including detached descendants; `timedOut: true`
|
|
100
|
+
and exit code 124 are returned.
|
|
92
101
|
- Captured stdout/stderr are capped at `maxOutputBytes`; truncation is reported.
|
|
93
|
-
- Direct calls return JSON with `exitCode`, `stdout`,
|
|
94
|
-
the stage returns raw stdout and composes as
|
|
102
|
+
- Direct calls return JSON with `exitCode`, `stdout`, `stderr`, `violations`, and
|
|
103
|
+
the sandbox decision. In `fs.pipe`, the stage returns raw stdout and composes as
|
|
104
|
+
a source or filter.
|
|
95
105
|
- A non-zero exit is a normal result, not a tool error.
|
|
106
|
+
- `procage` is lazy-loaded; if the host cannot load it, `fs.exec` fails with
|
|
107
|
+
`unsupported` while the other fs tools continue to work. Requires Linux
|
|
108
|
+
x64/arm64, glibc 2.28+, and Node 22.12+. On kernels before Landlock ABI 6,
|
|
109
|
+
signals are relaxed from self-and-children to same-user, as procage requires.
|
|
110
|
+
The trust-handoff class is waived so package tools can update workspace files
|
|
111
|
+
such as `.git/hooks`; the executable allowlist remains the trust boundary.
|
|
96
112
|
|
|
97
113
|
## Deliberate gaps
|
|
98
114
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "opencode-fs-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.9.2",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"engines": {
|
|
6
|
+
"node": ">=22.12"
|
|
7
|
+
},
|
|
5
8
|
"description": "OpenCode 2 plugin: Linux-style filesystem, text, and HTTP(S) tools under the fs namespace.",
|
|
6
9
|
"repository": {
|
|
7
10
|
"type": "git",
|
|
@@ -33,6 +36,7 @@
|
|
|
33
36
|
"@sinclair/typebox": "^0.34.52",
|
|
34
37
|
"diff": "^9.0.0",
|
|
35
38
|
"picomatch": "^4.0.7",
|
|
39
|
+
"procage": "^0.8.0",
|
|
36
40
|
"tar-stream": "^3.2.2",
|
|
37
41
|
"yauzl": "^3.4.0",
|
|
38
42
|
"yazl": "^3.3.1"
|
|
@@ -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` | `fs.exec` |
|
|
103
|
+
| `command` + `args` + optional `cwd`/`stdin`/`timeoutMs`/`network`/`allowPaths` | `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
|
|
@@ -148,22 +148,34 @@ remove them explicitly.
|
|
|
148
148
|
|
|
149
149
|
## Exec
|
|
150
150
|
|
|
151
|
-
`fs.exec` runs an allowlisted executable
|
|
152
|
-
is `npm`, `npx`, `uv`, `uvx`, `pip`, `pip3`;
|
|
153
|
-
option `execAllowlist` in `opencode.jsonc`:
|
|
151
|
+
`fs.exec` runs an allowlisted executable in a **procage sandbox** without a
|
|
152
|
+
shell. The default allowlist is `npm`, `npx`, `uv`, `uvx`, `pip`, `pip3`;
|
|
153
|
+
override it with the fs plugin option `execAllowlist` in `opencode.jsonc`:
|
|
154
154
|
|
|
155
155
|
```jsonc
|
|
156
156
|
{ "package": "./repos/opencode-fs-plugin", "options": { "execAllowlist": ["npm", "uv"] } }
|
|
157
157
|
```
|
|
158
158
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
`external_directory
|
|
162
|
-
|
|
159
|
+
The project is read/write/exec. **Network and external paths are denied by
|
|
160
|
+
default.** `network: true` asserts `fs_exec_network`; every `allowPaths` entry
|
|
161
|
+
asserts `external_directory`, and `mode: "ro"` keeps it read-only:
|
|
162
|
+
|
|
163
|
+
```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
|
+
})
|
|
171
|
+
```
|
|
172
|
+
|
|
173
|
+
Denied access returns `violations` and a `hint`; ask the user, then retry with
|
|
174
|
+
the approved path or network. `command` is a bare name, `args` are literal, and
|
|
175
|
+
`cwd` is sandboxed. Direct calls return JSON with `exitCode`/`stdout`/`stderr`
|
|
176
|
+
and violations; in `fs.pipe` the stage passes raw stdout.
|
|
163
177
|
|
|
164
178
|
```js
|
|
165
|
-
await tools.fs.exec({ command: "npm", args: ["run", "build"], timeoutMs: 300000 })
|
|
166
|
-
await tools.fs.exec({ command: "uv", args: ["run", "pytest"] })
|
|
167
179
|
await tools.fs.pipe({
|
|
168
180
|
stages: [
|
|
169
181
|
{ tool: "exec", args: { command: "npm", args: ["test"] } },
|
|
@@ -173,8 +185,9 @@ await tools.fs.pipe({
|
|
|
173
185
|
```
|
|
174
186
|
|
|
175
187
|
A non-zero exit is a normal result; allowlist rejection, timeout, and abort are
|
|
176
|
-
|
|
177
|
-
caps captured stdout/stderr.
|
|
188
|
+
handled explicitly. Procage kills every process the run started, including
|
|
189
|
+
detached descendants, and caps captured stdout/stderr. It requires Linux
|
|
190
|
+
x64/arm64, glibc 2.28+, and Node 22.12+.
|
|
178
191
|
|
|
179
192
|
## Batch independent calls
|
|
180
193
|
|
|
@@ -407,6 +420,8 @@ await tools.fs.http_request({
|
|
|
407
420
|
- Linux only; there is no Windows branch.
|
|
408
421
|
- `fs.sed` supports `s<delim>pattern<delim>replacement<delim>flags` with `g`,
|
|
409
422
|
`i` and `p`. It is not a full sed interpreter.
|
|
423
|
+
- `fs.exec` requires procage: Linux x64/arm64, glibc 2.28+, Node 22.12+. Network
|
|
424
|
+
and external paths are denied until approved.
|
|
410
425
|
- `fs.find` has no arbitrary `-exec`; use `action: "delete"`/`"trash"` for the
|
|
411
426
|
common cleanup case, or `fs.rm`/`fs.trash` separately.
|
|
412
427
|
- No `xz`, `zstd`, `7z`, `rar`, `chattr`, ACLs, xattrs, or `rsync`.
|
|
@@ -460,8 +475,9 @@ Command actions are `fs_<command>` (for example `fs_cp`). `fs_*` wildcards work
|
|
|
460
475
|
in agent rules. `fs.pipe` asserts each stage's action; a denied stage cannot be
|
|
461
476
|
hidden inside a pipeline. Do not work around a denied tool with shell.
|
|
462
477
|
|
|
463
|
-
`fs_exec` runs only exact allowlisted names
|
|
464
|
-
`
|
|
478
|
+
`fs_exec` runs only exact allowlisted names in a procage sandbox. It asserts
|
|
479
|
+
`fs_exec`, plus `fs_exec_network` for `network: true` and `external_directory`
|
|
480
|
+
for each `allowPaths` entry or an external `cwd`. There is no shell.
|
|
465
481
|
|
|
466
482
|
`fs_http_request` is allowed by default, like `fetch`. Its resource is the
|
|
467
483
|
request host; rules can allow one host and ask or deny the rest. Each redirect
|
package/src/tools/exec.ts
CHANGED
|
@@ -3,10 +3,13 @@
|
|
|
3
3
|
* license-wizard managed-header v1 EPL-2.0 short 73897e75828f
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
import
|
|
7
|
-
import {
|
|
6
|
+
import * as fs from "node:fs/promises";
|
|
7
|
+
import { constants as fsConstants } from "node:fs";
|
|
8
|
+
import * as path from "node:path";
|
|
8
9
|
import { Type, type Static } from "@sinclair/typebox";
|
|
9
|
-
import {
|
|
10
|
+
import type { Violation } from "procage";
|
|
11
|
+
import { FsError } from "../core/errors.js";
|
|
12
|
+
import { collect } from "../core/stream.js";
|
|
10
13
|
import { textResult, type RunContext, type ToolSpec } from "../core/types.js";
|
|
11
14
|
|
|
12
15
|
export const DEFAULT_EXEC_ALLOWLIST = ["npm", "npx", "uv", "uvx", "pip", "pip3"] as const;
|
|
@@ -39,28 +42,61 @@ const Schema = Type.Object(
|
|
|
39
42
|
timeoutMs: Type.Optional(
|
|
40
43
|
Type.Integer({ minimum: 100, maximum: 600_000, description: "Timeout in milliseconds. Default: 120000." }),
|
|
41
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
|
+
),
|
|
42
63
|
},
|
|
43
64
|
{ additionalProperties: false },
|
|
44
65
|
);
|
|
45
66
|
|
|
46
67
|
type Input = Static<typeof Schema>;
|
|
47
68
|
|
|
69
|
+
interface PolicyDocument {
|
|
70
|
+
fs: Array<{ access: string; target: { subpath: string } }>;
|
|
71
|
+
net: string;
|
|
72
|
+
[key: string]: unknown;
|
|
73
|
+
}
|
|
74
|
+
|
|
48
75
|
interface ExecOutcome {
|
|
49
76
|
stdout: Buffer;
|
|
50
77
|
stderr: Buffer;
|
|
51
78
|
exitCode: number;
|
|
52
|
-
signal:
|
|
79
|
+
signal: string | null;
|
|
53
80
|
timedOut: boolean;
|
|
54
81
|
aborted: boolean;
|
|
55
82
|
stdoutTruncated: boolean;
|
|
56
83
|
stderrTruncated: boolean;
|
|
84
|
+
violations: Violation[];
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
let procage: Promise<typeof import("procage")> | undefined;
|
|
88
|
+
|
|
89
|
+
function loadProcage(): Promise<typeof import("procage")> {
|
|
90
|
+
procage ??= import("procage");
|
|
91
|
+
return procage;
|
|
57
92
|
}
|
|
58
93
|
|
|
59
94
|
export const execTool: ToolSpec<Input> = {
|
|
60
95
|
name: "exec",
|
|
61
96
|
description:
|
|
62
|
-
"Run an allowlisted executable
|
|
63
|
-
"
|
|
97
|
+
"Run an allowlisted executable in a procage sandbox without a shell. Direct calls return JSON; " +
|
|
98
|
+
"pipe stages pass raw stdout. Default allowlist: npm, npx, uv, uvx, pip, pip3. Network and external " +
|
|
99
|
+
"paths are denied until approved.",
|
|
64
100
|
schema: Schema,
|
|
65
101
|
pipeable: "filter",
|
|
66
102
|
mutates: true,
|
|
@@ -78,178 +114,239 @@ export const execTool: ToolSpec<Input> = {
|
|
|
78
114
|
}
|
|
79
115
|
|
|
80
116
|
const requested = input.cwd ?? ".";
|
|
81
|
-
const
|
|
82
|
-
const
|
|
83
|
-
if (!
|
|
84
|
-
await ctx.permission.assert([{ target, boundary:
|
|
117
|
+
const cwdTarget = await ctx.sandbox.resolveExisting(requested, { allowRoot: true });
|
|
118
|
+
const cwdStats = await ctx.sandbox.stat(cwdTarget.full, requested);
|
|
119
|
+
if (!cwdStats.isDirectory()) throw new FsError(`exec cwd is not a directory: "${requested}"`, "not-directory");
|
|
120
|
+
await ctx.permission.assert([{ target: cwdTarget, boundary: cwdTarget.full }]);
|
|
85
121
|
if (ctx.signal.aborted) throw new FsError("exec interrupted.", "interrupted");
|
|
86
122
|
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
target.
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
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
|
+
const executable = await resolveExecutable(command);
|
|
136
|
+
const tempDir = await fs.mkdtemp(path.join(ctx.tmpdir, "fs-exec-"));
|
|
137
|
+
try {
|
|
138
|
+
const outcome = await runCaged(ctx, {
|
|
139
|
+
command,
|
|
140
|
+
executable,
|
|
141
|
+
args,
|
|
142
|
+
cwd: cwdTarget.full,
|
|
143
|
+
timeoutMs: input.timeoutMs ?? 120_000,
|
|
144
|
+
network: input.network === true,
|
|
145
|
+
allowed,
|
|
146
|
+
tempDir,
|
|
147
|
+
input: io.stdin === undefined
|
|
148
|
+
? input.stdin === undefined ? undefined : Buffer.from(input.stdin)
|
|
149
|
+
: await collect(io.stdin),
|
|
150
|
+
});
|
|
151
|
+
const metadata = {
|
|
152
|
+
command,
|
|
153
|
+
args,
|
|
154
|
+
exitCode: outcome.exitCode,
|
|
155
|
+
signal: outcome.signal,
|
|
156
|
+
timedOut: outcome.timedOut,
|
|
157
|
+
aborted: outcome.aborted,
|
|
158
|
+
stdoutTruncated: outcome.stdoutTruncated,
|
|
159
|
+
stderrTruncated: outcome.stderrTruncated,
|
|
160
|
+
sandboxBlocked: isSandboxBlocked(outcome),
|
|
161
|
+
violations: outcome.violations.length,
|
|
162
|
+
network: input.network === true,
|
|
163
|
+
allowPaths: allowed,
|
|
164
|
+
};
|
|
165
|
+
if (io.piped === true) {
|
|
166
|
+
return {
|
|
167
|
+
stdout: outcome.stdout,
|
|
168
|
+
stderr: outcome.stderr.toString("utf8"),
|
|
169
|
+
exitCode: outcome.exitCode === 0 ? 0 : 1,
|
|
170
|
+
metadata,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
const sandboxBlocked = isSandboxBlocked(outcome);
|
|
174
|
+
const body = {
|
|
175
|
+
...metadata,
|
|
176
|
+
stdout: outcome.stdout.toString("utf8"),
|
|
109
177
|
stderr: outcome.stderr.toString("utf8"),
|
|
110
|
-
|
|
111
|
-
|
|
178
|
+
violations: outcome.violations,
|
|
179
|
+
...(sandboxBlocked
|
|
180
|
+
? {
|
|
181
|
+
hint:
|
|
182
|
+
"The sandbox denied an external path or network access. Ask the user, then retry with allowPaths or network: true.",
|
|
183
|
+
}
|
|
184
|
+
: {}),
|
|
112
185
|
};
|
|
186
|
+
return textResult(JSON.stringify(body, null, 2), metadata, outcome.exitCode === 0 ? 0 : 1);
|
|
187
|
+
} finally {
|
|
188
|
+
/* v8 ignore next 5 */
|
|
189
|
+
try {
|
|
190
|
+
await fs.rm(tempDir, { recursive: true, force: true });
|
|
191
|
+
} catch {
|
|
192
|
+
/* already gone */
|
|
193
|
+
}
|
|
113
194
|
}
|
|
114
|
-
const body = {
|
|
115
|
-
...metadata,
|
|
116
|
-
stdout: outcome.stdout.toString("utf8"),
|
|
117
|
-
stderr: outcome.stderr.toString("utf8"),
|
|
118
|
-
};
|
|
119
|
-
return textResult(JSON.stringify(body, null, 2), metadata, outcome.exitCode === 0 ? 0 : 1);
|
|
120
195
|
},
|
|
121
196
|
};
|
|
122
197
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
args: string[]
|
|
127
|
-
cwd: string
|
|
128
|
-
timeoutMs: number
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const child = spawn(command, args, {
|
|
135
|
-
cwd,
|
|
136
|
-
env: process.env,
|
|
137
|
-
shell: false,
|
|
138
|
-
detached: process.platform !== "win32",
|
|
139
|
-
stdio: [feed ? "pipe" : "ignore", "pipe", "pipe"],
|
|
140
|
-
windowsHide: true,
|
|
141
|
-
});
|
|
142
|
-
const cap = ctx.maxOutputBytes;
|
|
143
|
-
let stdout: Buffer<ArrayBufferLike> = Buffer.alloc(0);
|
|
144
|
-
let stderr: Buffer<ArrayBufferLike> = Buffer.alloc(0);
|
|
145
|
-
let stdoutTruncated = false;
|
|
146
|
-
let stderrTruncated = false;
|
|
147
|
-
let timedOut = false;
|
|
148
|
-
let aborted = false;
|
|
149
|
-
let settled = false;
|
|
150
|
-
let forceTimer: NodeJS.Timeout | undefined;
|
|
151
|
-
|
|
152
|
-
child.stdout?.on("data", (chunk: Buffer) => {
|
|
153
|
-
const next = appendCapped(stdout, chunk, cap);
|
|
154
|
-
stdout = next.data;
|
|
155
|
-
stdoutTruncated ||= next.truncated;
|
|
156
|
-
});
|
|
157
|
-
child.stderr?.on("data", (chunk: Buffer) => {
|
|
158
|
-
const next = appendCapped(stderr, chunk, cap);
|
|
159
|
-
stderr = next.data;
|
|
160
|
-
stderrTruncated ||= next.truncated;
|
|
161
|
-
});
|
|
198
|
+
interface CagedInput {
|
|
199
|
+
command: string;
|
|
200
|
+
executable: { path: string; real: string };
|
|
201
|
+
args: string[];
|
|
202
|
+
cwd: string;
|
|
203
|
+
timeoutMs: number;
|
|
204
|
+
network: boolean;
|
|
205
|
+
allowed: Array<{ full: string; mode: "ro" | "rw" }>;
|
|
206
|
+
tempDir: string;
|
|
207
|
+
input: Buffer | undefined;
|
|
208
|
+
}
|
|
162
209
|
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
killTree(child, "SIGTERM");
|
|
172
|
-
};
|
|
173
|
-
ctx.signal.addEventListener("abort", onAbort, { once: true });
|
|
210
|
+
async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome> {
|
|
211
|
+
let api: typeof import("procage");
|
|
212
|
+
try {
|
|
213
|
+
api = await loadProcage();
|
|
214
|
+
} catch (error) {
|
|
215
|
+
/* v8 ignore next */
|
|
216
|
+
throw new FsError(`exec sandbox is unavailable: ${String(error)}`, "unsupported");
|
|
217
|
+
}
|
|
174
218
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
219
|
+
const doc = api.Policy.preset("agent_workspace", 3, { workspace: ctx.sandbox.root }).toJSON() as PolicyDocument;
|
|
220
|
+
doc.net = input.network ? "ip" : "none";
|
|
221
|
+
addRule(doc, "exec", path.dirname(input.executable.path));
|
|
222
|
+
addRule(doc, "exec", path.dirname(input.executable.real));
|
|
223
|
+
addRule(doc, "exec", path.dirname(process.execPath));
|
|
224
|
+
addRule(doc, "exec", path.resolve(path.dirname(process.execPath), ".."));
|
|
225
|
+
addRule(doc, "exec", path.dirname(input.executable.real));
|
|
226
|
+
addRule(doc, "exec", path.resolve(path.dirname(input.executable.real), ".."));
|
|
227
|
+
for (const root of ["/home/linuxbrew", "/var/home/linuxbrew"]) {
|
|
228
|
+
try {
|
|
229
|
+
await fs.access(root);
|
|
230
|
+
addRule(doc, "read", root);
|
|
231
|
+
addRule(doc, "exec", root);
|
|
232
|
+
} catch {
|
|
233
|
+
// Homebrew is optional.
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
addRule(doc, "write", input.tempDir);
|
|
237
|
+
addRule(doc, "write", input.cwd);
|
|
238
|
+
for (const allowed of input.allowed) addRule(doc, allowed.mode === "ro" ? "read" : "write", allowed.full);
|
|
239
|
+
|
|
240
|
+
let cage: InstanceType<typeof api.Cage>;
|
|
241
|
+
let policy: ReturnType<typeof api.Policy.fromJSON>;
|
|
242
|
+
try {
|
|
243
|
+
policy = api.Policy.fromJSON(doc);
|
|
244
|
+
cage = new api.Cage(policy, {
|
|
245
|
+
violations: true,
|
|
246
|
+
acceptSpawnSnapshot: true,
|
|
247
|
+
relax: ["waive:trust-handoff", "signals-same-user"],
|
|
201
248
|
});
|
|
249
|
+
} catch (error) {
|
|
250
|
+
/* v8 ignore next */
|
|
251
|
+
throw new FsError(`exec policy rejected: ${String(error)}`, "unsupported");
|
|
252
|
+
}
|
|
202
253
|
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
254
|
+
const env = Object.fromEntries(
|
|
255
|
+
Object.entries(process.env).filter((entry): entry is [string, string] => entry[1] !== undefined),
|
|
256
|
+
);
|
|
257
|
+
env.TMPDIR = input.tempDir;
|
|
206
258
|
|
|
207
|
-
|
|
208
|
-
child: ChildProcess,
|
|
209
|
-
input: string | undefined,
|
|
210
|
-
pipe: AsyncIterable<Buffer> | undefined,
|
|
211
|
-
): Promise<void> {
|
|
212
|
-
const stdin = child.stdin;
|
|
213
|
-
/* v8 ignore next */
|
|
214
|
-
if (!stdin) return;
|
|
259
|
+
let child;
|
|
215
260
|
try {
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
261
|
+
child = await cage.spawn([input.executable.path, ...input.args], {
|
|
262
|
+
cwd: input.cwd,
|
|
263
|
+
env,
|
|
264
|
+
stdin: input.input !== undefined,
|
|
265
|
+
});
|
|
266
|
+
} catch (error) {
|
|
267
|
+
/* v8 ignore next */
|
|
268
|
+
throw new FsError(`exec spawn failed: ${String(error)}`, "unsupported");
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
let aborted = false;
|
|
272
|
+
const onAbort = () => {
|
|
273
|
+
aborted = true;
|
|
274
|
+
child?.kill();
|
|
275
|
+
};
|
|
276
|
+
ctx.signal.addEventListener("abort", onAbort, { once: true });
|
|
277
|
+
try {
|
|
278
|
+
const out = await child.output({ timeout: input.timeoutMs, ...(input.input === undefined ? {} : { input: input.input }) });
|
|
279
|
+
const violations = filterViolations(await child.violations());
|
|
280
|
+
const stdout = cap(out.stdout, ctx.maxOutputBytes);
|
|
281
|
+
const stderr = cap(out.stderr, ctx.maxOutputBytes);
|
|
282
|
+
return {
|
|
283
|
+
stdout: stdout.data,
|
|
284
|
+
stderr: stderr.data,
|
|
285
|
+
exitCode: out.timedOut ? 124 : aborted ? 130 : (out.exitCode ?? 1),
|
|
286
|
+
signal: out.signal,
|
|
287
|
+
timedOut: out.timedOut,
|
|
288
|
+
aborted,
|
|
289
|
+
stdoutTruncated: stdout.truncated,
|
|
290
|
+
stderrTruncated: stderr.truncated,
|
|
291
|
+
violations,
|
|
292
|
+
};
|
|
293
|
+
} catch (error) {
|
|
294
|
+
/* v8 ignore next 14 */
|
|
295
|
+
if (aborted || ctx.signal.aborted) {
|
|
296
|
+
return {
|
|
297
|
+
stdout: Buffer.alloc(0),
|
|
298
|
+
stderr: Buffer.alloc(0),
|
|
299
|
+
exitCode: 130,
|
|
300
|
+
signal: null,
|
|
301
|
+
timedOut: false,
|
|
302
|
+
aborted: true,
|
|
303
|
+
stdoutTruncated: false,
|
|
304
|
+
stderrTruncated: false,
|
|
305
|
+
violations: [],
|
|
306
|
+
};
|
|
223
307
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
308
|
+
/* v8 ignore next */
|
|
309
|
+
throw new FsError(`exec failed: ${String(error)}`, "unsupported");
|
|
310
|
+
} finally {
|
|
311
|
+
ctx.signal.removeEventListener("abort", onAbort);
|
|
312
|
+
child.close();
|
|
227
313
|
}
|
|
228
314
|
}
|
|
229
315
|
|
|
230
|
-
function
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
316
|
+
function addRule(doc: PolicyDocument, access: string, target: string): void {
|
|
317
|
+
const subpath = path.resolve(target);
|
|
318
|
+
if (doc.fs.some((rule) => rule.access === access && rule.target.subpath === subpath)) return;
|
|
319
|
+
doc.fs.push({ access, target: { subpath } });
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
export function filterViolations(violations: readonly Violation[]): Violation[] {
|
|
323
|
+
return violations.filter((violation) => {
|
|
324
|
+
if (violation.target?.startsWith("/")) return true;
|
|
325
|
+
return /AF_INET|AF_INET6/.test(violation.target ?? "");
|
|
326
|
+
});
|
|
235
327
|
}
|
|
236
328
|
|
|
237
|
-
function
|
|
238
|
-
|
|
239
|
-
if (
|
|
240
|
-
|
|
241
|
-
|
|
329
|
+
function isSandboxBlocked(outcome: ExecOutcome): boolean {
|
|
330
|
+
if (outcome.violations.length > 0) return true;
|
|
331
|
+
if (outcome.exitCode === 0) return false;
|
|
332
|
+
return /EACCES|EPERM|EROFS|Permission denied|Read-only file system/i.test(outcome.stderr.toString("utf8"));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
async function resolveExecutable(command: string): Promise<{ path: string; real: string }> {
|
|
336
|
+
const directories = (process.env.PATH ?? "").split(path.delimiter).filter(Boolean);
|
|
337
|
+
for (const directory of directories) {
|
|
338
|
+
const candidate = path.join(directory, command);
|
|
242
339
|
try {
|
|
243
|
-
|
|
244
|
-
return;
|
|
340
|
+
await fs.access(candidate, fsConstants.X_OK);
|
|
341
|
+
return { path: candidate, real: await fs.realpath(candidate) };
|
|
245
342
|
} catch {
|
|
246
|
-
|
|
343
|
+
// keep searching PATH
|
|
247
344
|
}
|
|
248
345
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
346
|
+
throw new FsError(`exec command not found: "${command}"`, "not-found");
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function cap(data: Buffer, limit: number): { data: Buffer; truncated: boolean } {
|
|
350
|
+
if (data.length <= limit) return { data, truncated: false };
|
|
351
|
+
return { data: data.subarray(0, limit), truncated: true };
|
|
255
352
|
}
|