summaryrefslogtreecommitdiff
path: root/data/extensions/https-everywhere@eff.org/chrome/content/ruleset-tests.js
blob: f4eda1004acd701c4238b9701233350d8aeca2d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// load the HTTPS Everywhere component
var HTTPSEverywhere = null;
try {
  HTTPSEverywhere = Components.classes["@eff.org/https-everywhere;1"]
    .getService(Components.interfaces.nsISupports)
    .wrappedJSObject;
} catch(e) {
  // HTTPS Everywhere doesn't seem to be installed
}

// attach testRunner to the HTTPS Everywhere component so that status.js can run it
if(HTTPSEverywhere) {
  HTTPSEverywhere.httpseRulesetTests = {
    testRunner: testRunner
  };
}

function openStatus() {
  // make sure mixed content blocking preferences are correct
  Services.prefs.setBoolPref("security.mixed_content.block_display_content", false);
  Services.prefs.setBoolPref("security.mixed_content.block_active_content", true);
   
  // open the status tab
  var statusTab = gBrowser.addTab('chrome://https-everywhere/content/ruleset-tests-status.xul');
  gBrowser.selectedTab = statusTab;
}

// FIXME use a class rather than global state
var left_star = new RegExp(/^\*\./); // *.example.com
var accepted_test_targets = {}

function addTestTarget(urls, target, ruleset_ids) {
  // Add one target and associated metadata to the list of
  // URLs to be tested, performing housekeeping along the way
  var active_ids = [];
  if (target in accepted_test_targets) return;

  for (var n = 0; n < ruleset_ids.length; n++) {
    var rs_id = ruleset_ids[n];
    var rs = HTTPSEverywhere.https_rules.rulesetsByID[rs_id];
    if (rs.active) { active_ids.push(rs_id) };
  }
  // Some rulesets that might rewrite this target, let's test them
  if (active_ids.length > 0) {
    urls.push({
      url: 'http://'+target,
      target: target,
      ruleset_ids: active_ids
    });
  }
}

function testRunner() {
  Components.utils.import("resource://gre/modules/PopupNotifications.jsm");
  
  const numTabs = 6;
  var finished = false;
  var output = [];
  var urls = [];
  var num = 0;
  var targets_to_ids = HTTPSEverywhere.https_rules.targets;
  var ruleset_ids;
  accepted_test_targets = {};  // reset each time
 
  // we need every ruleset loaded from DB to check if it's active
  HTTPSEverywhere.https_rules.loadAllRulesets();

  for(var target in targets_to_ids) {
    ruleset_ids = targets_to_ids[target];
    if(target.indexOf("*") == -1)  {
      addTestTarget(urls, target, ruleset_ids);
    } else {
      // target is like *.example.wildcard.com, or www.example.*
      // let's see what we can do...
      var t = target.replace(left_star, "www.");
      if (t.indexOf("*") == -1) {
        addTestTarget(urls, t, ruleset_ids);
      }
    }
  }

  function test() {
    var i;
 
    HTTPSEverywhere.httpseRulesetTests.updateStatusBar(num, urls.length); 

    // start loading all the tabs
    window.focus
    for(i=0; i<numTabs; i++) {
      newTab(num);
    }
  }

  function newTab(number) {
    num +=1;
    // start a test in this tab
    if(urls.length) {

      // open a new tab
      var tab = gBrowser.addTab(urls[number].url);

      // wait for the page to load
      var intervalId = window.setTimeout(function(){

        // detect mixed content blocker
        if(PopupNotifications.getNotification("mixed-content-blocked", gBrowser.getBrowserForTab(tab))) {
          // build output to log
          ruleset_xmls = '';
          for(let i=0; i < urls[number].ruleset_ids.length; i++) {
            ruleset_xmls += urls[number].ruleset_ids[i].xmlName + ', ';
          }
          if(ruleset_xmls != '')
            ruleset_xmls = ruleset_xmls.substring(ruleset_xmls.length-2, 2);
          var output = 'MCB triggered: '+urls[number].url+' ('+ruleset_xmls+')';

          HTTPSEverywhere.httpseRulesetTests.updateLog(output);
        }

        // close this tab, and open another
        closeTab(tab);

      }, 10000);

    } else {

      //to run if urls is empty
      if (!finished) { 
        finished = true;
        window.setTimeout(function(){
          gBrowser.removeCurrentTab();
        }, 10000);
      }
    }
  }

  //closes tab
  function closeTab(tab) {
    HTTPSEverywhere.httpseRulesetTests.updateStatusBar(num, urls.length); 

    gBrowser.selectedTab = tab;
    gBrowser.removeCurrentTab();

    // open a new tab, if the tests haven't been canceled
    if(!HTTPSEverywhere.httpseRulesetTests.cancel) {
      newTab(num);
    }
  }

  //manages write out of output mochilog.txt, which contains sites that trigger mcb
  function writeout(weburl) {

    //initialize file
    var file = Components.classes["@mozilla.org/file/directory_service;1"].
    getService(Components.interfaces.nsIProperties).
    get("Home", Components.interfaces.nsIFile);
    writeoutfile = "mochilog.txt";
    file.append(writeoutfile);

    //create file if it does not already exist
    if(!file.exists()) {
      file.create(Components.interfaces.nsIFile.NORMAL_FILE_TYPE, 420);
    } 

    //initialize output stream
    var stream = Components.classes["@mozilla.org/network/file-output-stream;1"]
      .createInstance(Components.interfaces.nsIFileOutputStream);

    //permissions are set to append (will not delete existing contents)
    stream.init(file, 0x02 | 0x08 | 0x10, 0666, 0);

    var content = weburl + "\n";

    //Deal with ascii text and write out
    var converter = Components.classes["@mozilla.org/intl/converter-output-stream;1"].
      createInstance(Components.interfaces.nsIConverterOutputStream);
    converter.init(stream, "UTF-8", 0, 0);
    converter.writeString(content);
    converter.close();

    //alternative write out if ascii is not a concern
    //stream.write(content,content.length);
    //stream.close();

  }
  test();
}