tor-browser

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

test_dns_proxy_bypass.js (2422B)


      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 var prefs = Services.prefs;
      8 
      9 function setup() {
     10  prefs.setBoolPref("network.dns.notifyResolution", true);
     11  prefs.setCharPref("network.proxy.socks", "127.0.0.1");
     12  prefs.setIntPref("network.proxy.socks_port", 9000);
     13  prefs.setIntPref("network.proxy.type", 1);
     14 }
     15 
     16 setup();
     17 registerCleanupFunction(async () => {
     18  prefs.clearUserPref("network.proxy.socks");
     19  prefs.clearUserPref("network.proxy.socks_port");
     20  prefs.clearUserPref("network.proxy.type");
     21  prefs.clearUserPref("network.dns.notifyResolution");
     22 });
     23 
     24 var url = "ws://dnsleak.example.com";
     25 
     26 var dnsRequestObserver = {
     27  register() {
     28    this.obs = Services.obs;
     29    this.obs.addObserver(this, "dns-resolution-request");
     30  },
     31 
     32  unregister() {
     33    if (this.obs) {
     34      this.obs.removeObserver(this, "dns-resolution-request");
     35    }
     36  },
     37 
     38  observe(subject, topic, data) {
     39    if (topic == "dns-resolution-request") {
     40      Assert.ok(!data.includes("dnsleak.example.com"), `no dnsleak: ${data}`);
     41    }
     42  },
     43 };
     44 
     45 function WSListener(closure) {
     46  this._closure = closure;
     47 }
     48 WSListener.prototype = {
     49  onAcknowledge() {},
     50  onBinaryMessageAvailable() {},
     51  onMessageAvailable() {},
     52  onServerClose() {},
     53  onStart() {},
     54  onStop() {
     55    dnsRequestObserver.unregister();
     56    this._closure();
     57  },
     58 };
     59 
     60 add_task(async function test_dns_websocket_channel() {
     61  dnsRequestObserver.register();
     62 
     63  var chan = Cc["@mozilla.org/network/protocol;1?name=ws"].createInstance(
     64    Ci.nsIWebSocketChannel
     65  );
     66 
     67  var uri = Services.io.newURI(url);
     68  chan.initLoadInfo(
     69    null, // aLoadingNode
     70    Services.scriptSecurityManager.createContentPrincipal(uri, {}),
     71    null, // aTriggeringPrincipal
     72    Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
     73    Ci.nsIContentPolicy.TYPE_WEBSOCKET
     74  );
     75 
     76  await new Promise(resolve =>
     77    chan.asyncOpen(uri, url, {}, 0, new WSListener(resolve), null)
     78  );
     79 });
     80 
     81 add_task(async function test_dns_resolve_proxy() {
     82  dnsRequestObserver.register();
     83 
     84  let { error } = await new TRRDNSListener("dnsleak.example.com", {
     85    expectEarlyFail: true,
     86  });
     87  Assert.equal(
     88    error.result,
     89    Cr.NS_ERROR_UNKNOWN_PROXY_HOST,
     90    "error is NS_ERROR_UNKNOWN_PROXY_HOST"
     91  );
     92  dnsRequestObserver.unregister();
     93 });