summaryrefslogtreecommitdiff
path: root/tests/auth_tests/test_hashers.py
diff options
context:
space:
mode:
authorMichael Manfre <mike@manfre.net>2024-06-14 22:12:58 -0400
committerNatalia <124304+nessita@users.noreply.github.com>2024-07-09 10:03:20 -0300
commit07cefdee4a9d1fcd9a3a631cbd07c78defd1923b (patch)
tree34e5c5f44608fdcffd4c6ed0a6dbce944da7bf42 /tests/auth_tests/test_hashers.py
parent7285644640f085f41d60ab0c8ae4e9153f0485db (diff)
[5.0.x] Fixed CVE-2024-39329 -- Standarized timing of verify_password() when checking unusuable passwords.
Refs #20760. Thanks Michael Manfre for the fix and to Adam Johnson for the review.
Diffstat (limited to 'tests/auth_tests/test_hashers.py')
-rw-r--r--tests/auth_tests/test_hashers.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/tests/auth_tests/test_hashers.py b/tests/auth_tests/test_hashers.py
index cf2ebafa15..d5718fd79a 100644
--- a/tests/auth_tests/test_hashers.py
+++ b/tests/auth_tests/test_hashers.py
@@ -565,6 +565,38 @@ class TestUtilsHashPass(SimpleTestCase):
check_password("wrong_password", encoded)
self.assertEqual(hasher.harden_runtime.call_count, 1)
+ def test_check_password_calls_make_password_to_fake_runtime(self):
+ hasher = get_hasher("default")
+ cases = [
+ (None, None, None), # no plain text password provided
+ ("foo", make_password(password=None), None), # unusable encoded
+ ("letmein", make_password(password="letmein"), ValueError), # valid encoded
+ ]
+ for password, encoded, hasher_side_effect in cases:
+ with (
+ self.subTest(encoded=encoded),
+ mock.patch(
+ "django.contrib.auth.hashers.identify_hasher",
+ side_effect=hasher_side_effect,
+ ) as mock_identify_hasher,
+ mock.patch(
+ "django.contrib.auth.hashers.make_password"
+ ) as mock_make_password,
+ mock.patch(
+ "django.contrib.auth.hashers.get_random_string",
+ side_effect=lambda size: "x" * size,
+ ),
+ mock.patch.object(hasher, "verify"),
+ ):
+ # Ensure make_password is called to standardize timing.
+ check_password(password, encoded)
+ self.assertEqual(hasher.verify.call_count, 0)
+ self.assertEqual(mock_identify_hasher.mock_calls, [mock.call(encoded)])
+ self.assertEqual(
+ mock_make_password.mock_calls,
+ [mock.call("x" * UNUSABLE_PASSWORD_SUFFIX_LENGTH)],
+ )
+
def test_encode_invalid_salt(self):
hasher_classes = [
MD5PasswordHasher,