You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DigestAuth does not escape backslash or double quote in the Authorization header
#3858
DigestAuth writes the username and the echoed challenge values (realm, nonce, opaque) into the Authorization header as quoted strings. It does not escape \ or " in them.
RFC 7616 section 3.4 makes these values quoted-strings. In a quoted-string a backslash starts a quoted-pair (RFC 9110 section 5.6.4). So a Windows domain login like CORP\alice is read by an RFC parser as CORPalice. Apache mod_auth_digest reads it that way, and the login fails. A double quote in the username or the realm ends the string early and breaks the header.
The realm has the same problem the other way round. The challenge parser removes the escape from realm="my \"realm\"", and httpx then sends back realm="my "realm"".
The response hash is already computed from the unescaped values. So once the header is escaped, a strict server reads the same values back and the hash matches.
Other clients
curl, Firefox, Chrome and Internet Explorer 7+ escape these characters. curl has a test for it (test1229). Windows servers first try the escaped form and then retry with a raw backslash ([MS-DPSP] appendix A, note 4). That is why DOMAIN\user against IIS works today. A strict server like Apache fails.
Fix
Escaping \ and " in DigestAuth._get_header_value for every quoted field fixes both cases. Values without those characters stay byte-for-byte the same. I have a small PR with a test ready and will open it if this is welcome.
Environment
Windows 11, Python 3.14.7
httpx master 7cff5f7, no extra packages needed for the repro
Sync Client shown. AsyncClient uses the same _build_auth_header, and the problem does not depend on the HTTP version.
unquote() in httpx/_utils.py#L202-L212 strips surrounding quotes and unescapes RFC quoted-pairs via re.sub(r"\\(.)", r"\1", value[1:-1]). That is the right behavior for computing A1 = b":".join((self._username, challenge.realm, self._password)), because RFC 7616 §3.4.2 specifies that HA1 is computed over the unquoted parameter values.
Because _get_header_value interpolates to_str(value) directly into '{}="{}"' without escaping \ or " as quoted-pair (\\ and \" per RFC 9110 §5.6.4), any \ in username (such as DOMAIN\user) or any " / \ in realm / opaque produces an invalid or misparsed quoted-string.
Note on uri (digest-uri) and qop in RFC 7616 §3.4
When preparing your PR on _get_header_value:
Escaping \ first and then " (to_str(value).replace("\\", "\\\\").replace('"', '\\"')) on the quoted fields in _get_header_value cleanly handles username, realm, nonce, cnonce, and opaque (and uri, since "\\" and '"' are not valid raw URI path characters anyway).
Adding a unit test in tests/test_auth.py covering both a DOMAIN\user username and an escaped quote in realm (matching your MockTransport reproduction) will keep the diff minimal and self-contained.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
DigestAuthwrites the username and the echoed challenge values (realm, nonce, opaque) into theAuthorizationheader as quoted strings. It does not escape\or"in them.RFC 7616 section 3.4 makes these values quoted-strings. In a quoted-string a backslash starts a quoted-pair (RFC 9110 section 5.6.4). So a Windows domain login like
CORP\aliceis read by an RFC parser asCORPalice. Apachemod_auth_digestreads it that way, and the login fails. A double quote in the username or the realm ends the string early and breaks the header.The realm has the same problem the other way round. The challenge parser removes the escape from
realm="my \"realm\"", and httpx then sends backrealm="my "realm"".Repro
No network needed:
Output on master (7cff5f7):
Expected:
The response hash is already computed from the unescaped values. So once the header is escaped, a strict server reads the same values back and the hash matches.
Other clients
curl, Firefox, Chrome and Internet Explorer 7+ escape these characters. curl has a test for it (test1229). Windows servers first try the escaped form and then retry with a raw backslash ([MS-DPSP] appendix A, note 4). That is why
DOMAIN\useragainst IIS works today. A strict server like Apache fails.Fix
Escaping
\and"inDigestAuth._get_header_valuefor every quoted field fixes both cases. Values without those characters stay byte-for-byte the same. I have a small PR with a test ready and will open it if this is welcome.Environment
Clientshown.AsyncClientuses the same_build_auth_header, and the problem does not depend on the HTTP version.Validated with Claude Opus 5.5.
All reactions