Bug 1536411 - StoragePrincipal - part 7 - test for DOMCache and ServiceWorkers, r=Ehsan,asuth
authorAndrea Marchesini <amarchesini@mozilla.com>
Fri, 12 Apr 2019 05:30:36 +0000
changeset 469240 9fd7d42e7f557667dc8c972b68e49b9881a4cef5
parent 469239 c1074e3a0d5fbc17d6763d02593203ab0cccb4e7
child 469241 941f963a8fc82924465d14695f0f7c7aebf399d3
push id112776
push usershindli@mozilla.com
push dateFri, 12 Apr 2019 16:20:17 +0000
treeherdermozilla-inbound@b4501ced5619 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersEhsan, asuth
bugs1536411
milestone68.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 1536411 - StoragePrincipal - part 7 - test for DOMCache and ServiceWorkers, r=Ehsan,asuth Differential Revision: https://phabricator.services.mozilla.com/D25778
dom/serviceworkers/ServiceWorkerPrivate.cpp
dom/workers/ScriptLoader.cpp
toolkit/components/antitracking/test/browser/browser.ini
toolkit/components/antitracking/test/browser/browser_partitionedDOMCache.js
toolkit/components/antitracking/test/browser/browser_partitionedServiceWorkers.js
toolkit/components/antitracking/test/browser/matchAll.js
--- a/dom/serviceworkers/ServiceWorkerPrivate.cpp
+++ b/dom/serviceworkers/ServiceWorkerPrivate.cpp
@@ -1724,16 +1724,20 @@ nsresult ServiceWorkerPrivate::SpawnWork
   // bug 965637 is ever fixed this can be removed.
   info.mPrincipal =
       BasePrincipal::CreateCodebasePrincipal(uri, mInfo->GetOriginAttributes());
   if (NS_WARN_IF(!info.mPrincipal)) {
     return NS_ERROR_FAILURE;
   }
   info.mLoadingPrincipal = info.mPrincipal;
 
+  // StoragePrincipal for ServiceWorkers is equal to mPrincipal because, at the
+  // moment, ServiceWorkers are not exposed in partitioned contexts.
+  info.mStoragePrincipal = info.mPrincipal;
+
   info.mStorageAccess =
       nsContentUtils::StorageAllowedForServiceWorker(info.mPrincipal);
 
   info.mCookieSettings = mozilla::net::CookieSettings::Create();
   MOZ_ASSERT(info.mCookieSettings);
 
   info.mOriginAttributes = mInfo->GetOriginAttributes();
 
--- a/dom/workers/ScriptLoader.cpp
+++ b/dom/workers/ScriptLoader.cpp
@@ -1291,17 +1291,19 @@ class ScriptLoaderRunnable final : publi
       nsILoadGroup* loadGroup = mWorkerPrivate->GetLoadGroup();
       MOZ_DIAGNOSTIC_ASSERT(loadGroup);
 
       // Override the principal on the WorkerPrivate.  This is only necessary
       // in order to get a principal with exactly the correct URL.  The fetch
       // referrer logic depends on the WorkerPrivate principal having a URL
       // that matches the worker script URL.  If bug 1340694 is ever fixed
       // this can be removed.
-      // TODO: storagePrincipal here?
+      // XXX: force the storagePrincipal to be equal to the response one. This
+      // is OK for now because we don't want to expose storagePrincipal
+      // functionality in ServiceWorkers yet.
       rv = mWorkerPrivate->SetPrincipalsOnMainThread(
           responsePrincipal, responsePrincipal, loadGroup);
       MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
 
       rv = mWorkerPrivate->SetCSPFromHeaderValues(aCSPHeaderValue,
                                                   aCSPReportOnlyHeaderValue);
       MOZ_DIAGNOSTIC_ASSERT(NS_SUCCEEDED(rv));
 
--- a/toolkit/components/antitracking/test/browser/browser.ini
+++ b/toolkit/components/antitracking/test/browser/browser.ini
@@ -90,8 +90,11 @@ support-files = localStorage.html
 support-files = localStorageEvents.html
 [browser_workerPropagation.js]
 support-files = workerIframe.html
 [browser_cookieBetweenTabs.js]
 [browser_partitionedMessaging.js]
 [browser_partitionedIndexedDB.js]
 [browser_partitionedCookies.js]
 support-files = cookies.sjs
