tor-browser

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

browser_add_exception_dialog.js (4215B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 // This test makes sure that adding certificate exceptions behaves correctly
      8 // when done from the prefs window
      9 
     10 ChromeUtils.defineESModuleGetters(this, {
     11  BrowserWindowTracker: "resource:///modules/BrowserWindowTracker.sys.mjs",
     12 });
     13 
     14 const EXCEPTIONS_DLG_URL = "chrome://pippki/content/exceptionDialog.xhtml";
     15 const EXCEPTIONS_DLG_FEATURES = "chrome,centerscreen,modal";
     16 const INVALID_CERT_DOMAIN = "self-signed.example.com";
     17 const INVALID_CERT_LOCATION = "https://" + INVALID_CERT_DOMAIN + "/";
     18 
     19 registerCleanupFunction(() => {
     20  let certOverrideService = Cc[
     21    "@mozilla.org/security/certoverride;1"
     22  ].getService(Ci.nsICertOverrideService);
     23  certOverrideService.clearValidityOverride(INVALID_CERT_DOMAIN, -1, {});
     24 });
     25 
     26 async function onCertExceptionUI(win) {
     27  Services.obs.removeObserver(onCertExceptionUI, "cert-exception-ui-ready");
     28  ok(win.gCert, "The certificate information should be available now");
     29 
     30  // Clicking on the View… button should open the certificate viewer.
     31  let viewButton = win.document.getElementById("viewCertButton");
     32  let tabPromise = BrowserTestUtils.waitForNewTab(
     33    gBrowser,
     34    url => url.startsWith("about:certificate?cert="),
     35    true
     36  );
     37  EventUtils.synthesizeMouseAtCenter(viewButton, {}, win);
     38  BrowserTestUtils.removeTab(await tabPromise);
     39 
     40  if (AppConstants.platform != "macosx") {
     41    // Pressing enter on the View… button should open the certificate viewer.
     42    tabPromise = BrowserTestUtils.waitForNewTab(
     43      gBrowser,
     44      url => url.startsWith("about:certificate?cert="),
     45      true
     46    );
     47    viewButton.focus();
     48    EventUtils.synthesizeKey("KEY_Enter", {}, win);
     49    BrowserTestUtils.removeTab(await tabPromise);
     50  }
     51 
     52  let dialog = win.document.getElementById("exceptiondialog");
     53  let confirmButton = dialog.getButton("extra1");
     54  confirmButton.click();
     55 }
     56 
     57 add_task(async function test_with_subdialog() {
     58  Services.obs.addObserver(onCertExceptionUI, "cert-exception-ui-ready");
     59 
     60  await BrowserTestUtils.withNewTab("about:preferences", async browser => {
     61    let params = {
     62      exceptionAdded: false,
     63      location: INVALID_CERT_LOCATION,
     64      prefetchCert: true,
     65    };
     66    await new Promise(resolve => {
     67      // Open the add exception dialog in the way that about:preferences does (in a sub-dialog).
     68      browser.contentWindow.gSubDialog.open(
     69        EXCEPTIONS_DLG_URL,
     70        { features: EXCEPTIONS_DLG_FEATURES, closedCallback: resolve },
     71        params
     72      );
     73    });
     74    ok(
     75      params.exceptionAdded,
     76      "The certificate exception should have been added"
     77    );
     78  });
     79 
     80  BrowserTestUtils.startLoadingURIString(gBrowser, INVALID_CERT_LOCATION);
     81  let loaded = await BrowserTestUtils.browserLoaded(
     82    gBrowser,
     83    false,
     84    INVALID_CERT_LOCATION,
     85    true
     86  );
     87  ok(loaded, "The certificate exception should allow the page to load");
     88 
     89  let certOverrideService = Cc[
     90    "@mozilla.org/security/certoverride;1"
     91  ].getService(Ci.nsICertOverrideService);
     92  certOverrideService.clearValidityOverride(INVALID_CERT_DOMAIN, -1, {});
     93 });
     94 
     95 add_task(async function test_with_dialog() {
     96  Services.obs.addObserver(onCertExceptionUI, "cert-exception-ui-ready");
     97 
     98  let params = {
     99    exceptionAdded: false,
    100    location: INVALID_CERT_LOCATION,
    101    prefetchCert: true,
    102  };
    103 
    104  let bWin = BrowserWindowTracker.getTopWindow();
    105 
    106  // Open the add exception dialog without a sub-dialog.
    107  bWin.openDialog(EXCEPTIONS_DLG_URL, "", EXCEPTIONS_DLG_FEATURES, params);
    108 
    109  ok(params.exceptionAdded, "The certificate exception should have been added");
    110 
    111  BrowserTestUtils.startLoadingURIString(gBrowser, INVALID_CERT_LOCATION);
    112  let loaded = await BrowserTestUtils.browserLoaded(
    113    gBrowser,
    114    false,
    115    INVALID_CERT_LOCATION,
    116    true
    117  );
    118  ok(loaded, "The certificate exception should allow the page to load");
    119 
    120  let certOverrideService = Cc[
    121    "@mozilla.org/security/certoverride;1"
    122  ].getService(Ci.nsICertOverrideService);
    123  certOverrideService.clearValidityOverride(INVALID_CERT_DOMAIN, -1, {});
    124 });