Skip to content

Raise ProtocolError on malformed Digest auth challenges - #3856

Open
Ganesh1729-ui wants to merge 1 commit into
encode:masterfrom
Ganesh1729-ui:fix-digest-auth-malformed-challenge
Open

Ganesh1729-ui wants to merge 1 commit into
encode:masterfrom
Ganesh1729-ui:fix-digest-auth-malformed-challenge

Conversation

@Ganesh1729-ui

Copy link
Copy Markdown

I noticed that when a server returns a malformed Digest WWW-Authenticate challenge without an equals sign or with an unsupported algorithm, DigestAuth raises an unhandled ValueError or KeyError instead of ProtocolError.

This happened because the header field splitting was outside the try block, and the algorithm wasn't validated against the supported hash functions before lookup in _build_auth_header. I moved the field parsing inside the try block to catch ValueError, and added an algorithm check in _parse_challenge to raise ProtocolError consistently. I also added test cases covering both malformed fields and unsupported algorithms for sync and async clients.

@YashrajMiyatra YashrajMiyatra left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean error handling hardening. Catching both KeyError (missing realm/nonce) and ValueError (malformed field.strip().split("=", 1) when a parameter has no value) ensures that malformed HTTP Digest WWW-Authenticate challenge headers cleanly bubble up as httpx.ProtocolError rather than crashing the client with unhandled lower-level exceptions. The explicit check against self._ALGORITHM_TO_HASH_FUNCTION also adheres to RFC 7616. LGTM!

@Ganesh1729-ui

Copy link
Copy Markdown
Author

Thank you @YashrajMiyatra for reviewing and verifying the RFC 7616 error handling! Glad to hear the approach looks good.

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.

3 participants