【问题标题】:Why is this a CSP violation? blocked-uri = self when 'self' is explicitly allowed为什么这是违反 CSP 的?当“self”被明确允许时,blocked-uri = self
【发布时间】:2016-06-01 07:28:21
【问题描述】:

我无法理解下面的 CSP 违规报告(由 FireFox 44.0.2 / Ubuntu 发送)。这里真正被阻止的是什么,为什么? 应该注意的是,我是否将'self' 或(在报告中自动翻译)https://www.example.com 写入 CSP 标头并不重要。 此外,我不知道渲染页面中缺少任何内容。 那么我能做些什么呢? (显然,如果每个页面都触发虚假违规报告,我不应该在我的实时网站中添加报告)

{
    "csp-report":{
        "blocked-uri":"self",
        "document-uri":"https://www.example.com/foo/bar/baz.html",
        "original-policy":"report-uri https://reportserver.example.com/ContentSecurityPolicy-report.php; 
            default-src https://www.example.com; 
            style-src https://example.com https://www.example.com https://fonts.googleapis.com; 
            script-src https://www.example.com https://code.jquery.com https://ajax.googleapis.com; 
            font-src https://fonts.gstatic.com",
        "referrer":"https://www.example.com/foo/bar/wtf.html",
        "source-file":"https://www.example.com/foo/bar/baz.html",
        "violated-directive":"style-src https://example.com https://www.example.com https://fonts.googleapis.com"
    }
}

【问题讨论】:

  • 这是否部署在任何我可以查看的地方?根据您的描述,我想不出任何除了 Firefox 中的错误。
  • @oreoshake 你可以在https://www.redeker.de/ 上看到这个(在“仅报告”模式下从外部)。同时我有点怀疑被阻止的可能是一些'unsafe-inline'风格,即style="..."添加到标签,但这在html源代码中不可见,因为它稍后会被每个脚本添加(脚本本身是允许的CSP)。但如果你能证实这个怀疑,我会很高兴。
  • 是的,不安全的内联是必要的。我经常发现开发者控制台有时比报告更有用。 CSP 报告存在许多已知问题,内嵌内容报告就是其中之一。
  • @oreoshake 我想开发者控制台是指 Chrome 的控制台(我现在发现它非常有用),因为 Firefox 中的那个(我最初使用的)给我留下了很多猜测...

标签: content-security-policy


【解决方案1】:

通过将您的政策设置为:

default-src 'self'; style-src example.com www.example.com 'self' https://fonts.googleapis.com 'unsafe-inline'; script-src 'self' https://code.jquery.com https://ajax.googleapis.com; font-src https://fonts.gstatic.com 'self';

我没有看到任何违规行为。我在样式 src 中添加了“不安全内联”,在 font-src 中添加了“自我”。

【讨论】:

  • 在我将 'unsafe-inline' 添加到这些单独的策略或 default-src 策略之前,我遇到了内联样式和内联 javascript 的问题。当您收到"blocked-uri": "self" 时,表明存在内联问题。
【解决方案2】:

我见过这种对用户代理没有任何意义的 CSP 报告(2021 年 9 月)

Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0

我假设它要么是更新的浏览器遇到了一些旧的错误,要么是一些浏览器扩展改变了用户代理和浏览器的行为。

是的,服务器发出的Content-Security-Policy 确实包含script-src 'self' 'report-sample' ...,即使报告的violated-directive 既不包含self 也不包含report-sample。我没有费心用那个旧 Firefox 进行测试,但我认为它不支持 'report-sample',它会导致它错误地解析策略,导致 'self' 也被丢弃。

如果您从一些更现代的用户代理中看到同样的报告,我很想知道有关您的案例的更多详细信息。

这个错误也可能与此有关:https://bugzilla.mozilla.org/show_bug.cgi?id=1236222

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    • 2017-08-10
    • 2020-04-16
    • 2019-02-19
    • 1970-01-01
    相关资源
    最近更新 更多