feat(home): add repeatable security-audit Docker sandbox #11

Merged
hermes merged 5 commits from fm/dotfiles-audit-sandbox into main 2026-10-25 13:03:35 -04:00
Owner

Summary

Adds home/bin/security-audit-sandbox, installed as ~/.local/bin/security-audit-sandbox by home-manager following the existing oc2 entry, plus a "Security audit sandbox" section in readme.md.

  • start resolves the image from --image, then IMAGE= in the app's optional security-audit.conf, then marker-file inference (package.json gives node:22-bookworm, pyproject.toml/requirements.txt gives python:3.12-slim, go.mod gives golang:1.24-bookworm, Cargo.toml gives rust:1-bookworm, Gemfile gives ruby:3.3-bookworm).
  • start copies the app with rsync -a to ~/security-audit-runs/<slug>.XXXXXX, always excluding .git, .env*, and the top-level /data. It never mounts the original app path.
  • exec runs from /scratch/app under timeout --signal=KILL, default 600 seconds, with no host environment variables. It probes timeout --signal=KILL 2 sleep 10 before every command and refuses to run if the image lacks timeout or the probe does not stop at the limit.
  • Container controls: --network none, --read-only, writable run-dir mount at /scratch, --tmpfs /tmp:rw,nosuid,size=512m, HOME/TMPDIR/XDG_CACHE_HOME on scratch, host UID:GID, --memory 4g, --cpus 2, --pids-limit 256, --cap-drop ALL, --security-opt no-new-privileges, --init, nofile=1024:1024, and fsize=104857600 when Docker accepts it.
  • stop removes the container, state file, and run directory. --keep keeps the run directory. State is keyed by the canonical app path under ${XDG_STATE_HOME:-$HOME/.local/state}/security-audit-sandbox.

Verification

Dummy app with package.json, .env, .env.local, data/live.db, and src/keep.txt:

$ ./home/bin/security-audit-sandbox start "$T/app"
container: security-audit-app-857a0189
run dir:   /Users/laptran/security-audit-runs/app.2JMgEj
image:     node:22-bookworm            # inferred from package.json

$ find /Users/laptran/security-audit-runs/app.2JMgEj/app -type f
.../app/package.json
.../app/src/keep.txt                  # .env, .env.local, data/live.db absent

Loopback, network, and write checks:

$ security-audit-sandbox exec "$T/app" -- node loopback.js
LOOPBACK_OK ok

$ security-audit-sandbox exec "$T/app" -- node external.js
EXTERNAL_BLOCKED EAI_AGAIN

$ security-audit-sandbox exec "$T/app" -- sh -c 'echo scratch-write-ok > /scratch/probe && cat /scratch/probe'
scratch-write-ok

$ security-audit-sandbox exec "$T/app" -- sh -c 'echo root-write > /root/probe'
sh: 1: cannot create /root/probe: Permission denied       # exit 2

Timeout and state:

$ security-audit-sandbox exec "$T/app" --timeout 1 -- sleep 5
# exit 137 in 1.29s total

$ security-audit-sandbox start "$T/app"    # while running
security-audit-sandbox: a sandbox for ... is already running (container ...); run '... stop ...' first

$ security-audit-sandbox stop "$T/app"
removed container security-audit-app-857a0189 and run dir /Users/laptran/security-audit-runs/app.2JMgEj
# docker inspect and the run directory both report gone

Image selection:

$ security-audit-sandbox start "$T/app" --image node:22-bookworm
image:     node:22-bookworm

# security-audit.conf: IMAGE=node:22-alpine / EXCLUDE="src/keep.txt"
$ security-audit-sandbox start "$T/app"
image:     node:22-alpine
# copied src/keep.txt absent

$ security-audit-sandbox start "$T/app" --image node:22-bookworm   # overrides conf
image:     node:22-bookworm

Post-pipeline host scenarios for the wall-clock guard:

$ security-audit-sandbox exec "$T/app" --timeout 2 -- sleep 30
real 4.27s                             # 2s probe + 2s configured limit
exit 137

# image built with sh and sleep but /usr/bin/timeout removed
$ security-audit-sandbox exec "$T/app" -- true
OCI runtime exec failed: ... exec: "timeout": executable file not found in $PATH
security-audit-sandbox: image security-audit-no-timeout:test's 'timeout' does not enforce '--signal=KILL'; refusing to run without a wall-clock limit
exit 1

Static checks:

$ shellcheck home/bin/security-audit-sandbox        # clean
$ bash -n home/bin/security-audit-sandbox           # clean
$ nix-instantiate --parse home.nix                  # clean

no-mistakes run 01M5SXYQPW2QHCS8DF0VYMD613: passed. Review, test, document, lint, and push completed; pr and ci skipped because this forge is Gitea. The pipeline fixes are included in this branch:

  • stop now validates the run directory before removing container and state
  • multiple EXCLUDE= lines accumulate instead of silently overwriting
  • removed the undocumented SECURITY_AUDIT_RUN_PARENT override
  • stale-state recovery removes only the state file and a stopped container, never the previous run directory
  • exec probes that timeout really enforces the limit before running a command
