feat(server): add one-shot sensitive artifacts and retention (#4)

This commit is contained in:
Florian Schmidt
2026-11-16 00:19:26 +01:00
parent 1dbea28b43
commit 7ba8aabb37
7 changed files with 453 additions and 94 deletions
+41 -5
View File
@@ -1,4 +1,4 @@
# Server execution (M2, issue #3)
# Server execution and artifacts (M2/M3, issues #3/#4)
`buildApp({ config: { workspaceDir, dataDir, sshKeyPath }, events })` accepts
overrides for tests and embedding. Defaults come from `SHELLBOOK_WORKSPACE`
@@ -38,7 +38,8 @@ 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
are copied to a separate `artifacts/` tree and listed as `{name, sensitive}` entries
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
@@ -49,12 +50,47 @@ 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
is limited to 1 MiB. Failures emit short, specific `run.error` reasons (timeout,
authentication, connection, missing upload/artifact, or nonzero script exit),
with secret redaction and no raw transport errors or stack traces.
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.
## Sensitive artifacts and retention
Collection inspects only the first 512 bytes for PEM private-key headers, including
OpenSSH, RSA, EC, DSA, encrypted and generic private keys. Sensitivity is persisted
without file contents and reported in `run.finished.sensitiveArtifacts`. A local
sensitive file is removed from `work/` after collection so it has no spare copy.
Public files remain repeatable downloads and upload sources. Legacy name-only
metadata remains readable; sensitivity is detected from the file on access.
A sensitive download takes an exclusive filesystem claim before streaming. Parallel
requests cannot both succeed. Only after the HTTP response finishes does cleanup
delete the artifact; an interrupted response releases the claim for retry. Downloads
use `Cache-Control: no-store`. Once deleted, subsequent downloads return 410 and
upload attempts fail with a specific `run.error`. An undeclared name in an existing
run returns 404; a valid unknown run, expired run, or missing declared file returns
410. Invalid run IDs/paths return 400. A crash during a claimed download leaves a
fail-closed claim until retention removes the run; it does not allow a second copy.
`ServerConfig.artifactTtlHours` / `SHELLBOOK_ARTIFACT_TTL_HOURS` sets a positive,
finite TTL in hours (default **24**, fractional hours supported). Whole run directories
under `data/runs/` expire based on their directory modification time (normally last
collection/metadata write). Expiration is enforced at download/upload lookup; physical
cleanup runs on startup and hourly. Queued/executing runs are protected from the
sweep. The interval is unref'd, cleared on app close, and outstanding sweeps finish
before shutdown. The service owns this data directory; do not share it between
independent server processes with different run queues.
The HTTP tests in `test/m3.test.ts` use real core parsing/rendering and an in-process
SSH/SFTP server. A portable seeded-artifact flow covers sensitivity, downloads,
uploads, and capture/runtime rendering without bash. The full shipped `ssh-key.md`
flow generates real keys and skips cleanly when bash or ssh-keygen is unavailable;
it runs in CI/Linux, and also on Windows when a working bash with ssh-keygen is
installed. No real target host is modified by these tests.