Django

Code

root/django/branches/gis/django/contrib/csrf/middleware.py

Revision 8215, 3.6 kB (checked in by jbronn, 4 months ago)

gis: Merged revisions 7981-8001,8003-8011,8013-8033,8035-8036,8038-8039,8041-8063,8065-8076,8078-8139,8141-8154,8156-8214 via svnmerge from trunk.

  • Property svn:eol-style set to native
Line 
1 """
2 Cross Site Request Forgery Middleware.
3
4 This module provides a middleware that implements protection
5 against request forgeries from other sites.
6 """
7
8 import re
9 import itertools
10
11 from django.conf import settings
12 from django.http import HttpResponseForbidden
13 from django.utils.hashcompat import md5_constructor
14 from django.utils.safestring import mark_safe
15
16 _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>')
17
18 _POST_FORM_RE = \
19     re.compile(r'(<form\W[^>]*\bmethod=(\'|"|)POST(\'|"|)\b[^>]*>)', re.IGNORECASE)
20
21 _HTML_TYPES = ('text/html', 'application/xhtml+xml')
22
23 def _make_token(session_id):
24     return md5_constructor(settings.SECRET_KEY + session_id).hexdigest()
25
26 class CsrfMiddleware(object):
27     """Django middleware that adds protection against Cross Site
28     Request Forgeries by adding hidden form fields to POST forms and
29     checking requests for the correct value.
30
31     In the list of middlewares, SessionMiddleware is required, and must come
32     after this middleware.  CsrfMiddleWare must come after compression
33     middleware.
34
35     If a session ID cookie is present, it is hashed with the SECRET_KEY
36     setting to create an authentication token.  This token is added to all
37     outgoing POST forms and is expected on all incoming POST requests that
38     have a session ID cookie.
39
40     If you are setting cookies directly, instead of using Django's session
41     framework, this middleware will not work.
42     """
43
44     def process_request(self, request):
45         if request.method == 'POST':
46             try:
47                 session_id = request.COOKIES[settings.SESSION_COOKIE_NAME]
48             except KeyError:
49                 # No session, no check required
50                 return None
51
52             csrf_token = _make_token(session_id)
53             # check incoming token
54             try:
55                 request_csrf_token = request.POST['csrfmiddlewaretoken']
56             except KeyError:
57                 return HttpResponseForbidden(_ERROR_MSG)
58
59             if request_csrf_token != csrf_token:
60                 return HttpResponseForbidden(_ERROR_MSG)
61
62         return None
63
64     def process_response(self, request, response):
65         csrf_token = None
66         try:
67             cookie = response.cookies[settings.SESSION_COOKIE_NAME]
68             csrf_token = _make_token(cookie.value)
69         except KeyError:
70             # No outgoing cookie to set session, but
71             # a session might already exist.
72             try:
73                 session_id = request.COOKIES[settings.SESSION_COOKIE_NAME]
74                 csrf_token = _make_token(session_id)
75             except KeyError:
76                 # no incoming or outgoing cookie
77                 pass
78
79         if csrf_token is not None and \
80                 response['Content-Type'].split(';')[0] in _HTML_TYPES:
81
82             # ensure we don't add the 'id' attribute twice (HTML validity)
83             idattributes = itertools.chain(("id='csrfmiddlewaretoken'",),
84                                             itertools.repeat(''))
85             def add_csrf_field(match):
86                 """Returns the matched <form> tag plus the added <input> element"""
87                 return mark_safe(match.group() + "<div style='display:none;'>" + \
88                 "<input type='hidden' " + idattributes.next() + \
89                 " name='csrfmiddlewaretoken' value='" + csrf_token + \
90                 "' /></div>")
91
92             # Modify any POST forms
93             response.content = _POST_FORM_RE.sub(add_csrf_field, response.content)
94         return response
Note: See TracBrowser for help on using the browser.