Skip to content

Commit

Permalink
Refs #32800 -- Renamed _sanitize_token() to _check_token_format().
Browse files Browse the repository at this point in the history
  • Loading branch information
cjerdonek authored and felixxm committed Nov 29, 2021
1 parent 5d80843 commit 3ff7f6c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
6 changes: 3 additions & 3 deletions django/middleware/csrf.py
Expand Up @@ -128,7 +128,7 @@ def __init__(self, reason):
self.reason = reason


def _sanitize_token(token):
def _check_token_format(token):
"""
Raise an InvalidTokenFormat error if the token has an invalid length or
characters that aren't allowed. The token argument can be a CSRF cookie
Expand Down Expand Up @@ -239,7 +239,7 @@ def _get_secret(self, request):
csrf_secret = None
else:
# This can raise InvalidTokenFormat.
_sanitize_token(csrf_secret)
_check_token_format(csrf_secret)
if csrf_secret is None:
return None
# Django versions before 4.0 masked the secret before storing.
Expand Down Expand Up @@ -386,7 +386,7 @@ def _check_token(self, request):
token_source = 'POST'

try:
_sanitize_token(request_csrf_token)
_check_token_format(request_csrf_token)
except InvalidTokenFormat as exc:
reason = self._bad_token_message(exc.reason, token_source)
raise RejectRequest(reason)
Expand Down
10 changes: 5 additions & 5 deletions tests/csrf_tests/tests.py
Expand Up @@ -8,7 +8,7 @@
CSRF_ALLOWED_CHARS, CSRF_SECRET_LENGTH, CSRF_SESSION_KEY,
CSRF_TOKEN_LENGTH, REASON_BAD_ORIGIN, REASON_CSRF_TOKEN_MISSING,
REASON_NO_CSRF_COOKIE, CsrfViewMiddleware, InvalidTokenFormat,
RejectRequest, _does_token_match, _mask_cipher_secret, _sanitize_token,
RejectRequest, _check_token_format, _does_token_match, _mask_cipher_secret,
_unmask_cipher_token, get_token, rotate_token,
)
from django.test import SimpleTestCase, override_settings
Expand Down Expand Up @@ -106,7 +106,7 @@ def test_rotate_token(self):
self.assertNotEqual(cookie, TEST_SECRET)
self.assertIs(request.META['CSRF_COOKIE_NEEDS_UPDATE'], True)

def test_sanitize_token_valid(self):
def test_check_token_format_valid(self):
cases = [
# A token of length CSRF_SECRET_LENGTH.
TEST_SECRET,
Expand All @@ -116,18 +116,18 @@ def test_sanitize_token_valid(self):
]
for token in cases:
with self.subTest(token=token):
actual = _sanitize_token(token)
actual = _check_token_format(token)
self.assertIsNone(actual)

def test_sanitize_token_invalid(self):
def test_check_token_format_invalid(self):
cases = [
(64 * '*', 'has invalid characters'),
(16 * 'a', 'has incorrect length'),
]
for token, expected_message in cases:
with self.subTest(token=token):
with self.assertRaisesMessage(InvalidTokenFormat, expected_message):
_sanitize_token(token)
_check_token_format(token)

def test_does_token_match(self):
cases = [
Expand Down

0 comments on commit 3ff7f6c

Please sign in to comment.