CSP策略防止广告植入问题

xiaoxiao2021-02-28  91

使用CSP防止广告植入问题主要用法实在html页面的头部加上:

<metahttp-equiv="Content-Security-Policy"content="script-src 'self'">

http-equiv 的 value 为 CSP 的策略类型,而 content 则是声明指令集合,即白名单。

指令取值示例说明default-src'self' cdn.example.com定义针对所有类型(js/image/css/web font/ajax/iframe/多媒体等)资源的默认加载策略,某类型资源如果没有单独定义策略,就使用默认。script-src'self' js.example.com定义针对JavaScript的加载策略object-src'self'针对,, 等标签的加载策略style-src'self' css.example.com定义针对样式的加载策略img-src'self' image.example.com定义针对图片的加载策略media-src'media.example.com'针对或者引入的html多媒体等标签的加载策略frame-src'self'针对iframe的加载策略connect-src'self'针对Ajax、WebSocket等请求的加载策略。不允许的情况下,浏览器会模拟一个状态为400的响应font-srcfont.qq.com针对Web Font的加载策略sandboxallow-forms allow-scripts对请求的资源启用sandboxreport-uri/some-report-uri告诉浏览器如果请求的资源不被策略允许时,往哪个地址提交日志信息。不阻止任何内容,可以改用Content-Security-Policy-Report-Only头base-uri'self'限制当前页面的url(CSP2)child-src'self'限制子窗口的源(iframe、弹窗等),取代frame-src(CSP2)form-action'self'限制表单能够提交到的源(CSP2)frame-ancestors'none'限制了当前页面可以被哪些页面以iframe,frame,object等方式加载(CSP2)plugin-typesapplication/pdf限制插件的类型(CSP2)

指令值示例及说明

指令值示例说明*img-src *允许任何内容'none'img-src 'none'不允许任何内容'self'img-src 'self'允许同源内容data:img-src data:允许data:协议(如base64编码的图片)www.a.comimg-src www.a.com允许加载指定域名的资源*.a.comimg-src *.a.com允许加载a.com任何子域的资源https://img.comimg-src https://img.com允许加载img.com的https资源https:img-src https:允许加载https资源'unsafe-inline'script-src 'unsafe-inline'允许加载inline资源(style属性,onclick,inline js和inline css等等)'unsafe-eval'script-src 'unsafe-eval'允许加载动态js代码,例如eval() 下面是关于CSP的一些链接资料。

https://developer.mozilla.org/zh-CN/docs/Web/Security/CSP/CSP_policy_directives

http://www.cnblogs.com/apanly/p/6038845.html

https://github.com/joeyguo/blog/issues/5

转载请注明原文地址: https://www.6miu.com/read-79740.html

最新回复(0)