From dd552a8df1ddf04f591c523d0a8edd62c5e9b77f Mon Sep 17 00:00:00 2001 From: Eli Collins Date: Sat, 28 Mar 2020 18:50:54 -0400 Subject: passlib.utils: have safe_crypt() catch OSError thrown by crypt() -- py39 compat (fixes issue 115) --- docs/history/1.7.rst | 3 +++ passlib/utils/__init__.py | 10 +++++++++- setup.py | 1 + tox.ini | 3 ++- 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/docs/history/1.7.rst b/docs/history/1.7.rst index 132a343..5b61513 100644 --- a/docs/history/1.7.rst +++ b/docs/history/1.7.rst @@ -21,6 +21,9 @@ Bugfixes * :func:`passlib.utils.safe_crypt`: Support :func:`crypt.crypt` unexpectedly returning bytes under Python 3 (:issue:`113`). +* :func:`passlib.utils.safe_crypt`: Support :func:`crypt.crypt` throwing :exc:`OSError`, + which can happen as of Python 3.9 (:issue:`115`). + **1.7.2** (2019-11-22) ====================== diff --git a/passlib/utils/__init__.py b/passlib/utils/__init__.py index 1994fe6..ba54b2f 100644 --- a/passlib/utils/__init__.py +++ b/passlib/utils/__init__.py @@ -785,13 +785,21 @@ else: secret = secret.decode("utf-8") except UnicodeDecodeError: return None + # sanity check it encodes back to original byte string, + # otherwise when crypt() does it's encoding, it'll hash the wrong bytes! assert secret.encode("utf-8") == orig, \ "utf-8 spec says this can't happen!" if _NULL in secret: raise ValueError("null character in secret") if isinstance(hash, bytes): hash = hash.decode("ascii") - result = _crypt(secret, hash) + try: + 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 + # translating that to None for now (may revise safe_crypt behavior in future) + return None # NOTE: per issue 113, crypt() may return bytes in some odd cases. # assuming it should still return an ASCII hash though, # or there's a bigger issue at hand. diff --git a/setup.py b/setup.py index f715240..255bca9 100644 --- a/setup.py +++ b/setup.py @@ -113,6 +113,7 @@ Programming Language :: Python :: 3.5 Programming Language :: Python :: 3.6 Programming Language :: Python :: 3.7 Programming Language :: Python :: 3.8 +Programming Language :: Python :: 3.9 Programming Language :: Python :: Implementation :: CPython Programming Language :: Python :: Implementation :: Jython Programming Language :: Python :: Implementation :: PyPy diff --git a/tox.ini b/tox.ini index 8a99d6b..c49445a 100644 --- a/tox.ini +++ b/tox.ini @@ -47,7 +47,7 @@ envlist = # TODO: would like to 'default-pyston' but doesnt quite work # TODO: also add default-jython27 # NOTE: removed 2.6 & 3.3 as of 2019-11, tox+pip no longer work for these versions. - default-py{27,34,35,36,37,38,py,py3}, + default-py{27,34,35,36,37,38,39,py,py3}, # pbkdf2 backend testing # NOTE: 'hashlib' takes priority under py34+ @@ -102,6 +102,7 @@ basepython = py36: python3.6 py37: python3.7 py38: python3.8 + py39: python3.9 pypy: pypy pypy3: pypy3 -- cgit v1.2.1