summaryrefslogtreecommitdiff
path: root/passlib
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2020-10-05 21:26:25 -0400
committerEli Collins <elic@assurancetechnologies.com>2020-10-05 21:26:25 -0400
commit6badf316805b188c92165e342ca88ce3be9a96db (patch)
tree735fe346eb1140c858fc3e57a08ac3eb5292905d /passlib
parent3f1ad211d9499f5c36e9cc90c888c6e0da50ab3c (diff)
downloadpasslib-6badf316805b188c92165e342ca88ce3be9a96db.tar.gz
passlib.utils: add thread lock to fix some UT failures.
Diffstat (limited to 'passlib')
-rw-r--r--passlib/tests/utils.py12
-rw-r--r--passlib/utils/__init__.py27
-rw-r--r--passlib/utils/compat/__init__.py25
3 files changed, 58 insertions, 6 deletions
diff --git a/passlib/tests/utils.py b/passlib/tests/utils.py
index a698ac7..c8ab5a4 100644
--- a/passlib/tests/utils.py
+++ b/passlib/tests/utils.py
@@ -2728,7 +2728,9 @@ class HandlerCase(TestCase):
failed[0] += 1
raise
def launch(n):
- name = "Fuzz-Thread-%d" % (n,)
+ cls = type(self)
+ name = "Fuzz-Thread-%d ('%s:%s.%s')" % (n, cls.__module__, cls.__name__,
+ self._testMethodName)
thread = threading.Thread(target=wrapper, name=name)
thread.setDaemon(True)
thread.start()
@@ -3313,8 +3315,9 @@ class OsCryptMixin(HandlerCase):
(platform, name))
#===================================================================
- # fuzzy verified support -- add new verified that uses os crypt()
+ # fuzzy verified support -- add additional verifier that uses os crypt()
#===================================================================
+
def fuzz_verifier_crypt(self):
"""test results against OS crypt()"""
@@ -3326,14 +3329,17 @@ class OsCryptMixin(HandlerCase):
# create a wrapper for fuzzy verified to use
from crypt import crypt
+ from passlib.utils import _safe_crypt_lock
encoding = self.FuzzHashGenerator.password_encoding
def check_crypt(secret, hash):
"""stdlib-crypt"""
if not self.crypt_supports_variant(hash):
return "skip"
+ # XXX: any reason not to use safe_crypt() here? or just want to test against bare metal?
secret = to_native_str(secret, encoding)
- return crypt(secret, hash) == hash
+ with _safe_crypt_lock:
+ return crypt(secret, hash) == hash
return check_crypt
diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py
index fd30b3e..e3df5d5 100644
--- a/passlib/utils/__init__.py
+++ b/passlib/utils/__init__.py
@@ -36,6 +36,11 @@ else:
import time
if stringprep:
import unicodedata
+try:
+ import threading
+except ImportError:
+ # module optional before py37
+ threading = None
import timeit
import types
from warnings import warn
@@ -60,7 +65,7 @@ from passlib.utils.compat import (add_doc, join_bytes, join_byte_values,
join_byte_elems, irange, imap, PY3, u,
join_unicode, unicode, byte_elem_value, nextgetter,
unicode_or_str, unicode_or_bytes_types,
- get_method_function, suppress_cause)
+ get_method_function, suppress_cause, PYPY)
# local
__all__ = [
# constants
@@ -856,6 +861,20 @@ else:
has_crypt = True
_NULL = '\x00'
+ # XXX: replace this with lazy-evaluated bug detection?
+ if threading and PYPY and (7, 2, 0) <= sys.pypy_version_info <= (7, 3, 3):
+ #: internal lock used to wrap crypt() calls.
+ #: WARNING: if non-passlib code invokes crypt(), this lock won't be enough!
+ _safe_crypt_lock = threading.Lock()
+
+ #: detect if crypt.crypt() needs a thread lock around calls.
+ crypt_needs_lock = True
+
+ else:
+ from passlib.utils.compat import nullcontext
+ _safe_crypt_lock = nullcontext()
+ crypt_needs_lock = False
+
# some crypt() variants will return various constant strings when
# an invalid/unrecognized config string is passed in; instead of
# returning NULL / None. examples include ":", ":0", "*0", etc.
@@ -908,7 +927,8 @@ else:
if isinstance(hash, bytes):
hash = hash.decode("ascii")
try:
- result = _crypt(secret, hash)
+ with _safe_crypt_lock:
+ result = _crypt(secret, hash)
except OSError:
# new in py39 -- per https://bugs.python.org/issue39289,
# crypt() now throws OSError for various things, mainly unknown hash formats
@@ -935,7 +955,8 @@ else:
raise ValueError("null character in secret")
if isinstance(hash, unicode):
hash = hash.encode("ascii")
- result = _crypt(secret, hash)
+ with _safe_crypt_lock:
+ result = _crypt(secret, hash)
if not result:
return None
result = result.decode("ascii")
diff --git a/passlib/utils/compat/__init__.py b/passlib/utils/compat/__init__.py
index 1d6cf4d..f6ead24 100644
--- a/passlib/utils/compat/__init__.py
+++ b/passlib/utils/compat/__init__.py
@@ -80,6 +80,9 @@ __all__ = [
# collections
'OrderedDict',
+ # context helpers
+ 'nullcontext',
+
# introspection
'get_method_function', 'add_doc',
]
@@ -374,6 +377,28 @@ else:
_lazy_attrs['OrderedDict'] = 'collections.OrderedDict'
#=============================================================================
+# context managers
+#=============================================================================
+
+try:
+ # new in py37
+ from contextlib import nullcontext
+except ImportError:
+
+ class nullcontext(object):
+ """
+ Context manager that does no additional processing.
+ """
+ def __init__(self, enter_result=None):
+ self.enter_result = enter_result
+
+ def __enter__(self):
+ return self.enter_result
+
+ def __exit__(self, *exc_info):
+ pass
+
+#=============================================================================
# lazy overlay module
#=============================================================================
from types import ModuleType