support FIPS builds without SHA-1 (#5460)

This commit is contained in:
David Lord 2024-04-07 11:42:24 -07:00 committed by GitHub
commit b739390955
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 1 deletions

View file

@ -3,6 +3,10 @@ Version 3.0.3
Unreleased
- The default ``hashlib.sha1`` may not be available in FIPS builds. Don't
access it at import time so the developer has time to change the default.
:issue:`5448`
Version 3.0.2
-------------

View file

@ -277,6 +277,14 @@ class SessionInterface:
session_json_serializer = TaggedJSONSerializer()
def _lazy_sha1(string: bytes = b"") -> t.Any:
"""Don't access ``hashlib.sha1`` until runtime. FIPS builds may not include
SHA-1, in which case the import and use as a default would fail before the
developer can configure something else.
"""
return hashlib.sha1(string)
class SecureCookieSessionInterface(SessionInterface):
"""The default session interface that stores sessions in signed cookies
through the :mod:`itsdangerous` module.
@ -286,7 +294,7 @@ class SecureCookieSessionInterface(SessionInterface):
#: signing of cookie based sessions.
salt = "cookie-session"
#: the hash function to use for the signature. The default is sha1
digest_method = staticmethod(hashlib.sha1)
digest_method = staticmethod(_lazy_sha1)
#: the name of the itsdangerous supported key derivation. The default
#: is hmac.
key_derivation = "hmac"