Bug 1597228 - move 'get me out of here' logic to shared place and make toolkit-safe, r=dimi

Differential Revision: https://phabricator.services.mozilla.com/D238762
This commit is contained in:
Gijs Kruitbosch
2025-02-24 15:30:42 +00:00
parent d7fff069d3
commit 6b3212de9b
6 changed files with 52 additions and 117 deletions

View File

@@ -3,6 +3,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { EscapablePageParent } from "resource://gre/actors/NetErrorParent.sys.mjs";
let lazy = {}; let lazy = {};
ChromeUtils.defineESModuleGetters(lazy, { ChromeUtils.defineESModuleGetters(lazy, {
@@ -108,7 +110,7 @@ SafeBrowsingNotificationBox.prototype.QueryInterface = ChromeUtils.generateQI([
"nsISupportsWeakReference", "nsISupportsWeakReference",
]); ]);
export class BlockedSiteParent extends JSWindowActorParent { export class BlockedSiteParent extends EscapablePageParent {
receiveMessage(msg) { receiveMessage(msg) {
switch (msg.name) { switch (msg.name) {
case "Browser:SiteBlockedError": case "Browser:SiteBlockedError":
@@ -154,7 +156,7 @@ export class BlockedSiteParent extends JSWindowActorParent {
nsISecTel[bucketName + "GET_ME_OUT_OF_HERE"] nsISecTel[bucketName + "GET_ME_OUT_OF_HERE"]
); );
} }
browser.ownerGlobal.getMeOutOfHere(this.browsingContext); this.leaveErrorPage(browser, /* Never go back */ false);
break; break;
case "ignore_warning_link": case "ignore_warning_link":
if (Services.prefs.getBoolPref("browser.safebrowsing.allowOverride")) { if (Services.prefs.getBoolPref("browser.safebrowsing.allowOverride")) {
@@ -194,8 +196,9 @@ export class BlockedSiteParent extends JSWindowActorParent {
accessKey: lazy.browserBundle.GetStringFromName( accessKey: lazy.browserBundle.GetStringFromName(
"safebrowsing.getMeOutOfHereButton.accessKey" "safebrowsing.getMeOutOfHereButton.accessKey"
), ),
callback() { callback: () => {
browsingContext.topChromeWindow.getMeOutOfHere(browsingContext); let browser = browsingContext.top.embedderElement;
this.leaveErrorPage(browser, /* Never go back */ false);
}, },
}, },
]; ];

View File

@@ -1526,36 +1526,6 @@ function PageProxyClickHandler(aEvent) {
} }
} }
/**
* Re-direct the browser to a known-safe page. This function is
* used when, for example, the user browses to a known malware page
* and is presented with about:blocked. The "Get me out of here!"
* button should take the user to the default start page so that even
* when their own homepage is infected, we can get them somewhere safe.
*/
function getMeOutOfHere(browsingContext) {
browsingContext.top.fixupAndLoadURIString(getDefaultHomePage(), {
triggeringPrincipal: Services.scriptSecurityManager.getSystemPrincipal(), // Also needs to load homepage
});
}
/**
* Return the default start page for the cases when the user's own homepage is
* infected, so we can get them somewhere safe.
*/
function getDefaultHomePage() {
let url = BROWSER_NEW_TAB_URL;
if (PrivateBrowsingUtils.isWindowPrivate(window)) {
return url;
}
url = HomePage.getDefault();
// If url is a pipe-delimited set of pages, just take the first one.
if (url.includes("|")) {
url = url.split("|")[0];
}
return url;
}
var browserDragAndDrop = { var browserDragAndDrop = {
canDropLink: aEvent => Services.droppedLinkHandler.canDropLink(aEvent, true), canDropLink: aEvent => Services.droppedLinkHandler.canDropLink(aEvent, true),

View File

@@ -39,8 +39,6 @@
"UpdateUrlbarSearchSplitterState", "UpdateUrlbarSearchSplitterState",
"UpdatePopupNotificationsVisibility", "UpdatePopupNotificationsVisibility",
"PageProxyClickHandler", "PageProxyClickHandler",
"getMeOutOfHere",
"getDefaultHomePage",
"browserDragAndDrop", "browserDragAndDrop",
"homeButtonObserver", "homeButtonObserver",
"openHomeDialog", "openHomeDialog",

View File

@@ -133,6 +133,17 @@ export let HomePage = {
return homePages; return homePages;
}, },
getForErrorPage(win) {
if (lazy.PrivateBrowsingUtils.isWindowPrivate(win)) {
return win.BROWSER_NEW_TAB_URL;
}
let url = this.get(win);
if (url.includes("|")) {
url = url.split("|")[0];
}
return url;
},
/** /**
* @returns {string} * @returns {string}
* Returns the application default homepage. * Returns the application default homepage.

View File

@@ -2,10 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import { HomePage } from "resource:///modules/HomePage.sys.mjs"; import { EscapablePageParent } from "resource://gre/actors/NetErrorParent.sys.mjs";
import { PrivateBrowsingUtils } from "resource://gre/modules/PrivateBrowsingUtils.sys.mjs";
export class AboutHttpsOnlyErrorParent extends JSWindowActorParent { export class AboutHttpsOnlyErrorParent extends EscapablePageParent {
get browser() { get browser() {
return this.browsingContext.top.embedderElement; return this.browsingContext.top.embedderElement;
} }
@@ -13,36 +12,8 @@ export class AboutHttpsOnlyErrorParent extends JSWindowActorParent {
receiveMessage(aMessage) { receiveMessage(aMessage) {
switch (aMessage.name) { switch (aMessage.name) {
case "goBack": case "goBack":
this.goBackFromErrorPage(this.browser); this.leaveErrorPage(this.browser);
break; break;
} }
} }
goBackFromErrorPage(aBrowser) {
if (!aBrowser.canGoBack) {
// If the unsafe page is the first or the only one in history, go to the
// start page.
aBrowser.fixupAndLoadURIString(
this.getDefaultHomePage(aBrowser.ownerGlobal),
{
triggeringPrincipal:
Services.scriptSecurityManager.getSystemPrincipal(),
}
);
} else {
aBrowser.goBack();
}
}
getDefaultHomePage(win) {
if (PrivateBrowsingUtils.isWindowPrivate(win)) {
return win.BROWSER_NEW_TAB_URL || "about:blank";
}
let url = HomePage.getDefault();
// If url is a pipe-delimited set of pages, just take the first one.
if (url.includes("|")) {
url = url.split("|")[0];
}
return url;
}
} }

View File

@@ -5,8 +5,6 @@
import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs"; import { AppConstants } from "resource://gre/modules/AppConstants.sys.mjs";
import { PrivateBrowsingUtils } from "resource://gre/modules/PrivateBrowsingUtils.sys.mjs";
const PREF_SSL_IMPACT_ROOTS = [ const PREF_SSL_IMPACT_ROOTS = [
"security.tls.version.", "security.tls.version.",
"security.ssl3.", "security.ssl3.",
@@ -44,7 +42,36 @@ class CaptivePortalObserver {
} }
} }
export class NetErrorParent extends JSWindowActorParent { export class EscapablePageParent extends JSWindowActorParent {
/**
* Re-direct the browser to the previous page or a known-safe page if no
* previous page is found in history. This function is used when the user
* browses to a secure page with certificate issues and is presented with
* about:certerror. The "Go Back" button should take the user to the previous
* or a default start page so that even when their own homepage is on a server
* that has certificate errors, we can get them somewhere safe.
*/
leaveErrorPage(browser, allowGoingBack = true) {
if (!browser.canGoBack || !allowGoingBack) {
// If the unsafe page is the first or the only one in history, we need to
// go somewhere:
let safePage = "about:blank";
// Ideally we use the homepage...
if (AppConstants.MOZ_BUILD_APP == "browser") {
safePage = lazy.HomePage.getForErrorPage(browser.ownerGlobal);
}
browser.fixupAndLoadURIString(safePage, {
triggeringPrincipal:
Services.scriptSecurityManager.getSystemPrincipal(),
});
} else {
browser.goBack();
}
}
}
export class NetErrorParent extends EscapablePageParent {
constructor() { constructor() {
super(); super();
this.captivePortalObserver = new CaptivePortalObserver(this); this.captivePortalObserver = new CaptivePortalObserver(this);
@@ -73,51 +100,6 @@ export class NetErrorParent extends JSWindowActorParent {
return false; return false;
} }
/**
* Return the default start page for the cases when the user's own homepage is
* infected, so we can get them somewhere safe.
*/
getDefaultHomePage(win) {
let url;
if (
!PrivateBrowsingUtils.isWindowPrivate(win) &&
AppConstants.MOZ_BUILD_APP == "browser"
) {
url = lazy.HomePage.getDefault();
}
url ||= win.BROWSER_NEW_TAB_URL || "about:blank";
// If url is a pipe-delimited set of pages, just take the first one.
if (url.includes("|")) {
url = url.split("|")[0];
}
return url;
}
/**
* Re-direct the browser to the previous page or a known-safe page if no
* previous page is found in history. This function is used when the user
* browses to a secure page with certificate issues and is presented with
* about:certerror. The "Go Back" button should take the user to the previous
* or a default start page so that even when their own homepage is on a server
* that has certificate errors, we can get them somewhere safe.
*/
goBackFromErrorPage(browser) {
if (!browser.canGoBack) {
// If the unsafe page is the first or the only one in history, go to the
// start page.
browser.fixupAndLoadURIString(
this.getDefaultHomePage(browser.ownerGlobal),
{
triggeringPrincipal:
Services.scriptSecurityManager.getSystemPrincipal(),
}
);
} else {
browser.goBack();
}
}
/** /**
* This function does a canary request to a reliable, maintained endpoint, in * This function does a canary request to a reliable, maintained endpoint, in
* order to help network code detect a system-wide man-in-the-middle. * order to help network code detect a system-wide man-in-the-middle.
@@ -241,7 +223,7 @@ export class NetErrorParent extends JSWindowActorParent {
this.browser.reload(); this.browser.reload();
break; break;
case "Browser:SSLErrorGoBack": case "Browser:SSLErrorGoBack":
this.goBackFromErrorPage(this.browser); this.leaveErrorPage(this.browser);
break; break;
case "GetChangedCertPrefs": case "GetChangedCertPrefs":
let hasChangedCertPrefs = this.hasChangedCertPrefs(); let hasChangedCertPrefs = this.hasChangedCertPrefs();