{"title":"跨站请求伪造保护","version":"4.1","locale":"zh-hans","docname":"ref/csrf","url":"/zh-hans/4.1/ref/csrf/","canonical":"https://djangodocs.dev/zh-hans/4.1/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>跨站请求伪造保护<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://www.squarefree.com/securitytips/web-developers.html#CSRF\">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 other\n'safe' methods, as defined by <span class=\"target\" id=\"index-0\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7231.html#section-4.2.1\"><strong>RFC 7231 Section 4.2.1</strong></a>) are side effect free.\nRequests via 'unsafe' methods, such as POST, PUT, and DELETE, can then be\nprotected by the steps outlined in <a class=\"reference internal\" href=\"/zh-hans/4.1/howto/csrf/#using-csrf\"><span class=\"std std-ref\">How to use Django's CSRF protection</span></a>.</p>\n<section id=\"how-it-works\">\n<span id=\"how-csrf-works\"></span><h2>工作方式<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>CSRF 保护是基于以下几点：</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>这一部分是由模板标签来完成的。</p>\n</li>\n<li><p>对于所有不使用 HTTP GET、HEAD、OPTIONS 或 TRACE 的传入请求，必须存在一个 CSRF cookie，并且“csrfmiddlewaretoken”字段必须存在且正确。如果不存在，用户将得到一个 403 错误。</p>\n<p>当验证“csrfmiddlewaretoken”字段值时，只有密钥，而不是完整的令牌，会与 cookie 值中的密钥进行比较。这允许使用不断变化的令牌。虽然每个请求都可能使用自己的令牌，但密钥对所有请求都是通用的。</p>\n<p>这个检查是由 <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> 根据当前主机和 <a class=\"reference internal\" href=\"/zh-hans/4.1/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> 的设置，验证 <a class=\"reference external\" href=\"https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin\">Origin header</a> ，如果是由浏览器提供的。这提供了对跨子域攻击的保护。</p></li>\n<li><p>此外，对于 HTTPS 请求，如果没有提供 <code class=\"docutils literal notranslate\"><span class=\"pre\">Origin</span></code> 头，<code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code> 会执行严格的来源检查。这意味着，即使一个子域可以设置或修改你的域名上的 cookie，它也不能强迫用户向你的应用程序发布，因为该请求不会来自你自己的确切域名。</p>\n<p>这也解决了在 HTTPS 下使用独立于会话的密钥时可能出现的中间人攻击问题，这是因为 HTTP <code class=\"docutils literal notranslate\"><span class=\"pre\">Set-Cookie</span></code> 头会被客户接受（不幸的是），即使他们在 HTTPS 下与一个网站对话。对 HTTP 请求不进行 Referer 检查，因为 HTTP 下 <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> 头的存在不够可靠）。</p>\n<p>如果设置了 <a class=\"reference internal\" href=\"/zh-hans/4.1/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> 设置，则会将 referer 与之进行比较。你可以通过包含一个前导点号来允许跨子域请求。例如，<code class=\"docutils literal notranslate\"><span class=\"pre\">CSRF_COOKIE_DOMAIN</span> <span class=\"pre\">=</span> <span class=\"pre\">'.example.com'</span></code> 将允许来自 <code class=\"docutils literal notranslate\"><span class=\"pre\">www.example.com</span></code> 和 <code class=\"docutils literal notranslate\"><span class=\"pre\">api.example.com</span></code> 的 POST 请求。如果没有设置，那么 referer 必须与 HTTP <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 头匹配。</p>\n<p>通过 <a class=\"reference internal\" href=\"/zh-hans/4.1/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> 设置，可以将接受的 referer 扩展到当前主机或 cookie 域之外。</p>\n</li>\n</ol>\n<aside class=\"version-note version-added\" data-version=\"4.0\">\n<p class=\"version-note-title\">New in Django 4.0</p><p>如上所述，增加了 <code class=\"docutils literal notranslate\"><span class=\"pre\">Origin</span></code> 检查。</p>\n</aside>\n<aside class=\"version-note version-changed\" data-version=\"4.1\">\n<p class=\"version-note-title\">Changed in Django 4.1</p><p>In older versions, the CSRF cookie value was masked.</p>\n</aside>\n<p>这确保了只有源自受信任域的表单才能用于 POST 回数据。</p>\n<p>它故意忽略了 GET 请求（以及被 <span class=\"target\" id=\"index-5\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7231.html#section-4.2.1\"><strong>RFC 7231 Section 4.2.1</strong></a> 定义为“安全”的其他请求）。<span class=\"target\" id=\"index-6\"></span><a class=\"rfc reference external\" href=\"https://datatracker.ietf.org/doc/html/rfc7231.html#section-4.2.1\"><strong>RFC 7231 Section 4.2.1</strong></a> 将 POST、PUT 和 DELETE 定义为“不安全”，所有其他方法也被认为是不安全的，以获得最大的保护。</p>\n<p>CSRF 保护不能防止中间人攻击，所以使用 <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/security/#security-recommendation-ssl\"><span class=\"std std-ref\">HTTPS</span></a> 与 <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/middleware/#http-strict-transport-security\"><span class=\"std std-ref\">HTTP 严格传输安全</span></a>。它还假设 <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/security/#host-headers-virtual-hosting\"><span class=\"std std-ref\">验证 HOST 头</span></a> 和你的网站上没有任何 <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/security/#cross-site-scripting\"><span class=\"std std-ref\">跨站脚本漏洞</span></a> （因为 XSS 漏洞已经让攻击者做了 CSRF 漏洞允许的任何事情，甚至更糟）。</p>\n<aside class=\"admonition-removing-the-referer-header admonition\">\n<p class=\"admonition-title\">删除 <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> 头</p>\n<p>为了避免向第三方网站透露 referrer URL，你可能想在你的网站的 <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;a&gt;</span></code> 标签上 <a class=\"reference external\" href=\"https://www.w3.org/TR/referrer-policy/#referrer-policy-delivery\">禁用 referrer</a> 。例如，你可以使用 <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> 标签或包含 <code class=\"docutils literal notranslate\"><span class=\"pre\">Referrer-Policy:</span> <span class=\"pre\">no-referrer</span></code> 头。由于 CSRF 保护对 HTTPS 请求进行严格的 referer 检查，这些技术会在使用“不安全”方法的请求上导致 CSRF 失败。取而代之的是，使用诸如 <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> 这样的替代品来链接第三方网站。</p>\n</aside>\n</section>\n<section id=\"limitations\">\n<span id=\"csrf-limitations\"></span><h2>限制<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 whole\ndomain. By setting the cookie and using a corresponding token, subdomains will\nbe able to circumvent the CSRF protection. The only way to avoid this is to\nensure that subdomains are controlled by trusted users (or, are at least unable\nto set cookies). Note that even without CSRF, there are other vulnerabilities,\nsuch as session fixation, that make giving subdomains to untrusted parties a bad\nidea, and these vulnerabilities cannot easily be fixed with current browsers.</p>\n</section>\n<section id=\"module-django.views.decorators.csrf\">\n<span id=\"utilities\"></span><h2>实用程序<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>下面的例子假设你使用的是基于函数的视图。如果你正在使用基于类的视图，你可以参考 <a class=\"reference internal\" href=\"/zh-hans/4.1/topics/class-based-views/intro/#id1\"><span class=\"std std-ref\">装饰基于类的视图</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>该装饰器标记着一个视图被免除了中间件所确保的保护。例如：</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<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=\"s1\">&#39;Hello world&#39;</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>为视图提供 <code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code> 保护的装饰器。</p>\n<p>用法：</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<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>通常情况下，如果 <code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware.process_view</span></code> 或类似 <code class=\"docutils literal notranslate\"><span class=\"pre\">csrf_protect</span></code> 这样的等价物没有运行， <a class=\"reference internal\" href=\"/zh-hans/4.1/ref/templates/builtins/#std-templatetag-csrf_token\"><code class=\"xref std std-ttag docutils literal notranslate\"><span class=\"pre\">csrf_token</span></code></a> 模板标签将无法工作。视图装饰器 <code class=\"docutils literal notranslate\"><span class=\"pre\">requires_csrf_token</span></code> 可以用来确保模板标签工作。这个装饰器的工作原理与 <code class=\"docutils literal notranslate\"><span class=\"pre\">csrf_protect</span></code> 类似，但绝不会拒绝接收到的请求。</p>\n<p>举例：</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<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>该装饰器强制视图发送 CSRF cookie。</p>\n</dd></dl>\n\n</section>\n<section id=\"settings\">\n<h2>配置<a class=\"heading-anchor\" href=\"#settings\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>一些配置可以用来控制Django 的 CSRF 行为：</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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=\"/zh-hans/4.1/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>常问问题<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>可以提交任意的 CSRF 令牌对（cookie 和 POST 数据）是漏洞吗？<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>不，这是设计好的。如果没有中间人攻击，攻击者就没有办法向受害者的浏览器发送 CSRF 令牌 cookie，所以成功的攻击需要通过 XSS 或类似的方式获得受害者浏览器的 cookie，在这种情况下，攻击者通常不需要 CSRF 攻击。</p>\n<p>一些安全审计工具将此标记为问题，但如前所述，攻击者无法窃取用户浏览器的 CSRF cookie。使用 Firebug、Chrome 开发工具等“窃取”或修改 <em>自己的</em> 令牌并不是漏洞。</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>Django 的 CSRF 保护默认不与会话关联，是不是有问题？<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>不，这是设计好的。不将 CSRF 保护与会话联系起来，就可以在诸如 <em>pastebin</em> 这样允许匿名用户提交的网站上使用保护，而这些用户并没有会话。</p>\n<p>如果你希望在用户的会话中存储 CSRF 令牌，请使用 <a class=\"reference internal\" href=\"/zh-hans/4.1/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>\n</section>\n<section id=\"why-might-a-user-encounter-a-csrf-validation-failure-after-logging-in\">\n<h3>为什么用户登录后会遇到 CSRF 验证失败？<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>出于安全考虑，每次用户登录时，CSRF 令牌都会轮换。任何在登录前生成表单的页面都会有一个旧的、无效的 CSRF 令牌，需要重新加载。如果用户在登录后使用后退按钮或在不同的浏览器标签页中登录，可能会发生这种情况。</p>\n</section>\n</section>","rootId":"module-django.middleware.csrf","toc":[{"title":"工作方式","anchor":"how-it-works","children":[]},{"title":"限制","anchor":"limitations","children":[]},{"title":"实用程序","anchor":"module-django.views.decorators.csrf","children":[]},{"title":"配置","anchor":"settings","children":[]},{"title":"常问问题","anchor":"frequently-asked-questions","children":[{"title":"可以提交任意的 CSRF 令牌对（cookie 和 POST 数据）是漏洞吗？","anchor":"is-posting-an-arbitrary-csrf-token-pair-cookie-and-post-data-a-vulnerability","children":[]},{"title":"Django 的 CSRF 保护默认不与会话关联，是不是有问题？","anchor":"is-it-a-problem-that-django-s-csrf-protection-isn-t-linked-to-a-session-by-default","children":[]},{"title":"为什么用户登录后会遇到 CSRF 验证失败？","anchor":"why-might-a-user-encounter-a-csrf-validation-failure-after-logging-in","children":[]}]}],"breadcrumbs":[{"docname":"ref/index","title":"API 参考","url":"/zh-hans/4.1/ref/"}],"prev":{"docname":"ref/contrib/syndication","title":"Feed 聚合框架","url":"/zh-hans/4.1/ref/contrib/syndication/"},"next":{"docname":"ref/databases","title":"数据库","url":"/zh-hans/4.1/ref/databases/"},"formats":{"html":"/zh-hans/4.1/ref/csrf/","markdown":"/zh-hans/4.1/ref/csrf.md","json":"/zh-hans/4.1/ref/csrf.json"},"source":"https://github.com/django/django/blob/stable/4.1.x/docs/ref/csrf.txt","official":"https://docs.djangoproject.com/zh-hans/4.1/ref/csrf/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}