summaryrefslogtreecommitdiff
path: root/passlib
diff options
context:
space:
mode:
authorEli Collins <elic@assurancetechnologies.com>2019-11-11 13:43:29 -0500
committerEli Collins <elic@assurancetechnologies.com>2019-11-11 13:43:29 -0500
commit190e4a35eb29619f52dd17fec66e5638dc0b2173 (patch)
treea411f75fb62dbe067e00e4edc5b330678617ba31 /passlib
parente13c7c25b7145b5ef3fa0e7a424f46ab02a106c4 (diff)
downloadpasslib-190e4a35eb29619f52dd17fec66e5638dc0b2173.tar.gz
passlib.apache: adjust default context to use "2y" bcrypt hashes instead of "2b"
(fixes issue 95). also added UT which checks behavior against locally-installed 'htpasswd' tool (if available).
Diffstat (limited to 'passlib')
-rw-r--r--passlib/apache.py13
-rw-r--r--passlib/tests/backports.py4
-rw-r--r--passlib/tests/test_apache.py120
3 files changed, 133 insertions, 4 deletions
diff --git a/passlib/apache.py b/passlib/apache.py
index 04a64d0..35f72e5 100644
--- a/passlib/apache.py
+++ b/passlib/apache.py
@@ -465,6 +465,7 @@ def _init_default_schemes():
# set latest-apache version aliases
# XXX: could check for apache install, and pick correct host 22/24 default?
+ # could reuse _detect_htpasswd() helper in UTs
defaults.update(
portable=defaults['portable_apache_24'],
host=defaults['host_apache_24'],
@@ -507,8 +508,16 @@ def _init_htpasswd_context():
preferred = schemes[:3] + ["apr_md5_crypt"] + schemes
schemes = sorted(set(schemes), key=preferred.index)
- # NOTE: default will change to "portable" in passlib 2.0
- return CryptContext(schemes, default=htpasswd_defaults['portable_apache_22'])
+ # create context object
+ return CryptContext(
+ schemes=schemes,
+
+ # NOTE: default will change to "portable" in passlib 2.0
+ default=htpasswd_defaults['portable_apache_22'],
+
+ # NOTE: bcrypt "2y" is required, "2b" isn't recognized by libapr (issue 95)
+ bcrypt__ident="2y",
+ )
#: CryptContext configured to match htpasswd
htpasswd_context = _init_htpasswd_context()
diff --git a/passlib/tests/backports.py b/passlib/tests/backports.py
index c93b599..5058cec 100644
--- a/passlib/tests/backports.py
+++ b/passlib/tests/backports.py
@@ -14,7 +14,9 @@ from passlib.utils.compat import PY26
# local
__all__ = [
"TestCase",
- "skip", "skipIf", "skipUnless"
+ "unittest",
+ # TODO: deprecate these exports in favor of "unittest.XXX"
+ "skip", "skipIf", "skipUnless",
]
#=============================================================================
diff --git a/passlib/tests/test_apache.py b/passlib/tests/test_apache.py
index c734c99..198b425 100644
--- a/passlib/tests/test_apache.py
+++ b/passlib/tests/test_apache.py
@@ -6,17 +6,24 @@ from __future__ import with_statement
# core
from logging import getLogger
import os
+import subprocess
# site
# pkg
-from passlib import apache
+from passlib import apache, registry
from passlib.exc import MissingBackendError
from passlib.utils.compat import irange
+from passlib.tests.backports import unittest
from passlib.tests.utils import TestCase, get_file, set_file, ensure_mtime_changed
from passlib.utils.compat import u
from passlib.utils import to_bytes
+from passlib.utils.handlers import to_unicode_for_identify
# module
log = getLogger(__name__)
+#=============================================================================
+# helpers
+#=============================================================================
+
def backdate_file_mtime(path, offset=10):
"""backdate file's mtime by specified amount"""
# NOTE: this is used so we can test code which detects mtime changes,
@@ -26,6 +33,58 @@ def backdate_file_mtime(path, offset=10):
os.utime(path, (atime, mtime))
#=============================================================================
+# detect external HTPASSWD tool
+#=============================================================================
+
+
+htpasswd_path = os.environ.get("PASSLIB_TEST_HTPASSWD_PATH") or "htpasswd"
+
+
+def _call_htpasswd(args, stdin=None):
+ """
+ helper to run htpasswd cmd
+ """
+ if stdin is not None:
+ stdin = stdin.encode("utf-8")
+ proc = subprocess.Popen([htpasswd_path] + args, stdout=subprocess.PIPE,
+ stderr=subprocess.STDOUT, stdin=subprocess.PIPE if stdin else None)
+ out, err = proc.communicate(stdin)
+ rc = proc.wait()
+ out = to_unicode_for_identify(out or "")
+ return out, rc
+
+
+def _call_htpasswd_verify(path, user, password):
+ """
+ wrapper for htpasswd verify
+ """
+ out, rc = _call_htpasswd(["-vi", path, user], password)
+ return not rc
+
+
+def _detect_htpasswd():
+ """
+ helper to check if htpasswd is present
+ """
+ try:
+ out, rc = _call_htpasswd([])
+ except OSError:
+ # TODO: under py3, could trap the more specific FileNotFoundError
+ # cmd not found
+ return False, False
+ # when called w/o args, it should print usage to stderr & return rc=2
+ if not rc:
+ log.warning("htpasswd test returned with rc=0")
+ have_bcrypt = " -B " in out
+ return True, have_bcrypt
+
+
+HAVE_HTPASSWD, HAVE_HTPASSWD_BCRYPT = _detect_htpasswd()
+
+requires_htpasswd_cmd = unittest.skipUnless(HAVE_HTPASSWD, "requires `htpasswd` cmdline tool")
+
+
+#=============================================================================
# htpasswd
#=============================================================================
class HtpasswdFileTest(TestCase):
@@ -384,6 +443,65 @@ class HtpasswdFileTest(TestCase):
)
self.assertEqual(ht.to_string(), target)
+ @requires_htpasswd_cmd
+ def test_htpasswd_cmd_verify(self):
+ """
+ verify "htpasswd" command can read output
+ """
+ path = self.mktemp()
+ ht = apache.HtpasswdFile(path=path, new=True)
+
+ def hash_scheme(pwd, scheme):
+ return ht.context.handler(scheme).hash(pwd)
+
+ # base scheme
+ ht.set_hash("user1", hash_scheme("password","apr_md5_crypt"))
+
+ # 2.2-compat scheme
+ host_no_bcrypt = apache.htpasswd_defaults["host_apache_22"]
+ ht.set_hash("user2", hash_scheme("password", host_no_bcrypt))
+
+ # 2.4-compat scheme
+ host_best = apache.htpasswd_defaults["host"]
+ ht.set_hash("user3", hash_scheme("password", host_best))
+
+ # unsupported scheme -- should always fail to verify
+ ht.set_hash("user4", "$xxx$foo$bar$baz")
+
+ # make sure htpasswd properly recognizes hashes
+ ht.save()
+
+ self.assertFalse(_call_htpasswd_verify(path, "user1", "wrong"))
+ self.assertFalse(_call_htpasswd_verify(path, "user2", "wrong"))
+ self.assertFalse(_call_htpasswd_verify(path, "user3", "wrong"))
+ self.assertFalse(_call_htpasswd_verify(path, "user4", "wrong"))
+
+ self.assertTrue(_call_htpasswd_verify(path, "user1", "password"))
+ self.assertTrue(_call_htpasswd_verify(path, "user2", "password"))
+ self.assertTrue(_call_htpasswd_verify(path, "user3", "password"))
+
+ @requires_htpasswd_cmd
+ @unittest.skipUnless(registry.has_backend("bcrypt"), "bcrypt support required")
+ def test_htpasswd_cmd_verify_bcrypt(self):
+ """
+ verify "htpasswd" command can read bcrypt format
+
+ this tests for regression of issue 95, where we output "$2b$" instead of "$2y$";
+ fixed in v1.7.2.
+ """
+ path = self.mktemp()
+ ht = apache.HtpasswdFile(path=path, new=True)
+ def hash_scheme(pwd, scheme):
+ return ht.context.handler(scheme).hash(pwd)
+ ht.set_hash("user1", hash_scheme("password", "bcrypt"))
+ ht.save()
+ self.assertFalse(_call_htpasswd_verify(path, "user1", "wrong"))
+ if HAVE_HTPASSWD_BCRYPT:
+ self.assertTrue(_call_htpasswd_verify(path, "user1", "password"))
+ else:
+ # apache2.2 should fail, acting like it's an unknown hash format
+ self.assertFalse(_call_htpasswd_verify(path, "user1", "password"))
+
#===================================================================
# eoc
#===================================================================