Skip to content

Fix digest auth dropping challenge fields with empty string values - #12097

Merged
Dreamsorcerer merged 3 commits into
aio-libs:masterfrom
bysiber:fix/digest-auth-empty-realm
Feb 20, 2026
Merged

Fix digest auth dropping challenge fields with empty string values#12097
Dreamsorcerer merged 3 commits into
aio-libs:masterfrom
bysiber:fix/digest-auth-empty-realm

Conversation

@bysiber

@bysiber bysiber commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

What do these changes do?

Fix DigestAuthMiddleware._authenticate silently dropping challenge fields that have empty string values.

The current code uses if value := header_pairs.get(field): which relies on truthiness check. Since empty strings are falsy in Python, any challenge field with an empty value (e.g., realm="") is excluded from self._challenge. This causes _encode() to later raise a ClientError complaining about a missing realm parameter, even though the server did provide one.

The _encode() method already acknowledges that empty realm values are valid per RFC 7616 (Section 3.3 — the realm directive SHOULD contain the host name but MAY be empty). The fix changes the check to is not None so that only genuinely absent fields are skipped while empty values are preserved.

Empty nonce values are still correctly rejected by the existing validation in _encode():

if not nonce:
    raise ClientError("Security issue: Digest auth challenge contains empty 'nonce' value")

Are there changes in behavior for the user?

Yes — digest authentication against servers that send realm="" (or other empty challenge fields) will now work instead of raising ClientError.

Is it a substantial burden for the maintainers to support this?

No, this is a single-line change.

Related issue number

N/A

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • Add a new news fragment into the CHANGES/ folder
    • naming format: <issue_or_PR_number>.<type>.rst
    • <type> is one of feature, bugfix, doc, removal, misc

@bysiber
bysiber requested a review from asvetlov as a code owner February 20, 2026 01:29
@codspeed-hq

codspeed-hq Bot commented Feb 20, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 59 untouched benchmarks


Comparing bysiber:fix/digest-auth-empty-realm (46938eb) with master (e458a83)

Open in CodSpeed

Comment thread aiohttp/client_middleware_digest_auth.py
@codecov

codecov Bot commented Feb 20, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.77%. Comparing base (5efc6c8) to head (46938eb).
⚠️ Report is 382 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #12097   +/-   ##
=======================================
  Coverage   98.77%   98.77%           
=======================================
  Files         128      128           
  Lines       44892    44892           
  Branches     2383     2383           
=======================================
  Hits        44342    44342           
  Misses        390      390           
  Partials      160      160           
Flag Coverage Δ
CI-GHA 98.63% <100.00%> (ø)
OS-Linux 98.37% <100.00%> (ø)
OS-Windows 96.71% <100.00%> (ø)
OS-macOS 97.60% <100.00%> (-0.01%) ⬇️
Py-3.10.11 97.16% <100.00%> (-0.01%) ⬇️
Py-3.10.19 97.64% <100.00%> (ø)
Py-3.11.14 97.84% <100.00%> (-0.01%) ⬇️
Py-3.11.9 97.37% <100.00%> (ø)
Py-3.12.10 97.46% <100.00%> (ø)
Py-3.12.12 97.80% <100.00%> (-0.15%) ⬇️
Py-3.13.11 97.93% <100.00%> (ø)
Py-3.13.12 97.43% <100.00%> (-0.09%) ⬇️
Py-3.14.2 97.90% <100.00%> (ø)
Py-3.14.3 97.39% <100.00%> (-0.76%) ⬇️
Py-3.14.3t 97.24% <100.00%> (ø)
Py-pypy3.11.13-7.3.20 97.39% <100.00%> (+<0.01%) ⬆️
VM-macos 97.60% <100.00%> (-0.01%) ⬇️
VM-ubuntu 98.37% <100.00%> (ø)
VM-windows 96.71% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@bysiber

bysiber commented Feb 20, 2026

Copy link
Copy Markdown
Contributor Author

Added a regression test for the empty realm case — it's a new parametrized entry in test_authenticate_scenarios that sends a challenge with realm="" and verifies the field is preserved in the parsed challenge dict.

@Dreamsorcerer Dreamsorcerer added backport-3.13 backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot labels Feb 20, 2026
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Feb 20, 2026
@Dreamsorcerer
Dreamsorcerer merged commit 51d5dba into aio-libs:master Feb 20, 2026
45 checks passed
@patchback

patchback Bot commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Backport to 3.13: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.13/51d5dbac70b5321da631e224c384058948889608/pr-12097

Backported as #12107

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback Bot pushed a commit that referenced this pull request Feb 20, 2026
@patchback

patchback Bot commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

Backport to 3.14: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.14/51d5dbac70b5321da631e224c384058948889608/pr-12097

Backported as #12108

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback Bot pushed a commit that referenced this pull request Feb 20, 2026
Dreamsorcerer pushed a commit that referenced this pull request Feb 21, 2026
…e fields with empty string values (#12108)

**This is a backport of PR #12097 as merged into master
(51d5dba).**

Co-authored-by: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com>
Dreamsorcerer pushed a commit that referenced this pull request Feb 21, 2026
…e fields with empty string values (#12107)

**This is a backport of PR #12097 as merged into master
(51d5dba).**

Co-authored-by: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com>
wavebyrd pushed a commit to wavebyrd/aiohttp that referenced this pull request Mar 13, 2026
…challenge fields with empty string values (aio-libs#12107)

**This is a backport of PR aio-libs#12097 as merged into master
(4800115).**

Co-authored-by: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport-3.14 Trigger automatic backporting to the 3.14 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants