加载中...
<meta http-equiv="Content-Security-Policy" content="default-src *; child-src * 'self' blob: http:;img-src * 'self' data: http:; script-src 'self' 'unsafe-inline' 'unsafe-eval' ;style-src 'self' 'unsafe-inline' *">
指令名 | demo | 说明 |
default-src | 'self' cdn.example.com | 默认策略,可以应用于js文件/图片/css/ajax请求等所有访问 |
script-src | 'self' js.example.com | 定义js文件的过滤策略 |
style-src | 'self' css.example.com | 定义css文件的过滤策略 |
img-src | 'self' img.example.com | 定义图片文件的过滤策略 |
connect-src | 'self' | 定义请求连接文件的过滤策略 |
font-src | font.example.com | 定义字体文件的过滤策略 |
object-src | 'self' | 定义页面插件的过滤策略,如 <object>, <embed> 或者<applet>等元素 |
media-src | media.example.com | 定义媒体的过滤策略,如 HTML6的 <audio>, <video>等元素 |
frame-src | 'self' | 定义加载子frmae的策略 |
sandbox | allow-forms allow-scripts | 沙盒模式,会阻止页面弹窗/js执行等,你可以通过添加allow-forms allow-same-origin allow-scripts allow-popups, allow-modals, allow-orientation-lock, allow-pointer-lock, allow-presentation, allow-popups-to-escape-sandbox, and allow-top-navigation 策略来放开相应的操作 |
report-uri | /some-report-uri |
所有以-src结尾的指令都可以用一下的值来定义过滤规则,多个规则之间可以用空格来隔开
值 | demo | 说明 |
* | img-src * | 允许任意地址的url,但是不包括 blob: filesystem: schemes. |
'none' | object-src 'none' | 所有地址的咨询都不允许加载 |
'self' | script-src 'self' | 同源策略,即允许同域名同端口下,同协议下的请求 |
data: | img-src 'self' data: | 允许通过data来请求咨询 (比如用Base64 编码过的图片). |
domain.example.com | img-src domain.example.com | 允许特性的域名请求资源 |
*.example.com | img-src *.example.com | 允许从 example.com下的任意子域名加载资源 |
https://cdn.com | img-src https://cdn.com | 仅仅允许通过https协议来从指定域名下加载资源 |
https: | img-src https: | 只允许通过https协议加载资源 |
'unsafe-inline' | script-src 'unsafe-inline' | 允许行内代码执行 |
'unsafe-eval' | script-src 'unsafe-eval' | 允许不安全的动态代码执行,比如 JavaScript的 eval()方法 |
示例
default-src 'self';
只允许同源下的资源
script-src 'self';
只允许同源下的js
script-src 'self' www.google-analytics.com ajax.googleapis.com;
允许同源以及两个地址下的js加载
default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self';
多个资源时,后面的会覆盖前面的
服务器端配置
Apache服务
在VirtualHost的httpd.conf文件或者.htaccess文件中加入以下代码
Header set Content-Security-Policy "default-src 'self';"
Nginx
在 server {}对象块中添加如下代码
add_header Content-Security-Policy "default-src 'self';";
IIS
web.config:中添加
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Content-Security-Policy" value="default-src 'self';" />
</customHeaders>
</httpProtocol>
</system.webServer>
关注"都市百货" 了解南陵
管理员微信
评论已有 1 条