fix(credentials): match parsed host in cleartext-HTTP guard#1738
Open
sean-kim05 wants to merge 1 commit into
Open
fix(credentials): match parsed host in cleartext-HTTP guard#1738sean-kim05 wants to merge 1 commit into
sean-kim05 wants to merge 1 commit into
Conversation
`_require_https` exempted loopback endpoints from the https requirement
with a string prefix check (`url.startswith("http://localhost")`). That
also accepts hosts like `http://localhost.evil.com` and
`http://localhost@evil.com` (whose real host is `evil.com`), so a
misconfigured `base_url` would POST the Workload Identity assertion JWT
or a refresh token to an attacker-controlled host in cleartext — the
exact CWE-319 leak the guard exists to prevent.
Parse the URL and match the exact host against the loopback allowlist
{localhost, 127.0.0.1, ::1} instead of prefix-matching the raw string.
https URLs and genuine loopback HTTP endpoints are unaffected.
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.
What
_require_https(src/anthropic/lib/credentials/_constants.py) gates the credential token-exchangebase_url:https://is always allowed, and cleartexthttp://is tolerated only for a loopback host sobase_url="http://localhost:8080"works against a localoauth_serverduring testing.The loopback exemption was a raw string prefix check:
A string prefix isn't a host. The following all pass the exemption today but are not loopback:
base_urlurlsplit(url).hostname)http://localhost.evil.com/tokenlocalhost.evil.comhttp://localhost@evil.com/tokenevil.comhttp://127.0.0.1.evil.com/token127.0.0.1.evil.comhttp://127.0.0.1@evil.com/tokenevil.comhttp://localhostx:8080localhostxSo a misconfigured
base_urlpointing at anylocalhost/127.0.0.1-prefixed hostname — or one using alocalhost@…userinfo segment whose real authority is elsewhere — sends the Workload Identity assertion JWT or a long-lived refresh token to that host over cleartext HTTP, the exact CWE-319 leak this guard exists to prevent. This tightens the defense-in-depth hardening added for #1578.Fix
Parse the URL and compare the exact host against the loopback allowlist instead of prefix-matching the raw string:
urlsplitdrops anyuserinfo@segment from.hostname, sohttp://localhost@evil.comresolves to hostevil.comand is rejected.https://URLs and genuine loopback HTTP endpoints (http://localhost[:port][/path],http://127.0.0.1,http://[::1]) are unaffected.Tests
tests/lib/test_credentials.py, in the existing HTTPS-enforcement section:test_localhost_lookalike_http_rejected— the look-alike hosts above now raiseAnthropicError(fail without this change).test_loopback_and_https_allowed— genuine loopback HTTP endpoints and anyhttps://URL still pass (regression guard, including case-insensitive host matching and[::1]).Full
tests/lib/test_credentials.py(193 tests) passes;./scripts/lint(ruff + pyright + mypy) is clean.Related: #1578.