2021-01-11 19:31:49 +00:00
|
|
|
from datetime import datetime, timedelta
|
2010-10-10 01:06:42 +00:00
|
|
|
|
|
|
|
from django.conf import settings
|
2011-07-13 09:35:51 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-01-28 12:35:27 +00:00
|
|
|
from django.contrib.auth.tokens import PasswordResetTokenGenerator
|
2021-04-19 07:58:34 +00:00
|
|
|
from django.core.exceptions import ImproperlyConfigured
|
2010-10-10 01:06:42 +00:00
|
|
|
from django.test import TestCase
|
2021-04-19 07:58:34 +00:00
|
|
|
from django.test.utils import override_settings
|
2010-10-10 01:06:42 +00:00
|
|
|
|
2016-05-16 01:54:03 +00:00
|
|
|
from .models import CustomEmailField
|
|
|
|
|
2010-10-10 01:06:42 +00:00
|
|
|
|
2019-11-13 09:31:41 +00:00
|
|
|
class MockedPasswordResetTokenGenerator(PasswordResetTokenGenerator):
|
|
|
|
def __init__(self, now):
|
|
|
|
self._now_val = now
|
2020-07-31 18:56:33 +00:00
|
|
|
super().__init__()
|
2019-11-13 09:31:41 +00:00
|
|
|
|
|
|
|
def _now(self):
|
|
|
|
return self._now_val
|
|
|
|
|
|
|
|
|
2010-10-10 01:06:42 +00:00
|
|
|
class TokenGeneratorTest(TestCase):
|
|
|
|
def test_make_token(self):
|
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
tk1 = p0.make_token(user)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p0.check_token(user, tk1), True)
|
2010-10-10 01:06:42 +00:00
|
|
|
|
|
|
|
def test_10265(self):
|
|
|
|
"""
|
2016-10-27 07:53:39 +00:00
|
|
|
The token generated for a user created in the same request
|
2010-10-10 01:06:42 +00:00
|
|
|
will work correctly.
|
|
|
|
"""
|
|
|
|
user = User.objects.create_user("comebackkid", "test3@example.com", "testpw")
|
2019-11-13 09:33:32 +00:00
|
|
|
user_reload = User.objects.get(username="comebackkid")
|
|
|
|
p0 = MockedPasswordResetTokenGenerator(datetime.now())
|
2010-10-10 01:06:42 +00:00
|
|
|
tk1 = p0.make_token(user)
|
2019-11-13 09:33:32 +00:00
|
|
|
tk2 = p0.make_token(user_reload)
|
2010-10-10 01:06:42 +00:00
|
|
|
self.assertEqual(tk1, tk2)
|
|
|
|
|
2016-05-16 01:54:03 +00:00
|
|
|
def test_token_with_different_email(self):
|
|
|
|
"""Updating the user email address invalidates the token."""
|
|
|
|
tests = [
|
|
|
|
(CustomEmailField, None),
|
|
|
|
(CustomEmailField, "test4@example.com"),
|
|
|
|
(User, "test4@example.com"),
|
|
|
|
]
|
|
|
|
for model, email in tests:
|
|
|
|
with self.subTest(model=model.__qualname__, email=email):
|
|
|
|
user = model.objects.create_user(
|
|
|
|
"changeemailuser",
|
|
|
|
email=email,
|
|
|
|
password="testpw",
|
|
|
|
)
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
tk1 = p0.make_token(user)
|
|
|
|
self.assertIs(p0.check_token(user, tk1), True)
|
|
|
|
setattr(user, user.get_email_field_name(), "test4new@example.com")
|
|
|
|
user.save()
|
|
|
|
self.assertIs(p0.check_token(user, tk1), False)
|
|
|
|
|
2010-10-10 01:06:42 +00:00
|
|
|
def test_timeout(self):
|
2019-08-23 15:14:07 +00:00
|
|
|
"""The token is valid after n seconds, but no greater."""
|
2010-10-10 01:06:42 +00:00
|
|
|
# Uses a mocked version of PasswordResetTokenGenerator so we can change
|
2019-08-23 15:14:07 +00:00
|
|
|
# the value of 'now'.
|
2010-10-10 01:06:42 +00:00
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
2020-03-12 09:56:38 +00:00
|
|
|
now = datetime.now()
|
|
|
|
p0 = MockedPasswordResetTokenGenerator(now)
|
2010-10-10 01:06:42 +00:00
|
|
|
tk1 = p0.make_token(user)
|
2019-11-13 09:31:41 +00:00
|
|
|
p1 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT)
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p1.check_token(user, tk1), True)
|
2019-11-13 09:31:41 +00:00
|
|
|
p2 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1))
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p2.check_token(user, tk1), False)
|
2019-08-23 15:14:07 +00:00
|
|
|
with self.settings(PASSWORD_RESET_TIMEOUT=60 * 60):
|
2019-11-13 09:31:41 +00:00
|
|
|
p3 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=settings.PASSWORD_RESET_TIMEOUT)
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p3.check_token(user, tk1), True)
|
2019-11-13 09:31:41 +00:00
|
|
|
p4 = MockedPasswordResetTokenGenerator(
|
2020-03-12 09:56:38 +00:00
|
|
|
now + timedelta(seconds=(settings.PASSWORD_RESET_TIMEOUT + 1))
|
2019-11-13 09:31:41 +00:00
|
|
|
)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p4.check_token(user, tk1), False)
|
Fixed #14445 - Use HMAC and constant-time comparison functions where needed.
All adhoc MAC applications have been updated to use HMAC, using SHA1 to
generate unique keys for each application based on the SECRET_KEY, which is
common practice for this situation. In all cases, backwards compatibility
with existing hashes has been maintained, aiming to phase this out as per
the normal deprecation process. In this way, under most normal
circumstances the old hashes will have expired (e.g. by session expiration
etc.) before they become invalid.
In the case of the messages framework and the cookie backend, which was
already using HMAC, there is the possibility of a backwards incompatibility
if the SECRET_KEY is shorter than the default 50 bytes, but the low
likelihood and low impact meant compatibility code was not worth it.
All known instances where tokens/hashes were compared using simple string
equality, which could potentially open timing based attacks, have also been
fixed using a constant-time comparison function.
There are no known practical attacks against the existing implementations,
so these security improvements will not be backported.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@14218 bcc190cf-cafb-0310-a4f2-bffc1f526a37
2010-10-14 20:54:30 +00:00
|
|
|
|
2017-01-13 14:17:54 +00:00
|
|
|
def test_check_token_with_nonexistent_token_and_user(self):
|
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
tk1 = p0.make_token(user)
|
|
|
|
self.assertIs(p0.check_token(None, tk1), False)
|
|
|
|
self.assertIs(p0.check_token(user, None), False)
|
2017-05-26 11:37:36 +00:00
|
|
|
|
|
|
|
def test_token_with_different_secret(self):
|
|
|
|
"""
|
|
|
|
A valid token can be created with a secret other than SECRET_KEY by
|
|
|
|
using the PasswordResetTokenGenerator.secret attribute.
|
|
|
|
"""
|
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
|
|
|
new_secret = "abcdefghijkl"
|
|
|
|
# Create and check a token with a different secret.
|
|
|
|
p0 = PasswordResetTokenGenerator()
|
|
|
|
p0.secret = new_secret
|
|
|
|
tk0 = p0.make_token(user)
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p0.check_token(user, tk0), True)
|
2017-05-26 11:37:36 +00:00
|
|
|
# Create and check a token with the default secret.
|
|
|
|
p1 = PasswordResetTokenGenerator()
|
|
|
|
self.assertEqual(p1.secret, settings.SECRET_KEY)
|
|
|
|
self.assertNotEqual(p1.secret, new_secret)
|
|
|
|
tk1 = p1.make_token(user)
|
|
|
|
# Tokens created with a different secret don't validate.
|
2020-01-29 08:01:18 +00:00
|
|
|
self.assertIs(p0.check_token(user, tk1), False)
|
|
|
|
self.assertIs(p1.check_token(user, tk0), False)
|
2021-04-20 04:58:51 +00:00
|
|
|
|
|
|
|
def test_token_with_different_secret_subclass(self):
|
|
|
|
class CustomPasswordResetTokenGenerator(PasswordResetTokenGenerator):
|
|
|
|
secret = "test-secret"
|
|
|
|
|
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
|
|
|
custom_password_generator = CustomPasswordResetTokenGenerator()
|
|
|
|
tk_custom = custom_password_generator.make_token(user)
|
|
|
|
self.assertIs(custom_password_generator.check_token(user, tk_custom), True)
|
|
|
|
|
|
|
|
default_password_generator = PasswordResetTokenGenerator()
|
|
|
|
self.assertNotEqual(
|
|
|
|
custom_password_generator.secret,
|
|
|
|
default_password_generator.secret,
|
|
|
|
)
|
|
|
|
self.assertEqual(default_password_generator.secret, settings.SECRET_KEY)
|
|
|
|
# Tokens created with a different secret don't validate.
|
|
|
|
tk_default = default_password_generator.make_token(user)
|
|
|
|
self.assertIs(custom_password_generator.check_token(user, tk_default), False)
|
|
|
|
self.assertIs(default_password_generator.check_token(user, tk_custom), False)
|
2021-04-19 07:58:34 +00:00
|
|
|
|
|
|
|
@override_settings(SECRET_KEY="")
|
|
|
|
def test_secret_lazy_validation(self):
|
|
|
|
default_token_generator = PasswordResetTokenGenerator()
|
|
|
|
msg = "The SECRET_KEY setting must not be empty."
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
default_token_generator.secret
|
2021-12-14 03:47:03 +00:00
|
|
|
|
|
|
|
def test_check_token_secret_fallbacks(self):
|
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
|
|
|
p1 = PasswordResetTokenGenerator()
|
|
|
|
p1.secret = "secret"
|
|
|
|
tk = p1.make_token(user)
|
|
|
|
p2 = PasswordResetTokenGenerator()
|
|
|
|
p2.secret = "newsecret"
|
|
|
|
p2.secret_fallbacks = ["secret"]
|
|
|
|
self.assertIs(p1.check_token(user, tk), True)
|
|
|
|
self.assertIs(p2.check_token(user, tk), True)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
SECRET_KEY="secret",
|
|
|
|
SECRET_KEY_FALLBACKS=["oldsecret"],
|
|
|
|
)
|
|
|
|
def test_check_token_secret_key_fallbacks(self):
|
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
|
|
|
p1 = PasswordResetTokenGenerator()
|
|
|
|
p1.secret = "oldsecret"
|
|
|
|
tk = p1.make_token(user)
|
|
|
|
p2 = PasswordResetTokenGenerator()
|
|
|
|
self.assertIs(p2.check_token(user, tk), True)
|
|
|
|
|
|
|
|
@override_settings(
|
|
|
|
SECRET_KEY="secret",
|
|
|
|
SECRET_KEY_FALLBACKS=["oldsecret"],
|
|
|
|
)
|
|
|
|
def test_check_token_secret_key_fallbacks_override(self):
|
|
|
|
user = User.objects.create_user("tokentestuser", "test2@example.com", "testpw")
|
|
|
|
p1 = PasswordResetTokenGenerator()
|
|
|
|
p1.secret = "oldsecret"
|
|
|
|
tk = p1.make_token(user)
|
|
|
|
p2 = PasswordResetTokenGenerator()
|
|
|
|
p2.secret_fallbacks = []
|
|
|
|
self.assertIs(p2.check_token(user, tk), False)
|