Bug 1388413 - ServiceWorkerClients::OpenWindow() should use the triggeringPrincipal, r=bz
authorAndrea Marchesini <amarchesini@mozilla.com>
Thu, 28 Sep 2017 10:38:58 +0200
changeset 434544 7c4062d2329b7802fa69dbe2439adc1a22684293
parent 434543 c5e45bf14e9ab27b9cedac2b47d96bdb3725f3ca
child 434545 de11d13c7181b96234b3debf50fa9dddf88cad8e
push id8114
push userjlorenzo@mozilla.com
push dateThu, 02 Nov 2017 16:33:21 +0000
treeherdermozilla-beta@73e0d89a540f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1388413
milestone58.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 1388413 - ServiceWorkerClients::OpenWindow() should use the triggeringPrincipal, r=bz This is done using a JS Sandbox with the triggering Principal.
dom/workers/ServiceWorkerClients.cpp
--- a/dom/workers/ServiceWorkerClients.cpp
+++ b/dom/workers/ServiceWorkerClients.cpp
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "ServiceWorkerClients.h"
 
 #include "mozilla/dom/Promise.h"
 #include "mozilla/dom/PromiseWorkerProxy.h"
+#include "mozilla/JSObjectHolder.h"
 
 #include "ServiceWorkerClient.h"
 #include "ServiceWorkerManager.h"
 #include "ServiceWorkerPrivate.h"
 #include "ServiceWorkerWindowClient.h"
 
 #include "WorkerPrivate.h"
 #include "WorkerRunnable.h"
@@ -665,16 +666,19 @@ private:
   }
 
   nsresult
   OpenWindow(nsPIDOMWindowOuter** aWindow)
   {
     MOZ_DIAGNOSTIC_ASSERT(aWindow);
     WorkerPrivate* workerPrivate = mPromiseProxy->GetWorkerPrivate();
 
+    nsCOMPtr<nsIPrincipal> triggeringPrincipal = workerPrivate->GetPrincipal();
+    MOZ_DIAGNOSTIC_ASSERT(triggeringPrincipal);
+
     // [[1. Let url be the result of parsing url with entry settings object's API
     //   base URL.]]
     nsCOMPtr<nsIURI> uri;
     WorkerPrivate::LocationInfo& info = workerPrivate->GetLocationInfo();
 
     nsCOMPtr<nsIURI> baseURI;
     nsresult rv = NS_NewURI(getter_AddRefs(baseURI), info.mHref);
     if (NS_WARN_IF(NS_FAILED(rv))) {
@@ -689,16 +693,35 @@ private:
     // [[6.1 Open Window]]
     nsCOMPtr<nsIWindowMediator> wm = do_GetService(NS_WINDOWMEDIATOR_CONTRACTID,
                                                    &rv);
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;
     }
 
     if (XRE_IsContentProcess()) {
+
+      // Let's create a sandbox in order to have a valid JSContext and correctly
+      // propagate the SubjectPrincipal.
+      AutoJSAPI jsapi;
+      jsapi.Init();
+
+      JSContext* cx = jsapi.cx();
+
+      nsIXPConnect* xpc = nsContentUtils::XPConnect();
+      MOZ_ASSERT(xpc, "This should never be null!");
+
+      JS::Rooted<JSObject*> sandbox(cx);
+      rv = xpc->CreateSandbox(cx, triggeringPrincipal, sandbox.address());
+      if (NS_WARN_IF(NS_FAILED(rv))) {
+        return rv;
+      }
+
+      JSAutoCompartment ac(cx, sandbox);
+
       // ContentProcess
       nsCOMPtr<nsIWindowWatcher> wwatch =
         do_GetService(NS_WINDOWWATCHER_CONTRACTID, &rv);
       if (NS_WARN_IF(NS_FAILED(rv))) {
         return rv;
       }
       nsCOMPtr<nsPIWindowWatcher> pwwatch(do_QueryInterface(wwatch));
       NS_ENSURE_STATE(pwwatch);
@@ -749,19 +772,16 @@ private:
 
     nsCOMPtr<nsIBrowserDOMWindow> bwin;
     chromeWin->GetBrowserDOMWindow(getter_AddRefs(bwin));
 
     if (NS_WARN_IF(!bwin)) {
       return NS_ERROR_FAILURE;
     }
 
-    nsCOMPtr<nsIPrincipal> triggeringPrincipal = workerPrivate->GetPrincipal();
-    MOZ_DIAGNOSTIC_ASSERT(triggeringPrincipal);
-
     nsCOMPtr<mozIDOMWindowProxy> win;
     rv = bwin->OpenURI(uri, nullptr,
                        nsIBrowserDOMWindow::OPEN_DEFAULTWINDOW,
                        nsIBrowserDOMWindow::OPEN_NEW,
                        triggeringPrincipal,
                        getter_AddRefs(win));
     if (NS_WARN_IF(NS_FAILED(rv))) {
       return rv;