Bug 1347817 - Principal must always have a valid origin - part 6 - fixing tests, r=ehsan
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 29 Mar 2017 15:28:46 +0200
changeset 553290 235e1d29916fc30848b796ef9b416ec3932a743b
parent 553289 89b3b96c32feee7789b4f71410eb7fa62d87d972
child 553291 8efb75a46b6204eb5c4f1f3e1f3b20226b0981ab
push id51582
push userasasaki@mozilla.com
push dateWed, 29 Mar 2017 18:30:11 +0000
reviewersehsan
bugs1347817
milestone55.0a1
Bug 1347817 - Principal must always have a valid origin - part 6 - fixing tests, r=ehsan
browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js
browser/components/preferences/permissions.js
caps/tests/unit/test_origin.js
dom/base/WebSocket.cpp
dom/base/nsGlobalWindow.cpp
dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js
--- a/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js
+++ b/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js
@@ -28,28 +28,28 @@ add_task(function* test_remote_window_op
                  "remote about:blank should have firstPartyDomain set");
   });
 
   win.close();
 });
 
 /**
  * For loading the initial about:blank in non-e10s mode, it will be loaded with
- * codebase principal. So we test if it has correct firstPartyDomain set.
+ * a null principal. So we test if it has correct firstPartyDomain set.
  */
 add_task(function* test_nonremote_window_open_aboutBlank() {
   let win = yield BrowserTestUtils.openNewBrowserWindow({remote: false});
   let browser = win.gBrowser.selectedBrowser;
 
   Assert.ok(!browser.isRemoteBrowser, "shouldn't be a remote browser");
 
   let attrs = { firstPartyDomain: "about.ef2a7dd5-93bc-417f-a698-142c3116864f.mozilla" };
   yield ContentTask.spawn(browser, attrs, function* (expectAttrs) {
-    Assert.ok(content.document.nodePrincipal.isCodebasePrincipal,
-              "The principal of non-remote about:blank should be a codebase principal.");
+    Assert.ok(!content.document.nodePrincipal.isCodebasePrincipal,
+              "The principal of non-remote about:blank should not be a codebase principal.");
     Assert.equal(content.document.nodePrincipal.originAttributes.firstPartyDomain,
                  expectAttrs.firstPartyDomain,
                  "non-remote about:blank should have firstPartyDomain set");
   });
 
   win.close();
 });
 
--- a/browser/components/preferences/permissions.js
+++ b/browser/components/preferences/permissions.js
@@ -90,18 +90,19 @@ var gPermissionManager = {
       // `localhost:8080` as having the scheme `localhost`, rather than being
       // an invalid URI. A canonical origin representation is required by the
       // permission manager for storage, so this won't prevent any valid
       // permissions from being entered by the user.
       let uri;
       try {
         uri = Services.io.newURI(input_url);
         principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
-        // If we have ended up with an unknown scheme, the following will throw.
-        principal.origin;
+        if (principal.origin.startsWith("moz-nullprincipal:")) {
+          throw "Null principal";
+        }
       } catch (ex) {
         uri = Services.io.newURI("http://" + input_url);
         principal = Services.scriptSecurityManager.createCodebasePrincipal(uri, {});
         // If we have ended up with an unknown scheme, the following will throw.
         principal.origin;
       }
     } catch (ex) {
       var message = this._bundle.getString("invalidURI");
--- a/caps/tests/unit/test_origin.js
+++ b/caps/tests/unit/test_origin.js
@@ -295,17 +295,10 @@ function run_test() {
     Services.prefs.setBoolPref("security.fileuri.strict_origin_policy", t[0]);
     var filePrin = ssm.createCodebasePrincipal(fileURI, {});
     do_check_eq(filePrin.origin, t[1]);
   });
   Services.prefs.clearUserPref("security.fileuri.strict_origin_policy");
 
   var aboutBlankURI = makeURI('about:blank');
   var aboutBlankPrin = ssm.createCodebasePrincipal(aboutBlankURI, {});
