tor-browser

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

browser_security-2.js (5750B)


      1 const PREFS = [
      2  "browser.safebrowsing.phishing.enabled",
      3  "browser.safebrowsing.malware.enabled",
      4 
      5  "browser.safebrowsing.downloads.enabled",
      6 
      7  "browser.safebrowsing.downloads.remote.block_potentially_unwanted",
      8  "browser.safebrowsing.downloads.remote.block_uncommon",
      9 ];
     10 
     11 let originals = PREFS.map(pref => [pref, Services.prefs.getBoolPref(pref)]);
     12 let originalMalwareTable = Services.prefs.getCharPref(
     13  "urlclassifier.malwareTable"
     14 );
     15 registerCleanupFunction(function () {
     16  originals.forEach(([pref, val]) => Services.prefs.setBoolPref(pref, val));
     17  Services.prefs.setCharPref(
     18    "urlclassifier.malwareTable",
     19    originalMalwareTable
     20  );
     21 });
     22 
     23 // This test only opens the Preferences once, and then reloads the page
     24 // each time that it wants to test various preference combinations. We
     25 // only use one tab (instead of opening/closing for each test) for all
     26 // to help improve test times on debug builds.
     27 add_setup(async function () {
     28  await openPreferencesViaOpenPreferencesAPI("privacy", { leaveOpen: true });
     29  registerCleanupFunction(async function () {
     30    BrowserTestUtils.removeTab(gBrowser.selectedTab);
     31  });
     32 });
     33 
     34 // test the download protection preference
     35 add_task(async function () {
     36  async function checkPrefSwitch(val) {
     37    Services.prefs.setBoolPref("browser.safebrowsing.downloads.enabled", val);
     38 
     39    gBrowser.reload();
     40    await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
     41 
     42    let doc = gBrowser.selectedBrowser.contentDocument;
     43    let checkbox = doc.getElementById("blockDownloads");
     44 
     45    let blockUncommon = doc.getElementById("blockUncommonUnwanted");
     46    let checked = checkbox.checked;
     47    is(checked, val, "downloads preference is initialized correctly");
     48    // should be disabled when val is false (= pref is turned off)
     49    is(
     50      blockUncommon.hasAttribute("disabled"),
     51      !val,
     52      "block uncommon checkbox is set correctly"
     53    );
     54    let update = waitForSettingControlChange(checkbox.control);
     55    // scroll the checkbox into view, otherwise the synthesizeMouseAtCenter will be ignored, and click it
     56    checkbox.scrollIntoView();
     57    EventUtils.synthesizeMouseAtCenter(
     58      checkbox.inputEl,
     59      {},
     60      gBrowser.selectedBrowser.contentWindow
     61    );
     62    await update;
     63 
     64    // check that setting is now turned on or off
     65    is(
     66      Services.prefs.getBoolPref("browser.safebrowsing.downloads.enabled"),
     67      !checked,
     68      "safebrowsing.downloads preference is set correctly"
     69    );
     70 
     71    // check if the uncommon warning checkbox has updated
     72    is(
     73      blockUncommon.hasAttribute("disabled"),
     74      val,
     75      "block uncommon checkbox is set correctly"
     76    );
     77  }
     78 
     79  await checkPrefSwitch(true);
     80  await checkPrefSwitch(false);
     81 });
     82 
     83 requestLongerTimeout(2);
     84 // test the unwanted/uncommon software warning preference
     85 add_task(async function () {
     86  async function checkPrefSwitch(val1, val2, isV2) {
     87    Services.prefs.setBoolPref(
     88      "browser.safebrowsing.downloads.remote.block_potentially_unwanted",
     89      val1
     90    );
     91    Services.prefs.setBoolPref(
     92      "browser.safebrowsing.downloads.remote.block_uncommon",
     93      val2
     94    );
     95    let testMalwareTable = "goog-malware-" + (isV2 ? "shavar" : "proto");
     96    testMalwareTable += ",test-malware-simple";
     97    if (val1 && val2) {
     98      testMalwareTable += ",goog-unwanted-" + (isV2 ? "shavar" : "proto");
     99      testMalwareTable += ",moztest-unwanted-simple";
    100    }
    101    Services.prefs.setCharPref("urlclassifier.malwareTable", testMalwareTable);
    102 
    103    gBrowser.reload();
    104    await BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser);
    105 
    106    let doc = gBrowser.selectedBrowser.contentDocument;
    107    let checkbox = doc.getElementById("blockUncommonUnwanted");
    108    let checked = checkbox.checked;
    109    is(
    110      checked,
    111      val1 && val2,
    112      "unwanted/uncommon preference is initialized correctly"
    113    );
    114 
    115    // scroll the checkbox into view, otherwise the synthesizeMouseAtCenter will be ignored, and click it
    116    checkbox.scrollIntoView();
    117    EventUtils.synthesizeMouseAtCenter(
    118      checkbox,
    119      {},
    120      gBrowser.selectedBrowser.contentWindow
    121    );
    122 
    123    // check that both settings are now turned on or off
    124    is(
    125      Services.prefs.getBoolPref(
    126        "browser.safebrowsing.downloads.remote.block_potentially_unwanted"
    127      ),
    128      !checked,
    129      "block_potentially_unwanted is set correctly"
    130    );
    131    is(
    132      Services.prefs.getBoolPref(
    133        "browser.safebrowsing.downloads.remote.block_uncommon"
    134      ),
    135      !checked,
    136      "block_uncommon is set correctly"
    137    );
    138 
    139    // when the preference is on, the malware table should include these ids
    140    let malwareTable = Services.prefs
    141      .getCharPref("urlclassifier.malwareTable")
    142      .split(",");
    143    if (isV2) {
    144      is(
    145        malwareTable.includes("goog-unwanted-shavar"),
    146        !checked,
    147        "malware table doesn't include goog-unwanted-shavar"
    148      );
    149    } else {
    150      is(
    151        malwareTable.includes("goog-unwanted-proto"),
    152        !checked,
    153        "malware table doesn't include goog-unwanted-proto"
    154      );
    155    }
    156    is(
    157      malwareTable.includes("moztest-unwanted-simple"),
    158      !checked,
    159      "malware table doesn't include moztest-unwanted-simple"
    160    );
    161    let sortedMalware = malwareTable.slice(0);
    162    sortedMalware.sort();
    163    Assert.deepEqual(
    164      malwareTable,
    165      sortedMalware,
    166      "malware table has been sorted"
    167    );
    168  }
    169 
    170  await checkPrefSwitch(true, true, false);
    171  await checkPrefSwitch(false, true, false);
    172  await checkPrefSwitch(true, false, false);
    173  await checkPrefSwitch(false, false, false);
    174  await checkPrefSwitch(true, true, true);
    175  await checkPrefSwitch(false, true, true);
    176  await checkPrefSwitch(true, false, true);
    177  await checkPrefSwitch(false, false, true);
    178 });