tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_dom_xrays.html (21916B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=787070
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Test for Bug 787070</title>
      9  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
     10  <script src="file_reflected_attribute_frozenarray.js"></script>
     11  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
     12 </head>
     13 <body>
     14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=787070">Mozilla Bug 787070</a>
     15 <p id="display"></p>
     16 <div id="content" style="display: none">
     17 <iframe id="t"></iframe>
     18 </div>
     19 <pre id="test">
     20 <script type="application/javascript">
     21 
     22 /** Test for Bug 1021066 */
     23 
     24 // values should contain the values that the property should have on each of
     25 // the objects on the prototype chain of obj. A value of undefined signals
     26 // that the value should not be present on that prototype.
     27 function checkXrayProperty(obj, name, values) {
     28  var instance = obj;
     29  do {
     30    var value = values.shift();
     31    if (typeof value == "undefined") {
     32      ok(!obj.hasOwnProperty(name), "hasOwnProperty shouldn't see \"" + String(name) + "\" through Xrays");
     33      is(Object.getOwnPropertyDescriptor(obj, name), undefined, "getOwnPropertyDescriptor shouldn't see \"" + String(name) + "\" through Xrays");
     34      ok(!Object.keys(obj).includes(name), "Enumerating the Xray should not return \"" + String(name) + "\"");
     35      ok(!Object.getOwnPropertyNames(obj).includes(name),
     36         `The Xray's property names should not include ${String(name)}`);
     37      ok(!Object.getOwnPropertySymbols(obj).includes(name),
     38         `The Xray's property symbols should not include ${String(name)}`);
     39    } else {
     40      ok(obj.hasOwnProperty(name), "hasOwnProperty should see \"" + String(name) + "\" through Xrays");
     41      var pd = Object.getOwnPropertyDescriptor(obj, name);
     42      ok(pd, "getOwnPropertyDescriptor should see \"" + String(name) + "\" through Xrays");
     43      if (pd && pd.get) {
     44        is(pd.get.call(instance), value, "Should get the right value for \"" + String(name) + "\" through Xrays");
     45      } else {
     46        is(obj[name], value, "Should get the right value for \"" + String(name) + "\" through Xrays");
     47      }
     48      if (pd) {
     49        if (pd.enumerable) {
     50          ok(Object.keys(obj).indexOf("" + name) > -1, "Enumerating the Xray should return \"" + String(name) + "\"");
     51        }
     52        if (typeof name == "symbol") {
     53          ok(Object.getOwnPropertySymbols(obj).includes(name),
     54             `The Xray's property symbols should include ${String(name)}`);
     55        } else {
     56          ok(Object.getOwnPropertyNames(obj).includes("" + name),
     57             `The Xray's property names should include ${name}`);
     58        }
     59      }
     60    }
     61  } while ((obj = Object.getPrototypeOf(obj)));
     62 }
     63 
     64 function checkWindowXrayProperty(win, name, { windowValue, windowPrototypeValue, namedPropertiesValue, eventTargetPrototypeValue }) {
     65  checkXrayProperty(win, name, [ windowValue, windowPrototypeValue, namedPropertiesValue, eventTargetPrototypeValue ]);
     66 }
     67 function checkDocumentXrayProperty(doc, name, { documentValue, htmlDocumentPrototypeValue, documentPrototypeValue, nodePrototypeValue, eventTargetPrototypeValue }) {
     68  checkXrayProperty(doc, name, [ documentValue, htmlDocumentPrototypeValue, documentPrototypeValue, nodePrototypeValue, eventTargetPrototypeValue ]);
     69 }
     70 
     71 function loadFrame() {
     72  let frame = document.getElementById("t");
     73  let win = frame.contentWindow;
     74  let { promise: loaded, resolve } = Promise.withResolvers();
     75  frame.addEventListener("load", resolve, { once: true });
     76  frame.src = "http://example.org/tests/dom/bindings/test/file_dom_xrays.html";
     77  return loaded.then(() => ({ frame, win, doc: frame.contentDocument }));
     78 }
     79 
     80 async function test() {
     81  // Window
     82  let { frame, win, doc } = await loadFrame();
     83 
     84  var winProto = Object.getPrototypeOf(win);
     85  is(winProto, win.Window.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
     86 
     87  var namedPropertiesObject = Object.getPrototypeOf(winProto);
     88  is(Cu.getClassName(namedPropertiesObject, /* unwrap = */ true), "WindowProperties", "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
     89 
     90  var eventTargetProto = Object.getPrototypeOf(namedPropertiesObject);
     91  is(eventTargetProto, win.EventTarget.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
     92 
     93  let docProto = Object.getPrototypeOf(doc);
     94  is(docProto, win.HTMLDocument.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
     95 
     96  // Xrays need to filter expandos.
     97  checkDocumentXrayProperty(doc, "expando", {});
     98  ok(!("expando" in doc), "Xrays should filter expandos");
     99 
    100  checkDocumentXrayProperty(doc, "shadowedIframe", {});
    101  ok(!("shadowedIframe" in doc), "Named properties should not be exposed through Xrays");
    102 
    103  // Named properties live on the named properties object for global objects,
    104  // but are not exposed via Xrays.
    105  checkWindowXrayProperty(win, "iframe", {});
    106  ok(!("iframe" in win), "Named properties should not be exposed through Xrays");
    107 
    108  // Window properties live on the instance, shadowing the properties of the named property object.
    109  checkWindowXrayProperty(win, "document", { windowValue: doc });
    110  ok("document" in win, "WebIDL properties should be exposed through Xrays");
    111 
    112  // Unforgeable properties live on the instance, shadowing the properties of the named property object.
    113  checkWindowXrayProperty(win, "self", { windowValue: win });
    114  ok("self" in win, "WebIDL properties should be exposed through Xrays");
    115 
    116  // Named properties live on the instance for non-global objects, but are not
    117  // exposed via Xrays.
    118  checkDocumentXrayProperty(doc, "iframe", {});
    119  ok(!("iframe" in doc), "Named properties should not be exposed through Xrays");
    120 
    121  // Object.prototype is at the end of the prototype chain.
    122  var obj = win;
    123  var proto;
    124  while ((proto = Object.getPrototypeOf(obj))) {
    125    obj = proto;
    126  }
    127  is(obj, win.Object.prototype, "Object.prototype should be at the end of the prototype chain");
    128 
    129  // Named properties shouldn't shadow WebIDL- or ECMAScript-defined properties.
    130  checkWindowXrayProperty(win, "addEventListener", { eventTargetPrototypeValue: eventTargetProto.addEventListener });
    131  is(win.addEventListener, eventTargetProto.addEventListener, "Named properties shouldn't shadow WebIDL-defined properties");
    132 
    133  is(win.toString, win.Object.prototype.toString, "Named properties shouldn't shadow ECMAScript-defined properties");
    134 
    135  // WebIDL interface names should be exposed.
    136  var waivedWin = Cu.waiveXrays(win);
    137  checkWindowXrayProperty(win, "Element", { windowValue: Cu.unwaiveXrays(waivedWin.Element) });
    138 
    139  // JS standard classes should be exposed.
    140  checkWindowXrayProperty(win, "Array", { windowValue: Cu.unwaiveXrays(waivedWin.Array) });
    141 
    142  // HTMLDocument
    143  // Unforgeable properties live on the instance.
    144  checkXrayProperty(doc, "location", [ win.location ]);
    145  is(String(win.location), frame.src,
    146     "Should have the right stringification");
    147 
    148  // HTMLHtmlElement
    149  var elem = doc.documentElement;
    150 
    151  var elemProto = Object.getPrototypeOf(elem);
    152  is(elemProto, win.HTMLHtmlElement.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
    153 
    154  elemProto = Object.getPrototypeOf(elemProto);
    155  is(elemProto, win.HTMLElement.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
    156 
    157  elemProto = Object.getPrototypeOf(elemProto);
    158  is(elemProto, win.Element.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
    159 
    160  elemProto = Object.getPrototypeOf(elemProto);
    161  is(elemProto, win.Node.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
    162 
    163  elemProto = Object.getPrototypeOf(elemProto);
    164  is(elemProto, win.EventTarget.prototype, "The proto chain of the Xray should mirror the prototype chain of the Xrayed object");
    165 
    166  // Xrays need to filter expandos.
    167  ok(!("expando" in elem), "Xrays should filter expandos");
    168 
    169  // WebIDL-defined properties live on the prototype.
    170  checkXrayProperty(elem, "version", [ undefined, "" ]);
    171  is(elem.version, "", "WebIDL properties should be exposed through Xrays");
    172 
    173  // HTMLCollection
    174  var coll = doc.getElementsByTagName("div");
    175 
    176  // Named properties live on the instance for non-global objects.
    177  checkXrayProperty(coll, "iframe", [ doc.getElementById("iframe") ]);
    178 
    179  // Indexed properties live on the instance.
    180  checkXrayProperty(coll, 0, [ doc.getElementById("shadowedIframe") ]);
    181 
    182  // WebIDL-defined properties live on the prototype, overriding any named properties.
    183  checkXrayProperty(coll, "item", [ undefined, win.HTMLCollection.prototype.item ]);
    184 
    185  // ECMAScript-defined properties live on the prototype, overriding any named properties.
    186  checkXrayProperty(coll, "toString", [ undefined, undefined, win.Object.prototype.toString ]);
    187 
    188  // Constructors
    189  img = new win.Image();
    190  ok(win.HTMLImageElement.isInstance(img), "Constructor created the right type of object");
    191 
    192  let threw;
    193  try {
    194    threw = false;
    195    win.Image();
    196  } catch (e) {
    197    threw = true;
    198  }
    199  ok(threw, "Constructors should throw when called without new");
    200 
    201  try {
    202    threw = false;
    203    new win.Node();
    204  } catch (e) {
    205    threw = true;
    206  }
    207  ok(threw, "Constructing an interface without a constructor should throw");
    208 
    209  // Frozen arrays should come from our compartment, not the target one.
    210  var languages1 = win.navigator.languages;
    211  isnot(languages1, undefined, "Must have .languages");
    212  ok(Array.isArray(languages1), ".languages should be an array");
    213  ok(Object.isFrozen(languages1), ".languages should be a frozen array");
    214  ok(!Cu.isXrayWrapper(languages1), "Should have our own version of array");
    215  is(Cu.getGlobalForObject(languages1), window,
    216     "languages1 should come from our window");
    217  // We want to get .languages in the content compartment, but without waiving
    218  // Xrays altogether.
    219  var languages2 = win.eval("navigator.languages");
    220  isnot(languages2, undefined, "Must still have .languages");
    221  ok(Array.isArray(languages2), ".languages should still be an array");
    222  ok(Cu.isXrayWrapper(languages2), "Should have xray for content version of array");
    223  is(Cu.getGlobalForObject(languages2), win,
    224     "languages2 come from the underlying window");
    225  ok(Object.isFrozen(languages2.wrappedJSObject),
    226     ".languages should still be a frozen array underneath");
    227  isnot(languages1, languages2, "Must have distinct arrays");
    228  isnot(languages1, languages2.wrappedJSObject,
    229        "Must have distinct arrays no matter how we slice it");
    230 
    231  // Check that deleters work correctly in the [OverrideBuiltins] case.
    232  elem = win.document.documentElement;
    233  var dataset = elem.dataset;
    234  is(dataset.foo, undefined, "Should not have a 'foo' property");
    235  ok(!("foo" in dataset), "Really should not have a 'foo' property");
    236  is(elem.getAttribute("data-foo"), null,
    237     "Should not have a 'data-foo' attribute");
    238  ok(!elem.hasAttribute("data-foo"),
    239     "Really should not have a 'data-foo' attribute");
    240  dataset.foo = "bar";
    241  is(dataset.foo, "bar", "Should now have a 'foo' property");
    242  ok("foo" in dataset, "Really should have a 'foo' property");
    243  is(elem.getAttribute("data-foo"), "bar",
    244     "Should have a 'data-foo' attribute");
    245  ok(elem.hasAttribute("data-foo"),
    246     "Really should have a 'data-foo' attribute");
    247  delete dataset.foo;
    248  is(dataset.foo, undefined, "Should not have a 'foo' property again");
    249  ok(!("foo" in dataset), "Really should not have a 'foo' property again");
    250  is(elem.getAttribute("data-foo"), null,
    251     "Should not have a 'data-foo' attribute again");
    252  ok(!elem.hasAttribute("data-foo"),
    253     "Really should not have a 'data-foo' attribute again");
    254 
    255  // Check that deleters work correctly in the non-[OverrideBuiltins] case.
    256  var storage = win.sessionStorage;
    257  is(storage.foo, undefined, "Should not have a 'foo' property");
    258  ok(!("foo" in storage), "Really should not have a 'foo' property");
    259  is(storage.getItem("foo"), null, "Should not have an item named 'foo'");
    260  storage.foo = "bar";
    261  is(storage.foo, "bar", "Should have a 'foo' property");
    262  ok("foo" in storage, "Really should have a 'foo' property");
    263  is(storage.getItem("foo"), "bar", "Should have an item named 'foo'");
    264  delete storage.foo;
    265  is(storage.foo, undefined, "Should not have a 'foo' property again");
    266  ok(!("foo" in storage), "Really should not have a 'foo' property again");
    267  is(storage.getItem("foo"), null, "Should not have an item named 'foo' again");
    268 
    269  // Non-static properties are not exposed on interface objects or instances.
    270  is(win.HTMLInputElement.checkValidity, undefined,
    271     "Shouldn't see non-static property on interface objects");
    272  is(Object.getOwnPropertyDescriptor(win.HTMLInputElement, "checkValidity"), undefined,
    273     "Shouldn't see non-static property on interface objects");
    274  is(Object.getOwnPropertyNames(win.HTMLInputElement).indexOf("checkValidity"), -1,
    275     "Shouldn't see non-static property on interface objects");
    276  isnot(typeof doc.createElement("input").checkValidity, "undefined",
    277        "Should see non-static property on prototype objects");
    278  is(Object.getOwnPropertyDescriptor(doc.createElement("input"), "checkValidity"), undefined,
    279     "Shouldn't see non-static property on instances");
    280  isnot(typeof Object.getOwnPropertyDescriptor(win.HTMLInputElement.prototype, "checkValidity"), "undefined",
    281        "Should see non-static property on prototype objects");
    282 
    283  // Static properties are not exposed on prototype objects or instances.
    284  isnot(typeof win.URL.createObjectURL, "undefined",
    285        "Should see static property on interface objects");
    286  isnot(typeof Object.getOwnPropertyDescriptor(win.URL, "createObjectURL"), "undefined",
    287        "Should see static property on interface objects");
    288  isnot(Object.getOwnPropertyNames(win.URL).indexOf("createObjectURL"), -1,
    289        "Should see static property on interface objects");
    290  is(new URL("http://example.org").createObjectURL, undefined,
    291     "Shouldn't see static property on instances and prototype ojbects");
    292  is(Object.getOwnPropertyDescriptor(new URL("http://example.org"), "createObjectURL"), undefined,
    293     "Shouldn't see static property on instances");
    294  is(Object.getOwnPropertyDescriptor(win.URL.prototype, "createObjectURL"), undefined,
    295     "Shouldn't see static property on prototype objects");
    296 
    297  // Unforgeable properties are not exposed on prototype objects or interface
    298  // objects.
    299  is(Window.document, undefined,
    300     "Shouldn't see unforgeable property on interface objects");
    301  is(Object.getOwnPropertyDescriptor(Window, "document"), undefined,
    302     "Shouldn't see unforgeable property on interface objects");
    303  is(Object.getOwnPropertyNames(Window).indexOf("document"), -1,
    304     "Shouldn't see unforgeable property on interface objects");
    305  isnot(typeof win.document, "undefined",
    306        "Should see unforgeable property on instances");
    307  isnot(typeof Object.getOwnPropertyDescriptor(win, "document"), "undefined",
    308        "Should see unforgeable property on instances");
    309  is(Object.getOwnPropertyDescriptor(Window.prototype, "document"), undefined,
    310     "Shouldn't see unforgeable property on prototype objects");
    311 
    312  // Constant properties are not exposted on instances.
    313  isnot(typeof win.Node.ELEMENT_NODE, "undefined",
    314        "Should see constant property on interface objects");
    315  isnot(typeof Object.getOwnPropertyDescriptor(win.Node, "ELEMENT_NODE"), "undefined",
    316        "Should see constant property on interface objects");
    317  isnot(Object.getOwnPropertyNames(win.Node).indexOf("ELEMENT_NODE"), -1,
    318        "Should see constant property on interface objects");
    319  isnot(typeof elem.ELEMENT_NODE, "undefined",
    320        "Should see constant property on prototype objects");
    321  is(Object.getOwnPropertyDescriptor(elem, "ELEMENT_NODE"), undefined,
    322     "Shouldn't see constant property on instances");
    323  isnot(typeof Object.getOwnPropertyDescriptor(win.Node.prototype, "ELEMENT_NODE"), "undefined",
    324        "Should see constant property on prototype objects");
    325 
    326  // Interfaces can have both static and non-static properties with the same name.
    327  isnot(typeof win.TestFunctions.staticAndNonStaticOverload, "undefined",
    328        "Should see static property on interface objects (even with non-static property with the same name)");
    329  isnot(typeof Object.getOwnPropertyDescriptor(win.TestFunctions, "staticAndNonStaticOverload"), "undefined",
    330        "Should see static property on interface objects (even with non-static property with the same name)");
    331  isnot(Object.getOwnPropertyNames(win.TestFunctions).indexOf("staticAndNonStaticOverload"), -1,
    332        "Should see static property on interface objects (even with non-static property with the same name)");
    333  isnot(typeof (new win.TestFunctions("")).staticAndNonStaticOverload, "undefined",
    334        "Should see non-static property on prototype objects (even with static property with the same name)");
    335  let testFunctions = new win.TestFunctions();
    336  is(Object.getOwnPropertyDescriptor(testFunctions, "staticAndNonStaticOverload"), undefined,
    337     "Shouldn't see non-static property on instances (even with static property with the same name)");
    338  ok(!testFunctions.staticAndNonStaticOverload(),
    339     "Should call the non-static overload on the instance");
    340  ok(win.TestFunctions.staticAndNonStaticOverload(),
    341     "Should call the static overload on the interface object");
    342  isnot(typeof Object.getOwnPropertyDescriptor(win.TestFunctions.prototype, "staticAndNonStaticOverload"), "undefined",
    343        "Should see non-static property on prototype objects (even with static property with the same name)");
    344  is(Object.getOwnPropertyDescriptor(win.TestFunctions, "staticAndNonStaticOverload").value,
    345     Object.getOwnPropertyDescriptor(win.TestFunctions, "staticAndNonStaticOverload").value,
    346     "Should get the same value when getting the static property twice");
    347  is(Object.getOwnPropertyDescriptor(win.TestFunctions.prototype, "staticAndNonStaticOverload").value,
    348     Object.getOwnPropertyDescriptor(win.TestFunctions.prototype, "staticAndNonStaticOverload").value,
    349     "Should get the same value when getting the non-static property twice");
    350  isnot(Object.getOwnPropertyDescriptor(win.TestFunctions, "staticAndNonStaticOverload").value,
    351        Object.getOwnPropertyDescriptor(win.TestFunctions.prototype, "staticAndNonStaticOverload").value,
    352        "Should get different values for static and non-static properties with the same name");
    353 
    354  // Adopting nodes should not lose expandos.
    355  elem = document.createElement("span");
    356  elem.expando = 5;
    357  is(elem.expando, 5, "We just set this property");
    358  document.adoptNode(elem);
    359  is(elem.wrappedJSObject, undefined, "Shouldn't be an Xray anymore");
    360  is(elem.expando, 5, "Expando should not get lost");
    361 
    362  // Instanceof tests
    363  var img = doc.createElement("img");
    364  var img2 = document.createElement("img");
    365  ok(img instanceof win.HTMLImageElement,
    366     "Should be an instance of HTMLImageElement from its global");
    367  ok(win.HTMLImageElement.isInstance(img), "isInstance should work");
    368  ok(HTMLImageElement.isInstance(img), "isInstance should work cross-global");
    369  ok(win.HTMLImageElement.isInstance(img2),
    370     "isInstance should work cross-global in the other direction");
    371  ok(img instanceof win.Image,
    372     "Should be an instance of Image, because Image.prototype == HTMLImageElement.prototype");
    373  ok(!win.Image.isInstance, "Shouldn't have an isInstance method here");
    374  // Image does not have a Symbol.hasInstance, but its proto
    375  // (Function.prototype) does.
    376  checkXrayProperty(win.Image, Symbol.hasInstance,
    377                    [undefined, win.Function.prototype[Symbol.hasInstance]]);
    378 
    379  // toString/@@toStringTag
    380  let imageConstructor = win.Image;
    381  is(win.Function.prototype.toString.apply(imageConstructor),
    382     Function.prototype.toString.apply(Image),
    383     "Applying Function.prototype.toString through an Xray should give the same result as applying it directly");
    384  isDeeply(Object.getOwnPropertyDescriptor(win.CSS, Symbol.toStringTag),
    385           Object.getOwnPropertyDescriptor(CSS, Symbol.toStringTag),
    386           "Getting @@toStringTag on a namespace object through an Xray should give the same result as getting it directly");
    387 
    388  // legacyCaller should work.
    389  ok(win.HTMLAllCollection.isInstance(doc.all),
    390     "HTMLDocument.all should be an instance of HTMLAllCollection");
    391  let element;
    392  try {
    393    threw = false;
    394    element = doc.all(0);
    395  } catch (e) {
    396    threw = true;
    397  }
    398  ok(!threw,
    399     "Calling an instance object for an interface marked with legacycaller shouldn't throw");
    400  checkXrayProperty(doc.all, 0, [ element ]);
    401 
    402  let [ testObject, expectedValues ] = testReflectedAttributeWithFrozenArray(win);
    403  checkReflectedAttributeWithFrozenArray(testObject.wrappedJSObject, expectedValues,
    404                                         "on Xray and object",
    405                                         (a, b) => a.wrappedJSObject == b);
    406 
    407  // Load the frame again, so we get a pristine global.
    408  ({ frame, win, doc } = await loadFrame());
    409 
    410  is(win.wrappedJSObject.TestChromeOnlyInterface, undefined,
    411     "A ChromeOnly interface shouldn't be exposed on a non-system global");
    412  obj = win.TestFunctions.createTestChromeOnlyInterface();
    413  ok(Cu.isXrayWrapper(obj), "Object should be wrapped in an Xray");
    414  is(win.wrappedJSObject.TestChromeOnlyInterface, undefined,
    415     "A ChromeOnly interface should still not be exposed on a non-system global");
    416 
    417  SimpleTest.finish();
    418 }
    419 
    420 SimpleTest.waitForExplicitFinish();
    421 SimpleTest.requestLongerTimeout(2);
    422 
    423 addLoadEvent(() => {
    424  SpecialPowers.pushPrefEnv({set: [["dom.expose_test_interfaces", true]]},
    425                            test);
    426 });
    427 
    428 </script>
    429 </pre>
    430 </body>
    431 </html>