“Content-Security-Policy”头缺失或不安全的问题处理

作者:动易软件 来源:本站原创 点击数: 发布时间:2019年12月02日

问题:安全软件扫描提示“Content-Security-Policy” 头缺失或不安全的问题如何处理?

 

问题分析:

CSP全称Content Security Policy ,可以直接翻译为内容安全策略,说白了,就是为了页面内容安全而制定的一系列防护策略. 通过CSP所约束的的规责指定可信的内容来源(这里的内容可以指脚本、图片、iframe、font、style等等可能的远程的资源)。通过CSP协定,让WEB处于一个安全的运行环境中。

处理办法:

添加Content Security Policy:default-src 'self';   的响应头,如下图:

说明:

指令名

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'

定义页面插件的过滤策略,如 , 或者 等元素

media-src

media.example.com

定义媒体的过滤策略,如 HTML6的

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

 

示例

default-src 'self';   

只允许同源下的资源

 

script-src 'self';     

只允许同源下的js

 

script-src 'self' ajax.googleapis.com;

允许同源以及两个地址下的js加载

 

default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style-src 'self';

多个资源时,后面的会覆盖前面的