=====================================Cross Site Request Forgery protection=====================================.. module:: django.middleware.csrf:synopsis: Protects against Cross Site Request ForgeriesThe CSRF middleware and template tag provides easy-to-use protection against`Cross Site Request Forgeries`_. This type of attack occurs when a maliciouswebsite contains a link, a form button or some JavaScript that is intended toperform some action on your website, using the credentials of a logged-in userwho visits the malicious site in their browser. A related type of attack,'login CSRF', where an attacking site tricks a user's browser into logging intoa site with someone else's credentials, is also covered.The first defense against CSRF attacks is to ensure that GET requests (and other'safe' methods, as defined by :rfc:`7231#section-4.2.1`) are side effect free.Requests via 'unsafe' methods, such as POST, PUT, and DELETE, can then beprotected by the steps outlined in :ref:`using-csrf`... _Cross Site Request Forgeries: https://www.squarefree.com/securitytips/web-developers.html#CSRF.. _how-csrf-works:How it works============The CSRF protection is based on the following things:#. A CSRF cookie that is a random secret value, which other sites will not haveaccess to.``CsrfViewMiddleware`` sends this cookie with the response whenever``django.middleware.csrf.get_token()`` is called. It can also send it inother cases. For security reasons, the value of the secret is changed eachtime a user logs in.#. A hidden form field with the name 'csrfmiddlewaretoken', present in alloutgoing POST forms.In order to protect against `BREACH`_ attacks, the value of this field isnot simply the secret. It is scrambled differently with each response usinga mask. The mask is generated randomly on every call to ``get_token()``, sothe form field value is different each time.This part is done by the template tag.#. For all incoming requests that are not using HTTP GET, HEAD, OPTIONS orTRACE, a CSRF cookie must be present, and the 'csrfmiddlewaretoken' fieldmust be present and correct. If it isn't, the user will get a 403 error.When validating the 'csrfmiddlewaretoken' field value, only the secret,not the full token, is compared with the secret in the cookie value.This allows the use of ever-changing tokens. While each request may use itsown token, the secret remains common to all.This check is done by ``CsrfViewMiddleware``.#. ``CsrfViewMiddleware`` verifies the `Origin header`_, if provided by thebrowser, against the current host and the :setting:`CSRF_TRUSTED_ORIGINS`setting. This provides protection against cross-subdomain attacks.#. In addition, for HTTPS requests, if the ``Origin`` header isn't provided,``CsrfViewMiddleware`` performs strict referer checking. This means thateven if a subdomain can set or modify cookies on your domain, it can't forcea user to post to your application since that request won't come from yourown exact domain.This also addresses a man-in-the-middle attack that's possible under HTTPSwhen using a session independent secret, due to the fact that HTTP``Set-Cookie`` headers are (unfortunately) accepted by clients even whenthey are talking to a site under HTTPS. (Referer checking is not done forHTTP requests because the presence of the ``Referer`` header isn't reliableenough under HTTP.)If the :setting:`CSRF_COOKIE_DOMAIN` setting is set, the referer is comparedagainst it. You can allow cross-subdomain requests by including a leadingdot. For example, ``CSRF_COOKIE_DOMAIN = '.example.com'`` will allow POSTrequests from ``www.example.com`` and ``api.example.com``. If the setting isnot set, then the referer must match the HTTP ``Host`` header.Expanding the accepted referers beyond the current host or cookie domain canbe done with the :setting:`CSRF_TRUSTED_ORIGINS` setting... versionadded:: 4.0``Origin`` checking was added, as described above... versionchanged:: 4.1In older versions, the CSRF cookie value was masked.This ensures that only forms that have originated from trusted domains can beused to POST data back.It deliberately ignores GET requests (and other requests that are defined as'safe' by :rfc:`7231#section-4.2.1`). These requests ought never to have anypotentially dangerous side effects, and so a CSRF attack with a GET requestought to be harmless. :rfc:`7231#section-4.2.1` defines POST, PUT, and DELETEas 'unsafe', and all other methods are also assumed to be unsafe, for maximumprotection.The CSRF protection cannot protect against man-in-the-middle attacks, so use:ref:`HTTPS <security-recommendation-ssl>` with:ref:`http-strict-transport-security`. It also assumes :ref:`validation ofthe HOST header <host-headers-virtual-hosting>` and that there aren't any:ref:`cross-site scripting vulnerabilities <cross-site-scripting>` on your site(because XSS vulnerabilities already let an attacker do anything a CSRFvulnerability allows and much worse)... admonition:: Removing the ``Referer`` headerTo avoid disclosing the referrer URL to third-party sites, you might wantto `disable the referer`_ on your site's ``<a>`` tags. For example, youmight use the ``<meta name="referrer" content="no-referrer">`` tag orinclude the ``Referrer-Policy: no-referrer`` header. Due to the CSRFprotection's strict referer checking on HTTPS requests, those techniquescause a CSRF failure on requests with 'unsafe' methods. Instead, usealternatives like ``<a rel="noreferrer" ...>"`` for links to third-partysites... _BREACH: https://www.breachattack.com/.. _Origin header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin.. _disable the referer: https://www.w3.org/TR/referrer-policy/#referrer-policy-delivery.. _csrf-limitations:Limitations===========Subdomains within a site will be able to set cookies on the client for the wholedomain. By setting the cookie and using a corresponding token, subdomains willbe able to circumvent the CSRF protection. The only way to avoid this is toensure that subdomains are controlled by trusted users (or, are at least unableto set cookies). Note that even without CSRF, there are other vulnerabilities,such as session fixation, that make giving subdomains to untrusted parties a badidea, and these vulnerabilities cannot easily be fixed with current browsers.Utilities=========.. module:: django.views.decorators.csrfThe examples below assume you are using function-based views. If youare working with class-based views, you can refer to :ref:`Decoratingclass-based views<decorating-class-based-views>`... function:: csrf_exempt(view)This decorator marks a view as being exempt from the protection ensured bythe middleware. Example::from django.http import HttpResponsefrom django.views.decorators.csrf import csrf_exempt@csrf_exemptdef my_view(request):return HttpResponse('Hello world').. function:: csrf_protect(view)Decorator that provides the protection of ``CsrfViewMiddleware`` to a view.Usage::from django.shortcuts import renderfrom django.views.decorators.csrf import csrf_protect@csrf_protectdef my_view(request):c = {}# ...return render(request, "a_template.html", c).. function:: requires_csrf_token(view)Normally the :ttag:`csrf_token` template tag will not work if``CsrfViewMiddleware.process_view`` or an equivalent like ``csrf_protect``has not run. The view decorator ``requires_csrf_token`` can be used toensure the template tag does work. This decorator works similarly to``csrf_protect``, but never rejects an incoming request.Example::from django.shortcuts import renderfrom django.views.decorators.csrf import requires_csrf_token@requires_csrf_tokendef my_view(request):c = {}# ...return render(request, "a_template.html", c).. function:: ensure_csrf_cookie(view)This decorator forces a view to send the CSRF cookie.Settings========A number of settings can be used to control Django's CSRF behavior:* :setting:`CSRF_COOKIE_AGE`* :setting:`CSRF_COOKIE_DOMAIN`* :setting:`CSRF_COOKIE_HTTPONLY`* :setting:`CSRF_COOKIE_NAME`* :setting:`CSRF_COOKIE_PATH`* :setting:`CSRF_COOKIE_SAMESITE`* :setting:`CSRF_COOKIE_SECURE`* :setting:`CSRF_FAILURE_VIEW`* :setting:`CSRF_HEADER_NAME`* :setting:`CSRF_TRUSTED_ORIGINS`* :setting:`CSRF_USE_SESSIONS`Frequently Asked Questions==========================Is posting an arbitrary CSRF token pair (cookie and POST data) a vulnerability?-------------------------------------------------------------------------------No, this is by design. Without a man-in-the-middle attack, there is no way foran attacker to send a CSRF token cookie to a victim's browser, so a successfulattack would need to obtain the victim's browser's cookie via XSS or similar,in which case an attacker usually doesn't need CSRF attacks.Some security audit tools flag this as a problem but as mentioned before, anattacker cannot steal a user's browser's CSRF cookie. "Stealing" or modifying*your own* token using Firebug, Chrome dev tools, etc. isn't a vulnerability.Is it a problem that Django's CSRF protection isn't linked to a session by default?-----------------------------------------------------------------------------------No, this is by design. Not linking CSRF protection to a session allows usingthe protection on sites such as a *pastebin* that allow submissions fromanonymous users which don't have a session.If you wish to store the CSRF token in the user's session, use the:setting:`CSRF_USE_SESSIONS` setting.Why might a user encounter a CSRF validation failure after logging in?----------------------------------------------------------------------For security reasons, CSRF tokens are rotated each time a user logs in. Anypage with a form generated before a login will have an old, invalid CSRF tokenand need to be reloaded. This might happen if a user uses the back button aftera login or if they log in a different browser tab.