Skip to content

fix: strip ANTHROPIC_API_KEY from validation subprocess environment - #144

Open
vnsamy-code wants to merge 2 commits into
nizos:mainfrom
vnsamy-code:fix/issue-139-strip-anthropic-api-key
Open

fix: strip ANTHROPIC_API_KEY from validation subprocess environment#144
vnsamy-code wants to merge 2 commits into
nizos:mainfrom
vnsamy-code:fix/issue-139-strip-anthropic-api-key

Conversation

@vnsamy-code

@vnsamy-code vnsamy-code commented Apr 10, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Extends ClaudeAgentSdk.getCleanEnvironment() to also strip ANTHROPIC_API_KEY alongside the existing CLAUDECODE removal
  • Passes a cleaned env to ClaudeCli's execFileSync call, preventing the key from leaking into the subprocess
  • Adds tests for both clients verifying the key is stripped while other env vars are preserved

Context

When ANTHROPIC_API_KEY is set in the shell, the Claude Agent SDK and CLI inherit it and use it for billing instead of the user's Claude subscription. If that key belongs to a disabled or expired organization, validation fails with a cryptic JSON parse error:

Error: Error during validation: Unexpected token 'C', "Credit bal"... is not valid JSON

CI/CD users on VALIDATION_CLIENT=api are unaffected — they use TDD_GUARD_ANTHROPIC_API_KEY exclusively.

Fixes #139

Considered this fix since the user is after specifically unsetting the API key for tdd-guard: #139 (comment)

Test plan

  • npx vitest run src/validation/models/ClaudeCli.test.ts src/validation/models/ClaudeAgentSdk.test.ts passes
  • Verify with dummy key: ANTHROPIC_API_KEY=sk-dummy npm run test:unit
  • npm run lint:check and npm run format:check pass

🤖 Generated with Claude Code

vnsamy-code and others added 2 commits April 10, 2026 10:29
When ANTHROPIC_API_KEY is set in the shell, the Claude Agent SDK and CLI
inherit it and use it for billing instead of the user's Claude subscription.
If that key belongs to a disabled or expired organization, validation fails
with a cryptic JSON parse error ("Credit bal..." is not valid JSON).

Extends the existing getCleanEnvironment() pattern in ClaudeAgentSdk and
applies the same isolation to ClaudeCli, consistent with how CLAUDECODE is
already stripped. CI/CD users on VALIDATION_CLIENT=api are unaffected as
they use TDD_GUARD_ANTHROPIC_API_KEY exclusively.

Closes nizos#139

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Inline delete calls after assertions are skipped if a test throws,
leaving process.env dirty for subsequent tests. Moving cleanup to
afterEach guarantees it runs regardless of test outcome.

Also fixes a pre-existing CLAUDECODE leak in ClaudeAgentSdk tests
where the variable was set but never cleaned up.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@nizos

nizos commented Apr 11, 2026

Copy link
Copy Markdown
Owner

Thanks for digging into this @vnsamy-code!

I want us to hold on merging this while we rethink the broader situation. We now have some overlap between clients and auth methods, and I'd rather clean that up instead of adding more workarounds or fixes. Let's discuss it together. :)

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.

Error: Error during validation: Unexpected token 'C', "Credit bal"... is not valid JSON

2 participants