tor-browser

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

browser_httpsfirst.js (3071B)


      1 "use strict";
      2 
      3 const TEST_PATH_HTTP = getRootDirectory(gTestPath).replace(
      4  "chrome://mochitests/content",
      5  "http://example.com"
      6 );
      7 
      8 const TIMEOUT_PAGE_URI_HTTP =
      9  TEST_PATH_HTTP + "file_httpsfirst_timeout_server.sjs";
     10 
     11 async function runPrefTest(aURI, aDesc, aAssertURLStartsWith) {
     12  await BrowserTestUtils.withNewTab("about:blank", async function (browser) {
     13    const loaded = BrowserTestUtils.browserLoaded(browser, false, null, true);
     14    BrowserTestUtils.startLoadingURIString(browser, aURI);
     15    await loaded;
     16 
     17    await ContentTask.spawn(
     18      browser,
     19      { aDesc, aAssertURLStartsWith },
     20      function ({ aDesc, aAssertURLStartsWith }) {
     21        ok(
     22          content.document.location.href.startsWith(aAssertURLStartsWith),
     23          aDesc
     24        );
     25      }
     26    );
     27 
     28    await SpecialPowers.removePermission("https-only-load-insecure", aURI);
     29  });
     30 }
     31 
     32 add_task(async function () {
     33  await SpecialPowers.pushPrefEnv({
     34    set: [["dom.security.https_first", false]],
     35  });
     36  Services.fog.testResetFOG();
     37 
     38  await runPrefTest(
     39    "http://example.com",
     40    "HTTPS-First disabled; Should not upgrade",
     41    "http://"
     42  );
     43 
     44  await SpecialPowers.pushPrefEnv({
     45    set: [["dom.security.https_first", true]],
     46  });
     47 
     48  for (const key of [
     49    "upgraded",
     50    "upgradedSchemeless",
     51    "downgraded",
     52    "downgradedSchemeless",
     53    "downgradedOnTimer",
     54    "downgradedOnTimerSchemeless",
     55    "downgradeTime",
     56    "downgradeTimeSchemeless",
     57  ]) {
     58    is(
     59      Glean.httpsfirst[key].testGetValue(),
     60      null,
     61      `No telemetry should have been recorded yet for ${key}`
     62    );
     63  }
     64 
     65  await runPrefTest(
     66    "http://example.com",
     67    "Should upgrade upgradeable website",
     68    "https://"
     69  );
     70 
     71  await runPrefTest(
     72    "http://httpsfirst.com",
     73    "Should downgrade after error.",
     74    "http://"
     75  );
     76 
     77  await runPrefTest(
     78    "http://httpsfirst.com/?https://httpsfirst.com",
     79    "Should downgrade after error and leave query params untouched.",
     80    "http://httpsfirst.com/?https://httpsfirst.com"
     81  );
     82 
     83  await runPrefTest(
     84    "http://domain.does.not.exist.example.com",
     85    "Should not downgrade on dnsNotFound error.",
     86    "https://"
     87  );
     88 
     89  await runPrefTest(
     90    TIMEOUT_PAGE_URI_HTTP,
     91    "Should downgrade after timeout.",
     92    "http://"
     93  );
     94 
     95  await runPrefTest(
     96    "http://invalid.example.com",
     97    "Should downgrade non-reachable site.",
     98    "http://"
     99  );
    100 
    101  info("Checking expected telemetry");
    102  is(Glean.httpsfirst.upgraded.testGetValue(), 1);
    103  is(Glean.httpsfirst.upgradedSchemeless.testGetValue(), null);
    104  is(Glean.httpsfirst.downgraded.testGetValue(), 3);
    105  is(Glean.httpsfirst.downgradedSchemeless.testGetValue(), null);
    106  is(Glean.httpsfirst.downgradedOnTimer.testGetValue().numerator, 1);
    107  is(Glean.httpsfirst.downgradedOnTimerSchemeless.testGetValue(), null);
    108  const downgradeSeconds =
    109    Glean.httpsfirst.downgradeTime.testGetValue().sum / 1_000_000_000;
    110  Assert.less(
    111    downgradeSeconds,
    112    10,
    113    "Summed downgrade time should be below 10 seconds"
    114  );
    115  is(null, Glean.httpsfirst.downgradeTimeSchemeless.testGetValue());
    116 });