@@ -589,27 +589,31 @@ def wrapper(key, obj):
589
589
590
590
591
591
@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
+ """
594
599
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 ))
615
619
yield
0 commit comments