【问题标题】:How to restrict website to load only inside an iframe?如何限制网站仅在 iframe 内加载?
【发布时间】:2021-01-01 23:47:25
【问题描述】:

我有两个网站:

  • https://exampleiframe.com(第三方网站),
  • https://example.com(我的网站)

我想限制 https://example.com 仅在 https://exampleiframe.com 的 iframe 内加载

我在 https://example.com 的响应标头中的 CSP 下面添加了فاث。

"Content-Security-Policy": "frame-ancestors 'self' https://exampleiframe.com"

此政策仅限制来自 iframe 的请求。但我不想在这个 iframe 之外的任何地方加载这个 url (https://example.com)。

【问题讨论】:

    标签: javascript html iframe content-security-policy playframework-2.5


    【解决方案1】:

    如果您希望限制通过 Url 直接访问页面并只允许在 iframe 中显示它 - CSP 无法解决此问题。 CSP 仅限制 Urls 的框架(您为此显示了正确的 CSP)。

    但是你可以使用 JavaScript 并且如果 self === top 执行重定向到 /not_allowed Url。
    我可以使用data- 之类的<iframe data-access='framed' src=''></iframe> 属性并通过iframe 中的JS 检查此属性(如果可以访问)。 Direct Url 不会有这样的data- 属性。

    但是这种检查会因禁用 javascript 而失败。

    【讨论】:

      猜你喜欢
      • 2014-12-25
      • 2019-04-18
      • 2013-09-02
      • 2013-11-19
      • 2018-06-12
      • 2014-04-22
      • 2017-11-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多