Skip to content

Commit 231de68

Browse files
cjerdonekcarltongibson
authored andcommitted
Refs #32800 -- Added _add_new_csrf_cookie() helper function.
This centralizes the logic to use when setting a new cookie. It also eliminates the need for the _get_new_csrf_token() function, which is now removed.
1 parent be1fd66 commit 231de68

File tree

1 file changed

+21
-20
lines changed

1 file changed

+21
-20
lines changed

django/middleware/csrf.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,14 @@ def _unmask_cipher_token(token):
7979
return ''.join(chars[x - y] for x, y in pairs) # Note negative values are ok
8080

8181

82-
def _get_new_csrf_token():
83-
return _mask_cipher_secret(_get_new_csrf_string())
82+
def _add_new_csrf_cookie(request):
83+
"""Generate a new random CSRF_COOKIE value, and add it to request.META."""
84+
csrf_secret = _get_new_csrf_string()
85+
request.META.update({
86+
'CSRF_COOKIE': _mask_cipher_secret(csrf_secret),
87+
'CSRF_COOKIE_NEEDS_UPDATE': True,
88+
})
89+
return csrf_secret
8490

8591

8692
def get_token(request):
@@ -93,15 +99,14 @@ def get_token(request):
9399
header to the outgoing response. For this reason, you may need to use this
94100
function lazily, as is done by the csrf context processor.
95101
"""
96-
if "CSRF_COOKIE" not in request.META:
97-
csrf_secret = _get_new_csrf_string()
98-
request.META["CSRF_COOKIE"] = _mask_cipher_secret(csrf_secret)
99-
else:
102+
if 'CSRF_COOKIE' in request.META:
100103
csrf_secret = _unmask_cipher_token(request.META["CSRF_COOKIE"])
101-
# Since the cookie is being used, flag to send the cookie in
102-
# process_response() (even if the client already has it) in order to renew
103-
# the expiry timer.
104-
request.META['CSRF_COOKIE_NEEDS_UPDATE'] = True
104+
# Since the cookie is being used, flag to send the cookie in
105+
# process_response() (even if the client already has it) in order to
106+
# renew the expiry timer.
107+
request.META['CSRF_COOKIE_NEEDS_UPDATE'] = True
108+
else:
109+
csrf_secret = _add_new_csrf_cookie(request)
105110
return _mask_cipher_secret(csrf_secret)
106111

107112

@@ -110,10 +115,7 @@ def rotate_token(request):
110115
Change the CSRF token in use for a request - should be done on login
111116
for security purposes.
112117
"""
113-
request.META.update({
114-
'CSRF_COOKIE': _get_new_csrf_token(),
115-
'CSRF_COOKIE_NEEDS_UPDATE': True,
116-
})
118+
_add_new_csrf_cookie(request)
117119

118120

119121
class InvalidTokenFormat(Exception):
@@ -377,12 +379,11 @@ def process_request(self, request):
377379
try:
378380
csrf_token = self._get_token(request)
379381
except InvalidTokenFormat:
380-
csrf_token = _get_new_csrf_token()
381-
request.META["CSRF_COOKIE_NEEDS_UPDATE"] = True
382-
383-
if csrf_token is not None:
384-
# Use same token next time.
385-
request.META['CSRF_COOKIE'] = csrf_token
382+
_add_new_csrf_cookie(request)
383+
else:
384+
if csrf_token is not None:
385+
# Use same token next time.
386+
request.META['CSRF_COOKIE'] = csrf_token
386387

387388
def process_view(self, request, callback, callback_args, callback_kwargs):
388389
if getattr(request, 'csrf_processing_done', False):

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy