fix: strip ANTHROPIC_API_KEY from validation subprocess environment - #144
Open
vnsamy-code wants to merge 2 commits into
Open
fix: strip ANTHROPIC_API_KEY from validation subprocess environment#144vnsamy-code wants to merge 2 commits into
vnsamy-code wants to merge 2 commits into
Conversation
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>
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. :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ClaudeAgentSdk.getCleanEnvironment()to also stripANTHROPIC_API_KEYalongside the existingCLAUDECODEremovalenvtoClaudeCli'sexecFileSynccall, preventing the key from leaking into the subprocessContext
When
ANTHROPIC_API_KEYis 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:CI/CD users on
VALIDATION_CLIENT=apiare unaffected — they useTDD_GUARD_ANTHROPIC_API_KEYexclusively.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.tspassesANTHROPIC_API_KEY=sk-dummy npm run test:unitnpm run lint:checkandnpm run format:checkpass🤖 Generated with Claude Code