Skip to content

Commit aeb8c38

Browse files
MattBlack85timgraham
authored andcommitted
Fixed #29206 -- Fixed PasswordResetConfirmView crash when the URL contains a non-UUID where one is expected.
1 parent b60e5fd commit aeb8c38

File tree

4 files changed

+16
-1
lines changed

4 files changed

+16
-1
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,7 @@ answer newbie questions, and generally made Django that much better:
549549
Matt Riggott
550550
Matt Robenolt <[email protected]>
551551
Mattia Larentis <[email protected]>
552+
Mattia Procopio <[email protected]>
552553
Mattias Loverot <[email protected]>
553554
554555
Max Burstein <http://maxburstein.com>

django/contrib/auth/views.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
)
1313
from django.contrib.auth.tokens import default_token_generator
1414
from django.contrib.sites.shortcuts import get_current_site
15+
from django.core.exceptions import ValidationError
1516
from django.http import HttpResponseRedirect, QueryDict
1617
from django.shortcuts import resolve_url
1718
from django.urls import reverse_lazy
@@ -285,7 +286,7 @@ def get_user(self, uidb64):
285286
# urlsafe_base64_decode() decodes to bytestring
286287
uid = urlsafe_base64_decode(uidb64).decode()
287288
user = UserModel._default_manager.get(pk=uid)
288-
except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist):
289+
except (TypeError, ValueError, OverflowError, UserModel.DoesNotExist, ValidationError):
289290
user = None
290291
return user
291292

docs/releases/2.0.4.txt

+4
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,7 @@ Bugfixes
1717

1818
* Corrected admin's autocomplete widget to add a space after custom classes
1919
(:ticket:`29221`).
20+
21+
* Fixed ``PasswordResetConfirmView`` crash when using a user model with a
22+
``UUIDField`` primary key and the reset URL contains an encoded primary key
23+
value that decodes to an invalid UUID (:ticket:`29206`).

tests/auth_tests/test_views.py

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from django.test import Client, TestCase, override_settings
2929
from django.test.utils import patch_logger
3030
from django.urls import NoReverseMatch, reverse, reverse_lazy
31+
from django.utils.http import urlsafe_base64_encode
3132
from django.utils.translation import LANGUAGE_SESSION_KEY
3233

3334
from .client import PasswordResetConfirmClient
@@ -437,6 +438,14 @@ def _test_confirm_start(self):
437438
)
438439
return super()._test_confirm_start()
439440

441+
def test_confirm_invalid_uuid(self):
442+
"""A uidb64 that decodes to a non-UUID doesn't crash."""
443+
_, path = self._test_confirm_start()
444+
invalid_uidb64 = urlsafe_base64_encode('INVALID_UUID'.encode()).decode()
445+
first, _uuidb64_, second = path.strip('/').split('/')
446+
response = self.client.get('/' + '/'.join((first, invalid_uidb64, second)) + '/')
447+
self.assertContains(response, 'The password reset link was invalid')
448+
440449

441450
class ChangePasswordTest(AuthViewsTestCase):
442451

0 commit comments

Comments
 (0)