From 2c716cd27352a72c855b602bc5a0f01a4fe131af Mon Sep 17 00:00:00 2001 From: Andrea Marchesini Date: Wed, 29 Mar 2017 15:28:46 +0200 Subject: [PATCH] Bug 1347817 - Principal must always have a valid origin - part 6 - fixing tests, r=ehsan --- .../browser/browser_firstPartyIsolation_aboutPages.js | 6 +++--- browser/components/preferences/permissions.js | 5 +++-- caps/tests/unit/test_origin.js | 9 +-------- dom/base/WebSocket.cpp | 9 ++++----- dom/base/nsGlobalWindow.cpp | 11 +++++------ .../test/unit/test_isOriginPotentiallyTrustworthy.js | 2 -- 6 files changed, 16 insertions(+), 26 deletions(-) diff --git a/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js b/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js index 0123160bf9fc..e4b6c01b026f 100644 --- a/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js +++ b/browser/components/originattributes/test/browser/browser_firstPartyIsolation_aboutPages.js @@ -33,7 +33,7 @@ add_task(function* test_remote_window_open_aboutBlank() { /** * 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}); @@ -43,8 +43,8 @@ add_task(function* test_nonremote_window_open_aboutBlank() { 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"); diff --git a/browser/components/preferences/permissions.js b/browser/components/preferences/permissions.js index 9443949c2fe2..34d340beff8e 100644 --- a/browser/components/preferences/permissions.js +++ b/browser/components/preferences/permissions.js @@ -95,8 +95,9 @@ var gPermissionManager = { 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, {}); diff --git a/caps/tests/unit/test_origin.js b/caps/tests/unit/test_origin.js index eddb6dfac027..e10c118698f0 100644 --- a/caps/tests/unit/test_origin.js +++ b/caps/tests/unit/test_origin.js @@ -300,12 +300,5 @@ function run_test() { 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)); } diff --git a/dom/base/WebSocket.cpp b/dom/base/WebSocket.cpp index 0165c2a9d1ca..2800fd55efa0 100644 --- a/dom/base/WebSocket.cpp +++ b/dom/base/WebSocket.cpp @@ -1634,13 +1634,12 @@ WebSocketImpl::Init(JSContext* aCx, nsCOMPtr 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) { diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 99a232c48416..30ab0c70a4b0 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -2551,12 +2551,11 @@ nsGlobalWindow::SetInitialPrincipalToSubject() #ifdef DEBUG // If we have a document loaded at this point, it had better be about:blank. - // Otherwise, something is really weird. - nsCOMPtr 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 } diff --git a/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js b/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js index 7de8faa8f9cb..b1f669cbd094 100644 --- a/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js +++ b/dom/security/test/unit/test_isOriginPotentiallyTrustworthy.js @@ -30,11 +30,9 @@ add_task(function* test_isOriginPotentiallyTrustworthy() { ["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],