Bug 1366203 - revert about blank creation for URIs that don't inherit principals, r=mconley
authorGijs Kruitbosch <gijskruitbosch@gmail.com>
Mon, 29 May 2017 15:57:19 +0100
changeset 409420 8d98a318d6ecf6ecdbcfdf6c30f9cd8254fdea32
parent 409419 4c881f05785de7b390f476fcc01a0be9317a989d
child 409421 1ca009c2627899793a95ed52c3a2d14987bce672
push id7391
push usermtabara@mozilla.com
push dateMon, 12 Jun 2017 13:08:53 +0000
treeherdermozilla-beta@2191d7f87e2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmconley
bugs1366203
milestone55.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 1366203 - revert about blank creation for URIs that don't inherit principals, r=mconley MozReview-Commit-ID: 4F0Tq60iVUk
browser/base/content/tabbrowser.xml
browser/base/content/test/urlbar/browser_urlbar_stop_pending.js
browser/base/content/utilityOverlay.js
--- a/browser/base/content/tabbrowser.xml
+++ b/browser/base/content/tabbrowser.xml
@@ -2352,20 +2352,24 @@
 
             // if we're adding tabs, we're past interrupt mode, ditch the owner
             if (this.mCurrentTab.owner)
               this.mCurrentTab.owner = null;
 
             var t = document.createElementNS(NS_XUL, "tab");
 
             aURI = aURI || "about:blank";