+[browser_partitionedDOMCache.js]
+[browser_partitionedServiceWorkers.js]
+support-files = matchAll.js
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/test/browser/browser_partitionedDOMCache.js
@@ -0,0 +1,21 @@
+/* import-globals-from storageprincipal_head.js */
+
+StoragePrincipalHelper.runTest("DOMCache",
+  async (win3rdParty, win1stParty, allowed) => {
+    // DOM Cache is not supported. Always blocked.
+    await win3rdParty.caches.open("wow").then(
+      _ => { ok(allowed, "DOM Cache cannot be used!"); },
+      _ => { ok(!allowed, "DOM Cache cannot be used!"); }
+    );
+
+    await win1stParty.caches.open("wow").then(
+      _ => { ok(true, "DOM Cache shoulw be available"); },
+      _ => { ok(false, "DOM Cache shoulw be available"); },
+    );
+  },
+
+  async _ => {
+    await new Promise(resolve => {
+      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
+    });
+  });
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/test/browser/browser_partitionedServiceWorkers.js
@@ -0,0 +1,65 @@
+/* import-globals-from storageprincipal_head.js */
+
+StoragePrincipalHelper.runTest("ServiceWorkers",
+  async (win3rdParty, win1stParty, allowed) => {
+    // ServiceWorkers are not supported. Always blocked.
+    await win3rdParty.navigator.serviceWorker.register("empty.js").then(
+      _ => { ok(allowed, "Success: ServiceWorker cannot be used!"); },
+      _ => { ok(!allowed, "Failed: ServiceWorker cannot be used!"); });
+
+    await win1stParty.navigator.serviceWorker.register("empty.js").then(
+      _ => { ok(true, "Success: ServiceWorker should be available!"); },
+      _ => { ok(false, "Failed: ServiceWorker should be available!"); });
+  },
+
+  async _ => {
+    await new Promise(resolve => {
+      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
+    });
+  },
+
+  [["dom.serviceWorkers.exemptFromPerDomainMax", true],
+   ["dom.ipc.processCount", 1],
+   ["dom.serviceWorkers.enabled", true],
+   ["dom.serviceWorkers.testing.enabled", true]]);
+
+StoragePrincipalHelper.runTest("ServiceWorkers - MatchAll",
+  async (win3rdParty, win1stParty, allowed) => {
+    if (!win1stParty.sw) {
+      let reg = await win1stParty.navigator.serviceWorker.register("matchAll.js");
+      if (reg.installing.state !== "activated") {
+        await new Promise(resolve => {
+            let w = reg.installing;
+            w.addEventListener("statechange", function onStateChange() {
+              if (w.state === "activated") {
+                w.removeEventListener("statechange", onStateChange);
+                win1stParty.sw = reg.active;
+                resolve();
+              }
+            });
+          });
+      }
+    }
+
+    let msgPromise = new Promise(resolve => {
+      win1stParty.navigator.serviceWorker.addEventListener("message", msg => {
+        resolve(msg.data);
+      });
+    });
+
+    win1stParty.sw.postMessage(win3rdParty.location.href);
+    let msg = await msgPromise;
+
+    is(allowed, msg, "We want to have the 3rd party window controlled.");
+  },
+
+  async _ => {
+    await new Promise(resolve => {
+      Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
+    });
+  },
+
+  [["dom.serviceWorkers.exemptFromPerDomainMax", true],
+   ["dom.ipc.processCount", 1],
+   ["dom.serviceWorkers.enabled", true],
+   ["dom.serviceWorkers.testing.enabled", true]]);
new file mode 100644
--- /dev/null
+++ b/toolkit/components/antitracking/test/browser/matchAll.js
@@ -0,0 +1,13 @@
+self.addEventListener("message", async e => {
+  let clients = await self.clients.matchAll({type: "window", includeUncontrolled: true});
+
+  let hasWindow = false;
+  for (let client of clients) {
+    if (e.data == client.url) {
+      hasWindow = true;
+      break;
+    }
+  }
+
+  e.source.postMessage(hasWindow);
+});