Bug 1198078 - Part 2: Add tests for mixed content blocking of scripts in workers; r=ckerschb

This commit is contained in:
Ehsan Akhgari 2015-09-11 18:50:33 -04:00
parent d699f69c9a
commit 97c6396483
11 changed files with 276 additions and 0 deletions

View File

@ -0,0 +1 @@
response = "bad";

View File

@ -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();
}
};
};

View File

@ -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>

View File

@ -25,6 +25,9 @@ support-files =
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
@ -51,6 +54,7 @@ support-files =
promise_worker.js
recursion_worker.js
recursiveOnerror_worker.js
redirect_to_foreign.sjs
relativeLoad_import.js
relativeLoad_worker.js
relativeLoad_worker2.js
@ -153,6 +157,9 @@ skip-if = buildapp == 'b2g' # b2g(Failed to load script: errorwarning_worker.js)
[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]

View File

@ -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");
}

View File

@ -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'}}));
}
});

View File

@ -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>

View File

@ -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>

View File

@ -72,6 +72,9 @@ support-files =
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
@ -291,3 +294,5 @@ skip-if = e10s # Bug 1214305
skip-if = toolkit == "android" || toolkit == "gonk"
[test_imagecache.html]
[test_imagecache_max_age.html]
[test_importscript_mixedcontent.html]
tags = mcb

View File

@ -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>

View File

@ -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>