+            let aURIObject = null;
+            try {
+              aURIObject = Services.io.newURI(aURI);
+            } catch (ex) { /* we'll try to fix up this URL later */ }
 
             let lazyBrowserURI;
             if (aCreateLazyBrowser && aURI != "about:blank") {
-              lazyBrowserURI = Services.io.newURI(aURI);
+              lazyBrowserURI = aURIObject;
               aURI = "about:blank";
             }
 
             var uriIsAboutBlank = aURI == "about:blank";
 
             if (!aNoInitialLabel) {
               if (isBlankPageURL(aURI)) {
                 t.setAttribute("label", this.mStringBundle.getString("tabs.emptyTabTitle"));
@@ -2501,18 +2505,24 @@
 
             // Dispatch a new tab notification.  We do this once we're
             // entirely done, so that things are in a consistent state
             // even if the event listener opens or closes tabs.
             var detail = aEventDetail || {};
             var evt = new CustomEvent("TabOpen", { bubbles: true, detail });
             t.dispatchEvent(evt);
 
-            if (!usingPreloadedContent && aOriginPrincipal) {
-              b.createAboutBlankContentViewer(aOriginPrincipal);
+            if (!usingPreloadedContent && aOriginPrincipal && aURI) {
+              let {URI_INHERITS_SECURITY_CONTEXT} = Ci.nsIProtocolHandler;
+              // Unless we know for sure we're not inheriting principals,
+              // force the about:blank viewer to have the right principal:
+              if (!aURIObject ||
+                  (Services.io.getProtocolFlags(aURIObject.scheme) & URI_INHERITS_SECURITY_CONTEXT)) {
+                b.createAboutBlankContentViewer(aOriginPrincipal);
+              }
             }
 
             // If we didn't swap docShells with a preloaded browser
             // then let's just continue loading the page normally.
             if (!usingPreloadedContent && (!uriIsAboutBlank || aDisallowInheritPrincipal)) {
               // pretend the user typed this so it'll be available till
               // the document successfully loads
               if (aURI && gInitialPages.indexOf(aURI) == -1)
--- a/browser/base/content/test/urlbar/browser_urlbar_stop_pending.js
+++ b/browser/base/content/test/urlbar/browser_urlbar_stop_pending.js
@@ -35,8 +35,104 @@ add_task(async function() {
   is(gURLBar.value, expectedURLBarChange, "Should not have changed URL bar value synchronously.");
   await pageLoadPromise;
   ok(sawChange, "The URL bar change handler should have been called by the time the page was loaded");
   obs.disconnect();
   obs = null;
   await BrowserTestUtils.removeTab(tab);
 });
 
+/**
+ * Check that if we:
+ * 1) middle-click a link to a separate page whose server doesn't respond
+ * 2) we switch to that tab and stop the request
+ *
+ * The URL bar continues to contain the URL of the page we wanted to visit.
+ */
+add_task(async function() {
+  let socket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
+  socket.init(-1, true, -1);
+  const PORT = socket.port;
+  registerCleanupFunction(() => { socket.close(); });
+
+  const TEST_PATH = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "https://example.com");
+  const BASE_PAGE = TEST_PATH + "dummy_page.html";
+  const SLOW_HOST = `https://localhost:${PORT}/`;
+  info("Using URLs: " + SLOW_HOST);
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_PAGE);
+  info("opened tab");
+  await ContentTask.spawn(tab.linkedBrowser, SLOW_HOST, URL => {
+    let link = content.document.createElement("a");
+    link.href = URL;
+    link.textContent = "click me to open a slow page";
+    link.id = "clickme"
+    content.document.body.appendChild(link);
+  });
+  info("added link");
+  let newTabPromise = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
+  // Middle click the link:
+  await BrowserTestUtils.synthesizeMouseAtCenter("#clickme", { button: 1 }, tab.linkedBrowser);
+  // get new tab, switch to it
+  let newTab = (await newTabPromise).target;
+  await BrowserTestUtils.switchTab(gBrowser, newTab);
+  is(gURLBar.value, SLOW_HOST, "Should have slow page in URL bar");
+  let browserStoppedPromise = BrowserTestUtils.browserStopped(newTab.linkedBrowser);
+  BrowserStop();
+  await browserStoppedPromise;
+
+  is(gURLBar.value, SLOW_HOST, "Should still have slow page in URL bar after stop");
+  await BrowserTestUtils.removeTab(newTab);
+  await BrowserTestUtils.removeTab(tab);
+});
+/**
+ * Check that if we:
+ * 1) middle-click a link to a separate page whose server doesn't respond
+ * 2) we alter the URL on that page to some other server that doesn't respond
+ * 3) we stop the request
+ *
+ * The URL bar continues to contain the second URL.
+ */
+add_task(async function() {
+  let socket = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
+  socket.init(-1, true, -1);
+  const PORT1 = socket.port;
+  let socket2 = Cc["@mozilla.org/network/server-socket;1"].createInstance(Ci.nsIServerSocket);
+  socket2.init(-1, true, -1);
+  const PORT2 = socket2.port;
+  registerCleanupFunction(() => { socket.close(); socket2.close(); });
+
+  const TEST_PATH = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "https://example.com");
+  const BASE_PAGE = TEST_PATH + "dummy_page.html";
+  const SLOW_HOST1 = `https://localhost:${PORT1}/`;
+  const SLOW_HOST2 = `https://localhost:${PORT2}/`;
+  info("Using URLs: " + SLOW_HOST1 + " and " + SLOW_HOST2);
+  let tab = await BrowserTestUtils.openNewForegroundTab(gBrowser, BASE_PAGE);
+  info("opened tab");
+  await ContentTask.spawn(tab.linkedBrowser, SLOW_HOST1, URL => {
+    let link = content.document.createElement("a");
+    link.href = URL;
+    link.textContent = "click me to open a slow page";
+    link.id = "clickme"
+    content.document.body.appendChild(link);
+  });
+  info("added link");
+  let newTabPromise = BrowserTestUtils.waitForEvent(gBrowser.tabContainer, "TabOpen");
+  // Middle click the link:
+  await BrowserTestUtils.synthesizeMouseAtCenter("#clickme", { button: 1 }, tab.linkedBrowser);
+  // get new tab, switch to it
+  let newTab = (await newTabPromise).target;
+  await BrowserTestUtils.switchTab(gBrowser, newTab);
+  is(gURLBar.value, SLOW_HOST1, "Should have slow page in URL bar");
+  let browserStoppedPromise = BrowserTestUtils.browserStopped(newTab.linkedBrowser);
+  gURLBar.value = SLOW_HOST2;
+  gURLBar.handleCommand();
+  await browserStoppedPromise;
+
+  is(gURLBar.value, SLOW_HOST2, "Should have second slow page in URL bar");
+  browserStoppedPromise = BrowserTestUtils.browserStopped(newTab.linkedBrowser);
+  BrowserStop();
+  await browserStoppedPromise;
+
+  is(gURLBar.value, SLOW_HOST2, "Should still have second slow page in URL bar after stop");
+  await BrowserTestUtils.removeTab(newTab);
+  await BrowserTestUtils.removeTab(tab);
+});
+
--- a/browser/base/content/utilityOverlay.js
+++ b/browser/base/content/utilityOverlay.js
@@ -417,17 +417,22 @@ function openLinkIn(url, where, params) 
 
     if (aAllowPopups) {
       flags |= Ci.nsIWebNavigation.LOAD_FLAGS_ALLOW_POPUPS;
     }
     if (aIndicateErrorPageLoad) {
       flags |= Ci.nsIWebNavigation.LOAD_FLAGS_ERROR_LOAD_CHANGES_RV;
     }
 
-    if (aForceAboutBlankViewerInCurrent) {
+    let {URI_INHERITS_SECURITY_CONTEXT} = Ci.nsIProtocolHandler;
+    if (aForceAboutBlankViewerInCurrent &&
+        (!uriObj ||
+         (Services.io.getProtocolFlags(uriObj.scheme) & URI_INHERITS_SECURITY_CONTEXT))) {
+      // Unless we know for sure we're not inheriting principals,
+      // force the about:blank viewer to have the right principal:
       targetBrowser.createAboutBlankContentViewer(aPrincipal);
     }
 
     targetBrowser.loadURIWithFlags(url, {
       triggeringPrincipal: aTriggeringPrincipal,
       flags,
       referrerURI: aNoReferrer ? null : aReferrerURI,
       referrerPolicy: aReferrerPolicy,