requests icon indicating copy to clipboard operation
requests copied to clipboard

HTTPDigestAuth fails to select auth when qop contains a comma followed by a space

Open Zainalithegreat opened this issue 2 weeks ago • 4 comments

HTTPDigestAuth.build_digest_header returns None when a parsed server challenge advertises qop="auth-int, auth", even though the supported auth option is present. Removing the space produces a Digest header.

Expected Result

Both "auth-int,auth" and "auth-int, auth" should allow selection of auth. RFC 7616 section 3.3 describes qop alternatives, and section 3.9.1 includes a comma-separated list with a space.

Actual Result

On Requests 2.34.2:

'auth' -> Digest header
'auth-int,auth' -> Digest header
'auth-int, auth' -> None
'auth, auth-int' -> Digest header
'auth-int' -> None

The auth-int-only case is included as a control, not a request to implement auth-int. The current option check uses "auth" in qop.split(","), which does not strip whitespace from each item.

Reproduction Steps

This isolates the header-building helper. It sends no network requests and does not test a complete server authentication exchange.

import requests
from requests.auth import HTTPDigestAuth
from requests.utils import parse_dict_header

print("Requests:", requests.__version__)
for qop in ("auth", "auth-int,auth", "auth-int, auth",
            "auth, auth-int", "auth-int"):
    auth = HTTPDigestAuth("user", "pass")
    auth.init_per_thread_state()
    challenge = (
        f'realm="demo", nonce="nonce", qop="{qop}", '
        'algorithm="MD5"'
    )
    auth._thread_local.chal = parse_dict_header(challenge)
    header = auth.build_digest_header("GET", "https://example.test/private")
    print(repr(qop), "->", "None" if header is None else "Digest header")

Related reports checked: #1601 addressed multiple qop alternatives, and #2408 addressed using the selected qop in the digest calculation. This report concerns separator whitespace when selecting auth.

System Information

Output from python -m requests.help in the environment used for the reproduction:

{
  "chardet": {"version": null},
  "charset_normalizer": {"version": "3.4.9"},
  "cryptography": {"version": ""},
  "idna": {"version": "3.18"},
  "implementation": {"name": "CPython", "version": "3.12.10"},
  "platform": {"release": "11", "system": "Windows"},
  "pyOpenSSL": {"openssl_version": "", "version": null},
  "requests": {"version": "2.34.2"},
  "system_ssl": {"version": "30000100"},
  "urllib3": {"version": "2.7.0"},
  "using_charset_normalizer": true,
  "using_pyopenssl": false
}

The reproduction used an existing local installation; its installation method was not recorded.

Zainalithegreat avatar Nov 14 '26 05:11 Zainalithegreat

I have a tested fix for this on a branch: Sun-Wukhan/requests fix/digest-auth-qop-whitespace. build_digest_header now strips whitespace from each qop token before comparing, so a challenge with qop="auth-int, auth" selects auth instead of returning None. Regression tests are in tests/test_lowlevel.py, including a control case where qop="auth-int" alone still returns None. PR creation is restricted to collaborators on this repo, so I cannot open a PR directly; happy for a maintainer to pull the branch or advise another route.

Sun-Wukhan avatar Nov 14 '26 16:11 Sun-Wukhan

I have a fix ready for this (strip whitespace around each qop option before matching auth, plus a regression test) and will open a PR shortly.

zhengdaodeguL avatar Nov 15 '26 09:11 zhengdaodeguL

Update: the fix is ready on my fork (branch fix/digest-auth-qop-comma-space-7648, 1 commit: strip whitespace around each qop option before matching auth, plus a regression test and a HISTORY.md entry), but I cannot open the PR — GitHub says this repo restricts pull request creation to collaborators only (the API returns 404 and the web UI shows no Create pull request button). Compare link: https://github.com/psf/requests/compare/main...zhengdaodeguL:fix/digest-auth-qop-comma-space-7648 — happy for a maintainer to take it from there if useful.

zhengdaodeguL avatar Nov 15 '26 09:11 zhengdaodeguL