Skip to content

Scope cached DigestAuth challenge to the origin that issued it - #3865

Open
Ramya-9353 wants to merge 1 commit into
encode:masterfrom
Ramya-9353:digest-auth-origin-scope
Open

Ramya-9353 wants to merge 1 commit into
encode:masterfrom
Ramya-9353:digest-auth-origin-scope

Conversation

@Ramya-9353

Copy link
Copy Markdown

Summary

DigestAuth stores the server's WWW-Authenticate challenge in self._last_challenge and then pre-emptively attaches an Authorization header to every later request made through the same auth instance, without checking which origin issued that challenge. Since httpx.Client(auth=httpx.DigestAuth(...)) is the documented usage, one 401 from a single host makes the client authenticate against every host it talks to afterwards.

Repro:

import hashlib, os, httpx

def handler(request):
    if request.url.host == "intranet.example" and "Authorization" not in request.headers:
        nonce = hashlib.sha256(os.urandom(8)).hexdigest()
        challenge = (
            f'Digest realm="intranet@example", nonce="{nonce}", '
            'qop="auth", algorithm="SHA-256"'
        )
        return httpx.Response(401, headers={"www-authenticate": challenge})
    print(request.url, "->", request.headers.get("Authorization"))
    return httpx.Response(200)

auth = httpx.DigestAuth(username="olivia", password="s3cret-password")
with httpx.Client(transport=httpx.MockTransport(handler), auth=auth) as client:
    client.get("https://intranet.example/")
    client.get("https://evil.example/")

Observed on master:

https://intranet.example/ -> Digest username="olivia", realm="intranet@example", nonce="c5dd1d8c...", uri="/", response="4e783dd5...", algorithm=SHA-256, qop=auth, nc=00000001, cnonce="4044495d8434a91e"
https://evil.example/     -> Digest username="olivia", realm="intranet@example", nonce="c5dd1d8c...", uri="/", response="f3158a84...", algorithm=SHA-256, qop=auth, nc=00000002, cnonce="bc105fba06dd4058"

evil.example receives the username and realm in cleartext plus a digest response computed over the password, which leaves the password open to an offline attack. Expected behaviour is that the second request carries no Authorization header and negotiates separately against whichever origin actually challenges it, which is what happens after this change.

NetRCAuth already keys its credentials off request.url.host, so scoping the cached challenge the same way keeps the two schemes consistent. Reuse against the same origin is untouched and test_digest_auth_reuses_challenge still passes.

Verified locally on Python 3.12: ruff format --diff, ruff check and mypy clean, tests/test_auth.py and tests/client/test_auth.py pass (89 passed, 1 skipped) with httpx/_auth.py at 100% coverage. The new test fails on master and passes with the fix. The modules that need the live uvicorn fixture could not bind a socket in my sandbox, and they behave the same way on an unpatched checkout.

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant