opencode-fs-plugin 3.6.0 → 3.7.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 +140 -18
- package/SPEC.md +60 -4
- package/package.json +1 -1
- package/skills/filesystem/SKILL.md +304 -100
- package/src/core/types.ts +12 -0
- package/src/index.ts +9 -1
- package/src/tools/command.ts +234 -19
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ unit, end-to-end and coverage tests.
|
|
|
16
16
|
| `chmod` | `fs.chmod` | octal/symbolic mode, `reference`, `recursive`, `noDereference`, `verbose`, `changes`, `silent` |
|
|
17
17
|
| `chown` | `fs.chown` | `user`/`uid`, `group`/`gid`, `recursive`, `noDereference` |
|
|
18
18
|
| `comm` | `fs.comm` | `suppress1`/`2`/`3`, `outputDelimiter` |
|
|
19
|
-
| `command` | `fs.command` | `command`, `args`, `cwd`, `stdin`, `timeoutMs`; `fs_command` permission, procage sandbox, no shell |
|
|
19
|
+
| `command` | `fs.command` | `command`, `args`, `cwd`, `stdin`, `timeoutMs`, `envVars`; `fs_command` permission, procage sandbox, no shell; plugin `commandPaths` prepends trusted executable dirs and `commandEnvVars` adds fixed child environment entries that a call may overlay with `envVars` |
|
|
20
20
|
| `cp` | `fs.cp` | `recursive`, `archive`, `force`, `noClobber`, `update`, `preserve`, `dereference`, `verbose` |
|
|
21
21
|
| `cpio` | `fs.cpio` | `create`/`list`/`extract` (newc), `directory`, `members`, `input`, `member`, `verbose` |
|
|
22
22
|
| `curl` | `fs.curl` | `url`, `method`, `headers`, `body`, `json`, `output`, `checksum`, `maxBytes`, `timeoutMs`, `failOnError`, `followRedirects`, `maxRedirects` |
|
|
@@ -111,8 +111,13 @@ await tools.fs.pipe({
|
|
|
111
111
|
})
|
|
112
112
|
```
|
|
113
113
|
|
|
114
|
-
|
|
115
|
-
|
|
114
|
+
Registered stages are sources (`base64`, `cat`, `curl`, `dd`, `fold`, `nl`, `od`,
|
|
115
|
+
`paste`, `rev`, `shuf`, `strings`, `tac`), filters (`command`, `cut`, `grep`,
|
|
116
|
+
`gzip`, `gunzip`, `head`, `sed`, `sort`, `tail`, `tee`, `tr`, `uniq`), and sinks
|
|
117
|
+
(`cpio`, `hash`, `tar`, `wc`, `zip`). The first stage must be a source or receive
|
|
118
|
+
`input`; a filter first without `input` fails the whole call with
|
|
119
|
+
`ERROR: no input files and no stdin`, not pipeline data. `stages` holds 1–50
|
|
120
|
+
`{ tool, args }` entries.
|
|
116
121
|
|
|
117
122
|
`fs.curl` is a pipe source, so network data can feed the same stages:
|
|
118
123
|
|
|
@@ -159,21 +164,124 @@ resource. Deny `*` to close the set, then allow the names you need:
|
|
|
159
164
|
With no rule the base allow applies to every executable, so keep the `*` deny
|
|
160
165
|
when you want a closed set.
|
|
161
166
|
|
|
167
|
+
### `commandPaths`, `commandEnvVars`, and per-call `envVars`
|
|
168
|
+
|
|
169
|
+
`commandPaths` and `commandEnvVars` are plugin options, set once in the config.
|
|
170
|
+
`commandPaths` is config-only; `commandEnvVars` supplies defaults that one
|
|
171
|
+
`fs.command` call may overlay with its optional `envVars` argument. They touch
|
|
172
|
+
different surfaces:
|
|
173
|
+
|
|
174
|
+
| Option | Surface | What it changes |
|
|
175
|
+
|---|---|---|
|
|
176
|
+
| `commandPaths: string[]` | Executable lookup and child `PATH` | Prepends trusted absolute directories to executable lookup and to the child `PATH`, so a bare `command` name can resolve outside the host PATH |
|
|
177
|
+
| `commandEnvVars: Record<string, string>` | Child environment only | Adds fixed name/value entries to every `fs.command` child; it never changes lookup or `PATH` |
|
|
178
|
+
| `envVars: Record<string, string>` (per call) | Child environment only | Shallow overlay for this invocation: a same-named value replaces the configured one, unspecified configured entries remain, and it never changes lookup or `PATH` |
|
|
179
|
+
|
|
180
|
+
Combined example, modeled on a Homebrew + uv/podman setup:
|
|
181
|
+
|
|
182
|
+
```jsonc
|
|
183
|
+
{
|
|
184
|
+
"plugins": [
|
|
185
|
+
{
|
|
186
|
+
"package": "opencode-fs-plugin@<version>",
|
|
187
|
+
"options": {
|
|
188
|
+
"commandPaths": ["/var/home/linuxbrew/.linuxbrew/bin"],
|
|
189
|
+
"commandEnvVars": {
|
|
190
|
+
"PYTHONDONTWRITEBYTECODE": "1",
|
|
191
|
+
"HOMEBREW_NO_ENV_HINTS": "1",
|
|
192
|
+
"HOMEBREW_NO_ANALYTICS": "1",
|
|
193
|
+
"GOPATH": "{env:HOME}/.local/state/go",
|
|
194
|
+
"GOBIN": "{env:HOME}/.local/bin",
|
|
195
|
+
"PODMAN_COMPOSE_WARNING_LOGS": "false",
|
|
196
|
+
"DOCKER_HOST": "unix://{env:XDG_RUNTIME_DIR}/podman/podman.sock",
|
|
197
|
+
"UV_LINK_MODE": "copy",
|
|
198
|
+
"UV_ENV_FILE": ".env .env.creds",
|
|
199
|
+
"UV_LOCKED": "1"
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
]
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
**`commandPaths` — lookup and PATH.** Entries must be absolute, existing
|
|
208
|
+
directories and are canonicalized (a configuration symlink is trusted at its real
|
|
209
|
+
target), deduplicated in configured order, and prepended to both executable
|
|
210
|
+
lookup and the child `PATH`; the existing PATH follows them. Each root grants
|
|
211
|
+
procage `read` and `exec` for its whole directory tree and never pre-grants
|
|
212
|
+
`write`; a later write denial still goes through the normal `external_directory`
|
|
213
|
+
prompt. The existing exec rules also grant `exec` on the resolved binary's
|
|
214
|
+
directory and its parent; this option adds no read or write there. The bare-name
|
|
215
|
+
`fs_command` check is unchanged, so the option adds trusted locations, not
|
|
216
|
+
executable names. Entries must be absolute existing directories that are not the
|
|
217
|
+
filesystem root or the project root (or inside it), or plugin setup fails closed.
|
|
218
|
+
A project rooted at `/` therefore has no eligible entries: every absolute
|
|
219
|
+
directory is inside the workspace, so setup fails closed by design. Prepend only
|
|
220
|
+
narrow, trusted directories: a broad ancestor such as `$HOME` or its parent
|
|
221
|
+
grants broad read+exec, and prepending can shadow a same-named binary on PATH. An
|
|
222
|
+
executable symlink inside a configured directory is resolved before execution
|
|
223
|
+
under the same rules. When the option is unset or empty, resolution and the child
|
|
224
|
+
environment keep the preset PATH unchanged.
|
|
225
|
+
|
|
226
|
+
**`commandEnvVars` — child environment.** A closed object of environment variable
|
|
227
|
+
names to string values, added to every `fs.command` child, including `fs.pipe`
|
|
228
|
+
command stages and background jobs. They override a same-named value from the
|
|
229
|
+
explicit proxy list or procage's preset allowlist. The plugin does not explicitly
|
|
230
|
+
forward arbitrary caller `process.env` entries; procage's baseline allowlist
|
|
231
|
+
still supplies its normal variables. An empty string sets that variable to the
|
|
232
|
+
empty string; it does not unset or remove it. `PATH` and `TMPDIR` cannot be
|
|
233
|
+
configured: `commandPaths` owns lookup and PATH, and the tool owns each child's
|
|
234
|
+
private temp directory. Names must match `[A-Za-z_][A-Za-z0-9_]*`; values must be
|
|
235
|
+
strings without NUL bytes. Invalid configuration fails plugin setup. The values
|
|
236
|
+
above are non-secret examples, and none of them grants path, socket, or
|
|
237
|
+
filesystem access by itself: naming `unix://…/podman.sock` or `UV_ENV_FILE` does
|
|
238
|
+
not authorize that socket or file.
|
|
239
|
+
|
|
240
|
+
**`envVars` — per-invocation overlay.** `fs.command` also accepts an optional
|
|
241
|
+
`envVars` object with the same shape and validation rules. It is a shallow
|
|
242
|
+
overlay on `commandEnvVars` for that invocation only: a same-named value replaces
|
|
243
|
+
the configured one, unspecified configured entries remain, and an empty string
|
|
244
|
+
sets the variable to the empty string. It applies to direct calls, `fs.pipe`
|
|
245
|
+
command stages, and background jobs, and it never changes executable lookup or
|
|
246
|
+
`PATH`. Invalid shapes, names, non-string values, NUL bytes, `PATH`, or `TMPDIR`
|
|
247
|
+
make the call fail as invalid input before that command runs. It adds no filesystem
|
|
248
|
+
or network permission.
|
|
249
|
+
|
|
250
|
+
`{env:NAME}` is OpenCode's native configuration substitution, applied to config
|
|
251
|
+
strings before plugin setup; an unset variable becomes the empty string. That
|
|
252
|
+
substitution belongs to OpenCode, not to this plugin: `commandEnvVars` receives
|
|
253
|
+
the already-substituted text. The plugin does no interpolation itself, so a
|
|
254
|
+
literal `${NAME}` or `$NAME` in a configured value reaches the child unchanged
|
|
255
|
+
(for example, `UV_ENV_FILE` stays one literal string).
|
|
256
|
+
|
|
257
|
+
`commandEnvVars` and a call's `envVars` are trusted input and can change what the
|
|
258
|
+
allowlisted executable and its descendants do. The plugin itself does not
|
|
259
|
+
automatically add their values to its errors or result metadata, but the
|
|
260
|
+
executable and its descendants can read the values and may print or write them;
|
|
261
|
+
stdout and stderr are returned normally. Treat every configured or per-call value
|
|
262
|
+
as visible to that executable and its descendants.
|
|
263
|
+
|
|
162
264
|
The workspace (project) is read/write/exec. **Network egress needs Linux tier
|
|
163
265
|
T1** (user namespaces and TPROXY). Every run gets procage's transparent relay:
|
|
164
266
|
there is no direct route, and every TCP or UDP flow is named to the plugin as a
|
|
165
267
|
DNS QNAME, TLS SNI, HTTP `Host`, QUIC SNI, or an IP literal. Bare IPv6 literals
|
|
166
268
|
and bracketed IPv6 `Host` forms with an optional port are accepted as
|
|
167
|
-
resources. That exact resource must be approved through `
|
|
269
|
+
resources. That exact resource must be approved through `fs_command_network`; the
|
|
168
270
|
port, URL, headers, and payload are never part of the permission. There is no
|
|
169
|
-
separate network allowlist option
|
|
271
|
+
separate network allowlist option.
|
|
272
|
+
|
|
273
|
+
> **Migration from `fs_exec_network`:** replace every old rule with
|
|
274
|
+
> `fs_command_network` before upgrading. Old rules are ignored, and an unlisted
|
|
275
|
+
> `fs_command_network` resource can be allowed by the base rule unless another
|
|
276
|
+
> matching rule applies, so a previous `fs_exec_network` `deny *` no longer
|
|
277
|
+
> closes network egress.
|
|
170
278
|
|
|
171
279
|
```jsonc
|
|
172
280
|
{
|
|
173
281
|
"permissions": [
|
|
174
|
-
{ "action": "
|
|
175
|
-
{ "action": "
|
|
176
|
-
{ "action": "
|
|
282
|
+
{ "action": "fs_command_network", "resource": "*", "effect": "deny" },
|
|
283
|
+
{ "action": "fs_command_network", "resource": "registry.npmjs.org", "effect": "allow" },
|
|
284
|
+
{ "action": "fs_command_network", "resource": "pypi.org", "effect": "allow" }
|
|
177
285
|
]
|
|
178
286
|
}
|
|
179
287
|
```
|
|
@@ -202,12 +310,14 @@ External proxy settings are preserved: `HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`,
|
|
|
202
310
|
`NO_PROXY`, their lowercase forms, and the npm proxy variables are passed to the
|
|
203
311
|
child explicitly, because they are not in the preset's environment allowlist.
|
|
204
312
|
The relay treats the proxy as the destination, never its `CONNECT` target, so
|
|
205
|
-
an external proxy works only when `
|
|
313
|
+
an external proxy works only when `fs_command_network` allows the proxy's address
|
|
206
314
|
(or its cached DNS name, which also needs its DNS query allowed). That approval
|
|
207
315
|
lets the proxy forward to whatever it reaches; it is not scoped to the target
|
|
208
|
-
URL behind the proxy.
|
|
209
|
-
|
|
210
|
-
|
|
316
|
+
URL behind the proxy. Beyond the explicit proxy list and plugin-managed `TMPDIR`
|
|
317
|
+
(and `PATH` when `commandPaths` is set), the only caller-supplied entries passed
|
|
318
|
+
explicitly are the validated `commandEnvVars` and per-call `envVars`; arbitrary
|
|
319
|
+
caller `process.env` entries are not. The preset's own environment allowlist
|
|
320
|
+
still applies, and a proxy variable may itself carry proxy credentials.
|
|
211
321
|
|
|
212
322
|
External paths are separate: a blocked path asks `external_directory` for its
|
|
213
323
|
containing directory (or the nearest existing ancestor), and the sandbox grants
|
|
@@ -236,12 +346,24 @@ Unix sockets.
|
|
|
236
346
|
await tools.fs.command({ command: "npm", args: ["install"], timeoutMs: 300000 })
|
|
237
347
|
```
|
|
238
348
|
|
|
349
|
+
A per-call `envVars` overlays only the names it lists; everything else comes from
|
|
350
|
+
`commandEnvVars` and the sandbox baseline:
|
|
351
|
+
|
|
352
|
+
```js
|
|
353
|
+
await tools.fs.command({
|
|
354
|
+
command: "npm",
|
|
355
|
+
args: ["test"],
|
|
356
|
+
envVars: { NODE_ENV: "test" },
|
|
357
|
+
})
|
|
358
|
+
```
|
|
359
|
+
|
|
239
360
|
`command` is a bare name, `args` are literal, and `cwd` is sandboxed. Direct
|
|
240
361
|
calls return JSON with `exitCode`, `stdout`, `stderr`, `violations`, and the
|
|
241
362
|
network fields `network`, `networkMode`, `networkHosts`, and `networkReason`
|
|
242
363
|
(when networking was disabled); `externalPaths` lists the external roots the
|
|
243
|
-
run could write to.
|
|
244
|
-
`sort`, `tee`,
|
|
364
|
+
run could write to. The JSON does not echo the configured or per-call environment
|
|
365
|
+
values. A pipe stage returns raw stdout so it composes with `grep`, `sort`, `tee`,
|
|
366
|
+
and the rest.
|
|
245
367
|
|
|
246
368
|
```js
|
|
247
369
|
await tools.fs.pipe({
|
|
@@ -494,7 +616,7 @@ paths, absolute external boundaries, or network names/addresses. A specific
|
|
|
494
616
|
resource saves narrowly; an explicitly requested `*` remains `*`.
|
|
495
617
|
|
|
496
618
|
`fs.command` asserts `fs_command` with the executable name, asks
|
|
497
|
-
`external_directory` for any blocked path, and asks `
|
|
619
|
+
`external_directory` for any blocked path, and asks `fs_command_network` for the
|
|
498
620
|
exact DNS name or IP address the transparent relay named. There is no `*`
|
|
499
621
|
fallback and no direct-IP mode; networking that cannot use the relay is
|
|
500
622
|
disabled:
|
|
@@ -505,15 +627,15 @@ disabled:
|
|
|
505
627
|
{ "action": "fs_command", "resource": "*", "effect": "deny" },
|
|
506
628
|
{ "action": "fs_command", "resource": "npm", "effect": "allow" },
|
|
507
629
|
{ "action": "external_directory", "resource": "$HOME/.npm/**", "effect": "ask" },
|
|
508
|
-
{ "action": "
|
|
509
|
-
{ "action": "
|
|
630
|
+
{ "action": "fs_command_network", "resource": "*", "effect": "deny" },
|
|
631
|
+
{ "action": "fs_command_network", "resource": "registry.npmjs.org", "effect": "allow" }
|
|
510
632
|
]
|
|
511
633
|
}
|
|
512
634
|
```
|
|
513
635
|
|
|
514
636
|
Keep the rules explicit. Plugin actions match OpenCode's base
|
|
515
637
|
`{ "action": "*", "resource": "*", "effect": "allow" }` rule, so an unlisted
|
|
516
|
-
`fs_command`, `
|
|
638
|
+
`fs_command`, `fs_command_network`, or `external_directory` resource is allowed
|
|
517
639
|
without a prompt, not asked.
|
|
518
640
|
|
|
519
641
|
`fs.curl` reuses the built-in `webfetch` permission, with the requested
|
package/SPEC.md
CHANGED
|
@@ -150,6 +150,54 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
|
|
|
150
150
|
`fs_command` permission: the tool asserts that action with the bare name as its
|
|
151
151
|
resource. With no rule the base allow applies to every executable; a `*` deny
|
|
152
152
|
plus name allows closes the set.
|
|
153
|
+
- The plugin option `commandPaths` accepts absolute existing directory paths.
|
|
154
|
+
Each entry is canonicalized (a configuration symlink is trusted at its real
|
|
155
|
+
target), deduplicated in configured order, and rejected if it is the
|
|
156
|
+
filesystem root, the project root, or inside the project. A project rooted at
|
|
157
|
+
`/` therefore has no eligible entry: every absolute directory is inside the
|
|
158
|
+
workspace, so setup fails closed by design. Configured roots are
|
|
159
|
+
prepended, in order, to executable resolution and to the child `PATH`; the
|
|
160
|
+
existing PATH follows. Each root grants procage `read` and `exec` for its
|
|
161
|
+
whole directory tree and never pre-grants `write`; a later write denial still
|
|
162
|
+
goes through the normal `external_directory` prompt. The existing exec rules
|
|
163
|
+
also grant `exec` on the resolved binary's directory and its parent; this
|
|
164
|
+
option adds no read or write there. The option is config-level only: there is
|
|
165
|
+
no per-call path or arbitrary `env`/PATH field; the only invocation-level
|
|
166
|
+
environment input is the validated `envVars` overlay below. It never adds
|
|
167
|
+
names to the `fs_command` allowlist. Prepend only narrow, trusted directories:
|
|
168
|
+
a broad ancestor such as `$HOME` or its parent grants broad read+exec, and
|
|
169
|
+
prepending can shadow a same-named binary. An executable symlink inside a
|
|
170
|
+
configured directory is resolved before execution under the same rules.
|
|
171
|
+
Omitting or emptying the option leaves resolution, policy, and the child
|
|
172
|
+
environment unchanged.
|
|
173
|
+
- The plugin option `commandEnvVars` is a closed object of POSIX-style
|
|
174
|
+
environment variable names (`[A-Za-z_][A-Za-z0-9_]*`) to string values. Each
|
|
175
|
+
entry is added to every `fs.command` child, including `fs.pipe` command stages
|
|
176
|
+
and background jobs, and overrides a same-named value from the explicit proxy
|
|
177
|
+
list or procage's preset allowlist. The plugin does not explicitly forward
|
|
178
|
+
arbitrary caller `process.env` entries; procage's baseline allowlist still
|
|
179
|
+
supplies its normal variables. Empty strings set the variable to the empty
|
|
180
|
+
string; they do not unset or remove it. Values are literal: the plugin does no
|
|
181
|
+
interpolation or implicit forwarding of unrelated host variables. OpenCode's
|
|
182
|
+
own `{env:VARIABLE_NAME}` config substitution may apply before plugin setup,
|
|
183
|
+
and an unset variable becomes the empty string. Non-string
|
|
184
|
+
values, NUL bytes, invalid names, arrays or non-objects are rejected, and
|
|
185
|
+
`PATH` and `TMPDIR` are refused
|
|
186
|
+
because `commandPaths` owns lookup/PATH and the tool owns each child's private
|
|
187
|
+
temp directory. Invalid configuration fails plugin setup.
|
|
188
|
+
- `fs.command` also accepts an optional per-invocation `envVars` object with the
|
|
189
|
+
same shape, validation, and failure behavior. It is a shallow overlay on
|
|
190
|
+
`commandEnvVars`: a same-named value wins, unspecified configured entries
|
|
191
|
+
remain, and an empty string sets the variable to the empty string. It applies
|
|
192
|
+
to direct calls, `fs.pipe` command stages, and background jobs. It never
|
|
193
|
+
changes executable lookup or `PATH` and adds no filesystem or network
|
|
194
|
+
permission. Invalid input fails the call before that command runs.
|
|
195
|
+
- `commandEnvVars` and `envVars` are trusted input and can change what the
|
|
196
|
+
allowlisted executable and its descendants do. The plugin does not
|
|
197
|
+
automatically add their values to its own errors or result metadata, but the
|
|
198
|
+
executable and its descendants can read them and may print or write them;
|
|
199
|
+
stdout and stderr are returned normally. Treat every configured or per-call
|
|
200
|
+
value as visible to that executable and its descendants.
|
|
153
201
|
- The policy is procage `agent_workspace` v3: the project is read/write/exec.
|
|
154
202
|
External paths are denied by default and there are no per-call grants.
|
|
155
203
|
- Network egress uses procage's transparent relay on Linux tier T1. A per-run
|
|
@@ -159,9 +207,14 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
|
|
|
159
207
|
- Every TCP/UDP flow is named as a DNS QNAME, TLS SNI, HTTP `Host`, QUIC SNI, or
|
|
160
208
|
IP literal. Bare IPv6 literals and bracketed IPv6 `Host` forms with an
|
|
161
209
|
optional port pass resource validation. That exact resource is asserted with
|
|
162
|
-
`
|
|
210
|
+
`fs_command_network`; the port, protocol, URL, headers, and payload never reach
|
|
163
211
|
the permission. The permission rules are the only network allowlist; there is
|
|
164
212
|
no plugin option for it.
|
|
213
|
+
- **Migration from `fs_exec_network`:** replace every old rule with
|
|
214
|
+
`fs_command_network` before upgrading. Old rules are ignored; an unlisted
|
|
215
|
+
`fs_command_network` resource can fall back to the base allow unless another
|
|
216
|
+
matching rule applies, so an old `fs_exec_network` `deny *` no longer closes
|
|
217
|
+
egress.
|
|
165
218
|
- DNS queries are decisions too. An allowed QNAME is forwarded to the parent
|
|
166
219
|
resolver; procage's discovery reads `/etc/resolv.conf` and systemd-resolved
|
|
167
220
|
paths, including a loopback stub, and the plugin leaves `relay_dns_upstream`
|
|
@@ -190,9 +243,12 @@ Aliases are accepted only where noted: `path` for one `paths` element (`stat`,
|
|
|
190
243
|
`NO_PROXY`, lowercase forms, and npm proxy variables) are passed explicitly
|
|
191
244
|
because they are not in the preset environment allowlist. The relay treats
|
|
192
245
|
the proxy as the destination, never its `CONNECT` target, so the proxy's
|
|
193
|
-
address or cached DNS name must be allowed in `
|
|
194
|
-
|
|
195
|
-
|
|
246
|
+
address or cached DNS name must be allowed in `fs_command_network`. Beyond the
|
|
247
|
+
explicit proxy list and plugin-managed `TMPDIR` (and `PATH` when `commandPaths`
|
|
248
|
+
is set), the only caller-supplied entries passed explicitly are the validated
|
|
249
|
+
`commandEnvVars` and per-call `envVars`; arbitrary caller `process.env` entries
|
|
250
|
+
are not. The preset's own allowlist still applies, and a proxy variable may
|
|
251
|
+
itself carry credentials.
|
|
196
252
|
- Denied paths ask `external_directory` on their containing directory (or the
|
|
197
253
|
nearest existing ancestor for a missing or redacted path) only when procage
|
|
198
254
|
reports a structured **write** violation (`file-write-*`). Read, exec,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: Filesystem
|
|
3
|
-
description: Use when a task needs Linux-style filesystem, text, network, process, or system operations
|
|
3
|
+
description: Use when a task needs Linux-style filesystem, text, network, process, or system operations through the `fs` namespace - call `tools.fs.*` under `execute` or direct `fs_*` tools for paths, metadata, copy/move/remove, permissions, text filters and pipes, archives, hashing, comparing files/directories (`fs.diff`/`fs.comm`), HTTP(S) fetches, process listing, or the sandboxed `command` runner; includes `commandPaths`/`commandEnvVars`/per-call `envVars`, background jobs, and exact per-tool argument schemas, field types, bounds, required/optional fields, returns, and exit codes.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
6
|
# Filesystem
|
|
@@ -25,7 +25,7 @@ With `execute` allowed, call it as `tools.fs.<command>`; agents without
|
|
|
25
25
|
| Regex substitution or in-place edit | `fs.sed` |
|
|
26
26
|
| Recoverable deletion | `fs.trash` |
|
|
27
27
|
| `curl` fetch, API call, or file download | `fs.curl` |
|
|
28
|
-
| Allowlisted package
|
|
28
|
+
| Allowlisted CLI or package tool | `fs.command` (no shell; names allowed by `fs_command`) |
|
|
29
29
|
| Many independent fs calls | one multi-path call, else `Promise.all` in `execute` |
|
|
30
30
|
| Sequential byte pipeline | `fs.pipe` |
|
|
31
31
|
|
|
@@ -63,67 +63,182 @@ what `fs.stat`, `fs.ls`, `fs.hash`, `fs.diff`, or `fs.cat` can confirm.
|
|
|
63
63
|
| cpio / ar | `fs.cpio`, `fs.ar` (`create`/`list`/`extract`) |
|
|
64
64
|
| Background jobs | `background: true`, then `fs.job` |
|
|
65
65
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
|
88
|
-
|
|
89
|
-
| `
|
|
90
|
-
| `
|
|
91
|
-
| `
|
|
92
|
-
| `
|
|
93
|
-
| `
|
|
94
|
-
| `
|
|
95
|
-
| `
|
|
96
|
-
| `
|
|
97
|
-
| `
|
|
98
|
-
| `
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
`fs.
|
|
106
|
-
`fs.
|
|
107
|
-
`
|
|
108
|
-
|
|
66
|
+
## Argument reference
|
|
67
|
+
|
|
68
|
+
Every registered tool is listed below with its real input schema. `?` marks an
|
|
69
|
+
optional field; a field without `?` is required. Types are `str`, `int`, `bool`,
|
|
70
|
+
`str[]`, `int[]`, `enum(a, b)`, `obj`; Notes give real bounds and defaults.
|
|
71
|
+
Canonical names are the schema names. These aliases exist and no others: `path`
|
|
72
|
+
for one `paths` element (`fs.stat`, `fs.ls`, `fs.cat`, `fs.find`, `fs.tee`,
|
|
73
|
+
`fs.grep`, `fs.sed`), `source` for one `sources` element (`fs.cp`, `fs.mv`,
|
|
74
|
+
`fs.ln`), `expression` for one `expressions` element (`fs.sed`), `limit` for
|
|
75
|
+
`maxCount` (`fs.grep`), and `paths`/`path` for `files` (`fs.hash`). `fs.mkdir`
|
|
76
|
+
accepts `path` or `paths`, but not both.
|
|
77
|
+
|
|
78
|
+
- There are no positional arguments and no `-` flags; every option is a named
|
|
79
|
+
field, and enum values are exact lowercase strings.
|
|
80
|
+
- The host drops unknown fields, so a misspelled field silently runs the call
|
|
81
|
+
without that option. Check the exact name here instead of guessing.
|
|
82
|
+
- Every tool except `fs.job` also accepts `background? bool`; a pipe stage's
|
|
83
|
+
`args` does not (see Background jobs).
|
|
84
|
+
|
|
85
|
+
### Paths, metadata, links
|
|
86
|
+
|
|
87
|
+
| Tool | Fields | Notes |
|
|
88
|
+
|---|---|---|
|
|
89
|
+
| `fs.stat` | `paths? str[]`, `path? str`, `follow? bool`, `format? str`, `terse? bool`, `filesystem? bool` | Runtime requires `paths` or `path` even though the schema marks both optional. `format` is printf-style (`%n %s %a %y`); `%s` is apparent bytes. |
|
|
90
|
+
| `fs.ls` | `paths? str[]` (default `["."]`), `path? str`, `all? bool`, `almostAll? bool`, `long? bool`, `human? bool`, `recursive? bool`, `directory? bool`, `inode? bool`, `classify? bool`, `size? bool`, `numericIds? bool`, `noGroup? bool`, `noOwner? bool`, `slash? bool`, `quote? bool`, `versionSort? bool`, `extensionSort? bool`, `fullTime? bool`, `timeStyle? enum(full-iso, long-iso, iso, locale)`, `hide? str[]`, `ignore? str[]`, `groupDirectoriesFirst? bool`, `sortBy? enum(name, size, time, none)`, `reverse? bool`, `onePerLine? bool` | `sortBy` default `name`; `hide`/`ignore` are name globs. |
|
|
91
|
+
| `fs.file` | `paths str[]` (min 1), `mime? bool` | Content magic detection. |
|
|
92
|
+
| `fs.find` | `paths? str[]` (default `["."]`), `path? str`, `name? str`, `iname? str`, `pathPattern? str`, `lname? str`, `ilname? str`, `regex? str`, `iregex? str`, `type? enum(f, d, l, p, s)`, `size? str` (`+10k`), `mtime? str` (`+7`), `mmin? str`, `maxDepth? int` (min 0), `minDepth? int` (min 0), `empty? bool`, `perm? str`, `uid? int`, `gid? int`, `user? str`, `group? str`, `links? str` (`+1`), `inum? int`, `samefile? str`, `fstype? str`, `newer? str`, `newerXY? str`, `printf? str`, `follow? bool`, `print0? bool`, `sort? bool` (default true), `action? enum(print, delete, trash)`, `recursive? bool` | `recursive: true` is legal only with `action: "delete"` and is needed to remove matched directories. `delete`/`trash` reject `printf`/`print0` and never remove the starting directory. `newerXY` is `XY:path`, e.g. `mm:/tmp/ref`. |
|
|
93
|
+
| `fs.du` | `paths? str[]` (default `["."]`), `summarize? bool`, `all? bool`, `human? bool`, `total? bool`, `maxDepth? int` (min 0), `follow? bool`, `oneFileSystem? bool` | Reports allocated blocks; `fs.stat %s` gives apparent bytes. |
|
|
94
|
+
| `fs.df` | `paths? str[]` (default `["."]`), `human? bool`, `inodes? bool`, `printType? bool`, `all? bool` | |
|
|
95
|
+
| `fs.findmnt` | `paths? str[]`, `fsType? str`, `noHeader? bool` | Mounts containing the paths. |
|
|
96
|
+
| `fs.mountpoint` | `paths str[]` (min 1), `quiet? bool` | Exit code 1 when a path is not a mount point. |
|
|
97
|
+
| `fs.readlink` | `paths str[]` (min 1), `mode? enum(raw, canonical, existing, missing)`, `noNewline? bool` | `mode` default `raw`. |
|
|
98
|
+
| `fs.sync` | `paths? str[]` (default `["."]`) | fsync files or directories. |
|
|
99
|
+
|
|
100
|
+
### Copy, move, remove, permissions, times
|
|
101
|
+
|
|
102
|
+
| Tool | Fields | Notes |
|
|
103
|
+
|---|---|---|
|
|
104
|
+
| `fs.cp` | `sources? str[]`, `source? str`, `destination str`, `recursive? bool`, `archive? bool`, `force? bool`, `noClobber? bool`, `update? bool`, `preserve? bool`, `dereference? bool` (default true), `verbose? bool` | Needs `sources` or `source`. Directory sources need `recursive` or `archive`; `archive` implies recursive+preserve+keep symlinks. Multiple sources need an existing directory destination. |
|
|
105
|
+
| `fs.mv` | `sources? str[]`, `source? str`, `destination str`, `force? bool`, `noClobber? bool`, `update? bool`, `verbose? bool` | Same source rule; multiple sources need an existing directory destination. |
|
|
106
|
+
| `fs.ln` | `sources? str[]`, `source? str`, `destination str`, `symbolic? bool`, `force? bool`, `noDereference? bool`, `relative? bool`, `verbose? bool` | Hard link by default; `symbolic: true` for a symlink. |
|
|
107
|
+
| `fs.mkdir` | `path? str`, `paths? str[]` (min 1), `parents? bool` (default false), `mode? str`, `verbose? bool` | Use `path` or `paths`, not both. An existing directory fails unless `parents: true` (then it is a no-op). |
|
|
108
|
+
| `fs.mktemp` | `template? str` (default `tmp.XXXXXXXXXX`), `directory? bool`, `tmpdir? str`, `suffix? str` | The template must END with at least three `X`; `suffix` must be one path component. The created path is printed. |
|
|
109
|
+
| `fs.rm` | `paths str[]` (min 1), `recursive? bool`, `force? bool`, `dir? bool`, `verbose? bool` | Permanent. Directory removal needs `recursive` (or `dir` for empty dirs). |
|
|
110
|
+
| `fs.rmdir` | `paths str[]` (min 1), `parents? bool`, `ignoreNonEmpty? bool`, `verbose? bool` | Empty directories only. |
|
|
111
|
+
| `fs.trash` | `paths str[]` (min 1), `force? bool` | XDG home trash; refuses cross-device moves. |
|
|
112
|
+
| `fs.chmod` | `paths str[]` (min 1), `mode? str`, `reference? str`, `recursive? bool`, `noDereference? bool`, `verbose? bool`, `changes? bool`, `silent? bool` | `mode` or `reference` is required. `mode` is octal (`755`) or symbolic (`u=rwx,go=rx`). |
|
|
113
|
+
| `fs.chown` | `paths str[]` (min 1), `user? str`, `uid? int` (min 0), `group? str`, `gid? int` (min 0), `recursive? bool`, `noDereference? bool` | `user`/`uid` are mutually exclusive, as are `group`/`gid`; at least one of user or group is required. |
|
|
114
|
+
| `fs.touch` | `paths str[]` (min 1), `accessOnly? bool`, `modifyOnly? bool`, `noCreate? bool`, `date? str`, `timestamp? str`, `reference? str`, `noDereference? bool` | `date` is `Date.parse` text or `@epoch`; `timestamp` is GNU `[[CC]YY]MMDDhhmm[.ss]`; `reference` copies another file's times. |
|
|
115
|
+
| `fs.truncate` | `paths str[]` (min 1), `size? str`, `reference? str`, `noCreate? bool`, `noDereference? bool` | `size` or `reference` is required. `size` accepts `10M` or a relative `+1K`/`-1K`. |
|
|
116
|
+
|
|
117
|
+
### Read, transform, compare text
|
|
118
|
+
|
|
119
|
+
| Tool | Fields | Notes |
|
|
120
|
+
|---|---|---|
|
|
121
|
+
| `fs.cat` | `paths? str[]`, `path? str`, `number? bool`, `numberNonBlank? bool`, `squeezeBlank? bool`, `showEnds? bool`, `showTabs? bool`, `showNonPrinting? bool` | With no `paths` it reads stdin; Code Mode has no stdin outside `fs.pipe`, so `fs.cat({})` fails. |
|
|
122
|
+
| `fs.head` | `paths? str[]`, `lines? int` (min 1, default 10), `skip? int` (min 0, default 0), `bytes? int` (min 1), `quiet? bool`, `verbose? bool` | `lines` XOR `bytes`; `skip` applies to lines only and is rejected with `bytes`. Range output is lines `skip+1 … skip+lines`. |
|
|
123
|
+
| `fs.tail` | `paths? str[]`, `lines? int` (min 1, default 10), `bytes? int` (min 1), `fromStart? bool`, `quiet? bool`, `verbose? bool`, `follow? bool`, `timeoutMs? int` (100–600000, default 10000), `intervalMs? int` (50–10000, default 250), `maxBytes? int` (min 1, default 1048576) | `lines` XOR `bytes`. `fromStart` makes `lines` a 1-based start line. `follow` needs exactly one file. |
|
|
124
|
+
| `fs.nl` | `paths? str[]`, `all? bool`, `start? int` (min 1, default 1), `width? int` (1–20, default 6), `separator? str` (default tab) | |
|
|
125
|
+
| `fs.tac` | `paths? str[]` | Reverse line order. |
|
|
126
|
+
| `fs.rev` | `paths? str[]` | Reverse characters per line. |
|
|
127
|
+
| `fs.fold` | `paths? str[]`, `width? int` (1–10000, default 80), `spaces? bool` | Wrap at spaces when `spaces`. |
|
|
128
|
+
| `fs.cut` | `paths? str[]`, `fields? str` (`1,3-5`), `characters? str`, `bytes? str`, `delimiter? str` (default tab), `outputDelimiter? str`, `complement? bool`, `onlyDelimited? bool` | Use one of fields/characters/bytes. |
|
|
129
|
+
| `fs.paste` | `paths? str[]`, `delimiters? str` (default tab), `serial? bool` | |
|
|
130
|
+
| `fs.comm` | `left str`, `right str`, `suppress1? bool`, `suppress2? bool`, `suppress3? bool`, `outputDelimiter? str` (default tab) | Inputs must be sorted. |
|
|
131
|
+
| `fs.tr` | `paths? str[]`, `set1 str`, `set2? str`, `delete? bool`, `squeeze? bool`, `complement? bool` | Ranges and character classes supported. |
|
|
132
|
+
| `fs.sed` | `script? str`, `expression? str`, `expressions? str[]`, `paths? str[]`, `path? str`, `inPlace? bool`, `extendedRegexp? bool`, `quiet? bool` | Requires `script`/`expression`/`expressions`. Only `s<delim>pattern<delim>replacement<delim>flags` with `g`, `i`, `p`. `inPlace` requires `paths`. |
|
|
133
|
+
| `fs.sort` | `paths? str[]`, `numeric? bool`, `generalNumeric? bool`, `humanNumeric? bool`, `version? bool`, `reverse? bool`, `unique? bool`, `ignoreCase? bool`, `dictionary? bool`, `ignoreNonPrinting? bool`, `ignoreLeadingBlanks? bool`, `stable? bool`, `check? bool`, `checkQuiet? bool`, `zeroTerminated? bool`, `keys? str[]`, `field? int` (min 1), `separator? str` | `keys` use GNU `-k` syntax (`2,2n`); `field` is a 1-based single field. |
|
|
134
|
+
| `fs.uniq` | `paths? str[]`, `count? bool`, `repeated? bool`, `uniqueOnly? bool`, `ignoreCase? bool`, `skipFields? int` (min 0), `skipChars? int` (min 0) | Compares adjacent lines; sort first for global uniqueness. |
|
|
135
|
+
| `fs.shuf` | `paths? str[]`, `headCount? int` (min 1), `zeroTerminated? bool` | |
|
|
136
|
+
| `fs.wc` | `paths? str[]`, `lines? bool`, `words? bool`, `bytes? bool`, `chars? bool`, `maxLineLength? bool` | |
|
|
137
|
+
| `fs.grep` | `pattern str`, `paths? str[]`, `path? str`, `recursive? bool`, `include? str[]`, `exclude? str[]`, `ignoreCase? bool`, `invert? bool`, `lineNumbers? bool`, `withFilename? bool`, `noFilename? bool`, `filesWithMatches? bool`, `filesWithoutMatch? bool`, `count? bool`, `onlyMatching? bool`, `word? bool`, `line? bool`, `fixed? bool`, `extended? bool`, `context? int` (min 0), `before? int` (min 0), `after? int` (min 0), `maxCount? int` (min 1), `limit? int` (min 1), `noMessages? bool` | A directory path needs `recursive: true`. Exit code 1 when nothing matches. |
|
|
138
|
+
| `fs.tee` | `paths? str[]`, `path? str`, `append? bool`, `content? str` | Needs `paths`, `content`, or stdin. Empty `paths` passes through only. |
|
|
139
|
+
| `fs.strings` | `paths? str[]`, `minLength? int` (1–255, default 4) | |
|
|
140
|
+
| `fs.base64` | `paths? str[]`, `decode? bool`, `wrap? int` (0–4096, default 76), `ignoreGarbage? bool` | `wrap: 0` disables wrapping. |
|
|
141
|
+
| `fs.od` | `paths? str[]`, `offset? int` (min 0, default 0), `length? int` (min 1), `width? int` (1–64, default 16) | |
|
|
142
|
+
| `fs.split` | `input? str`, `prefix? str` (default `x`), `lines? int` (min 1), `bytes? int` (min 1), `suffixLength? int` (1–16, default 2), `numeric? bool`, `additionalSuffix? str` | Exactly one of `lines` or `bytes`. Chunk capacity is checked before writing. |
|
|
143
|
+
| `fs.dd` | `input? str`, `output? str`, `blockSize? int` (1–16777216, default 512), `count? int` (min 1), `skip? int` (min 0), `seek? int` (min 0), `status? bool` | Needs `input` or stdin; omit `output` to write stdout. No conversion flags. |
|
|
144
|
+
| `fs.diff` | `left str`, `right str`, `unified? int` (min 0, default 3), `brief? bool`, `recursive? bool`, `ignoreWhitespace? bool`, `ignoreCase? bool`, `exclude? str[]` | Exit code 1 when files differ; that is a result, not a failure. |
|
|
145
|
+
|
|
146
|
+
### Archives and hashing
|
|
147
|
+
|
|
148
|
+
| Tool | Fields | Notes |
|
|
149
|
+
|---|---|---|
|
|
150
|
+
| `fs.tar` | `mode enum(create, extract, list)`, `archive str`, `paths? str[]`, `directory? str`, `gzip? bool`, `stripComponents? int` (min 0), `exclude? str[]`, `verbose? bool`, `input? str`, `member? str` | Create needs `paths`, `input`, or stdin. `gzip` is inferred from `.gz`/`.tgz`. Create resolves relative paths under `directory`. |
|
|
151
|
+
| `fs.zip` | `archive str`, `paths? str[]`, `level? int` (0–9), `input? str`, `member? str` | Recursive by default. Create needs `paths`, `input`, or stdin. |
|
|
152
|
+
| `fs.unzip` | `archive str`, `mode? enum(extract, list)`, `directory? str`, `overwrite? bool`, `noOverwrite? bool` | `mode` default `extract`; `directory` is the destination. |
|
|
153
|
+
| `fs.cpio` | `mode enum(create, list, extract)`, `archive str`, `paths? str[]`, `directory? str`, `members? str[]`, `input? str`, `member? str`, `verbose? bool` | newc format; create needs `paths`, `input`, or stdin. |
|
|
154
|
+
| `fs.ar` | `mode enum(create, list, extract)`, `archive str`, `paths? str[]`, `directory? str`, `members? str[]` | GNU short names (16 bytes max). Create needs `paths`. |
|
|
155
|
+
| `fs.gzip` | `paths? str[]`, `keep? bool`, `force? bool`, `stdout? bool`, `level? int` (0–9, default 6) | File mode writes `<name>.gz` and removes the source unless `keep`. |
|
|
156
|
+
| `fs.gunzip` | `paths? str[]`, `keep? bool`, `force? bool`, `stdout? bool`, `suffix? str` (default `.gz`) | Input must end with `suffix`; strips it and removes the source unless `keep`. |
|
|
157
|
+
| `fs.hash` | `files? str[]`, `paths? str[]`, `path? str`, `algorithm? enum(md5, sha1, sha256, sha512)`, `tag? bool` | Canonical is `files`; `algorithm` default `sha256`. Omit files to hash stdin. |
|
|
158
|
+
|
|
159
|
+
### Processes, system, HTTP
|
|
160
|
+
|
|
161
|
+
| Tool | Fields | Notes |
|
|
162
|
+
|---|---|---|
|
|
163
|
+
| `fs.ps` | `pids? int[]` (min 1), `user? str`, `command? str`, `sortBy? enum(pid, rss, start)`, `limit? int` (min 1), `noHeader? bool`, `format? enum(table, json)` | `sortBy` default `pid`; `format` default `table`; `command` is a regex. |
|
|
164
|
+
| `fs.pgrep` | `pattern str`, `full? bool`, `ignoreCase? bool`, `user? str`, `newest? bool`, `oldest? bool`, `count? bool`, `delimiter? str` | Exit code 1 when nothing matches; `delimiter` default newline. |
|
|
165
|
+
| `fs.kill` | `pids int[]` (each min 2), `signal? enum(HUP, INT, QUIT, KILL, TERM, USR1, USR2, STOP, CONT, CHLD)`, `dryRun? bool` | Signal default `TERM`; pid 0, 1, and process groups are rejected. |
|
|
166
|
+
| `fs.id` | `only? enum(user, group, groups)` | |
|
|
167
|
+
| `fs.uname` | `kernel? bool`, `nodename? bool`, `release? bool`, `version? bool`, `machine? bool` | |
|
|
168
|
+
| `fs.uptime` | `pretty? bool`, `loadAverage? bool` | |
|
|
169
|
+
| `fs.date` | `value? str`, `utc? bool`, `format? enum(iso, unix, rfc, date)` | `value` is `Date.parse` text or `@epoch`; `format` default `iso`. |
|
|
170
|
+
| `fs.curl` | `url str`, `method? enum(GET, POST, PUT, PATCH, DELETE, HEAD)`, `headers? obj`, `body? str`, `json? any`, `output? str`, `checksum? str`, `maxBytes? int` (min 1), `timeoutMs? int` (100–600000, default 30000), `failOnError? bool` (default true), `followRedirects? bool` (default true), `maxRedirects? int` (0–20, default 10) | `headers` maps names to string values; `body` and `json` are mutually exclusive. `checksum` is `<md5\|sha1\|sha256\|sha512>:<hex>`; bare hex means sha256. `output` streams to a temp file then renames. |
|
|
171
|
+
|
|
172
|
+
### Execution and orchestration
|
|
173
|
+
|
|
174
|
+
| Tool | Fields | Notes |
|
|
175
|
+
|---|---|---|
|
|
176
|
+
| `fs.command` | `command str`, `args? str[]`, `cwd? str`, `stdin? str`, `timeoutMs? int` (100–600000, default 120000), `envVars? obj` (name → str) | `command` is a bare executable name with no path or shell syntax. `args` are literal argv; `cwd` defaults to the project root and is sandboxed; `stdin` is used only outside a pipe. `envVars` is a per-call shallow overlay on `commandEnvVars` with the same validation; `commandPaths`/`commandEnvVars` are plugin options. There is no arbitrary `env` or per-call PATH field. |
|
|
177
|
+
| `fs.pipe` | `stages` 1–50 × `{ tool str, args? obj }`, `input? str`, `output? str`, `append? bool` | Stage names are short (`cat`, not `fs.cat`) and only the registered stage set is accepted (see Pipe). `output`/`append` act as a final `tee` under `fs_tee`. |
|
|
178
|
+
| `fs.job` | `id? str`, `wait? bool`, `cancel? bool`, `list? bool`, `timeoutMs? int` (1–600000, default 600000) | Needs `id` or `list`. `list` cannot combine with `id`/`wait`/`cancel`; `cancel` and `wait` are mutually exclusive. Permission `fs_job`. |
|
|
109
179
|
|
|
110
180
|
### Common mistakes
|
|
111
181
|
|
|
112
|
-
-
|
|
113
|
-
|
|
182
|
+
- Code Mode has no implicit stdin. For a direct call, pass the input field(s)
|
|
183
|
+
that tool's schema requires: usually `paths`, but also `files` (`fs.hash`),
|
|
184
|
+
`input` (`fs.split`, `fs.dd`), or `left`/`right` (`fs.diff`, `fs.comm`). With no
|
|
185
|
+
input, tools that expect stdin fail, for example `fs.cat({})` returns
|
|
186
|
+
`ERROR: no input files and no stdin.` Use `fs.pipe({ input, ... })` to supply
|
|
187
|
+
stdin to a pipe source or filter.
|
|
188
|
+
- `fs.hash` canonical is `files`; `paths` and `path` are accepted aliases. `hash`
|
|
189
|
+
needs `files` or pipe stdin.
|
|
190
|
+
- `fs.mkdir` canonical is `path`; `paths` is accepted but cannot be combined, and
|
|
191
|
+
an existing directory is an error unless `parents: true`.
|
|
192
|
+
- `fs.stat` accepts `paths` or `path`; omitting both fails even though both are
|
|
193
|
+
schema-optional.
|
|
194
|
+
- `fs.cp`/`fs.mv`/`fs.ln` use `sources` (or one `source`) plus `destination`;
|
|
195
|
+
there is no `from`/`to`.
|
|
114
196
|
- `fs.sed` canonical is `script`/`expressions`; `expression` is accepted. Only
|
|
115
197
|
`s/.../.../flags` is supported; line ranges use `fs.head({ skip, lines })` or
|
|
116
198
|
`fs.tail`.
|
|
117
|
-
- `fs.grep` canonical is `maxCount`; `limit` is accepted.
|
|
118
|
-
for
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
- `fs.
|
|
123
|
-
|
|
124
|
-
|
|
199
|
+
- `fs.grep` canonical is `maxCount`; `limit` is accepted. `pattern` is required,
|
|
200
|
+
and `maxCount`/`limit` must be at least 1: there is no `0` for "unlimited", so
|
|
201
|
+
omit the field instead. Add `recursive: true` for a directory.
|
|
202
|
+
- `fs.find.type` takes one GNU letter (`f`, `d`, `l`, `p`, `s`), not a word such
|
|
203
|
+
as `file` or `dir`; any other value fails the call as invalid input.
|
|
204
|
+
- `fs.find` defaults to `action: "print"`; `recursive: true` is not a delete
|
|
205
|
+
switch and is legal only together with `action: "delete"`. To delete, pass both
|
|
206
|
+
`action: "delete"` and `recursive: true`; neither one removes a matched
|
|
207
|
+
directory on its own. `delete`/`trash` never remove the starting directory, so
|
|
208
|
+
use `fs.rm` if you mean the root.
|
|
209
|
+
- `fs.readlink` requires `paths` (at least one); it has no single `path` alias.
|
|
210
|
+
- `fs.zip` has no `directory` field: use `archive` plus `paths`/`input`/`member`.
|
|
211
|
+
`directory` belongs to `fs.unzip` (and to `fs.tar`/`fs.cpio` create/extract).
|
|
212
|
+
- `fs.curl` rejects a body over `maxBytes` with
|
|
213
|
+
`ERROR: Response exceeded maxBytes (...)`; the host call still completes, so
|
|
214
|
+
check that text (and `metadata.ok` where exposed) rather than assuming a
|
|
215
|
+
successful download.
|
|
216
|
+
- An unknown or stale `fs.job` id returns `ERROR: job not found: <id>` as normal
|
|
217
|
+
content, not a host-level rejection; `fs.job({ list: true })` shows the ids
|
|
218
|
+
still retained for this session.
|
|
219
|
+
- A missing path in `fs.ls`/`fs.stat`/`fs.cat` returns `ERROR: Not found: ...`;
|
|
220
|
+
that is not a permission problem. Permission asks and denials happen at the
|
|
221
|
+
host layer instead of producing an `ERROR:` string, so fix the path or the
|
|
222
|
+
rule rather than retrying field names.
|
|
223
|
+
- For `fs.command`, a malformed name returns
|
|
224
|
+
`ERROR: command must be a bare executable name: "<name>"` (schema-type errors
|
|
225
|
+
still surface as `ERROR: Invalid ...`), an allowed name with no matching
|
|
226
|
+
binary is `ERROR: exec command not found`, and a name that `fs_command` does
|
|
227
|
+
not allow is a permission decision, not an argument error; allow the bare
|
|
228
|
+
executable name rather than changing `args`.
|
|
229
|
+
- `fs.head` range is `skip` lines skipped, then `lines` lines (default 10), so
|
|
230
|
+
`20–40` is `skip: 19, lines: 21`.
|
|
125
231
|
- `fs.mktemp` needs a template ending in at least three `X`; use `suffix` for an
|
|
126
232
|
extension.
|
|
233
|
+
- `fs.chmod` needs `mode` or `reference`; `fs.truncate` needs `size` or
|
|
234
|
+
`reference`; `fs.split` needs exactly one of `lines`/`bytes`; `fs.tee` needs
|
|
235
|
+
`paths`, `content`, or stdin.
|
|
236
|
+
- `fs.command` does not accept a path, arbitrary `env`, or per-call PATH. Use
|
|
237
|
+
its validated `envVars` overlay for selected child variables, and configure
|
|
238
|
+
`commandPaths`/`commandEnvVars` in the plugin options.
|
|
239
|
+
- There is no `fs.write`; use the built-in `write` or `fs.tee({ paths, content })`.
|
|
240
|
+
- Do not expect a JSON envelope from ordinary tools: a call resolves to text.
|
|
241
|
+
`fs.command`, background starts, and `fs.job` are the exceptions.
|
|
127
242
|
|
|
128
243
|
### Shell patterns and their fs equivalents
|
|
129
244
|
|
|
@@ -165,15 +280,66 @@ you need:
|
|
|
165
280
|
|
|
166
281
|
With no rule the base allow applies to every executable.
|
|
167
282
|
|
|
283
|
+
Executable lookup can be widened once in the plugin options with
|
|
284
|
+
`commandPaths`: absolute trusted directories are canonicalized (a configuration
|
|
285
|
+
symlink is trusted at its real target), deduplicated, and prepended in order to
|
|
286
|
+
both lookup and the child `PATH`. Each root grants read+exec for its whole
|
|
287
|
+
directory tree and never pre-grants write; the existing exec rules also add exec
|
|
288
|
+
on the resolved binary's directory and parent, with no target read/write from
|
|
289
|
+
this option. The option adds no executable names; `fs_command` still checks the
|
|
290
|
+
bare command name and there is no per-call path, arbitrary `env`, or PATH field
|
|
291
|
+
(only the validated `envVars` overlay). Prepend
|
|
292
|
+
only narrow, trusted directories: a broad ancestor such as `$HOME` grants broad
|
|
293
|
+
read+exec, and prepending shadows same-named binaries. A project rooted at `/`
|
|
294
|
+
has no eligible entries: every absolute directory is inside the workspace, so
|
|
295
|
+
setup fails closed. A symlink inside a configured directory is resolved before
|
|
296
|
+
execution under the same rules.
|
|
297
|
+
|
|
298
|
+
Extra child environment entries can be configured once with `commandEnvVars`, a
|
|
299
|
+
closed object of names to string values. They reach every `fs.command` child,
|
|
300
|
+
including pipe stages and background jobs, and override same-named proxy or
|
|
301
|
+
preset values. The plugin does not explicitly forward arbitrary caller
|
|
302
|
+
`process.env` entries; procage's baseline allowlist still supplies its normal
|
|
303
|
+
variables. Empty strings set a variable to the empty string; they do not unset
|
|
304
|
+
it. `PATH` and `TMPDIR` are refused as plugin-managed, names must match
|
|
305
|
+
`[A-Za-z_][A-Za-z0-9_]*`, values must be NUL-free strings, and invalid
|
|
306
|
+
configuration fails setup. Values are literal: the plugin does no interpolation
|
|
307
|
+
or implicit forwarding of unrelated host variables. OpenCode's own
|
|
308
|
+
`{env:VARIABLE_NAME}` config substitution may apply before plugin setup, and an
|
|
309
|
+
unset variable becomes the empty string.
|
|
310
|
+
|
|
311
|
+
A single call can overlay selected values with `envVars`. It is a shallow
|
|
312
|
+
per-invocation overlay on `commandEnvVars`: a same-named value wins, unspecified
|
|
313
|
+
configured entries remain, and an empty string sets that variable to empty. It
|
|
314
|
+
works for direct calls, pipe command stages, and background jobs, uses the same
|
|
315
|
+
validation, and adds no lookup, `PATH`, filesystem, or network permission:
|
|
316
|
+
|
|
317
|
+
```js
|
|
318
|
+
await tools.fs.command({ command: "npm", args: ["test"], envVars: { NODE_ENV: "test" } })
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
The configured and per-call values are trusted input and can change what the
|
|
322
|
+
allowlisted executable and its descendants do. The plugin does
|
|
323
|
+
not automatically add them to its own errors or result metadata,
|
|
324
|
+
but the executable and its descendants can read them and may print or write
|
|
325
|
+
them; stdout and stderr are returned normally. Treat them as visible to that
|
|
326
|
+
executable and its descendants.
|
|
327
|
+
|
|
168
328
|
The project is read/write/exec. **Network egress needs Linux tier T1** and uses
|
|
169
329
|
procage's transparent relay. There are no per-call grants. Every TCP/UDP flow is
|
|
170
330
|
named as a DNS QNAME, TLS SNI, HTTP `Host`, QUIC SNI, or IP literal, and that
|
|
171
|
-
exact resource is approved through `
|
|
331
|
+
exact resource is approved through `fs_command_network`; ports, URLs, and payloads
|
|
172
332
|
are never part of the permission. Bare IPv6 literals and bracketed IPv6 `Host`
|
|
173
333
|
forms with an optional port are valid resources. The permission rules are the
|
|
174
334
|
network allowlist: a `*` deny plus name/address allows fails closed for anything
|
|
175
335
|
else.
|
|
176
336
|
|
|
337
|
+
> **Migration from `fs_exec_network`:** replace every old rule with
|
|
338
|
+
> `fs_command_network` before upgrading. Old rules are ignored; an unlisted
|
|
339
|
+
> `fs_command_network` resource can fall back to the base allow unless another
|
|
340
|
+
> matching rule applies, so an old `fs_exec_network` `deny *` no longer closes
|
|
341
|
+
> egress.
|
|
342
|
+
|
|
177
343
|
DNS is authorized by QNAME. Allowed answers feed a per-run address-to-name
|
|
178
344
|
cache: a nameless flow is allowed by a fresh allowed name bound to its address,
|
|
179
345
|
else by the address. A named flow needs the exact `(name, address)` from an
|
|
@@ -193,10 +359,12 @@ await tools.fs.command({ command: "npm", args: ["install"], timeoutMs: 300000 })
|
|
|
193
359
|
Proxy variables (`HTTP_PROXY`, `HTTPS_PROXY`, `ALL_PROXY`, `NO_PROXY`, lowercase
|
|
194
360
|
forms, npm proxy settings) pass through explicitly. The relay treats the proxy
|
|
195
361
|
as the destination, never its `CONNECT` target, so allow the proxy's address or
|
|
196
|
-
cached DNS name in `
|
|
197
|
-
it forwards.
|
|
198
|
-
|
|
199
|
-
|
|
362
|
+
cached DNS name in `fs_command_network`; approving it lets the proxy reach whatever
|
|
363
|
+
it forwards. Beyond the explicit proxy list and plugin-managed `TMPDIR` (and
|
|
364
|
+
`PATH` when `commandPaths` is set), the only caller-supplied entries passed
|
|
365
|
+
explicitly are the validated `commandEnvVars` and per-call `envVars`; arbitrary
|
|
366
|
+
caller `process.env` entries do not pass. The preset's own allowlist still
|
|
367
|
+
applies, and a proxy variable may itself carry proxy credentials.
|
|
200
368
|
|
|
201
369
|
Denied paths ask `external_directory` and approval covers the denied path's
|
|
202
370
|
containing directory (or nearest existing ancestor) only when procage reports a
|
|
@@ -301,12 +469,20 @@ await tools.fs.pipe({
|
|
|
301
469
|
| `output` | Write final stdout to a file (final `tee`) |
|
|
302
470
|
| `append` | Append to `output` instead of overwriting |
|
|
303
471
|
|
|
304
|
-
The
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
`
|
|
309
|
-
|
|
472
|
+
The registered stage set is exact:
|
|
473
|
+
|
|
474
|
+
| Mode | Stage names |
|
|
475
|
+
|---|---|
|
|
476
|
+
| source | `base64`, `cat`, `curl`, `dd`, `fold`, `nl`, `od`, `paste`, `rev`, `shuf`, `strings`, `tac` |
|
|
477
|
+
| filter | `command`, `cut`, `grep`, `gzip`, `gunzip`, `head`, `sed`, `sort`, `tail`, `tee`, `tr`, `uniq` |
|
|
478
|
+
| sink | `cpio`, `hash`, `tar`, `wc`, `zip` |
|
|
479
|
+
|
|
480
|
+
The first stage must be a source or receive `input`. A filter first without
|
|
481
|
+
`input` fails the whole call with `ERROR: no input files and no stdin.`; the error
|
|
482
|
+
is the call result, not pipeline data. A source stage later in the chain ignores
|
|
483
|
+
pipe stdin when it has `paths`. Archive writers (`tar`, `zip`, `cpio`) emit no
|
|
484
|
+
stdout, so put them last; `wc` and `hash` emit their result text and can also end
|
|
485
|
+
a chain or feed a later stage.
|
|
310
486
|
|
|
311
487
|
Stages exchange whole buffers. Set `maxBytes` on `curl`; for large
|
|
312
488
|
downloads prefer `fs.curl({ output })`, which streams to disk. In-memory
|
|
@@ -328,26 +504,28 @@ await tools.fs.pipe({
|
|
|
328
504
|
})
|
|
329
505
|
```
|
|
330
506
|
|
|
331
|
-
`output` and `append` assert `fs_tee` on the destination. The result
|
|
332
|
-
|
|
333
|
-
verification trail
|
|
507
|
+
`output` and `append` assert `fs_tee` on the destination. The result carries
|
|
508
|
+
per-stage metadata (`tool`, `bytesIn`, `bytesOut`, `exitCode`), which is the
|
|
509
|
+
verification trail where the surface exposes metadata; `execute` resolves to the
|
|
510
|
+
final text only. A stage that yields no stdout becomes an empty buffer, so
|
|
334
511
|
`output` writes an empty file. The signal is checked before each stage and
|
|
335
512
|
before the final write, so an abort stops the chain before creating output.
|
|
336
513
|
|
|
514
|
+
The returned content is the final buffer decoded as UTF-8. A binary stage such as
|
|
515
|
+
`gzip` cannot round-trip through that text; write it with `output`, which gets the
|
|
516
|
+
raw bytes before decoding. A stage's `args` use the same schema as the tool but
|
|
517
|
+
cannot include `background`.
|
|
518
|
+
|
|
337
519
|
Common pipe mistakes:
|
|
338
520
|
|
|
339
521
|
- `commands` is not a field; the field is `stages`.
|
|
340
|
-
- A bare `{ tool: "sort", args: {} }` first stage
|
|
341
|
-
`ERROR: no input files and no stdin
|
|
342
|
-
- Stage names are `cat`, not `fs.cat
|
|
522
|
+
- A bare `{ tool: "sort", args: {} }` first stage fails with
|
|
523
|
+
`ERROR: no input files and no stdin.`; add a source stage or `input`.
|
|
524
|
+
- Stage names are `cat`, not `fs.cat`; any name outside the table fails with
|
|
525
|
+
`ERROR: Unknown pipe stage: <name>` (`unsupported`).
|
|
343
526
|
- There is no shell, redirection operator, nested pipe, `find -exec`, or
|
|
344
527
|
`xargs`.
|
|
345
528
|
|
|
346
|
-
Pipeable filters include `base64`, `command`, `cut`, `fold`, `grep`, `gunzip`,
|
|
347
|
-
`gzip`, `head`, `nl`, `od`, `paste`, `rev`, `sed`, `shuf`, `sort`, `strings`,
|
|
348
|
-
`tac`, `tail`, `tee`, `tr`, and `uniq`. Unknown stage names fail with
|
|
349
|
-
`unsupported`.
|
|
350
|
-
|
|
351
529
|
## Background jobs
|
|
352
530
|
|
|
353
531
|
Every tool except `fs.job` accepts `background: true`. It returns immediately
|
|
@@ -371,9 +549,12 @@ await tools.fs.job({ id: job.jobId, cancel: true }) // abort
|
|
|
371
549
|
await tools.fs.job({ list: true }) // newest first
|
|
372
550
|
```
|
|
373
551
|
|
|
374
|
-
`wait` returns the original tool
|
|
375
|
-
returns the status with `timedOut: true`.
|
|
376
|
-
|
|
552
|
+
`wait` returns the original tool content once the job completes; on timeout it
|
|
553
|
+
returns the status JSON with `timedOut: true`. Status and `fs.job({ id })` content
|
|
554
|
+
is JSON with `id`, `tool`, `state` (`running`, `completed`, `failed`,
|
|
555
|
+
`cancelled`), `startedAt`, optional `finishedAt`/`error`, and `statusPath`;
|
|
556
|
+
`fs.job({ list: true })` returns `{ "jobs": [ ... ] }`, newest first. Jobs are
|
|
557
|
+
process-local and lost on server restart. Status files live under `/tmp/opencode/fs-jobs/<server-pid>/`
|
|
377
558
|
by default; set the plugin option `jobDir` to move them. The caps are global to
|
|
378
559
|
one server, not per session: eight jobs may run at once and 64 are retained,
|
|
379
560
|
evicting the oldest finished job and deleting its status file; finished jobs are
|
|
@@ -539,8 +720,9 @@ await tools.fs.curl({
|
|
|
539
720
|
Files under `/tmp` are usually on another filesystem; use a project path or a
|
|
540
721
|
same-filesystem holding directory instead.
|
|
541
722
|
- `fs.mktemp` uses the OS temp directory unless `tmpdir` is supplied.
|
|
542
|
-
- `fs.mkdir` fails when the directory already exists
|
|
543
|
-
|
|
723
|
+
- `fs.mkdir` fails when the directory already exists unless `parents: true`,
|
|
724
|
+
which makes an existing directory a no-op; check with `fs.stat` first when you
|
|
725
|
+
need a hard answer.
|
|
544
726
|
- `fs.gunzip` needs `force` to overwrite the restored file; without `keep` it
|
|
545
727
|
removes the `.gz` after decompressing.
|
|
546
728
|
|
|
@@ -583,27 +765,49 @@ hidden inside a pipeline. Do not work around a denied tool with shell.
|
|
|
583
765
|
|
|
584
766
|
`fs.command` asserts `fs_command` with the executable name as its resource;
|
|
585
767
|
deny `*` for a closed set. It asks `external_directory` for a blocked path and
|
|
586
|
-
`
|
|
768
|
+
`fs_command_network` with the exact DNS name or IP address named by the relay.
|
|
587
769
|
There is no `*` fallback and no direct-IP mode; networking that cannot use the
|
|
588
770
|
relay is disabled. There are no per-call grants and no shell.
|
|
589
771
|
|
|
590
|
-
`
|
|
591
|
-
URL as the resource, so one rule set covers both; each
|
|
592
|
-
against its URL. Nothing blocks localhost or private addresses by default.
|
|
593
|
-
|
|
594
|
-
##
|
|
595
|
-
|
|
596
|
-
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
772
|
+
`fs.curl` reuses the built-in `webfetch` permission (there is no `fs_curl`
|
|
773
|
+
action) with the requested URL as the resource, so one rule set covers both; each
|
|
774
|
+
redirect hop is checked against its URL. Nothing blocks localhost or private addresses by default.
|
|
775
|
+
|
|
776
|
+
## Returns, errors, and exit codes
|
|
777
|
+
|
|
778
|
+
- A normal call resolves to its **content text**: stdout decoded as UTF-8. The
|
|
779
|
+
host attaches metadata beside it (`ok`, `exitCode`, `bytes`, `encoding`, and
|
|
780
|
+
tool counters); in `execute` you see the content string, not that metadata.
|
|
781
|
+
JSON *object* text is returned by direct `fs.command`, a background start,
|
|
782
|
+
`fs.job` status/list/cancel or a timed-out `wait`, while
|
|
783
|
+
`fs.ps({ format: "json" })` returns a JSON *array*. A completed
|
|
784
|
+
`fs.job({ wait: true })` returns the original tool's content instead (its
|
|
785
|
+
plain text, or that tool's own JSON). Every other tool returns plain text.
|
|
786
|
+
- Empty stdout means empty content. Many mutating tools succeed silently, for
|
|
787
|
+
example `cp`, `mv`, `rm`, `touch`, `chmod`, `truncate`, `sed -i`, archive
|
|
788
|
+
writes, and `fs.job wait` for them. Verify the effect with `fs.stat`,
|
|
789
|
+
`fs.ls`, `fs.hash`, or `fs.diff`; do not read empty content as failure.
|
|
790
|
+
- Binary or NUL output is returned as **base64** when stdout contains a NUL byte
|
|
791
|
+
in its first 8 KiB, with `metadata.encoding: "base64"`. That covers compressed
|
|
792
|
+
data and NUL modes such as `fs.find({ print0: true })`,
|
|
793
|
+
`fs.sort({ zeroTerminated: true })`, and `fs.shuf({ zeroTerminated: true })`.
|
|
794
|
+
Prefer newline output; a pipe's final content is always decoded as UTF-8.
|
|
795
|
+
- `ERROR: ...` is a recoverable failure (`ERROR: Not found`, `ERROR: Invalid
|
|
796
|
+
<tool> input: ...`, `ERROR: Unknown pipe stage: ...`); `BLOCKED: ...` is a
|
|
797
|
+
sandbox hard stop. Both carry a stable `code` in metadata. Domain errors
|
|
798
|
+
resolve as text; permission denials and host validation can reject the call
|
|
799
|
+
instead.
|
|
800
|
+
- Exit code 1 is a normal result for `fs.grep` with no match, `fs.diff` with
|
|
801
|
+
differences, `fs.mountpoint` on a non-mount, and `fs.pgrep` with no match.
|
|
802
|
+
Do not retry those as failures.
|
|
803
|
+
- `fs.command` (direct call) content is JSON text for an object with `command`,
|
|
804
|
+
`args`, `exitCode`, `signal`, `timedOut`, `aborted`, `sandboxBlocked`, `violations`,
|
|
805
|
+
`network`, `networkMode`, `networkHosts`, `externalPaths`, `stdout`, and
|
|
806
|
+
`stderr`, plus `networkReason` only when networking was disabled and `hint`
|
|
807
|
+
when the sandbox blocked it. A non-zero exit is still a normal result. As a
|
|
808
|
+
pipe stage it emits raw stdout instead.
|
|
809
|
+
- `fs.mktemp` prints the created path; `fs.ps`/`fs.pgrep` output is text, and
|
|
810
|
+
`fs.ps({ format: "json" })` returns JSON text.
|
|
607
811
|
|
|
608
812
|
## Verification
|
|
609
813
|
|
package/src/core/types.ts
CHANGED
|
@@ -30,6 +30,18 @@ export interface RunContext {
|
|
|
30
30
|
readonly concurrency: number;
|
|
31
31
|
readonly signal: AbortSignal;
|
|
32
32
|
readonly tmpdir: string;
|
|
33
|
+
/**
|
|
34
|
+
* Canonical trusted executable directories configured with the plugin
|
|
35
|
+
* `commandPaths` option. They are prepended to PATH for `fs.command` only.
|
|
36
|
+
*/
|
|
37
|
+
readonly commandPaths: readonly string[];
|
|
38
|
+
/**
|
|
39
|
+
* Validated extra environment entries configured with the plugin
|
|
40
|
+
* `commandEnvVars` option. They are added to every `fs.command` child and
|
|
41
|
+
* override same-named proxy or preset values; `PATH` and `TMPDIR` stay
|
|
42
|
+
* plugin-managed.
|
|
43
|
+
*/
|
|
44
|
+
readonly commandEnvVars: Readonly<Record<string, string>>;
|
|
33
45
|
}
|
|
34
46
|
|
|
35
47
|
export interface ToolSpec<I> {
|
package/src/index.ts
CHANGED
|
@@ -20,7 +20,7 @@ import { catTool } from "./tools/cat.js";
|
|
|
20
20
|
import { chmodTool } from "./tools/chmod.js";
|
|
21
21
|
import { chownTool } from "./tools/chown.js";
|
|
22
22
|
import { commTool } from "./tools/comm.js";
|
|
23
|
-
import { commandTool } from "./tools/command.js";
|
|
23
|
+
import { commandTool, resolveCommandEnvVars, resolveCommandPaths } from "./tools/command.js";
|
|
24
24
|
import { cpTool } from "./tools/cp.js";
|
|
25
25
|
import { cpioTool } from "./tools/cpio.js";
|
|
26
26
|
import { curlTool } from "./tools/curl.js";
|
|
@@ -206,6 +206,12 @@ export default Plugin.define({
|
|
|
206
206
|
const concurrency = clampConcurrency(
|
|
207
207
|
typeof ctx.options?.concurrency === "number" ? ctx.options.concurrency : undefined,
|
|
208
208
|
);
|
|
209
|
+
const commandPaths = await resolveCommandPaths(
|
|
210
|
+
ctx.options?.commandPaths,
|
|
211
|
+
sandbox.root,
|
|
212
|
+
concurrency,
|
|
213
|
+
);
|
|
214
|
+
const commandEnvVars = resolveCommandEnvVars(ctx.options?.commandEnvVars);
|
|
209
215
|
const jobs = new JobManager(
|
|
210
216
|
typeof ctx.options?.jobDir === "string" ? { dir: ctx.options.jobDir } : {},
|
|
211
217
|
);
|
|
@@ -253,6 +259,8 @@ export default Plugin.define({
|
|
|
253
259
|
concurrency,
|
|
254
260
|
signal,
|
|
255
261
|
tmpdir: os.tmpdir(),
|
|
262
|
+
commandPaths,
|
|
263
|
+
commandEnvVars,
|
|
256
264
|
},
|
|
257
265
|
parsed,
|
|
258
266
|
{},
|
package/src/tools/command.ts
CHANGED
|
@@ -9,7 +9,8 @@ import { randomBytes } from "node:crypto";
|
|
|
9
9
|
import * as path from "node:path";
|
|
10
10
|
import { Type, type Static } from "@sinclair/typebox";
|
|
11
11
|
import type { CageOptions, Relaxation, Violation } from "procage";
|
|
12
|
-
import { FsError, errorMessage } from "../core/errors.js";
|
|
12
|
+
import { FsError, errorMessage, fromErrno, isMissing } from "../core/errors.js";
|
|
13
|
+
import { mapLimit } from "../core/parallel.js";
|
|
13
14
|
import { RelayControlServer } from "../core/relay-control.js";
|
|
14
15
|
import { collect } from "../core/stream.js";
|
|
15
16
|
import { textResult, type RunContext, type ToolSpec } from "../core/types.js";
|
|
@@ -17,9 +18,10 @@ import { textResult, type RunContext, type ToolSpec } from "../core/types.js";
|
|
|
17
18
|
/**
|
|
18
19
|
* Proxy settings the child may inherit in addition to the preset's own
|
|
19
20
|
* environment allowlist. They are passed explicitly because the policy's
|
|
20
|
-
* allowlist does not name them.
|
|
21
|
-
*
|
|
22
|
-
* variable
|
|
21
|
+
* allowlist does not name them. The validated `commandEnvVars` and per-call
|
|
22
|
+
* `envVars` entries are also passed explicitly; no other caller `process.env`
|
|
23
|
+
* variable is, but the preset's allowlist still applies, and a proxy variable
|
|
24
|
+
* may itself carry proxy credentials.
|
|
23
25
|
*/
|
|
24
26
|
export const EXEC_PROXY_ENV = [
|
|
25
27
|
"HTTP_PROXY",
|
|
@@ -46,6 +48,161 @@ export function isSafeExecName(value: string): boolean {
|
|
|
46
48
|
return EXEC_NAME.test(value);
|
|
47
49
|
}
|
|
48
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Containment predicate for canonical absolute paths. `path.relative` handles
|
|
53
|
+
* the filesystem root correctly: for root `/`, relative("/", "/usr") is "usr",
|
|
54
|
+
* while a naive `startsWith(root + path.sep)` becomes `startsWith("//")` and
|
|
55
|
+
* reports a false negative.
|
|
56
|
+
*/
|
|
57
|
+
function isWithinRoot(root: string, candidate: string): boolean {
|
|
58
|
+
if (candidate === root) return true;
|
|
59
|
+
const relative = path.relative(root, candidate);
|
|
60
|
+
return (
|
|
61
|
+
relative.length > 0 &&
|
|
62
|
+
relative !== ".." &&
|
|
63
|
+
!relative.startsWith(`..${path.sep}`) &&
|
|
64
|
+
!path.isAbsolute(relative)
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type CommandPathResult =
|
|
69
|
+
| { readonly ok: true; readonly real: string }
|
|
70
|
+
| { readonly ok: false; readonly error: FsError };
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Validate the plugin `commandPaths` option and return canonical directory
|
|
74
|
+
* roots in configured order. This is config-level only: there is no per-call
|
|
75
|
+
* executable path option.
|
|
76
|
+
*
|
|
77
|
+
* Each entry must be a nonempty absolute directory that exists. Entries are
|
|
78
|
+
* canonicalized (so a configuration symlink is trusted at its real target),
|
|
79
|
+
* deduplicated after canonicalization, and must not be the filesystem root or
|
|
80
|
+
* the project root, or a descendant of the project root. Those roots are
|
|
81
|
+
* rejected because the workspace baseline is writable, while `commandPaths`
|
|
82
|
+
* grants only read+exec.
|
|
83
|
+
*/
|
|
84
|
+
export async function resolveCommandPaths(
|
|
85
|
+
raw: unknown,
|
|
86
|
+
projectRoot: string,
|
|
87
|
+
concurrency: number,
|
|
88
|
+
): Promise<string[]> {
|
|
89
|
+
if (raw === undefined) return [];
|
|
90
|
+
if (!Array.isArray(raw)) {
|
|
91
|
+
throw new FsError("commandPaths must be an array of absolute directory paths.", "invalid");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const root = path.parse(projectRoot).root;
|
|
95
|
+
// Entries are independent, so resolve them in bounded parallel. Each mapper
|
|
96
|
+
// returns a result instead of throwing: errors are then thrown in configured
|
|
97
|
+
// order, so the first configured entry's error always wins regardless of
|
|
98
|
+
// which realpath/stat finished first.
|
|
99
|
+
const results = await mapLimit(
|
|
100
|
+
raw,
|
|
101
|
+
concurrency,
|
|
102
|
+
async (entry): Promise<CommandPathResult> => {
|
|
103
|
+
try {
|
|
104
|
+
if (typeof entry !== "string" || entry.length === 0) {
|
|
105
|
+
throw new FsError("commandPaths entries must be non-empty strings.", "invalid");
|
|
106
|
+
}
|
|
107
|
+
if (entry.includes("\0")) {
|
|
108
|
+
throw new FsError("commandPaths entries must not contain NUL bytes.", "invalid");
|
|
109
|
+
}
|
|
110
|
+
if (!path.isAbsolute(entry)) {
|
|
111
|
+
throw new FsError(
|
|
112
|
+
`commandPaths entry must be an absolute directory path: "${entry}".`,
|
|
113
|
+
"invalid",
|
|
114
|
+
);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
let real: string;
|
|
118
|
+
try {
|
|
119
|
+
real = await fs.realpath(entry);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
/* v8 ignore next */
|
|
122
|
+
if (!isMissing(error)) throw fromErrno(error, "access");
|
|
123
|
+
throw new FsError(`commandPaths entry not found: "${entry}".`, "not-found");
|
|
124
|
+
}
|
|
125
|
+
const stats = await fs.stat(real);
|
|
126
|
+
if (!stats.isDirectory()) {
|
|
127
|
+
throw new FsError(`commandPaths entry is not a directory: "${entry}".`, "not-directory");
|
|
128
|
+
}
|
|
129
|
+
if (real === root) {
|
|
130
|
+
throw new FsError(
|
|
131
|
+
`commandPaths must not be the filesystem root: "${entry}".`,
|
|
132
|
+
"invalid",
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
if (isWithinRoot(projectRoot, real)) {
|
|
136
|
+
throw new FsError(
|
|
137
|
+
`commandPaths must not be the project root or inside it: "${entry}".`,
|
|
138
|
+
"invalid",
|
|
139
|
+
);
|
|
140
|
+
}
|
|
141
|
+
return { ok: true, real };
|
|
142
|
+
} catch (error) {
|
|
143
|
+
/* v8 ignore next */
|
|
144
|
+
return { ok: false, error: error instanceof FsError ? error : fromErrno(error, "access") };
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
);
|
|
148
|
+
|
|
149
|
+
const resolved = new Set<string>();
|
|
150
|
+
for (const result of results) {
|
|
151
|
+
if (!result.ok) throw result.error;
|
|
152
|
+
resolved.add(result.real);
|
|
153
|
+
}
|
|
154
|
+
return [...resolved];
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** POSIX-style environment variable names only; no `=` or NUL can occur. */
|
|
158
|
+
const ENV_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
159
|
+
/** Names the plugin owns, so a configuration cannot break resolution or sandboxing. */
|
|
160
|
+
const MANAGED_ENV_NAMES: ReadonlySet<string> = new Set(["PATH", "TMPDIR"]);
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Validate an environment entry map and return its entries. The same rules
|
|
164
|
+
* apply to the plugin `commandEnvVars` option and the per-invocation `envVars`
|
|
165
|
+
* argument; `label` names the field in diagnostics.
|
|
166
|
+
*
|
|
167
|
+
* It is a closed object of environment-name keys to string values. Values are
|
|
168
|
+
* never echoed in an error, because they may carry credentials. NUL bytes are
|
|
169
|
+
* rejected because they cannot reach a child environment intact. `PATH` and
|
|
170
|
+
* `TMPDIR` are rejected because `commandPaths` owns lookup/PATH and the tool
|
|
171
|
+
* owns each child's private temp directory. Empty values are accepted and set
|
|
172
|
+
* the variable to the empty string; they do not unset or remove it.
|
|
173
|
+
*/
|
|
174
|
+
export function resolveCommandEnvVars(
|
|
175
|
+
raw: unknown,
|
|
176
|
+
label = "commandEnvVars",
|
|
177
|
+
): Record<string, string> {
|
|
178
|
+
if (raw === undefined) return {};
|
|
179
|
+
if (typeof raw !== "object" || raw === null || Array.isArray(raw)) {
|
|
180
|
+
throw new FsError(
|
|
181
|
+
`${label} must be an object mapping environment variable names to string values.`,
|
|
182
|
+
"invalid",
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const entries: Array<[string, string]> = [];
|
|
187
|
+
for (const [name, value] of Object.entries(raw)) {
|
|
188
|
+
if (!ENV_NAME.test(name)) {
|
|
189
|
+
// Do not echo the supplied key: it can be malformed or carry sensitive text.
|
|
190
|
+
throw new FsError(`${label} contains an invalid environment variable name.`, "invalid");
|
|
191
|
+
}
|
|
192
|
+
if (MANAGED_ENV_NAMES.has(name)) {
|
|
193
|
+
throw new FsError(`${label} cannot set ${name}; the plugin manages it.`, "invalid");
|
|
194
|
+
}
|
|
195
|
+
if (typeof value !== "string") {
|
|
196
|
+
throw new FsError(`${label}.${name} must be a string.`, "invalid");
|
|
197
|
+
}
|
|
198
|
+
if (value.includes("\0")) {
|
|
199
|
+
throw new FsError(`${label}.${name} must not contain NUL bytes.`, "invalid");
|
|
200
|
+
}
|
|
201
|
+
entries.push([name, value]);
|
|
202
|
+
}
|
|
203
|
+
return Object.fromEntries(entries);
|
|
204
|
+
}
|
|
205
|
+
|
|
49
206
|
const Schema = Type.Object(
|
|
50
207
|
{
|
|
51
208
|
command: Type.String({ minLength: 1, description: "Allowlisted executable name, without a path or shell syntax." }),
|
|
@@ -55,6 +212,16 @@ const Schema = Type.Object(
|
|
|
55
212
|
timeoutMs: Type.Optional(
|
|
56
213
|
Type.Integer({ minimum: 100, maximum: 600_000, description: "Timeout in milliseconds. Default: 120000." }),
|
|
57
214
|
),
|
|
215
|
+
envVars: Type.Optional(
|
|
216
|
+
// The key pattern admits every string and values stay opaque, so TypeBox
|
|
217
|
+
// cannot surface a malformed-key path or a bad-value detail; the shared
|
|
218
|
+
// validator enforces the name/value rules. It never echoes a malformed
|
|
219
|
+
// key or a value, though a valid variable name can appear in its message.
|
|
220
|
+
Type.Record(Type.String({ pattern: "^[\\s\\S]*$" }), Type.Unknown(), {
|
|
221
|
+
description:
|
|
222
|
+
"Object mapping environment variable names to string values. Shallow per-call overlay on the plugin `commandEnvVars` values: same name overrides, unspecified values remain. Same rules as the plugin option; `PATH` and `TMPDIR` are refused.",
|
|
223
|
+
}),
|
|
224
|
+
),
|
|
58
225
|
},
|
|
59
226
|
{ additionalProperties: false },
|
|
60
227
|
);
|
|
@@ -101,7 +268,7 @@ class RelayUnsupportedError extends Error {
|
|
|
101
268
|
/** Why a blocked run failed, and what to do about it. */
|
|
102
269
|
export function blockedHint(mode: NetworkMode): string {
|
|
103
270
|
return mode === "relay"
|
|
104
|
-
? "The sandbox denied a path or refused a network flow. Approve the denied path, or the refused resource in
|
|
271
|
+
? "The sandbox denied a path or refused a network flow. Approve the denied path, or the refused resource in fs_command_network, and retry."
|
|
105
272
|
: "Networking is disabled in this sandbox. Approve the denied external path in the permission prompt and retry; relayed networking needs Linux tier T1.";
|
|
106
273
|
}
|
|
107
274
|
|
|
@@ -128,8 +295,10 @@ export const commandTool: ToolSpec<Input> = {
|
|
|
128
295
|
name: "command",
|
|
129
296
|
description:
|
|
130
297
|
"Run an executable allowed by fs_command in a procage sandbox without a shell. Direct calls return JSON; " +
|
|
131
|
-
"pipe stages pass raw stdout.
|
|
132
|
-
"
|
|
298
|
+
"pipe stages pass raw stdout. An optional per-call `envVars` overwrites selected plugin `commandEnvVars` values " +
|
|
299
|
+
"for this invocation only. Trusted executable directories configured by the plugin `commandPaths` option " +
|
|
300
|
+
"are searched before PATH; they are not per-call arguments. On Linux tier T1 every network flow is named " +
|
|
301
|
+
"to the transparent relay, which asks fs_command_network for its exact resource; elsewhere networking is disabled.",
|
|
133
302
|
schema: Schema,
|
|
134
303
|
pipeable: "filter",
|
|
135
304
|
mutates: true,
|
|
@@ -142,6 +311,10 @@ export const commandTool: ToolSpec<Input> = {
|
|
|
142
311
|
if (args.some((arg) => arg.includes("\0"))) {
|
|
143
312
|
throw new FsError("arguments must not contain NUL bytes.", "invalid");
|
|
144
313
|
}
|
|
314
|
+
// Validated before any permission or filesystem work for this command: a
|
|
315
|
+
// malformed overlay fails closed, and this command is never spawned. A
|
|
316
|
+
// pipe stage can be reached after earlier stages already ran.
|
|
317
|
+
const envVars = resolveCommandEnvVars(input.envVars, "envVars");
|
|
145
318
|
await ctx.permission.assertTrusted([command]);
|
|
146
319
|
|
|
147
320
|
const requested = input.cwd ?? ".";
|
|
@@ -154,7 +327,7 @@ export const commandTool: ToolSpec<Input> = {
|
|
|
154
327
|
await ctx.permission.assertExternal([{ target: cwdTarget, boundary: cwdTarget.full }]);
|
|
155
328
|
if (ctx.signal.aborted) throw new FsError("command interrupted.", "interrupted");
|
|
156
329
|
|
|
157
|
-
const executable = await resolveExecutable(command);
|
|
330
|
+
const executable = await resolveExecutable(command, ctx.commandPaths);
|
|
158
331
|
const tempDir = await fs.mkdtemp(path.join(ctx.tmpdir, "fs-exec-"));
|
|
159
332
|
try {
|
|
160
333
|
const inputData = io.stdin === undefined
|
|
@@ -163,7 +336,7 @@ export const commandTool: ToolSpec<Input> = {
|
|
|
163
336
|
const approvedResources: string[] = [];
|
|
164
337
|
const approved: Array<{ full: string }> = [];
|
|
165
338
|
const assertNetwork = async (resource: string) => {
|
|
166
|
-
await ctx.permissionFor("
|
|
339
|
+
await ctx.permissionFor("fs_command_network").assertTrusted([resource]);
|
|
167
340
|
if (!approvedResources.includes(resource)) approvedResources.push(resource);
|
|
168
341
|
};
|
|
169
342
|
const base = {
|
|
@@ -175,6 +348,8 @@ export const commandTool: ToolSpec<Input> = {
|
|
|
175
348
|
allowed: approved,
|
|
176
349
|
tempDir,
|
|
177
350
|
input: inputData,
|
|
351
|
+
commandPaths: ctx.commandPaths,
|
|
352
|
+
envVars,
|
|
178
353
|
};
|
|
179
354
|
|
|
180
355
|
let attempt: RelayAttempt | undefined;
|
|
@@ -258,6 +433,9 @@ interface CagedInput {
|
|
|
258
433
|
allowed: Array<{ full: string }>;
|
|
259
434
|
tempDir: string;
|
|
260
435
|
input: Buffer | undefined;
|
|
436
|
+
commandPaths: readonly string[];
|
|
437
|
+
/** Validated per-call overlay, layered over the plugin defaults when the child env is built. */
|
|
438
|
+
envVars: Readonly<Record<string, string>>;
|
|
261
439
|
relay?: RelaySettings;
|
|
262
440
|
}
|
|
263
441
|
|
|
@@ -333,6 +511,13 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
333
511
|
addRule(doc, "exec", path.dirname(input.executable.real));
|
|
334
512
|
addRule(doc, "exec", path.resolve(path.dirname(input.executable.real), ".."));
|
|
335
513
|
await addOptionalRoots(doc, ["/home/linuxbrew", "/var/home/linuxbrew"], (root) => fs.access(root));
|
|
514
|
+
// Configured command roots are trusted for read and exec only. Never add a
|
|
515
|
+
// write rule here: the option exists to resolve binaries, not to widen the
|
|
516
|
+
// writable workspace.
|
|
517
|
+
for (const root of input.commandPaths) {
|
|
518
|
+
addRule(doc, "read", root);
|
|
519
|
+
addRule(doc, "exec", root);
|
|
520
|
+
}
|
|
336
521
|
addRule(doc, "write", input.tempDir);
|
|
337
522
|
addRule(doc, "write", input.cwd);
|
|
338
523
|
for (const allowed of input.allowed) addRule(doc, "write", allowed.full);
|
|
@@ -364,17 +549,35 @@ async function runCaged(ctx: RunContext, input: CagedInput): Promise<ExecOutcome
|
|
|
364
549
|
|
|
365
550
|
// The policy's allowlist supplies HOME, LANG, LC_ALL, PATH, TERM, TMPDIR and
|
|
366
551
|
// USER. Explicit values win over that allowlist, so pass only the tool's own
|
|
367
|
-
// TMPDIR
|
|
368
|
-
// preset's secret filtering.
|
|
369
|
-
//
|
|
370
|
-
// proxy
|
|
371
|
-
//
|
|
372
|
-
//
|
|
373
|
-
|
|
552
|
+
// TMPDIR, proxy settings, and the validated environment entries:
|
|
553
|
+
// forwarding process.env would defeat the preset's secret filtering.
|
|
554
|
+
// Configured entries and then the per-call overlay are appended last so they
|
|
555
|
+
// override a same-named proxy value or preset allowlist entry; the overlay
|
|
556
|
+
// wins over a same-named plugin default. PATH and TMPDIR cannot be set by
|
|
557
|
+
// either layer.
|
|
558
|
+
// The transparent relay treats a connection to the proxy as the proxy's own
|
|
559
|
+
// destination (never its CONNECT target), so the proxy's address, or its
|
|
560
|
+
// cached DNS name, is authorized through fs_command_network; the target URL
|
|
561
|
+
// behind the proxy is never treated as approved.
|
|
562
|
+
const envEntries: Array<[string, string]> = [["TMPDIR", input.tempDir]];
|
|
374
563
|
for (const name of EXEC_PROXY_ENV) {
|
|
375
564
|
const value = process.env[name];
|
|
376
|
-
if (value !== undefined)
|
|
565
|
+
if (value !== undefined) envEntries.push([name, value]);
|
|
566
|
+
}
|
|
567
|
+
// Configured command roots are prepended to the existing PATH. Add nothing
|
|
568
|
+
// when the option is empty, so the preset supplies its own PATH unchanged.
|
|
569
|
+
if (input.commandPaths.length > 0) {
|
|
570
|
+
envEntries.push([
|
|
571
|
+
"PATH",
|
|
572
|
+
[...input.commandPaths, process.env.PATH ?? ""]
|
|
573
|
+
.filter((entry) => entry.length > 0)
|
|
574
|
+
.join(path.delimiter),
|
|
575
|
+
]);
|
|
576
|
+
}
|
|
577
|
+
for (const [name, value] of Object.entries({ ...ctx.commandEnvVars, ...input.envVars })) {
|
|
578
|
+
envEntries.push([name, value]);
|
|
377
579
|
}
|
|
580
|
+
const env: Record<string, string> = Object.fromEntries(envEntries);
|
|
378
581
|
|
|
379
582
|
// Attach the listener before the spawn: an abort during the spawn await must
|
|
380
583
|
// still reach the child once it exists. The flag also captures an abort that
|
|
@@ -521,12 +724,24 @@ export function discoverExternalPaths(ctx: RunContext, outcome: ExecOutcome): st
|
|
|
521
724
|
.filter((candidate, index, all) => all.indexOf(candidate) === index);
|
|
522
725
|
}
|
|
523
726
|
|
|
524
|
-
async function resolveExecutable(
|
|
525
|
-
|
|
727
|
+
export async function resolveExecutable(
|
|
728
|
+
command: string,
|
|
729
|
+
commandPaths: readonly string[] = [],
|
|
730
|
+
): Promise<{ path: string; real: string }> {
|
|
731
|
+
const directories = [
|
|
732
|
+
...commandPaths,
|
|
733
|
+
...(process.env.PATH ?? "").split(path.delimiter).filter(Boolean),
|
|
734
|
+
];
|
|
526
735
|
for (const directory of directories) {
|
|
527
736
|
const candidate = path.join(directory, command);
|
|
528
737
|
try {
|
|
529
738
|
await fs.access(candidate, fsConstants.X_OK);
|
|
739
|
+
// A directory named like the command can pass the X_OK search check but
|
|
740
|
+
// can never be spawned. Skip it so it does not shadow a real executable
|
|
741
|
+
// further down PATH. `stat` follows symlinks, so a symlink to a regular
|
|
742
|
+
// file still resolves and is accepted.
|
|
743
|
+
const stats = await fs.stat(candidate);
|
|
744
|
+
if (!stats.isFile()) continue;
|
|
530
745
|
return { path: candidate, real: await fs.realpath(candidate) };
|
|
531
746
|
} catch {
|
|
532
747
|
// keep searching PATH
|