tor-browser

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

browser_httpsonly_prefs.js (2677B)


      1 "use strict";
      2 
      3 async function runPrefTest(
      4  aHTTPSOnlyPref,
      5  aHTTPSOnlyPrefPBM,
      6  aExecuteFromPBM,
      7  aDesc,
      8  aAssertURLStartsWith
      9 ) {
     10  await SpecialPowers.pushPrefEnv({
     11    set: [
     12      ["dom.security.https_only_mode", aHTTPSOnlyPref],
     13      ["dom.security.https_only_mode_pbm", aHTTPSOnlyPrefPBM],
     14    ],
     15  });
     16 
     17  await BrowserTestUtils.withNewTab("about:blank", async function (browser) {
     18    await ContentTask.spawn(
     19      browser,
     20      { aExecuteFromPBM, aDesc, aAssertURLStartsWith },
     21      // eslint-disable-next-line no-shadow
     22      async function ({ aExecuteFromPBM, aDesc, aAssertURLStartsWith }) {
     23        const responseURL = await new Promise(resolve => {
     24          let xhr = new XMLHttpRequest();
     25          xhr.timeout = 1200;
     26          xhr.open("GET", "http://example.com");
     27          if (aExecuteFromPBM) {
     28            xhr.channel.loadInfo.originAttributes = {
     29              privateBrowsingId: 1,
     30            };
     31          }
     32          xhr.onreadystatechange = () => {
     33            // We don't care about the result and it's possible that
     34            // the requests might even succeed in some testing environments
     35            if (
     36              xhr.readyState !== XMLHttpRequest.OPENED ||
     37              xhr.readyState !== XMLHttpRequest.UNSENT
     38            ) {
     39              // Let's make sure this function does not get called anymore
     40              xhr.onreadystatechange = undefined;
     41              resolve(xhr.responseURL);
     42            }
     43          };
     44          xhr.send();
     45        });
     46        ok(responseURL.startsWith(aAssertURLStartsWith), aDesc);
     47      }
     48    );
     49  });
     50 }
     51 
     52 add_task(async function () {
     53  requestLongerTimeout(2);
     54 
     55  await runPrefTest(
     56    false,
     57    false,
     58    false,
     59    "Setting no prefs should not upgrade",
     60    "http://"
     61  );
     62 
     63  await runPrefTest(
     64    true,
     65    false,
     66    false,
     67    "Setting aHTTPSOnlyPref should upgrade",
     68    "https://"
     69  );
     70 
     71  await runPrefTest(
     72    false,
     73    true,
     74    false,
     75    "Setting aHTTPSOnlyPrefPBM should not upgrade",
     76    "http://"
     77  );
     78 
     79  await runPrefTest(
     80    false,
     81    false,
     82    true,
     83    "Setting aPBM should not upgrade",
     84    "http://"
     85  );
     86 
     87  await runPrefTest(
     88    true,
     89    true,
     90    false,
     91    "Setting aHTTPSOnlyPref and aHTTPSOnlyPrefPBM should should upgrade",
     92    "https://"
     93  );
     94 
     95  await runPrefTest(
     96    true,
     97    false,
     98    true,
     99    "Setting aHTTPSOnlyPref and aPBM should upgrade",
    100    "https://"
    101  );
    102 
    103  await runPrefTest(
    104    false,
    105    true,
    106    true,
    107    "Setting aHTTPSOnlyPrefPBM and aPBM should upgrade",
    108    "https://"
    109  );
    110 
    111  await runPrefTest(
    112    true,
    113    true,
    114    true,
    115    "Setting aHTTPSOnlyPref and aHTTPSOnlyPrefPBM and aPBM should upgrade",
    116    "https://"
    117  );
    118 });