Bug 1530608 part 3 - Add tests verifying same-origin globals are same-compartment. r=bzbarsky
authorJan de Mooij <jdemooij@mozilla.com>
Mon, 04 Mar 2019 09:06:23 +0000
changeset 520091 25cfc0e0371799d741acb3bdcc44328a1fe7909a
parent 520090 23671e7b0bb7a73b4734a36e2b8981b441cf14f2
child 520092 0d9daf8104d7199600b2664557891c33118d36d0
push id10862
push userffxbld-merge
push dateMon, 11 Mar 2019 13:01:11 +0000
treeherdermozilla-beta@a2e7f5c935da [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbzbarsky
bugs1530608
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1530608 part 3 - Add tests verifying same-origin globals are same-compartment. r=bzbarsky Differential Revision: https://phabricator.services.mozilla.com/D21570
dom/base/test/iframe_shared_compartment2a.html
dom/base/test/iframe_shared_compartment2b.html
dom/base/test/mochitest.ini
dom/base/test/test_shared_compartment1.html
dom/base/test/test_shared_compartment2.html
new file mode 100644
--- /dev/null
+++ b/dom/base/test/iframe_shared_compartment2a.html
@@ -0,0 +1,2 @@
+<iframe src="http://mochi.test:8888/tests/dom/base/test/iframe_shared_compartment2b.html">
+</iframe>
new file mode 100644
--- /dev/null
+++ b/dom/base/test/iframe_shared_compartment2b.html
@@ -0,0 +1,3 @@
+<script>
+window.onload = () => window.parent.parent.go(this);
+</script>
--- a/dom/base/test/mochitest.ini
+++ b/dom/base/test/mochitest.ini
@@ -225,16 +225,18 @@ support-files =
   iframe2_bug431701.html
   iframe3_bug431701.html
   iframe4_bug431701.xml
   iframe5_bug431701.xml
   iframe6_bug431701.xml
   iframe7_bug431701.xml
   iframe1_bug426646.html
   iframe2_bug426646.html
+  iframe_shared_compartment2a.html
+  iframe_shared_compartment2b.html
   file1_setting_opener.html
   file2_setting_opener.html
   file3_setting_opener.html
   file4_setting_opener.html
   PASS.html
   FAIL.html
   !/dom/animation/test/testcommon.js
   !/dom/events/test/event_leak_utils.js
@@ -786,16 +788,18 @@ support-files =
   file_js_cache_syntax_error.js
 [test_setInterval_from_start.html]
 [test_setInterval_uncatchable_exception.html]
 skip-if = debug == false
 [test_settimeout_extra_arguments.html]
 [test_settimeout_inner.html]
 [test_setTimeoutWith0.html]
 [test_setting_opener.html]
+[test_shared_compartment1.html]
+[test_shared_compartment2.html]
 [test_style_cssText.html]
 [test_text_wholeText.html]
 [test_textnode_normalize_in_selection.html]
 [test_textnode_split_in_selection.html]
 [test_timeout_clamp.html]
 skip-if = debug == true && toolkit == 'android' # Timing dependent, skip slow debug android builds
 [test_timer_flood.html]
 [test_title.html]
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_shared_compartment1.html
@@ -0,0 +1,77 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1530608
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1530608</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript">
+
+  /** Test for Bug 1530608 **/
+  SimpleTest.waitForExplicitFinish();
+
+  var Cu = SpecialPowers.Cu;
+  var isSameCompartment = Cu.getJSTestingFunctions().isSameCompartment;
+
+  var testsDone = 0;
+  function finishIfDone() {
+    testsDone++;
+    if (testsDone === 4) {
+      SimpleTest.finish();
+    }
+  }
+
+  // Test 1: same-origin iframe.
+  function testFrame1() {
+    var frameWin = document.getElementById("frame1").contentWindow;
+    ok(isSameCompartment(window, frameWin),
+       "Same-origin iframe must be same-compartment");
+    finishIfDone();
+  }
+
+  // Test 2: cross-origin iframe.
+  function testFrame2() {
+    var frameWin = document.getElementById("frame2").contentWindow;
+    ok(!isSameCompartment(window, frameWin),
+       "Cross-origin iframe must be cross-compartment");
+    finishIfDone();
+  }
+
+  // Test 3: same-site, cross-origin iframe.
+  function testFrame3() {
+    var frame = document.getElementById("frame3");
+    ok(!isSameCompartment(window, frame.contentWindow),
+       "Same-site cross-origin iframe must be cross-compartment");
+
+    // Now load a same-origin page in this iframe.
+    frame.onload = function() {
+      ok(isSameCompartment(window, frame.contentWindow),
+         "Frame must be same-compartment now");
+      finishIfDone();
+    };
+    frame.src = "file_empty.html";
+  }
+
+  // Test 4: dynamically created iframe.
+  addLoadEvent(function() {
+    var frame = document.createElement("iframe");
+    document.body.appendChild(frame);
+    ok(isSameCompartment(window, frame.contentWindow),
+       "Newly created iframe must be same-compartment");
+    finishIfDone();
+  });
+
+  </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1530608">Mozilla Bug 1530608</a>
+
+<iframe id="frame1" onload="testFrame1()" src="file_empty.html"></iframe>
+<iframe id="frame2" onload="testFrame2()" src="http://example.org/tests/js/xpconnect/tests/mochitest/file_empty.html"></iframe>
+<iframe id="frame3" onload="testFrame3()" src="http://test1.mochi.test:8888/tests/js/xpconnect/tests/mochitest/file_empty.html"></iframe>
+
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/base/test/test_shared_compartment2.html
@@ -0,0 +1,47 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+https://bugzilla.mozilla.org/show_bug.cgi?id=1530608
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Test for Bug 1530608</title>
+  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
+  <script type="application/javascript">
+
+  /** Test for Bug 1530608 **/
+  SimpleTest.waitForExplicitFinish();
+
+  // We have the following origins:
+  //
+  // 1: this page:    mochi.test:8888
+  // 2: iframe:       example.org
+  // 3: inner iframe: mochi.test:8888
+  //
+  // Test that 1 and 2 are cross-compartment (because cross-origin), but 1 and 3
+  // are same-compartment.
+
+  function go(innerWin) {
+    var Cu = SpecialPowers.Cu;
+    var isSameCompartment = Cu.getJSTestingFunctions().isSameCompartment;
+
+    var frame = document.getElementById("frame");
+    ok(!isSameCompartment(window, frame.contentWindow),
+       "Cross-origin iframe must be cross-compartment");
+
+    ok(isSameCompartment(window, innerWin),
+       "Same-origin inner iframe must be same-compartment");
+
+    SimpleTest.finish();
+  }
+
+  </script>
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1530608">Mozilla Bug 1530608</a>
+
+<iframe id="frame" src="http://example.org/tests/dom/base/test/iframe_shared_compartment2a.html"></iframe>
+
+</body>
+</html>