aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/security/csp
diff options
context:
space:
mode:
Diffstat (limited to 'files/zh-cn/web/security/csp')
-rw-r--r--files/zh-cn/web/security/csp/index.html35
-rw-r--r--files/zh-cn/web/security/csp/introducing_content_security_policy/index.html55
-rw-r--r--files/zh-cn/web/security/csp/using_csp_violation_reports/index.html96
3 files changed, 186 insertions, 0 deletions
diff --git a/files/zh-cn/web/security/csp/index.html b/files/zh-cn/web/security/csp/index.html
new file mode 100644
index 0000000000..de5f82e383
--- /dev/null
+++ b/files/zh-cn/web/security/csp/index.html
@@ -0,0 +1,35 @@
+---
+title: 内容安全策略 (CSP)
+slug: Web/Security/CSP
+translation_of: Web/HTTP/CSP
+---
+<div>{{gecko_minversion_header("2.0")}}</div>
+
+<p><b>内容安全策略</b> (CSP, Content Security Policy) 是一个附加的安全层,用于帮助检测和缓解某些类型的攻击,包括跨站脚本 (XSS) 和数据注入等攻击。 这些攻击可用于实现从数据窃取到网站破坏或作为恶意软件分发版本等用途。</p>
+
+<p>尽管内容安全策略在 Firefox 4 中已经包含,使用 <code>X-Content-Security-Policy</code> 头部来实现,但它使用的是过时的 CSP 标准。Firefox 23 包含了更新的 CSP 实现,使用的是 W3C CSP 1.0 标准中描述的没有前缀的 <code>Content-Security-Policy</code> 头部和指令。</p>
+
+<h2 id="内容安全策略主题">内容安全策略主题</h2>
+
+<dl>
+ <dt><a href="/zh-CN/docs/Security/CSP/Introducing_Content_Security_Policy">介绍内容安全策略</a></dt>
+ <dd>概述什么是 CSP,以及它如何让你的网站变得更安全。</dd>
+ <dt><a href="/zh-CN/docs/Security/CSP/CSP_policy_directives">CSP 策略指令</a></dt>
+ <dd>CSP 策略指令参考。</dd>
+ <dt><a href="/zh-CN/docs/Security/CSP/Using_Content_Security_Policy">使用内容安全策略</a></dt>
+ <dd>你可以通过配置策略集调整 CSP 的行为。这让你可以按需对个别类型的资源使用宽松或严格的安全策略。这篇文章讲述了如何建立 CSP 和如何激活你网站的 CSP。</dd>
+ <dt><a href="/zh-CN/docs/Security/CSP/Using_CSP_violation_reports">使用 CSP 违规报告</a></dt>
+ <dd>如何使用 CSP 违规报告来监视攻击你网站的尝试和攻击者。</dd>
+ <dt><a href="/zh-CN/docs/Security/CSP/Default_CSP_restrictions">默认 CSP 限制 </a>{{obsolete_inline("15.0")}}</dt>
+ <dd>CSP 强制实施的默认限制的细节</dd>
+</dl>
+
+<h2 id="另请参阅">另请参阅</h2>
+
+<ul>
+ <li><a href="/zh-CN/docs/Security">Security</a></li>
+ <li><a href="/zh-CN/docs/HTTP_access_control">HTTP access control</a></li>
+ <li><a class="link-https" href="http://www.w3.org/TR/CSP/">CSP 1.0 specification</a></li>
+ <li><a class="link-https" href="https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html">CSP 1.1 specification</a></li>
+ <li><a href="/docs/Apps/CSP">CSP restrictions for Open Web Apps</a></li>
+</ul>
diff --git a/files/zh-cn/web/security/csp/introducing_content_security_policy/index.html b/files/zh-cn/web/security/csp/introducing_content_security_policy/index.html
new file mode 100644
index 0000000000..f730c260c6
--- /dev/null
+++ b/files/zh-cn/web/security/csp/introducing_content_security_policy/index.html
@@ -0,0 +1,55 @@
+---
+title: 内容安全策略介绍
+slug: Web/Security/CSP/Introducing_Content_Security_Policy
+tags:
+ - 介绍
+ - 内容安全策略
+ - 安全
+translation_of: Web/HTTP/CSP
+---
+<p>{{ gecko_minversion_header("2") }}</p>
+
+<p><strong>内容安全策略</strong> (CSP)  是一个附加的安全层,用于帮助检测和缓解某些类型的攻击,包括跨站脚本 (XSS) 和数据注入等攻击。 这些攻击可用于实现从数据窃取到网站破坏或作为恶意软件分发版本等用途。</p>
+
+<p>CSP被设计成向后兼容;不支持的浏览器依然可以运行使用了它的服务器页面,反之亦然。不支持CSP的浏览器会忽略它,像平常一样运行,默认对网页内容使用标准的同源策略。如果网站不提供CSP头部,浏览器同样会使用标准的<a href="/zh-CN/docs/Same_origin_policy_for_JavaScript">同源策略</a>。</p>
+
+<p>开启CSP就如配置您的页面服务来返回<code>Content-Security-Policy</code> HTTP 头部一样简单. (Firefox 23之前的版本使用的是<code>X-Content-Security-Policy</code> ). 查看 <a href="/en/Security/CSP/Using_Content_Security_Policy" title="en/Security/CSP/Using Content Security Policy">Using Content Security Policy</a> 获取如何配置和开启CSP的细节</p>
+
+<div class="note"><strong>提示:</strong> 内容安全策略<a class="link-https" href="https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html#meta-http-equiv--x-content-security-policy---html-element" title="https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html#meta-http-equiv--x-content-security-policy---html-element">标准特点</a> 是一种能被 {{ HTMLElement("meta") }}元素来配置的一种协议,但这种做法仍未被Firefox支持, 支持这种做法是被添加在<a class="link-https" href="https://bugzilla.mozilla.org/show_bug.cgi?id=663570" title="https://bugzilla.mozilla.org/show_bug.cgi?id=663570">bug 663570</a>.</div>
+
+<h2 id="减少跨域脚本">减少跨域脚本</h2>
+
+<p>CSP的主要目标是减少和报告XSS攻击. XSS攻击利用浏览器对从服务器接受的内容的信任。恶意的脚本在受害的浏览器被执行, 因为浏览器相信内容源,甚至当内容源并不是从它应该来的地方过来的。</p>
+
+<p>CSP使服务器管理员能够通过制定浏览器能够执行的可信赖脚本的域名来减少或者消除由XSS可能出现的矢量。 一个兼容CSP的浏览器将只会执行加载与白名单域名的源文件的脚本,忽略那些其他的脚本(包括内联脚本和事件操控HTML属性)</p>
+
+<p>作为一种最终的保护,想要禁止脚本的站点可以选择全局禁止脚本执行</p>
+
+<h2 id="减少数据包监听攻击">减少数据包监听攻击</h2>
+
+<p>为了重新约束内容被下载的域名, 服务端能够制定那种协议能够被使用;例如(理论上,从安全的立足点来看),一个服务制定所有的内容都通过HTTPS协议来加载</p>
+
+<div class="note"><strong>提示:</strong>一个完整地数据传输安全策略不单单包括通HTTPS加强数据的传输,还可以使所有cookie带上安全标志并且提供从HTTP页面到对应HTTPS页面的自动重定向。</div>
+
+<div class="note"><strong>提示:</strong>站点可能也会使用 <a href="/en/Security/HTTP_Strict_Transport_Security" title="/en/Security/HTTP_Strict_Transport_Security">Strict-Transport-Security</a> HTTP头部来确保浏览器只通过一个加密渠道来连接他们</div>
+
+<h2 id="See_also">See also</h2>
+
+<ul>
+ <li><a href="/en/Security/CSP/Using_Content_Security_Policy" title="en/Security/CSP/Using Content Security Policy">Using Content Security Policy</a></li>
+ <li><a href="/en/Security/CSP/CSP_policy_directives" title="en/Security/CSP/CSP policy directives">CSP policy directives</a></li>
+ <li><a href="/en/Security/CSP/Using_CSP_violation_reports" title="en/Security/CSP/Using CSP violation reports">Using CSP violation reports</a></li>
+</ul>
+
+<h2 id="Specification">Specification</h2>
+
+<ul>
+ <li>{{ spec("https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/latest.html", "Content Security Policy 1.1 (Editor's Draft)") }}</li>
+ <li>{{ spec("http://www.w3.org/TR/CSP/", "Content Security Policy 1.0 (Candidate Recommendation)") }}</li>
+</ul>
+
+<div class="noinclude">
+<p>{{ languages( { "ja": "ja/Introducing_Content_Security_Policy" } ) }}</p>
+</div>
+
+<p> </p>
diff --git a/files/zh-cn/web/security/csp/using_csp_violation_reports/index.html b/files/zh-cn/web/security/csp/using_csp_violation_reports/index.html
new file mode 100644
index 0000000000..decd9db66f
--- /dev/null
+++ b/files/zh-cn/web/security/csp/using_csp_violation_reports/index.html
@@ -0,0 +1,96 @@
+---
+title: Using CSP violation reports
+slug: Web/Security/CSP/Using_CSP_violation_reports
+translation_of: Web/HTTP/CSP
+---
+<p>{{ gecko_minversion_header("2.0") }}{{ draft() }}</p>
+
+<p>CSP其中的一个强大的特性是它为web站点管理员提供了生成和报告详细的站点攻击的描述信息。这些报告通过HTTP的POST请求发送到预先你指定的一个或多个服务器上,这些服务器可以通过 <a href="/en/Security/CSP/CSP_policy_directives#report-uri" title="en/Security/CSP/CSP policy directives#report-uri"><code>report-uri</code></a> 策略来制定。发送的报告是JSON格式的,对于非ASCII字符,其使用了默认的JSON UTF-8编码。本文将向你展示如何在你的站点上配置报告,以及报告的格式。</p>
+
+<p>对于支持CSP的浏览器,只要你制定了合法的 <a href="/en/Security/CSP/CSP_policy_directives#report-uri" style="text-decoration: underline;" title="en/Security/CSP/CSP policy directives#report-uri"><code>report-uri</code></a> 指令,任何违背该策略的攻击操作都会被浏览器所报告。</p>
+
+<h2 id="开启报告">开启报告</h2>
+
+<p>默认情况下,攻击是不会被报告的。要开启报告模式,你要指定 <a href="/en/Security/CSP/CSP_policy_directives#report-uri" title="en/Security/CSP/CSP policy directives#report-uri"><code>report-uri</code></a> 指令,这个指令包含了至少一个用于接收报告的URI:</p>
+
+<pre>Content-Security-Policy: default-src self; report-uri http://reportcollector.example.com/collector.cgi
+</pre>
+
+<p>然后,你要搭建接受报告的服务器;它可以对报告进行存储和并进行适时的处理。</p>
+
+<p>注意:Firefox 23以前的版本所使用的报告头为 X-Content-Security-Policy。这个头在将来将被废弃。</p>
+
+<h2 id="攻击报告的语法">攻击报告的语法</h2>
+
+<p>报告的JSON对象包含了以下的数据:</p>
+
+<dl>
+ <dt><code>document-uri</code></dt>
+ <dd>当前攻击所发生的文档的URI。</dd>
+ <dt><code>referrer</code></dt>
+ <dd>当前攻击所发生的文档的来源页面的URI。</dd>
+ <dt><code>blocked-uri</code></dt>
+ <dd>被CSP策略所拦截的资源的URI。如果被拦截资源的URI属于与当前文档不同的来源,则所拦截的资源URI会被削减至只剩scheme,host和port三部分。</dd>
+ <dt><code>violated-directive</code></dt>
+ <dd>攻击所针对的策略部分的名称</dd>
+ <dt><code>original-policy</code></dt>
+ <dd>由X-Content-Security-Policy头指定的原始策略。</dd>
+</dl>
+
+<h2 id="Sample_violation_report">Sample violation report</h2>
+
+<h2 id="攻击报告的样例">攻击报告的样例</h2>
+
+<div>在CSP规范中,已经有一个样例展示攻击报告。这里我们来看另一个例子。</div>
+
+<div> </div>
+
+<div>假设有一个页面叫做<a class="external" href="http://example.com/signup.html" rel="freelink" style="font-family: Consolas, Monaco, 'Andale Mono', monospace;">http://example.com/signup.html</a>. 它使用了一下的策略,它禁止从除cdn.example.com以外的域加载样式表。</div>
+
+<div>
+<pre>Content-Security-Policy: default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports
+</pre>
+</div>
+
+<div>signup.html的HTML则像下面这样:</div>
+
+<pre class="brush: html">&lt;!DOCTYPE html&gt;
+&lt;html&gt;
+ &lt;head&gt;
+ &lt;title&gt;Sign Up&lt;/title&gt;
+ &lt;link rel="stylesheet" href="css/style.css"&gt;
+ &lt;/head&gt;
+ &lt;body&gt;
+ ... Content ...
+ &lt;/body&gt;
+&lt;/html&gt;
+</pre>
+
+<div><span style="line-height: 22.0080013275147px;">看到问题了么?在策略中指明样式表只能从cdn.example.com加载,而这个页面则试图从它本域(http://example.com)下载样式表. 浏览器基于强制CSP的开启,在这个页面被访问时,将以下的报告通过POST请求发送到 </span><a href="http://example.com/_/csp-reports" rel="freelink" style="font-family: Consolas, Monaco, 'Andale Mono', monospace;">http://example.com/_/csp-reports</a><span style="line-height: 22.0080013275147px;"> 下</span></div>
+
+<pre>{
+ "csp-report": {
+ "document-uri": "http://example.com/signup.html",
+ "referrer": "",
+ "blocked-uri": "http://example.com/css/style.css",
+ "violated-directive": "style-src cdn.example.com",
+ "original-policy": "default-src 'none'; style-src cdn.example.com; report-uri /_/csp-reports"
+ }
+}
+</pre>
+
+<p><span style="line-height: 22.0080013275147px;">我们可以看到,这个报告在blocked-uri中包含了错误资源的整个路径。而这并非总是这样。例如,当signup.html试图从</span><a href="http://anothercdn.example.com/stylesheet.css" style="text-decoration: underline;"><code>http://anothercdn.example.com/stylesheet.css</code></a><span style="line-height: 22.0080013275147px;">加载css的时候,浏览器只会记录来源(http://anothercdn.example.com)而不会记录完整的路径。CSP规范中对这一行为进行了</span><a href="http://www.w3.org/TR/CSP/#violation-reports">解释</a>。总结一下<span style="line-height: 22.0080013275147px;">,CSP的作用是放置跨域信息的泄露。值得注意的是,规范中的</span> <a href="http://www.w3.org/TR/CSP/#sample-violation-report">攻击报告范例</a> 有一处错误(其中blocked-uri应该是"http://evil.example.com")。</p>
+
+<h2 id="更多参考">更多参考</h2>
+
+<ul>
+ <li><a href="/en/Security/CSP/Introducing_Content_Security_Policy" title="en/Security/CSP/Introducing Content Security Policy">Introducing Content Security Policy</a></li>
+ <li><a href="/en/Security/CSP/Using_Content_Security_Policy" title="en/Security/CSP/Using Content Security Policy">Using Content Security Policy</a></li>
+ <li><a href="/en/Security/CSP/CSP_policy_directives" title="en/Security/CSP/CSP policy directives">CSP policy directives</a></li>
+</ul>
+
+<div class="noinclude">
+<p>{{ languages( { "ja": "ja/Security/CSP/Using_CSP_violation_reports" } ) }}</p>
+</div>
+
+<p> </p>