Skip to content

fix(auth): raise ProtocolError on malformed Digest challenge or unsupported algorithm - #3896

Open
Songram-Biswas wants to merge 1 commit into
encode:masterfrom
Songram-Biswas:fix/digest-auth-malformed-challenge
Open

Songram-Biswas wants to merge 1 commit into
encode:masterfrom
Songram-Biswas:fix/digest-auth-malformed-challenge

Conversation

@Songram-Biswas

Copy link
Copy Markdown

Summary

Fixes unhandled ValueError and KeyError exceptions in DigestAuth (httpx/_auth.py) when a server responds with a malformed or unsupported WWW-Authenticate: Digest header, ensuring callers catching httpx.HTTPError receive httpx.ProtocolError.

Root Cause

  1. In _parse_challenge(), field.strip().split('=', 1) ran outside the try block, raising an unhandled ValueError on tokens without =.
  2. In _build_auth_header(), self._ALGORITHM_TO_HASH_FUNCTION[challenge.algorithm.upper()] raised an unhandled KeyError for unsupported algorithms (e.g. SHA-512-256).
  3. Challenge parameter names were stored case-sensitively, rejecting valid RFC 7616 headers with mixed-case parameter names (e.g. Realm, Nonce).

Fix

  • Move field parsing inside the try block, catch (KeyError, ValueError), and normalize parameter keys with .lower().
  • Catch KeyError on unsupported Digest algorithms and raise ProtocolError.
  • Add regression tests in tests/test_auth.py.

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