Skip to content

Commit 9e3b327

Browse files
committed
Patch CSRF-protection system to deal with reported security issue. Announcement and details to follow.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13698 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent ef4b29a commit 9e3b327

File tree

3 files changed

+11
-5
lines changed

3 files changed

+11
-5
lines changed

django/middleware/csrf.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.core.urlresolvers import get_callable
1414
from django.utils.cache import patch_vary_headers
1515
from django.utils.hashcompat import md5_constructor
16+
from django.utils.html import escape
1617
from django.utils.safestring import mark_safe
1718

1819
_POST_FORM_RE = \
@@ -52,7 +53,8 @@ def _make_legacy_session_token(session_id):
5253

5354
def get_token(request):
5455
"""
55-
Returns the the CSRF token required for a POST form.
56+
Returns the the CSRF token required for a POST form. No assumptions should
57+
be made about what characters might be in the CSRF token.
5658
5759
A side effect of calling this function is to make the the csrf_protect
5860
decorator and the CsrfViewMiddleware add a CSRF cookie and a 'Vary: Cookie'
@@ -247,7 +249,7 @@ def add_csrf_field(match):
247249
"""Returns the matched <form> tag plus the added <input> element"""
248250
return mark_safe(match.group() + "<div style='display:none;'>" + \
249251
"<input type='hidden' " + idattributes.next() + \
250-
" name='csrfmiddlewaretoken' value='" + csrf_token + \
252+
" name='csrfmiddlewaretoken' value='" + escape(csrf_token) + \
251253
"' /></div>")
252254

253255
# Modify any POST forms

django/template/defaulttags.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from django.template import get_library, Library, InvalidTemplateLibrary
1010
from django.template.smartif import IfParser, Literal
1111
from django.conf import settings
12+
from django.utils.html import escape
1213
from django.utils.encoding import smart_str, smart_unicode
1314
from django.utils.safestring import mark_safe
1415

@@ -42,7 +43,7 @@ def render(self, context):
4243
if csrf_token == 'NOTPROVIDED':
4344
return mark_safe(u"")
4445
else:
45-
return mark_safe(u"<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='%s' /></div>" % (csrf_token))
46+
return mark_safe(u"<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='%s' /></div>" % escape(csrf_token))
4647
else:
4748
# It's very probable that the token is missing because of
4849
# misconfiguration, so we raise a warning

tests/regressiontests/csrf_tests/tests.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.views.decorators.csrf import csrf_exempt, csrf_view_exempt
77
from django.core.context_processors import csrf
88
from django.contrib.sessions.middleware import SessionMiddleware
9+
from django.utils.html import escape
910
from django.utils.importlib import import_module
1011
from django.conf import settings
1112
from django.template import RequestContext, Template
@@ -56,7 +57,9 @@ def is_secure(self):
5657
return getattr(self, '_is_secure', False)
5758

5859
class CsrfMiddlewareTest(TestCase):
59-
_csrf_id = "1"
60+
# The csrf token is potentially from an untrusted source, so could have
61+
# characters that need escaping
62+
_csrf_id = "<1>"
6063

6164
# This is a valid session token for this ID and secret key. This was generated using
6265
# the old code that we're to be backwards-compatible with. Don't use the CSRF code
@@ -101,7 +104,7 @@ def _get_POST_session_request_no_token(self):
101104
return req
102105

103106
def _check_token_present(self, response, csrf_id=None):
104-
self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % (csrf_id or self._csrf_id))
107+
self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % escape(csrf_id or self._csrf_id))
105108

106109
# Check the post processing and outgoing cookie
107110
def test_process_response_no_csrf_cookie(self):

0 commit comments

Comments
 (0)