2012-03-12 8 views
2

iframe(同じ起点)を含むウィンドウがあるため、このiframeからのスクリプトは単にtop.fooを参照することによって上部ウィンドウの属性にアクセスできます。これらの属性の一部にアクセスを許可し、ブラックリスト経由で他のユーザーを非表示にしたいと考えています。iframe内のスクリプトからオブジェクトの特定のプロパティを非表示にする

これは私がこれまで持っているものです。

(function(){ 
    var private = PrivateObject; 
    Object.defineProperty(window, 'PrivateObject', { 
     get: function getter() { 
      if (!(getter.caller instanceof Function)) { 
       throw 'You can\'t access PrivateObject from the iframe'; 
      } 
      return private; 
     }, 
     set: function setter(x) { 
      if (!(setter.caller instanceof Function)) { 
       throw 'You can\'t access PrivateObject from the iframe'; 
      } 
      private = x; 
     }, 
    }); 
})(); 

この背後にある基本的な考え方はf.caller instanceof Functionwindow1.Function !== window2.Function以来、外国ウィンドウオブジェクトからの呼び出しを検出しなければならないことです。

ただし、このdoes not workは、アクセサがトップレベルコードから呼び出された場合、f.caller === nullです。どんな解決策ですか?今のところ、私はそれがトップレベルの呼び出しを検出することが可能だとは思わないので、次のようなアプローチで行くことに決めました

+0

申し訳ありません。ナイーブであれば.callerがnullの場合、これはトップレベルのコードからは機能しませんか?例外はスローされませんか? – dave

+0

この方法では、偽陽性と偽陰性を得るという問題があります。だから、iframeのトップレベルコードからのアクセスをブロックしながら、親のトップレベルコードからのアクセスを許可するソリューションを探しています。 – user123444555621

答えて

0

/** 
* Hide objects from access from other window objects. For example, this may be used to prevent access to 
* top.Ext from scipts inside iframes. 
* <strong>Warning:</strong> This does not work reliably, since calls from top-level code cannot be detected. 
* You may either <strong>allow all</strong> top-level access (from top and other windows), or <strong>disallow all</strong> top-level access. 
* Also remember that objects may have indirect references. 
* @param {Object} object The object whose properties shall be hidden 
* @param {Array|String} properties A comma-separated list or an array of property names 
* @param {Boolean} allowTopLevel <tt>true</tt> to allow access from top-level code. Defaults to <tt>false</tt> 
*/ 
hideObjectsFromFrames = function (object, properties, allowTopLevel) { 
    if (typeof properties == 'string') { 
     properties = properties.split(/ *, */); 
    } 
    Ext.each(properties, function (property) { 
     var orig = object[property]; 
     if (allowTopLevel) { // checking outside the accessors improves performance 
      Object.defineProperty(object, property, { 
       get: function g() { 
        if (g.caller && !(g.caller instanceof Function)) { 
         throw 'Security error. Attempt to access ' + property + ' from foreign window'; 
        } 
        return orig; 
       }, 
       set: function s(x) { 
        if (s.caller && !(s.caller instanceof Function)) { 
         throw 'Security error. Attempt to overwrite ' + property + ' from foreign window'; 
        } 
        orig = x; 
       } 
      }); 
     } else { 
      Object.defineProperty(object, property, { 
       get: function g() { 
        if (!(g.caller instanceof Function)) { 
         throw 'Security error. Attempt to access ' + property + ' from foreign window'; 
        } 
        return orig; 
       }, 
       set: function s(x) { 
        if (!(s.caller instanceof Function)) { 
         throw 'Security error. Attempt to overwrite ' + property + ' from foreign window'; 
        } 
        orig = x; 
       } 
      }); 
     } 
    }); 
}; 

誰もがよりよい解決策を思い付く場合は、してくださいお知らせ下さい!

+0

FWIW、Safari 5.0に 'allowTopLevel'が正しく動作しないというバグがあります:https://bugs.webkit.org/show_bug.cgi?id=45480 – user123444555621

関連する問題