forked from gorhill/httpswitchboard
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttpsb.js
More file actions
306 lines (253 loc) · 9.85 KB
/
httpsb.js
File metadata and controls
306 lines (253 loc) · 9.85 KB
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
/*******************************************************************************
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
*/
/******************************************************************************/
HTTPSB.temporaryScopes = new PermissionScopes(HTTPSB);
HTTPSB.permanentScopes = new PermissionScopes(HTTPSB);
/******************************************************************************/
HTTPSB.normalizeScopeURL = function(url) {
if ( !url ) {
return null;
}
if ( url !== '*' ) {
url = uriTools.rootURLFromURI(url);
}
return url;
};
/******************************************************************************/
HTTPSB.createPageScopeIfNotExists = function(url) {
if ( url && url === '*' ) {
return true;
}
if ( !url ) {
return false;
}
url = uriTools.rootURLFromURI(url);
var tscope = this.temporaryScopes.scopes[url];
var pscope = this.permanentScopes.scopes[url];
if ( !tscope !== !pscope ) {
throw 'HTTP Switchboard.createPageScopeIfNotExists(): corrupted internal state';
}
// Skip everything if scopes exist and are switched on
if ( tscope && !tscope.off && pscope && !pscope.off ) {
return false;
}
// Create temporary scope or switch it on
if ( !tscope ) {
tscope = new PermissionScope(this);
tscope.whitelist('main_frame', '*');
tscope.whitelist('image', '*');
this.temporaryScopes.scopes[url] = tscope;
} else {
tscope.off = false;
}
// Create permanent scope or switch it on
if ( !pscope ) {
pscope = new PermissionScope(this);
pscope.whitelist('main_frame', '*');
pscope.whitelist('image', '*');
this.permanentScopes.scopes[url] = pscope;
} else {
pscope.off = false;
}
// Page-scoped permissions are always persisted, so that the
// entry is present, in order to be sure at least '*|main_frame' is
// persisted.
this.savePermissions();
return true;
};
/******************************************************************************/
HTTPSB.destroyPageScopeIfExists = function(url) {
if ( !url || url === '*' ) {
return false;
}
url = uriTools.rootURLFromURI(url);
var tscope = this.temporaryScopes.scopes[url];
var pscope = this.permanentScopes.scopes[url];
if ( !tscope !== !pscope ) {
throw 'HTTP Switchboard.destroyPageScopeIfExists(): corrupted internal state';
}
if ( !tscope && !pscope ) {
return false;
}
if ( tscope.off && pscope.off ) {
return false;
}
tscope.off = true;
pscope.off = true;
// Flush out the page permissions from storage.
this.savePermissions();
return true;
};
/******************************************************************************/
HTTPSB.scopePageExists = function(url) {
if ( !url ) {
return false;
}
// Global scope always exists
if ( url === '*' ) {
return true;
}
url = uriTools.rootURLFromURI(url);
var tscope = this.temporaryScopes.scopes[url];
var pscope = this.permanentScopes.scopes[url];
if ( !tscope !== !pscope ) {
throw 'HTTP Switchboard.scopePageExists(): corrupted internal state';
}
return tscope && !tscope.off && pscope && !pscope.off;
};
/******************************************************************************/
HTTPSB.evaluate = function(src, type, hostname) {
// rhill 2013-12-03: When HTTPSB is disengaged, all requests are
// considered being "allowed temporarily".
if ( this.off ) {
return 'gpt';
}
return this.temporaryScopes.evaluate(src, type, hostname);
};
/******************************************************************************/
// Whitelist something
HTTPSB.whitelistTemporarily = function(src, type, hostname) {
this.temporaryScopes.whitelist(src, type, hostname);
};
HTTPSB.whitelistPermanently = function(src, type, hostname) {
if ( this.permanentScopes.whitelist(src, type, hostname) ) {
this.savePermissions();
}
};
/******************************************************************************/
// Blacklist something
HTTPSB.blacklistTemporarily = function(src, type, hostname) {
this.temporaryScopes.blacklist(src, type, hostname);
};
HTTPSB.blacklistPermanently = function(src, type, hostname) {
if ( this.permanentScopes.blacklist(src, type, hostname) ) {
this.savePermissions();
}
};
/******************************************************************************/
// Remove something from both black and white lists.
// If key is [specific hostname]/[any type], remove also any existing
// auto-blacklisted types for the specific hostname.
HTTPSB.graylistTemporarily = function(src, type, hostname) {
this.temporaryScopes.graylist(src, type, hostname);
};
HTTPSB.graylistPermanently = function(src, type, hostname) {
if ( this.permanentScopes.graylist(src, type, hostname) ) {
// console.log('HTTP Switchboard > permanent graylisting %s from %s', type, hostname);
this.savePermissions();
}
};
/******************************************************************************/
// check whether something is blacklisted
HTTPSB.blacklisted = function(src, type, hostname) {
return this.evaluate(src, type, hostname).charAt(0) === 'r';
};
// check whether something is whitelisted
HTTPSB.whitelisted = function(src, type, hostname) {
return this.evaluate(src, type, hostname).charAt(0) === 'g';
};
/******************************************************************************/
HTTPSB.getTemporaryColor = function(src, type, hostname) {
// console.debug('HTTP Switchboard > getTemporaryColor(%s, %s, %s) = %s', src, type, hostname, evaluate(src, type, hostname));
return this.evaluate(src, type, hostname);
};
/******************************************************************************/
HTTPSB.getPermanentColor = function(src, type, hostname) {
var key = type + '|' + hostname;
var scope = this.permanentScopes.scopeFromURL(src);
if ( !scope || scope.off ) {
scope = this.permanentScopes.scopes['*'];
}
if ( scope.white.list[key] ) {
return 'gdp';
}
if ( scope.black.list[key] ) {
return 'rdp';
}
// rhill 2013-10-13: optimization: if type is not '*', hostname is not
// in the remote blacklists.
if ( type !== '*' ) {
return 'xxx';
}
// rhill 2013-11-07: if in the graylist, this means a read-only blacklist
// entry is occulted.
if ( scope.gray.list[key] ) {
return 'xxx';
}
// console.debug('this.blacklistReadonly[%s] = %o', hostname, this.blacklistReadonly[hostname]);
if ( this.blacklistReadonly[hostname] ) {
return 'rdp';
}
return 'xxx';
};
/******************************************************************************/
// Reset permission lists to their default state.
HTTPSB.revertPermissions = function() {
this.temporaryScopes.assign(this.permanentScopes);
};
/******************************************************************************/
// save white/blacklist
HTTPSB.savePermissions = function() {
var bin = {
'name': this.manifest.name,
'version': this.manifest.version,
// version < 0.1.3
// whitelist: this.whitelistUser,
// blacklist: this.blacklistUser
// version < 0.5.0
// 'whitelist': this.whitelistUser.toString(),
// 'blacklist': this.blacklistUser.toString(),
// 'graylist': this.graylistUser.toString(),
// version = 0.5.0
'scopes': this.permanentScopes.toString()
};
// console.debug('HTTP Switchboard > HTTPSB.savePermissions(): persisting %o', bin);
chrome.storage.local.set(bin, function() {
if ( chrome.runtime.lastError ) {
// console.log('HTTP Switchboard > saved permissions: %s', chrome.runtime.lastError.message());
}
chrome.storage.local.getBytesInUse('scopes', function(bytesInUse) {
// console.log('HTTP Switchboard > saved permissions: %d bytes used', bytesInUse);
});
// Remove pre-v0.5.0 obsolete entries
// TODO: Can be removed once everybody is using v0.5.0 or above
chrome.storage.local.remove(['whitelist','blacklist','graylist']);
});
};
/******************************************************************************/
HTTPSB.turnOff = function() {
this.off = true;
// rhill 2013-12-07:
// Relinquish control over javascript execution to the user.
// https://github.com/gorhill/httpswitchboard/issues/74
chrome.contentSettings.javascript.clear({});
}
HTTPSB.turnOn = function() {
chrome.contentSettings.javascript.clear({});
// rhill 2013-12-07:
// Tell Chromium to all javascript: HTTPSB will control whether
// javascript execute through `Content-Policy-Directive` and webRequest.
// https://github.com/gorhill/httpswitchboard/issues/74
chrome.contentSettings.javascript.set({
primaryPattern: 'https://*/*',
setting: 'allow'
});
chrome.contentSettings.javascript.set({
primaryPattern: 'http://*/*',
setting: 'allow'
});
this.off = false;
}