feat: add RFC 7616 support for non-Latin credentials in HTTPDigestAuth#7232
Open
feat: add RFC 7616 support for non-Latin credentials in HTTPDigestAuth#7232
Conversation
HTTPDigestAuth currently fails when usernames contain non-Latin-1 characters (e.g., Cyrillic, Czech diacritics). The username is directly interpolated into the Digest header, which either produces garbled output or raises a UnicodeEncodeError. This commit implements RFC 7616 (HTTP Digest Access Authentication) extensions to properly handle non-Latin credentials: 1. username* parameter (RFC 7616 Section 3.4 + RFC 5987): When the username contains characters outside Latin-1, the username* parameter with RFC 5987 encoding is used instead of the standard username parameter. Format: username*=UTF-8''percent-encoded-value 2. userhash support (RFC 7616 Section 3.4.4): When the server sends userhash=true in the WWW-Authenticate challenge, the client hashes the username with the realm using the selected hash algorithm, providing privacy protection. 3. charset support (RFC 7616 Section 3.3): When the server advertises charset=UTF-8, the client echoes it back in the Authorization header. The fix also adds: - _is_latin1_encodable() helper to detect non-Latin-1 strings - _encode_rfc5987() helper for RFC 5987 encoding - Comprehensive test suite in tests/test_digest_rfc7616.py Fixes psf#6102
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.
Summary
Implement RFC 7616 extensions to fix
HTTPDigestAuthfailing with non-Latin-1 usernames (e.g., Cyrillic, Czech diacritics).Problem
Solution
This PR implements three RFC 7616 features:
1.
username*parameter (RFC 7616 §3.4 + RFC 5987)When the username cannot be encoded as Latin-1, the
username*parameter with RFC 5987 encoding is used:2.
userhashsupport (RFC 7616 §3.4.4)When the server sends
userhash=truein the challenge, the username is hashed for privacy:3.
charsetecho (RFC 7616 §3.3)When the server advertises
charset=UTF-8, the client echoes it back.Changes
src/requests/auth.py_is_latin1_encodable(),_encode_rfc5987(), and updatedbuild_digest_header()with RFC 7616 username handlingtests/test_digest_rfc7616.pyusername*parameter,userhash,charset, and response digest validityBackward Compatibility
username="..."parameter is used as beforeA1 = username:realm:passwordis still hashed with UTF-8Fixes #6102