代码之家  ›  专栏  ›  技术社区  ›  Nate Anderson

内容安全策略时mailto href inside frame不工作

  •  5
  • Nate Anderson  · 技术社区  · 7 年前

    总结 :

    站点位于 https://localhost:3000 具有 Content-Security-Policy 的值 default-src 'self' 'unsafe-inline' https://localhost:3001/https_index.html 包含指向的iframe https://localhost:3001/index.html . 的内容 :3001/index.html 包含 <a href="mailto..."></a> . 单击该链接失败: Refused to frame '' because it violates the following Content Security Policy directive... . 如何更改CSP值以防止此错误;在用户首选的电子邮件客户端中打开新电子邮件(正常行为 mailto )? 我正在使用Chrome 1.

    细节 :

    相似但不同于 this question "mailto link not working within a frame chrome (over https) "

    我认为我的不是复制品,因为:

    1. 不能 复制那个bug,我看到一个控制台 警告 关于 mixed-content 当我试图重现他们的步骤时:

      混合内容:页面位于' https://localhost:3001/https_index.html “”已通过HTTPS加载,但请求了不安全的资源“mailto:…”。此内容还应通过HTTPS提供。

    2. 我的步骤是具体的;我的页面和;其iframe src为 https ,但页面本身具有特定的 限制性的 内容安全策略 ( CSP ):

      app.use(csp({ directives: { defaultSrc: ["'self' 'unsafe-inline' https://localhost:3001/https_index.html"] } }));

    3. 以及由此产生的 错误 可以 复制不同:

      拒绝框架“”,因为它违反了以下内容安全策略指令:“default src“self” https://localhost:3001/https_index.html 。请注意,没有显式设置“frame src”,因此使用“default src”作为回退。

    图像如下: showing Google Chrome sad-face icon suggesting error; the message says 'Requests to the server have been blocked by an extension'

    1. 这个 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 '...

    如果 顾客服务提供商 -如果我可以使用违反的URL,则可以确定该URL;将其添加到我的 顾客服务提供商 的值 default-src :

    `app.use(csp({
      directives: {
        defaultSrc: ["http://evil.com/evil.js 'self' 'unsafe-inline' https://localhost:3001/https_index.html"]
        }
      }));`
    

    但是我可以允许 href 价值专门针对 电子邮件 ? 我试过通配符,比如 mailto* ,但:

    内容安全策略指令“default src”的源列表包含无效的源:“mailto*”。

    我想知道是否有通配符会起作用;Chrome真的考虑了 href="mailto..." 帧作为空字符串?我想是的,因为它本身不是一个URL;Chrome“希望”在iframe的上下文中启动外部应用程序(即Outlook);谁被绑定到其父页的CSP规则。。。

    脚注:

    1. Chrome在CSP或沙盒案例中显示上述错误。Internet Explorer不会抱怨 iframes href,尽管 顾客服务提供商 . Internet Explorer也没有“新选项卡”问题,尽管 sandbox . IE 11.1914只会给出以下信息:

    Internet Explorer dialog asking user to confirm they want to allow this page to open Outlook to a source of href, and choose to ignore this message in future

    1. 使用修复 target=“\u顶部” 可以打开新选项卡 ,如果您 沙箱 ed你的iframe!( 沙箱 不同于 顾客服务提供商 ). 我不喜欢新的标签。Chrome给了我这个错误。。。

      不安全的JavaScript试图为URL为“”的框架启动导航 http://localhost:3000/ '来自具有URL的框架' https://localhost:3001/index.html '. 尝试导航顶层窗口的框架已沙盒化,但未设置“允许顶部导航”或“允许通过用户激活进行顶部导航”的标志。

    ... 但打开了一个新选项卡,以及Outlook电子邮件客户端。。。

    showing a new tab opened in Chrome browser, whose URL is set to the "mailto:" value from within the iframe

    我做了错误提示的事情;修改 iframe 沙箱 属性: sandbox="allow-top-navigation allow-same-origin ..." ,mailto链接也起了作用(与以前一样),但确实起了作用 打开过多的新选项卡。太棒了

    1 回复  |  直到 7 年前
        1
  •  7
  •   Mark Mullan    6 年前

    在遇到同样的问题后偶然发现了这个问题。经过数小时的搜索,关于这一点的文档竟然很少。

    我的第一反应是做一些你正在做的事情, mailto* mailto:* .

    最终起作用的是省略通配符,并修改 frame-src 指令本身:

    frame-src 'self' mailto: tel: *.mydomain.com

    tel: iframes中的链接也被破坏。