{"title":"Django 的安全性","version":"3.1","locale":"zh-hans","docname":"topics/security","url":"/zh-hans/3.1/topics/security/","canonical":"https://djangodocs.dev/zh-hans/3.1/topics/security/","summary":"此文档是对 Django 安全性特征的概述。它包含了对保障使用 Django 驱动的网页安全的建议。 防御跨站脚本攻击（XSS） Link to this heading # XSS…","html":"<h1>Django 的安全性<a class=\"heading-anchor\" href=\"#security-in-django\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>此文档是对 Django 安全性特征的概述。它包含了对保障使用 Django 驱动的网页安全的建议。</p>\n<section id=\"cross-site-scripting-xss-protection\">\n<span id=\"cross-site-scripting\"></span><h2>防御跨站脚本攻击（XSS）<a class=\"heading-anchor\" href=\"#cross-site-scripting-xss-protection\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>XSS 攻击让其使用者可以向其他用户的浏览器中注入客户端脚本。它通常由在数据库中存储恶意脚本的方式来实现，这些脚本会在数据库中被检索并显示给其他用户；或通过用户点击那些会引发攻击者的 JavaScript 脚本被用户浏览器执行的链接来实现。然而，倘若在数据加载到页面之前未经过彻底的清理，那么 XSS 攻击可以来自任何不可信任的数据源，比如 cookies 或者 Web 服务器。</p>\n<p>使用 Django 的模板可以保护您免受大多数XSS攻击。但是了解它提供了怎样的保护，以及有什么限制等是很重要的。</p>\n<p>Django 模板 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/templates/language/#automatic-html-escaping\"><span class=\"std std-ref\">escape specific characters</span></a> 对于 HTML 来说是很危险的。这保护着用户免受多数恶意输入的攻击，但并非万无一失。比如，出现下面这种情况就会保护失效：</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>&lt;style class={{ var }}&gt;...&lt;/style&gt;\n</code></pre></div>\n<p>如果 <code class=\"docutils literal notranslate\"><span class=\"pre\">var</span></code> 被设置为 <code class=\"docutils literal notranslate\"><span class=\"pre\">'class1</span> <span class=\"pre\">onmouseover=javascript:func()'</span></code> ，将导致未经授权的  JavaScript 脚本执行，这取决于浏览器如何呈现不完美的HTML。（引用属性值可以解决这个问题）</p>\n<p>同样重要的，还有在 <code class=\"docutils literal notranslate\"><span class=\"pre\">is_safe</span></code> 与自定义模板标签，<a class=\"reference internal\" href=\"/zh-hans/3.1/ref/templates/builtins/#std-templatefilter-safe\"><code class=\"xref std std-tfilter docutils literal notranslate\"><span class=\"pre\">safe</span></code></a> 模板标签，<a class=\"reference internal\" href=\"/zh-hans/3.1/ref/utils/#module-django.utils.safestring\" title=\"django.utils.safestring: Functions and classes for working with strings that can be displayed safely without further escaping in HTML.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">mark_safe</span></code></a> 一起设置时，以及关闭自动转义时要特别小心。</p>\n<p>此外，如果使用模板系统输出了除 HTML 之外的内容，可能会有完全独立的字符和单词需要转义。</p>\n<p>您在将 HTML 储存到数据库中时也要非常小心，特别是在检索和显示 HTML 的时候。</p>\n</section>\n<section id=\"cross-site-request-forgery-csrf-protection\">\n<h2>防御跨站点请求伪造（CSRF）<a class=\"heading-anchor\" href=\"#cross-site-request-forgery-csrf-protection\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>CSRF 攻击让恶意用户可以使用别的用户的证书执行操作，且是在其不知情或不同意的情况下。</p>\n<p>Django 已经内置了保护措施来对抗大多数 CSRF 攻击，您需要在合适的地方 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/csrf/#using-csrf\"><span class=\"std std-ref\">enabled and used it</span></a>  。但和多数缓解性技术一样，它是有局限性的。比如可以全局禁用 CSRF 模块或者特定的视图。如果您真的想这么做，请三思而后行。如果您的网页还有脱离您控制的子域，还将会有其他 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/csrf/#csrf-limitations\"><span class=\"std std-ref\">limitations</span></a> 。</p>\n<p><a class=\"reference internal\" href=\"/zh-hans/3.1/ref/csrf/#how-csrf-works\"><span class=\"std std-ref\">CSRF 保护机制</span></a> 通过检查每一个 POST 请求中的密文来实现。这保证恶意用户不能“复现”一个表单并用 POST 提交到你的网页，并让一个已登录用户无意中提交该表单。恶意用户必须知道特定于用户的密文（使用 cookie）。</p>\n<p>在部署 <a class=\"reference internal\" href=\"#security-recommendation-ssl\"><span class=\"std std-ref\">HTTPS</span></a> 时，<code class=\"docutils literal notranslate\"><span class=\"pre\">CsrfViewMiddleware</span></code> 会检查 HTTP 报文的 referer 首部是否设置为同源的 URL（包括子域和端口）。因为 HTTPS 提供了额外的安全性，所有通过转发不安全连接请求并在支持的浏览器中使用 HSTS 来确保连接在可用的地方使用了 HTTPS ，这一点是很重要的。</p>\n<p>除非绝对需要，否则对视图进行标记 <code class=\"docutils literal notranslate\"><span class=\"pre\">csrf_exempt</span></code> 装饰器时要极其慎重。</p>\n</section>\n<section id=\"sql-injection-protection\">\n<span id=\"id1\"></span><h2>防御 SQL 注入<a class=\"heading-anchor\" href=\"#sql-injection-protection\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>SQL 注入是一种让恶意用户能在数据库中执行任意 SQL 代码的攻击方式。这将导致记录被删除或泄露。</p>\n<p>Django 的 querysets 在被参数化查询构建出来时就被保护而免于 SQL 注入。查询的 SQL 代码与查询的参数是分开定义的。参数可能来自用户从而不安全，因此它们由底层数据库引擎进行转义。</p>\n<p>Django 也为开发者提供了书写 <a class=\"reference internal\" href=\"/zh-hans/3.1/topics/db/sql/#executing-raw-queries\"><span class=\"std std-ref\">raw queries</span></a> 或执行 <a class=\"reference internal\" href=\"/zh-hans/3.1/topics/db/sql/#executing-custom-sql\"><span class=\"std std-ref\">custom sql</span></a> 的权利。应当尽可能少地使用这些方法，并且您应该小心并准确的转义一切用户可控的参数。另外，在使用 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/models/querysets/#django.db.models.query.QuerySet.extra\" title=\"django.db.models.query.QuerySet.extra\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">extra()</span></code></a> 和 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/models/expressions/#django.db.models.expressions.RawSQL\" title=\"django.db.models.expressions.RawSQL\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RawSQL</span></code></a> 时应当小心谨慎。</p>\n</section>\n<section id=\"clickjacking-protection\">\n<h2>防御访问劫持<a class=\"heading-anchor\" href=\"#clickjacking-protection\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>访问劫持是一种让恶意网页能覆盖另一个网页框架的攻击方式。这将导致毫不知情的用户被骗入目标网页并执行意料之外的操作。</p>\n<p>Django 包含 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/clickjacking/#clickjacking-prevention\"><span class=\"std std-ref\">clickjacking protection</span></a> ，以 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/middleware/#django.middleware.clickjacking.XFrameOptionsMiddleware\" title=\"django.middleware.clickjacking.XFrameOptionsMiddleware\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">X-Frame-Options</span> <span class=\"pre\">middleware</span></code></a> 的形式在支持它的浏览器中阻止一个网页被渲染在 frame 的内部。可在每个视图的基础上禁用保护，也可配置发送的确切头部值。</p>\n<p>对于任何不会被第三方网站嵌入 frame 的网页，或者只允许使用一小部分的网页来说，强烈建议使用中间件。</p>\n</section>\n<section id=\"ssl-https\">\n<span id=\"security-recommendation-ssl\"></span><h2>SSL/HTTPS<a class=\"heading-anchor\" href=\"#ssl-https\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>将您的网页通过 HTTPS 部署，对于保障安全性来说是最佳的。没有它，恶意网络用户就可以在客户端和服务器之间嗅探验证资格或其他任何信息，在某些情况下 -- <strong>主动</strong> 网络攻击者 -- 会修改发向其中任何一方的数据。</p>\n<p>如果您想得到 HTTPS 的保护，且已经在您的服务器上启用了，下面还有一些额外的步骤需要执行：</p>\n<ul>\n<li><p>如果有必要，设置 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a>，确保您已经彻底的了解了它的警告。如果不这么做，将会导致 CSRF 漏洞，如果操作不正确，也是非常危险的。</p></li>\n<li><p>设置 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-SECURE_SSL_REDIRECT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_SSL_REDIRECT</span></code></a> 为  <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>，这样 HTTP 的请求就会被重定向到 HTTPS。</p>\n<p>请注意 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-SECURE_PROXY_SSL_HEADER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_PROXY_SSL_HEADER</span></code></a> 下的警告。对于反向代理，设置主服务器来重定向到 HTTPS 会更简单且更安全。</p>\n</li>\n<li><p>使用 'secure' cookies。</p>\n<p>如果浏览器使用默认的 HTTP 来实现初始连接，可能会导致已有的 cookies 泄露。因此，您应当将 <a class=\"reference internal\" href=\"/zh-hans/3.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> 和 <a class=\"reference internal\" href=\"/zh-hans/3.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> 设置为 <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>。这会让浏览器仅用 HTTPS 连接来发动这些 cookies。注意，这让 sessions 不能再通过 HTTP 工作，且 CSRF 防御机制将会阻止任何通过 HTTP 接收到的 POST 数据（当然把所有 HTTP 都弄成 HTTPS 是最好的）。</p>\n</li>\n<li><p>使用 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/middleware/#http-strict-transport-security\"><span class=\"std std-ref\">HTTP 严格传输安全</span></a> （HSTS）</p>\n<p>HSTS 是一个 HTTP 头部，它使得浏览器未来总是在连接到某特殊网页时使用 HTTPS。结合将请求从 HTTP 重定向到 HTTPS，一旦一个连接被成功建立，就能保证之后的连接总能受到 SSL 提供的额外安全保证。HSTS 或者在 Web 服务器上配置，或者通过 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-SECURE_HSTS_SECONDS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_SECONDS</span></code></a>， <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-SECURE_HSTS_INCLUDE_SUBDOMAINS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_INCLUDE_SUBDOMAINS</span></code></a>，以及 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-SECURE_HSTS_PRELOAD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECURE_HSTS_PRELOAD</span></code></a> 来进行配置。</p>\n</li>\n</ul>\n</section>\n<section id=\"host-header-validation\">\n<span id=\"host-headers-virtual-hosting\"></span><h2>Host 头部验证<a class=\"heading-anchor\" href=\"#host-header-validation\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>在某些情况下，Django 使用客户端提供的 <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 头部来构造 URLs。这些值虽被清理以阻止跨站脚本攻击，但伪造 <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 值还是可以用于跨站请求伪造，缓存毒化攻击，以及电子邮件中的有毒链接。</p>\n<p>因为即使看起来安全的服务器配置也容易受到假的 <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 头部信息的影响，Django 依靠定义在 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/request-response/#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">django.http.HttpRequest.get_host()</span></code></a> 方法中的 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ALLOWED_HOSTS</span></code></a> 来验证 <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 头部。</p>\n<p>这些验证仅通过 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/request-response/#django.http.HttpRequest.get_host\" title=\"django.http.HttpRequest.get_host\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">get_host()</span></code></a> 来实现；如果您的代码直接从 <code class=\"docutils literal notranslate\"><span class=\"pre\">request.META</span></code> 得到 <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 头部，您就绕过了这种安全保护机制。</p>\n<p>更多细节请参照完整的 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ALLOWED_HOSTS</span></code></a> 文档。</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Warning</p>\n<p>本文档以前的版本建议配置你的网络服务器，以确保它验证传入的 HTTP <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 头。虽然这个建议仍然有效，但在许多常见的 Web 服务器中，看似验证了 <code class=\"docutils literal notranslate\"><span class=\"pre\">Host</span></code> 头的配置，实际上可能并没有验证。例如，即使 Apache 的配置是让你的 Django 网站从一个非默认的虚拟主机上运行，并设置了 &quot;ServerName&quot;，HTTP 请求仍然有可能匹配这个虚拟主机，并提供一个假的 &quot;Host &quot; 头。因此，Django 现在要求你显式地设置 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-ALLOWED_HOSTS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ALLOWED_HOSTS</span></code></a>，而不是依赖 Web 服务器的配置。</p>\n</aside>\n<p>另外，如果您的配置有需求，Django 要求您明确启用对 <code class=\"docutils literal notranslate\"><span class=\"pre\">X-Forwarded-Host</span></code> 标头的支持（通过 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-USE_X_FORWARDED_HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USE_X_FORWARDED_HOST</span></code></a> 配置）。</p>\n</section>\n<section id=\"referrer-policy\">\n<h2>Referrer 策略<a class=\"heading-anchor\" href=\"#referrer-policy\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>浏览器使用 <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> 头部来把关于用户是如何到达哪里的信息发送到网页。通过设置 <em>Referrer 策略</em>，限制在哪些情况下设置 <code class=\"docutils literal notranslate\"><span class=\"pre\">Referer</span></code> 头部，您可以保护您的用户的隐私。请看 :ref:` 安全中间件参考书的 referrer 策略部分&lt;referrer-policy&gt;` 了解更多细节。</p>\n</section>\n<section id=\"session-security\">\n<h2>会话安全<a class=\"heading-anchor\" href=\"#session-security\"><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/3.1/ref/csrf/#csrf-limitations\"><span class=\"std std-ref\">CSRF 限制</span></a> 要求一个被部署的网页应让不受信任的用户不能访问任何子域，<a class=\"reference internal\" href=\"/zh-hans/3.1/topics/http/sessions/#module-django.contrib.sessions\" title=\"django.contrib.sessions: Provides session management for Django projects.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.sessions</span></code></a> 也有限制。参照 <a class=\"reference internal\" href=\"/zh-hans/3.1/topics/http/sessions/#topics-session-security\"><span class=\"std std-ref\">the session topic guide section on security</span></a> 获取更多细节。</p>\n</section>\n<section id=\"user-uploaded-content\">\n<span id=\"user-uploaded-content-security\"></span><h2>用户上传内容<a class=\"heading-anchor\" href=\"#user-uploaded-content\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Note</p>\n<p>考虑从云服务或 CDN 提供静态文件服务来避免此类问题。</p>\n</aside>\n<ul>\n<li><p>如果您的网站接受文件上传，强烈建议您在服务器配置中将这些上传限制在合理大小范围中，以此来防御拒绝服务（DOS）攻击。在 Apache 中，使用 <a class=\"reference external\" href=\"https://httpd.apache.org/docs/2.4/mod/core.html#limitrequestbody\">LimitRequestBody</a> 指令可以很容易地实现这个设置。</p></li>\n<li><p>如果您为自己的静态文件提供服务，确保像 Apache 的 <code class=\"docutils literal notranslate\"><span class=\"pre\">mod_php</span></code> 这种能把静态文件当作代码来执行的处理程序已经被关闭。您绝不会希望用户能够通过上传并请求特质文件来执行任意的代码。</p></li>\n<li><p>如果媒体文件没有遵循安全性最佳惯例，Django 的媒体上传处理会产生一些漏洞。特别的，如果一个 HTML 文件包含合法的 PNG 格式头部并附加一些恶意的 HTML，它是可以作为一个图片文件上传的。该文件将会通过 Django 用于 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/models/fields/#django.db.models.ImageField\" title=\"django.db.models.ImageField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ImageField</span></code></a> 图片处理（Pillow）库的验证。当此文件随后被展示给用户时，它可以被显示为 HTML，这取决于您的服务器类型于配置。</p>\n<p>在框架级别上没有防弹技术方案可以安全地验证所有用户上传的文件内容，但是您可以采取其他步骤来减轻这些攻击：</p>\n<ol class=\"arabic simple\">\n<li><p>通过一直为来自不同顶级域名或二级域名的用户提供上传内容可以防御一类的攻击。这可以防止被 <a class=\"reference external\" href=\"https://en.wikipedia.org/wiki/Same-origin_policy\">same-origin policy</a> 保护机制阻止的任何攻击，比如跨站脚本攻击。例如您的网站是 <code class=\"docutils literal notranslate\"><span class=\"pre\">example.com</span></code>，您应当通过形如 <code class=\"docutils literal notranslate\"><span class=\"pre\">usercontent-example.com</span></code> 的方式来提供上传内容服务（配置 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-MEDIA_URL\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MEDIA_URL</span></code></a>）。仅仅从像 <code class=\"docutils literal notranslate\"><span class=\"pre\">usercontent.example.com</span></code> 这样的子域提供内容是*不够*的。</p></li>\n<li><p>除此之外，应用可以选择定义一个列表来限制允许用户上传的文件的扩展名，并将 Web 服务器配置为仅为此类文件服务。</p></li>\n</ol>\n</li>\n</ul>\n</section>\n<section id=\"additional-security-topics\">\n<span id=\"id2\"></span><h2>其他安全性相关主题<a class=\"heading-anchor\" href=\"#additional-security-topics\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>尽管 Django 提供了开箱即用的良好安全保护，正确部署应用程序并利用 Web 服务器，操作系统和其他组件的安全保护仍然很重要。</p>\n<ul class=\"simple\">\n<li><p>确保您的 Python 代码位于 Web 服务器的根目录之外。这将确保您的 Python 代码不会意外地被用作纯文本（或意外地被执行）。</p></li>\n<li><p>小心一切 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/models/fields/#file-upload-security\"><span class=\"std std-ref\">用户上传的文件</span></a>。</p></li>\n<li><p>Django 不会限制对用户进行身份验证的请求。为了防止对身份验证系统的暴力攻击，您可以考虑部署 Django 插件或 Web 服务器模块来限制这些请求。</p></li>\n<li><p>保持 <a class=\"reference internal\" href=\"/zh-hans/3.1/ref/settings/#std-setting-SECRET_KEY\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">SECRET_KEY</span></code></a> 是密文。</p></li>\n<li><p>用防火墙限制缓存系统和数据库的可访问性是个好主意。</p></li>\n<li><p>看一下开源 Web 应用安全计划（OWASP） <a class=\"reference external\" href=\"https://owasp.org/www-project-top-ten/OWASP_Top_Ten_2017/\">Top 10 list</a> ，它指定了网络应用程序中一些常见的漏洞。尽管 Django 拥有解决某些问题的工具，但在项目设计中必须考虑其他问题。</p></li>\n<li><p>Mozilla 讨论了很多与 <a class=\"reference external\" href=\"https://infosec.mozilla.org/guidelines/web_security.html\">web security</a> 相关的主题。他们的网页还包括适用于任何系统的安全原则。</p></li>\n</ul>\n</section>","rootId":"security-in-django","toc":[{"title":"防御跨站脚本攻击（XSS）","anchor":"cross-site-scripting-xss-protection","children":[]},{"title":"防御跨站点请求伪造（CSRF）","anchor":"cross-site-request-forgery-csrf-protection","children":[]},{"title":"防御 SQL 注入","anchor":"sql-injection-protection","children":[]},{"title":"防御访问劫持","anchor":"clickjacking-protection","children":[]},{"title":"SSL/HTTPS","anchor":"ssl-https","children":[]},{"title":"Host 头部验证","anchor":"host-header-validation","children":[]},{"title":"Referrer 策略","anchor":"referrer-policy","children":[]},{"title":"会话安全","anchor":"session-security","children":[]},{"title":"用户上传内容","anchor":"user-uploaded-content","children":[]},{"title":"其他安全性相关主题","anchor":"additional-security-topics","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"使用 Django","url":"/zh-hans/3.1/topics/"}],"prev":{"docname":"topics/pagination","title":"分页","url":"/zh-hans/3.1/topics/pagination/"},"next":{"docname":"topics/performance","title":"性能和优化","url":"/zh-hans/3.1/topics/performance/"},"formats":{"html":"/zh-hans/3.1/topics/security/","markdown":"/zh-hans/3.1/topics/security.md","json":"/zh-hans/3.1/topics/security.json"},"source":"https://github.com/django/django/blob/stable/3.1.x/docs/topics/security.txt","official":"https://docs.djangoproject.com/zh-hans/3.1/topics/security/","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","pt-br","ko","es","el","pl"]}