Skip to content

Commit

Permalink
Bug 1874801 - Check that HTTPS-Only is disabled when checking if HTTP…
Browse files Browse the repository at this point in the history
…S-First is enabled in nsHTTPSOnlyUtils r=freddyb

Differential Revision: https://phabricator.services.mozilla.com/D198780
  • Loading branch information
maltejur committed Jan 29, 2024
1 parent e935285 commit a5249be
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
8 changes: 6 additions & 2 deletions dom/security/nsHTTPSOnlyUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ bool nsHTTPSOnlyUtils::IsHttpsOnlyModeEnabled(bool aFromPrivateWindow) {

/* static */
bool nsHTTPSOnlyUtils::IsHttpsFirstModeEnabled(bool aFromPrivateWindow) {
// HTTPS-Only takes priority over HTTPS-First
if (IsHttpsOnlyModeEnabled(aFromPrivateWindow)) {
return false;
}

// if the general pref is set to true, then we always return
if (mozilla::StaticPrefs::dom_security_https_first()) {
return true;
Expand Down Expand Up @@ -122,8 +127,7 @@ void nsHTTPSOnlyUtils::PotentiallyFireHttpRequestToShortenTimout(
// early if attempting to send a background request to a non standard port.
if ((IsHttpsFirstModeEnabled(isPrivateWin) ||
(loadInfo->GetWasSchemelessInput() &&
mozilla::StaticPrefs::dom_security_https_first_schemeless())) &&
!IsHttpsOnlyModeEnabled(isPrivateWin)) {
mozilla::StaticPrefs::dom_security_https_first_schemeless()))) {
int32_t port = 0;
nsresult rv = channelURI->GetPort(&port);
int defaultPortforScheme = NS_GetDefaultPort("http");
Expand Down
6 changes: 6 additions & 0 deletions dom/security/test/https-only/browser.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ prefs = ["dom.security.https_first=false"]
["browser_background_redirect.js"]
support-files = ["file_background_redirect.sjs"]

["browser_bug1874801.js"]
support-files = [
"file_bug1874801.sjs",
"file_bug1874801.html",
]

["browser_console_logging.js"]
support-files = ["file_console_logging.html"]

Expand Down
56 changes: 56 additions & 0 deletions dom/security/test/https-only/browser_bug1874801.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Specifically test https://bugzilla.mozilla.org/show_bug.cgi?id=1874801

const TAB_URL =
"https://example.com/browser/dom/security/test/https-only/file_bug1874801.html";

function assertImageLoaded(tab) {
return ContentTask.spawn(tab.linkedBrowser, {}, () => {
const img = content.document.getElementsByTagName("img")[0];

ok(!!img, "Image tag should exist");
ok(img.complete && img.naturalWidth > 0, "Image should have loaded ");
});
}

add_task(async function test_bug1874801() {
await SpecialPowers.pushPrefEnv({
set: [
["security.mixed_content.upgrade_display_content", false],
["dom.security.https_first", true],
["dom.security.https_only_mode", true],
],
});

// Open Tab
const tabToClose = await BrowserTestUtils.openNewForegroundTab(
gBrowser,
TAB_URL,
true
);

// Make sure the image was loaded via HTTPS
await assertImageLoaded(tabToClose);

// Close Tab
const tabClosePromise =
BrowserTestUtils.waitForSessionStoreUpdate(tabToClose);
BrowserTestUtils.removeTab(tabToClose);
await tabClosePromise;

// Restore Tab
const restoredTabPromise = BrowserTestUtils.waitForNewTab(
gBrowser,
TAB_URL,
true
);
undoCloseTab();
const restoredTab = await restoredTabPromise;

// Make sure the image was loaded via HTTPS
await assertImageLoaded(restoredTab);
});
11 changes: 11 additions & 0 deletions dom/security/test/https-only/file_bug1874801.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Bug 1874801</title>
</head>
<body>
<img src="http://example.com/browser/dom/security/test/https-only/file_bug1874801.sjs">
</body>
</html>
17 changes: 17 additions & 0 deletions dom/security/test/https-only/file_bug1874801.sjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */

function handleRequest(request, response) {
response.setHeader("Cache-Control", "no-cache", false);
if (request.scheme === "https") {
response.setStatusLine(request.httpVersion, 200, "OK");
response.setHeader("Content-Type", "image/svg+xml");
response.write(
`<svg version="1.1" width="100" height="40" xmlns="http://www.w3.org/2000/svg"><text x="20" y="20">HTTPS</text></svg>`
);
return;
}
if (request.scheme === "http") {
response.setStatusLine(request.httpVersion, 400, "Bad Request");
}
}

0 comments on commit a5249be

Please sign in to comment.