<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[Content Security Policy 入门教程]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Mon, 21 Feb 2022 01:14:56 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	跨域脚本攻击 XSS 是最常见、危害最大的网页安全漏洞。<br/><a href="http://www.dzhope.com/attachment.php?fid=248" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=248" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>为了防止它们，要采取很多编程措施，非常麻烦。很多人提出，能不能根本上解决问题，浏览器自动禁止外部注入恶意脚本？<br/><br/>这就是"网页安全政策"（Content Security Policy，缩写 CSP）的来历。本文详细介绍如何使用 CSP 防止 XSS 攻击。<br/><a href="http://www.dzhope.com/attachment.php?fid=249" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=249" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>一、简介<br/>CSP 的实质就是白名单制度，开发者明确告诉客户端，哪些外部资源可以加载和执行，等同于提供白名单。它的实现和执行全部由浏览器完成，开发者只需提供配置。<br/><br/>CSP 大大增强了网页的安全性。攻击者即使发现了漏洞，也没法注入脚本，除非还控制了一台列入了白名单的可信主机。<br/><br/>两种方法可以启用 CSP。一种是通过 HTTP 头信息的<div class="code">Content-Security-Policy</div>的字段。<br/><br/><a href="http://www.dzhope.com/attachment.php?fid=250" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=250" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/><div class="code"><br/>Content-Security-Policy: script-src &#039;self&#039;; object-src &#039;none&#039;; style-src cdn.example.org third-party.org; child-src https:<br/></div><br/>另一种是通过网页的<div class="code">&lt;meta&gt;</div>标签。<br/><div class="code"><br/>&lt;meta http-equiv=&quot;Content-Security-Policy&quot; content=&quot;script-src &#039;self&#039;; object-src &#039;none&#039;; style-src cdn.example.org third-party.org; child-src https:&quot;&gt;<br/></div><br/>上面代码中，CSP 做了如下配置。<br/><div class="code"><br/>脚本：只信任当前域名<br/>&lt;object&gt;标签：不信任任何URL，即不加载任何资源<br/>样式表：只信任cdn.example.org和third-party.org<br/>框架（frame）：必须使用HTTPS协议加载<br/>其他资源：没有限制<br/>启用后，不符合 CSP 的外部资源就会被阻止加载。<br/></div><br/>Chrome 的报错信息。<br/><a href="http://www.dzhope.com/attachment.php?fid=251" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=251" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>Firefox 的报错信息。<br/><a href="http://www.dzhope.com/attachment.php?fid=252" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=252" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>二、限制选项<br/>CSP 提供了很多限制选项，涉及安全的各个方面。<br/><br/>2.1 资源加载限制<br/>以下选项限制各类资源的加载。<br/><div class="code"><br/>script-src：外部脚本<br/>style-src：样式表<br/>img-src：图像<br/>media-src：媒体文件（音频和视频）<br/>font-src：字体文件<br/>object-src：插件（比如 Flash）<br/>child-src：框架<br/>frame-ancestors：嵌入的外部资源（比如&lt;frame&gt;、&lt;iframe&gt;、&lt;embed&gt;和&lt;applet&gt;） <br/>connect-src：HTTP 连接（通过 XHR、WebSockets、EventSource等）<br/>worker-src：worker脚本<br/>manifest-src：manifest 文件<br/></div><br/>2.2 default-src<br/>default-src用来设置上面各个选项的默认值。<br/><div class="code"><br/>Content-Security-Policy: default-src &#039;self&#039;<br/></div><br/>上面代码限制所有的外部资源，都只能从当前域名加载。<br/><br/>如果同时设置某个单项限制（比如font-src）和default-src，前者会覆盖后者，即字体文件会采用font-src的值，其他资源依然采用default-src的值。<br/>2.3 URL 限制<br/>有时，网页会跟其他 URL 发生联系，这时也可以加以限制。<br/><div class="code"><br/>frame-ancestors：限制嵌入框架的网页<br/>base-uri：限制&lt;base#href&gt;<br/>form-action：限制&lt;form#action&gt;<br/></div><br/>2.4 其他限制<br/>其他一些安全相关的功能，也放在了 CSP 里面。<br/><div class="code"><br/>block-all-mixed-content：HTTPS 网页不得加载 HTTP 资源（浏览器已经默认开启）<br/>upgrade-insecure-requests：自动将网页上所有加载外部资源的 HTTP 链接换成 HTTPS 协议<br/>plugin-types：限制可以使用的插件格式<br/>sandbox：浏览器行为的限制，比如不能有弹出窗口等。<br/></div><br/>2.5 report-uri<br/>有时，我们不仅希望防止 XSS，还希望记录此类行为。report-uri就用来告诉浏览器，应该把注入行为报告给哪个网址。<br/><div class="code"><br/>Content-Security-Policy: default-src &#039;self&#039;; ...; report-uri /my_amazing_csp_report_parser;<br/></div><br/>上面代码指定，将注入行为报告给/my_amazing_csp_report_parser这个 URL。<br/><br/>浏览器会使用POST方法，发送一个JSON对象，下面是一个例子。<br/><div class="code"><br/>&#123;&nbsp;&nbsp; &quot;csp-report&quot;: &#123;&nbsp;&nbsp;&nbsp;&nbsp; &quot;document-uri&quot;: &quot;http://example.org/page.html&quot;,&nbsp;&nbsp;&nbsp;&nbsp; &quot;referrer&quot;: &quot;http://evil.example.com/&quot;,&nbsp;&nbsp;&nbsp;&nbsp; &quot;blocked-uri&quot;: &quot;http://evil.example.com/evil.js&quot;,&nbsp;&nbsp;&nbsp;&nbsp; &quot;violated-directive&quot;: &quot;script-src &#039;self&#039; https://apis.google.com&quot;,&nbsp;&nbsp;&nbsp;&nbsp; &quot;original-policy&quot;: &quot;script-src &#039;self&#039; https://apis.google.com; report-uri http://example.org/my_amazing_csp_report_parser&quot;&nbsp;&nbsp; &#125; &#125;<br/></div><br/><a href="http://www.dzhope.com/attachment.php?fid=253" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=253" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>三、Content-Security-Policy-Report-Only<br/>除了Content-Security-Policy，还有一个Content-Security-Policy-Report-Only字段，表示不执行限制选项，只是记录违反限制的行为。<br/><br/>它必须与report-uri选项配合使用。<br/><div class="code"><br/>Content-Security-Policy-Report-Only: default-src &#039;self&#039;; ...; report-uri /my_amazing_csp_report_parser;<br/></div><br/>四、选项值<br/>每个限制选项可以设置以下几种值，这些值就构成了白名单。 <br/><div class="code"><br/>主机名：example.org，https://example.com:443<br/>路径名：example.org/resources/js/<br/>通配符：*.example.org，*://*.example.com:*（表示任意协议、任意子域名、任意端口）<br/>协议名：https:、data:<br/>关键字&#039;self&#039;：当前域名，需要加引号<br/>关键字&#039;none&#039;：禁止加载任何外部资源，需要加引号<br/></div><br/>多个值也可以并列，用空格分隔。<br/><div class="code"><br/>Content-Security-Policy: script-src &#039;self&#039; https://apis.google.com<br/></div><br/>如果同一个限制选项使用多次，只有第一次会生效。<br/><div class="code"><br/># 错误的写法 script-src https://host1.com; script-src https://host2.com&nbsp;&nbsp;# 正确的写法 script-src https://host1.com https://host2.com<br/></div><br/>如果不设置某个限制选项，就是默认允许任何值。<br/>五、script-src 的特殊值<br/>除了常规值，script-src还可以设置一些特殊值。注意，下面这些值都必须放在单引号里面。<br/><div class="code"><br/>&#039;unsafe-inline&#039;：允许执行页面内嵌的&amp;lt;script&gt;标签和事件监听函数<br/>unsafe-eval：允许将字符串当作代码执行，比如使用eval、setTimeout、setInterval和Function等函数。<br/>nonce值：每次HTTP回应给出一个授权token，页面内嵌脚本必须有这个token，才会执行<br/>hash值：列出允许执行的脚本代码的Hash值，页面内嵌脚本的哈希值只有吻合的情况下，才能执行。<br/></div><br/>nonce值的例子如下，服务器发送网页的时候，告诉浏览器一个随机生成的token。<br/><div class="code"><br/>Content-Security-Policy: script-src &#039;nonce-EDNnf03nceIOfn39fn3e9h3sdfa&#039;<br/></div><br/>页面内嵌脚本，必须有这个token才能执行。<br/><div class="code"><br/>&lt;script nonce=EDNnf03nceIOfn39fn3e9h3sdfa&gt;&nbsp;&nbsp; // some code &lt;/script&gt;<br/></div><br/>hash值的例子如下，服务器给出一个允许执行的代码的hash值。<br/><div class="code"><br/>Content-Security-Policy: script-src &#039;sha256-qznLcsROx4GACP2dm0UCKCzCG-HiZ1guq6ZZDob_Tng=&#039;<br/></div><br/>下面的代码就会允许执行，因为hash值相符。<br/><div class="code"><br/>&lt;script&gt;alert(&#039;Hello, world.&#039;);&lt;/script&gt;<br/></div><br/>注意，计算hash值的时候，<div class="code">&lt;script&gt;</div>标签不算在内。<br/><br/>除了script-src选项，nonce值和hash值还可以用在<div class="code">style-src</div>选项，控制页面内嵌的样式表。<br/>六、注意点<br/>（1）<div class="code">script-src</div>和<div class="code">object-src</div>是必设的，除非设置了default-src。<br/><br/>因为攻击者只要能注入脚本，其他限制都可以规避。而object-src必设是因为 Flash 里面可以执行外部脚本。<br/><br/>（2）<div class="code">script-src</div>不能使用<div class="code">unsafe-inline</div>关键字（除非伴随一个nonce值），也不能允许设置data:URL。<br/><br/>下面是两个恶意攻击的例子。<br/><div class="code"><br/>&lt;img src=&quot;x&quot; onerror=&quot;evil()&quot;&gt; &lt;script src=&quot;data:text/javascript,evil()&quot;&gt;&lt;/script&gt;<br/></div><br/>（3）必须特别注意 JSONP 的回调函数。<br/><div class="code"><br/>&lt;script src=&quot;/path/jsonp?callback=alert(document.domain)//&quot;&gt; &lt;/script&gt;<br/></div><br/>上面的代码中，虽然加载的脚本来自当前域名，但是通过改写回调函数，攻击者依然可以执行恶意代码。<br/>转自：<a href="https://cloud.tencent.com/developer/article/1095692?from=15425" target="_blank">https://cloud.tencent.com/developer/article/1095692?from=15425</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] Content Security Policy 入门教程]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>