tor-browser

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

test_trr_proxy_auth.js (3236B)


      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 const { NodeHTTP2ProxyServer } = ChromeUtils.importESModule(
      8  "resource://testing-common/NodeServer.sys.mjs"
      9 );
     10 
     11 /* import-globals-from head_cache.js */
     12 /* import-globals-from head_cookies.js */
     13 /* import-globals-from head_channels.js */
     14 
     15 function setup() {
     16  trr_test_setup();
     17 }
     18 
     19 setup();
     20 registerCleanupFunction(async () => {
     21  trr_clear_prefs();
     22 });
     23 
     24 function AuthPrompt() {}
     25 
     26 AuthPrompt.prototype = {
     27  user: "guest",
     28  pass: "guest",
     29 
     30  QueryInterface: ChromeUtils.generateQI(["nsIAuthPrompt2"]),
     31 
     32  promptAuth: function ap_promptAuth(channel, level, authInfo) {
     33    authInfo.username = this.user;
     34    authInfo.password = this.pass;
     35 
     36    return true;
     37  },
     38 
     39  asyncPromptAuth: function ap_async() {
     40    throw Components.Exception("", Cr.NS_ERROR_NOT_IMPLEMENTED);
     41  },
     42 };
     43 
     44 function Requestor() {}
     45 
     46 Requestor.prototype = {
     47  QueryInterface: ChromeUtils.generateQI(["nsIInterfaceRequestor"]),
     48 
     49  getInterface: function requestor_gi(iid) {
     50    if (iid.equals(Ci.nsIAuthPrompt2)) {
     51      // Allow the prompt to store state by caching it here
     52      if (!this.prompt) {
     53        this.prompt = new AuthPrompt();
     54      }
     55      return this.prompt;
     56    }
     57 
     58    throw Components.Exception("", Cr.NS_ERROR_NO_INTERFACE);
     59  },
     60 
     61  prompt: null,
     62 };
     63 
     64 // Test if we successfully retry TRR request on main thread.
     65 add_task(async function test_trr_proxy_auth() {
     66  let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
     67    Ci.nsIX509CertDB
     68  );
     69  addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
     70  addCertFromFile(certdb, "proxy-ca.pem", "CTu,u,u");
     71 
     72  let trrServer = new TRRServer();
     73  await trrServer.start();
     74  Services.prefs.setIntPref("network.trr.mode", 3);
     75  Services.prefs.setCharPref(
     76    "network.trr.uri",
     77    `https://foo.example.com:${trrServer.port()}/dns-query`
     78  );
     79 
     80  await trrServer.registerDoHAnswers("test.proxy.com", "A", {
     81    answers: [
     82      {
     83        name: "test.proxy.com",
     84        ttl: 55,
     85        type: "A",
     86        flush: false,
     87        data: "3.3.3.3",
     88      },
     89    ],
     90  });
     91 
     92  await new TRRDNSListener("test.proxy.com", "3.3.3.3");
     93 
     94  let proxy = new NodeHTTP2ProxyServer();
     95  await proxy.start(0, true);
     96  registerCleanupFunction(async () => {
     97    await proxy.stop();
     98    await trrServer.stop();
     99  });
    100 
    101  let authTriggered = false;
    102  let observer = {
    103    QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
    104    observe(aSubject, aTopic) {
    105      if (aTopic == "http-on-examine-response") {
    106        Services.obs.removeObserver(observer, "http-on-examine-response");
    107        let channel = aSubject.QueryInterface(Ci.nsIChannel);
    108        channel.notificationCallbacks = new Requestor();
    109        if (
    110          channel.URI.spec.startsWith(
    111            `https://foo.example.com:${trrServer.port()}/dns-query`
    112          )
    113        ) {
    114          authTriggered = true;
    115        }
    116      }
    117    },
    118  };
    119  Services.obs.addObserver(observer, "http-on-examine-response");
    120 
    121  Services.dns.clearCache(true);
    122  await new TRRDNSListener("test.proxy.com", "3.3.3.3");
    123  Assert.ok(authTriggered);
    124 });