【问题标题】:Get around same-origin policy to get top URL of a page from inside a cross-domain iframe绕过同源策略以从跨域 iframe 中获取页面的顶部 URL
【发布时间】:2015-05-21 14:33:50
【问题描述】:

我在第三方网站的 iframe 中运行了一些代码。有些会直接在首页,有些会在另一个 iframe 中,其中一些可能是跨域的。我需要找到一种方法来使用任何必要的方式获取首页的 URL 值。

由于跨域策略,我能走的最远是直到浏览器停止代码正在执行的操作。我发现错误并查看我所在的当前窗口上下文的引用者。大多数情况下,此页面上方的页面是首页,但不一定。

我能看到的唯一方法是建立一个我认为是首页的 URL 列表,然后发送一个带有 JS 浏览器的机器人,通过查看我的代码所达到的 iframe 是否实际上是直接的嵌套在其中。

这仍然不是特别准确,我敢肯定一定有另一种方法......

感谢任何可以提供帮助的人。

【问题讨论】:

    标签: javascript security iframe cross-domain phantomjs


    【解决方案1】:

    如果不与某种外部系统进行通信,绝对不可能。收集数据的最干净/最准确的方法是在浏览器允许的情况下获取顶部窗口 URL,但捕获错误并使用带有标志的引用者来说明它是引用者。

    【讨论】:

      【解决方案2】:

      实际上有一种方法可以在 Chrome 和 Opera 中获取域(在多个嵌套的跨域 iframe 中),但在其他浏览器中是不可能的。

      您需要使用“window.location.ancestorOrigins”属性。

      我在下面创建了一个 sn-p 代码,它应该对你有用,如果你认为你可以改进代码或 cmets,请不要犹豫,在 Github 上编辑 gist,以便我们可以做得更好:

      要点:https://gist.github.com/ocundale/281f98a36a05c183ff3f.js

      代码(ES2015):

      // return topmost browser window of current window & boolean to say if cross-domain exception occurred
      const getClosestTop = () => {
          let oFrame  = window,
              bException = false;
      
          try {
              while (oFrame.parent.document !== oFrame.document) {
                  if (oFrame.parent.document) {
                      oFrame = oFrame.parent;
                  } else {
                      //chrome/ff set exception here
                      bException = true;
                      break;
                  }
              }
          } catch(e){
              // Safari needs try/catch so sets exception here
              bException = true;
          }
      
          return {
              'topFrame': oFrame,
              'err': bException
          };
      };
      
      // get best page URL using info from getClosestTop
      const getBestPageUrl = ({err:crossDomainError, topFrame}) => {
          let sBestPageUrl = '';
      
          if (!crossDomainError) {
              // easy case- we can get top frame location
              sBestPageUrl = topFrame.location.href;
          } else {
              try {
                  try {
                      // If friendly iframe
                      sBestPageUrl = window.top.location.href;
                  } catch (e) {
                      //If chrome use ancestor origin array
                      let aOrigins = window.location.ancestorOrigins;
                      //Get last origin which is top-domain (chrome only):
                      sBestPageUrl = aOrigins[aOrigins.length - 1];
                  }
              } catch (e) {
                  sBestPageUrl = topFrame.document.referrer;
              }
          }
      
          return sBestPageUrl;
      };
      
      // To get page URL, simply run following within an iframe on the page:
      const TOPFRAMEOBJ = getClosestTop();
      const PAGE_URL = getBestPageUrl(TOPFRAMEOBJ);
      

      如果有人想要标准 ES5 中的代码,请告诉我,或者直接通过转换器在线运行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-17
        • 2012-12-30
        • 2014-12-17
        • 2012-01-26
        • 1970-01-01
        • 1970-01-01
        • 2014-02-01
        • 2015-07-11
        相关资源
        最近更新 更多