Bug 1824218 - Close captive portal page even if it navigated to support.mozilla.org r=mconley

In bug 1433500 we made the canonical URI be a meta refresh to a SUMO page in
order to show the user useful information if they happen to unlock the captive
portal just as the page is loading.

The portal watcher will close the tab if its location is
http://detectportal.firefox.com/canonical.html, but since the page will do a
meta refresh, its URL might be support.mozilla.org at that point, so we
make sure to also close it if it's already refreshed so it doesn't stick
around and urge the user to file a bug about it :)

Differential Revision: https://phabricator.services.mozilla.com/D200928
This commit is contained in:
Valentin Gosu
2024-02-09 20:12:07 +00:00
parent fe610e9f40
commit 7212b60dc1
2 changed files with 84 additions and 6 deletions

View File

@@ -142,7 +142,8 @@ var CaptivePortalWatcher = {
let canonicalURI = Services.io.newURI(this.canonicalURL);
if (
tab &&
tab.linkedBrowser.currentURI.equalsExceptRef(canonicalURI) &&
(tab.linkedBrowser.currentURI.equalsExceptRef(canonicalURI) ||
tab.linkedBrowser.currentURI.host == "support.mozilla.org") &&
(this._cps.state == this._cps.UNLOCKED_PORTAL ||
this._cps.state == this._cps.UNKNOWN)
) {
@@ -261,7 +262,8 @@ var CaptivePortalWatcher = {
if (
tab &&
tab.linkedBrowser &&
tab.linkedBrowser.currentURI.equalsExceptRef(canonicalURI)
(tab.linkedBrowser.currentURI.equalsExceptRef(canonicalURI) ||
tab.linkedBrowser.currentURI.host == "support.mozilla.org")
) {
this._previousCaptivePortalTab = null;
gBrowser.removeTab(tab);

View File

@@ -12,11 +12,26 @@ const CANONICAL_SUCCESS_URL = "http://localhost:8080/success";
const CPS = Cc["@mozilla.org/network/captive-portal-service;1"].getService(
Ci.nsICaptivePortalService
);
const META_CANONICAL_CONTENT = `<meta http-equiv=\"refresh\" content=\"0;url=http://support.mozilla.org:8080/sumo\"/>`;
const cps = Cc["@mozilla.org/network/captive-portal-service;1"].getService(
Ci.nsICaptivePortalService
);
let server;
let loginPageShown = false;
let showSumo = false;
const SUMO_URL = "https://support.mozilla.org:8080/sumo";
function redirectHandler(request, response) {
if (showSumo) {
response.setHeader("Content-Type", "text/html");
response.bodyOutputStream.write(
META_CANONICAL_CONTENT,
META_CANONICAL_CONTENT.length
);
return;
}
if (loginPageShown) {
return;
}
@@ -25,6 +40,22 @@ function redirectHandler(request, response) {
response.setHeader("Location", LOGIN_URL);
}
function sumoHandler(request, response) {
response.setHeader("Content-Type", "text/html");
let content = `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"/>
</head>
<body>
Testing
</body>
</html>
`;
response.bodyOutputStream.write(content, content.length);
}
function loginHandler(request, response) {
response.setHeader("Content-Type", "text/html");
response.bodyOutputStream.write(LOGIN_LINK, LOGIN_LINK.length);
@@ -40,16 +71,20 @@ function unlockHandler(request, response) {
add_setup(async function () {
// Set up a mock server for handling captive portal redirect.
server = new HttpServer();
server.identity.add("http", "support.mozilla.org", 8080);
server.registerPathHandler("/success", redirectHandler);
server.registerPathHandler("/login", loginHandler);
server.registerPathHandler("/unlock", unlockHandler);
server.registerPathHandler("/sumo", sumoHandler);
server.start(8080);
registerCleanupFunction(async () => await server.stop());
info("Mock server is now set up for captive portal redirect");
await SpecialPowers.pushPrefEnv({
set: [
["captivedetect.canonicalURL", CANONICAL_SUCCESS_URL],
["captivedetect.canonicalContent", CANONICAL_CONTENT],
["captivedetect.canonicalContent", META_CANONICAL_CONTENT],
["network.dns.native-is-localhost", true],
],
});
});
@@ -148,7 +183,48 @@ add_task(async function checkCaptivePortalTabCloseOnCanonicalURL_two() {
await errorPageReloaded;
info("Captive portal error page was reloaded");
gBrowser.removeTab(errorTab);
// Stop the server.
await new Promise(r => server.stop(r));
});
// Test that the captive portal page is closed after a successful login
// even if it's self-refreshed to support.mozilla.org
add_task(async function checkCaptivePortalTabCloseOnCanonicalURL_three() {
loginPageShown = false;
await portalDetected();
let errorTab = await openCaptivePortalErrorTab();
let tab = await openCaptivePortalLoginTab(errorTab, LOGIN_URL);
let browser = tab.linkedBrowser;
let errorPageReloaded = BrowserTestUtils.waitForErrorPage(
errorTab.linkedBrowser
);
let tabClosed = BrowserTestUtils.waitForTabClosing(tab);
showSumo = true;
await SpecialPowers.spawn(browser, [], async () => {
let doc = content.document;
let loginButton = doc.querySelector("a");
await ContentTaskUtils.waitForCondition(
() => loginButton,
"Login button on the captive portal tab is visible"
);
info("Clicking the login button on the captive portal tab page");
await EventUtils.synthesizeMouseAtCenter(loginButton, {}, content);
});
Services.obs.notifyObservers(null, "captive-portal-login-success");
await TestUtils.waitForCondition(
() => CPS.state == CPS.UNLOCKED_PORTAL,
"Captive portal is released"
);
await tabClosed;
info(
"Captive portal tab was closed on re-direct to canonical URL after login as expected"
);
await errorPageReloaded;
info("Captive portal error page was reloaded");
showSumo = false;
gBrowser.removeTab(errorTab);
});