Skip to content

Commit 20c9103

Browse files
committed
fix tests on FIPS builds
1 parent 4c36842 commit 20c9103

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

Lib/test/support/hashlib_helper.py

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -589,27 +589,31 @@ def wrapper(key, obj):
589589

590590

591591
@contextlib.contextmanager
592-
def block_algorithm(*names, allow_openssl=False, allow_builtin=False):
593-
"""Block a hash algorithm for both hashing and HMAC."""
592+
def block_algorithm(name, *, allow_openssl=False, allow_builtin=False):
593+
"""Block a hash algorithm for both hashing and HMAC.
594+
595+
Be careful with this helper as a function may be allowed, but can
596+
still raise a ValueError at runtime if the OpenSSL security policy
597+
disables it, e.g., if allow_openssl=True and FIPS mode is on.
598+
"""
594599
with contextlib.ExitStack() as stack:
595-
for name in names:
596-
if not (allow_openssl or allow_builtin):
597-
# If one of the private interface is allowed, then the
598-
# public interface will fallback to it even though the
599-
# comment in hashlib.py says otherwise.
600-
#
601-
# So we should only block it if the private interfaces
602-
# are blocked as well.
603-
stack.enter_context(_block_hashlib_hash_constructor(name))
604-
if not allow_openssl:
605-
stack.enter_context(_block_openssl_hash_new(name))
606-
stack.enter_context(_block_openssl_hmac_new(name))
607-
stack.enter_context(_block_openssl_hmac_digest(name))
608-
stack.enter_context(_block_openssl_hash_constructor(name))
609-
if not allow_builtin:
610-
stack.enter_context(_block_builtin_hash_new(name))
611-
stack.enter_context(_block_builtin_hmac_new(name))
612-
stack.enter_context(_block_builtin_hmac_digest(name))
613-
stack.enter_context(_block_builtin_hash_constructor(name))
614-
stack.enter_context(_block_builtin_hmac_constructor(name))
600+
if not (allow_openssl or allow_builtin):
601+
# If one of the private interface is allowed, then the
602+
# public interface will fallback to it even though the
603+
# comment in hashlib.py says otherwise.
604+
#
605+
# So we should only block it if the private interfaces
606+
# are blocked as well.
607+
stack.enter_context(_block_hashlib_hash_constructor(name))
608+
if not allow_openssl:
609+
stack.enter_context(_block_openssl_hash_new(name))
610+
stack.enter_context(_block_openssl_hmac_new(name))
611+
stack.enter_context(_block_openssl_hmac_digest(name))
612+
stack.enter_context(_block_openssl_hash_constructor(name))
613+
if not allow_builtin:
614+
stack.enter_context(_block_builtin_hash_new(name))
615+
stack.enter_context(_block_builtin_hmac_new(name))
616+
stack.enter_context(_block_builtin_hmac_digest(name))
617+
stack.enter_context(_block_builtin_hash_constructor(name))
618+
stack.enter_context(_block_builtin_hmac_constructor(name))
615619
yield

Lib/test/test_support.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,11 @@ def check_builtin_hmac(self, name, *, disabled=True):
935935
)
936936
)
937937
def test_disable_hash(self, name, allow_openssl, allow_builtin):
938+
# In FIPS mode, the function may be available but would still need
939+
# to raise a ValueError. For simplicity, we don't test the helper
940+
# when we're in FIPS mode.
941+
if self._hashlib.get_fips_mode():
942+
self.skipTest("hash functions may still be blocked in FIPS mode")
938943
flags = dict(allow_openssl=allow_openssl, allow_builtin=allow_builtin)
939944
is_simple_disabled = not allow_builtin and not allow_openssl
940945

0 commit comments

Comments
 (0)