Skip to content

Commit

Permalink
Bug 1142727 - Do not intercept sandboxed iframes with service workers…
Browse files Browse the repository at this point in the history
…; r=nsm

This is temporary until we clarify what we want to do with these
iframes in the spec.
  • Loading branch information
rmottola committed Jun 30, 2019
1 parent 899b62e commit fee4c2a
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 0 deletions.
5 changes: 5 additions & 0 deletions docshell/base/nsDocShell.cpp
Expand Up @@ -14120,6 +14120,11 @@ NS_IMETHODIMP
nsDocShell::ShouldPrepareForIntercept(nsIURI* aURI, bool aIsNavigate, bool* aShouldIntercept)
{
*aShouldIntercept = false;
if (mSandboxFlags) {
// If we're sandboxed, don't intercept.
return NS_OK;
}

nsCOMPtr<nsIServiceWorkerManager> swm = mozilla::services::GetServiceWorkerManager();
if (!swm) {
return NS_OK;
Expand Down
5 changes: 5 additions & 0 deletions dom/workers/test/serviceworkers/fetch/sandbox/index.html
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<script>
window.parent.postMessage({status: "ok", result: true, message: "The iframe is not being intercepted"}, "*");
window.parent.postMessage({status: "done"}, "*");
</script>
@@ -0,0 +1,5 @@
<!DOCTYPE html>
<script>
window.parent.postMessage({status: "ok", result: false, message: "The iframe is being intercepted"}, "*");
window.parent.postMessage({status: "done"}, "*");
</script>
26 changes: 26 additions & 0 deletions dom/workers/test/serviceworkers/fetch/sandbox/register.html
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<script>
function ok(v, msg) {
window.parent.postMessage({status: "ok", result: !!v, message: msg}, "*");
}

var isDone = false;
function done(reg) {
if (!isDone) {
ok(reg.waiting || reg.active, "Either active or waiting worker should be available.");
window.parent.postMessage({status: "registrationdone"}, "*");
isDone = true;
}
}

navigator.serviceWorker.register("sandbox_test.js", {scope: "."})
.then(function(registration) {
if (registration.installing) {
registration.installing.onstatechange = function(e) {
done(registration);
};
} else {
done(registration);
}
});
</script>
5 changes: 5 additions & 0 deletions dom/workers/test/serviceworkers/fetch/sandbox/sandbox_test.js
@@ -0,0 +1,5 @@
self.addEventListener("fetch", function(event) {
if (event.request.url.indexOf("index.html") >= 0) {
event.respondWith(fetch("intercepted_index.html"));
}
});
10 changes: 10 additions & 0 deletions dom/workers/test/serviceworkers/fetch/sandbox/unregister.html
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<script>
navigator.serviceWorker.getRegistration(".").then(function(registration) {
registration.unregister().then(function(success) {
if (success) {
window.parent.postMessage({status: "unregistrationdone"}, "*");
}
});
});
</script>
6 changes: 6 additions & 0 deletions dom/workers/test/serviceworkers/mochitest.ini
Expand Up @@ -33,6 +33,11 @@ support-files =
fetch/https/clonedresponse/register.html
fetch/https/clonedresponse/unregister.html
fetch/https/clonedresponse/https_test.js
fetch/sandbox/index.html
fetch/sandbox/intercepted_index.html
fetch/sandbox/register.html
fetch/sandbox/unregister.html
fetch/sandbox/sandbox_test.js
match_all_properties_worker.js
match_all_clients/match_all_controlled.html
test_serviceworker_interfaces.js
Expand Down Expand Up @@ -68,3 +73,4 @@ skip-if = true # Bug 1133805
[test_serviceworker_interfaces.html]
[test_serviceworker_not_sharedworker.html]
[test_match_all_client_id.html]
[test_sandbox_intercept.html]
51 changes: 51 additions & 0 deletions dom/workers/test/serviceworkers/test_sandbox_intercept.html
@@ -0,0 +1,51 @@
<!--
Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/
-->
<!DOCTYPE HTML>
<html>
<head>
<title>Bug 1142727 - Test that sandboxed iframes are not intercepted</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">
<iframe sandbox="allow-scripts allow-same-origin"></iframe>
</div>
<pre id="test"></pre>
<script class="testbody" type="text/javascript">

var iframe;
function runTest() {
iframe = document.querySelector("iframe");
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/sandbox/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 = "/tests/dom/workers/test/serviceworkers/fetch/sandbox/index.html";
} else if (e.data.status == "done") {
iframe.src = "/tests/dom/workers/test/serviceworkers/fetch/sandbox/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.enabled", true],
["dom.serviceWorkers.testing.enabled", true],
]}, runTest);
};
</script>
</pre>
</body>
</html>

0 comments on commit fee4c2a

Please sign in to comment.