【已解决】“Content-Security-Policy”头缺失

1、作用

简称CSP,意为内容安全策略,通过设置约束指定可信的内容来源,降低异源文件攻击,例如:js/css/image等

2、相关设置值

指令名

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

3、示例:

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';多个资源时,后面的会覆盖前面的

4、在nginx配置文件中添加,例如:

add_header Content-Security-Policy "default-src 'self'";只允许同源下的资源

add_header Content-Security-Policy "upgrade-insecure-requests;content *";将本站内部http链接自动改为https,并不限制内容加载来源。

猜你喜欢

转载自blog.csdn.net/weixin_45481821/article/details/131240781