This repository has been archived by the owner on Nov 15, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 84
/
traffic.js
602 lines (520 loc) · 21.3 KB
/
traffic.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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
/*******************************************************************************
httpswitchboard - a Chromium browser extension to black/white list requests.
Copyright (C) 2013 Raymond Hill
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}.
Home: https://github.com/gorhill/httpswitchboard
*/
/******************************************************************************/
/*jshint multistr: true */
var rootFrameReplacement = "<!DOCTYPE html> \
<html> \
<head> \
<style> \
@font-face { \
font-family: 'httpsb'; \
font-style: normal; \
font-weight: 400; \
src: local('httpsb'), url('{{fontUrl}}') format('truetype'); \
} \
body { \
margin: 0; \
border: 0; \
padding: 0; \
font: 13px httpsb,sans-serif; \
width: 100%; \
height: 100%; \
background: transparent url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACMAAAAjCAYAAAAe2bNZAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QkOFgcvc4DETwAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAACGSURBVFjD7ZZBCsAgEAMT6f+/nJ5arYcqiKtIPAaFYR2DFCAAgEQ8iwzLCLxZWglSZgKUdgHJk2kdLEY5C4QAUxeIFOINfwUOBGkLPBnkAIEDQPoEDiw+uoGHBQ4ovv4GnvTMS4EvC+wvhBvYAltgC2yBLbAFPlTgvKG6vxXZB6QOl2S7gNw6ktgOp+IH7wAAAABJRU5ErkJggg==') repeat; \
text-align: center; \
} \
div { \
margin: 2px; \
border: 0; \
padding: 0 2px; \
display: inline-block; \
color: white; \
background: #c00; \
} \
</style> \
<link href='{{cssURL}}?url={{originalURL}}&hostname={{hostname}}&t={{now}}' rel='stylesheet' type='text/css'> \
<title>Blocked by HTTPSB</title> \
</head> \
<body title='“{{hostname}}” blocked by HTTP Switchboard'> \
<div>{{hostname}}</div> \
</body> \
</html>";
var subFrameReplacement = "<!DOCTYPE html> \
<html> \
<head> \
<style> \
@font-face { \
font-family: 'httpsb'; \
font-style: normal; \
font-weight: 400; \
src: local('httpsb'), url('{{fontUrl}}') format('truetype'); \
} \
body { \
margin: 0; \
border: 0; \
padding: 0; \
font: 13px httpsb,sans-serif; \
width: 100%; \
height: 100%; \
background: transparent url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACMAAAAjCAYAAAAe2bNZAAAABmJLR0QA/wD/AP+gvaeTAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAB3RJTUUH3QkOFgcvc4DETwAAABl0RVh0Q29tbWVudABDcmVhdGVkIHdpdGggR0lNUFeBDhcAAACGSURBVFjD7ZZBCsAgEAMT6f+/nJ5arYcqiKtIPAaFYR2DFCAAgEQ8iwzLCLxZWglSZgKUdgHJk2kdLEY5C4QAUxeIFOINfwUOBGkLPBnkAIEDQPoEDiw+uoGHBQ4ovv4GnvTMS4EvC+wvhBvYAltgC2yBLbAFPlTgvKG6vxXZB6QOl2S7gNw6ktgOp+IH7wAAAABJRU5ErkJggg==') repeat; \
text-align: center; \
} \
div { \
margin: 2px; \
border: 0; \
padding: 0 2px; \
display: inline-block; \
color: white; \
background: #c00; \
} \
</style> \
<title>Blocked by HTTPSB</title> \
</head> \
<body title='“{{hostname}}” blocked by HTTP Switchboard'> \
<div>{{hostname}}</div> \
</body> \
</html>";
/******************************************************************************/
// Intercept and filter web requests according to white and black lists.
function onBeforeRequestHandler(details) {
// quickProfiler.start();
// console.debug('onBeforeRequestHandler()> "%s": %o', details.url, details);
var canEvaluate = true;
var httpsb = HTTPSB;
var tabId = details.tabId;
// Do not ignore traffic outside tabs
if ( tabId < 0 ) {
tabId = httpsb.behindTheSceneTabId;
// console.debug('onBeforeRequestHandler()> behind-the-scene: "%s"', details.url);
}
var requestURL = uriTools.normalizeURI(details.url);
var hostname, pageURL;
// Don't block chrome extensions
// rhill 2013-12-10: Avoid regex whenever a faster indexOf() can be used:
// here we can use fast indexOf() as a first filter -- which is executed
// for every single request (so speed matters).
if ( requestURL.indexOf(httpsb.chromeExtensionURLPrefix) === 0 ) {
// If it is HTTP Switchboard's root frame replacement URL, verify that
// the page that was blacklisted is still blacklisted, and if not,
// redirect to the previously blacklisted page.
if ( details.parentFrameId < 0 && requestURL.indexOf(httpsb.noopCSSURL) === 0 ) {
var matches = requestURL.match(/url=([^&]+)&hostname=([^&]+)/);
if ( matches ) {
pageURL = decodeURIComponent(matches[1]);
hostname = decodeURIComponent(matches[2]);
if ( httpsb.whitelisted(pageURL, 'main_frame', hostname) ) {
chrome.runtime.sendMessage({
what: 'gotoURL',
tabId: tabId,
url: pageURL
});
}
}
}
// Chrome extensions are not processed further
// quickProfiler.stop('onBeforeRequestHandler');
return;
}
// If it's a root frame or an app, bind to a new page stats store
var type = details.type;
var isSubFrame = type === 'sub_frame';
var isMainFrame = type === 'main_frame';
var isWebPage = isMainFrame && details.parentFrameId < 0;
var pageStats = httpsb.pageStatsFromTabId(tabId);
// rhill 2013-12-16: Do not interfere with apps. For now the heuristic is:
// If we have a `sub_frame` and no pageStats store, this is an app.
// https://github.com/gorhill/httpswitchboard/issues/91
var isApp = isSubFrame && !pageStats;
if ( isWebPage || isApp ) {
httpsb.bindTabToPageStats(tabId, requestURL);
}
pageStats = httpsb.pageStatsFromTabId(tabId);
// rhill 2013-12-16: I don't remember... Can pageStats still be nil at
// this point?
// Answer: Yes. Requests might still be dispatched after
// closing a tab it appears.
// if ( !pageStats ) {
// console.error('onBeforeRequestHandler() > no pageStats: %o', details);
// }
if ( isApp && pageStats ) {
pageStats.ignore = true;
}
hostname = uriTools.hostnameFromURI(requestURL);
pageURL = httpsb.pageUrlFromPageStats(pageStats);
// rhill 2013-12-15:
// Try to transpose generic `other` category into something more
// meaningful.
if ( type === 'other' ) {
type = httpsb.transposeType(type, requestURL);
}
if ( pageStats && pageStats.ignore ) {
canEvaluate = false;
}
// Block request?
// https://github.com/gorhill/httpswitchboard/issues/27
var block = false;
if ( canEvaluate ) {
block = httpsb.blacklisted(pageURL, type, hostname);
}
if ( pageStats ) {
// rhill 2014-01-15: Delay logging of non-blocked top `main_frame`
// requests, in order to ensure any potential redirects is reported
// in proper chronological order.
// https://github.com/gorhill/httpswitchboard/issues/112
if ( !isWebPage || block ) {
pageStats.recordRequest(type, requestURL, block);
}
}
// Collect global stats
httpsb.requestStats.record(type, block);
// whitelisted?
if ( !block ) {
// console.debug('onBeforeRequestHandler > allowing %s from %s', type, hostname);
// If the request is not blocked, this means the response could contain
// cookies. Thus, we go cookie hunting for this page url and record all
// those we find which hit any hostname found on this page.
// No worry, this is async.
// rhill 2013-11-07: Senseless to do this for behind-the-scene
// requests.
// rhill 2013-12-03: Do this here only for root frames.
if ( isWebPage && tabId !== httpsb.behindTheSceneTabId ) {
cookieHunter.recordPageCookiesAsync(pageStats);
}
// quickProfiler.stop('onBeforeRequestHandler');
return;
}
// blacklisted
// console.debug('onBeforeRequestHandler > blocking %s from %s', type, hostname);
// If it's a blacklisted frame, redirect to frame.html
// rhill 2013-11-05: The root frame contains a link to noop.css, this
// allows to later check whether the root frame has been unblocked by the
// user, in which case we are able to force a reload using a redirect.
var html, dataURI;
if ( isWebPage ) {
html = rootFrameReplacement;
html = html.replace(/{{fontUrl}}/g, httpsb.fontCSSURL);
html = html.replace(/{{cssURL}}/g, httpsb.noopCSSURL);
html = html.replace(/{{hostname}}/g, encodeURIComponent(hostname));
html = html.replace(/{{originalURL}}/g, encodeURIComponent(requestURL));
html = html.replace(/{{now}}/g, String(Date.now()));
dataURI = 'data:text/html;base64,' + btoa(html);
// quickProfiler.stop('onBeforeRequestHandler');
return { "redirectUrl": dataURI };
} else if ( isSubFrame ) {
html = subFrameReplacement;
html = html.replace(/{{fontUrl}}/g, httpsb.fontCSSURL);
html = html.replace(/{{hostname}}/g, hostname);
dataURI = 'data:text/html;base64,' + btoa(html);
// quickProfiler.stop('onBeforeRequestHandler');
return { "redirectUrl": dataURI };
}
// quickProfiler.stop('onBeforeRequestHandler');
return { "cancel": true };
}
/******************************************************************************/
// This is to handle cookies leaving the browser.
function onBeforeSendHeadersHandler(details) {
var httpsb = HTTPSB;
// Do not ignore traffic outside tabs
var tabId = details.tabId;
if ( tabId < 0 ) {
tabId = httpsb.behindTheSceneTabId;
}
// rhill 2013-12-16: do not interfere with apps.
// https://github.com/gorhill/httpswitchboard/issues/91
var pageStats = httpsb.pageStatsFromTabId(tabId);
if ( pageStats && pageStats.ignore ) {
return;
}
// Any cookie in there?
var ut = uriTools;
var hostname = ut.hostnameFromURI(details.url);
var pageURL = httpsb.pageUrlFromTabId(tabId);
var blacklistCookie = httpsb.blacklisted(pageURL, 'cookie', hostname);
var processReferer = httpsb.userSettings.processReferer;
if ( !blacklistCookie && !processReferer ) {
return;
}
var headerName, fromDomain, toDomain;
var headers = details.requestHeaders;
var i = headers.length;
var changed = false;
// I am no fan of deeply indented code paths, but for performance reasons
// I will tolerate it here. Thing is, here it is best to reuse as much
// already computed data as possible. (also, not sure if 'switch' would be
// a gain here, so far there is only two cases to treat).
while ( i-- ) {
headerName = headers[i].name.toLowerCase();
if ( headerName === 'referer' ) {
if ( processReferer ) {
fromDomain = ut.domainFromURI(headers[i].value);
toDomain = ut.domainFromHostname(hostname);
if ( fromDomain !== toDomain ) {
if ( httpsb.blacklisted(pageURL, '*', hostname) ) {
// console.debug('onBeforeSendHeadersHandler()> nulling referer "%s" for "%s"', fromDomain, toDomain);
headers[i].value = '';
httpsb.refererHeaderFoiledCounter++;
changed = true;
}
}
}
continue;
}
if ( headerName === 'cookie' ) {
if ( blacklistCookie ) {
// console.debug('HTTP Switchboard > foiled browser attempt to send cookie(s) to %o', details);
headers.splice(i, 1);
httpsb.cookieHeaderFoiledCounter++;
changed = true;
}
continue;
}
}
if ( changed ) {
return { requestHeaders: headers };
}
}
/******************************************************************************/
// To prevent inline javascript from being executed.
// Prevent inline scripting using `Content-Security-Policy`:
// https://dvcs.w3.org/hg/content-security-policy/raw-file/tip/csp-specification.dev.html
// This fixes:
// https://github.com/gorhill/httpswitchboard/issues/35
function onHeadersReceivedHandler(details) {
// console.debug('onHeadersReceivedHandler()> "%s": "%s"', details.url, details.statusLine);
var requestType = details.type;
var isSubFrame = requestType === 'sub_frame';
var isWebPage = requestType === 'main_frame' && details.parentFrameId < 0;
// Ignore anything which is not a top doc or an iframe
if ( !isWebPage && !isSubFrame ) {
return;
}
// Ignore schemes other than 'http...'
var requestURL = details.url;
if ( requestURL.indexOf('http') !== 0 ) {
return;
}
requestURL = uriTools.normalizeURI(requestURL);
var requestHostname = uriTools.hostname();
var requestScheme = uriTools.scheme();
// rhill 2013-12-08: ALWAYS evaluate for javascript, do not rely too much
// on the top page to be bound to a tab.
// https://github.com/gorhill/httpswitchboard/issues/75
// rhill 2013-12-07:
// Apparently in Opera, onBeforeRequest() is triggered while the
// URL is not yet bound to a tab (-1), which caused the code here
// to not be able to lookup the pageStats. So let the code here bind
// the page to a tab if not done yet.
// https://github.com/gorhill/httpswitchboard/issues/75
var httpsb = HTTPSB;
var tabId = details.tabId;
if ( tabId >= 0 && isWebPage ) {
httpsb.bindTabToPageStats(tabId, requestURL);
}
var pageStats = httpsb.pageStatsFromTabId(tabId);
var headers = details.responseHeaders;
if ( isWebPage ) {
// rhill 2014-01-15: Report redirects.
// https://github.com/gorhill/httpswitchboard/issues/112
if ( details.statusLine.indexOf(' 302') > 0 ) {
var i = headerIndexFromName('location', headers);
if ( i >= 0 ) {
// rhill 2014-01-20: Be ready to handle relative URLs.
// https://github.com/gorhill/httpswitchboard/issues/162
var locationURL = uriTools.normalizeURI(headers[i].value.trim());
if ( uriTools.scheme() === '' ) {
locationURL = requestScheme + '://' + requestHostname + uriTools.path();
}
httpsb.redirectRequests[locationURL] = requestURL;
}
// console.debug('onHeadersReceivedHandler()> redirect "%s" to "%s"', requestURL, headers[i].value);
}
// rhill 2014-01-11: Auto-scope and/or auto-whitelist only when the
// `main_frame` object is really received (status = 200 OK), i.e. avoid
// redirection, because the final URL might differ. This ensures proper
// scope is looked-up before auto-site-scoping and/or auto-whitelisting.
// https://github.com/gorhill/httpswitchboard/issues/119
if ( details.statusLine.indexOf(' 200') > 0 ) {
// rhill 2014-01-15: Report redirects if any.
// https://github.com/gorhill/httpswitchboard/issues/112
var mainFrameStack = [requestURL];
var destinationURL = requestURL;
var sourceURL;
while ( sourceURL = httpsb.redirectRequests[destinationURL] ) {
mainFrameStack.push(sourceURL);
delete httpsb.redirectRequests[destinationURL];
destinationURL = sourceURL;
}
if ( pageStats ) {
while ( destinationURL = mainFrameStack.pop() ) {
pageStats.recordRequest('main_frame', destinationURL, false);
}
}
// rhill 2014-01-10: Auto-site scope?
if ( httpsb.userSettings.autoCreateSiteScope ) {
httpsb.autoCreateTemporarySiteScope(requestURL);
}
// rhill 2013-12-23: Auto-whitelist page domain?
if ( httpsb.userSettings.autoWhitelistPageDomain ) {
httpsb.autoWhitelistTemporarilyPageDomain(requestURL);
}
}
}
// At this point we have a top web page or a embedded web page in a frame,
// so do not assume requestURL === pageURL.
// Evaluate according to scope.
// rhill 2013-12-07:
// Worst case scenario, if no pageURL can be found for this
// request, use global scope to evaluate whether it should be blocked
// or allowed.
// https://github.com/gorhill/httpswitchboard/issues/75
var pageURL = pageStats ? httpsb.pageUrlFromPageStats(pageStats) : '*';
if ( httpsb.whitelisted(pageURL, 'script', requestHostname) ) {
return;
}
// If javascript not allowed, say so through a `Content-Security-Policy`
// directive.
if ( isWebPage ) {
headers.push({
'name': 'Content-Security-Policy',
'value': "script-src 'none'"
});
}
// For inline javascript within iframes, we need to sandbox.
// https://github.com/gorhill/httpswitchboard/issues/73
// Now because sandbox cancels all permissions, this means
// not just javascript is disabled. To avoid negative side
// effects, I allow some other permissions, but...
// TODO: Reuse CSP `sandbox` directive if it's already in the
// headers (strip out `allow-scripts` if present),
// and find out if the `sandbox` in the header interfere with a
// `sandbox` attribute which might be present on the iframe.
else {
headers.push({
'name': 'Content-Security-Policy',
'value': 'sandbox allow-forms allow-same-origin'
});
}
return { responseHeaders: headers };
}
/******************************************************************************/
// As per Chrome API doc, webRequest.onErrorOccurred event is the last
// one called in the sequence of webRequest events.
// http://developer.chrome.com/extensions/webRequest.html
function onErrorOccurredHandler(details) {
// console.debug('onErrorOccurred()> "%s": %o', details.url, details);
// Ignore all that is not a main document
if ( details.type !== 'main_frame' || details.parentFrameId >= 0 ) {
return;
}
var httpsb = HTTPSB;
var pageStats = httpsb.pageStatsFromPageUrl(details.url);
if ( !pageStats ) {
return;
}
// rhill 2014-01-28: Unwind the stack of redirects if any. Chromium will
// emit an error when a web page redirects apparently endlessly, so
// we need to unravel and report all these redirects upon error.
// https://github.com/gorhill/httpswitchboard/issues/171
var requestURL = uriTools.normalizeURI(details.url);
var mainFrameStack = [requestURL];
var destinationURL = requestURL;
var sourceURL;
while ( sourceURL = httpsb.redirectRequests[destinationURL] ) {
mainFrameStack.push(sourceURL);
delete httpsb.redirectRequests[destinationURL];
destinationURL = sourceURL;
}
while ( destinationURL = mainFrameStack.pop() ) {
pageStats.recordRequest('main_frame', destinationURL, false);
}
}
/******************************************************************************/
var webRequestHandlerRequirements = {
'tabsBound': 0,
'listsLoaded': 0
};
function startWebRequestHandler(from) {
// Do not launch traffic handler if not all requirements are fullfilled.
// This takes care of pages being blocked when chromium is launched
// because there is no whitelist loaded and default is to block everything.
var o = webRequestHandlerRequirements;
o[from] = 1;
if ( Object.keys(o).map(function(k){return o[k];}).join().search('0') >= 0 ) {
return;
}
chrome.webRequest.onBeforeRequest.addListener(
onBeforeRequestHandler,
{
"urls": [
"http://*/*",
"https://*/*",
"chrome-extension://*/*"
],
"types": [
"main_frame",
"sub_frame",
'stylesheet',
"script",
"image",
"object",
"xmlhttprequest",
"other"
]
},
[ "blocking" ]
);
console.log('HTTP Switchboard> Beginning to intercept net requests at %s', (new Date()).toISOString());
chrome.webRequest.onBeforeSendHeaders.addListener(
onBeforeSendHeadersHandler,
{
'urls': [
"http://*/*",
"https://*/*"
]
},
['blocking', 'requestHeaders']
);
chrome.webRequest.onHeadersReceived.addListener(
onHeadersReceivedHandler,
{
'urls': [
"http://*/*",
"https://*/*"
]
},
['blocking', 'responseHeaders']
);
chrome.webRequest.onErrorOccurred.addListener(
onErrorOccurredHandler,
{
'urls': [
"http://*/*",
"https://*/*"
]
}
);
}
/******************************************************************************/
// Caller must ensure headerName is normalized to lower case.
function headerIndexFromName(headerName, headers) {
var i = headers.length;
while ( i-- ) {
if ( headers[i].name.toLowerCase() === headerName ) {
return i;
}
}
return -1;
}