csrf.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. """
  2. Cross Site Request Forgery Middleware.
  3. This module provides a middleware that implements protection
  4. against request forgeries from other sites.
  5. """
  6. from __future__ import unicode_literals
  7. import logging
  8. import re
  9. import string
  10. from django.conf import settings
  11. from django.urls import get_callable
  12. from django.utils.cache import patch_vary_headers
  13. from django.utils.crypto import constant_time_compare, get_random_string
  14. from django.utils.deprecation import MiddlewareMixin
  15. from django.utils.encoding import force_text
  16. from django.utils.http import is_same_domain
  17. from django.utils.six.moves import zip
  18. from django.utils.six.moves.urllib.parse import urlparse
  19. logger = logging.getLogger('django.request')
  20. REASON_NO_REFERER = "Referer checking failed - no Referer."
  21. REASON_BAD_REFERER = "Referer checking failed - %s does not match any trusted origins."
  22. REASON_NO_CSRF_COOKIE = "CSRF cookie not set."
  23. REASON_BAD_TOKEN = "CSRF token missing or incorrect."
  24. REASON_MALFORMED_REFERER = "Referer checking failed - Referer is malformed."
  25. REASON_INSECURE_REFERER = "Referer checking failed - Referer is insecure while host is secure."
  26. CSRF_SECRET_LENGTH = 32
  27. CSRF_TOKEN_LENGTH = 2 * CSRF_SECRET_LENGTH
  28. CSRF_ALLOWED_CHARS = string.ascii_letters + string.digits
  29. def _get_failure_view():
  30. """
  31. Returns the view to be used for CSRF rejections
  32. """
  33. return get_callable(settings.CSRF_FAILURE_VIEW)
  34. def _get_new_csrf_string():
  35. return get_random_string(CSRF_SECRET_LENGTH, allowed_chars=CSRF_ALLOWED_CHARS)
  36. def _salt_cipher_secret(secret):
  37. """
  38. Given a secret (assumed to be a string of CSRF_ALLOWED_CHARS), generate a
  39. token by adding a salt and using it to encrypt the secret.
  40. """
  41. salt = _get_new_csrf_string()
  42. chars = CSRF_ALLOWED_CHARS
  43. pairs = zip((chars.index(x) for x in secret), (chars.index(x) for x in salt))
  44. cipher = ''.join(chars[(x + y) % len(chars)] for x, y in pairs)
  45. return salt + cipher
  46. def _unsalt_cipher_token(token):
  47. """
  48. Given a token (assumed to be a string of CSRF_ALLOWED_CHARS, of length
  49. CSRF_TOKEN_LENGTH, and that its first half is a salt), use it to decrypt
  50. the second half to produce the original secret.
  51. """
  52. salt = token[:CSRF_SECRET_LENGTH]
  53. token = token[CSRF_SECRET_LENGTH:]
  54. chars = CSRF_ALLOWED_CHARS
  55. pairs = zip((chars.index(x) for x in token), (chars.index(x) for x in salt))
  56. secret = ''.join(chars[x - y] for x, y in pairs) # Note negative values are ok
  57. return secret
  58. def _get_new_csrf_token():
  59. return _salt_cipher_secret(_get_new_csrf_string())
  60. def get_token(request):
  61. """
  62. Returns the CSRF token required for a POST form. The token is an
  63. alphanumeric value. A new token is created if one is not already set.
  64. A side effect of calling this function is to make the csrf_protect
  65. decorator and the CsrfViewMiddleware add a CSRF cookie and a 'Vary: Cookie'
  66. header to the outgoing response. For this reason, you may need to use this
  67. function lazily, as is done by the csrf context processor.
  68. """
  69. if "CSRF_COOKIE" not in request.META:
  70. csrf_secret = _get_new_csrf_string()
  71. request.META["CSRF_COOKIE"] = _salt_cipher_secret(csrf_secret)
  72. else:
  73. csrf_secret = _unsalt_cipher_token(request.META["CSRF_COOKIE"])
  74. request.META["CSRF_COOKIE_USED"] = True
  75. return _salt_cipher_secret(csrf_secret)
  76. def rotate_token(request):
  77. """
  78. Changes the CSRF token in use for a request - should be done on login
  79. for security purposes.
  80. """
  81. request.META.update({
  82. "CSRF_COOKIE_USED": True,
  83. "CSRF_COOKIE": _get_new_csrf_token(),
  84. })
  85. request.csrf_cookie_needs_reset = True
  86. def _sanitize_token(token):
  87. # Allow only ASCII alphanumerics
  88. if re.search('[^a-zA-Z0-9]', force_text(token)):
  89. return _get_new_csrf_token()
  90. elif len(token) == CSRF_TOKEN_LENGTH:
  91. return token
  92. elif len(token) == CSRF_SECRET_LENGTH:
  93. # Older Django versions set cookies to values of CSRF_SECRET_LENGTH
  94. # alphanumeric characters. For backwards compatibility, accept
  95. # such values as unsalted secrets.
  96. # It's easier to salt here and be consistent later, rather than add
  97. # different code paths in the checks, although that might be a tad more
  98. # efficient.
  99. return _salt_cipher_secret(token)
  100. return _get_new_csrf_token()
  101. def _compare_salted_tokens(request_csrf_token, csrf_token):
  102. # Assume both arguments are sanitized -- that is, strings of
  103. # length CSRF_TOKEN_LENGTH, all CSRF_ALLOWED_CHARS.
  104. return constant_time_compare(
  105. _unsalt_cipher_token(request_csrf_token),
  106. _unsalt_cipher_token(csrf_token),
  107. )
  108. class CsrfViewMiddleware(MiddlewareMixin):
  109. """
  110. Middleware that requires a present and correct csrfmiddlewaretoken
  111. for POST requests that have a CSRF cookie, and sets an outgoing
  112. CSRF cookie.
  113. This middleware should be used in conjunction with the csrf_token template
  114. tag.
  115. """
  116. # The _accept and _reject methods currently only exist for the sake of the
  117. # requires_csrf_token decorator.
  118. def _accept(self, request):
  119. # Avoid checking the request twice by adding a custom attribute to
  120. # request. This will be relevant when both decorator and middleware
  121. # are used.
  122. request.csrf_processing_done = True
  123. return None
  124. def _reject(self, request, reason):
  125. logger.warning(
  126. 'Forbidden (%s): %s', reason, request.path,
  127. extra={
  128. 'status_code': 403,
  129. 'request': request,
  130. }
  131. )
  132. return _get_failure_view()(request, reason=reason)
  133. def process_view(self, request, callback, callback_args, callback_kwargs):
  134. if getattr(request, 'csrf_processing_done', False):
  135. return None
  136. try:
  137. cookie_token = request.COOKIES[settings.CSRF_COOKIE_NAME]
  138. except KeyError:
  139. csrf_token = None
  140. else:
  141. csrf_token = _sanitize_token(cookie_token)
  142. if csrf_token != cookie_token:
  143. # Cookie token needed to be replaced;
  144. # the cookie needs to be reset.
  145. request.csrf_cookie_needs_reset = True
  146. # Use same token next time.
  147. request.META['CSRF_COOKIE'] = csrf_token
  148. # Wait until request.META["CSRF_COOKIE"] has been manipulated before
  149. # bailing out, so that get_token still works
  150. if getattr(callback, 'csrf_exempt', False):
  151. return None
  152. # Assume that anything not defined as 'safe' by RFC7231 needs protection
  153. if request.method not in ('GET', 'HEAD', 'OPTIONS', 'TRACE'):
  154. if getattr(request, '_dont_enforce_csrf_checks', False):
  155. # Mechanism to turn off CSRF checks for test suite.
  156. # It comes after the creation of CSRF cookies, so that
  157. # everything else continues to work exactly the same
  158. # (e.g. cookies are sent, etc.), but before any
  159. # branches that call reject().
  160. return self._accept(request)
  161. if request.is_secure():
  162. # Suppose user visits http://example.com/
  163. # An active network attacker (man-in-the-middle, MITM) sends a
  164. # POST form that targets https://example.com/detonate-bomb/ and
  165. # submits it via JavaScript.
  166. #
  167. # The attacker will need to provide a CSRF cookie and token, but
  168. # that's no problem for a MITM and the session-independent
  169. # secret we're using. So the MITM can circumvent the CSRF
  170. # protection. This is true for any HTTP connection, but anyone
  171. # using HTTPS expects better! For this reason, for
  172. # https://example.com/ we need additional protection that treats
  173. # http://example.com/ as completely untrusted. Under HTTPS,
  174. # Barth et al. found that the Referer header is missing for
  175. # same-domain requests in only about 0.2% of cases or less, so
  176. # we can use strict Referer checking.
  177. referer = force_text(
  178. request.META.get('HTTP_REFERER'),
  179. strings_only=True,
  180. errors='replace'
  181. )
  182. if referer is None:
  183. return self._reject(request, REASON_NO_REFERER)
  184. referer = urlparse(referer)
  185. # Make sure we have a valid URL for Referer.
  186. if '' in (referer.scheme, referer.netloc):
  187. return self._reject(request, REASON_MALFORMED_REFERER)
  188. # Ensure that our Referer is also secure.
  189. if referer.scheme != 'https':
  190. return self._reject(request, REASON_INSECURE_REFERER)
  191. # If there isn't a CSRF_COOKIE_DOMAIN, assume we need an exact
  192. # match on host:port. If not, obey the cookie rules.
  193. if settings.CSRF_COOKIE_DOMAIN is None:
  194. # request.get_host() includes the port.
  195. good_referer = request.get_host()
  196. else:
  197. good_referer = settings.CSRF_COOKIE_DOMAIN
  198. server_port = request.get_port()
  199. if server_port not in ('443', '80'):
  200. good_referer = '%s:%s' % (good_referer, server_port)
  201. # Here we generate a list of all acceptable HTTP referers,
  202. # including the current host since that has been validated
  203. # upstream.
  204. good_hosts = list(settings.CSRF_TRUSTED_ORIGINS)
  205. good_hosts.append(good_referer)
  206. if not any(is_same_domain(referer.netloc, host) for host in good_hosts):
  207. reason = REASON_BAD_REFERER % referer.geturl()
  208. return self._reject(request, reason)
  209. if csrf_token is None:
  210. # No CSRF cookie. For POST requests, we insist on a CSRF cookie,
  211. # and in this way we can avoid all CSRF attacks, including login
  212. # CSRF.
  213. return self._reject(request, REASON_NO_CSRF_COOKIE)
  214. # Check non-cookie token for match.
  215. request_csrf_token = ""
  216. if request.method == "POST":
  217. try:
  218. request_csrf_token = request.POST.get('csrfmiddlewaretoken', '')
  219. except IOError:
  220. # Handle a broken connection before we've completed reading
  221. # the POST data. process_view shouldn't raise any
  222. # exceptions, so we'll ignore and serve the user a 403
  223. # (assuming they're still listening, which they probably
  224. # aren't because of the error).
  225. pass
  226. if request_csrf_token == "":
  227. # Fall back to X-CSRFToken, to make things easier for AJAX,
  228. # and possible for PUT/DELETE.
  229. request_csrf_token = request.META.get(settings.CSRF_HEADER_NAME, '')
  230. request_csrf_token = _sanitize_token(request_csrf_token)
  231. if not _compare_salted_tokens(request_csrf_token, csrf_token):
  232. return self._reject(request, REASON_BAD_TOKEN)
  233. return self._accept(request)
  234. def process_response(self, request, response):
  235. if not getattr(request, 'csrf_cookie_needs_reset', False):
  236. if getattr(response, 'csrf_cookie_set', False):
  237. return response
  238. if not request.META.get("CSRF_COOKIE_USED", False):
  239. return response
  240. # Set the CSRF cookie even if it's already set, so we renew
  241. # the expiry timer.
  242. response.set_cookie(settings.CSRF_COOKIE_NAME,
  243. request.META["CSRF_COOKIE"],
  244. max_age=settings.CSRF_COOKIE_AGE,
  245. domain=settings.CSRF_COOKIE_DOMAIN,
  246. path=settings.CSRF_COOKIE_PATH,
  247. secure=settings.CSRF_COOKIE_SECURE,
  248. httponly=settings.CSRF_COOKIE_HTTPONLY
  249. )
  250. # Content varies with the CSRF cookie, so set the Vary header.
  251. patch_vary_headers(response, ('Cookie',))
  252. response.csrf_cookie_set = True
  253. return response