-  var thrown = false;
-  try {
-    aboutBlankPrin.origin;
-  } catch (e) {
-    thrown = true;
-  }
-  do_check_true(thrown);
-
+  do_check_true(/^moz-nullprincipal:\{([0-9]|[a-z]|\-){36}\}$/.test(aboutBlankPrin.origin));
 }
--- a/dom/base/WebSocket.cpp
+++ b/dom/base/WebSocket.cpp
@@ -1629,23 +1629,22 @@ WebSocketImpl::Init(JSContext* aCx,
       nsCOMPtr<nsIGlobalObject> globalObject(GetEntryGlobal());
       if (globalObject) {
         principal = globalObject->PrincipalOrNull();
       }
 
       nsCOMPtr<nsPIDOMWindowInner> innerWindow;
 
       while (true) {
-        bool isNullPrincipal = true;
         if (principal) {
+          bool isNullPrincipal = true;
           isNullPrincipal = principal->GetIsNullPrincipal();
-        }
-
-        if (!isNullPrincipal) {
-          break;
+          if (isNullPrincipal || nsContentUtils::IsSystemPrincipal(principal)) {
+            break;
+          }
         }
 
         if (!innerWindow) {
           innerWindow = do_QueryInterface(globalObject);
           if (NS_WARN_IF(!innerWindow)) {
             return NS_ERROR_DOM_SECURITY_ERR;
           }
         }
--- a/dom/base/nsGlobalWindow.cpp
+++ b/dom/base/nsGlobalWindow.cpp
@@ -2546,22 +2546,21 @@ nsGlobalWindow::SetInitialPrincipalToSub
     if (!mDoc->IsInitialDocument())
       return;
     // (b) already has the correct principal.
     if (mDoc->NodePrincipal() == newWindowPrincipal)
       return;
 
 #ifdef DEBUG
     // If we have a document loaded at this point, it had better be about:blank.
-    // Otherwise, something is really weird.
-    nsCOMPtr<nsIURI> uri;
-    mDoc->NodePrincipal()->GetURI(getter_AddRefs(uri));
-    NS_ASSERTION(uri && NS_IsAboutBlank(uri) &&
-                 NS_IsAboutBlank(mDoc->GetDocumentURI()),
-                 "Unexpected original document");
+    // Otherwise, something is really weird. An about:blank page has a
+    // NullPrincipal.
+    bool isNullPrincipal;
+    MOZ_ASSERT(NS_SUCCEEDED(mDoc->NodePrincipal()->GetIsNullPrincipal(&isNullPrincipal)) &&
+               isNullPrincipal);
 #endif
   }
 
   GetDocShell()->CreateAboutBlankContentViewer(newWindowPrincipal);
   mDoc->SetIsInitialDocument(true);
 
   nsCOMPtr<nsIPresShell> shell = GetDocShell()->GetPresShell();
 
--- a/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js
+++ b/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js
@@ -25,21 +25,19 @@ prefs.setCharPref("dom.securecontext.whi
 add_task(function* test_isOriginPotentiallyTrustworthy() {
   for (let [uriSpec, expectedResult] of [
     ["http://example.com/", false],
     ["https://example.com/", true],
     ["http://localhost/", true],
     ["http://127.0.0.1/", true],
     ["file:///", true],
     ["resource:///", true],
-    ["app://", true],
     ["moz-extension://", true],
     ["wss://example.com/", true],
     ["about:config", false],
-    ["urn:generic", false],
     ["http://example.net/", true],
     ["ws://example.org/", true],
     ["chrome://example.net/content/messenger.xul", false],
   ]) {
     let uri = NetUtil.newURI(uriSpec);
     let principal = gScriptSecurityManager.getCodebasePrincipal(uri);
     Assert.equal(gContentSecurityManager.isOriginPotentiallyTrustworthy(principal),
                  expectedResult);