fix: prevent ANTHROPIC_API_KEY from interfering with Gemini provider auth

Two fixes for issue #133 where setting ANTHROPIC_API_KEY=dummy alongside
CLAUDE_CODE_USE_GEMINI=1 causes "Invalid API key" errors:

1. auth.ts: In the CI branch of getAnthropicApiKeyWithSource(), the
   ANTHROPIC_API_KEY value was returned without checking isUsing3PServices().
   A dummy key leaked into the Anthropic key resolution pipeline even when
   Gemini was the active provider. Now guards with isUsing3PServices().

2. errors.ts: The x-api-key error handler surfaced "Invalid API key" for
   any provider. Added getAPIProvider() === 'firstParty' guard so 3P users
   see the real underlying error instead of a misleading auth message.

Note: The cli.tsx Gemini validation fix (originally part of this PR) was
independently implemented in PR #121 and is already on main.
This commit is contained in:
Juan Camilo
2026-04-02 15:40:07 +02:00
parent 1514220ee7
commit d430ddd568
2 changed files with 4 additions and 2 deletions

View File

@@ -812,7 +812,8 @@ export function getAssistantMessageFromError(
if ( if (
error instanceof Error && error instanceof Error &&
error.message.toLowerCase().includes('x-api-key') error.message.toLowerCase().includes('x-api-key') &&
getAPIProvider() === 'firstParty'
) { ) {
// In CCR mode, auth is via JWTs - this is likely a transient network issue // In CCR mode, auth is via JWTs - this is likely a transient network issue
if (isCCRMode()) { if (isCCRMode()) {

View File

@@ -286,7 +286,7 @@ export function getAnthropicApiKeyWithSource(
) )
} }
if (apiKeyEnv) { if (apiKeyEnv && !isUsing3PServices()) {
return { return {
key: apiKeyEnv, key: apiKeyEnv,
source: 'ANTHROPIC_API_KEY', source: 'ANTHROPIC_API_KEY',
@@ -294,6 +294,7 @@ export function getAnthropicApiKeyWithSource(
} }
// OAuth token is present but this function returns API keys only // OAuth token is present but this function returns API keys only
// Also reached when 3P provider is active — ANTHROPIC_API_KEY is ignored
return { return {
key: null, key: null,
source: 'none', source: 'none',