diff options
| author | Michael Manfre <mike@manfre.net> | 2024-06-14 22:12:58 -0400 |
|---|---|---|
| committer | Natalia <124304+nessita@users.noreply.github.com> | 2024-07-09 09:21:19 -0300 |
| commit | 5d8645857936c142a3973694799c52165e2bdcdb (patch) | |
| tree | 4adc96a93c6d2ff8a1d257d8e90ff9a558da703b /tests/auth_tests/test_hashers.py | |
| parent | d6664574539c1531612dea833d264ed5c2b04e1e (diff) | |
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.py | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/tests/auth_tests/test_hashers.py b/tests/auth_tests/test_hashers.py index 09d7056411..1b41c75e69 100644 --- a/tests/auth_tests/test_hashers.py +++ b/tests/auth_tests/test_hashers.py @@ -452,6 +452,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, |
