tor-browser

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

browser_console_webconsole_private_browsing.js (5521B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Bug 874061: test for how the browser and web consoles display messages coming
      5 // from private windows. See bug for description of expected behavior.
      6 
      7 "use strict";
      8 
      9 const NON_PRIVATE_MESSAGE = "This is not a private message";
     10 const PRIVATE_MESSAGE = "This is a private message";
     11 const PRIVATE_UNDEFINED_FN = "privateException";
     12 const PRIVATE_EXCEPTION = `${PRIVATE_UNDEFINED_FN} is not defined`;
     13 
     14 const NON_PRIVATE_TEST_URI =
     15  "data:text/html;charset=utf8,<!DOCTYPE html>Not private";
     16 const PRIVATE_TEST_URI = `data:text/html;charset=utf8,<!DOCTYPE html>Test console in private windows
     17  <script>
     18    function logMessages() {
     19      /* Wrap the exception so we don't throw in ContentTask. */
     20      setTimeout(() => {
     21        console.log("${PRIVATE_MESSAGE}");
     22        ${PRIVATE_UNDEFINED_FN}();
     23      }, 10);
     24    }
     25  </script>`;
     26 
     27 add_task(async function () {
     28  await pushPref("devtools.browsertoolbox.scope", "everything");
     29 
     30  const publicTab = await addTab(NON_PRIVATE_TEST_URI);
     31 
     32  const privateWindow = await BrowserTestUtils.openNewBrowserWindow({
     33    private: true,
     34  });
     35  ok(PrivateBrowsingUtils.isWindowPrivate(privateWindow), "window is private");
     36  const privateBrowser = privateWindow.gBrowser;
     37  privateBrowser.selectedTab = BrowserTestUtils.addTab(
     38    privateBrowser,
     39    PRIVATE_TEST_URI
     40  );
     41  const privateTab = privateBrowser.selectedTab;
     42 
     43  info("private tab opened");
     44  ok(
     45    PrivateBrowsingUtils.isBrowserPrivate(privateBrowser.selectedBrowser),
     46    "tab window is private"
     47  );
     48 
     49  let hud = await openConsole(privateTab);
     50  ok(hud, "web console opened");
     51 
     52  const onLogMessage = waitForMessageByType(
     53    hud,
     54    PRIVATE_MESSAGE,
     55    ".console-api"
     56  );
     57  const onErrorMessage = waitForMessageByType(hud, PRIVATE_EXCEPTION, ".error");
     58  logPrivateMessages(privateBrowser.selectedBrowser);
     59 
     60  await onLogMessage;
     61  await onErrorMessage;
     62  ok(true, "Messages are displayed as expected");
     63 
     64  info("Check that commands executed in private windows aren't put in history");
     65  const privateCommand = `"command in private window"`;
     66  await executeAndWaitForResultMessage(hud, privateCommand, "");
     67 
     68  const publicHud = await openConsole(publicTab);
     69  const historyMessage = await executeAndWaitForMessageByType(
     70    publicHud,
     71    ":history",
     72    "",
     73    ".simpleTable"
     74  );
     75 
     76  ok(
     77    Array.from(
     78      historyMessage.node.querySelectorAll("tr td:last-of-type")
     79    ).every(td => td.textContent !== privateCommand),
     80    "command from private window wasn't added to the history"
     81  );
     82  await closeConsole(publicTab);
     83 
     84  info("test cached messages");
     85  await closeConsole(privateTab);
     86  info("web console closed");
     87  hud = await openConsole(privateTab);
     88  ok(hud, "web console reopened");
     89 
     90  await waitFor(() => findConsoleAPIMessage(hud, PRIVATE_MESSAGE));
     91  await waitFor(() => findErrorMessage(hud, PRIVATE_EXCEPTION));
     92  ok(
     93    true,
     94    "Messages are still displayed after closing and reopening the console"
     95  );
     96 
     97  info("Test Browser Console");
     98  await closeConsole(privateTab);
     99  info("web console closed");
    100  hud = await BrowserConsoleManager.toggleBrowserConsole();
    101 
    102  // Add a non-private message to the console.
    103  const onBrowserConsoleNonPrivateMessage = waitForMessageByType(
    104    hud,
    105    NON_PRIVATE_MESSAGE,
    106    ".console-api"
    107  );
    108  SpecialPowers.spawn(
    109    gBrowser.selectedBrowser,
    110    [NON_PRIVATE_MESSAGE],
    111    function (msg) {
    112      content.console.log(msg);
    113    }
    114  );
    115  await onBrowserConsoleNonPrivateMessage;
    116 
    117  info(
    118    "Check that cached messages from private tabs are not displayed in the browser console"
    119  );
    120  // We do the check at this moment, after we received the "live" message, so the browser
    121  // console would have displayed any cached messages by now.
    122  assertNoPrivateMessages(hud);
    123 
    124  const onBrowserConsolePrivateLogMessage = waitForMessageByType(
    125    hud,
    126    PRIVATE_MESSAGE,
    127    ".console-api"
    128  );
    129  const onBrowserConsolePrivateErrorMessage = waitForMessageByType(
    130    hud,
    131    PRIVATE_EXCEPTION,
    132    ".error"
    133  );
    134  logPrivateMessages(privateBrowser.selectedBrowser);
    135 
    136  info("Wait for private log message");
    137  await onBrowserConsolePrivateLogMessage;
    138  info("Wait for private error message");
    139  await onBrowserConsolePrivateErrorMessage;
    140  ok(true, "Messages are displayed as expected");
    141 
    142  info("close the private window and check if private messages are removed");
    143  const onPrivateMessagesCleared = hud.ui.once("private-messages-cleared");
    144  privateWindow.BrowserCommands.tryToCloseWindow();
    145  await onPrivateMessagesCleared;
    146 
    147  ok(
    148    findConsoleAPIMessage(hud, NON_PRIVATE_MESSAGE),
    149    "non-private messages are still shown after private window closed"
    150  );
    151  assertNoPrivateMessages(hud);
    152 
    153  info("close the browser console");
    154  await safeCloseBrowserConsole();
    155 
    156  info("reopen the browser console");
    157  hud = await BrowserConsoleManager.toggleBrowserConsole();
    158  ok(hud, "browser console reopened");
    159 
    160  assertNoPrivateMessages(hud);
    161 
    162  info("close the browser console again");
    163  await safeCloseBrowserConsole();
    164 });
    165 
    166 function logPrivateMessages(browser) {
    167  SpecialPowers.spawn(browser, [], () => content.wrappedJSObject.logMessages());
    168 }
    169 
    170 function assertNoPrivateMessages(hud) {
    171  is(
    172    findConsoleAPIMessage(hud, PRIVATE_MESSAGE, ":not(.error)")?.textContent,
    173    undefined,
    174    "no console message displayed"
    175  );
    176  is(
    177    findErrorMessage(hud, PRIVATE_EXCEPTION)?.textContent,
    178    undefined,
    179    "no exception displayed"
    180  );
    181 }