Bug 1198078 - Part 2: Add tests for mixed content blocking of scripts in workers; r=ckerschb
authorEhsan Akhgari <ehsan@mozilla.com>
Fri, 11 Sep 2015 18:50:33 -0400
changeset 273535 5204bd4ad56c68506513f348959f14a17429a860
parent 273534 027edbd76ba2b32b9cd10490d8e216d982e7dc60
child 273536 69e4bb1392d58f5304842bc62bc36e21a0982b8d
push id68310
push usereakhgari@mozilla.com
push dateFri, 20 Nov 2015 16:07:23 +0000
treeherdermozilla-inbound@5204bd4ad56c [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersckerschb
bugs1198078
milestone45.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 1198078 - Part 2: Add tests for mixed content blocking of scripts in workers; r=ckerschb
dom/workers/test/foreign.js
dom/workers/test/importForeignScripts_worker.js
dom/workers/test/importScripts_mixedcontent.html
dom/workers/test/mochitest.ini
dom/workers/test/redirect_to_foreign.sjs
dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/https_test.js
dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/register.html
dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/unregister.html
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/test_importscript_mixedcontent.html
dom/workers/test/test_importScripts_mixedcontent.html
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/foreign.js
@@ -0,0 +1,1 @@
+response = "bad";
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/importForeignScripts_worker.js
@@ -0,0 +1,55 @@
+/**
+ * Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+var target = self;
+var response;
+
+function runTests() {
+  response = "good";
+  try {
+    importScripts("http://example.org/tests/dom/workers/test/foreign.js");
+  } catch(e) {
+    dump("Got error " + e + " when calling importScripts");
+  }
+  if (response === "good") {
+    try {
+      importScripts("redirect_to_foreign.sjs");
+    } catch(e) {
+      dump("Got error " + e + " when calling importScripts");
+    }
+  }
+  target.postMessage(response);
+
+  // Now, test a nested worker.
+  if (location.search !== "?nested") {
+    var worker = new Worker("importForeignScripts_worker.js?nested");
+
+    worker.onmessage = function(e) {
+      target.postMessage(e.data);
+      target.postMessage("finish");
+    }
+
+    worker.onerror = function() {
+      target.postMessage("nested worker error");
+    }
+
+    worker.postMessage("start");
+  }
+}
+
+onmessage = function(e) {
+  if (e.data === "start") {
+    runTests();
+  }
+};
+
+onconnect = function(e) {
+  target = e.ports[0];
+  e.ports[0].onmessage = function(e) {
+    if (e.data === "start") {
+      runTests();
+    }
+  };
+};
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/importScripts_mixedcontent.html
@@ -0,0 +1,46 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE html>
+<script>
+  function ok(cond, msg) {
+    window.parent.postMessage({status: "ok", data: cond, msg: msg}, "*");
+  }
+  function finish() {
+    window.parent.postMessage({status: "done"}, "*");
+  }
+
+  function testSharedWorker() {
+    var sw = new SharedWorker("importForeignScripts_worker.js");
+    sw.port.onmessage = function(e) {
+      if (e.data == "finish") {
+        finish();
+        return;
+      }
+      ok(e.data === "good", "mixed content for shared workers is correctly blocked");
+    };
+
+    sw.onerror = function() {
+      ok(false, "Error on shared worker ");
+    };
+
+    sw.port.postMessage("start");
+  }
+
+  var worker = new Worker("importForeignScripts_worker.js");
+
+  worker.onmessage = function(e) {
+    if (e.data == "finish") {
+      testSharedWorker();
+      return;
+    }
+    ok(e.data === "good", "mixed content is correctly blocked");
+  }
+
+  worker.onerror = function() {
+    ok(false, "Error on worker");
+  }
+
+  worker.postMessage("start");
+</script>
--- a/dom/workers/test/mochitest.ini
+++ b/dom/workers/test/mochitest.ini
@@ -20,16 +20,19 @@ support-files =
   csp_worker.js
   404_server.sjs
   errorPropagation_iframe.html
   errorPropagation_worker.js
   errorwarning_worker.js
   eventDispatch_worker.js
   fibonacci_worker.js
   file_bug1010784_worker.js
+  foreign.js
+  importForeignScripts_worker.js
+  importScripts_mixedcontent.html
   importScripts_worker.js
   importScripts_worker_imported1.js
   importScripts_worker_imported2.js
   importScripts_worker_imported3.js
   importScripts_worker_imported4.js
   instanceof_worker.js
   json_worker.js
   jsversion_worker.js
@@ -46,16 +49,17 @@ support-files =
   notification_worker_child-parent.js
   notification_permission_worker.js
   onLine_worker.js
   onLine_worker_child.js
   onLine_worker_head.js
   promise_worker.js
   recursion_worker.js
   recursiveOnerror_worker.js
+  redirect_to_foreign.sjs
   relativeLoad_import.js
   relativeLoad_worker.js
   relativeLoad_worker2.js
   rvals_worker.js
   sharedWorker_console.js
   sharedWorker_sharedWorker.js
   simpleThread_worker.js
   suspend_iframe.html
@@ -148,16 +152,19 @@ skip-if = (toolkit == 'gonk' && debug) #
 [test_errorPropagation.html]
 skip-if = buildapp == 'b2g' # b2g(times out) b2g-debug(times out) b2g-desktop(times out)
 [test_errorwarning.html]
 skip-if = buildapp == 'b2g' # b2g(Failed to load script: errorwarning_worker.js) b2g-debug(Failed to load script: errorwarning_worker.js) b2g-desktop(Failed to load script: errorwarning_worker.js)
 [test_eventDispatch.html]
 [test_fibonacci.html]
 skip-if = buildapp == 'b2g' # b2g(Failed to load script: fibonacci_worker.js) b2g-debug(Failed to load script: fibonacci_worker.js) b2g-desktop(Failed to load script: fibonacci_worker.js)
 [test_importScripts.html]
+[test_importScripts_mixedcontent.html]
+skip-if = buildapp == 'b2g' # no https on b2g
+tags = mcb
 [test_instanceof.html]
 [test_json.html]
 [test_jsversion.html]
 skip-if = (toolkit == 'gonk' && debug) #debug-only failure
 [test_loadEncoding.html]
 [test_loadError.html]
 [test_location.html]
 [test_longThread.html]
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/redirect_to_foreign.sjs
@@ -0,0 +1,4 @@
+function handleRequest(request, response) {
+  response.setStatusLine("1.1", 302, "Found");
+  response.setHeader("Location", "http://example.org/tests/dom/workers/test/foreign.js");
+}
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/https_test.js
@@ -0,0 +1,28 @@
+function sendResponseToParent(response) {
+  return `
+    <!DOCTYPE html>
+    <script>
+      window.parent.postMessage({status: "done", data: "${response}"}, "*");
+    </script>
+  `;
+}
+
+self.addEventListener("fetch", function(event) {
+  if (event.request.url.indexOf("index.html") >= 0) {
+    var response = "good";
+    try {
+      importScripts("http://example.org/tests/dom/workers/test/foreign.js");
+    } catch(e) {
+      dump("Got error " + e + " when importing the script\n");
+    }
+    if (response === "good") {
+      try {
+        importScripts("/tests/dom/workers/test/redirect_to_foreign.sjs");
+      } catch(e) {
+        dump("Got error " + e + " when importing the script\n");
+      }
+    }
+    event.respondWith(new Response(sendResponseToParent(response),
+                                   {headers: {'Content-Type': 'text/html'}}));
+  }
+});
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/register.html
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<script>
+  function ok(v, msg) {
+    window.parent.postMessage({status: "ok", result: !!v, message: msg}, "*");
+  }
+
+  function done(reg) {
+    ok(reg.active, "The active worker should be available.");
+    window.parent.postMessage({status: "registrationdone"}, "*");
+  }
+
+  navigator.serviceWorker.ready.then(done);
+  navigator.serviceWorker.register("https_test.js", {scope: "."});
+</script>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/unregister.html
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<script>
+  navigator.serviceWorker.getRegistration(".").then(function(registration) {
+    registration.unregister().then(function(success) {
+      if (success) {
+        window.parent.postMessage({status: "unregistrationdone"}, "*");
+      }
+    }, function(e) {
+      dump("Unregistering the SW failed with " + e + "\n");
+    });
+  });
+</script>
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -67,16 +67,19 @@ support-files =
   fetch/imagecache/register.html
   fetch/imagecache/unregister.html
   fetch/imagecache-maxage/index.html
   fetch/imagecache-maxage/image-20px.png
   fetch/imagecache-maxage/image-40px.png
   fetch/imagecache-maxage/maxage_test.js
   fetch/imagecache-maxage/register.html
   fetch/imagecache-maxage/unregister.html
+  fetch/importscript-mixedcontent/register.html
+  fetch/importscript-mixedcontent/unregister.html
+  fetch/importscript-mixedcontent/https_test.js
   fetch/interrupt.sjs
   fetch/origin/index.sjs
   fetch/origin/index-to-https.sjs
   fetch/origin/realindex.html
   fetch/origin/realindex.html^headers^
   fetch/origin/register.html
   fetch/origin/unregister.html
   fetch/origin/origin_test.js
@@ -286,8 +289,10 @@ skip-if = toolkit == "android" || toolki
 skip-if = e10s # Bug 1214305
 [test_csp_upgrade-insecure_intercept.html]
 skip-if = e10s # Bug 1214305
 [test_serviceworker_header.html]
 [test_openWindow.html]
 skip-if = toolkit == "android" || toolkit == "gonk"
 [test_imagecache.html]
 [test_imagecache_max_age.html]
+[test_importscript_mixedcontent.html]
+tags = mcb
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/test_importscript_mixedcontent.html
@@ -0,0 +1,54 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1198078 - test that we respect mixed content blocking in importScript() inside service workers</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<p id="display"></p>
+<div id="content" style="display: none">
+<iframe></iframe>
+</div>
+<pre id="test"></pre>
+<script class="testbody" type="text/javascript">
+
+  var iframe;
+  function runTest() {
+    iframe = document.querySelector("iframe");
+    iframe.src = "https://example.com/tests/dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/register.html";
+    var ios;
+    window.onmessage = function(e) {
+      if (e.data.status == "ok") {
+        ok(e.data.result, e.data.message);
+      } else if (e.data.status == "registrationdone") {
+        iframe.src = "https://example.com/tests/dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/index.html";
+      } else if (e.data.status == "done") {
+        ok(e.data.data, "good", "Mixed content blocking should work correctly for service workers");
+        iframe.src = "https://example.com/tests/dom/workers/test/serviceworkers/fetch/importscript-mixedcontent/unregister.html";
+      } else if (e.data.status == "unregistrationdone") {
+        window.onmessage = null;
+        ok(true, "Test finished successfully");
+        SimpleTest.finish();
+      }
+    };
+  }
+
+  SimpleTest.waitForExplicitFinish();
+  onload = function() {
+    SpecialPowers.pushPrefEnv({"set": [
+      ["dom.serviceWorkers.exemptFromPerDomainMax", true],
+      ["dom.serviceWorkers.interception.enabled", true],
+      ["dom.serviceWorkers.enabled", true],
+      ["dom.serviceWorkers.testing.enabled", true],
+      ["security.mixed_content.block_active_content", false],
+    ]}, runTest);
+  };
+</script>
+</pre>
+</body>
+</html>
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/test_importScripts_mixedcontent.html
@@ -0,0 +1,50 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Bug 1198078 - test that we respect mixed content blocking in importScript() inside workers</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1198078">DOM Worker Threads Bug 1198078</a>
+<iframe></iframe>
+<p id="display"></p>
+<div id="content" style="display: none">
+  
+</div>
+<pre id="test">
+<script class="testbody" type="text/javascript">
+
+  onmessage = function(event) {
+    switch (event.data.status) {
+      case "done":
+        SimpleTest.finish();
+        break;
+      case "ok":
+        ok(event.data.data, event.data.msg);
+        break;
+      default:
+        ok(false, "Unexpected message:" + event.data);
+        SimpleTest.finish();
+    }
+  };
+
+  SimpleTest.waitForExplicitFinish();
+  onload = function() {
+    SpecialPowers.pushPrefEnv({"set": [
+      ["dom.workers.sharedWorkers.enabled", true],
+      ["security.mixed_content.block_active_content", false],
+    ]}, function() {
+      var iframe = document.querySelector("iframe");
+      iframe.src = "https://example.com/tests/dom/workers/test/importScripts_mixedcontent.html";
+    });
+  };
+
+</script>
+</pre>
+</body>
+</html>