内容安全策略 (CSP):如果使用元标记,是否可以以某种方式报告违规行为?

2024-02-11

在玩了相当长一段时间后,我仍在寻找解决方案。某个网站的内容安全策略需要一长串允许的来源(特别是因为 Google 使用动态 .TLD 加载某些内容 - Google 在文档中还指出这对于某些服务是必要的:https://developers.google.com/tag-platform/tag-manager/web/csp?hl=de https://developers.google.com/tag-platform/tag-manager/web/csp?hl=de).

CSP 的更好解决方案是将其放入标头中,但不幸的是,非常大的标头会导致服务器和客户端出现问题。

简单看一下政策:

default-src 'self' https://*.sentry-cdn.com https://*.ingest.sentry.io https://*.sampleassets.com https://*.stripe.com https://cdn.rawgit.com https://cdn.jsdelivr.net https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net  https://project.dev https://*.project.dev https://project.dev; script-src 'unsafe-inline' https:  'strict-dynamic' 'nonce-xxx' *.sentry-cdn.com https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net https://*.google.com/ https://*.google.ad/ https://*.google.ae/ https://*.google.com.af/ https://*.google.com.ag/ https://*.google.com.ai/ https://*.google.al/ https://*.google.am/ https://*.google.co.ao/ https://*.google.com.ar/ https://*.google.as/ https://*.google.at/ https://*.google.com.au/ https://*.google.az/ https://*.google.ba/ https://*.google.com.bd/ https://*.google.be/ https://*.google.bf/ https://*.google.bg/ https://*.google.com.bh/ https://*.google.bi/ https://*.google.bj/ https://*.google.com.bn/ https://*.google.com.bo/ https://*.google.com.br/ https://*.google.bs/ https://*.google.bt/ https://*.google.co.bw/ https://*.google.by/ https://*.google.com.bz/ https://*.google.ca/ https://*.google.cd/ https://*.google.cf/ https://*.google.cg/ https://*.google.ch/ https://*.google.ci/ https://*.google.co.ck/ https://*.google.cl/ https://*.google.cm/ https://*.google.cn/ https://*.google.com.co/ https://*.google.co.cr/ https://*.google.com.cu/ https://*.google.cv/ https://*.google.com.cy/ https://*.google.cz/ https://*.google.de/ https://*.google.dj/ https://*.google.dk/ https://*.google.dm/ https://*.google.com.do/ https://*.google.dz/ https://*.google.com.ec/ https://*.google.ee/ https://*.google.com.eg/ https://*.google.es/ https://*.google.com.et/ https://*.google.fi/ https://*.google.com.fj/ https://*.google.fm/ https://*.google.fr/ https://*.google.ga/ https://*.google.ge/ https://*.google.gg/ https://*.google.com.gh/ https://*.google.com.gi/ https://*.google.gl/ https://*.google.gm/ https://*.google.gr/ https://*.google.com.gt/ https://*.google.gy/ https://*.google.com.hk/ https://*.google.hn/ https://*.google.hr/ https://*.google.ht/ https://*.google.hu/ https://*.google.co.id/ https://*.google.ie/ https://*.google.co.il/ https://*.google.im/ https://*.google.co.in/ https://*.google.iq/ https://*.google.is/ https://*.google.it/ https://*.google.je/ https://*.google.com.jm/ https://*.google.jo/ https://*.google.co.jp/ https://*.google.co.ke/ https://*.google.com.kh/ https://*.google.ki/ https://*.google.kg/ https://*.google.co.kr/ https://*.google.com.kw/ https://*.google.kz/ https://*.google.la/ https://*.google.com.lb/ https://*.google.li/ https://*.google.lk/ https://*.google.co.ls/ https://*.google.lt/ https://*.google.lu/ https://*.google.lv/ https://*.google.com.ly/ https://*.google.co.ma/ https://*.google.md/ https://*.google.me/ https://*.google.mg/ https://*.google.mk/ https://*.google.ml/ https://*.google.com.mm/ https://*.google.mn/ https://*.google.ms/ https://*.google.com.mt/ https://*.google.mu/ https://*.google.mv/ https://*.google.mw/ https://*.google.com.mx/ https://*.google.com.my/ https://*.google.co.mz/ https://*.google.com.na/ https://*.google.com.ng/ https://*.google.com.ni/ https://*.google.ne/ https://*.google.nl/ https://*.google.no/ https://*.google.com.np/ https://*.google.nr/ https://*.google.nu/ https://*.google.co.nz/ https://*.google.com.om/ https://*.google.com.pa/ https://*.google.com.pe/ https://*.google.com.pg/ https://*.google.com.ph/ https://*.google.com.pk/ https://*.google.pl/ https://*.google.pn/ https://*.google.com.pr/ https://*.google.ps/ https://*.google.pt/ https://*.google.com.py/ https://*.google.com.qa/ https://*.google.ro/ https://*.google.ru/ https://*.google.rw/ https://*.google.com.sa/ https://*.google.com.sb/ https://*.google.sc/ https://*.google.se/ https://*.google.com.sg/ https://*.google.sh/ https://*.google.si/ https://*.google.sk/ https://*.google.com.sl/ https://*.google.sn/ https://*.google.so/ https://*.google.sm/ https://*.google.sr/ https://*.google.st/ https://*.google.com.sv/ https://*.google.td/ https://*.google.tg/ https://*.google.co.th/ https://*.google.com.tj/ https://*.google.tl/ https://*.google.tm/ https://*.google.tn/ https://*.google.to/ https://*.google.com.tr/ https://*.google.tt/ https://*.google.com.tw/ https://*.google.co.tz/ https://*.google.com.ua/ https://*.google.co.ug/ https://*.google.co.uk/ https://*.google.com.uy/ https://*.google.co.uz/ https://*.google.com.vc/ https://*.google.co.ve/ https://*.google.vg/ https://*.google.co.vi/ https://*.google.com.vn/ https://*.google.vu/ https://*.google.ws/ https://*.google.rs/ https://*.google.co.za/ https://*.google.co.zm/ https://*.google.co.zw/ https://*.google.cat/; img-src data: 'self' https://*.sentry-cdn.com https://*.ingest.sentry.io https://*.sampleassets.com https://*.stripe.com https://cdn.rawgit.com https://cdn.jsdelivr.net https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net  https://project.dev https://*.project.dev https://project.dev https://*.s3.amazonaws.com https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net https://*.google.com/ https://*.google.ad/ https://*.google.ae/ https://*.google.com.af/ https://*.google.com.ag/ https://*.google.com.ai/ https://*.google.al/ https://*.google.am/ https://*.google.co.ao/ https://*.google.com.ar/ https://*.google.as/ https://*.google.at/ https://*.google.com.au/ https://*.google.az/ https://*.google.ba/ https://*.google.com.bd/ https://*.google.be/ https://*.google.bf/ https://*.google.bg/ https://*.google.com.bh/ https://*.google.bi/ https://*.google.bj/ https://*.google.com.bn/ https://*.google.com.bo/ https://*.google.com.br/ https://*.google.bs/ https://*.google.bt/ https://*.google.co.bw/ https://*.google.by/ https://*.google.com.bz/ https://*.google.ca/ https://*.google.cd/ https://*.google.cf/ https://*.google.cg/ https://*.google.ch/ https://*.google.ci/ https://*.google.co.ck/ https://*.google.cl/ https://*.google.cm/ https://*.google.cn/ https://*.google.com.co/ https://*.google.co.cr/ https://*.google.com.cu/ https://*.google.cv/ https://*.google.com.cy/ https://*.google.cz/ https://*.google.de/ https://*.google.dj/ https://*.google.dk/ https://*.google.dm/ https://*.google.com.do/ https://*.google.dz/ https://*.google.com.ec/ https://*.google.ee/ https://*.google.com.eg/ https://*.google.es/ https://*.google.com.et/ https://*.google.fi/ https://*.google.com.fj/ https://*.google.fm/ https://*.google.fr/ https://*.google.ga/ https://*.google.ge/ https://*.google.gg/ https://*.google.com.gh/ https://*.google.com.gi/ https://*.google.gl/ https://*.google.gm/ https://*.google.gr/ https://*.google.com.gt/ https://*.google.gy/ https://*.google.com.hk/ https://*.google.hn/ https://*.google.hr/ https://*.google.ht/ https://*.google.hu/ https://*.google.co.id/ https://*.google.ie/ https://*.google.co.il/ https://*.google.im/ https://*.google.co.in/ https://*.google.iq/ https://*.google.is/ https://*.google.it/ https://*.google.je/ https://*.google.com.jm/ https://*.google.jo/ https://*.google.co.jp/ https://*.google.co.ke/ https://*.google.com.kh/ https://*.google.ki/ https://*.google.kg/ https://*.google.co.kr/ https://*.google.com.kw/ https://*.google.kz/ https://*.google.la/ https://*.google.com.lb/ https://*.google.li/ https://*.google.lk/ https://*.google.co.ls/ https://*.google.lt/ https://*.google.lu/ https://*.google.lv/ https://*.google.com.ly/ https://*.google.co.ma/ https://*.google.md/ https://*.google.me/ https://*.google.mg/ https://*.google.mk/ https://*.google.ml/ https://*.google.com.mm/ https://*.google.mn/ https://*.google.ms/ https://*.google.com.mt/ https://*.google.mu/ https://*.google.mv/ https://*.google.mw/ https://*.google.com.mx/ https://*.google.com.my/ https://*.google.co.mz/ https://*.google.com.na/ https://*.google.com.ng/ https://*.google.com.ni/ https://*.google.ne/ https://*.google.nl/ https://*.google.no/ https://*.google.com.np/ https://*.google.nr/ https://*.google.nu/ https://*.google.co.nz/ https://*.google.com.om/ https://*.google.com.pa/ https://*.google.com.pe/ https://*.google.com.pg/ https://*.google.com.ph/ https://*.google.com.pk/ https://*.google.pl/ https://*.google.pn/ https://*.google.com.pr/ https://*.google.ps/ https://*.google.pt/ https://*.google.com.py/ https://*.google.com.qa/ https://*.google.ro/ https://*.google.ru/ https://*.google.rw/ https://*.google.com.sa/ https://*.google.com.sb/ https://*.google.sc/ https://*.google.se/ https://*.google.com.sg/ https://*.google.sh/ https://*.google.si/ https://*.google.sk/ https://*.google.com.sl/ https://*.google.sn/ https://*.google.so/ https://*.google.sm/ https://*.google.sr/ https://*.google.st/ https://*.google.com.sv/ https://*.google.td/ https://*.google.tg/ https://*.google.co.th/ https://*.google.com.tj/ https://*.google.tl/ https://*.google.tm/ https://*.google.tn/ https://*.google.to/ https://*.google.com.tr/ https://*.google.tt/ https://*.google.com.tw/ https://*.google.co.tz/ https://*.google.com.ua/ https://*.google.co.ug/ https://*.google.co.uk/ https://*.google.com.uy/ https://*.google.co.uz/ https://*.google.com.vc/ https://*.google.co.ve/ https://*.google.vg/ https://*.google.co.vi/ https://*.google.com.vn/ https://*.google.vu/ https://*.google.ws/ https://*.google.rs/ https://*.google.co.za/ https://*.google.co.zm/ https://*.google.co.zw/ https://*.google.cat/; style-src 'self' https://*.sentry-cdn.com https://*.ingest.sentry.io https://*.sampleassets.com https://*.stripe.com https://cdn.rawgit.com https://cdn.jsdelivr.net https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net  https://project.dev https://*.project.dev https://project.dev https://fonts.googleapis.com https://fonts.gstatic.com 'unsafe-inline'; font-src 'self'  https://project.dev https://*.project.dev https://project.dev https://fonts.googleapis.com https://fonts.gstatic.com data: ; frame-src 'self' https://*.sentry-cdn.com https://*.ingest.sentry.io https://*.sampleassets.com https://*.stripe.com https://cdn.rawgit.com https://cdn.jsdelivr.net https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net  https://project.dev https://*.project.dev https://project.dev; object-src 'none'; media-src 'unsafe-inline' data: 'self' https://*.sentry-cdn.com https://*.ingest.sentry.io https://*.sampleassets.com https://*.stripe.com https://cdn.rawgit.com https://cdn.jsdelivr.net https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net  https://project.dev https://*.project.dev https://project.dev; child-src 'self' blob: ; manifest-src 'self'; connect-src 'self' https://*.sentry-cdn.com https://*.ingest.sentry.io https://*.sampleassets.com https://*.stripe.com https://cdn.rawgit.com https://cdn.jsdelivr.net https://www.google.com https://www.google-analytics.com https://www.gstatic.com https://www.google.com/recaptcha/ https://recaptcha.google.com/recaptcha/ https://www.googletagmanager.com https://*.doubleclick.net  https://project.dev https://*.project.dev https://project.dev; base-uri 'self'; upgrade-insecure-requests;

现在让我们假设这个政策运作良好,但不知何故我希望报告违规行为(在需要时调整政策)。

尝试设置这样的标题:

内容安全策略:report-urihttps://meow.ingest.sentry.io/api/meow/security/?sentry_key=meow; https://meow.ingest.sentry.io/api/meow/security/?sentry_key=meow;

然而,尽管设置了此标头,但似乎并未报告违反 CSP 的情况。如果我在标头中设置策略,它会完美地工作。是否有解决方案可以使报告与元标记一起使用?

编辑:已经尝试过新的report-to标题,带有设置report-to政策范围内。这也行不通:/


我找到了一个解决方案:

<meta http-equiv="Content-Security-Policy" content="[CSP_CONTENT_HERE]">
<script nonce="[CSP_NONCE_HERE]">
    document.addEventListener('securitypolicyviolation', function (event) {
        console.log(event)
        let config = JSON.parse('{"keys" : ["blockedURI", "columnNumber", "disposition", "documentURI", "effectiveDirective", "lineNumber", "originalPolicy", "referrer", "sample", "sourceFile", "statusCode", "violatedDirective"],"reportUri" : "[YOUR_REPORT_URI_HERE]"}');
        let reportKeys = {'blockedURI':'blocked-uri', 'columnNumber':'column-number', 'documentURI':'document-uri', 'effectiveDirective':'effective-directive', 'lineNumber':'line-number', 'originalPolicy':'original-policy', 'sourceFile':'source-file', 'statusCode':'status-code', 'violatedDirective':'violated-directive'};
        let json = {'csp-report': {}};
        for (let i = 0, len = config.keys.length; i < len; i++) {
            if (event[config.keys[i]] !== 0 && event[config.keys[i]] !== '') {
                json['csp-report'][(reportKeys[config.keys[i]] ? reportKeys[config.keys[i]] : config.keys[i])] = event[config.keys[i]];
            }
        }
        let xhr = new XMLHttpRequest();
        xhr.open('POST', config.reportUri, true);
        xhr.setRequestHeader('content-type', 'application/csp-report');
        xhr.send(JSON.stringify(json));
    });
</script>

因此,您不必设置某种报告 uri 或配置报告,而是使用 JS 事件侦听器securitypolicyviolation。从事件中,您可以获取报告可能感兴趣的所有参数,并将它们发送到所需的报告 URI。

脚本取自这里,但我不喜欢添加另一个外部脚本的想法:https://report-uri.github.io/report-uri-js-demo/ https://report-uri.github.io/report-uri-js-demo/

只需看一下占位符即可:

  • [CSP_CONTENT_HERE]:您的 CSP 定义
  • [YOUR_REPORT_URI_HERE]:您的报告 URI(例如 Sentry)
  • [CSP_NONCE_HERE]:您的 CSP 随机数(如果您使用 CSP 随机数)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

内容安全策略 (CSP):如果使用元标记,是否可以以某种方式报告违规行为? 的相关文章

  • 在 istio 中为 envoy 启用 http 标头日志记录

    我希望能够捕获 日志 至少部分 envoy我的 HTTP 标头istio服务网格 我已经经历过envoy s docs https www envoyproxy io docs envoy latest start quick start
  • codeigniter 标头中重复的“set-cookie: ci-session”字段

    每次在我的控制器中使用 this gt session gt set userdata 或 this gt session gt set flashdata 时 另一个相同的 Set Cookie ci session 会添加到 http
  • 如何将内容安全策略与本地主机文件一起使用

    我的页面上出现以下错误 Refused to load the script http 127 0 0 1 35729 livereload js because it violates the following Content Secu
  • Android Http 获取会话 Cookie

    我本来不想在这里发帖 因为网上有太多信息 但我已经深入搜寻但无法弄清楚 好吧 所以我无法让它在两种情况下工作 希望这两种情况的答案是相同的 我的问题是我设置了请求标头 但它似乎没有发送它 我有一个会话 id s e32ff223fwefd3
  • 发送 HTTP 标头后,服务器无法附加标头

    我在我的 ASP NET C Web 应用程序中间歇性地收到此异常 发送 HTTP 标头后 服务器无法附加标头 这是由于应用程序在发送页面后将内容附加到页面响应标头而引起的 我不确定为什么它是间歇性的 但我需要做的是在修改标题之前执行检查
  • RestKit 0.20.0pre5:每个请求的 HTTP 标头

    我想发送一个If Modified Since带有 GET 请求的 http 标头 由 RKObjectManager getObjectsAtPath the 迁移指南 https github com RestKit RestKit w
  • 在 Java Web 应用程序中处理 X-FORWARDED-PROTO 标头

    任何人都可以指导我与X 转发 原型 https developer mozilla org en US docs Web HTTP Headers X Forwarded Proto部署到 Apache Tomcat 的 Java Web
  • 如何在 Visual Studio 中打开 .rdl 文件?

    我有一个 rdl 文件 需要在 Visual Studio 中打开 当我尝试打开该文件时 我得到了一个 XML 文件 但是 我无法看到设计器格式 我不知道使用哪个版本的 Visual Studio 创建此 rdl 文件 是否可以在 Visu
  • 使用内容安全策略防止 Internet Explorer 11 上的内联 JavaScript

    是否可以使用 ASP NET WebForm 上的 CSP 来阻止 Internet Explorer 11 上的内联 JavaScript 我知道 IE 11 不支持内容安全策略级别 2 但它支持级别 1 0 我尝试了很多方法 但没有明确
  • 从 WPF 打印/报告的最佳方法是什么? [关闭]

    就目前情况而言 这个问题不太适合我们的问答形式 我们希望答案得到事实 参考资料或专业知识的支持 但这个问题可能会引发辩论 争论 民意调查或扩展讨论 如果您觉得这个问题可以改进并可能重新开放 访问帮助中心 help reopen questi
  • Ajax - 下载前获取文件大小

    基本上 我想弄清楚是否应该使用 AJAX 下载文件 具体取决于文件大小有多大 我想这个问题也可以改写为 如何仅获取ajax请求的标头 EDIT ultima rat0 https stackoverflow com users 239962
  • 请求标头与响应标头

    我正在尝试将图像缓存在网站上 该图像由 PHP 提供服务 其中我使用适当的值设置缓存控制的响应标头 但这不是缓存图像 我认为这可能与具有无缓存缓存控制的请求标头有关 标题如下 响应头 Access Control Allow Origin
  • 删除 Safari 上的 CORS Accept-Encoding 标头

    我制作了一个 javascript 书签 它可以访问公共共享的 google drive 文件夹上的字节混洗图像 因此我制作了 ArrayBuffer CORS 在 Firefox Chrome Opera 甚至 Safari 5 some
  • 如何测试“If-Modified-Since”HTTP 标头支持

    使用 PHP 如何准确测试远程网站supports If Modified Since HTTP 标头 据我所知 如果您获取的远程文件自标头请求中指定的日期以来已被修改 它应该返回 200 OK 状态 如果尚未修改 则应返回 304 Not
  • Next JS - 源代码中缺少元标签

    我安装了多个元标记 例如标题 描述 关键字等 但是 它没有在查看源代码中显示这些标签 因此 Facebook 共享卡无法正常工作 我已经安装了og标签 但在 Facebook 上似乎找不到它们Scraper或我的查看来源 然而 它填充在客户
  • 如何确定服务器是否支持 Range 标头?

    我一直在尝试使用 Range 标头值从特定点流式传输音频 但我总是从一开始就得到歌曲 我正在通过程序执行此操作 因此不确定问题是否出在我的代码中或服务器上 如何确定服务器是否支持 Range 标头参数 Thanks 方式HTTP规范 htt
  • AngularJS 和 Apiary.IO - 无法读取任何响应标头?

    我使用 Apiary io 模拟我的 API 但不知怎的 我无法使用 angularJS 从响应对象中读取任何标头 我确信我至少通过检查 firebug 正确设置了 Content Type application json Angular
  • 如何将“X-Content-Type-Options: nosniff”添加到我的网络服务器的所有响应标头中

    我正在运行一个 apache 网络服务器 我想将 X Content Type Options nosniff 添加到来自我的网络服务器的所有响应标头 我怎样才能做到这一点 是否可以通过更改 apache 配置文件来实现此目的 确保 mod
  • 如何知道 HTTP 请求标头值是否存在

    我确信这很简单 但是却让我感到厌烦 我在 Web 应用程序中使用了一个组件 它在 Web 请求期间通过添加标头 XYZComponent true 来标识自身 我遇到的问题是 如何在视图中检查此组件 以下内容不起作用 if Request
  • mysql表中的数据非常大。即使 select 语句也需要很多时间

    我正在开发一个数据库 它是一个相当大的数据库 有 13 亿行和大约 35 列 这是我检查表状态后得到的结果 Name Table Name Engine InnoDB Version 10 Row format Compact Rows 1

随机推荐