tor-browser

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

file_report_chromescript.js (2068B)


      1 /* eslint-env mozilla/chrome-script */
      2 
      3 const { NetUtil } = ChromeUtils.importESModule(
      4  "resource://gre/modules/NetUtil.sys.mjs"
      5 );
      6 
      7 // eslint-disable-next-line mozilla/reject-importGlobalProperties
      8 Cu.importGlobalProperties(["TextDecoder"]);
      9 
     10 const reportURI = "http://mochi.test:8888/foo.sjs";
     11 
     12 var openingObserver = {
     13  observe(subject, topic, data) {
     14    // subject should be an nsURI
     15    if (subject.QueryInterface == undefined) {
     16      return;
     17    }
     18 
     19    var message = { report: "", error: false };
     20 
     21    if (topic == "http-on-opening-request") {
     22      var asciiSpec = subject.QueryInterface(Ci.nsIHttpChannel).URI.asciiSpec;
     23      if (asciiSpec !== reportURI) {
     24        return;
     25      }
     26 
     27      var reportText = false;
     28      try {
     29        // Verify that the report was properly formatted.
     30        // We'll parse the report text as JSON and verify that the properties
     31        // have expected values.
     32        var reportText = "{}";
     33        var uploadStream = subject.QueryInterface(
     34          Ci.nsIUploadChannel
     35        ).uploadStream;
     36 
     37        if (uploadStream) {
     38          // get the bytes from the request body
     39          var binstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
     40            Ci.nsIBinaryInputStream
     41          );
     42          binstream.setInputStream(uploadStream);
     43 
     44          let bytes = NetUtil.readInputStream(binstream);
     45 
     46          // rewind stream as we are supposed to - there will be an assertion later if we don't.
     47          uploadStream
     48            .QueryInterface(Ci.nsISeekableStream)
     49            .seek(Ci.nsISeekableStream.NS_SEEK_SET, 0);
     50 
     51          let textDecoder = new TextDecoder();
     52          reportText = textDecoder.decode(bytes);
     53        }
     54 
     55        message.report = reportText;
     56      } catch (e) {
     57        message.error = e.toString();
     58      }
     59 
     60      sendAsyncMessage("opening-request-completed", message);
     61    }
     62  },
     63 };
     64 
     65 Services.obs.addObserver(openingObserver, "http-on-opening-request");
     66 addMessageListener("finish", function () {
     67  Services.obs.removeObserver(openingObserver, "http-on-opening-request");
     68 });