Deliver CLAUDE_CODE_OAUTH_TOKEN to the sandbox as a file via a claude shim, not a docker run -e #198

Closed
opened 2026-10-15 02:59:03 +00:00 by ben · 3 comments
Owner

Filed from the 2026-10-14 infrastructure review (Claude Code session on Ben's workstation; four parallel read-only review passes over the root harness). Line anchors are against main at 3e50274.

Symptom

.env.example:16 / main.mts:220 document the residual: the OAuth token is forwarded as docker run -e and lands in docker inspect .Config.Env (the 2026-07-11 breach vector). Sandcastle takes .env value || process.env and passes it as -e (node_modules/@ai-hero/sandcastle/dist/index.js:629-637, dist/chunk-CP3TYXZA.js:72-74). The Dockerfile already shims git with git-fence; the same trick works for claude. This is also the prerequisite for per-host and per-project tokens (#199, #200): a file per host is trivially host-owned, an org Actions secret is not.

Proposed change

  • Dockerfile: install the real CLI, then put a shim first on PATH. NOTE the real binary lands in /home/agent/.local/bin which is already first on PATH (Dockerfile:33-34), so a /usr/local/bin shim like git-fence WOULD BE SHADOWED — rename the real binary (e.g. claude-real) or prepend a shim dir. Shim: exec env CLAUDE_CODE_OAUTH_TOKEN="$(cat /run/secrets/claude_oauth_token)" claude-real "$@" (fall back to the env var if the file is absent, so a bare pnpm run sandcastle still works).
  • provision_write_secrets (provision-lib.sh:94-99) writes claude_oauth_token from the SELECTED token — so claude_select_token must run before provisioning (today it runs later, execute-lib.sh:109), and _execute_failover must rewrite the file before its retry.
  • Remove CLAUDE_CODE_OAUTH_TOKEN from .env.example and ENV_ALLOWLIST_KEYS (env-allowlist-lib.sh:16); update .sandcastle/secrets/README.md, docs/SECRETS_CHECKLIST.md, the main.mts:220 comment, tests/env-allowlist-lib-test.sh, tests/provision-lib-test.sh.
  • Also: run-triage.sh:276 and heal.sh:386 run claude unsandboxed with the FULL job env (_B, MATTERMOST_BOT_TOKEN, FORGEJO_TOKEN). Invoke via env -u CLAUDE_CODE_OAUTH_TOKEN_B -u CLAUDE_TOKEN_PRIMARY -u MATTERMOST_BOT_TOKEN claude ... after claude_select_token.

Acceptance

  • docker inspect of a live worker shows no CLAUDE_CODE_OAUTH_TOKEN in .Config.Env (live acceptance, noted in the close report).
  • Offline: provision test asserts the file is written 0600 with the selected account's value and rewritten on failover.
  • The three stale references to docs/architecture/07-secrets-architecture.md (env-allowlist-lib.sh:5, main.mts:220, .env.example:5) are fixed to a path that exists.
_Filed from the 2026-10-14 infrastructure review (Claude Code session on Ben's workstation; four parallel read-only review passes over the root harness). Line anchors are against `main` at 3e50274._ ## Symptom `.env.example:16` / `main.mts:220` document the residual: the OAuth token is forwarded as `docker run -e` and lands in `docker inspect .Config.Env` (the 2026-07-11 breach vector). Sandcastle takes `.env value || process.env` and passes it as `-e` (`node_modules/@ai-hero/sandcastle/dist/index.js:629-637`, `dist/chunk-CP3TYXZA.js:72-74`). The Dockerfile already shims `git` with `git-fence`; the same trick works for `claude`. This is also the prerequisite for per-host and per-project tokens (#199, #200): a file per host is trivially host-owned, an org Actions secret is not. ## Proposed change - Dockerfile: install the real CLI, then put a shim first on PATH. NOTE the real binary lands in `/home/agent/.local/bin` which is already first on PATH (`Dockerfile:33-34`), so a `/usr/local/bin` shim like git-fence WOULD BE SHADOWED — rename the real binary (e.g. `claude-real`) or prepend a shim dir. Shim: `exec env CLAUDE_CODE_OAUTH_TOKEN="$(cat /run/secrets/claude_oauth_token)" claude-real "$@"` (fall back to the env var if the file is absent, so a bare `pnpm run sandcastle` still works). - `provision_write_secrets` (`provision-lib.sh:94-99`) writes `claude_oauth_token` from the SELECTED token — so `claude_select_token` must run before provisioning (today it runs later, `execute-lib.sh:109`), and `_execute_failover` must rewrite the file before its retry. - Remove `CLAUDE_CODE_OAUTH_TOKEN` from `.env.example` and `ENV_ALLOWLIST_KEYS` (`env-allowlist-lib.sh:16`); update `.sandcastle/secrets/README.md`, `docs/SECRETS_CHECKLIST.md`, the `main.mts:220` comment, `tests/env-allowlist-lib-test.sh`, `tests/provision-lib-test.sh`. - Also: `run-triage.sh:276` and `heal.sh:386` run claude unsandboxed with the FULL job env (`_B`, `MATTERMOST_BOT_TOKEN`, `FORGEJO_TOKEN`). Invoke via `env -u CLAUDE_CODE_OAUTH_TOKEN_B -u CLAUDE_TOKEN_PRIMARY -u MATTERMOST_BOT_TOKEN claude ...` after `claude_select_token`. ## Acceptance - `docker inspect` of a live worker shows no `CLAUDE_CODE_OAUTH_TOKEN` in `.Config.Env` (live acceptance, noted in the close report). - Offline: provision test asserts the file is written 0600 with the selected account's value and rewritten on failover. - The three stale references to `docs/architecture/07-secrets-architecture.md` (`env-allowlist-lib.sh:5`, `main.mts:220`, `.env.example:5`) are fixed to a path that exists.

This was generated by AI during triage.

Ruled by agent under ADR 0174 — veto anytime.

  • Ruling: ready-for-agent.
  • Why this is a two-way door: it moves an EXISTING token from -e delivery to a read-only file mount — the same mechanism already used for the forgejo/mattermost/digitalocean tokens — so it narrows exposure rather than widening posture; no new secret/bot/host is provisioned, and it is revertible.
  • What proves it: the offline provisioning test (secret file written 0600 with the selected account's value, rewritten on failover) plus the env-allowlist/example tests; the docker inspect check is a confirming live acceptance, not the only proof.

Agent Brief

Category: enhancement

Summary: Deliver the Claude OAuth token into the sandbox as a read-only mounted file consumed by a claude shim, instead of a docker run -e environment variable that lands in docker inspect .Config.Env.

Current behavior: The Claude OAuth token is forwarded as a container env var (the documented residual, the 2026-07-11 breach vector). The secret-file provisioning helper writes three other bearer tokens as 0600 files but not this one, and it runs before the account token is selected. The two unsandboxed heavy callers (triage, healer) invoke the CLI with the full job env, exposing the standby token and the forge/chat tokens to that process.

Desired behavior: Install the real CLI under a non-shadowing name (or prepend a shim dir) and put a shim first on PATH that reads the token from the mounted secret file and execs the real CLI, falling back to the env var when the file is absent (so a bare local run still works). Provisioning writes the selected account's token to the secret file at 0600, and the failover path rewrites the file before its retry — which requires token selection to happen before provisioning. Remove the Claude token from the env allowlist and .env.example, and invoke the unsandboxed triage/healer CLI calls with the standby/chat tokens unset from the child env after token selection. Fix the stale secrets-doc references to a path that exists.

Key interfaces:

  • Shim contract: resolve token from secret file, else env fallback, then exec the real CLI transparently with all args.
  • Provisioning writes the selected account token as a 0600 secret file and rewrites it on failover; token selection ordered before provisioning.
  • Env allowlist and example no longer carry the Claude token.
  • Unsandboxed CLI invocations run with the standby and chat/forge tokens removed from the child environment.

Acceptance criteria:

  • Offline: provisioning test asserts the secret file is written 0600 with the selected account's value, and rewritten on failover.
  • Env-allowlist and env-example tests reflect removal of the Claude token.
  • The three stale 07-secrets-architecture.md references point to a path that exists.
  • Dockerfile template renders a non-shadowed real CLI plus a shim first on PATH; local fallback preserved.
  • Live acceptance noted in the close report: docker inspect of a live worker shows no Claude token in .Config.Env.

Out of scope: Per-host and per-project token scoping (#199, #200) — this issue only changes the delivery mechanism of the existing token.

> *This was generated by AI during triage.* **Ruled by agent under ADR 0174 — veto anytime.** - **Ruling:** `ready-for-agent`. - **Why this is a two-way door:** it moves an EXISTING token from `-e` delivery to a read-only file mount — the same mechanism already used for the forgejo/mattermost/digitalocean tokens — so it *narrows* exposure rather than widening posture; no new secret/bot/host is provisioned, and it is revertible. - **What proves it:** the offline provisioning test (secret file written 0600 with the selected account's value, rewritten on failover) plus the env-allowlist/example tests; the `docker inspect` check is a confirming live acceptance, not the only proof. ## Agent Brief **Category:** enhancement **Summary:** Deliver the Claude OAuth token into the sandbox as a read-only mounted file consumed by a `claude` shim, instead of a `docker run -e` environment variable that lands in `docker inspect .Config.Env`. **Current behavior:** The Claude OAuth token is forwarded as a container env var (the documented residual, the 2026-07-11 breach vector). The secret-file provisioning helper writes three other bearer tokens as 0600 files but not this one, and it runs before the account token is selected. The two unsandboxed heavy callers (triage, healer) invoke the CLI with the full job env, exposing the standby token and the forge/chat tokens to that process. **Desired behavior:** Install the real CLI under a non-shadowing name (or prepend a shim dir) and put a shim first on PATH that reads the token from the mounted secret file and execs the real CLI, falling back to the env var when the file is absent (so a bare local run still works). Provisioning writes the selected account's token to the secret file at 0600, and the failover path rewrites the file before its retry — which requires token selection to happen before provisioning. Remove the Claude token from the env allowlist and `.env.example`, and invoke the unsandboxed triage/healer CLI calls with the standby/chat tokens unset from the child env after token selection. Fix the stale secrets-doc references to a path that exists. **Key interfaces:** - Shim contract: resolve token from secret file, else env fallback, then exec the real CLI transparently with all args. - Provisioning writes the selected account token as a 0600 secret file and rewrites it on failover; token selection ordered before provisioning. - Env allowlist and example no longer carry the Claude token. - Unsandboxed CLI invocations run with the standby and chat/forge tokens removed from the child environment. **Acceptance criteria:** - [ ] Offline: provisioning test asserts the secret file is written 0600 with the selected account's value, and rewritten on failover. - [ ] Env-allowlist and env-example tests reflect removal of the Claude token. - [ ] The three stale `07-secrets-architecture.md` references point to a path that exists. - [ ] Dockerfile template renders a non-shadowed real CLI plus a shim first on PATH; local fallback preserved. - [ ] Live acceptance noted in the close report: `docker inspect` of a live worker shows no Claude token in `.Config.Env`. **Out of scope:** Per-host and per-project token scoping (#199, #200) — this issue only changes the delivery mechanism of the existing token.

swarm-claim host=matou-dev-server-01 run=21816
(automated multi-host claim — lowest live claim id works this ticket)

swarm-claim host=matou-dev-server-01 run=21816 (automated multi-host claim — lowest live claim id works this ticket)

close-report for #198 — status success

✅ close-report gates passed — every claim verified against main; issue closed.

envelope
{
  "issue": 198,
  "status": "success",
  "commits": [
    "f4b163d5586e98b94e5a9bb9046314d3a48be47b"
  ],
  "changed_files": [
    ".env.example",
    "STATUS.md",
    "claude-fence",
    "docs/SECRETS_CHECKLIST.md",
    "env-allowlist-lib.sh",
    "execute-lib.sh",
    "heal.sh",
    "main.mts",
    "onboarding/templates/Dockerfile.tmpl",
    "onboarding/templates/SECRETS_CHECKLIST.md.tmpl",
    "onboarding/tests/onboard-lib-test.sh",
    "onboarding/vendor-manifest",
    "preflight-swarm.sh",
    "provision-lib.sh",
    "run-swarm.sh",
    "run-triage.sh",
    "tests/claude-fence-test.sh",
    "tests/env-allowlist-lib-test.sh",
    "tests/execute-lib-test.sh",
    "tests/provision-lib-test.sh",
    "tests/run-swarm-env-guard-test.sh",
    "tests/run-triage-test.sh"
  ],
  "tests": [
    {
      "command": "bash tests/claude-fence-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/provision-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/execute-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/env-allowlist-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/run-swarm-env-guard-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/run-triage-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash tests/preflight-swarm-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash onboarding/tests/onboard-lib-test.sh",
      "exit_code": 0
    },
    {
      "command": "bash onboarding/tests/vendor-manifest-coverage-test.sh",
      "exit_code": 0
    },
    {
      "command": "for t in tests/*.sh onboarding/tests/*.sh (full offline suite)",
      "exit_code": 0
    }
  ],
  "criteria": {
    "docker inspect of a live worker shows no CLAUDE_CODE_OAUTH_TOKEN in .Config.Env (live acceptance)": "Mechanism verified offline: resolveEnv (node_modules/@ai-hero/sandcastle dist) forwards -e ONLY for keys present in .sandcastle/.env; CLAUDE_CODE_OAUTH_TOKEN removed from .env.example (materializes .env in CI) so it is no longer forwarded. The token now ships via the mounted 0600 file + claude-fence shim (main.mts mounts, Dockerfile.tmpl shim). Live `docker inspect` acceptance runs after a human re-vendors the self-copy (pull-only, GOTCHAS 4); it cannot run from a sandboxed worker (no docker).",
    "Offline: provision test asserts the file is written 0600 with the selected account's value, and rewritten on failover": "tests/provision-lib-test.sh section 3b: provision_write_secrets writes secrets/claude_oauth_token == selected value at 0600; provision_write_claude_secret rewrite to standby stays 0600; unset writes no file. execute-lib-test.sh section 3c proves _execute_failover rewrites the file to the standby on a #510 flip.",
    "Env-allowlist and env-example tests reflect removal of the Claude token": "env-allowlist-lib.sh drops CLAUDE_CODE_OAUTH_TOKEN from ENV_ALLOWLIST_KEYS; tests/env-allowlist-lib-test.sh adds a case asserting it now trips the guard and moves clean fixtures to ANTHROPIC_API_KEY; .env.example drops the key; provision-lib-test.sh and run-swarm-env-guard-test.sh host-mode fixtures updated to ANTHROPIC_API_KEY.",
    "The three stale 07-secrets-architecture.md references point to a path that exists": "env-allowlist-lib.sh:5, main.mts, and .env.example all repointed from docs/architecture/07-secrets-architecture.md (absent) to docs/SECRETS_CHECKLIST.md (exists in this repo and in consumers at onboard time).",
    "Dockerfile template renders a non-shadowed real CLI plus a shim first on PATH; local fallback preserved": "onboarding/templates/Dockerfile.tmpl COPYs claude-fence to /usr/local/claude-shim/claude and prepends that dir to PATH ahead of ~/.local/bin; the shim execs the real CLI by absolute path and falls back to the ambient env var when /run/secrets/claude_oauth_token is absent. tests/claude-fence-test.sh covers file-source, file-wins, env fallback, empty-file fallback, no-token pass-through; onboard-lib-test renders the template placeholder-free."
  },
  "summary": "Claude OAuth token now ships into the worker sandbox as a 0600 mounted file read by a claude-fence shim first on PATH, removing it from the docker -e / docker inspect .Config.Env surface.",
  "blockers": []
}
**close-report** for #198 — status `success` :white_check_mark: **close-report gates passed** — every claim verified against main; issue closed. <details><summary>envelope</summary> ```json { "issue": 198, "status": "success", "commits": [ "f4b163d5586e98b94e5a9bb9046314d3a48be47b" ], "changed_files": [ ".env.example", "STATUS.md", "claude-fence", "docs/SECRETS_CHECKLIST.md", "env-allowlist-lib.sh", "execute-lib.sh", "heal.sh", "main.mts", "onboarding/templates/Dockerfile.tmpl", "onboarding/templates/SECRETS_CHECKLIST.md.tmpl", "onboarding/tests/onboard-lib-test.sh", "onboarding/vendor-manifest", "preflight-swarm.sh", "provision-lib.sh", "run-swarm.sh", "run-triage.sh", "tests/claude-fence-test.sh", "tests/env-allowlist-lib-test.sh", "tests/execute-lib-test.sh", "tests/provision-lib-test.sh", "tests/run-swarm-env-guard-test.sh", "tests/run-triage-test.sh" ], "tests": [ { "command": "bash tests/claude-fence-test.sh", "exit_code": 0 }, { "command": "bash tests/provision-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/execute-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/env-allowlist-lib-test.sh", "exit_code": 0 }, { "command": "bash tests/run-swarm-env-guard-test.sh", "exit_code": 0 }, { "command": "bash tests/run-triage-test.sh", "exit_code": 0 }, { "command": "bash tests/preflight-swarm-test.sh", "exit_code": 0 }, { "command": "bash onboarding/tests/onboard-lib-test.sh", "exit_code": 0 }, { "command": "bash onboarding/tests/vendor-manifest-coverage-test.sh", "exit_code": 0 }, { "command": "for t in tests/*.sh onboarding/tests/*.sh (full offline suite)", "exit_code": 0 } ], "criteria": { "docker inspect of a live worker shows no CLAUDE_CODE_OAUTH_TOKEN in .Config.Env (live acceptance)": "Mechanism verified offline: resolveEnv (node_modules/@ai-hero/sandcastle dist) forwards -e ONLY for keys present in .sandcastle/.env; CLAUDE_CODE_OAUTH_TOKEN removed from .env.example (materializes .env in CI) so it is no longer forwarded. The token now ships via the mounted 0600 file + claude-fence shim (main.mts mounts, Dockerfile.tmpl shim). Live `docker inspect` acceptance runs after a human re-vendors the self-copy (pull-only, GOTCHAS 4); it cannot run from a sandboxed worker (no docker).", "Offline: provision test asserts the file is written 0600 with the selected account's value, and rewritten on failover": "tests/provision-lib-test.sh section 3b: provision_write_secrets writes secrets/claude_oauth_token == selected value at 0600; provision_write_claude_secret rewrite to standby stays 0600; unset writes no file. execute-lib-test.sh section 3c proves _execute_failover rewrites the file to the standby on a #510 flip.", "Env-allowlist and env-example tests reflect removal of the Claude token": "env-allowlist-lib.sh drops CLAUDE_CODE_OAUTH_TOKEN from ENV_ALLOWLIST_KEYS; tests/env-allowlist-lib-test.sh adds a case asserting it now trips the guard and moves clean fixtures to ANTHROPIC_API_KEY; .env.example drops the key; provision-lib-test.sh and run-swarm-env-guard-test.sh host-mode fixtures updated to ANTHROPIC_API_KEY.", "The three stale 07-secrets-architecture.md references point to a path that exists": "env-allowlist-lib.sh:5, main.mts, and .env.example all repointed from docs/architecture/07-secrets-architecture.md (absent) to docs/SECRETS_CHECKLIST.md (exists in this repo and in consumers at onboard time).", "Dockerfile template renders a non-shadowed real CLI plus a shim first on PATH; local fallback preserved": "onboarding/templates/Dockerfile.tmpl COPYs claude-fence to /usr/local/claude-shim/claude and prepends that dir to PATH ahead of ~/.local/bin; the shim execs the real CLI by absolute path and falls back to the ambient env var when /run/secrets/claude_oauth_token is absent. tests/claude-fence-test.sh covers file-source, file-wins, env fallback, empty-file fallback, no-token pass-through; onboard-lib-test renders the template placeholder-free." }, "summary": "Claude OAuth token now ships into the worker sandbox as a 0600 mounted file read by a claude-fence shim first on PATH, removing it from the docker -e / docker inspect .Config.Env surface.", "blockers": [] } ``` </details>
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
Matou/dev-factory#198
No description provided.