Skip to content

Commit

Permalink
support FIPS builds without SHA-1 (#5460)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidism authored Apr 7, 2024
2 parents 7320e31 + db46111 commit b739390
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
-------------
Expand Down
10 changes: 9 additions & 1 deletion src/flask/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ def save_session(
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.
Expand All @@ -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"
Expand Down

0 comments on commit b739390

Please sign in to comment.