## Summary Adds `home/bin/security-audit-sandbox`, installed as `~/.local/bin/security-audit-sandbox` by home-manager following the existing `oc2` entry, plus a "Security audit sandbox" section in `readme.md`. - `start` resolves the image from `--image`, then `IMAGE=` in the app's optional `security-audit.conf`, then marker-file inference (`package.json` gives `node:22-bookworm`, `pyproject.toml`/`requirements.txt` gives `python:3.12-slim`, `go.mod` gives `golang:1.24-bookworm`, `Cargo.toml` gives `rust:1-bookworm`, `Gemfile` gives `ruby:3.3-bookworm`). - `start` copies the app with `rsync -a` to `~/security-audit-runs/<slug>.XXXXXX`, always excluding `.git`, `.env*`, and the top-level `/data`. It never mounts the original app path. - `exec` runs from `/scratch/app` under `timeout --signal=KILL`, default 600 seconds, with no host environment variables. It probes `timeout --signal=KILL 2 sleep 10` before every command and refuses to run if the image lacks `timeout` or the probe does not stop at the limit. - Container controls: `--network none`, `--read-only`, writable run-dir mount at `/scratch`, `--tmpfs /tmp:rw,nosuid,size=512m`, `HOME`/`TMPDIR`/`XDG_CACHE_HOME` on scratch, host UID:GID, `--memory 4g`, `--cpus 2`, `--pids-limit 256`, `--cap-drop ALL`, `--security-opt no-new-privileges`, `--init`, `nofile=1024:1024`, and `fsize=104857600` when Docker accepts it. - `stop` removes the container, state file, and run directory. `--keep` keeps the run directory. State is keyed by the canonical app path under `${XDG_STATE_HOME:-$HOME/.local/state}/security-audit-sandbox`. ## Verification Dummy app with `package.json`, `.env`, `.env.local`, `data/live.db`, and `src/keep.txt`: ```bash $ ./home/bin/security-audit-sandbox start "$T/app" container: security-audit-app-857a0189 run dir: /Users/laptran/security-audit-runs/app.2JMgEj image: node:22-bookworm # inferred from package.json $ find /Users/laptran/security-audit-runs/app.2JMgEj/app -type f .../app/package.json .../app/src/keep.txt # .env, .env.local, data/live.db absent ``` Loopback, network, and write checks: ```bash $ security-audit-sandbox exec "$T/app" -- node loopback.js LOOPBACK_OK ok $ security-audit-sandbox exec "$T/app" -- node external.js EXTERNAL_BLOCKED EAI_AGAIN $ security-audit-sandbox exec "$T/app" -- sh -c 'echo scratch-write-ok > /scratch/probe && cat /scratch/probe' scratch-write-ok $ security-audit-sandbox exec "$T/app" -- sh -c 'echo root-write > /root/probe' sh: 1: cannot create /root/probe: Permission denied # exit 2 ``` Timeout and state: ```bash $ security-audit-sandbox exec "$T/app" --timeout 1 -- sleep 5 # exit 137 in 1.29s total $ security-audit-sandbox start "$T/app" # while running security-audit-sandbox: a sandbox for ... is already running (container ...); run '... stop ...' first $ security-audit-sandbox stop "$T/app" removed container security-audit-app-857a0189 and run dir /Users/laptran/security-audit-runs/app.2JMgEj # docker inspect and the run directory both report gone ``` Image selection: ```bash $ security-audit-sandbox start "$T/app" --image node:22-bookworm image: node:22-bookworm # security-audit.conf: IMAGE=node:22-alpine / EXCLUDE="src/keep.txt" $ security-audit-sandbox start "$T/app" image: node:22-alpine # copied src/keep.txt absent $ security-audit-sandbox start "$T/app" --image node:22-bookworm # overrides conf image: node:22-bookworm ``` Post-pipeline host scenarios for the wall-clock guard: ```bash $ security-audit-sandbox exec "$T/app" --timeout 2 -- sleep 30 real 4.27s # 2s probe + 2s configured limit exit 137 # image built with sh and sleep but /usr/bin/timeout removed $ security-audit-sandbox exec "$T/app" -- true OCI runtime exec failed: ... exec: "timeout": executable file not found in $PATH security-audit-sandbox: image security-audit-no-timeout:test's 'timeout' does not enforce '--signal=KILL'; refusing to run without a wall-clock limit exit 1 ``` Static checks: ```bash $ shellcheck home/bin/security-audit-sandbox # clean $ bash -n home/bin/security-audit-sandbox # clean $ nix-instantiate --parse home.nix # clean ``` `no-mistakes` run `01M5SXYQPW2QHCS8DF0VYMD613`: passed. Review, test, document, lint, and push completed; `pr` and `ci` skipped because this forge is Gitea. The pipeline fixes are included in this branch: - stop now validates the run directory before removing container and state - multiple `EXCLUDE=` lines accumulate instead of silently overwriting - removed the undocumented `SECURITY_AUDIT_RUN_PARENT` override - stale-state recovery removes only the state file and a stopped container, never the previous run directory - `exec` probes that `timeout` really enforces the limit before running a command
hermes added 5 commits 2026-10-25 12:50:37 -04:00
hermes merged commit 1cd03f93c6 into main 2026-10-25 13:03:35 -04:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: transnet/dotfiles#11