tor-browser

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

browser_triggering_principal_exemption.js (2163B)


      1 // Bug 1662359 - Don't upgrade subresources whose triggering principal is exempt from HTTPS-Only mode.
      2 // https://bugzilla.mozilla.org/bug/1662359
      3 "use strict";
      4 
      5 const TRIGGERING_PAGE = "http://example.org";
      6 const LOADED_RESOURCE = "http://example.com";
      7 
      8 add_task(async function () {
      9  // Enable HTTPS-Only Mode
     10  await SpecialPowers.pushPrefEnv({
     11    set: [["dom.security.https_only_mode", true]],
     12  });
     13 
     14  await runTest(
     15    "Request with not exempt triggering principal should get upgraded.",
     16    "https://"
     17  );
     18 
     19  // Now exempt the triggering page
     20  await SpecialPowers.pushPermissions([
     21    {
     22      type: "https-only-load-insecure",
     23      allow: true,
     24      context: TRIGGERING_PAGE,
     25    },
     26  ]);
     27 
     28  await runTest(
     29    "Request with exempt triggering principal should not get upgraded.",
     30    "http://"
     31  );
     32 
     33  await SpecialPowers.popPermissions();
     34 });
     35 
     36 async function runTest(desc, startsWith) {
     37  const responseURL = await new Promise(resolve => {
     38    let xhr = new XMLHttpRequest();
     39    xhr.open("GET", LOADED_RESOURCE);
     40 
     41    // Replace loadinfo with one whose triggeringPrincipal is a content
     42    // principal for TRIGGERING_PAGE.
     43    const triggeringPrincipal =
     44      Services.scriptSecurityManager.createContentPrincipalFromOrigin(
     45        TRIGGERING_PAGE
     46      );
     47    let dummyURI = Services.io.newURI(LOADED_RESOURCE);
     48    let dummyChannel = NetUtil.newChannel({
     49      uri: dummyURI,
     50      triggeringPrincipal,
     51      loadingPrincipal: xhr.channel.loadInfo.loadingPrincipal,
     52      securityFlags: xhr.channel.loadInfo.securityFlags,
     53      contentPolicyType: xhr.channel.loadInfo.externalContentPolicyType,
     54    });
     55    xhr.channel.loadInfo = dummyChannel.loadInfo;
     56 
     57    xhr.onreadystatechange = () => {
     58      // We don't care about the result, just if Firefox upgraded the URL
     59      // internally.
     60      if (
     61        xhr.readyState !== XMLHttpRequest.OPENED ||
     62        xhr.readyState !== XMLHttpRequest.UNSENT
     63      ) {
     64        // Let's make sure this function doesn't get called anymore
     65        xhr.onreadystatechange = undefined;
     66        resolve(xhr.responseURL);
     67      }
     68    };
     69    xhr.send();
     70  });
     71  ok(responseURL.startsWith(startsWith), desc);
     72 }