{"title":"Cross Site Request Forgery protection","version":"6.0","locale":"it","docname":"ref/csrf","url":"/it/6.0/ref/csrf/","canonical":"https://djangodocs.dev/it/6.0/ref/csrf/","summary":"The CSRF middleware and template tag provides easy-to-use protection against Cross Site Request Forgeries . This type of attack occurs when a malicious website…","html":"<span id=\"cross-site-request-forgery-protection\"></span><h1>Cross Site Request Forgery protection<a class=\"heading-anchor\" href=\"#module-django.middleware.csrf\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>The CSRF middleware and template tag provides easy-to-use protection against\n<a class=\"reference external\" href=\"https://owasp.org/www-community/attacks/csrf#overview\">Cross Site Request Forgeries</a>. This type of attack occurs when a malicious\nwebsite contains a link, a form button or some JavaScript that is intended to\nperform some action on your website, using the credentials of a logged-in user\nwho visits the malicious site in their browser. A related type of attack,\n“login CSRF”, where an attacking site tricks a user’s browser into logging into\na site with someone else’s credentials, is also covered.</p>\n<p>The first defense against CSRF attacks is to ensure that GET requests (and\nother “safe” methods, as defined by <span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-9.2.1\"><strong>RFC 9110 Section 9.2.1</strong></a>) are side effect\nfree. Requests via “unsafe” methods, such as POST, PUT, and DELETE, can then be\nprotected by the steps outlined in <a class=\"reference internal\" href=\"/it/6.0/howto/csrf/#using-csrf\"><span class=\"std std-ref\">Come usare la protezione CSRF di Django</span></a>.</p>\n<section id=\"how-it-works\">\n<span id=\"how-csrf-works\"></span><h2>How it works<a class=\"heading-anchor\" href=\"#how-it-works\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The CSRF protection is based on the following things:</p>\n<ol class=\"arabic\">\n<li><p>A CSRF cookie that is a random secret value, which other sites will not have\naccess to.</p>\n<p><code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code> sends this cookie with the response whenever\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.middleware.csrf.get_token()</span></code> is called. It can also send it in\nother cases. For security reasons, the value of the secret is changed each\ntime a user logs in.</p>\n</li>\n<li><p>A hidden form field with the name “csrfmiddlewaretoken”, present in all\noutgoing POST forms.</p>\n<p>In order to protect against <a class=\"reference external\" href=\"https://www.breachattack.com/\">BREACH</a> attacks, the value of this field is\nnot simply the secret. It is scrambled differently with each response using\na mask. The mask is generated randomly on every call to <code class=\"docutils literal notranslate\"><span class=\"pre\">get_token()</span></code>, so\nthe form field value is different each time.</p>\n<p>This part is done by the <a class=\"reference internal\" href=\"/it/6.0/ref/templates/builtins/#std-templatetag-csrf_token\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">csrf_token</span></code></a> template tag.</p>\n</li>\n<li><p>For all incoming requests that are not using HTTP GET, HEAD, OPTIONS or\nTRACE, a CSRF cookie must be present, and the “csrfmiddlewaretoken” field\nmust be present and correct. If it isn’t, the user will get a 403 error.</p>\n<p>When validating the “csrfmiddlewaretoken” field value, only the secret,\nnot the full token, is compared with the secret in the cookie value.\nThis allows the use of ever-changing tokens. While each request may use its\nown token, the secret remains common to all.</p>\n<p>This check is done by <code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code>.</p>\n</li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code> verifies the <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin\">Origin header</a>, if provided by the\nbrowser, against the current host and the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_TRUSTED_ORIGINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_TRUSTED_ORIGINS</span></code></a>\nsetting. This provides protection against cross-subdomain attacks.</p></li>\n<li><p>In addition, for HTTPS requests, if the <code class=\"docutils literal notranslate\"><span class=\"pre\">Origin</span></code> header isn’t provided,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code> performs strict referer checking. This means that\neven if a subdomain can set or modify cookies on your domain, it can’t force\na user to post to your application since that request won’t come from your\nown exact domain.</p>\n<p>This also addresses a man-in-the-middle attack that’s possible under HTTPS\nwhen using a session independent secret, due to the fact that HTTP\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Set-Cookie</span></code> headers are (unfortunately) accepted by clients even when\nthey are talking to a site under HTTPS. (Referer checking is not done for\nHTTP requests because the presence of the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header isn’t reliable\nenough under HTTP.)</p>\n<p>If the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_DOMAIN\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_DOMAIN</span></code></a> setting is set, the referer is compared\nagainst it. You can allow cross-subdomain requests by including a leading\ndot. For example, <code class=\"docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_DOMAIN</span> <span class=\"pre\">=</span> <span class=\"pre\">'.example.com'</span></code> will allow POST\nrequests from <code class=\"docutils literal notranslate\"><span class=\"pre\">www.example.com</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">api.example.com</span></code>. If the setting is\nnot set, then the referer must match the HTTP <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> header.</p>\n<p>Expanding the accepted referers beyond the current host or cookie domain can\nbe done with the <a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_TRUSTED_ORIGINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_TRUSTED_ORIGINS</span></code></a> setting.</p>\n</li>\n</ol>\n<p>This ensures that only forms that have originated from trusted domains can be\nused to POST data back.</p>\n<p>It deliberately ignores GET requests (and other requests that are defined as\n“safe” by <span class=\"target\" id=\"index-1\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-9.2.1\"><strong>RFC 9110 Section 9.2.1</strong></a>). These requests ought never to have any\npotentially dangerous side effects, and so a CSRF attack with a GET request\nought to be harmless. <span class=\"target\" id=\"index-2\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc9110.html#section-9.2.1\"><strong>RFC 9110 Section 9.2.1</strong></a> defines POST, PUT, and DELETE\nas “unsafe”, and all other methods are also assumed to be unsafe, for maximum\nprotection.</p>\n<p>The CSRF protection cannot protect against man-in-the-middle attacks, so use\n<a class=\"reference internal\" href=\"/it/6.0/topics/security/#security-recommendation-ssl\"><span class=\"std std-ref\">HTTPS</span></a> with\n<a class=\"reference internal\" href=\"/it/6.0/ref/middleware/#http-strict-transport-security\"><span class=\"std std-ref\">HTTP Strict Transport Security</span></a>. It also assumes <a class=\"reference internal\" href=\"/it/6.0/topics/security/#host-headers-virtual-hosting\"><span class=\"std std-ref\">validation of\nthe HOST header</span></a> and that there aren’t any\n<a class=\"reference internal\" href=\"/it/6.0/topics/security/#cross-site-scripting\"><span class=\"std std-ref\">cross-site scripting vulnerabilities</span></a> on your site\n(because XSS vulnerabilities already let an attacker do anything a CSRF\nvulnerability allows and much worse).</p>\n<aside class=\"admonition-removing-the-referer-header admonition\">\n<p class=\"admonition-title\">Removing the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> header</p>\n<p>To avoid disclosing the referrer URL to third-party sites, you might want\nto <a class=\"reference external\" href=\"https://www.w3.org/TR/referrer-policy/#referrer-policy-delivery\">disable the referer</a> on your site’s <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;a&gt;</span></code> tags. For example, you\nmight use the <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;meta</span> <span class=\"pre\">name=&quot;referrer&quot;</span> <span class=\"pre\">content=&quot;no-referrer&quot;&gt;</span></code> tag or\ninclude the <code class=\"docutils literal notranslate\"><span class=\"pre\">Referrer-Policy:</span> <span class=\"pre\">no-referrer</span></code> header. Due to the CSRF\nprotection’s strict referer checking on HTTPS requests, those techniques\ncause a CSRF failure on requests with “unsafe” methods. Instead, use\nalternatives like <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;a</span> <span class=\"pre\">rel=&quot;noreferrer&quot;</span> <span class=\"pre\">...&gt;&quot;</span></code> for links to third-party\nsites.</p>\n</aside>\n</section>\n<section id=\"limitations\">\n<span id=\"csrf-limitations\"></span><h2>Limitations<a class=\"heading-anchor\" href=\"#limitations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Subdomains within a site will be able to set cookies on the client for the\nwhole domain. By setting the cookie and using a corresponding token, subdomains\nwill be able to circumvent the CSRF protection. The only way to avoid this is\nto ensure that subdomains are controlled by trusted users (or, are at least\nunable to set cookies). Note that even without CSRF, there are other\nvulnerabilities, such as session fixation, that make giving subdomains to\nuntrusted parties a bad idea, and these vulnerabilities cannot easily be fixed\nwith current browsers.</p>\n</section>\n<section id=\"module-django.views.decorators.csrf\">\n<span id=\"utilities\"></span><h2>Utilities<a class=\"heading-anchor\" href=\"#module-django.views.decorators.csrf\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>The examples below assume you are using function-based views. If you\nare working with class-based views, you can refer to <a class=\"reference internal\" href=\"/it/6.0/topics/class-based-views/intro/#id1\"><span class=\"std std-ref\">Decorating\nclass-based views</span></a>.</p>\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csrf.csrf_exempt\">\n<span class=\"sig-name descname\"><span class=\"pre\">csrf_exempt</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">view</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csrf.csrf_exempt\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This decorator marks a view as being exempt from the protection ensured by\nthe middleware. Example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.http</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">HttpResponse</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.csrf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">csrf_exempt</span>\n\n\n<span class=\"nd\">@csrf_exempt</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"k\">return</span> <span class=\"n\">HttpResponse</span><span class=\"p\">(</span><span class=\"s2\">&quot;Hello world&quot;</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csrf.csrf_protect\">\n<span class=\"sig-name descname\"><span class=\"pre\">csrf_protect</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">view</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csrf.csrf_protect\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Decorator that provides the protection of\n<a class=\"reference internal\" href=\"/it/6.0/ref/middleware/#django.middleware.csrf.CsrfViewMiddleware\" title=\"django.middleware.csrf.CsrfViewMiddleware\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code></a> to a view.</p>\n<p>Usage:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.shortcuts</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">render</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.csrf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">csrf_protect</span>\n\n\n<span class=\"nd\">@csrf_protect</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"n\">c</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">return</span> <span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"s2\">&quot;a_template.html&quot;</span><span class=\"p\">,</span> <span class=\"n\">c</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csrf.requires_csrf_token\">\n<span class=\"sig-name descname\"><span class=\"pre\">requires_csrf_token</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">view</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csrf.requires_csrf_token\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>Normally the <a class=\"reference internal\" href=\"/it/6.0/ref/templates/builtins/#std-templatetag-csrf_token\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">csrf_token</span></code></a> template tag will not work if\n<code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware.process_view</span></code> or an equivalent like <code class=\"docutils literal notranslate\"><span class=\"pre\">csrf_protect</span></code>\nhas not run. The view decorator <code class=\"docutils literal notranslate\"><span class=\"pre\">requires_csrf_token</span></code> can be used to\nensure the template tag does work. This decorator works similarly to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">csrf_protect</span></code>, but never rejects an incoming request.</p>\n<p>Example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.shortcuts</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">render</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.views.decorators.csrf</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">requires_csrf_token</span>\n\n\n<span class=\"nd\">@requires_csrf_token</span>\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">my_view</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">):</span>\n    <span class=\"n\">c</span> <span class=\"o\">=</span> <span class=\"p\">{}</span>\n    <span class=\"c1\"># ...</span>\n    <span class=\"k\">return</span> <span class=\"n\">render</span><span class=\"p\">(</span><span class=\"n\">request</span><span class=\"p\">,</span> <span class=\"s2\">&quot;a_template.html&quot;</span><span class=\"p\">,</span> <span class=\"n\">c</span><span class=\"p\">)</span>\n</code></pre></div>\n</dd></dl>\n\n<dl class=\"py function\">\n<dt class=\"sig sig-object py\" id=\"django.views.decorators.csrf.ensure_csrf_cookie\">\n<span class=\"sig-name descname\"><span class=\"pre\">ensure_csrf_cookie</span></span><span class=\"sig-paren\">(</span><em class=\"sig-param\"><span class=\"n\"><span class=\"pre\">view</span></span></em><span class=\"sig-paren\">)</span><a class=\"heading-anchor\" href=\"#django.views.decorators.csrf.ensure_csrf_cookie\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd><p>This decorator forces a view to send the CSRF cookie.</p>\n</dd></dl>\n\n</section>\n<section id=\"settings\">\n<h2>Settings<a class=\"heading-anchor\" href=\"#settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>A number of settings can be used to control Django’s CSRF behavior:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_AGE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_AGE</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_DOMAIN\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_DOMAIN</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_HTTPONLY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_HTTPONLY</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_NAME</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_PATH\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_PATH</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_SAMESITE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_SAMESITE</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_COOKIE_SECURE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_SECURE</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_FAILURE_VIEW\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_FAILURE_VIEW</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_HEADER_NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_HEADER_NAME</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_TRUSTED_ORIGINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_TRUSTED_ORIGINS</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a></p></li>\n</ul>\n</section>\n<section id=\"frequently-asked-questions\">\n<h2>Frequently Asked Questions<a class=\"heading-anchor\" href=\"#frequently-asked-questions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<section id=\"is-posting-an-arbitrary-csrf-token-pair-cookie-and-post-data-a-vulnerability\">\n<h3>Is posting an arbitrary CSRF token pair (cookie and POST data) a vulnerability?<a class=\"heading-anchor\" href=\"#is-posting-an-arbitrary-csrf-token-pair-cookie-and-post-data-a-vulnerability\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>No, this is by design. Without a man-in-the-middle attack, there is no way for\nan attacker to send a CSRF token cookie to a victim’s browser, so a successful\nattack would need to obtain the victim’s browser’s cookie via XSS or similar,\nin which case an attacker usually doesn’t need CSRF attacks.</p>\n<p>Some security audit tools flag this as a problem but as mentioned before, an\nattacker cannot steal a user’s browser’s CSRF cookie. «Stealing» or modifying\n<em>your own</em> token using Firebug, Chrome dev tools, etc. isn’t a vulnerability.</p>\n</section>\n<section id=\"is-it-a-problem-that-django-s-csrf-protection-isn-t-linked-to-a-session-by-default\">\n<h3>Is it a problem that Django’s CSRF protection isn’t linked to a session by default?<a class=\"heading-anchor\" href=\"#is-it-a-problem-that-django-s-csrf-protection-isn-t-linked-to-a-session-by-default\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>No, this is by design. Not linking CSRF protection to a session allows using\nthe protection on sites such as a <em>pastebin</em> that allow submissions from\nanonymous users which don’t have a session.</p>\n<p>If you wish to store the CSRF token in the user’s session, use the\n<a class=\"reference internal\" href=\"/it/6.0/ref/settings/#std-setting-CSRF_USE_SESSIONS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">CSRF_USE_SESSIONS</span></code></a> setting.</p>\n</section>\n<section id=\"why-might-a-user-encounter-a-csrf-validation-failure-after-logging-in\">\n<h3>Why might a user encounter a CSRF validation failure after logging in?<a class=\"heading-anchor\" href=\"#why-might-a-user-encounter-a-csrf-validation-failure-after-logging-in\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>For security reasons, CSRF tokens are rotated each time a user logs in. Any\npage with a form generated before a login will have an old, invalid CSRF token\nand need to be reloaded. This might happen if a user uses the back button after\na login or if they log in a different browser tab.</p>\n</section>\n</section>","rootId":"module-django.middleware.csrf","toc":[{"title":"How it works","anchor":"how-it-works","children":[]},{"title":"Limitations","anchor":"limitations","children":[]},{"title":"Utilities","anchor":"module-django.views.decorators.csrf","children":[]},{"title":"Settings","anchor":"settings","children":[]},{"title":"Frequently Asked Questions","anchor":"frequently-asked-questions","children":[{"title":"Is posting an arbitrary CSRF token pair (cookie and POST data) a vulnerability?","anchor":"is-posting-an-arbitrary-csrf-token-pair-cookie-and-post-data-a-vulnerability","children":[]},{"title":"Is it a problem that Django’s CSRF protection isn’t linked to a session by default?","anchor":"is-it-a-problem-that-django-s-csrf-protection-isn-t-linked-to-a-session-by-default","children":[]},{"title":"Why might a user encounter a CSRF validation failure after logging in?","anchor":"why-might-a-user-encounter-a-csrf-validation-failure-after-logging-in","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API Reference","url":"/it/6.0/ref/"}],"prev":{"docname":"ref/csp","title":"Content Security Policy","url":"/it/6.0/ref/csp/"},"next":{"docname":"ref/databases","title":"Databases","url":"/it/6.0/ref/databases/"},"formats":{"html":"/it/6.0/ref/csrf/","markdown":"/it/6.0/ref/csrf.md","json":"/it/6.0/ref/csrf.json"},"source":"https://github.com/django/django/blob/stable/6.0.x/docs/ref/csrf.txt","official":"https://docs.djangoproject.com/it/6.0/ref/csrf/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}