fix(client): treat empty-string env credentials as unset#1641
Open
LiFeiYu-boost wants to merge 1 commit into
Open
fix(client): treat empty-string env credentials as unset#1641LiFeiYu-boost wants to merge 1 commit into
LiFeiYu-boost wants to merge 1 commit into
Conversation
When ANTHROPIC_API_KEY / ANTHROPIC_AUTH_TOKEN are present in the environment but set to an empty string (common in CI, containers, and shells that `export VAR=`), the client treated "" as a real credential because every guard checks `is None`. This produced a malformed `Authorization: Bearer ` header (trailing space) that h11 rejects at request-write time, so every request failed with APIConnectionError. Coerce empty-string env credentials to None at the read site so they fall through to the normal no-credential path (default_credentials auto-discovery, or a clear "Could not resolve authentication method" error). This also matches the credential chain in lib/credentials/_chain.py, which already treats empty env vars as absent via truthy checks. Fixes anthropics#1640
|
Thanks for the fast fix — this resolves the reported (env) case cleanly, and coercing at the read site is the robust choice: it covers every downstream guard (the missing-auth check and both One optional, non-blocking note for defense-in-depth: an explicitly-passed empty string still slips through, since it skips the env-read coercion ( >>> from anthropic import Anthropic
>>> dict(Anthropic(auth_token="").auth_headers)
{'Authorization': 'Bearer '} # same malformed headerMaking the |
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
Fixes #1640.
When
ANTHROPIC_API_KEY/ANTHROPIC_AUTH_TOKENare present in the environment but set to an empty string (common in CI, containers, and shells thatexport VAR=— including the Claude Code CLI shell), the client treated""as a real credential because every guard checksis None. It then built a malformedAuthorization: Bearerheader (trailing space), which h11 rejects at request-write time — so every request failed withAPIConnectionError.Change
Coerce empty-string env credentials to
Noneat the read site, in bothAnthropic.__init__andAsyncAnthropic.__init__:Empty env credentials now fall through to the normal no-credential path:
default_credentials()auto-discovery (profile / federation), or a clearCould not resolve authentication methoderror — instead of emitting a malformed header.This also makes
_client.pyconsistent with the existing credential chain:lib/credentials/_chain.pyalready treats empty env vars as absent via truthy checks (if os.environ.get(ENV_API_KEY):), and uses the sameor Noneidiom forANTHROPIC_WORKSPACE_ID.Explicitly-passed
api_key=""/auth_token=""constructor arguments are intentionally left unchanged — this PR only normalizes the implicit env-var read.Tests
Added
test_empty_env_credentials_treated_as_unsetto bothTestAnthropicandTestAsyncAnthropic, mirroring the existingtest_validate_headerspattern (withdefault_credentialsmocked out): assertsapi_key/auth_tokenresolve toNone,auth_headersis empty (previously{"X-Api-Key": "", "Authorization": "Bearer "}), and request building raises the standard auth-resolutionTypeError.tests/test_client.pypasses;ruff check,ruff format --check, andpyrightare clean on the touched files.