Skip to content

Keep research provider keys out of Claude Code and OpenCode sandboxes - #64

Merged
LikC1606 merged 1 commit into
mainfrom
fix/harness-credential-relay
Nov 29, 2026
Merged

LikC1606 merged 1 commit into
mainfrom
fix/harness-credential-relay

Conversation

@LikC1606

Copy link
Copy Markdown
Collaborator

Problem

Non-sealed Claude Code and OpenCode research sessions passed the host provider key into bwrap. A loopback repro with Claude Code 2.1.332 showed the agent's Bash tool reading ANTHROPIC_API_KEY, and /proc/$PPID/environ of the claude process contained it too. Sandbox network is inherited, so the key could leave the host.

Fix

The key now stays in a host loopback relay. The sandbox gets only the relay URL and a disposable capability, the same pattern sealed roles and the Codex responses relay use.

  • src/runtime/researchCredentialRelay.ts (new)
    • Claude Code: ANTHROPIC_API_KEY / ANTHROPIC_BASE_URL become the capability and relay URL. Routes: /v1/messages and /v1/messages/count_tokens, each with ?beta=true. ANTHROPIC_AUTH_TOKEN uses Bearer upstream.
    • OpenCode: the selected provider's baseURL / apiKey are rewritten to the relay; headers are resolved on the host. Supports @ai-sdk/anthropic, @ai-sdk/openai and @ai-sdk/openai-compatible.
    • Credential variables, and any other variable whose value contains the key, are removed from the sandbox environment.
    • Fails closed for Claude OAuth-only (math_claude_oauth_credential_relay_unsupported) and other OpenCode SDKs (math_opencode_provider_relay_unsupported).
  • src/runtime/formalizationRelay.ts: per-route upstream endpoints, forwarded anthropic-version / anthropic-beta, bounded redacted JSON provider errors (keeps 401/429/context-overflow classification), configurable request cap (32 MiB for research).
  • Usage accounting (native-turn-totals) and sealed paths are unchanged.

Tests

  • npx tsc --noEmit passes.
  • claudeCodeSession, openCodeSession, sealedHarness, researchHarness, formalizationRelay and the new researchCredentialRelay tests: 280 passed, 2 skipped.
  • New regressions: installed Claude Code probe (Bash sees the capability, 0 key hits in env and parent environ, upstream receives the real key); OpenCode peer sees no key in its environment or config, and the relay closes with the session; relay unit tests for routes, headers, error redaction and fail-closed cases.
  • Full suite: 8 failures in 4 files. 6 (formalizationReviewCli, putnamBench) also fail on base 10fecbf; webSample needs an out/ directory missing in the worktree; codexSessionFileWatch failed once under full-suite load and passes alone.

Not covered

Codex's own /proc environ, the generic cli harness inheritEnvironment keys, a Claude login stored in a profile, and literal keys written into a profile's opencode.json.

Not deployed. Running runtimes were not touched.

🤖 Generated with Claude Code

Non-sealed Claude Code and OpenCode research sessions passed the host
provider key into bwrap, where the agent's shell could read it from its
environment and from /proc/$PPID/environ. Hold the key in a host loopback
relay instead and give the sandbox only the relay URL and a disposable
capability.

- researchCredentialRelay: credential-custody protocol over the native
  relay (permissive admission, native usage accounting unchanged), Claude
  messages/count_tokens routes, OpenCode provider rewrite for the
  anthropic, openai and openai-compatible SDKs.
- formalizationRelay: per-route upstream endpoints, forwarded
  anthropic-version/anthropic-beta headers, bounded redacted provider
  error bodies, configurable request cap (32 MiB for research).
- Fail closed for Claude OAuth-only and unsupported OpenCode providers.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LikC1606
LikC1606 merged commit 1742637 into main Nov 29, 2026
4 checks passed
@LikC1606
LikC1606 deleted the fix/harness-credential-relay branch December 1, 2026 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant