Skip to content

Add preemptive authentication support to DigestAuthMiddleware #11129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 4, 2025

Conversation

bdraco
Copy link
Member

@bdraco bdraco commented Jun 3, 2025

What do these changes do?

It was discovered that DigestAuthMiddleware doesn't work for some servers because the original implementation didn't implement preemptive support.

This PR adds preemptive authentication support to DigestAuthMiddleware, following RFC 7616 Section 3.6. The middleware now remembers successful authentication challenges and automatically includes the Authorization header in subsequent requests to the same protection space.

Key changes:

  • Added preemptive parameter to DigestAuthMiddleware constructor (default: True)
  • Implemented protection space tracking based on the domain parameter from server challenges
  • When no domain is specified, the entire origin becomes the protection space
  • Added support for the stale parameter to handle expired nonces
  • The middleware only sends preemptive auth to URLs within the same protection space

Are there changes in behavior for the user?

Yes, but backwards compatible:

  • By default, the middleware now uses preemptive authentication (can be disabled with preemptive=False)
  • Subsequent requests to the same protection space will include the Authorization header automatically
  • This improves performance by avoiding unnecessary 401 round trips
  • Matches how modern web browsers handle digest authentication

Related issue number

Fixes #11128

@bdraco bdraco changed the title Implement preemptive digest auth Add preemptive authentication support to DigestAuthMiddleware Jun 3, 2025
Copy link

codecov bot commented Jun 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.85%. Comparing base (e4bffe9) to head (6a121b1).
Report is 1 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master   #11129      +/-   ##
==========================================
- Coverage   98.85%   98.85%   -0.01%     
==========================================
  Files         131      131              
  Lines       42425    42630     +205     
  Branches     2282     2297      +15     
==========================================
+ Hits        41938    42140     +202     
- Misses        337      340       +3     
  Partials      150      150              
Flag Coverage Δ
CI-GHA 98.73% <100.00%> (-0.01%) ⬇️
OS-Linux 98.46% <100.00%> (+<0.01%) ⬆️
OS-Windows 96.78% <100.00%> (+0.02%) ⬆️
OS-macOS 97.67% <100.00%> (+0.01%) ⬆️
Py-3.10.11 97.45% <100.00%> (+0.01%) ⬆️
Py-3.10.17 97.94% <100.00%> (+<0.01%) ⬆️
Py-3.11.12 98.10% <100.00%> (+<0.01%) ⬆️
Py-3.11.9 97.62% <100.00%> (+<0.01%) ⬆️
Py-3.12.10 98.50% <100.00%> (+<0.01%) ⬆️
Py-3.13.3 98.48% <100.00%> (+0.01%) ⬆️
Py-3.9.13 97.33% <100.00%> (+0.01%) ⬆️
Py-3.9.22 97.82% <100.00%> (+<0.01%) ⬆️
Py-pypy7.3.16 94.28% <100.00%> (-3.14%) ⬇️
VM-macos 97.67% <100.00%> (+0.01%) ⬆️
VM-ubuntu 98.46% <100.00%> (+<0.01%) ⬆️
VM-windows 96.78% <100.00%> (+0.02%) ⬆️

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.

@psf-chronographer psf-chronographer bot added the bot:chronographer:provided There is a change note present in this PR label Jun 3, 2025
Copy link

codspeed-hq bot commented Jun 3, 2025

CodSpeed Performance Report

Merging #11129 will not alter performance

Comparing digest_auth_preemptive (6a121b1) with master (e4bffe9)

Summary

✅ 59 untouched benchmarks

@bdraco bdraco added backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot backport-3.13 Trigger automatic backporting to the 3.13 release branch by Patchback robot labels Jun 3, 2025
@bdraco bdraco marked this pull request as ready for review June 4, 2025 08:02
@bdraco bdraco requested review from webknjaz and asvetlov as code owners June 4, 2025 08:02
@bdraco bdraco merged commit c0449bb into master Jun 4, 2025
40 checks passed
@bdraco bdraco deleted the digest_auth_preemptive branch June 4, 2025 08:29
Copy link
Contributor

patchback bot commented Jun 4, 2025

Backport to 3.12: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.12/c0449bb5bfb49904afdc81fc26fa4634cbc66b40/pr-11129

Backported as #11131

🤖 @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 Jun 4, 2025
Copy link
Contributor

patchback bot commented Jun 4, 2025

Backport to 3.13: 💚 backport PR created

✅ Backport PR branch: patchback/backports/3.13/c0449bb5bfb49904afdc81fc26fa4634cbc66b40/pr-11129

Backported as #11132

🤖 @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 Jun 4, 2025
bdraco added a commit that referenced this pull request Jun 4, 2025
…port to DigestAuthMiddleware (#11131)

Co-authored-by: J. Nick Koston <[email protected]>
Fixes #11128
bdraco added a commit that referenced this pull request Jun 4, 2025
…port to DigestAuthMiddleware (#11132)

Co-authored-by: J. Nick Koston <[email protected]>
Fixes #11128

HTTP digest authentication client middleware.

:param str login: login
:param str password: password
:param bool preemptive: Enable preemptive authentication (default: ``True``)
Copy link
Member

Choose a reason for hiding this comment

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

Is there any point at all to being able to disable this? Seems like a pointless option at a glance. If it's enabled and the server doesn't recognise the header, I think it will respond just the same as if this setting was False, meaning there would be no advantage to disabling it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Its compatibility only as some really bad IOT device implementations choke if you send it preemptively so there needs to be a way to disable. I expect 99.9% of the time you never need to touch it.

Copy link
Member

Choose a reason for hiding this comment

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

Perhaps, spell it out in the docs? Maybe, in a .. hint:: admonition or similar?

Also, when referencing True / False / None in Sphinx, try using the :py:data: role as that's how they are declared in the CPython docs.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added to my queue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-3.12 Trigger automatic backporting to the 3.12 release branch by Patchback robot backport-3.13 Trigger automatic backporting to the 3.13 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.

ClientDigestMiddleware doesn't work like browsers
3 participants