tor-browser

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

test_trr_proxy.js (4749B)


      1 // These are globlas defined for proxy servers, in ProxyAutoConfig.cpp. See
      2 // PACGlobalFunctions
      3 /* globals dnsResolve, alert */
      4 
      5 /* This test checks that using a PAC script still works when TRR is on.
      6   Steps:
      7     - Set the pac script
      8     - Do a request to make sure that the script is loaded
      9     - Set the TRR mode
     10     - Make a request that would lead to running the PAC script
     11   We run these steps for TRR mode 2 and 3, and with fetchOffMainThread = true/false
     12 */
     13 
     14 const { HttpServer } = ChromeUtils.importESModule(
     15  "resource://testing-common/httpd.sys.mjs"
     16 );
     17 const { MockRegistrar } = ChromeUtils.importESModule(
     18  "resource://testing-common/MockRegistrar.sys.mjs"
     19 );
     20 
     21 registerCleanupFunction(async () => {
     22  Services.prefs.clearUserPref("network.proxy.type");
     23  Services.prefs.clearUserPref("network.proxy.parse_pac_on_socket_process");
     24  trr_clear_prefs();
     25 });
     26 
     27 function FindProxyForURL(url, host) {
     28  alert(`PAC resolving: ${host}`);
     29  alert(dnsResolve(host));
     30  return "DIRECT";
     31 }
     32 
     33 ChromeUtils.defineLazyGetter(this, "systemSettings", function () {
     34  return {
     35    QueryInterface: ChromeUtils.generateQI(["nsISystemProxySettings"]),
     36 
     37    mainThreadOnly: true,
     38    PACURI: `data:application/x-ns-proxy-autoconfig;charset=utf-8,${encodeURIComponent(
     39      FindProxyForURL.toString()
     40    )}`,
     41    getProxyForURI() {
     42      throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
     43    },
     44  };
     45 });
     46 
     47 const override = Cc["@mozilla.org/network/native-dns-override;1"].getService(
     48  Ci.nsINativeDNSResolverOverride
     49 );
     50 
     51 async function do_test_pac_dnsResolve() {
     52  Services.prefs.setCharPref("network.trr.confirmationNS", "skip");
     53  Services.console.reset();
     54  // Create a console listener.
     55  let consolePromise = new Promise(resolve => {
     56    let listener = {
     57      observe(message) {
     58        // Ignore unexpected messages.
     59        if (!(message instanceof Ci.nsIConsoleMessage)) {
     60          return;
     61        }
     62 
     63        if (message.message.includes("PAC file installed from")) {
     64          Services.console.unregisterListener(listener);
     65          resolve();
     66        }
     67      },
     68    };
     69 
     70    Services.console.registerListener(listener);
     71  });
     72 
     73  MockRegistrar.register(
     74    "@mozilla.org/system-proxy-settings;1",
     75    systemSettings
     76  );
     77  Services.prefs.setIntPref(
     78    "network.proxy.type",
     79    Ci.nsIProtocolProxyService.PROXYCONFIG_SYSTEM
     80  );
     81 
     82  let httpserv = new HttpServer();
     83  httpserv.registerPathHandler("/", function handler(metadata, response) {
     84    let content = "ok";
     85    response.setHeader("Content-Length", `${content.length}`);
     86    response.bodyOutputStream.write(content, content.length);
     87  });
     88  httpserv.start(-1);
     89 
     90  Services.prefs.setBoolPref("network.dns.native-is-localhost", false);
     91  Services.prefs.setIntPref("network.trr.mode", 0); // Disable TRR until the PAC is loaded
     92  override.addIPOverride("example.org", "127.0.0.1");
     93  let chan = NetUtil.newChannel({
     94    uri: `http://example.org:${httpserv.identity.primaryPort}/`,
     95    loadUsingSystemPrincipal: true,
     96  }).QueryInterface(Ci.nsIHttpChannel);
     97  await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
     98  await consolePromise;
     99 
    100  let trrServer = new TRRServer();
    101  await trrServer.start();
    102 
    103  registerCleanupFunction(async () => {
    104    if (trrServer) {
    105      await trrServer.stop();
    106    }
    107  });
    108 
    109  override.addIPOverride("foo.example.com", "127.0.0.1");
    110  Services.prefs.setCharPref(
    111    "network.trr.uri",
    112    `https://foo.example.com:${trrServer.port()}/doh?responseIP=127.0.0.1`
    113  );
    114 
    115  trr_test_setup();
    116 
    117  async function test_with(DOMAIN, trrMode) {
    118    Services.prefs.setIntPref("network.trr.mode", trrMode); // TRR first
    119    override.addIPOverride(DOMAIN, "127.0.0.1");
    120 
    121    chan = NetUtil.newChannel({
    122      uri: `http://${DOMAIN}:${httpserv.identity.primaryPort}/`,
    123      loadUsingSystemPrincipal: true,
    124    }).QueryInterface(Ci.nsIHttpChannel);
    125    await new Promise(resolve => chan.asyncOpen(new ChannelListener(resolve)));
    126 
    127    await override.clearHostOverride(DOMAIN);
    128  }
    129 
    130  await test_with("test1.com", 2);
    131  await test_with("test2.com", 3);
    132  await httpserv.stop();
    133  if (trrServer) {
    134    await trrServer.stop();
    135    trrServer = null;
    136  }
    137 }
    138 
    139 add_task(async function test_pac_dnsResolve() {
    140  Services.prefs.setBoolPref(
    141    "network.proxy.parse_pac_on_socket_process",
    142    false
    143  );
    144 
    145  await do_test_pac_dnsResolve();
    146 
    147  if (mozinfo.socketprocess_networking) {
    148    info("run test again");
    149    Services.prefs.clearUserPref("network.proxy.type");
    150    trr_clear_prefs();
    151    Services.prefs.setBoolPref(
    152      "network.proxy.parse_pac_on_socket_process",
    153      true
    154    );
    155    Services.prefs.setIntPref("network.proxy.type", 2);
    156    Services.prefs.setIntPref("network.proxy.type", 0);
    157    await do_test_pac_dnsResolve();
    158  }
    159 });