0

This I don't understand. I made a simple recursive function to search in whatever object I pass to it.
It works as I expect unless I try to give it an array (or object) of window properties as the first parameter.

window.search_object([window.API, window.Chat], /vote/);  //Error: Not allowed to enumerate cross origin objects
window.search_object({x:window.API, y:window.Chat}, /vote/);  //Error: Not allowed to enumerate cross origin objects

The error occurs on line with for(var i in object).

The object search function:

window.search_object = function(object, search_string, max_recursion, search_value, history, path) {
    //This will be returned in the end
    var results = [];
    //Just ignore document object
    if(object == window.document)
      return [];
    //Only created in level 1 function, others get this passed by parameter
    if(history==null)
      history = {data:[]};
    //Simplest way to find the origin of found values
    if(path==null)
      path = [];
    //Convert sring to regext (I'm lazy)
    if(typeof search_string=="string") {
      search_string = new RegExp(search_string);
    }        
    //Some errors
    if(!(search_string instanceof window.RegExp)) {
      console.log([search_string, search_string instanceof window.RegExp])
      throw new Error("search_object() -> Parameter 2 (searched string) must be a string or RegExp object.");    
    }
    if(typeof object != "object") {
      throw new Error("search_object() -> Parameter 1 (object to search in) must be a Javascript object");    
    }
    //Loop
    for(var i in object) {   //<-------THIS IS THE ERROR LINE
        //Test if the property name matches
        if(search_string.test(i)) {
          //Add in results (no duplicate fallback here)
          results.push([object[i], path.concat(i)]);
        }
        //Recursion
        if(typeof object[i]=="object") {
          //Check if we did not parse this object already
          if(history.data.find(object[i])===false) {
            //Remember the object
            history.data.push(object[i]);
            //Add results from all sub-objects
            results = results.concat(window.search_object(object[i], search_string, null, null, history, path.concat(i)));
          }
        }
    }
    //console.log(results);
    return results;
}

Array.prototype.find = function(needle) {
    for(var i=0; i<this.length; i++) {
       if(this[i]==needle)
         return i;
    }
    return false;
} 
Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • You are aware of the [Same-origin policy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Same_origin_policy_for_JavaScript)? – Teemu Jul 09 '13 at 19:59
  • How could I break this in this case? – Tomáš Zato Jul 09 '13 at 20:06
  • Please check this [SO answer](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy/3076648#3076648), maybe you can find your answer... – Teemu Jul 09 '13 at 20:10
  • Please take a note that I am, in fact, not looping through `window` object - non of the objects I pass as argument leave any refference back to window object (as far as I know). And, again, the error is thrown before the `for` loop starts. I see no way the script could access different frames contained in window. – Tomáš Zato Jul 09 '13 at 20:13
  • What is `API`? What is `Chat`? – user123444555621 Jul 09 '13 at 20:14
  • Some objects with huge structure. Is there anything they should countain to make it matter what they are? I have a suspition that they cause the error, yet it cannot be the any of errors meant so far. Finding the issue in them will not be easy since these are the obejct I actually wanted to search in. – Tomáš Zato Jul 09 '13 at 20:15

1 Answers1

0

The window object has circular references. This will cause a -- wait for it -- a stack overflow.

jeffo
  • 410
  • 1
  • 3
  • 9
  • What is circular reference? I have a recursion fallback (I never parse one object twice). The error is thrown before the loop even starts. – Tomáš Zato Jul 09 '13 at 19:54
  • @TomášZato See the [Wikipedia circular reference article](http://en.wikipedia.org/wiki/Circular_reference#In_computer_programming) – ajp15243 Jul 09 '13 at 20:05
  • This has no relation to my problem. The function **will not loop one object twice** (I've tested this on dummy objects!). Also, the object I pass in the array do **not contain any refference to window object**. – Tomáš Zato Jul 09 '13 at 20:14