Files
ShellBook/apps/server/README.md
T
Florian Schmidt 6926526a56 feat(server): implement workspace and SSH run pipeline (#3)
Add strict host-key confirmation, FIFO execution, streamed events, audit records, captures and SFTP artifacts/uploads. Use ssh2 for SSH/SFTP, yaml for inventory parsing, and compile-time ssh2/ws declarations.
2026-11-16 00:07:42 +01:00

3.9 KiB

Server execution (M2, issue #3)

buildApp({ config: { workspaceDir, dataDir, sshKeyPath }, events }) accepts overrides for tests and embedding. Defaults come from SHELLBOOK_WORKSPACE (workspace), SHELLBOOK_DATA (data), and SHELLBOOK_SSH_KEY (data/id_ed25519). Paths resolve against the service working directory.

The workspace contains notebooks/**/*.md, hosts.yaml, and known_hosts. Inventory accepts either a YAML list or { hosts: [...] }; every entry requires name, address, and user. Port defaults to 22, tags to an empty list. Nested notebook and artifact route parameters must be URL-encoded as a single parameter. Saves require an existing notebook and its SHA256 content version; concurrent saves in this process are serialized and stale versions return 409. Filesystem symlinks, traversal, Windows drive paths and non-Markdown notebooks are rejected. The workspace is trusted local storage; external editors must not replace directories with symlinks concurrently with requests.

Pass a shared EventsHub from src/runs/events.ts as events to publish watcher events using hub.publish(event). Each connected /api/events WebSocket receives the shared events. There is no replay buffer. Subscribe before POSTing a run. Run execution is FIFO per SSH address/port (including different inventory aliases), with a separate local queue. Different endpoints can execute concurrently.

SSH uses the configured private key without a PTY. Host keys are pinned in OpenSSH's plain host key-type base64-key format (bracketed host/port for non-22 ports). Hashed hostnames, wildcard entries, certificates and marker directives are not supported. Existing plain keys are checked strictly. Unknown or changed keys abort before exec; acceptHostKey must equal the observed SHA256 fingerprint before a pin is written or replaced. Failed authentication never writes a pin.

The script passed to bash is the core-rendered script. Execution setup selects a fresh workdir, and prepends export NAME=<core shellQuote(value)> lines on stdin for secrets. This avoids sshd AcceptEnv and sudo environment filtering and also works with Windows WSL bash. Secret values are never placed in exec argv. Neither the transport prelude nor raw SSH errors are logged. Local runs use the same stdin transport. Streamed output and captures redact exact secret values across chunk boundaries; encoded or transformed secret output cannot be detected. Audit records store only the rendered script, with defensive secret redaction.

Local working files are in data/runs/<uuid>/work/. Declared successful artifacts are copied to a separate artifacts/ tree and listed in artifacts.json; only listed files can be downloaded or used as upload sources, including after a restart. Local upload destinations must be relative to the fresh workdir. Remote uploads may use absolute destinations (the notebook format intentionally permits /tmp/new_key.pub). Remote artifact realpaths must remain inside the fresh mktemp -d directory. Cleanup is attempted before closing SSH; connection loss or forced timeout can leave remote temporary files behind.

Execution timeouts close the channel and connection; Unix local runs kill the process group. Windows process-tree termination depends on the installed bash implementation. SFTP establishment and transfers have a 30-second limit. Capture is limited to 1 MiB. Authentication, retention/24-hour artifact expiration and sensitive one-download artifacts are separate integration work; this module does not add those policies. The service has root-equivalent access and must be deployed with the authentication plugin and trusted workspace permissions.

Dependencies: ssh2 supplies SSH/SFTP and strict host verification, yaml parses the inventory. @types/ssh2 and @types/ws are compile-time declarations only. Tests use an in-process SSH/SFTP server that never invokes a real shell. Local bash tests skip only when bash cannot successfully execute a probe.