Skip to content

Commit b739390

Browse files
authored
support FIPS builds without SHA-1 (#5460)
2 parents 7320e31 + db46111 commit b739390

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ Version 3.0.3
33

44
Unreleased
55

6+
- The default ``hashlib.sha1`` may not be available in FIPS builds. Don't
7+
access it at import time so the developer has time to change the default.
8+
:issue:`5448`
9+
610

711
Version 3.0.2
812
-------------

src/flask/sessions.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,14 @@ def save_session(
277277
session_json_serializer = TaggedJSONSerializer()
278278

279279

280+
def _lazy_sha1(string: bytes = b"") -> t.Any:
281+
"""Don't access ``hashlib.sha1`` until runtime. FIPS builds may not include
282+
SHA-1, in which case the import and use as a default would fail before the
283+
developer can configure something else.
284+
"""
285+
return hashlib.sha1(string)
286+
287+
280288
class SecureCookieSessionInterface(SessionInterface):
281289
"""The default session interface that stores sessions in signed cookies
282290
through the :mod:`itsdangerous` module.
@@ -286,7 +294,7 @@ class SecureCookieSessionInterface(SessionInterface):
286294
#: signing of cookie based sessions.
287295
salt = "cookie-session"
288296
#: the hash function to use for the signature. The default is sha1
289-
digest_method = staticmethod(hashlib.sha1)
297+
digest_method = staticmethod(_lazy_sha1)
290298
#: the name of the itsdangerous supported key derivation. The default
291299
#: is hmac.
292300
key_derivation = "hmac"

0 commit comments

Comments
 (0)