Shell
Spawn an allowlisted program, feed its stdin, read its bounded output, close or kill it, list what is live. The runtime registers it under shell, and the config decides what a page may run:
// crust.config.ts
shell: {
allow: ["git", "/opt/homebrew/bin/ffmpeg"],
cwdRoot: "/Users/you/project",
},
allow names the programs a page may run: a bare name, or an absolute path. A relative path is refused, because it would resolve against a cwd the page supplies and so name different programs.
The allow list decides what may run, as an absolute real path: it is canonicalised when the plugin is built, and a spawn resolves to a real path and is compared against that set. An allow-listed program runs wherever it lives, so /opt/homebrew/bin/ffmpeg needs no cwdRoot containing it. A basename no longer satisfies the list — a /tmp/echo a page planted is not echo — and a symlink borrowing an allowed name is refused, with the refusal naming both forms: "./echo" is not in the allow list ("./echo" resolves to /private/…/payload).
cwdRoot decides where a cwd and a relative program path may point, because a relative path means something only against a cwd the page supplies. Absent, it is the process working directory; a built app runs from /, so name it. Both the root and the cwd are realpath'd, so a symlinked cwd answers cwd-not-allowed.
| program form | resolved by | then checked against |
|---|---|---|
| bare name | Bun.which | realpath, then the allow list |
| absolute path | realpath | the allow list — no containment check |
| relative path | the cwd | containment on the real path, then the allow list |
An absent shell section means an empty allowlist and every spawn is refused by name — the default is deny, never allow. A bare name the app's PATH cannot resolve is dropped at construction: the app still starts, the name is absent from the (allowed: …) part of a refusal, and spawning it answers spawn-failed: "<name>" is not on PATH.
Config validation warns when an entry is an interpreter — sh, bash, zsh, node, bun, deno, python*, perl*, ruby*, osascript, pwsh, Rscript and the rest. Allowing one lets a page run any program and read any file this app can reach, so the allow list stops bounding what runs. It warns rather than refuses, because an app may genuinely need one.
No shell is ever invoked: a page's argument reaches the program as argv, never as a command line, so there is no quoting to get wrong and no sh -c to inject into.
Commands
| command | arguments | answers |
|---|---|---|
spawn | { program, args?, cwd?, env?, stdin? } | { ok: true, id } |
output | { id } | { ok: true, stdout, stderr, exitCode, signal } |
write | { id, data } | { ok: true, bytes } |
close | { id } | { ok: true, closed, id } |
kill | { id } | { ok: true, killed, id } |
list | none | { ok: true, ids } |
A page-supplied env is refused by spawn — see Environment.
const { id } = await window.crust.invoke("shell", "spawn", {
program: "git",
args: ["status", "--short"],
});
const out = await window.crust.invoke("shell", "output", { id });
spawn answers as soon as the process exists, not when it ends. output reads what has been collected so far and is complete only after the process is gone; exitCode is null for a killed process, which reports signal instead. list holds live processes only, while output still answers for a finished one.
Stdin
A process is spawned with stdin ignored unless spawn is given stdin: "pipe". With a pipe, write { id, data } sends text down it and answers how many bytes it accepted; close { id } ends the pipe so a reader sees EOF. A process spawned without the pipe, one whose pipe close already ended, and one that has exited all have no writable stdin, so write answers no-stdin rather than throwing into the page.
Environment
A page cannot set env. The whole object is refused unless the host opted in by building the plugin with createShellPlugin({ env: true }), because a page asking for env is asking to configure a program and there is no benign subset to guess at:
shell.spawn: "env" is not available to a page; createShellPlugin({ env: true }) is the app's opt-in, and the loader and shell variables stay refused even then
Even with the opt-in, the names that turn a permitted program into an execution primitive stay refused one by one:
| name | why it is code |
|---|---|
NODE_OPTIONS | --require / --import a file of the caller's choosing |
BASH_ENV, ENV | sourced by bash and sh before the program runs |
GIT_EXEC_PATH | swaps where git looks for its own subcommands |
PYTHONSTARTUP, PERL5OPT, RUBYOPT | run code at start-up |
LD_PRELOAD, LD_LIBRARY_PATH | replace or redirect the loader's libraries |
DYLD_* | the macOS family that does the same |
A malformed env is invalid-args whatever the host configured — the shape is checked before the opt-in, so a bad payload never reports a policy answer.
Still usable
Nothing here makes spawn unusable. An allowed program with no env runs exactly as before, and output answers its result:
{ "ok": true, "stdout": "still works", "exitCode": 0 }
Limits
- 500 lines per stream. Older lines drop, so a chatty program cannot grow the buffer.
- 8192 characters per line. A longer line is kept in pieces rather than unbounded.
- 256 finished records. Past that the oldest record is dropped and its
outputbecomesunknown-process. - No pty. Stdin is a plain pipe, not a terminal; a program that needs a TTY is out of scope.
Failure codes
| code | when |
|---|---|
invalid-args | program empty or not a string, args not an array of strings, env not an object of strings, stdin other than "pipe"/"ignore", no { id }, or write without { id, data } |
program-not-allowed | the resolved real path is not in allow — bare names go through Bun.which, path forms through realpath; the message names both the argument and what it resolved to |
program-outside-root | a relative program path resolved outside cwdRoot; an absolute path is never containment-checked |
cwd-not-allowed | cwd resolves outside cwdRoot, through a symlink, or to a path that does not exist |
env-not-allowed | env supplied without the host's { env: true }, or a refused name inside it |
spawn-failed | a bare name the app's PATH cannot resolve ("<name>" is not on PATH), an absolute path that does not exist, or a program that was allowed and still would not start |
unknown-process | no live or kept record with that id |
no-stdin | write to a process spawned without stdin: "pipe", one whose pipe close already ended, or one that has exited |
Every one resolves with { ok: false, code, message } in the body and a non-200 status mapped from code through the same table CrustError uses (invalid-args → 400, and the other shell codes, being unmapped, → 400), so a caller working at the fetch level sees a refusal too. A spawn refusal happens before any process exists: program-not-allowed means nothing was started.