您现在的位置是:网络安全 >>正文
Web安全之Content Security Policy(CSP 内容安全策略)详解
网络安全42435人已围观
简介什么是Content Security PolicyCSP)Content Security Policy是一种网页安全策略,现代浏览器使用它来增强网页的安全性。可以通过Content Securit ...

什么是安安全Content Security Policy(CSP)
Content Security Policy是一种网页安全策略,现代浏览器使用它来增强网页的内容安全性。可以通过Content Security Policy来限制哪些资源(如JavaScript、策略CSS 、详解图像等)可以被加载 ,安安全从哪些url加载 。内容
CSP 本质上是策略白名单机制,开发者明确告诉浏览器哪些外部资源可以加载和执行,源码库详解可以从哪些url加载资源 。安安全
CSP最初被设计用来减少跨站点脚本攻击(XSS),内容该规范的策略后续版本还可以防止其他形式的攻击,如点击劫持 。详解
启用CSP的安安全两种方法
启用CSP的方法有两种,第一种是内容通过设置一个HTTP响应头(HTTP response header) “Content-Security-Policy”,建站模板第二种是策略通过HTML标签<meta>设置 ,例如 :
复制<meta http-equiv="Content-Security-Policy" content="script-src self; object-src none">1.除了Content-Security-Policy外 ,还有一个Content-Security-Policy-Report-Only字段,表示不执行限制选项,只是记录违反限制的行为,必须与report-uri值选项配合使用 ,例如:
复制Content-Security-Policy-Report-Only: default-src self; report-uri /some-report-uri;1.CSP指令介绍
Content-Security-Policy值由一个或多个指令组成 ,多个指令用分号分隔。高防服务器
csp资源加载项限制指令如下: 复制script-src:外部脚本style-src:样式文件img-src:图片文件media-src:媒体文件(音频和视频)font-src:字体文件object-src:插件(比如 Flash)child-src:框架frame-ancestors :嵌入的外部资源(比如<frame> 、<iframe> 、<embed>和<applet>)connect-src:HTTP 连接(通过 XHR、WebSockets、EventSource等)worker-src:worker脚本manifest-src :manifest 文件default-src:用来设置上面各个选项的默认值。1.2.3.4.5.6.7.8.9.10.11.12.上述指令对应的值如下 :
Source Value
Example
Description
*img-src
*Wildcard, allows any URL except data: blob: filesystem: schemes.
none
object-src none
Prevents loading resources from any source.
self
script-src self
Allows loading resources from the same origin (same scheme, host and port).
data:
img-src self data:
Allows loading resources via the data scheme (eg Base64 encoded images).
domain.example.com
img-src domain.example.com
Allows loading resources from the specified domain name.
*.example.com
img-src *.example.com
Allows loading resources from any subdomain under example.com.
https://cdn.com
img-src https://cdn.com
Allows loading resources only over HTTPS matching the given domain.
https:
img-src https:
Allows loading resources only over HTTPS on any domain.
unsafe-inline
script-src unsafe-inline
Allows use of inline source elements such as style attribute, onclick, or script tag bodies (depends on the context of the source it is applied to) and javascript: URIs
unsafe-eval
script-src unsafe-eval
Allows unsafe dynamic code evaluation such as JavaScript eval()
sha256-
script-src sha256-xyz...
Allows an inline script or CSS to execute if its hash matches the specified hash in the header. Currently supports SHA256, SHA384 or SHA512. CSP Level 2
nonce-
script-src nonce-rAnd0m
Allows an inline script or CSS to execute if the script (eg: <script nonce="rAnd0m">) tag contains a nonce attribute matching the nonce specifed in the CSP header. The nonce should be a secure random string, and should not be reused. CSP Level 2
strict-dynamic
script-src strict-dynamic
Enables an allowed script to load additional scripts via non-"parser-inserted" script elements (for example document.createElement(script); is allowed). CSP Level 3
unsafe-hashes
script-src unsafe-hashes sha256-abc...
Allows you to enable scripts in event handlers (eg onclick). Does not apply to javascript: or inline <script> CSP Level 3
CSP URL限制指令如下:复制frame-ancestors:限制嵌入框架的网页
base-uri :限制
form-action :限制<form#action>1.2.3. CSP其它限制指令如下:复制block-all-mixed-content:HTTPS 网页不得加载HTTP链接的资源
upgrade-insecure-requests:自动将网页加载的免费模板外部资源的链接由HTTP改为HTTPS
plugin-types:限制可以使用的插件格式
sandbox :浏览器行为的限制 ,比如不能有弹出窗口等 。1.2.3.4.小结
CSP对于保护Web应用程序的安全非常重要 ,可以帮助减少很多XSS类攻击 。需要注意的是 ,服务器租用CSP只是一种安全策略 ,不能完全保证网站的安全性。因此,在使用CSP时,还需要结合其他安全措施 ,如使用HTTPS 、防火墙等 ,进一步提高网站的安全性 。源码下载
Tags:
转载:欢迎各位朋友分享到网络,但转载请说明文章出处“商站动力”。http://www.noorid.com/html/704c899287.html
相关文章
把手机变“肉鸡”—移动应用攻击的新特点与防护
网络安全近年来,随着移动互联产业的兴起,移动应用软件(App)逐渐渗透到社会生活的各个领域,App种类和数量呈爆发式增长。第三方软件开发包(SDK)、移动应用接口(API)、人脸识别等生物技术广泛集成、应用于 ...
【网络安全】
阅读更多戴尔电脑主机维修教程(轻松解决常见故障,快速恢复电脑正常运行)
网络安全作为一台常见的电脑品牌,戴尔电脑主机在使用过程中难免会遇到各种故障。本文将以戴尔电脑主机维修教程为主题,针对常见故障提供解决方法,帮助用户轻松解决问题,快速恢复电脑的正常运行。1.电脑无法开机-主机电 ...
【网络安全】
阅读更多神舟战神电脑装系统教程(一步步教你如何为神舟战神电脑装系统)
网络安全对于购买了神舟战神电脑的用户来说,装系统是必不可少的一项任务。本文将以详细的步骤和说明,教你如何为神舟战神电脑装系统,让你能够轻松上手。1.确定装系统所需材料在进行系统安装前,首先需要准备好装系统所需 ...
【网络安全】
阅读更多
热门文章
最新文章
友情链接
- 小米膜的优点和特点(为你的手机屏幕保驾护航)
- 烈焰战神1050ti(探索烈焰战神1050ti的卓越性能与多功能特性)
- 小辣椒JDPlus全方位解读,值得购买吗?(便宜好用的小辣椒JDPlus手机,性价比之王!)
- 《雨林木风win764系统安装教程》(详细教你如何安装雨林木风win764系统)
- M8peg与750比较(一探M8peg和750处理器之间的差异与应用场景选择)
- X270在双核CPU中的性能表现(探索X270在双核CPU中的优势和应用场景)
- iPhone7防抖技术(探索iPhone7防抖技术的出色表现和关键特性)
- 蒸汽吸尘器的清洁效果及优点(以蒸汽吸尘器为主题的家居清洁新选择)
- 探究惠普导航的优势与应用(解密惠普导航的功能和使用体验)
- 如何评估数据中心?高度互联的生态系统是关键 云服务器亿华云网站建设b2b信息平台香港物理机企业服务器源码库