-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
582 lines (544 loc) · 17.1 KB
/
background.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
var bouncerActive = false;
var thirdParties = {};
var mainHosts = {};
var mainUrls = {};
var helperAnchor = document.createElement( 'a' );
var active = false;
var blockHosts = new Array();
var whiteList = new Array();
var detected_spof = {};
var blocked = {};
var badges = {};
var tab_icons = {};
var tab_text = {};
var page_tlds = {};
var page_urls = {};
// known 3rd-party assets
var blockList = [
'platform\.twitter\.com',
'connect\.facebook\.net',
'platform\.linkedin\.com',
'assets\.pinterest\.com',
'widgets\.digg\.com',
'.*\.addthis\.com',
'ajax\.googleapis\.com',
'code\.jquery\.com',
'cdn\.jquerytools\.org',
'apis\.google\.com',
'.*\.google-analytics\.com',
'.*\.chartbeat\.com',
'static\.chartbeat\.com',
'.*\.2o7\.net',
'.*\.revsci\.net',
'.*\.omtrdc\.net',
'b\.scorecardresearch\.com',
'cdn\.sailthru\.com',
'.*browserid\.org',
'ad\.doubleclick\.net',
'js\.adsonar\.com',
'ycharts\.com',
'.*\.googlecode\.com',
'.*\.gstatic\.com',
'.*\.quantserve\.com',
'.*\.brightcove\.com'
];
var ICON_ACTIVE = "active.png";
var ICON_INACTIVE = "inactive.png";
var ICON_BLOCKED = "blocked.png";
var ICON_DETECTED = "detected.png";
var ICON_DEFAULT = ICON_INACTIVE;
var hostRegex = /[^\/]*\/\/([^\/]+)/;
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
// Determining the top-level-domain for a given host is way too complex to do right
// (you need a full list of them basically)
// We are going to simplify it and assume anything that is .co.xx will have 3 parts
// and everything else will have 2
function GetTLD(host){
var tld = host;
var noSecondaries = /\.(gov|ac|mil|net|org|co)\.\w\w$/i;
if (host.match(noSecondaries)) {
var threePart = /[\w]+\.[\w]+\.[\w]+$/i;
tld = host.match(threePart).toString();
} else {
var twoPart = /[\w]+\.[\w]+$/i;
tld = host.match(twoPart).toString();
}
return tld;
};
chrome.webRequest.onBeforeRequest.addListener(
function( info ) {
helperAnchor.href = info.url;
if ( info.type === 'main_frame' ) {
thirdParties[ info.tabId ] = {};
mainHosts[ info.tabId ] = helperAnchor.host;
mainUrls[ info.tabId ] = info.url;
}
},
{
urls: [
'http://*/*',
'https://*/*',
]
}
);
chrome.webRequest.onCompleted.addListener(
function( info ) {
helperAnchor.href = info.url;
if ( info.type !== 'main_frame' ) {
if ( thirdParties[ info.tabId ] ) {
var host = helperAnchor.host;
if ( host !== mainHosts[ info.tabId ] ) {
thirdParties[ info.tabId ][ host ] = thirdParties[ info.tabId ][ host ] || { total : 0 };
thirdParties[ info.tabId ][ host ][ info.type ] = thirdParties[ info.tabId ][ host ][ info.type ] || [];
if (
! thirdParties[ info.tabId ][ host ][ info.type ].filter(
request => request.url === info.url
).length
) {
thirdParties[ info.tabId ][ host ][ info.type ].push( { url : info.url } );
thirdParties[ info.tabId ][ host ].total++;
chrome.browserAction.setBadgeText( {
tabId : info.tabId,
text : '' + Object.keys( thirdParties[ info.tabId ] ).length
} );
}
}
}
}
},
{
urls: [
"http://*/*",
"https://*/*",
]
}
);
chrome.webRequest.onBeforeRequest.addListener(
function(info) {
var action = {};
if (info.type == 'main_frame') {
detected_spof[info.tabId] = {};
blocked[info.tabId] = {};
delete detected_spof[info.tabId];
delete blocked[info.tabId];
SetBadge(info.tabId, ICON_DEFAULT);
// update the TLD list for the current page
page_tlds[info.tabId] = new Array();
page_tlds[info.tabId].push(GetTLD(info.url.match(hostRegex)[1].toString()));
page_urls[info.tabId] = info.url;
}
if (active && (info.type == 'script' || info.type == 'stylesheet') && BlockURL(info.url, info.tabId)) {
Blocked(info.url, info.tabId);
SetBadge(info.tabId, ICON_BLOCKED)
console.log("blocking: " + info.url);
action.redirectUrl = 'https://blackhole.webpagetest.org/';
}
return action;
},
// filters
{
urls: [
"http://*/*",
"https://*/*",
]
},
// extraInfoSpec
["blocking"]
);
chrome.webRequest.onCompleted.addListener(
function(info) {
if (info.method == 'GET' && info.statusCode == 200) {
// make sure it was a text resource (html preferred) and not a file download
var ok = true;
if (info['responseHeaders'] != undefined) {
for (i = 0; i < info.responseHeaders.length && ok; i++) {
var name = info.responseHeaders[i].name.toLowerCase();
var value = info.responseHeaders[i].value.toLowerCase();
if (name == 'content-type') {
if (value.indexOf('text') < 0) {
ok = false;
}
} else if (name == 'content-disposition') {
ok = false;
}
}
}
if (ok) {
try {
var xhr = new XMLHttpRequest();
console.log('Fetching ' + info.url);
xhr.open('GET', info.url, true);
xhr.onreadystatechange = function() {
if (xhr.readyState != 4)
return;
if (xhr.status == 200) {
spofCheck(info.tabId, info.url, xhr.responseText);
xhr.responseText = null;
}
xhr.onreadystatechange = null;
xhr.abort();
};
xhr.send();
} catch (err) {}
}
}
return {};
},
// filters
{
urls: [
"http://*/*",
"https://*/*",
],
types: ['main_frame']
},
// extraInfoSpec
["responseHeaders"]
);
/*
See if we need to block the given URL
*/
function BlockURL(url, tabid) {
var block = false;
// get the host name
var host = url.match(hostRegex)[1].toString();
if (!isOnWhiteList(host, tabid)) {
block = IsOnBlockList(host);
for (i = 0; i < blockHosts.length && !block; i++) {
if (blockHosts[i] == host) {
block = true;
}
}
}
if (block) {
console.log("blocking: " + url);
}
return block;
}
/*
The given URL was blocked, keep track of it
*/
function Blocked(url, tabId) {
var host = url.match(hostRegex)[1].toString();
if (blocked[tabId] == undefined) {
blocked[tabId] = {};
}
if (blocked[tabId][host] == undefined) {
blocked[tabId][host] = new Array();
}
var exists = false;
for (i=0; i < blocked[tabId][host].length && !exists; i++) {
if (blocked[tabId][host][i] == url) {
exists = true;
}
}
if (!exists) {
blocked[tabId][host].push(url);
}
}
function IsOnBlockList(host) {
var found = false;
for (i = 0; i < blockList.length && !found; i++) {
var blockRegex = new RegExp(blockList[i], 'im');
if (blockRegex.test(host)) {
found = true;
}
}
return found;
}
function UpdateBlockList() {
console.log('Default block list:');
console.log(blockList);
var blockHostsStr = localStorage['hosts'];
if (blockHostsStr && blockHostsStr.length) {
blockHosts = JSON.parse(blockHostsStr);
// console.log('Local block list:');
// console.log(blockHosts);
}
var whiteListStr = localStorage['whitelist'];
if (whiteListStr && whiteListStr.length) {
whiteList = JSON.parse(whiteListStr);
// console.log('White List:');
// console.log(whiteList);
}
}
UpdateBlockList();
function SetBadge(tab_id, popupIcon, badgeText) {
var popupUrl = "popup.html?tab=" + tab_id;
chrome.browserAction.setPopup({tabId: tab_id, popup: popupUrl});
chrome.browserAction.setIcon({tabId: tab_id, path: popupIcon});
if (badgeText && badgeText.length) {
chrome.browserAction.setBadgeText({tabId: tab_id, text: badgeText});
}
tab_icons[tab_id] = popupIcon;
tab_text[tab_id] = badgeText;
}
function RefreshBadge(tab_id) {
if (tab_icons[tab_id] != undefined) {
SetBadge(tab_id, tab_icons[tab_id], tab_text[tab_id]);
}
}
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// refresh the badge every time the tab state changes, otherwise Chrome tends to revert to the default
RefreshBadge(tabId);
});
/*********************************************************************************
**********************************************************************************
**
** Communication with the pop-up
**
**********************************************************************************
**********************************************************************************/
function onRequest(request, sender, sendResponse) {
console.log( request );
if (request.msg == 'getSPOF') {
if (request['tab']) {
tabId = request['tab'];
var response = {isActive: active};
if (detected_spof[tabId] != undefined) {
response['spof'] = detected_spof[tabId];
if (response['spof']['scripts'] != undefined) {
for( var i = 0; i < response['spof']['scripts'].length; i++) {
if (response['spof']['scripts'][i]['host'] != undefined) {
response['spof']['scripts'][i]['whitelist'] =
isOnWhiteList(response['spof']['scripts'][i]['host'], null);
}
}
}
}
if (active && blocked[tabId] != undefined) {
response['blocked'] = blocked[tabId];
for (host in response['blocked']) {
if (isOnWhiteList(host, null)) {
delete response['blocked'][host];
}
}
}
if (page_urls[tabId] != undefined) {
response['url'] = page_urls[tabId];
}
sendResponse(response);
}
} else if (request.msg == 'getLists') {
var response = {isActive: active};
response['whitelist'] = whiteList;
response['block'] = blockHosts;
sendResponse(response);
} else if (request.msg == 'enable') {
active = true;
ICON_DEFAULT = ICON_ACTIVE;
chrome.browserAction.setIcon({path: ICON_DEFAULT});
sendResponse({});
} else if (request.msg == 'disable') {
active = false;
ICON_DEFAULT = ICON_INACTIVE;
chrome.browserAction.setIcon({path: ICON_DEFAULT});
sendResponse({});
} else if (request.msg == 'reset') {
blockHosts = new Array();
localStorage['hosts'] = JSON.stringify(blockHosts);
whiteList = new Array();
localStorage['whitelist'] = JSON.stringify(whiteList);
UpdateBlockList();
} else if (request.msg == 'wl_add' && request['host'] != undefined) {
addToWhiteList(request['host']);
} else if (request.msg == 'wl_remove' && request['host'] != undefined) {
removeFromWhiteList(request['host']);
}
if ( request.msg === 'bouncer_analyze' ) {
chrome.tabs.reload();
}
if ( request.msg == 'bouncer_get' && request.tab ) {
console.log( thirdParties );
sendResponse( {
url : mainUrls[ request.tab ],
thirdParties : thirdParties[ request.tab ]
} );
}
};
chrome.extension.onRequest.addListener( onRequest );
/*********************************************************************************
**********************************************************************************
**
** White list management
**
**********************************************************************************
**********************************************************************************/
function isOnWhiteList(host, tabid) {
var found = false;
// check the explicit white list
for (i = 0; i < whiteList.length && !found; i++) {
if (whiteList[i] == host) {
found = true;
}
}
// see if it matches a tld on the current page
if (tabid && !found && page_tlds[tabid] !== undefined) {
var tld = GetTLD(host);
for (i = 0; i < page_tlds[tabid].length && !found; i++) {
if (page_tlds[tabid][i] == tld) {
found = true;
}
}
}
return found;
}
function addToWhiteList(host) {
if (!isOnWhiteList(host, null)) {
whiteList.push(host);
localStorage['whitelist'] = JSON.stringify(whiteList);
}
}
function removeFromWhiteList(host) {
var found = false;
for (i = 0; i < whiteList.length && !found; i++) {
if (whiteList[i] == host) {
whiteList.remove(i);
localStorage['whitelist'] = JSON.stringify(whiteList);
found = true;
}
}
}
/*********************************************************************************
**********************************************************************************
**
** SPOF Detection logic
**
**********************************************************************************
**********************************************************************************/
function setSPOF(tab_id, spofHosts, spofScripts) {
// make sure we have hosts that aren't on the white list
var hostCount = 0;
for (var i = 0; i < spofHosts.length; i++) {
if (!isOnWhiteList(spofHosts[i], null)) {
hostCount++;
}
}
// count the scripts that block >= 40% of the content
warnCount = 0;
for (var i = 0; i < spofScripts.length; i++) {
if (spofScripts[i]['scripts'] !== undefined) {
for (j = 0; j < spofScripts[i]['scripts'].length; j++) {
if (spofScripts[i]['scripts'][j]['blockedContent'] !== undefined) {
if (spofScripts[i]['scripts'][j]['blockedContent'] >= 40) {
warnCount++;
}
}
}
}
}
if (warnCount > 0 && hostCount > 0 &&
(!active || blocked[tab_id] == undefined)) {
SetBadge(tab_id, ICON_DETECTED, warnCount.toString());
}
detected_spof[tab_id] = {hosts: spofHosts, scripts: spofScripts};
// add the hosts if we don't already know about them
var modified = false;
for (var i = 0; i < spofHosts.length; i++) {
var found = false;
for( var j = 0; j < blockHosts.length && !found; j++) {
if (blockHosts[j].toString() == spofHosts[i]) {
found = true;
}
}
if (!found) {
blockHosts.push(spofHosts[i]);
modified = true;
}
}
if (modified) {
localStorage['hosts'] = JSON.stringify(blockHosts);
}
}
function spofMatch(arr, str) {
var found = false;
for( var i = 0; i < arr.length && !found; i++) {
if (arr[i].toString() == str) {
found = true;
}
}
return found;
}
function spofAddArrayElement(arr, str) {
if (!spofMatch(arr,str)) {
arr.push(str);
}
}
function spofAddScript(arr, host, script, blockedContent) {
var found = false;
for( var i = 0; i < arr.length && !found; i++) {
if (arr[i]['host'].toString() == host) {
arr[i]['scripts'].push({'script':script, 'blockedContent':blockedContent});
found = true;
}
}
if (!found) {
arr.push({'host':host,
'scripts':[{'script':script, 'blockedContent':blockedContent}]});
}
}
function spofCheck(tab_id, url, pageText) {
// build a list of "safe" host names (anything where css or images were served)
var cssRegex = /<link [^>]*href[ =htps:"']+\/\/([^\/ "]+)\/[^>]+>/gi;
var cssValidRegex = /<link [^>]*rel[ ='"]+stylesheet/gi;
var imgRegex = /<img [^>]*src[ =htps:"']+\/\/([^\/ "]+)\/[^>]+>/gi;
var scriptRegex = /<script [^>]*src[ =htps:"']+\/\/([^\/ "]+)\/[^>]+>/gi;
var spofRegex = /<(script|link) [^>]*(src|href)[ =htps:"']+\/\/([^\/ "]+)\/[^>]+>/gi;
var htmlUrlRegex = /(href|src)[ =htps:"']+\/\/([^'"> "]+)\//i;
var htmlHostRegex = /(href|src)[ =htps:"']+\/\/([^\/ "]+)\//i;
var asyncRegex = /async[ ]*=/i;
var safeTLDs = new Array();
var thirdParty = new Array();
var spofHosts = new Array();
var spofScripts = new Array();
safeTLDs.push(GetTLD(url.match(hostRegex)[1].toString()));
var matches = pageText.match(cssRegex);
if (matches) {
for (var i = 0; i < matches.length; i++) {
try {
// do not count css files that have "font" in the name as being safe
var url = matches[i].toString().match(htmlUrlRegex)[2].toString();
if (!url.match(/font/i)) {
spofAddArrayElement(safeTLDs, GetTLD(matches[i].toString().match(htmlHostRegex)[2].toString()));
}
} catch(err) {}
}
}
matches = pageText.match(imgRegex);
if (matches) {
for (var i = 0; i < matches.length; i++) {
try {
spofAddArrayElement(safeTLDs, GetTLD(matches[i].toString().match(htmlHostRegex)[2].toString()));
} catch(err) {}
}
}
console.log("Safe TLD's: " + safeTLDs);
var pageLen = pageText.length;
while ((match = spofRegex.exec(pageText)) != null) {
try {
var script = match[0].toString();
var blockedContent = 100 - (((match.index + script.length) / pageLen) * 100);
var host = script.match(htmlHostRegex)[2].toString();
var tld = GetTLD(host);
if (!script.match(cssRegex) || script.match(cssValidRegex)) {
if (IsOnBlockList(host) || !spofMatch(safeTLDs, tld)) {
spofAddArrayElement(thirdParty, host);
if (!asyncRegex.test(script)) {
spofAddArrayElement(spofHosts, host);
spofAddScript(spofScripts, host, script, blockedContent);
console.log('SPOF (' + blockedContent.toFixed(2) + '%): ' + script);
}
}
}
} catch(err) {}
}
console.log("SPOF Hosts: " + spofHosts);
if (spofHosts.length) {
setSPOF(tab_id, spofHosts, spofScripts);
}
}