This repository has been archived by the owner on Jun 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 108
/
interceptor.js
143 lines (106 loc) · 3.13 KB
/
interceptor.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
/**
* Interceptor
* Belongs to Decentraleyes.
*
* @author Thomas Rientjes
* @since 2016-04-06
* @license MPL 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
/**
* Interceptor
*/
var interceptor = {};
/**
* Public Methods
*/
interceptor.handleRequest = function (requestDetails, tabIdentifier, tab) {
let validCandidate, tabDomain, targetDetails, targetPath;
validCandidate = requestAnalyzer.isValidCandidate(requestDetails, tab);
if (!validCandidate) {
return {
'cancel': false
};
}
tabDomain = helpers.extractDomainFromUrl(tab.url, true);
if (tabDomain === null) {
tabDomain = Address.EXAMPLE;
}
// Temporary list of undetectable tainted domains.
let undetectableTaintedDomains = {
'cdnjs.com': true,
'dropbox.com': true,
'glowing-bear.org': true,
'minigames.mail.ru': true,
'report-uri.io': true,
'scotthelme.co.uk': true,
'securityheaders.io': true,
'stefansundin.github.io': true,
'udacity.com': true,
'yadi.sk': true,
'yourvotematters.co.uk': true
};
if (undetectableTaintedDomains[tabDomain] || (/yandex\./).test(tabDomain)) {
if (tabDomain !== 'yandex.ru') {
return interceptor._handleMissingCandidate(requestDetails.url);
}
}
targetDetails = requestAnalyzer.getLocalTarget(requestDetails);
targetPath = targetDetails.path;
if (!targetPath) {
return interceptor._handleMissingCandidate(requestDetails.url);
}
if (!files[targetPath]) {
return interceptor._handleMissingCandidate(requestDetails.url);
}
stateManager.requests[requestDetails.requestId] = {
tabIdentifier, targetDetails
};
return {
'redirectUrl': chrome.extension.getURL(targetPath)
};
};
/**
* Private Methods
*/
interceptor._handleMissingCandidate = function (requestUrl) {
if (interceptor.blockMissing === true) {
return {
'cancel': true
};
}
let requestUrlSegments = new URL(requestUrl);
if (requestUrlSegments.protocol === Address.HTTP) {
requestUrlSegments.protocol = Address.HTTPS;
requestUrl = requestUrlSegments.toString();
return {
'redirectUrl': requestUrl
};
} else {
return {
'cancel': false
};
}
};
interceptor._handleStorageChanged = function (changes) {
if (Setting.BLOCK_MISSING in changes) {
interceptor.blockMissing = changes.blockMissing.newValue;
}
};
/**
* Initializations
*/
interceptor.amountInjected = 0;
interceptor.blockMissing = false;
chrome.storage.local.get([Setting.AMOUNT_INJECTED, Setting.BLOCK_MISSING], function (items) {
interceptor.amountInjected = items.amountInjected || 0;
interceptor.blockMissing = items.blockMissing || false;
});
/**
* Event Handlers
*/
chrome.storage.onChanged.addListener(interceptor._handleStorageChanged);