This repository has been archived by the owner on Nov 9, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
app.js
376 lines (333 loc) · 10.2 KB
/
app.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* Modern.IE main service; runs under node.js.
*
* Copyright (c) Microsoft Corporation; All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* THIS CODE IS PROVIDED AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS
* OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
*
* See the Apache Version 2.0 License for specific language governing permissions
* and limitations under the License.
*/
'use strict';
var url = require('url'),
fs = require('fs'),
port = process.env.PORT || 1337,
requester = require('./lib/requester.js'),
express = require('express'),
app = express(),
bodyParser = require('body-parser'),
cheerio = require('cheerio'),
//promises = require('promised-io/promise'),
//Deferred = require('promised-io').Deferred,
//promised = require('promised-io/promise'),
bluebird = require('bluebird'),
cssLoader = require('./lib/loadcss.js'),
jsLoader = require('./lib/loadjs.js'),
tests = require('./lib/loadchecks.js').tests,
//http = require('http'),
path = require('path'),
zlib = require('zlib'),
sanitize = require('validator').sanitize,
charset = 'utf-8',
querystring = require('querystring'),
version = JSON.parse(fs.readFileSync('package.json')).version;
/**
* Serializes a test results array and sends it via the response
* @param {object} res The response to use to send the results
* @param {Timestamp} start The start timestamp
* @param {Array} resultsArray The results of all the tests
* */
var sendResults = function (res, start, resultsArray) {
var results = {};
for (var i = 0; i < resultsArray.length; i++) {
results[resultsArray[i].testName] = resultsArray[i];
}
res.writeHeader(200, {
'Content-Type': 'application/json',
'X-Content-Type-Options': 'nosniff'
});
res.write(JSON.stringify({
version: version,
url: {uri: (this && this.url && this.url.href) || 'http://private'},
processTime: (Date.now() - start) / 1000,
results: results
}));
res.end();
};
/**
* Responds with a bad request error
* */
var sendBadRequest = function (res) {
res.writeHeader(400, {'Content-Type': 'text/plain'});
res.write('Your package is malformed' + '\n');
res.end();
};
/**
* Responds with an internal server error
* */
var sendInternalServerError = function (error, res) {
res.writeHeader(500, {'Content-Type': 'text/plain'});
res.write(JSON.stringify(error) + '\n');
res.end();
};
/**
* Responds with the error and message passed as parameters
* */
var remoteErrorResponse = function (response, statusCode, message) {
response.writeHead(200, {'Content-Type': 'application/json'});
response.write(JSON.stringify({statusCode: statusCode, message: message}));
response.end();
};
/**
* Decompresses a byte array using the decompression method passed by type.
* It supports gunzip and deflate
* */
//var decompress = function (body, type) {
// var deferred = new Deferred();
//
// if (type === 'gzip') {
// zlib.gunzip(body, function (err, data) {
// if (!err) {
// deferred.resolve({
// body: data.toString(charset),
// compression: 'gzip'
// });
// } else {
// deferred.reject('Error found: can\'t gunzip content ' + err);
// }
// });
// } else if (type === 'deflate') {
// zlib.inflateRaw(body, function (err, data) {
// if (!err) {
// deferred.resolve({
// body: data.toString(charset),
// compression: 'deflate'
// }
// );
// } else {
// deferred.reject('Error found: can\'t deflate content' + err);
// }
// });
// } else {
// process.nextTick(function () {
// deferred.reject('Unknown content encoding: ' + type);
// });
// }
//
// return deferred.promise;
//};
/**
* Launches and returns an array with the promises of all the non parallel tests
* (browser detection, css prefixes, etc.)
* */
var launchNonParallelTests = bluebird.method(function (promisesArray, website) {
tests.forEach(function (test) {
if (!test.parallel) {
promisesArray.push(test.check(website));
}
});
return bluebird.all(promisesArray);
});
/**
* Since several tests need HTML/JS/CSS content, fetch it all at once
* before calling any of the tests. Note that the tests still could
* retrieve additional content async, since they return a promise.
*/
// function analyze(data, content, res) {
// var start = Date.now(),
// promisesTests = [];
// var website = {
// url: url.parse(data.uri),
// auth: data.auth,
// content: content.body,
// compression: content.compression,
// $: cheerio.load(content.body, { lowerCaseTags: true, lowerCaseAttributeNames: true })
// };
// tests.forEach(function (test) {
// if (test.parallel) {
// promisesTests.push(test.check(website));
// }
// });
// cssLoader.loadCssFiles(website)
// .then(jsLoader.loadjsFiles)
// .then(launchNonParallelTests.bind(null, promisesTests))
// .then(promises.all)
// .then(sendResults.bind(website, res, start), sendInternalServerError.bind(website, res));
// }
/**
* Gets the body of a pages and decompresses if needed
* */
var getBody = function (request, website) {
return request.getAsync(website.url)
.then(function (result) {
var res = result[0];
var body = result[1];
if (!body) {
return bluebird.reject('Error found: Empty body');
}
website.url = url.parse(res.request.href); //In case there have been redirects
if (res.headers['content-encoding']) {
website.compression = res.headers['content-encoding'];
}
website.content = body.toString(charset);
website.$ = cheerio.load(website.content, {lowerCaseTags: true, lowerCaseAttributeNames: true});
return bluebird.resolve(website);
});
};
/**
* Returns the local scan page
* */
var returnMainPage = function (response) {
fs.readFile(path.join(__dirname, 'lib', 'index.html'), function (err, data) {
if (!err) {
response.writeHeader(200, {'Content-Type': 'text/html'});
} else {
response.writeHeader(500, {'Content-Type': 'text/plain'});
data = 'Server error: ' + err + '\n';
}
response.write(data);
response.end();
});
};
var getDomain = function (req, res) {
res.writeHeader(200, {'Content-Type': 'text/plain'});
res.write(process.env.USERDOMAIN || process.env.USERDNSDOMAIN);
res.end();
};
/**
* Decides what action needs to be done: show the main page or analyze a website
* */
var handleRequest = function (req, response) {
if (req.url === '/') {
// Return the "local scan" page
returnMainPage(response);
return;
}
var requestUrl = url.parse(req.url),
parameters = querystring.parse(requestUrl.query),
urlToAnalyze = sanitize(decodeURIComponent(parameters.url)).xss(),
user = sanitize(decodeURIComponent(parameters.user)).xss(),
password = sanitize(decodeURIComponent(parameters.password)).xss(),
domain = sanitize(decodeURIComponent(parameters.domain)).xss(),
auth;
var website = {
url: urlToAnalyze,
auth: null, // We might not need this field
$: null,
content: '',
compression: ''
};
var request;
// If the request gave a user/pass, send it along. Wait for 401 response before sending passwords.
if (user !== 'undefined' && password !== 'undefined') {
auth = {
'user': user,
'pass': password,
'sendImmediately': false
};
request = requester(auth);
website.auth = auth;
// request(urlToAnalyze,
// {auth: auth},
// processResponse(response, auth));
} else {
request = requester();
}
website.request = request;
var start = Date.now(),
promisesTests = [];
tests.forEach(function (test) {
if (test.parallel) {
promisesTests.push(test.check(website));
}
});
getBody(request, website)
.then(function (web) {
return cssLoader.loadCssFiles(web);
})
.then(function (web) {
return jsLoader.loadjsFiles(web);
})
.then(function (web) {
return launchNonParallelTests(promisesTests, web);
})
.then(function (results) {
sendResults(response, start, results);
})
.catch(function (error) {
sendInternalServerError(error, response);
});
};
/**
* Handles the content of a package sent via any of the plugins
* */
var handlePackage = function (req, res) {
if (!req.body.js || !req.body.css || !req.body.html || !req.body.url) {
remoteErrorResponse(res, 400, 'Missing information');
}
var start = Date.now(),
cssPromises = [],
website;
//TODO: try/catch this
try {
website = {
url: req.body.url ? url.parse(req.body.url.replace(/"/g, '')) : 'http://privates.ite',
content: req.body.html,
css: null,
js: JSON.parse(req.body.js),
$: cheerio.load(req.body.html, {lowerCaseTags: true, lowerCaseAttributeNames: true})
};
} catch (e) {
sendBadRequest(res);
return;
}
var remoteCSS = JSON.parse(req.body.css);
remoteCSS.forEach(function (parsedCSS) {
if (parsedCSS.content !== '') {
cssPromises.push(cssLoader.parseCSS(parsedCSS.content, parsedCSS.url, null, null, website));
}
});
bluebird.all(cssPromises)
.then(function (results) {
var cssResults = [],
promisesTests = [];
cssResults.concat.apply(cssResults, results);
website.css = cssResults;
for (var i = 0; i < tests.length; i++) {
// Call each test and save its returned promise
promisesTests.push(tests[i].check(website));
}
bluebird.all(promisesTests)
.then(sendResults.bind(null, res, start));
});
};
// ## CORS middleware
//
// see: http://stackoverflow.com/questions/7067966/how-to-allow-cors-in-express-nodejs
var allowCrossDomain = function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'POST');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// intercept OPTIONS method
if (req.method === 'OPTIONS') {
res.send(204);
}
else {
next();
}
};
app.use(allowCrossDomain);
app.use(bodyParser.json());
app.get('/', handleRequest);
app.get('/domain', getDomain);
app.post('/package', handlePackage);
app.listen(port);
console.log('Server started on port ' + port);
console.log('To scan a private url go to http://localhost:' + port + '/ and follow the instructions');
module.exports.port = port;