Practical technical guides

Anthropic Python SDK 2.17 Matches Loopback Hosts Exactly Instead of by Prefix

Released December 3, 2026: the token endpoint check now parses the host. Measured on 2.16.0 and 2.17.0, with the general lesson for URL allowlists.

By Sandeep Belgavi · Published 2026-12-05

A check that read the URL as text

The Anthropic Python SDK can exchange credentials with a token endpoint, and the requests it sends there carry secret material: an assertion JWT or a long-lived refresh token. The SDK therefore refuses a token endpoint that does not use HTTPS, with one exception for local testing, so that a base URL such as http://localhost:8080 works against a server on the same machine.

Version 2.17.0, released on December 3, 2026, changes how that exception is decided. Its release notes say a plain http:// base URL is now allowed for the token exchange only when its host is exactly localhost, 127.0.0.1 or ::1. A URL that only starts that way, such as http://localhost.example.com or http://localhost@example.com, used to be accepted and now raises CredentialsError. A loopback URL with a username, such as http://user@localhost:8080, used to be refused and is now accepted. The same release keeps a base URL's password out of token endpoint errors.

The bug class is older than any SDK: deciding something about a URL by looking at the start of its text instead of parsing it.

What the code did before and after

The function is _require_https in anthropic/lib/credentials/_constants.py. In 2.16.0, after accepting anything starting with https://, it lowercased the URL and accepted any string that started with http://localhost, http://127.0.0.1 or http://[::1]. In 2.17.0 it parses the URL with the bundled HTTP client's URL type and accepts plain HTTP only when the scheme is http and the parsed host is exactly one of localhost, 127.0.0.1 or ::1. The error message also omits the URL entirely when it contains an @, because the URL may hold a password.

I installed both versions in separate environments, limited to packages published by December 5, and called the function directly with eleven URLs. No request was sent anywhere.

Base URL2.16.02.17.0Host the request would reach
https://auth.example.com/tokenacceptedacceptedauth.example.com over TLS
http://localhost:8080acceptedacceptedlocalhost
http://127.0.0.1:9000acceptedaccepted127.0.0.1
http://[::1]:8080acceptedaccepted::1
http://LOCALHOST:8080acceptedacceptedlocalhost
http://localhost.example.comacceptedrejectedlocalhost.example.com
http://localhost@example.comacceptedrejectedexample.com
http://127.0.0.1.example.netacceptedrejected127.0.0.1.example.net
http://[::1].example.comacceptedrejectednot a valid host
http://user@localhost:8080rejectedacceptedlocalhost
http://localhostacceptedacceptedlocalhost

The two interesting rows are the ones the release notes name. localhost.example.com is an ordinary DNS name under someone else's domain. localhost@example.com is a URL whose userinfo is localhost and whose host is example.com. Both start with the text http://localhost, and in both cases a cleartext request would have gone to a remote host. The row with 127.0.0.1.example.net, not mentioned in the notes, shows that the same flaw covered the IPv4 literal. The last-but-one row shows the opposite error: a prefix check rejected a harmless loopback URL because the username came before the host.

Why prefix checks keep failing

A URL has structure defined by RFC 3986: scheme, then an authority made of optional userinfo, a host and an optional port, then path, query and fragment. Every security decision about a URL is a decision about one of those components, almost always the scheme and the host. A string prefix test conflates them. Three ways to defeat it are visible in the table: a host that extends the trusted name with more labels, userinfo that places the trusted name before an @, and a literal address used as the first label of a domain. Others exist, including percent-encoding, backslashes that some parsers treat as slashes, and trailing dots.

The repair has two parts, and the SDK change does both. Parse with the same parser that will make the request, so the check and the connection agree on which host is meant. Then compare the parsed host for exact equality with an allowlist, not with startswith or endswith. A suffix test has the mirror-image flaw: evilexample.com ends with example.com.

from urllib.parse import urlsplit

LOOPBACK_HOSTS = {"localhost", "127.0.0.1", "::1"}

def allow_cleartext(url: str) -> bool:
    parts = urlsplit(url)
    if parts.scheme == "https":
        return True
    host = parts.hostname or ""  # lowercased, userinfo and port removed
    return parts.scheme == "http" and host in LOOPBACK_HOSTS

This sketch uses the standard library parser for illustration. In real code, use the parser of the HTTP client that will send the request, as the SDK now does; two parsers that disagree on an edge case reintroduce the problem.

Who was affected

Exploiting the old check required control over the base URL passed to the credential flow, through configuration, an environment variable or a profile file. An attacker who controls that value can usually point the SDK at a server of their choice anyway. The practical risk is narrower and still real: a mistyped or copied configuration value that looked local, or a configuration surface where one party may set a URL that another party's credentials flow through. In those cases the old check turned what looked like a local test endpoint into a cleartext exchange of a refresh token with a remote host.

Loopback names and addresses are not equivalent

Even an exact match leaves questions that an allowlist should answer deliberately. The name localhost is resolved by the operating system, normally to a loopback address, but a resolver configuration or hosts file can change that; the literals 127.0.0.1 and ::1 do not depend on name resolution. The whole range 127.0.0.0/8 is loopback in IPv4, yet the SDK accepts only 127.0.0.1, which is a reasonable narrowing for a test exception. A check that accepts any address beginning with 127. must parse it as an address first, or 127.0.0.1.example.net returns. For a strict policy, resolve nothing and allow only the literals; for a convenient one, allow localhost and document that it trusts local name resolution. Either way, write the decision into a test, because the next refactoring is where it usually changes silently.

Upgrade and audit steps

  1. Upgrade to 2.17.0 or later where the SDK's credential exchange is used.
  2. Search configuration for token endpoint base URLs that use http://. Each should be a bare loopback host; anything else will now raise, which is the intended outcome.
  3. If a local test server used http://user@localhost, it now works; if a configuration relied on a non-loopback http:// name that merely began with localhost, move it to HTTPS.
  4. Search your own code for the same pattern: startswith("http://localhost"), startswith("https://trusted."), endswith(".example.com") on raw URLs, and regular expressions anchored only at the start.
  5. Make sure error messages and logs do not print full URLs that may carry credentials in userinfo, as the 2.17.0 change now avoids.

The same release changes what HTTP middleware sees: APIRequest.headers now includes the SDK's default headers and credentials such as the authorization header. Middleware that logs request headers should redact them, matching header names without regard to case. That change is separate from the loopback check but lands in the same upgrade.

Verification

  • A table-driven test like the one above for every URL allowlist in your code, including userinfo, extra labels, IPv6 literals, uppercase hosts and trailing dots.
  • A test that the check and the HTTP client parse the same host for each test URL.
  • A log scan for URLs containing @ in error messages.

The companion example contains the probe harness, the recorded outputs for 2.16.0 and 2.17.0, and a standalone version of the parsed-host check with tests.

Primary references and authorship

Sources read December 5, 2026. Original explanations, measurements and synthetic examples by Sandeep Belgavi. Companion code: Sandeep-BA/agent-decision-lab/examples/python/loopback_host_check. Views are my own and do not represent any employer, past or present.