Ticket #9977: csrf_template_tag_r11365_1.diff
File csrf_template_tag_r11365_1.diff, 69.6 KB (added by , 15 years ago) |
---|
-
AUTHORS
diff -r 61e8647f8f4f AUTHORS
a b 468 468 Gasper Zejn <zejn@kiberpipa.org> 469 469 Jarek Zgoda <jarek.zgoda@gmail.com> 470 470 Cheng Zhang 471 Glenn 472 bthomas 471 473 472 474 A big THANK YOU goes to: 473 475 -
django/conf/global_settings.py
diff -r 61e8647f8f4f django/conf/global_settings.py
a b 165 165 'django.core.context_processors.debug', 166 166 'django.core.context_processors.i18n', 167 167 'django.core.context_processors.media', 168 'django.contrib.csrf.context_processors.csrf', 168 169 # 'django.core.context_processors.request', 169 170 ) 170 171 … … 300 301 MIDDLEWARE_CLASSES = ( 301 302 'django.middleware.common.CommonMiddleware', 302 303 'django.contrib.sessions.middleware.SessionMiddleware', 304 'django.contrib.csrf.middleware.CsrfViewMiddleware', 303 305 'django.contrib.auth.middleware.AuthenticationMiddleware', 304 306 # 'django.middleware.http.ConditionalGetMiddleware', 305 307 # 'django.middleware.gzip.GZipMiddleware', … … 374 376 # The number of days a password reset link is valid for 375 377 PASSWORD_RESET_TIMEOUT_DAYS = 3 376 378 379 ######## 380 # CSRF # 381 ######## 382 383 # Dotted path to callable to be used as view when a request is 384 # rejected by the CSRF middleware. 385 CSRF_FAILURE_VIEW = 'django.contrib.csrf.views.csrf_failure' 386 387 # Name and domain for CSRF cookie. 388 CSRF_COOKIE_NAME = 'csrf_cookie_name' 389 CSRF_COOKIE_DOMAIN = None 390 377 391 ########### 378 392 # TESTING # 379 393 ########### -
django/conf/project_template/settings.py
diff -r 61e8647f8f4f django/conf/project_template/settings.py
a b 60 60 MIDDLEWARE_CLASSES = ( 61 61 'django.middleware.common.CommonMiddleware', 62 62 'django.contrib.sessions.middleware.SessionMiddleware', 63 'django.contrib.csrf.middleware.CsrfViewMiddleware', 63 64 'django.contrib.auth.middleware.AuthenticationMiddleware', 64 65 ) 65 66 … … 74 75 INSTALLED_APPS = ( 75 76 'django.contrib.auth', 76 77 'django.contrib.contenttypes', 78 'django.contrib.csrf', 77 79 'django.contrib.sessions', 78 80 'django.contrib.sites', 79 81 ) -
django/contrib/admin/sites.py
diff -r 61e8647f8f4f django/contrib/admin/sites.py
a b 3 3 from django.contrib.admin import ModelAdmin 4 4 from django.contrib.admin import actions 5 5 from django.contrib.auth import authenticate, login 6 from django.contrib.csrf.middleware import csrf_response_exempt 6 7 from django.db.models.base import ModelBase 7 8 from django.core.exceptions import ImproperlyConfigured 8 9 from django.core.urlresolvers import reverse … … 154 155 raise ImproperlyConfigured("Put 'django.contrib.admin' in your INSTALLED_APPS setting in order to use the admin application.") 155 156 if not ContentType._meta.installed: 156 157 raise ImproperlyConfigured("Put 'django.contrib.contenttypes' in your INSTALLED_APPS setting in order to use the admin application.") 158 if 'django.contrib.csrf' not in settings.INSTALLED_APPS: 159 raise ImproperlyConfigured("Put 'django.contrib.csrf' in your INSTALLED_APPS setting in order to use the admin application.") 157 160 if 'django.core.context_processors.auth' not in settings.TEMPLATE_CONTEXT_PROCESSORS: 158 161 raise ImproperlyConfigured("Put 'django.core.context_processors.auth' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.") 162 if 'django.contrib.csrf.context_processors.csrf' not in settings.TEMPLATE_CONTEXT_PROCESSORS: 163 raise ImproperlyConfigured("Put 'django.contrib.csrf.context_processors.csrf' in your TEMPLATE_CONTEXT_PROCESSORS setting in order to use the admin application.") 159 164 160 165 def admin_view(self, view, cacheable=False): 161 166 """ … … 186 191 return view(request, *args, **kwargs) 187 192 if not cacheable: 188 193 inner = never_cache(inner) 194 inner = csrf_response_exempt(inner) 189 195 return update_wrapper(inner, view) 190 196 191 197 def get_urls(self): -
django/contrib/admin/templates/admin/auth/user/change_password.html
diff -r 61e8647f8f4f django/contrib/admin/templates/admin/auth/user/change_password.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n admin_modify adminmedia %}2 {% load i18n admin_modify adminmedia csrf %} 3 3 {% block extrahead %}{{ block.super }} 4 4 <script type="text/javascript" src="../../../../jsi18n/"></script> 5 5 {% endblock %} … … 15 15 </div> 16 16 {% endif %}{% endblock %} 17 17 {% block content %}<div id="content-main"> 18 <form action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% block form_top %}{% endblock %}18 <form action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %} 19 19 <div> 20 20 {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %} 21 21 {% if form.errors %} -
django/contrib/admin/templates/admin/change_form.html
diff -r 61e8647f8f4f django/contrib/admin/templates/admin/change_form.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n admin_modify adminmedia %}2 {% load i18n admin_modify adminmedia csrf %} 3 3 4 4 {% block extrahead %}{{ block.super }} 5 5 <script type="text/javascript" src="../../../jsi18n/"></script> … … 29 29 </ul> 30 30 {% endif %}{% endif %} 31 31 {% endblock %} 32 <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% block form_top %}{% endblock %}32 <form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="{{ form_url }}" method="post" id="{{ opts.module_name }}_form">{% csrf_token %}{% block form_top %}{% endblock %} 33 33 <div> 34 34 {% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %} 35 35 {% if save_on_top %}{% submit_row %}{% endif %} -
django/contrib/admin/templates/admin/change_list.html
diff -r 61e8647f8f4f django/contrib/admin/templates/admin/change_list.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load adminmedia admin_list i18n %}2 {% load adminmedia admin_list i18n csrf %} 3 3 4 4 {% block extrastyle %} 5 5 {{ block.super }} … … 68 68 {% endif %} 69 69 {% endblock %} 70 70 71 <form action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}> 71 <form action="" method="post"{% if cl.formset.is_multipart %} enctype="multipart/form-data"{% endif %}>{% csrf_token %} 72 72 {% if cl.formset %} 73 73 {{ cl.formset.management_form }} 74 74 {% endif %} -
django/contrib/admin/templates/admin/delete_confirmation.html
diff -r 61e8647f8f4f django/contrib/admin/templates/admin/delete_confirmation.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block breadcrumbs %} 5 5 <div class="breadcrumbs"> … … 22 22 {% else %} 23 23 <p>{% blocktrans with object as escaped_object %}Are you sure you want to delete the {{ object_name }} "{{ escaped_object }}"? All of the following related items will be deleted:{% endblocktrans %}</p> 24 24 <ul>{{ deleted_objects|unordered_list }}</ul> 25 <form action="" method="post"> 25 <form action="" method="post">{% csrf_token %} 26 26 <div> 27 27 <input type="hidden" name="post" value="yes" /> 28 28 <input type="submit" value="{% trans "Yes, I'm sure" %}" /> -
django/contrib/admin/templates/admin/delete_selected_confirmation.html
diff -r 61e8647f8f4f django/contrib/admin/templates/admin/delete_selected_confirmation.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block breadcrumbs %} 5 5 <div class="breadcrumbs"> … … 23 23 {% for deleteable_object in deletable_objects %} 24 24 <ul>{{ deleteable_object|unordered_list }}</ul> 25 25 {% endfor %} 26 <form action="" method="post"> 26 <form action="" method="post">{% csrf_token %} 27 27 <div> 28 28 {% for obj in queryset %} 29 29 <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk }}" /> … … 34 34 </div> 35 35 </form> 36 36 {% endif %} 37 {% endblock %} 38 No newline at end of file 37 {% endblock %} -
django/contrib/admin/templates/admin/login.html
diff -r 61e8647f8f4f django/contrib/admin/templates/admin/login.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block extrastyle %}{% load adminmedia %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% admin_media_prefix %}css/login.css" />{% endblock %} 5 5 … … 14 14 <p class="errornote">{{ error_message }}</p> 15 15 {% endif %} 16 16 <div id="content-main"> 17 <form action="{{ app_path }}" method="post" id="login-form"> 17 <form action="{{ app_path }}" method="post" id="login-form">{% csrf_token %} 18 18 <div class="form-row"> 19 19 <label for="id_username">{% trans 'Username:' %}</label> <input type="text" name="username" id="id_username" /> 20 20 </div> -
django/contrib/admin/templates/admin/template_validator.html
diff -r 61e8647f8f4f django/contrib/admin/templates/admin/template_validator.html
a b 1 1 {% extends "admin/base_site.html" %} 2 2 {% load csrf %} 3 3 {% block content %} 4 4 5 5 <div id="content-main"> 6 6 7 <form action="" method="post"> 7 <form action="" method="post">{% csrf_token %} 8 8 9 9 {% if form.errors %} 10 10 <p class="errornote">Your template had {{ form.errors|length }} error{{ form.errors|pluralize }}:</p> -
django/contrib/admin/templates/registration/password_change_form.html
diff -r 61e8647f8f4f django/contrib/admin/templates/registration/password_change_form.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 {% block userlinks %}{% url django-admindocs-docroot as docsroot %}{% if docsroot %}<a href="{{ docsroot }}">{% trans 'Documentation' %}</a> / {% endif %} {% trans 'Change password' %} / <a href="../logout/">{% trans 'Log out' %}</a>{% endblock %} 4 4 {% block breadcrumbs %}<div class="breadcrumbs"><a href="../">{% trans 'Home' %}</a> › {% trans 'Password change' %}</div>{% endblock %} 5 5 … … 11 11 12 12 <p>{% trans "Please enter your old password, for security's sake, and then enter your new password twice so we can verify you typed it in correctly." %}</p> 13 13 14 <form action="" method="post"> 14 <form action="" method="post">{% csrf_token %} 15 15 16 16 {{ form.old_password.errors }} 17 17 <p class="aligned wide"><label for="id_old_password">{% trans 'Old password:' %}</label>{{ form.old_password }}</p> -
django/contrib/admin/templates/registration/password_reset_confirm.html
diff -r 61e8647f8f4f django/contrib/admin/templates/registration/password_reset_confirm.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block breadcrumbs %}<div class="breadcrumbs"><a href="../">{% trans 'Home' %}</a> › {% trans 'Password reset confirmation' %}</div>{% endblock %} 5 5 … … 13 13 14 14 <p>{% trans "Please enter your new password twice so we can verify you typed it in correctly." %}</p> 15 15 16 <form action="" method="post"> 16 <form action="" method="post">{% csrf_token %} 17 17 {{ form.new_password1.errors }} 18 18 <p class="aligned wide"><label for="id_new_password1">{% trans 'New password:' %}</label>{{ form.new_password1 }}</p> 19 19 {{ form.new_password2.errors }} -
django/contrib/admin/templates/registration/password_reset_form.html
diff -r 61e8647f8f4f django/contrib/admin/templates/registration/password_reset_form.html
a b 1 1 {% extends "admin/base_site.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block breadcrumbs %}<div class="breadcrumbs"><a href="../">{% trans 'Home' %}</a> › {% trans 'Password reset' %}</div>{% endblock %} 5 5 … … 11 11 12 12 <p>{% trans "Forgotten your password? Enter your e-mail address below, and we'll e-mail instructions for setting a new one." %}</p> 13 13 14 <form action="" method="post"> 14 <form action="" method="post">{% csrf_token %} 15 15 {{ form.email.errors }} 16 16 <p><label for="id_email">{% trans 'E-mail address:' %}</label> {{ form.email }} <input type="submit" value="{% trans 'Reset my password' %}" /></p> 17 17 </form> -
django/contrib/comments/templates/comments/approve.html
diff -r 61e8647f8f4f django/contrib/comments/templates/comments/approve.html
a b 1 1 {% extends "comments/base.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block title %}{% trans "Approve a comment" %}{% endblock %} 5 5 6 6 {% block content %} 7 7 <h1>{% trans "Really make this comment public?" %}</h1> 8 8 <blockquote>{{ comment|linebreaks }}</blockquote> 9 <form action="." method="post"> 9 <form action="." method="post">{% csrf_token %} 10 10 {% if next %}<input type="hidden" name="next" value="{{ next }}" id="next" />{% endif %} 11 11 <p class="submit"> 12 12 <input type="submit" name="submit" value="{% trans "Approve" %}" /> or <a href="{{ comment.get_absolute_url }}">cancel</a> -
django/contrib/comments/templates/comments/delete.html
diff -r 61e8647f8f4f django/contrib/comments/templates/comments/delete.html
a b 1 1 {% extends "comments/base.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block title %}{% trans "Remove a comment" %}{% endblock %} 5 5 6 6 {% block content %} 7 7 <h1>{% trans "Really remove this comment?" %}</h1> 8 8 <blockquote>{{ comment|linebreaks }}</blockquote> 9 <form action="." method="post"> 9 <form action="." method="post">{% csrf_token %} 10 10 {% if next %}<input type="hidden" name="next" value="{{ next }}" id="next" />{% endif %} 11 11 <p class="submit"> 12 12 <input type="submit" name="submit" value="{% trans "Remove" %}" /> or <a href="{{ comment.get_absolute_url }}">cancel</a> -
django/contrib/comments/templates/comments/flag.html
diff -r 61e8647f8f4f django/contrib/comments/templates/comments/flag.html
a b 1 1 {% extends "comments/base.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block title %}{% trans "Flag this comment" %}{% endblock %} 5 5 6 6 {% block content %} 7 7 <h1>{% trans "Really flag this comment?" %}</h1> 8 8 <blockquote>{{ comment|linebreaks }}</blockquote> 9 <form action="." method="post"> 9 <form action="." method="post">{% csrf_token %} 10 10 {% if next %}<input type="hidden" name="next" value="{{ next }}" id="next" />{% endif %} 11 11 <p class="submit"> 12 12 <input type="submit" name="submit" value="{% trans "Flag" %}" /> or <a href="{{ comment.get_absolute_url }}">cancel</a> -
django/contrib/comments/templates/comments/form.html
diff -r 61e8647f8f4f django/contrib/comments/templates/comments/form.html
a b 1 {% load comments i18n %}2 <form action="{% comment_form_target %}" method="post"> 1 {% load comments i18n csrf %} 2 <form action="{% comment_form_target %}" method="post">{% csrf_token %} 3 3 {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %} 4 4 {% for field in form %} 5 5 {% if field.is_hidden %} -
django/contrib/comments/templates/comments/moderation_queue.html
diff -r 61e8647f8f4f django/contrib/comments/templates/comments/moderation_queue.html
a b 1 1 {% extends "admin/change_list.html" %} 2 {% load adminmedia i18n %}2 {% load adminmedia i18n csrf %} 3 3 4 4 {% block title %}{% trans "Comment moderation queue" %}{% endblock %} 5 5 … … 44 44 {% for comment in comments %} 45 45 <tr class="{% cycle 'row1' 'row2' %}"> 46 46 <td class="actions"> 47 <form action="{% url comments-approve comment.pk %}" method="post"> 47 <form action="{% url comments-approve comment.pk %}" method="post">{% csrf_token %} 48 48 <input type="hidden" name="next" value="{% url comments-moderation-queue %}" /> 49 49 <input class="approve submit" type="submit" name="submit" value="{% trans "Approve" %}" /> 50 50 </form> 51 <form action="{% url comments-delete comment.pk %}" method="post"> 51 <form action="{% url comments-delete comment.pk %}" method="post">{% csrf_token %} 52 52 <input type="hidden" name="next" value="{% url comments-moderation-queue %}" /> 53 53 <input class="remove submit" type="submit" name="submit" value="{% trans "Remove" %}" /> 54 54 </form> -
django/contrib/comments/templates/comments/preview.html
diff -r 61e8647f8f4f django/contrib/comments/templates/comments/preview.html
a b 1 1 {% extends "comments/base.html" %} 2 {% load i18n %}2 {% load i18n csrf %} 3 3 4 4 {% block title %}{% trans "Preview your comment" %}{% endblock %} 5 5 6 6 {% block content %} 7 7 {% load comments %} 8 <form action="{% comment_form_target %}" method="post"> 8 <form action="{% comment_form_target %}" method="post">{% csrf_token %} 9 9 {% if next %}<input type="hidden" name="next" value="{{ next }}" />{% endif %} 10 10 {% if form.errors %} 11 11 <h1>{% blocktrans count form.errors|length as counter %}Please correct the error below{% plural %}Please correct the errors below{% endblocktrans %}</h1> -
new file django/contrib/csrf/context_processors.py
diff -r 61e8647f8f4f django/contrib/csrf/context_processors.py
- + 1 from django.contrib.csrf.middleware import get_token 2 from django.utils.functional import lazy 3 4 def csrf(request): 5 """ 6 Context processor that provides a CSRF token, or the string 7 'NOTNEEDED' if it is not needed 8 """ 9 def _get_val(): 10 token = get_token(request) 11 if token is None: 12 # In order to be able to provide debugging info in the 13 # case of misconfiguration, we use a sentinel value 14 # instead of returning an empty dict. 15 return 'NOTNEEDED' 16 else: 17 return token 18 _get_val = lazy(_get_val, str) 19 20 return {'csrf_token': _get_val() } -
django/contrib/csrf/middleware.py
diff -r 61e8647f8f4f django/contrib/csrf/middleware.py
a b 5 5 against request forgeries from other sites. 6 6 """ 7 7 8 import itertools 8 9 import re 9 import itertools10 import random 10 11 try: 11 12 from functools import wraps 12 13 except ImportError: 13 14 from django.utils.functional import wraps # Python 2.3, 2.4 fallback. 14 15 15 16 from django.conf import settings 16 from django. http import HttpResponseForbidden17 from django.core.urlresolvers import get_callable 17 18 from django.utils.hashcompat import md5_constructor 18 19 from django.utils.safestring import mark_safe 19 20 20 _ERROR_MSG = mark_safe('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><body><h1>403 Forbidden</h1><p>Cross Site Request Forgery detected. Request aborted.</p></body></html>')21 22 21 _POST_FORM_RE = \ 23 22 re.compile(r'(<form\W[^>]*\bmethod\s*=\s*(\'|"|)POST(\'|"|)\b[^>]*>)', re.IGNORECASE) 24 23 25 24 _HTML_TYPES = ('text/html', 'application/xhtml+xml') 26 25 27 def _make_token(session_id): 26 # Use the system (hardware-based) random number generator if it exists. 27 if hasattr(random, 'SystemRandom'): 28 randrange = random.SystemRandom().randrange 29 else: 30 randrange = random.randrange 31 _MAX_CSRF_KEY = 18446744073709551616L # 2 << 63 32 33 def _get_failure_view(): 34 """ 35 Returns the view to be used for CSRF rejections 36 """ 37 return get_callable(settings.CSRF_FAILURE_VIEW) 38 39 def _get_new_csrf_key(): 40 return md5_constructor("%s%s" 41 % (randrange(0, _MAX_CSRF_KEY), settings.SECRET_KEY)).hexdigest() 42 43 def _make_legacy_session_token(session_id): 28 44 return md5_constructor(settings.SECRET_KEY + session_id).hexdigest() 29 45 46 def get_token(request): 47 """ 48 Returns the the CSRF token required for a POST form. 49 """ 50 request.META["CSRF_COOKIE_USED"] = True 51 return request.META.get("CSRF_COOKIE", None) 52 30 53 class CsrfViewMiddleware(object): 31 54 """ 32 55 Middleware that requires a present and correct csrfmiddlewaretoken 33 for POST requests that have an active session. 56 for POST requests that have a CSRF cookie, and sets an outgoing 57 CSRF cookie. 58 59 This middleware should be used in conjunction with the csrf_token template 60 tag. 34 61 """ 35 62 def process_view(self, request, callback, callback_args, callback_kwargs): 63 if getattr(callback, 'csrf_exempt', False): 64 return None 65 66 # If the user doesn't have a CSRF cookie, generate one and store it in the 67 # request, so it's available to the view. We'll store it in a cookie when 68 # we reach the response. 69 try: 70 request.META["CSRF_COOKIE"] = request.COOKIES[settings.CSRF_COOKIE_NAME] 71 cookie_is_new = False 72 except KeyError: 73 # No cookie, so create one. 74 request.META["CSRF_COOKIE"] = _get_new_csrf_key() 75 cookie_is_new = True 76 36 77 if request.method == 'POST': 37 if getattr(callback, 'csrf_exempt', False):38 return None39 40 78 if request.is_ajax(): 41 79 return None 42 80 43 try: 44 session_id = request.COOKIES[settings.SESSION_COOKIE_NAME] 45 except KeyError: 46 # No session, no check required 47 return None 81 # If the user didn't already have a CSRF key, then accept the 82 # session key for the middleware token, so CSRF protection isn't lost 83 # for the period between upgrading to CSRF cookes to the first time 84 # each user comes back to the site to receive one. 85 if cookie_is_new: 86 try: 87 session_id = request.COOKIES[settings.SESSION_COOKIE_NAME] 88 csrf_token = _make_legacy_session_token(session_id) 89 except KeyError: 90 # No CSRF cookie and no session cookie. For POST requests, 91 # we insist on a CSRF cookie, and in this was we can avoid 92 # all CSRF attacks, including login CSRF. 93 return _get_failure_view()(request) 94 else: 95 csrf_token = request.META["CSRF_COOKIE"] 48 96 49 csrf_token = _make_token(session_id)50 97 # check incoming token 51 try: 52 request_csrf_token = request.POST['csrfmiddlewaretoken'] 53 except KeyError: 54 return HttpResponseForbidden(_ERROR_MSG) 55 98 request_csrf_token = request.POST.get('csrfmiddlewaretoken', None) 56 99 if request_csrf_token != csrf_token: 57 return HttpResponseForbidden(_ERROR_MSG)100 return _get_failure_view()(request) 58 101 59 102 return None 60 103 104 def process_response(self, request, response): 105 # If CSRF_COOKIE is unset, then CsrfViewMiddleware.process_view was 106 # never called, probaby because a request middleware returned a response 107 # (for example, contrib.auth redirecting to a login page). 108 if request.META.get("CSRF_COOKIE") is None: 109 return response 110 111 if not request.META.get("CSRF_COOKIE_USED", False): 112 return response 113 114 # Set the CSRF cookie even if it's already set, so we renew the expiry timer. 115 response.set_cookie(settings.CSRF_COOKIE_NAME, 116 request.META["CSRF_COOKIE"], max_age = 60 * 60 * 24 * 7 * 52, 117 domain=settings.CSRF_COOKIE_DOMAIN) 118 119 return response 120 61 121 class CsrfResponseMiddleware(object): 62 122 """ 63 Middleware that post-processes a response to add a 64 csrfmiddlewaretoken if the response/request have an active 65 session. 123 Middleware that post-processes a response to add a csrfmiddlewaretoken. 124 125 It is recommended to use the csrf_token template tag instead of 126 this middleware. 66 127 """ 67 128 def process_response(self, request, response): 68 129 if getattr(response, 'csrf_exempt', False): 69 130 return response 70 131 71 csrf_token = None 72 try: 73 # This covers a corner case in which the outgoing response 74 # both contains a form and sets a session cookie. This 75 # really should not be needed, since it is best if views 76 # that create a new session (login pages) also do a 77 # redirect, as is done by all such view functions in 78 # Django. 79 cookie = response.cookies[settings.SESSION_COOKIE_NAME] 80 csrf_token = _make_token(cookie.value) 81 except KeyError: 82 # Normal case - look for existing session cookie 83 try: 84 session_id = request.COOKIES[settings.SESSION_COOKIE_NAME] 85 csrf_token = _make_token(session_id) 86 except KeyError: 87 # no incoming or outgoing cookie 88 pass 89 90 if csrf_token is not None and \ 91 response['Content-Type'].split(';')[0] in _HTML_TYPES: 132 if response['Content-Type'].split(';')[0] in _HTML_TYPES: 133 csrf_token = get_token(request) 134 # If csrf_token is None, we have no token for this request, which probably 135 # means that this is a response from a request middleware. 136 if csrf_token is None: 137 return response 92 138 93 139 # ensure we don't add the 'id' attribute twice (HTML validity) 94 140 idattributes = itertools.chain(("id='csrfmiddlewaretoken'",), 95 141 itertools.repeat('')) 96 142 def add_csrf_field(match): 97 143 """Returns the matched <form> tag plus the added <input> element""" 98 144 return mark_safe(match.group() + "<div style='display:none;'>" + \ … … 104 150 response.content = _POST_FORM_RE.sub(add_csrf_field, response.content) 105 151 return response 106 152 107 class CsrfMiddleware(CsrfViewMiddleware, CsrfResponseMiddleware): 108 """Django middleware that adds protection against Cross Site 153 class CsrfMiddleware(object): 154 """ 155 Django middleware that adds protection against Cross Site 109 156 Request Forgeries by adding hidden form fields to POST forms and 110 157 checking requests for the correct value. 111 158 112 In the list of middlewares, SessionMiddleware is required, and 113 must come after this middleware. CsrfMiddleWare must come after 114 compression middleware. 159 CsrfMiddleware uses two middleware, CsrfViewMiddleware and 160 CsrfResponseMiddleware, which can be used independently. It is recommended 161 to use only CsrfViewMiddleware and use the csrf_token template tag in 162 templates for inserting the token. 163 """ 164 # We can't just inherit from CsrfViewMiddleware and CsrfResponseMiddleware 165 # because both have process_response methods. 166 def __init__(self): 167 self.response_middleware = CsrfResponseMiddleware() 168 self.view_middleware = CsrfViewMiddleware() 115 169 116 If a session ID cookie is present, it is hashed with the 117 SECRET_KEY setting to create an authentication token. This token 118 is added to all outgoing POST forms and is expected on all 119 incoming POST requests that have a session ID cookie. 170 def process_response(self, request, resp): 171 # We must do the response post-processing first, because that calls 172 # get_token(), which triggers a flag saying that the CSRF cookie needs 173 # to be sent (done in CsrfViewMiddleware.process_response) 174 resp2 = self.response_middleware.process_response(request, resp) 175 return self.view_middleware.process_response(request, resp2) 120 176 121 If you are setting cookies directly, instead of using Django's 122 session framework, this middleware will not work. 123 124 CsrfMiddleWare is composed of two middleware, CsrfViewMiddleware 125 and CsrfResponseMiddleware which can be used independently. 126 """ 127 pass 177 def process_view(self, request, callback, callback_args, callback_kwargs): 178 return self.view_middleware.process_view(request, callback, callback_args, 179 callback_kwargs) 128 180 129 181 def csrf_response_exempt(view_func): 130 182 """ -
new file django/contrib/csrf/templatetags/__init__.py
diff -r 61e8647f8f4f django/contrib/csrf/templatetags/__init__.py
- + 1 -
new file django/contrib/csrf/templatetags/csrf.py
diff -r 61e8647f8f4f django/contrib/csrf/templatetags/csrf.py
- + 1 from django import template 2 from django.utils.safestring import mark_safe 3 4 register = template.Library() 5 6 class CsrfTokenNode(template.Node): 7 def render(self, context): 8 csrf_token = context.get('csrf_token', None) 9 if csrf_token: 10 if csrf_token == 'NOTNEEDED': 11 return mark_safe(u"") 12 else: 13 return mark_safe(u"<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='%s' /></div>" % (csrf_token)) 14 else: 15 # It's very probable that the token is missing because of 16 # misconfiguration, so we raise a warning 17 from django.conf import settings 18 if settings.DEBUG: 19 import warnings 20 warnings.warn("A {% csrf_token %} was used in a template, but the context did not provide the value. This is usually caused by not having 'django.contrib.csrf.context_processors.csrf' in TEMPLATE_CONTEXT_PROCESSORS.") 21 return u'' 22 23 def csrf_token(parser, token): 24 return CsrfTokenNode() 25 register.tag(csrf_token) -
django/contrib/csrf/tests.py
diff -r 61e8647f8f4f django/contrib/csrf/tests.py
a b 1 1 # -*- coding: utf-8 -*- 2 2 3 3 from django.test import TestCase 4 from django.http import HttpRequest, HttpResponse, HttpResponseForbidden 5 from django.contrib.csrf.middleware import CsrfMiddleware, _make_token, csrf_exempt 4 from django.http import HttpRequest, HttpResponse 5 from django.contrib.csrf.middleware import CsrfMiddleware, CsrfViewMiddleware, csrf_exempt 6 from django.contrib.csrf.context_processors import csrf 7 from django.contrib.sessions.middleware import SessionMiddleware 8 from django.utils.importlib import import_module 6 9 from django.conf import settings 10 from django.template import RequestContext, Template 7 11 8 12 # Response/views used for CsrfResponseMiddleware and CsrfViewMiddleware tests 9 13 def post_form_response(): 10 14 resp = HttpResponse(content=""" 11 15 <html><body><form method="POST"><input type="text" /></form></body></html> 12 16 """, mimetype="text/html") 13 17 return resp 14 18 15 def test_view(request): 19 def post_form_response_non_html(): 20 resp = post_form_response() 21 resp["Content-Type"] = "application/xml" 22 return resp 23 24 def post_form_view(request): 25 """A view that returns a POST form (without a token)""" 16 26 return post_form_response() 17 27 28 # Response/views used for template tag tests 29 def _token_template(): 30 return Template("{% load csrf %}{% csrf_token %}") 31 32 def _render_csrf_token_template(req): 33 context = RequestContext(req, processors=[csrf]) 34 template = _token_template() 35 return template.render(context) 36 37 def token_view(request): 38 """A view that uses {% csrf_token %}""" 39 return HttpResponse(_render_csrf_token_template(request)) 40 41 def non_token_view_using_request_processor(request): 42 """ 43 A view that doesn't use the token, but does use the csrf view processor. 44 """ 45 context = RequestContext(request, processors=[csrf]) 46 template = Template("{% load csrf %}") 47 return HttpResponse(template.render(context)) 48 18 49 class CsrfMiddlewareTest(TestCase): 50 _csrf_id = "1" 19 51 52 # This is a valid session token for this ID and secret key. This was generated using 53 # the old code that we're to be backwards-compatible with. Don't use the CSRF code 54 # to generate this hash, or we're merely testing the code against itself and not 55 # checking backwards-compatibility. This is also the output of (echo -n test1 | md5sum). 56 _session_token = "5a105e8b9d40e1329780d62ea2265d8a" 20 57 _session_id = "1" 58 _secret_key_for_session_test= "test" 21 59 22 def _get_GET_no_ session_request(self):60 def _get_GET_no_csrf_cookie_request(self): 23 61 return HttpRequest() 24 62 25 def _get_GET_session_request(self): 26 req = self._get_GET_no_session_request() 63 def _get_GET_csrf_cookie_request(self): 64 req = HttpRequest() 65 req.COOKIES[settings.CSRF_COOKIE_NAME] = self._csrf_id 66 return req 67 68 def _get_POST_csrf_cookie_request(self): 69 req = self._get_GET_csrf_cookie_request() 70 req.method = "POST" 71 return req 72 73 def _get_POST_no_csrf_cookie_request(self): 74 req = self._get_GET_no_csrf_cookie_request() 75 req.method = "POST" 76 return req 77 78 def _get_POST_request_with_token(self): 79 req = self._get_POST_csrf_cookie_request() 80 req.POST['csrfmiddlewaretoken'] = self._csrf_id 81 return req 82 83 def _get_POST_session_request_with_token(self): 84 req = self._get_POST_no_csrf_cookie_request() 85 req.COOKIES[settings.SESSION_COOKIE_NAME] = self._session_id 86 req.POST['csrfmiddlewaretoken'] = self._session_token 87 return req 88 89 def _get_POST_session_request_no_token(self): 90 req = self._get_POST_no_csrf_cookie_request() 27 91 req.COOKIES[settings.SESSION_COOKIE_NAME] = self._session_id 28 92 return req 29 93 30 def _get_POST_session_request(self): 31 req = self._get_GET_session_request() 32 req.method = "POST" 33 return req 94 def _check_token_present(self, response, csrf_id=None): 95 self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % (csrf_id or self._csrf_id)) 34 96 35 def _get_POST_no_session_request(self): 36 req = self._get_GET_no_session_request() 37 req.method = "POST" 38 return req 97 # Check the post processing and outgoing cookie 98 def test_process_response_no_csrf_cookie(self): 99 """ 100 When no prior CSRF cookie exists, check that the cookie is created and a 101 token is inserted. 102 """ 103 req = self._get_GET_no_csrf_cookie_request() 104 CsrfMiddleware().process_view(req, post_form_view, (), {}) 39 105 40 def _get_POST_session_request_with_token(self): 41 req = self._get_POST_session_request() 42 req.POST['csrfmiddlewaretoken'] = _make_token(self._session_id) 43 return req 44 45 def _get_post_form_response(self): 46 return post_form_response() 47 48 def _get_new_session_response(self): 49 resp = self._get_post_form_response() 50 resp.cookies[settings.SESSION_COOKIE_NAME] = self._session_id 51 return resp 52 53 def _check_token_present(self, response): 54 self.assertContains(response, "name='csrfmiddlewaretoken' value='%s'" % _make_token(self._session_id)) 55 56 def get_view(self): 57 return test_view 58 59 # Check the post processing 60 def test_process_response_no_session(self): 61 """ 62 Check the post-processor does nothing if no session active 63 """ 64 req = self._get_GET_no_session_request() 65 resp = self._get_post_form_response() 106 resp = post_form_response() 66 107 resp_content = resp.content # needed because process_response modifies resp 67 108 resp2 = CsrfMiddleware().process_response(req, resp) 68 self.assertEquals(resp_content, resp2.content)69 109 70 def test_process_response_existing_session(self): 110 csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False) 111 self.assertNotEqual(csrf_cookie, False) 112 self.assertNotEqual(resp_content, resp2.content) 113 self._check_token_present(resp2, csrf_cookie.value) 114 115 def test_process_response_no_csrf_cookie_view_only_get_token_used(self): 71 116 """ 72 Check that the token is inserted if there is an existing session 117 When no prior CSRF cookie exists, check that the cookie is created, even 118 if only CsrfViewMiddleware is used. 73 119 """ 74 req = self._get_GET_session_request() 75 resp = self._get_post_form_response() 120 # This is checking that CsrfViewMiddleware has the cookie setting 121 # code. Most of the other tests use CsrfMiddleware. 122 req = self._get_GET_no_csrf_cookie_request() 123 # token_view calls get_token() indirectly 124 CsrfViewMiddleware().process_view(req, token_view, (), {}) 125 resp = token_view(req) 126 resp2 = CsrfViewMiddleware().process_response(req, resp) 127 128 csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False) 129 self.assertNotEqual(csrf_cookie, False) 130 131 def test_process_response_get_token_not_used(self): 132 """ 133 Check that if get_token() is not called, the view middleware does not 134 add a cookie. 135 """ 136 # This is important to make pages cacheable. Pages which do call 137 # get_token(), assuming they use the token, are not cacheable because 138 # the token is specific to the user 139 req = self._get_GET_no_csrf_cookie_request() 140 # non_token_view_using_request_processor does not call get_token(), but 141 # does use the csrf request processor. By using this, we are testing 142 # that the view processor is properly lazy and doesn't call get_token() 143 # until needed. 144 CsrfViewMiddleware().process_view(req, non_token_view_using_request_processor, (), {}) 145 resp = non_token_view_using_request_processor(req) 146 resp2 = CsrfViewMiddleware().process_response(req, resp) 147 148 csrf_cookie = resp2.cookies.get(settings.CSRF_COOKIE_NAME, False) 149 self.assertEqual(csrf_cookie, False) 150 151 def test_process_response_existing_csrf_cookie(self): 152 """ 153 Check that the token is inserted when a prior CSRF cookie exists 154 """ 155 req = self._get_GET_csrf_cookie_request() 156 CsrfMiddleware().process_view(req, post_form_view, (), {}) 157 158 resp = post_form_response() 76 159 resp_content = resp.content # needed because process_response modifies resp 77 160 resp2 = CsrfMiddleware().process_response(req, resp) 78 161 self.assertNotEqual(resp_content, resp2.content) 79 162 self._check_token_present(resp2) 80 163 81 def test_process_response_n ew_session(self):164 def test_process_response_non_html(self): 82 165 """ 83 Check th at the token is inserted if there is a new session being started166 Check the the post-processor does nothing for content-types not in _HTML_TYPES. 84 167 """ 85 req = self._get_GET_no_session_request() # no session in request 86 resp = self._get_new_session_response() # but new session started 168 req = self._get_GET_no_csrf_cookie_request() 169 CsrfMiddleware().process_view(req, post_form_view, (), {}) 170 resp = post_form_response_non_html() 87 171 resp_content = resp.content # needed because process_response modifies resp 88 172 resp2 = CsrfMiddleware().process_response(req, resp) 89 self.assertNotEqual(resp_content, resp2.content) 90 self._check_token_present(resp2) 173 self.assertEquals(resp_content, resp2.content) 91 174 92 175 def test_process_response_exempt_view(self): 93 176 """ 94 177 Check that no post processing is done for an exempt view 95 178 """ 96 req = self._get_POST_ session_request()97 resp = csrf_exempt( self.get_view())(req)179 req = self._get_POST_csrf_cookie_request() 180 resp = csrf_exempt(post_form_view)(req) 98 181 resp_content = resp.content 99 182 resp2 = CsrfMiddleware().process_response(req, resp) 100 183 self.assertEquals(resp_content, resp2.content) 101 184 102 185 # Check the request processing 103 def test_process_request_no_session (self):186 def test_process_request_no_session_no_csrf_cookie(self): 104 187 """ 105 Check that if n o session is present, the middleware does nothing.106 t o the incoming request.188 Check that if neither a CSRF cookie nor a session cookie are present, 189 the middleware rejects the incoming request. This will stop login CSRF. 107 190 """ 108 req = self._get_POST_no_session_request() 109 req2 = CsrfMiddleware().process_view(req, self.get_view(), (), {}) 191 req = self._get_POST_no_csrf_cookie_request() 192 req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) 193 self.assertEquals(403, req2.status_code) 194 195 def test_process_request_csrf_cookie_no_token(self): 196 """ 197 Check that if a CSRF cookie is present but no token, the middleware 198 rejects the incoming request. 199 """ 200 req = self._get_POST_csrf_cookie_request() 201 req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) 202 self.assertEquals(403, req2.status_code) 203 204 def test_process_request_csrf_cookie_and_token(self): 205 """ 206 Check that if both a cookie and a token is present, the middleware lets it through. 207 """ 208 req = self._get_POST_request_with_token() 209 req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) 110 210 self.assertEquals(None, req2) 111 211 112 def test_process_request_session_ no_token(self):212 def test_process_request_session_cookie_no_csrf_cookie_token(self): 113 213 """ 114 Check that if a session is present but no token, we get a 'forbidden' 214 When no CSRF cookie exists, but the user has a session, check that a token 215 using the session cookie as a legacy CSRF cookie is accepted. 115 216 """ 116 req = self._get_POST_session_request() 117 req2 = CsrfMiddleware().process_view(req, self.get_view(), (), {}) 118 self.assertEquals(HttpResponseForbidden, req2.__class__) 217 orig_secret_key = settings.SECRET_KEY 218 settings.SECRET_KEY = self._secret_key_for_session_test 219 try: 220 req = self._get_POST_session_request_with_token() 221 req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) 222 self.assertEquals(None, req2) 223 finally: 224 settings.SECRET_KEY = orig_secret_key 119 225 120 def test_process_request_session_ and_token(self):226 def test_process_request_session_cookie_no_csrf_cookie_no_token(self): 121 227 """ 122 Check that if a session is present and a token, the middleware lets it through 228 Check that if a session cookie is present but no token and no CSRF cookie, 229 the request is rejected. 123 230 """ 124 req = self._get_POST_session_request_ with_token()125 req2 = CsrfMiddleware().process_view(req, self.get_view(), (), {})126 self.assertEquals( None, req2)231 req = self._get_POST_session_request_no_token() 232 req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) 233 self.assertEquals(403, req2.status_code) 127 234 128 def test_process_request_ session_no_token_exempt_view(self):235 def test_process_request_csrf_cookie_no_token_exempt_view(self): 129 236 """ 130 Check that if a sessionis present and no token, but the csrf_exempt237 Check that if a CSRF cookie is present and no token, but the csrf_exempt 131 238 decorator has been applied to the view, the middleware lets it through 132 239 """ 133 req = self._get_POST_ session_request()134 req2 = CsrfMiddleware().process_view(req, csrf_exempt( self.get_view()), (), {})240 req = self._get_POST_csrf_cookie_request() 241 req2 = CsrfMiddleware().process_view(req, csrf_exempt(post_form_view), (), {}) 135 242 self.assertEquals(None, req2) 136 243 137 244 def test_ajax_exemption(self): 138 245 """ 139 246 Check that AJAX requests are automatically exempted. 140 247 """ 141 req = self._get_POST_ session_request()248 req = self._get_POST_csrf_cookie_request() 142 249 req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' 143 req2 = CsrfMiddleware().process_view(req, self.get_view(), (), {})250 req2 = CsrfMiddleware().process_view(req, post_form_view, (), {}) 144 251 self.assertEquals(None, req2) 252 253 # Tests for the template tag method 254 def test_token_node_no_csrf_cookie(self): 255 """ 256 Check that CsrfTokenNode works when no CSRF cookie is set 257 """ 258 req = self._get_GET_no_csrf_cookie_request() 259 resp = token_view(req) 260 self.assertEquals(u"", resp.content) 261 262 def test_token_node_with_csrf_cookie(self): 263 """ 264 Check that CsrfTokenNode works when a CSRF cookie is set 265 """ 266 req = self._get_GET_csrf_cookie_request() 267 CsrfViewMiddleware().process_view(req, token_view, (), {}) 268 resp = token_view(req) 269 self._check_token_present(resp) 270 271 def test_token_node_with_new_csrf_cookie(self): 272 """ 273 Check that CsrfTokenNode works when a CSRF cookie is created by 274 the middleware (when one was not already present) 275 """ 276 req = self._get_GET_no_csrf_cookie_request() 277 CsrfViewMiddleware().process_view(req, token_view, (), {}) 278 resp = token_view(req) 279 resp2 = CsrfViewMiddleware().process_response(req, resp) 280 csrf_cookie = resp2.cookies[settings.CSRF_COOKIE_NAME] 281 self._check_token_present(resp, csrf_id=csrf_cookie.value) 282 283 def test_response_middleware_without_view_middleware(self): 284 """ 285 Check that CsrfResponseMiddleware finishes without error if the view middleware 286 has not been called, as is the case if a request middleware returns a response. 287 """ 288 req = self._get_GET_no_csrf_cookie_request() 289 resp = post_form_view(req) 290 CsrfMiddleware().process_response(req, resp) -
new file django/contrib/csrf/views.py
diff -r 61e8647f8f4f django/contrib/csrf/views.py
- + 1 from django.http import HttpResponseForbidden 2 from django.utils.safestring import mark_safe 3 4 _ERROR_MSG = mark_safe('<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"><body><h1>403 Forbidden</h1><p>CSRF verification failed. Request aborted.</p></body></html>') 5 6 def csrf_failure(request): 7 """ 8 Default view used when request fails CSRF protection 9 """ 10 return HttpResponseForbidden(_ERROR_MSG) -
django/contrib/formtools/templates/formtools/form.html
diff -r 61e8647f8f4f django/contrib/formtools/templates/formtools/form.html
a b 1 1 {% extends "base.html" %} 2 2 {% load csrf %} 3 3 {% block content %} 4 4 5 5 {% if form.errors %}<h1>Please correct the following errors</h1>{% else %}<h1>Submit</h1>{% endif %} 6 6 7 <form action="" method="post"> 7 <form action="" method="post">{% csrf_token %} 8 8 <table> 9 9 {{ form }} 10 10 </table> -
django/contrib/formtools/templates/formtools/preview.html
diff -r 61e8647f8f4f django/contrib/formtools/templates/formtools/preview.html
a b 1 1 {% extends "base.html" %} 2 2 {% load csrf %} 3 3 {% block content %} 4 4 5 5 <h1>Preview your submission</h1> … … 15 15 16 16 <p>Security hash: {{ hash_value }}</p> 17 17 18 <form action="" method="post"> 18 <form action="" method="post">{% csrf_token %} 19 19 {% for field in form %}{{ field.as_hidden }} 20 20 {% endfor %} 21 21 <input type="hidden" name="{{ stage_field }}" value="2" /> … … 25 25 26 26 <h1>Or edit it again</h1> 27 27 28 <form action="" method="post"> 28 <form action="" method="post">{% csrf_token %} 29 29 <table> 30 30 {{ form }} 31 31 </table> -
docs/ref/contrib/csrf.txt
diff -r 61e8647f8f4f docs/ref/contrib/csrf.txt
a b 7 7 .. module:: django.contrib.csrf 8 8 :synopsis: Protects against Cross Site Request Forgeries 9 9 10 The C srfMiddleware classprovides easy-to-use protection against10 The CSRF middleware and template tag provides easy-to-use protection against 11 11 `Cross Site Request Forgeries`_. This type of attack occurs when a malicious 12 12 Web site creates a link or form button that is intended to perform some action 13 on your Web site, using the credentials of a logged-in user who is tricked 14 into clicking on the link in their browser. 13 on your Web site, using the credentials of a logged-in user who is tricked into 14 clicking on the link in their browser. A related type of attack, 'login CSRF', 15 where an attacking site tricks a user's browser into logging into a site with 16 someone else's credentials, is also covered. 15 17 16 The first defense against CSRF attacks is to ensure that GET requests 17 are side-effect free. POST requests can then be protected by adding this 18 middleware into your list of installed middleware. 18 The first defense against CSRF attacks is to ensure that GET requests are 19 side-effect free. POST requests can then be protected by adding these 20 middleware into your list of installed middleware following the steps below. 21 22 .. versionadded:: 1.2 23 The 'contrib' apps, including the admin, depend on the functionality 24 described here. Anyone upgrading from earlier versions should read 25 the `Upgrading notes`_ carefully. 19 26 20 27 .. _Cross Site Request Forgeries: http://www.squarefree.com/securitytips/web-developers.html#CSRF 21 28 22 29 How to use it 23 30 ============= 24 31 25 Add the middleware ``'django.contrib.csrf.middleware.CsrfMiddleware'`` to 26 your list of middleware classes, :setting:`MIDDLEWARE_CLASSES`. It needs to process 27 the response after the SessionMiddleware, so must come before it in the 28 list. It also must process the response before things like compression 29 happen to the response, so it must come after GZipMiddleware in the 30 list. 32 .. versionchanged:: 1.2 33 The template tag functionality (the recommended way to use this) was added 34 in version 1.2. The previous method (still available) is described under 35 `Legacy method`_. 31 36 32 The ``CsrfMiddleware`` class is actually composed of two middleware: 33 ``CsrfViewMiddleware`` which performs the checks on incoming requests, 34 and ``CsrfResponseMiddleware`` which performs post-processing of the 35 result. This allows the individual components to be used and/or 36 replaced instead of using ``CsrfMiddleware``. 37 To enable CSRF protection for your views, follow these steps: 37 38 38 .. versionchanged:: 1.1 39 (previous versions of Django did not provide these two components 40 of ``CsrfMiddleware`` as described above) 39 1. Add the middleware 40 ``'django.contrib.csrf.middleware.CsrfViewMiddleware'`` to your list of 41 middleware classes, :setting:`MIDDLEWARE_CLASSES`. (It should come 42 before ``CsrfResponseMiddleware`` if that is being used, and before any 43 view middleware that assume that CSRF attacks have been dealt with.) 44 45 2. Add ``'django.contrib.csrf'`` to your :setting:`INSTALLED_APPS`. 46 47 3. In any template that uses a POST form, first load the 'csrf' template tag 48 library:: 49 50 {% load csrf %} 51 52 Then use the ``csrf_token`` tag inside the ``<form>`` element, e.g.:: 53 54 <form action="" method="POST">{% csrf_token %} 55 56 This should not be done for POST forms that target external URLs, since 57 that would cause the CSRF token to be leaked, leading to a vulnerability. 58 59 4. In the corresponding view functions, ensure that the 60 ``'django.contrib.csrf.context_processors.csrf'`` context processor is 61 being used. Usually, this can be done in one of two ways: 62 63 1. Using RequestContext: 64 65 1. Ensure ``'django.contrib.csrf.context_processors.csrf'`` is present 66 in your :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting. It is 67 present by default. 68 69 2. Use ``RequestContext`` as the context instance in the relevant 70 views. If you are using generic views or contrib apps, you are 71 covered already. 72 73 2. Manually import and use the processor to generate the CSRF token and 74 add it to the template context. e.g.:: 75 76 from django.contrib.csrf.context_processors import csrf 77 from django.template import Context 78 from django.shortcuts import render_to_response 79 def my_view(request): 80 c = Context() 81 c.update(csrf(request)) 82 return render_to_response("a_template.html", context_instance=c) 83 84 Legacy method 85 ------------- 86 87 In Django 1.1, the template tag did not exist. Instead, a post-processing 88 middleware that re-wrote POST forms to include the CRSF token was used. If you 89 are upgrading a site from version 1.1 or earlier, please read this section and 90 the `Upgrading notes`_ below. The post-processing middleware is still available 91 as ``CsrfResponseMiddleware``, and it can be used by following these steps: 92 93 1. Follow step 1 above to install ``CsrfViewMiddleware``. 94 95 2. Add ``'django.contrib.csrf.middleware.CsrfResponseMiddleware'`` to your 96 :setting:`MIDDLEWARE_CLASSES` setting. 97 98 ``CsrfResponseMiddleware`` needs to process the response before things 99 like compression happen to the response, so it must come after 100 ``GZipMiddleware`` in the list. It also must come after 101 ``CsrfViewMiddleware``. 102 103 Use of the ``CsrfResponseMiddleware`` is not recommended because of the 104 performance hit it imposes, and because of a potential security problem (see 105 below). It can be used as an interim measure until applications have been 106 updated to use the ``{% crsf_token %}`` tag. 107 108 Django 1.1 and earlier provided a single ``CsrfMiddleware`` class. This is also 109 still available for backwards compatibility. It combines the functions of the 110 two new middleware. 111 112 Note also that previous versions of these classes depended on the sessions 113 framework, but this dependency has now been removed, with backward compatibility 114 support so that upgrading will not produce any issues. 115 116 Security of legacy method 117 ~~~~~~~~~~~~~~~~~~~~~~~~~ 118 119 The post-processing ``CsrfResponseMiddleware`` adds the CSRF token to all POST 120 forms (unless the view has been decorated with ``csrf_response_exempt``). If 121 the POST form has an external untrusted site as its target, rather than an 122 internal page, that site will be sent the CSRF token when the form is submitted. 123 Armed with this leaked information, that site will then be able to successfully 124 launch a CSRF attack on your site against that user. The 125 ``@csrf_response_exempt`` decorator can be used to fix this, but only if the 126 page doesn't also contain internal forms that require the token. 127 128 Upgrading notes 129 --------------- 130 131 When upgrading to version 1.2 or later, you may have applications that rely on 132 the old post-processing functionality for CSRF protection, or you may not have 133 enabled any CSRF protection. This section outlines the steps necessary for a 134 smooth upgrade, without having to fix all the applications to use the new 135 template tag method immediately. 136 137 If you are using any of the contrib apps (such as the admin), there are some 138 required steps for these applications to continue working. First, the CSRF 139 application must be added to :setting:`INSTALLED_APPS` (See `How to use it`_ 140 above, step 2). Second, the :setting:`TEMPLATE_CONTEXT_PROCESSORS` setting must 141 be updated (step 4.1.1 above). 142 143 If you have ``CsrfMiddleware`` in your :setting:`MIDDLEWARE_CLASSES`, you will now 144 have a working installation with CSRF protection. It is recommended at this 145 point that you replace ``CsrfMiddleware`` with its two components, 146 ``CsrfViewMiddleware`` and ``CsrfResponseMiddleware`` (in that order). 147 148 If you do not have any of the middleware in your :setting:`MIDDLEWARE_CLASSES`, 149 you will have a working installation but without any CSRF protection (just as 150 you had before). It is recommended to install ``CsrfViewMiddleware`` and 151 ``CsrfResponseMiddleware``, but if you are not interested in having any CSRF 152 protection, you can simply stop here (not recommended!). 153 154 Assuming you have followed the above, all views in your Django site will now be 155 protected by the ``CsrfViewMiddleware``. Contrib apps meet the requirements 156 imposed by the ``CsrfViewMiddleware`` using the template tag, and other 157 applications in your project will meet its requirements by virtue of the 158 ``CsrfResponseMiddleware``. 159 160 The next step is to update all your applications to use the template tag, as 161 described in `How to use it`_, steps 3-4. This can be done as soon as is 162 practical. Any applications that are updated will now require Django 1.2 or 163 later, since they will use the CSRF template tag library which was not available 164 in earlier versions. 165 166 Finally, once all applications are upgraded, ``CsrfResponseMiddleware`` can be 167 removed from your settings. 168 169 While ``CsrfResponseMiddleware`` is still in use, the ``csrf_response_exempt`` 170 decorator, described in `Exceptions`_, may be useful. The post-processing 171 middleware imposes a performance hit (and a potential vulnerability), and any 172 views that have been upgraded to use the new template tag method no longer need 173 it. 41 174 42 175 Exceptions 43 176 ---------- 44 177 45 178 .. versionadded:: 1.1 46 179 47 To manually exclude a view function from being handled by the48 CsrfMiddleware, you can use the ``csrf_exempt`` decorator, found in 49 the``django.contrib.csrf.middleware`` module. For example::180 To manually exclude a view function from being handled by either of the two CSRF 181 middleware, you can use the ``csrf_exempt`` decorator, found in the 182 ``django.contrib.csrf.middleware`` module. For example:: 50 183 51 184 from django.contrib.csrf.middleware import csrf_exempt 52 185 … … 54 187 return HttpResponse('Hello world') 55 188 my_view = csrf_exempt(my_view) 56 189 57 Like the middleware itself, the ``csrf_exempt`` decorator is composed58 of two parts: a ``csrf_view_exempt`` decorator and a 59 ``csrf_response_exempt`` decorator, found in the same module. These 60 disable the view protection mechanism (``CsrfViewMiddleware``) and the 61 response post-processing (``CsrfResponseMiddleware``) respectively. 62 They can be used individually ifrequired.190 Like the middleware, the ``csrf_exempt`` decorator is composed of two parts: a 191 ``csrf_view_exempt`` decorator and a ``csrf_response_exempt`` decorator, found 192 in the same module. These disable the view protection mechanism 193 (``CsrfViewMiddleware``) and the response post-processing 194 (``CsrfResponseMiddleware``) respectively. They can be used individually if 195 required. 63 196 64 You don't have to worry about doing this for most AJAX views. Any 65 request sent with "X-Requested-With: XMLHttpRequest" is automatically 66 exempt. (See the next section.) 197 You don't have to worry about doing this for most AJAX views. Any request sent 198 with "X-Requested-With: XMLHttpRequest" is automatically exempt. (See the next 199 section.) 200 201 Subdomains 202 ---------- 203 204 By default, CSRF cookies are specific to the subdomain they are set for. This 205 means that a form served from one subdomain (e.g. server1.example.com) will not 206 be able to have a target on another subdomain (e.g. server2.example.com). This 207 restriction can be removed by setting :setting:`CSRF_COOKIE_DOMAIN` to be 208 something like ``".example.com"``. 209 210 Please note that, with or without use of this setting, this CSRF protection 211 mechanism is not safe against cross-subdomain attacks -- see `Limitations`_. 212 213 Rejected requests 214 ================= 215 216 By default, a '403 Forbidden' response is sent to the user if an incoming 217 request fails the checks performed by ``CsrfViewMiddleware``. This should 218 usually only be seen when there is a genuine Cross Site Request Forgery, or 219 when, due to a programming error, the CSRF token has not been included with a 220 POST form. 221 222 No logging is done, and the error message is not very friendly, so you may want 223 to provide your own page for handling this condition. To do this, simply set 224 the :setting:`CSRF_FAILURE_VIEW` setting to a dotted path to your own view 225 function. 67 226 68 227 How it works 69 228 ============ 70 229 71 CsrfMiddleware does twothings:230 The CSRF protection is based on three things: 72 231 73 1. It modifies outgoing requests by adding a hidden form field to all 74 'POST' forms, with the name 'csrfmiddlewaretoken' and a value which is 75 a hash of the session ID plus a secret. If there is no session ID set, 76 this modification of the response isn't done, so there is very little 77 performance penalty for those requests that don't have a session. 78 (This is done by ``CsrfResponseMiddleware``). 232 1. A CSRF cookie that is set to a random value, which other sites will not have 233 access to. 79 234 80 2. On all incoming POST requests that have the session cookie set, it 81 checks that the 'csrfmiddlewaretoken' is present and correct. If it82 isn't, the user will get a 403 error. (This is done by83 ``CsrfViewMiddleware``)235 This cookie is set by ``CsrfViewMiddleware``. It is meant to be permanent, 236 but since there is no way to set a cookie that never expires, it is sent with 237 every response that has called ``django.contrib.csrf.middleware.get_token()`` 238 (the function used internally to retrieve the CSRF token). 84 239 85 This ensures that only forms that have originated from your Web site 86 can be used to POST data back. 240 2. A hidden form field with the name 'csrfmiddlewaretoken' present in all 241 outgoing POST forms. The value of this field is the contents of the CSRF 242 cookie. 243 244 This part is done by the template tag (and with the legacy method, it is done 245 by ``CsrfResponseMiddleware``). 246 247 3. For all incoming POST requests, a CSRF cookie must be present, and the 248 'csrfmiddlewaretoken' field must be present and correct. If it isn't, the 249 user will get a 403 error. 250 251 This check is done by ``CsrfViewMiddleware``. 252 253 This ensures that only forms that have originated from your Web site can be used 254 to POST data back. 87 255 88 256 It deliberately only targets HTTP POST requests (and the corresponding POST 89 forms). GET requests ought never to have any potentially dangerous side 90 effects (see `9.1.1 Safe Methods, HTTP 1.1, RFC 2616`_), and so a 91 CSRF attack with a GET request ought to be harmless. 92 93 POST requests that are not accompanied by a session cookie are not protected, 94 but they do not need to be protected, since the 'attacking' Web site 95 could make these kind of requests anyway. 96 97 The Content-Type is checked before modifying the response, and only 98 pages that are served as 'text/html' or 'application/xml+xhtml' 99 are modified. 257 forms). GET requests ought never to have any potentially dangerous side effects 258 (see `9.1.1 Safe Methods, HTTP 1.1, RFC 2616`_), and so a CSRF attack with a GET 259 request ought to be harmless. 100 260 101 261 The middleware tries to be smart about requests that come in via AJAX. Many 102 262 JavaScript toolkits send an "X-Requested-With: XMLHttpRequest" HTTP header; 103 263 these requests are detected and automatically *not* handled by this middleware. 104 264 We can do this safely because, in the context of a browser, the header can only 105 265 be added by using ``XMLHttpRequest``, and browsers already implement a 106 same-domain policy for ``XMLHttpRequest``. (Note that this is not secure if you 107 don't trust content within the same domain or subdomains.) 266 same-domain policy for ``XMLHttpRequest``. 108 267 268 ``CsrfResponseMiddleware`` checks the Content-Type before modifying the 269 response, and only pages that are served as 'text/html' or 270 'application/xml+xhtml' are modified. 109 271 110 272 .. _9.1.1 Safe Methods, HTTP 1.1, RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html 111 273 112 274 Limitations 113 275 =========== 114 276 115 CsrfMiddleware requires Django's session framework to work. If you have 116 a custom authentication system that manually sets cookies and the like, 117 it won't help you. 277 Subdomains within a site will be able to set cookies on the client for the whole 278 domain. By setting the cookie and using a corresponding token, subdomains will 279 be able to circumvent the CSRF protection. The only way to avoid this is to 280 ensure that subdomains are controlled by trusted users (or, are at least unable 281 to set cookies). Note that even without CSRF, there are other vulnerabilities, 282 such as session fixation, that make giving subdomains to untrusted parties a bad 283 idea, and these vulnerabilities cannot easily be fixed with current browsers. 118 284 119 If your app creates HTML pages and forms in some unusual way, (e.g. 120 it sends fragments of HTML in JavaScript document.write statements) 121 you might bypass the filter that adds the hidden field to the form, 122 in which case form submission will always fail. It may still be possible 123 to use the middleware, provided you can find some way to get the 124 CSRF token and ensure that is included when your form is submitted. 285 If you are using ``CsrfResponseMiddleware`` and your app creates HTML pages and 286 forms in some unusual way, (e.g. it sends fragments of HTML in JavaScript 287 document.write statements) you might bypass the filter that adds the hidden 288 field to the form, in which case form submission will always fail. It may still 289 be possible to use this middleware, provided you use the template tag or 290 :meth:`django.contrib.csrf.middleware.get_token` to get the CSRF token and 291 ensure that is included when your form is submitted. 292 293 An active network (man-in-the-middle) attacker will obviously be able to 294 circumvent these protections. What is less obvious is that this is still true 295 for HTTPS connections, which are normally immune from other MITM attacks. This 296 is due to the fact that clients accept HTTP Set-Cookie headers even if the rest 297 of the connection is HTTPS. -
docs/ref/settings.txt
diff -r 61e8647f8f4f docs/ref/settings.txt
a b 144 144 The default number of seconds to cache a page when the caching middleware or 145 145 ``cache_page()`` decorator is used. 146 146 147 .. setting:: CSRF_COOKIE_NAME 148 149 CSRF_COOKIE_NAME 150 ---------------- 151 Default: ``'csrf_token'`` 152 153 The name of the cookie to use for the CSRF authentication token. This can be whatever you 154 want. See :ref:`ref-contrib-csrf`. 155 156 .. setting:: CSRF_COOKIE_DOMAIN 157 158 CSRF_COOKIE_DOMAIN 159 ------------------ 160 161 Default: ``None`` 162 163 The domain to be used when setting the CSRF cookie. This can be useful for 164 allowing cross-subdomain requests to be exluded from the normal cross site 165 request forgery protection. It should be set to a string such as 166 ``".lawrence.com"`` to allow a POST request from a form on one subdomain to be 167 accepted by accepted by a view served from another subdomain. 168 169 .. setting:: CSRF_FAILURE_VIEW 170 171 CSRF_FAILURE_VIEW 172 ----------------- 173 174 Default: ``'django.contrib.csrf.views.csrf_failure'`` 175 176 A dotted path to the view function to be used when an incoming request 177 is rejected by the CSRF protection. See :ref:`ref-contrib-csrf`. 178 147 179 .. setting:: DATABASE_ENGINE 148 180 149 181 DATABASE_ENGINE … … 751 783 752 784 ('django.middleware.common.CommonMiddleware', 753 785 'django.contrib.sessions.middleware.SessionMiddleware', 786 'django.contrib.csrf.middleware.CsrfViewMiddleware', 754 787 'django.contrib.auth.middleware.AuthenticationMiddleware',) 755 788 756 789 A tuple of middleware classes to use. See :ref:`topics-http-middleware`. -
docs/topics/http/middleware.txt
diff -r 61e8647f8f4f docs/topics/http/middleware.txt
a b 29 29 MIDDLEWARE_CLASSES = ( 30 30 'django.middleware.common.CommonMiddleware', 31 31 'django.contrib.sessions.middleware.SessionMiddleware', 32 'django.contrib.csrf.middleware.CsrfViewMiddleware', 32 33 'django.contrib.auth.middleware.AuthenticationMiddleware', 33 34 ) 34 35 -
tests/regressiontests/admin_views/tests.py
diff -r 61e8647f8f4f tests/regressiontests/admin_views/tests.py
a b 874 874 # 4 action inputs (3 regular checkboxes, 1 checkbox to select all) 875 875 # main form submit button = 1 876 876 # search field and search submit button = 2 877 # 6 + 2 + 1 + 2 = 11inputs877 # 6 + 2 + 4 + 1 + 2 + 1 = 15 inputs 878 878 self.failUnlessEqual(response.content.count("<input"), 15) 879 879 # 1 select per object = 3 selects 880 880 self.failUnlessEqual(response.content.count("<select"), 4) -
tests/runtests.py
diff -r 61e8647f8f4f tests/runtests.py
a b 30 30 'django.contrib.sessions', 31 31 'django.contrib.comments', 32 32 'django.contrib.admin', 33 'django.contrib.csrf', 33 34 ] 34 35 35 36 def get_test_models():