CSP内容安全策略
在浏览网页的过程中,尤其是移动端的网页,经常看到有很多无关的广告,其实大部分广告都是所在的网络劫持了网站响应的内容,并在其中植入了广告代码。为了防止这种情况发生,我们可以使用CSP来快速的阻止这种广告植入。而且可以比较好的防御dom xss。
CSP使用方式有两种
1. 使用meta标签, 直接在页面添加meta标签
<meta http-equiv="Content-Security-Policy" content="default-src 'self' *.xx.com *.xx.cn 'unsafe-inline' 'unsafe-eval';">
这种方式最简单,但是也有些缺陷,每个页面都需要添加,而且不能对限制的域名进行上报。
2. 在服务端配置csp
Apache :
Add the following to your httpd.conf in your VirtualHost or in an .htaccess file:
Header set Content-Security-Policy "default-src 'self';"
Nginx :
In your server {} block add:
add_header Content-Security-Policy "default-src 'self';";
在服务端配置所有的页面都可以不需要改了,而且还支持上报。
如果meta、响应头里都指定了Content-Security-Policy,则会优先使用响应头里的Content-Security-Policy
CSP内容匹配的规则:规则名称 规则 规则;规则名称 规则 ...
比如:
default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self';
default-src 'self' *.xx.com *.xx.cn aa.com 'unsafe-inline' 'unsafe-eval'
*.xx.com 支持多级域名, 可以不填写http协议。
default-src   所有资源的默认策略
			script-src     JS的加载策略,会覆盖default-src中的策略,比如写了default-src xx.com;script-src x.com xx.com;   必须同时加上xx.com,因为script-src会当作一个整体覆盖整个默认的default-src规则。
			'unsafe-inline'  允许执行内联的JS代码,默认为不允许,如果有内联的代码必须加上这条
			'unsafe-eval'    允许执行eval等
对自定义的协议 比如 jsxxx://aaa.com 可以写成 jsxxx:
https协议下自动把http请求转为https可以使用 upgrade-insecure-requests
CSP浏览器支持
目前CSP LEVER1 已经被大部分浏览器所支持

csp lever1 涉及到的规则有:
default-src、script-src、style-src、img-src、connect-src、font-src、object-src、media-src、
sandbox、report-uri
CSP LEVER2 加了一些新的规则:
child-src、form-action、frame-ancestors、plugin-types 。对于现在的移动端开发来说,lever2已经完全可以使用了。

