需要帮助理解 CSP 报告并正确设置 frame-ancestor url

Need help understanding a CSP Report and set the right frame-ancestor url

我正在尝试将我的 Web 应用程序作为 Office add-in。 add-in 包括我使用 iframe 的应用程序,因此我需要调整我的应用程序的 'Content Security Policy' header 以使其正常工作。

目前 header 的值为 content-security-policy: frame-src 'self' ; frame-ancestors 'self'; object-src 'none';。如果我将 header 设置为 content-security-policy: frame-src 'self' * ; frame-ancestors 'self' *; object-src 'none'; 一切正常,但我不想让每个人都使用 iframe 包含我的应用程序。

所以我尝试限制使用 content-security-policy: frame-src 'self' https://outlook.live.com; frame-ancestors 'self' https://outlook.live.com ; object-src 'none'; 之类的东西,但这不起作用。 Chrome 的 JS 控制台显示:拒绝框架 'https://auth-recette-broc.kinexo.fr/' 因为祖先违反了以下内容安全策略指令:“frame-ancestors 'self' https:// outlook.live.com".

而 'csp report' 是:

{
  "document-uri": "https://auth-mywebsite.xx/",
  "referrer": "https://yy-mywebsite.xx/",
  "violated-directive": "frame-ancestors",
  "effective-directive": "frame-ancestors",
  "original-policy": "frame-src 'self'  https://outlook.live.com; frame-ancestors 'self' https://outlook.live.com ; object-src 'none';",
  "disposition": "report",
  "blocked-uri": "https://auth-mywebsite.xx/",
  "status-code": 0,
  "script-sample": ""
}

我不明白这个csp-report

  1. 为什么 parent 框架 URI (https://outlook.live.com) 的 url 没有显示为 document-uri
  2. 为什么 document-uriblocked-uri 属性相同而允许 self 框架时框架被拒绝?
  3. 如何知道预期的 url 为 frame-ancestor 以允许框架?

Why the url of the parent frame URI (https://outlook.live.com) does not appear as document-uri ?

frame-ancestors 指令是特殊的 - 它作用于 parent 页面,而其他指令作用于页面本身(发布了 CSP)。
因此,如果 frame-ancestors 违规,document-uriframe itself 的 Url(它发布 CSP)。

Why the frame is refused when the document-uri and blocked-uri attributes are identical and self is allowed to frame ?

blocked-uri 如果 frame-ancestors 取决于浏览器:

  • Chrome 显示为 blocked-uri 锁定容器 (iframe) 本身的 URI。
  • Firefox 从 top-level window 获取 blocked-uri(但对于嵌套级别 >1 Firefox 有一个错误并发送一个空的 blocked-uri)。

因此,在 Chrome 中 blocked-uri 将等于 document-uri

据我了解,您有一个 https://yy-mywebsite.xx 页面,其中嵌入了 https://auth-mywebsite.xx/
https://auth-mywebsite.xx/ 页面发布了一个 CSP frame-ancestors 'self'; object-src 'none';
在这种情况下 'self' 表示 https://auth-mywebsite.xx/ 因此不允许嵌入到 https://yy-mywebsite.xx 中。您必须至少 frame-ancestors 'self' https://yy-mywebsite.xx; object-src 'none';

How to know the expected url as frame-ancestor to allow framing ?

frame-ancestors 指令检查所有祖先(parent 的整个上游链),而不仅仅是最近的 parent。因此,您必须指定允许嵌入您的 iframe 的所有主机。