内容安全政策-拒绝从Google Analytics分析加载脚本-CSP规则正确

问题描述

在服务器上设置CSP规则时遇到问题。

在服务器中,我将:@H_502_3@ Header Content-Security-Policy设置为:

for file in filelist: attachment_part = MIMEBase("application","octet-stream") attachment_part.set_payload(open(file,"rb").read()) encoders.encode_base64(attachment_part) attachment_part.add_header("Content-disposition","attachment; filename='%s'" % os.path.basename(file)) message.attach(attachment_part) # Create Server Connection with smtplib.SMTP_SSL("smtp.gmail.com",465,context=context) as server: server.login(config.email_sender,config.email_password) server.sendmail( sender_email,reciever_email,message.as_string() )

当刷新浏览器时,我得到:

enter image description here

检查js源代码时,它是库jquery。

我该如何解决? 谢谢

config csp:

enter image description here

浏览器错误

enter image description here

解决方法

耦合事物。

答案在错误消息中。它说违反来自script-src

它会在解析脚本时打印出script-src的值,并且在打印值时不包含www.google-analytics.com

所以-取出“报告样本”,看看有什么变化。

接下来,查看syntax for the script-src,它需要一个方案或端口号。用https://www.googleanalytics.com替换主机。

搜索csp和google-analytics会返回一些结果-这是来自google support resource的结果:

它不仅限于google-analytics,而且可能有助于在CSP策略中获取正确的URI。

Content-Security-Policy: default-src 'self'; script-src: https://www.google-analytics.com https://ssl.google-analytics.com; img-src: https://www.google-analytics.com
connect-src: https://www.google-analytics.com

如果您仍然遇到问题,请向我们显示开发人员工具“网络”标签中的标题示例。