详细规则内容:(参考:https://content-security-policy.com/)
| Directive | Example Value | Description | 
|---|---|---|
default-src | 
'self' cdn.example.com | 
The default-src is the default policy for loading content such as JavaScript, Images, CSS, Font's, AJAX requests, Frames, HTML5 Media. See the Source List Reference for possible values.
CSP Level 1 25+ 23+ 7+ 12+  | 
script-src | 
'self' js.example.com | 
Defines valid sources of JavaScript.
 CSP Level 1 25+ 23+ 7+ 12+  | 
style-src | 
'self' css.example.com | 
Defines valid sources of stylesheets.
 CSP Level 1 25+ 23+ 7+ 12+  | 
img-src | 
'self' img.example.com | 
Defines valid sources of images.
 CSP Level 1 25+ 23+ 7+ 12+  | 
connect-src | 
'self' | 
Applies to XMLHttpRequest (AJAX), WebSocket or EventSource. If not allowed the browser emulates a 400 HTTP status code.
CSP Level 1 25+ 23+ 7+ 12+  | 
font-src | 
font.example.com | 
Defines valid sources of fonts.
 CSP Level 1 25+ 23+ 7+ 12+  | 
object-src | 
'self' | 
Defines valid sources of plugins, eg <object>, <embed> or <applet>.
CSP Level 1 25+ 23+ 7+ 12+  | 
media-src | 
media.example.com | 
Defines valid sources of audio and video, eg HTML5 <audio>, <video> elements.
CSP Level 1 25+ 23+ 7+ 12+  | 
frame-src | 
'self' | 
Defines valid sources for loading frames. child-src is preferred over this deprecated directive.
Deprecated  | 
sandbox | 
allow-forms allow-scripts | 
Enables a sandbox for the requested resource similar to the iframe sandbox attribute. The sandbox applies a same origin policy, prevents popups, plugins and script execution is blocked. You can keep the sandbox value empty to keep all restrictions in place, or add values: allow-formsallow-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
CSP Level 1 25+ 50+ 7+ 12+  | 
report-uri | 
/some-report-uri | 
Instructs the browser to POST a reports of policy failures to this URI. You can also append -Report-Only to the HTTP header name to instruct the browser to only send reports (does not block anything).
CSP Level 1 25+ 23+ 7+ 12+  | 
child-src | 
'self' | 
Defines valid sources for web wokers and nested browsing contexts loaded using elements such as <frame> and <iframe>
CSP Level 2 40+ 45+  | 
form-action | 
'self' | 
Defines valid sources that can be used as a HTML <form> action.
CSP Level 2 40+ 36+  | 
frame-ancestors | 
'none' | 
Defines valid sources for embedding the resource using <frame> <iframe> <object> <embed><applet>. Setting this directive to 'none' should be roughly equivalent to X-Frame-Options: DENY
CSP Level 2 39+ 33+  | 
plugin-types | 
application/pdf | 
Defines valid MIME types for plugins invoked via <object> and <embed>. To load an <applet>you must specify application/x-java-applet.
CSP Level 2 40+  | 
参考文档:
https://content-security-policy.com/
http://baike.baidu.com/link?url=d0CILP0CXyvCuc_pRv7-3gRNXjEPKwiDWEReXi4uzEr8IPkktX3VLfnUnRyc70cLn9zSyviOfmpS8aAWUd3xrK
CSP内容安全策略的更多相关文章
- aspnet core2中使用csp内容安全策略
		
aspnet core2中使用csp内容安全策略 问题:aspnet core2如何使用csp防止xss的攻击 方法: public void ConfigureServices( IServiceC ...
 - 【XSS】再谈CSP内容安全策略
		
再谈CSP内容安全策略 之前每次都是想的很浅,或者只是个理论派,事实证明就是得动手实践 参考 CSP的用法 官方文档 通过设置属性来告诉浏览器允许加载的资源数据来源.可通过Response响应头来设置 ...
 - Content Security Policy (CSP)内容安全策略
		
CSP简介 Content Security Policy(CSP),内容(网页)安全策略,为了缓解潜在的跨站脚本问题(XSS攻击),浏览器的扩展程序系统引入了内容安全策略(CSP)这个概念. CSP ...
 - CSP内容安全策略总结及如何抵御 XSS 攻击
		
跨域脚本攻击 XSS 是最常见.危害最大的网页安全漏洞.为了防止它们,要采取很多编程措施,非常麻烦.很多人提出,能不能根本上解决问题,浏览器自动禁止外部注入恶意脚本?这就是"网页安全政策&q ...
 - 网页入侵最后一道防线:CSP内容安全策略
		
首先,什么是最后一道防线?网页入侵都有一个过程,简单来说,就是1.代码注入,2.代码执行. 对于黑客来说,代码注入后并不代表就万事大吉了,因为此时代码只是安静地躺在受害者的服务器里,什么坏事都没干呢! ...
 - Content Security Policy (CSP)内容安全策略总结
		
跨域脚本攻击 XSS 是最常见.危害最大的网页安全漏洞. 为了防止它们,要采取很多编程措施,非常麻烦.很多人提出,能不能根本上解决问题,浏览器自动禁止外部注入恶意脚本?这就是"网页安全政策& ...
 - 安全 - 内容安全策略(CSP)(未完)
		
威胁 跨站脚本攻击(Cross-site scripting) 跨站脚本攻击Cross-site scripting (XSS)是一种安全漏洞,攻击者可以利用这种漏洞在网站上注入恶意的客户端代码. 攻 ...
 - 内容安全策略(CSP)详解
		
1.背景 1.1.同源策略 网站的安全模式源于"同源策略",web浏览器允许第一个web页面中的脚本访问页面中的数据,但前提是两个web页面具有相同的源.此策略防止一个页面的恶意脚 ...
 - Web 安全之内容安全策略 (CSP)
		
内容安全策略 (CSP, Content Security Policy) 是一个附加的安全层,用于帮助检测和缓解某些类型的攻击,包括跨站脚本攻击 (XSS) 和数据注入等攻击. 这些攻击可用于实现从 ...
 
随机推荐
- 石英晶振频率后面带的PPM是什么单位
			
PPM是石英晶振的基本单位之一,表示晶振的精度和相对偏差, PPM代表着百万分之一,它表明晶体的频率可能会偏离标称值多少.晶振频率是以MHZ(10的6次方)和KHZ(10的3次方)为基本单位的,标称频 ...
 - sed的选项与命令简要
			
第一部分:sed命令选项 sed选项 说明 -n, --quiet, --silent 静默模式,取消将模式空间中的内容自动打印出来. -e script, --expression=script 以 ...
 - 安装robotframework时提示权限受限
			
mba下准备装robotframework,python已默认装好,按照如下的教程继续按照robotframework,发现会提示权限受限,如图. 第二步: 安装 robotframework下载地址 ...
 - XQuery FLWOR 表达式
			
FLWOR 是 "For, Let, Where, Order by, Return" 的只取首字母缩写.for 语句把 bookstore 元素下的所有 book 元素提取到名为 ...
 - Visual Studio的2个有趣的插件:声音控制和放屁:)
			
.NET Slave | Talk to, hear, touch and see your code介绍了2个有趣的Visual Studio的插件,你可以通过它们和你的代码对话. 声音控制(Voi ...
 - [ES6] Array.findIndex()
			
In es5, you can use indexOf to get the index of one item in an array. In es6, you can use findIndex( ...
 - [译]C++如何切分字符串
			
声明: 翻译仅以技术学习和交流为目的,如需转载请务必标明原帖链接. http://stackoverflow.com/questions/236129/how-to-split-a-string-in ...
 - 实现nodejs的promises库(基于promise.js改写)
			
原promise.js库地址:https://github.com/stackp/promisejs promises是JavaScript实现优雅编程的一个非常不错的轻量级框架.该框架可以让你从杂乱 ...
 - 使用HTML5 WebDataBase设计离线数据库
			
基于HTML5的Web DataBase 可以让你在浏览器中进行数据持久地存储管理和有效查询,假设你的离线应用程序有需要规范化的存储功能,那么使用Web DataBase,可以使你的应用程序无论是在离 ...
 - Java基础知识强化之集合框架笔记07:Collection集合的遍历之迭代器遍历
			
1. Collection的迭代器: Iterator iterator():迭代器,集合的专用遍历方式 2. 代码示例: package cn.itcast_03; import java.util ...