Changeset 9554
- Timestamp:
- 12/02/08 18:34:18 (1 month ago)
- Files:
-
- django/trunk/django/contrib/csrf/middleware.py (modified) (3 diffs)
- django/trunk/django/contrib/csrf/tests.py (modified) (4 diffs)
- django/trunk/docs/ref/contrib/csrf.txt (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/contrib/csrf/middleware.py
r9553 r9554 8 8 import re 9 9 import itertools 10 try: 11 from functools import wraps 12 except ImportError: 13 from django.utils.functional import wraps # Python 2.3, 2.4 fallback. 10 14 11 15 from django.conf import settings … … 31 35 def process_view(self, request, callback, callback_args, callback_kwargs): 32 36 if request.method == 'POST': 37 if getattr(callback, 'csrf_exempt', False): 38 return None 39 40 if request.is_ajax(): 41 return None 42 33 43 try: 34 44 session_id = request.COOKIES[settings.SESSION_COOKIE_NAME] … … 108 118 """ 109 119 pass 120 121 def csrf_exempt(view_func): 122 """ 123 Marks a view function as being exempt from the CSRF checks 124 """ 125 def wrapped_view(*args, **kwargs): 126 return view_func(*args, **kwargs) 127 # We could just do view.csrf_exempt = True, but decorators are 128 # nicer if they don't have side-effects. 129 wrapped_view.csrf_exempt = True 130 return wraps(view_func)(wrapped_view) django/trunk/django/contrib/csrf/tests.py
r9553 r9554 3 3 from django.test import TestCase 4 4 from django.http import HttpRequest, HttpResponse, HttpResponseForbidden 5 from django.contrib.csrf.middleware import CsrfMiddleware, _make_token 5 from django.contrib.csrf.middleware import CsrfMiddleware, _make_token, csrf_exempt 6 6 from django.conf import settings 7 7 8 9 def post_form_response(): 10 resp = HttpResponse(content=""" 11 <html><body><form method="POST"><input type="text" /></form></body></html> 12 """, mimetype="text/html") 13 return resp 14 15 def test_view(request): 16 return post_form_response() 8 17 9 18 class CsrfMiddlewareTest(TestCase): … … 35 44 36 45 def _get_post_form_response(self): 37 resp = HttpResponse(content=""" 38 <html><body><form method="POST"><input type="text" /></form></body></html> 39 """, mimetype="text/html") 40 return resp 46 return post_form_response() 41 47 42 48 def _get_new_session_response(self): … … 49 55 50 56 def get_view(self): 51 def dummyview(request): 52 return self._get_post_form_response() 57 return test_view 53 58 54 59 # Check the post processing … … 110 115 req2 = CsrfMiddleware().process_view(req, self.get_view(), (), {}) 111 116 self.assertEquals(None, req2) 117 118 def test_process_request_session_no_token_exempt_view(self): 119 """ 120 Check that if a session is present and no token, but the csrf_exempt 121 decorator has been applied to the view, the middleware lets it through 122 """ 123 req = self._get_POST_session_request() 124 req2 = CsrfMiddleware().process_view(req, csrf_exempt(self.get_view()), (), {}) 125 self.assertEquals(None, req2) 126 127 def test_ajax_exemption(self): 128 """ 129 Check the AJAX requests are automatically exempted. 130 """ 131 req = self._get_POST_session_request() 132 req.META['HTTP_X_REQUESTED_WITH'] = 'XMLHttpRequest' 133 req2 = CsrfMiddleware().process_view(req, self.get_view(), (), {}) 134 self.assertEquals(None, req2) django/trunk/docs/ref/contrib/csrf.txt
r8506 r9554 27 27 the response after the SessionMiddleware, so must come before it in the 28 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 list. 29 happen to the response, so it must come after GZipMiddleware in the 30 list. 31 32 Exceptions 33 ---------- 34 35 To manually exclude a view function from being handled by the 36 CsrfMiddleware, you can use the ``csrf_exempt`` decorator (found in 37 the ``django.contrib.csrf.middleware`` module). 38 39 AJAX requests sent with "X-Requested-With: XMLHttpRequest" are 40 automatically exempt (see below). 30 41 31 42 How it works … … 60 71 are modified. 61 72 73 AJAX requests sent with "X-Requested-With: XMLHttpRequest", as done by 74 many AJAX toolkits, are detected and automatically excepted from this 75 mechanism. This is because in the context of a browser, this header 76 can only be added by using XMLHttpRequest, and browsers already 77 implement a same-domain policy for XMLHttpRequest. This is not secure 78 if you do not trust content within the same domain or sub-domains. 79 80 The above two functions of ``CsrfMiddleware`` are split between two 81 classes: ``CsrfResponseMiddleware`` and ``CsrfViewMiddleware`` 82 respectively. This allows the individual components to be used and/or 83 replaced instead of using ``CsrfMiddleware``. 84 62 85 .. _9.1.1 Safe Methods, HTTP 1.1, RFC 2616: http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html 63 86
