forked from EFForg/https-everywhere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tester.js
168 lines (158 loc) · 5.29 KB
/
tester.js
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
// HTTPS Ruleset tester
// Generate a set of test URLs by reading in each of the rulesets and using the
// host from each target. Targets with a left-side wildcard ("*.foo.com") are
// treated as www. Targets with a wildcard elsewhere are ignored for now.
//
// Then, for each of the test URLs:
// - Run it through the rulesets.
// - Check that it changed, if not fail that URL.
// - Fetch the rewritten URL, following redirects.
// - If there any of the following occur, fail the original URL:
// - connection failure
// - connection timeout (30 seconds)
// - non-200 final status code
// - more than 10 redirects
//
// TODO: It seems like Node's HTTP library chooses among DNS responses
// differently than Firefox. To reproduce, run:
// js rewriter/tester.js Apple
// Much of the time this will fail on https://www.itunes.com/. However, loading
// https://www.itunes.com in a browser succeeds reliably. Then run:
// dig www.itunes.com
// and for each of the returned addresses,
// openssl s_client -connect 17.172.224.35:443 < /dev/null
// You'll find that some IP addresses refuse the connection and some don't.
var PARALLELISM = 10;
var path = require("path"),
fs = require("fs"),
DOMParser = require('xmldom').DOMParser,
readdirp = require('readdirp'),
es = require('event-stream'),
async = require('async'),
lrucache = require("../chromium/lru"),
rules = require("../chromium/rules"),
request = require("request"),
URI = require("URIjs");
var ruleSets = null;
var cases = [];
var ruleSets = new rules.RuleSets("fake user agent", lrucache.LRUCache, {});
var q;
function runTests() {
console.log("Running tests")
q = async.queue(runTestCase, PARALLELISM);
q.drain = function() {
console.log("All done.");
}
q.push(cases);
}
function runTestCase(testCase, callback) {
var uriString = testCase.url;
var ruleFile = testCase.ruleFile;
var line = ruleFile + " " + uriString;
var uri = new URI(uriString);
var rewritten = ruleSets.rewriteURI(uri.toString(), uri.host());
if (!rewritten) {
console.log("FAIL: no rewrite: ", line);
return callback();
} else {
line = ruleFile + " " + uriString + " " + rewritten;
console.log("Requesting:", rewritten);
request({
uri: rewritten,
maxRedirects: 10,
timeout: 10000, /* ms */
gzip: true,
strictSSL: true
}, function(error, response, body) {
// On failure, enqueue the test case for a second try later.
if (!testCase.tries) {
testCase.tries = 1;
q.push(testCase);
return callback();
}
if (error && error.message === "CERT_UNTRUSTED") {
// The nodejs cert validation is somewhat different from Firefox. The
// may be related to path building, intermediate cert fetching,
// different roots, and/or many other things. For now, just ignore any
// CERT_UNTRUSTED errors (but other errors like
// DEPTH_ZERO_SELF_SIGNED_CERT, we do not ignore).
console.log("PASS: ", uriString);
return callback();
} else if (error) {
console.log("FAIL:", error.message.replace(/\n/g, ' '), ":", line);
return callback();
} else if (response.statusCode !== 200) {
console.log("FAIL: status code", response.statusCode, ":", line);
return callback();
} else {
console.log("PASS: ", uriString);
return callback();
}
});
}
};
function readXml(dir) {
var stream = readdirp({
root: dir,
fileFilter: ['*.xml']
});
stream
.on('error', function (err) { console.error('fatal error', err); })
.pipe(es.map(function (entry, callback) {
var filename = path.join(dir, entry.path);
fs.readFile(filename, function(err, fileContents) {
// If the caller provided a filename or pattern on the command line, skip
// all rules that don't match that.
if (process.argv.length > 2) {
var matched = false;
for (var i = 2; i < process.argv.length; i++) {
if (filename.match(process.argv[i])) {
matched = true;
break;
}
}
if (!matched) {
callback();
return;
}
}
var xml = new DOMParser().parseFromString(fileContents.toString('utf8'), 'text/xml');
ruleSets.addFromXml(xml);
var rulesets = xml.getElementsByTagName('ruleset');
if (rulesets.length > 1) {
console.log('FAIL: more than one ruleset in', filename);
process.exit(1);
}
addTestCases(rulesets[0], filename);
callback();
});
})).on('end', function(err, body) {
runTests();
});
}
function addTestCases(xml, filename) {
if (xml.getAttribute('default_off') || xml.getAttribute('platform')) {
//console.log('Skipping inactive ruleset', filename);
return;
}
var targets = xml.getElementsByTagName('target');
for (var i = 0; i < targets.length; i++) {
var targetTag = targets[i];
var target = targetTag.getAttribute('host');
if (target[0] === '*') {
cases.push({
url: 'http://www.' + target.slice(2),
ruleFile: filename
});
} else if (target.match(/\*/)) {
// skip
} else {
cases.push({
url: 'http://' + target,
ruleFile: filename
});
}
};
}
console.log("Loading rules...");
readXml(__dirname + '/../rules');