Skip to content

Commit 2d4aaf6

Browse files
Merge pull request #54 from stan-sack/always_return_200
Implement DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE setting
2 parents f352046 + 4233209 commit 2d4aaf6

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ The following settings can be set in Djangos ``settings.py`` file:
6565

6666
**Please note**: expired tokens are automatically cleared based on this setting in every call of ``ResetPasswordRequestToken.post``.
6767

68+
* `DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE` - will cause a 200 to be returned on `POST ${API_URL}/reset_password/`
69+
even if the user doesn't exist in the databse (Default: False)
70+
6871
* `DJANGO_REST_MULTITOKENAUTH_REQUIRE_USABLE_PASSWORD` - allows password reset for a user that does not
6972
[have a usable password](https://docs.djangoproject.com/en/2.2/ref/contrib/auth/#django.contrib.auth.models.User.has_usable_password) (Default: True)
7073

django_rest_passwordreset/views.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ def post(self, request, *args, **kwargs):
118118
active_user_found = True
119119

120120
# No active user found, raise a validation error
121-
if not active_user_found:
121+
# but not if DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE == True
122+
if not active_user_found and not getattr(settings, 'DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE', False):
122123
raise exceptions.ValidationError({
123124
'email': [_(
124125
"There is no active user associated with this e-mail address or the password can not be changed")],

tests/test/test_auth_test_case.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,14 @@ def test_signals(self,
227227
self.assertTrue(mock_post_password_reset.called)
228228
self.assertTrue(mock_pre_password_reset.called)
229229

230+
@override_settings(DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE=True)
231+
def test_try_reset_password_email_does_not_exist_no_leakage_enabled(self):
232+
"""
233+
Tests requesting a token for an email that does not exist when
234+
DJANGO_REST_PASSWORDRESET_NO_INFORMATION_LEAKAGE == True
235+
"""
236+
response = self.rest_do_request_reset_token(email="foobar@doesnotexist.com")
237+
self.assertEqual(response.status_code, status.HTTP_200_OK)
230238
def test_user_without_password(self):
231239
""" Tests requesting a token for an email without a password doesn't work"""
232240
response = self.rest_do_request_reset_token(email="user4@mail.com")

0 commit comments

Comments
 (0)