diff options
author | Eli Collins <elic@assurancetechnologies.com> | 2020-05-12 12:06:12 -0400 |
---|---|---|
committer | Eli Collins <elic@assurancetechnologies.com> | 2020-05-12 12:06:12 -0400 |
commit | bc91df7f02c9b2977d86d8398dd1a1af37d0c591 (patch) | |
tree | d12b230a235380323e1e21f4192a9d3c450256c6 | |
parent | fd5f041dbd807f18cb250ce13c16c0c4b7362cac (diff) | |
download | passlib-bc91df7f02c9b2977d86d8398dd1a1af37d0c591.tar.gz |
tests / bcrypt: bcrypt_os_crypt_test now strips out non-UTF8 test vectors under py3,
since these can't be passed through crypt.crypt()
-rw-r--r-- | passlib/tests/test_handlers_bcrypt.py | 9 | ||||
-rw-r--r-- | passlib/utils/__init__.py | 13 |
2 files changed, 20 insertions, 2 deletions
diff --git a/passlib/tests/test_handlers_bcrypt.py b/passlib/tests/test_handlers_bcrypt.py index e9d2280..a573d4e 100644 --- a/passlib/tests/test_handlers_bcrypt.py +++ b/passlib/tests/test_handlers_bcrypt.py @@ -11,8 +11,8 @@ import warnings # pkg from passlib import hash from passlib.handlers.bcrypt import IDENT_2, IDENT_2X -from passlib.utils import repeat_string, to_bytes -from passlib.utils.compat import irange +from passlib.utils import repeat_string, to_bytes, is_safe_crypt_input +from passlib.utils.compat import irange, PY3 from passlib.tests.utils import HandlerCase, TEST_MODE from passlib.tests.test_handlers import UPASS_TABLE # module @@ -178,6 +178,7 @@ class _bcrypt_test(HandlerCase): else: self.addCleanup(os.environ.__delitem__, key) os.environ[key] = "true" + super(_bcrypt_test, self).setUp() # silence this warning, will come up a bunch during testing of old 2a hashes. @@ -429,6 +430,10 @@ bcrypt_bcryptor_test = _bcrypt_test.create_backend_case("bcryptor") class bcrypt_os_crypt_test(_bcrypt_test.create_backend_case("os_crypt")): + # os crypt doesn't support non-utf8 secret bytes + known_correct_hashes = [row for row in _bcrypt_test.known_correct_hashes + if is_safe_crypt_input(row[0])] + # os crypt backend doesn't currently implement a per-call fallback if it fails has_os_crypt_fallback = False diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index b9fad4d..cb8872c 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -756,6 +756,19 @@ def as_bool(value, none=None, param="boolean"): # host OS helpers #============================================================================= +def is_safe_crypt_input(value): + """ + UT helper -- + test if value is safe to pass to crypt.crypt(); + under PY3, can't pass non-UTF8 bytes to crypt.crypt. + """ + try: + if PY3 and isinstance(value, bytes): + value.decode("utf-8") + return True + except UnicodeDecodeError: + return False + try: from crypt import crypt as _crypt except ImportError: # pragma: no cover |