diff options
| author | Luke Plant <L.Plant.98@cantab.net> | 2010-09-10 22:56:56 +0000 |
|---|---|---|
| committer | Luke Plant <L.Plant.98@cantab.net> | 2010-09-10 22:56:56 +0000 |
| commit | 364583b8947dc7073445e3a8449bf021a80ca593 (patch) | |
| tree | e89e19c180bdda44ad0315ea41b3385e52ccc341 /django | |
| parent | fd1e4b81d9fb58010166135622c9be415c3b1e31 (diff) | |
Fixed #14235 - UnicodeDecodeError in CSRF middleware
Thanks to jbg for the report.
This changeset essentially backs out [13698] in favour of a method that
sanitizes the token rather than escaping it.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@13732 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/middleware/csrf.py | 23 | ||||
| -rw-r--r-- | django/template/defaulttags.py | 3 |
2 files changed, 19 insertions, 7 deletions
diff --git a/django/middleware/csrf.py b/django/middleware/csrf.py index ca2ec8aa38..5d3a871adb 100644 --- a/django/middleware/csrf.py +++ b/django/middleware/csrf.py @@ -13,7 +13,6 @@ from django.conf import settings from django.core.urlresolvers import get_callable from django.utils.cache import patch_vary_headers from django.utils.hashcompat import md5_constructor -from django.utils.html import escape from django.utils.safestring import mark_safe _POST_FORM_RE = \ @@ -53,8 +52,8 @@ def _make_legacy_session_token(session_id): def get_token(request): """ - Returns the the CSRF token required for a POST form. No assumptions should - be made about what characters might be in the CSRF token. + Returns the the CSRF token required for a POST form. The token is an + alphanumeric value. A side effect of calling this function is to make the the csrf_protect decorator and the CsrfViewMiddleware add a CSRF cookie and a 'Vary: Cookie' @@ -65,6 +64,17 @@ def get_token(request): return request.META.get("CSRF_COOKIE", None) +def _sanitize_token(token): + # Allow only alphanum, and ensure we return a 'str' for the sake of the post + # processing middleware. + token = re.sub('[^a-zA-Z0-9]', '', str(token.decode('ascii', 'ignore'))) + if token == "": + # In case the cookie has been truncated to nothing at some point. + return _get_new_csrf_key() + else: + return token + + class CsrfViewMiddleware(object): """ Middleware that requires a present and correct csrfmiddlewaretoken @@ -90,7 +100,10 @@ class CsrfViewMiddleware(object): # request, so it's available to the view. We'll store it in a cookie when # we reach the response. try: - request.META["CSRF_COOKIE"] = request.COOKIES[settings.CSRF_COOKIE_NAME] + # In case of cookies from untrusted sources, we strip anything + # dangerous at this point, so that the cookie + token will have the + # same, sanitized value. + request.META["CSRF_COOKIE"] = _sanitize_token(request.COOKIES[settings.CSRF_COOKIE_NAME]) cookie_is_new = False except KeyError: # No cookie, so create one. This will be sent with the next @@ -249,7 +262,7 @@ class CsrfResponseMiddleware(object): """Returns the matched <form> tag plus the added <input> element""" return mark_safe(match.group() + "<div style='display:none;'>" + \ "<input type='hidden' " + idattributes.next() + \ - " name='csrfmiddlewaretoken' value='" + escape(csrf_token) + \ + " name='csrfmiddlewaretoken' value='" + csrf_token + \ "' /></div>") # Modify any POST forms diff --git a/django/template/defaulttags.py b/django/template/defaulttags.py index 0914b1c3b1..1b07413530 100644 --- a/django/template/defaulttags.py +++ b/django/template/defaulttags.py @@ -9,7 +9,6 @@ from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG from django.template import get_library, Library, InvalidTemplateLibrary from django.template.smartif import IfParser, Literal from django.conf import settings -from django.utils.html import escape from django.utils.encoding import smart_str, smart_unicode from django.utils.safestring import mark_safe @@ -43,7 +42,7 @@ class CsrfTokenNode(Node): if csrf_token == 'NOTPROVIDED': return mark_safe(u"") else: - return mark_safe(u"<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='%s' /></div>" % escape(csrf_token)) + return mark_safe(u"<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='%s' /></div>" % csrf_token) else: # It's very probable that the token is missing because of # misconfiguration, so we raise a warning |
