Skip to content

Commit

Permalink
Misc clarifications in csrf middleware comments
Browse files Browse the repository at this point in the history
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11673 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
spookylukey committed Oct 27, 2009
1 parent 43c2ed0 commit 905dba3
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions django/middleware/csrf.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ def accept():
request.META["CSRF_COOKIE"] = request.COOKIES[settings.CSRF_COOKIE_NAME] request.META["CSRF_COOKIE"] = request.COOKIES[settings.CSRF_COOKIE_NAME]
cookie_is_new = False cookie_is_new = False
except KeyError: except KeyError:
# No cookie, so create one. # No cookie, so create one. This will be sent with the next
# response.
request.META["CSRF_COOKIE"] = _get_new_csrf_key() request.META["CSRF_COOKIE"] = _get_new_csrf_key()
# Set a flag to allow us to fall back and allow the session id in
# place of a CSRF cookie for this request only.
cookie_is_new = True cookie_is_new = True


if request.method == 'POST': if request.method == 'POST':
Expand Down Expand Up @@ -133,10 +136,10 @@ def accept():
return reject("Referer checking failed - %s does not match %s." % return reject("Referer checking failed - %s does not match %s." %
(referer, good_referer)) (referer, good_referer))


# If the user didn't already have a CSRF key, then accept the # If the user didn't already have a CSRF cookie, then fall back to
# session key for the middleware token, so CSRF protection isn't lost # the Django 1.1 method (hash of session ID), so a request is not
# for the period between upgrading to CSRF cookes to the first time # rejected if the form was sent to the user before upgrading to the
# each user comes back to the site to receive one. # Django 1.2 method (session independent nonce)
if cookie_is_new: if cookie_is_new:
try: try:
session_id = request.COOKIES[settings.SESSION_COOKIE_NAME] session_id = request.COOKIES[settings.SESSION_COOKIE_NAME]
Expand Down Expand Up @@ -226,7 +229,7 @@ def add_csrf_field(match):
patch_vary_headers(response, ('Cookie',)) patch_vary_headers(response, ('Cookie',))


# Since the content has been modified, any Etag will now be # Since the content has been modified, any Etag will now be
# incorrect. We could recalculate, but only if we assume that # incorrect. We could recalculate, but only if we assume that
# the Etag was set by CommonMiddleware. The safest thing is just # the Etag was set by CommonMiddleware. The safest thing is just
# to delete. See bug #9163 # to delete. See bug #9163
del response['ETag'] del response['ETag']
Expand Down

0 comments on commit 905dba3

Please sign in to comment.