tor-browser

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

browser_console_logging.js (5261B)


      1 // Bug 1625448 - HTTPS Only Mode - Tests for console logging
      2 // https://bugzilla.mozilla.org/show_bug.cgi?id=1625448
      3 // This test makes sure that the various console messages from the HTTPS-Only
      4 // mode get logged to the console.
      5 "use strict";
      6 
      7 // Test Cases
      8 // description:    Description of what the subtests expects.
      9 // expectLogLevel: Expected log-level of a message.
     10 // expectIncludes: Expected substrings the message should contain.
     11 let tests = [
     12  {
     13    description: "Top-Level upgrade should get logged",
     14    expectLogLevel: Ci.nsIConsoleMessage.warn,
     15    expectIncludes: [
     16      "HTTPS-Only Mode: Upgrading insecure request",
     17      "to use",
     18      "file_console_logging.html",
     19    ],
     20  },
     21  {
     22    description: "iFrame upgrade failure should get logged",
     23    expectLogLevel: Ci.nsIConsoleMessage.error,
     24    expectIncludes: [
     25      "HTTPS-Only Mode: Upgrading insecure request",
     26      "failed",
     27      "file_console_logging.html",
     28    ],
     29  },
     30  {
     31    description: "WebSocket upgrade should get logged",
     32    expectLogLevel: Ci.nsIConsoleMessage.warn,
     33    expectIncludes: [
     34      "HTTPS-Only Mode: Upgrading insecure request",
     35      "to use",
     36      "ws://does.not.exist",
     37    ],
     38  },
     39  {
     40    description: "Sub-Resource upgrade for file_1 should get logged",
     41    expectLogLevel: Ci.nsIConsoleMessage.warn,
     42    expectIncludes: ["Upgrading insecure", "request", "file_1.jpg"],
     43  },
     44  {
     45    description: "Sub-Resource upgrade for file_2 should get logged",
     46    expectLogLevel: Ci.nsIConsoleMessage.warn,
     47    expectIncludes: ["Upgrading insecure", "request", "to use", "file_2.jpg"],
     48  },
     49  {
     50    description: "Exempt request for file_exempt should get logged",
     51    expectLogLevel: Ci.nsIConsoleMessage.info,
     52    expectIncludes: [
     53      "Not upgrading insecure request",
     54      "because it is exempt",
     55      "file_exempt.jpg",
     56    ],
     57  },
     58  {
     59    description: "Sub-Resource upgrade failure for file_2 should get logged",
     60    expectLogLevel: Ci.nsIConsoleMessage.error,
     61    expectIncludes: ["Upgrading insecure request", "failed", "file_2.jpg"],
     62  },
     63 ];
     64 
     65 const testPathUpgradeable = getRootDirectory(gTestPath).replace(
     66  "chrome://mochitests/content",
     67  "http://example.com"
     68 );
     69 // DNS errors are not logged as HTTPS-Only Mode upgrade failures, so we have to
     70 // upgrade to a domain that exists but fails.
     71 const testPathNotUpgradeable = getRootDirectory(gTestPath).replace(
     72  "chrome://mochitests/content",
     73  "http://self-signed.example.com"
     74 );
     75 const kTestURISuccess = testPathUpgradeable + "file_console_logging.html";
     76 const kTestURIFail = testPathNotUpgradeable + "file_console_logging.html";
     77 const kTestURIExempt = testPathUpgradeable + "file_exempt.jpg";
     78 
     79 const UPGRADE_DISPLAY_CONTENT =
     80  "security.mixed_content.upgrade_display_content";
     81 
     82 add_task(async function () {
     83  // A longer timeout is necessary for this test than the plain mochitests
     84  // due to opening a new tab with the web console.
     85  requestLongerTimeout(4);
     86 
     87  // Enable HTTPS-Only Mode and register console-listener
     88  await SpecialPowers.pushPrefEnv({
     89    set: [["dom.security.https_only_mode", true]],
     90  });
     91  Services.console.registerListener(on_new_message);
     92  // 1. Upgrade page to https://
     93  BrowserTestUtils.startLoadingURIString(
     94    gBrowser.selectedBrowser,
     95    kTestURISuccess
     96  );
     97  // 2. Make an exempt http:// request
     98  let xhr = new XMLHttpRequest();
     99  xhr.open("GET", kTestURIExempt, true);
    100  xhr.channel.loadInfo.httpsOnlyStatus |= Ci.nsILoadInfo.HTTPS_ONLY_EXEMPT;
    101  xhr.send();
    102  // 3. Make Websocket request
    103  new WebSocket("ws://does.not.exist");
    104 
    105  await BrowserTestUtils.waitForCondition(() => tests.length === 0);
    106 
    107  // Clean up
    108  Services.console.unregisterListener(on_new_message);
    109 });
    110 
    111 function on_new_message(msgObj) {
    112  const message = msgObj.message;
    113  const logLevel = msgObj.logLevel;
    114 
    115  // Bools about message and pref
    116  const isMCL2Enabled = Services.prefs.getBoolPref(UPGRADE_DISPLAY_CONTENT);
    117  const isHTTPSOnlyModeLog = message.includes("HTTPS-Only Mode:");
    118  const isMCLog = message.includes("Mixed Content:");
    119 
    120  // Check for messages about HTTPS-only upgrades (those should be unrelated to mixed content upgrades)
    121  // or for mixed content upgrades which should only occur if security.mixed_content.upgrade_display_content is enabled
    122  // (unrelated to https-only logs).
    123  if (
    124    (isHTTPSOnlyModeLog && !isMCLog) ||
    125    (isMCLog && isMCL2Enabled && !isHTTPSOnlyModeLog)
    126  ) {
    127    for (let i = 0; i < tests.length; i++) {
    128      const testCase = tests[i];
    129      // If security.mixed_content.upgrade_display_content is enabled, the mixed content control mechanism is upgrading file2.jpg
    130      // and HTTPS-Only mode is not failing upgrading file2.jpg, so it won't be logged.
    131      // so skip last test case
    132      if (
    133        testCase.description ==
    134          "Sub-Resource upgrade failure for file_2 should get logged" &&
    135        isMCL2Enabled
    136      ) {
    137        tests.splice(i, 1);
    138        continue;
    139      }
    140      // Check if log-level matches
    141      if (logLevel !== testCase.expectLogLevel) {
    142        continue;
    143      }
    144      // Check if all substrings are included
    145      if (testCase.expectIncludes.some(str => !message.includes(str))) {
    146        continue;
    147      }
    148      ok(true, testCase.description);
    149      tests.splice(i, 1);
    150      break;
    151    }
    152  }
    153 }