【问题标题】:Howto Prevent IFraming w/ Whitelist如何使用白名单防止 IFrame
【发布时间】:2009-12-03 01:36:28
【问题描述】:

我正在创建一些 IFrameable 内容。我们希望用户能够 IFrame 此页面,但只能从一组域列表中。

有什么我们可以检查的,看看父页面的域名是什么?

if (top != self) { top.location.replace(self.location.href); }

【问题讨论】:

    标签: php javascript iframe whitelist


    【解决方案1】:

    不,如果父页面不在您的安全上下文中(同源策略),则父页面的 location 不可见。您当然可以查看自己框架的document.referrer,但这并不完全防水......客户端的引荐来源检查比服务器端的用处稍小,但仍然可以通过以下方式规避类似于框架中的刷新转发器。

    Content Security Policy 中的 frame-ancestors 限制可能有一天会允许这样做。

    【讨论】:

      【解决方案2】:

      正如 bobince 所说,document.referrer 看起来是您最好的选择。您将在 iFrame 的 src 页面中检查这一点。但是,HTTP 引用者信息很容易被欺骗,因此这种方法不是很安全。

      本文展示了如何使用 PHP:How to bypass the REFERER security check

      【讨论】:

      • 我敢打赌,大多数人都不够熟练,无法在他们的网站中构建 iframe 并让它通过有效的引用。目标是防止公众在其他人的网站上看到 iframe。感谢您的信息。
      • 这就是我的想法,所以在这种情况下,document.referrer 将完成工作。
      【解决方案3】:

      我正在使用它来检查页面是否从白名单域加载。我确信有办法解决这个问题,但它似乎有效。

      var currentUrl = document.referrer;
      var okayUrl = "http://good-domain.com";
      var okayUrl2 = "http://another-good-domain.com";
      
      //check if page is loaded in iframe
      if ( window.location !== window.parent.location ) {
          //check if parent is a whitelisted domain
          if (currentUrl == okayUrl || currentUrl == okayUrl2)
          {
           //if it is a good domain, then just log the parent url or something
           console.log(currentUrl);
           } else {
           //if it is a bad domain, then do something about it
           alert ("Woah buddy. Can't touch this!");
           window.location = "http://en.wikipedia.org/wiki/Rickrolling";
         }
      }
      

      【讨论】:

        猜你喜欢
        • 2021-07-02
        • 2011-01-07
        • 1970-01-01
        • 1970-01-01
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-21
        相关资源
        最近更新 更多