tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_downgrade_mixed_content_auto_upgrade_console.js (2383B)


      1 // Bug 1673574 - Improve Console logging for mixed content auto upgrading
      2 "use strict";
      3 
      4 const testPath = getRootDirectory(gTestPath).replace(
      5  "chrome://mochitests/content",
      6  "http://httpsfirst.com"
      7 );
      8 
      9 let tests = [
     10  {
     11    description: "Top-Level upgrade should get logged",
     12    expectLogLevel: Ci.nsIConsoleMessage.warn,
     13    expectIncludes: ["Upgrading insecure request", "to use", "httpsfirst.com"],
     14  },
     15  {
     16    description: "Top-Level upgrade failure should get logged",
     17    expectLogLevel: Ci.nsIConsoleMessage.warn,
     18    expectIncludes: [
     19      "Upgrading insecure request",
     20      "failed",
     21      "httpsfirst.com",
     22      "Downgrading to",
     23    ],
     24  },
     25 ];
     26 
     27 const kTestURI = testPath + "file_mixed_content_auto_upgrade.html";
     28 
     29 add_task(async function () {
     30  // A longer timeout is necessary for this test than the plain mochitests
     31  // due to opening a new tab with the web console.
     32  requestLongerTimeout(4);
     33 
     34  // Enable ML2 and HTTPS-First Mode and register console-listener
     35  await SpecialPowers.pushPrefEnv({
     36    set: [
     37      ["security.mixed_content.upgrade_display_content", true],
     38      ["dom.security.https_first", true],
     39    ],
     40  });
     41  Services.console.registerListener(on_new_message);
     42  // 1. Upgrade page to https://
     43  let promiseLoaded = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
     44  BrowserTestUtils.startLoadingURIString(gBrowser.selectedBrowser, kTestURI);
     45  await promiseLoaded;
     46 
     47  await BrowserTestUtils.waitForCondition(() => tests.length === 0);
     48 
     49  // Clean up
     50  Services.console.unregisterListener(on_new_message);
     51 });
     52 
     53 function on_new_message(msgObj) {
     54  const message = msgObj.message;
     55  const logLevel = msgObj.logLevel;
     56 
     57  // The console message is:
     58  // Should only show HTTPS-First messages
     59 
     60  if (message.includes("Mixed Content:")) {
     61    ok(
     62      !message.includes("Upgrading insecure display request"),
     63      "msg included a mixed content upgrade"
     64    );
     65  }
     66  if (message.includes("HTTPS-First Mode:")) {
     67    for (let i = 0; i < tests.length; i++) {
     68      const testCase = tests[i];
     69      // Check if log-level matches
     70      if (logLevel !== testCase.expectLogLevel) {
     71        continue;
     72      }
     73      // Check if all substrings are included
     74      if (testCase.expectIncludes.some(str => !message.includes(str))) {
     75        continue;
     76      }
     77      ok(true, testCase.description);
     78      tests.splice(i, 1);
     79      break;
     80    }
     81  }
     82 }