【发布时间】:2018-06-01 09:19:08
【问题描述】:
总结:
位于 https://localhost:3000 的站点,Content-Security-Policy 值为 default-src 'self' 'unsafe-inline' https://localhost:3001/https_index.html 包含指向 https://localhost:3001/index.html 的 iframe。 :3001/index.html 的内容包含一个<a href="mailto..."></a>。单击该链接失败:Refused to frame '' because it violates the following Content Security Policy directive...。 如何更改我的 CSP 值以防止出现此错误;在用户的首选电子邮件客户端中打开新电子邮件(mailto 的正常行为)?我正在使用 Chrome1
详情:
与this question "mailto link not working within a frame chrome (over https) "相似但不同
我认为我的不是重复的,因为:
-
我无法重现该错误,当我尝试重现他们的步骤时,我看到一个关于mixed-content 的控制台警告:
混合内容:“https://localhost:3001/https_index.html”处的页面通过 HTTPS 加载,但请求了不安全的资源“mailto:...”。此内容也应通过 HTTPS 提供。
-
我的步骤是具体的;我的页面和它的 iframe src 都是
https,但页面本身提供了特定的 限制性Content-Security-Policy(CSP):app.use(csp({ directives: { defaultSrc: ["'self' 'unsafe-inline' https://localhost:3001/https_index.html"] } })); -
另外产生的错误我可以重现是不同的:
拒绝框架'',因为它违反了以下内容安全策略指令:“default-src 'self' https://localhost:3001/https_index.html”。请注意,'frame-src' 没有显式设置,因此 'default-src' 用作后备。
- 原始问题的accepted answers将帮助我解决我的 CSP 特定问题,也就是说,如果我在链接中添加
target="_top",电子邮件客户端打开时没有错误:<a target="_top" href="mailto:...">email</a>类似的修复适用于另一个 similar but different issue. 但是,这可能1有时会打开一个新标签
所以我的问题是关于Content-Security-Policy error (see above):
...拒绝框架'',因为它违反了以下内容安全策略指令:...
注意上面写着frame ''。该框架被识别为空字符串!
通常如果某些资源违反 CSP,the URL of the resource is identified; i.e.
拒绝加载脚本'http://evil.com/evil.js'...
如果CSP-violating URL 被识别 + 如果我可以使用它;将其添加到我的 CSP 值中以获取 default-src:
`app.use(csp({
directives: {
defaultSrc: ["http://evil.com/evil.js 'self' 'unsafe-inline' https://localhost:3001/https_index.html"]
}
}));`
但是我可以允许href 值的例外吗?专门针对mailto?我试过像mailto*这样的通配符,但是:
内容安全策略指令“default-src”的源列表包含无效源:“mailto*”。
我想知道是否有任何通配符可以工作; Chrome 是否真的将 href="mailto..." 框架视为空字符串?我想是的,因为它本身不是 URL; Chrome“想要”在 iframe 的上下文中启动外部应用程序(即 Outlook);谁被其父页面的CSP规则绑定...
脚注:
- Chrome 在 CSP 或沙盒案例中显示上述错误。尽管
CSP的值,Internet Explorer 不会抱怨iframeshref。尽管sandbox的值,Internet Explorer 也没有“新标签”问题。 IE 11.1914 只会给出消息:
-
使用
target="_top"的修复可能会打开一个新标签,如果你已经sandboxed 你的 iframe! (sandbox与CSP不同)。我不喜欢新标签。 Chrome 给了我这个错误...不安全的 JavaScript 尝试从 URL 为“https://localhost:3001/index.html”的框架启动对 URL 为“http://localhost:3000/”的框架的导航。顶层窗口尝试导航的框架被沙盒化,但未设置“allow-top-navigation”或“allow-top-navigation-by-user-activation”标志。
...但打开了一个新选项卡,以及 Outlook 电子邮件客户端...
我按照错误提示做了;修改iframe sandbox 属性的值:
sandbox="allow-top-navigation allow-same-origin ...",并且 mailto 链接有效(和以前一样),但 没有 打开过多的新标签。太好了!
【问题讨论】:
标签: iframe href mailto content-security-policy