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
|
/*******************************************************************************
uBlock Origin - a comprehensive, efficient content blocker
Copyright (C) 2018-present 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/uBlock
*/
import {
domainFromHostname,
hostnameFromURI,
originFromURI,
} from './uri-utils.js';
/******************************************************************************/
// https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType
// Long term, convert code wherever possible to work with integer-based type
// values -- the assumption being that integer operations are faster than
// string operations.
export const NO_TYPE = 0;
export const BEACON = 1 << 0;
export const CSP_REPORT = 1 << 1;
export const FONT = 1 << 2;
export const IMAGE = 1 << 4;
export const IMAGESET = 1 << 4;
export const MAIN_FRAME = 1 << 5;
export const MEDIA = 1 << 6;
export const OBJECT = 1 << 7;
export const OBJECT_SUBREQUEST = 1 << 7;
export const PING = 1 << 8;
export const SCRIPT = 1 << 9;
export const STYLESHEET = 1 << 10;
export const SUB_FRAME = 1 << 11;
export const WEBSOCKET = 1 << 12;
export const XMLHTTPREQUEST = 1 << 13;
export const INLINE_FONT = 1 << 14;
export const INLINE_SCRIPT = 1 << 15;
export const OTHER = 1 << 16;
export const FRAME_ANY = MAIN_FRAME | SUB_FRAME | OBJECT;
export const FONT_ANY = FONT | INLINE_FONT;
export const INLINE_ANY = INLINE_FONT | INLINE_SCRIPT;
export const PING_ANY = BEACON | CSP_REPORT | PING;
export const SCRIPT_ANY = SCRIPT | INLINE_SCRIPT;
const typeStrToIntMap = {
'no_type': NO_TYPE,
'beacon': BEACON,
'csp_report': CSP_REPORT,
'font': FONT,
'image': IMAGE,
'imageset': IMAGESET,
'main_frame': MAIN_FRAME,
'media': MEDIA,
'object': OBJECT,
'object_subrequest': OBJECT_SUBREQUEST,
'ping': PING,
'script': SCRIPT,
'stylesheet': STYLESHEET,
'sub_frame': SUB_FRAME,
'websocket': WEBSOCKET,
'xmlhttprequest': XMLHTTPREQUEST,
'inline-font': INLINE_FONT,
'inline-script': INLINE_SCRIPT,
'other': OTHER,
};
export const METHOD_NONE = 0;
export const METHOD_CONNECT = 1 << 1;
export const METHOD_DELETE = 1 << 2;
export const METHOD_GET = 1 << 3;
export const METHOD_HEAD = 1 << 4;
export const METHOD_OPTIONS = 1 << 5;
export const METHOD_PATCH = 1 << 6;
export const METHOD_POST = 1 << 7;
export const METHOD_PUT = 1 << 8;
const methodStrToBitMap = {
'': METHOD_NONE,
'connect': METHOD_CONNECT,
'delete': METHOD_DELETE,
'get': METHOD_GET,
'head': METHOD_HEAD,
'options': METHOD_OPTIONS,
'patch': METHOD_PATCH,
'post': METHOD_POST,
'put': METHOD_PUT,
'CONNECT': METHOD_CONNECT,
'DELETE': METHOD_DELETE,
'GET': METHOD_GET,
'HEAD': METHOD_HEAD,
'OPTIONS': METHOD_OPTIONS,
'PATCH': METHOD_PATCH,
'POST': METHOD_POST,
'PUT': METHOD_PUT,
};
const methodBitToStrMap = new Map([
[ METHOD_NONE, '' ],
[ METHOD_CONNECT, 'connect' ],
[ METHOD_DELETE, 'delete' ],
[ METHOD_GET, 'get' ],
[ METHOD_HEAD, 'head' ],
[ METHOD_OPTIONS, 'options' ],
[ METHOD_PATCH, 'patch' ],
[ METHOD_POST, 'post' ],
[ METHOD_PUT, 'put' ],
]);
const reIPv4 = /^\d+\.\d+\.\d+\.\d+$/;
/******************************************************************************/
export const FilteringContext = class {
constructor(other) {
if ( other instanceof FilteringContext ) {
return this.fromFilteringContext(other);
}
this.tstamp = 0;
this.realm = '';
this.method = 0;
this.itype = NO_TYPE;
this.stype = undefined;
this.url = undefined;
this.aliasURL = undefined;
this.hostname = undefined;
this.domain = undefined;
this.ipaddress = undefined;
this.docId = -1;
this.frameId = -1;
this.docOrigin = undefined;
this.docHostname = undefined;
this.docDomain = undefined;
this.tabId = undefined;
this.tabOrigin = undefined;
this.tabHostname = undefined;
this.tabDomain = undefined;
this.redirectURL = undefined;
this.filter = undefined;
}
get type() {
return this.stype;
}
set type(a) {
this.itype = typeStrToIntMap[a] || NO_TYPE;
this.stype = a;
}
isRootDocument() {
return (this.itype & MAIN_FRAME) !== 0;
}
isDocument() {
return (this.itype & FRAME_ANY) !== 0;
}
isFont() {
return (this.itype & FONT_ANY) !== 0;
}
fromFilteringContext(other) {
this.realm = other.realm;
this.type = other.type;
this.method = other.method;
this.url = other.url;
this.hostname = other.hostname;
this.domain = other.domain;
this.ipaddress = other.ipaddress;
this.docId = other.docId;
this.frameId = other.frameId;
this.docOrigin = other.docOrigin;
this.docHostname = other.docHostname;
this.docDomain = other.docDomain;
this.tabId = other.tabId;
this.tabOrigin = other.tabOrigin;
this.tabHostname = other.tabHostname;
this.tabDomain = other.tabDomain;
this.redirectURL = other.redirectURL;
this.filter = undefined;
return this;
}
fromDetails({ originURL, url, type }) {
this.setDocOriginFromURL(originURL)
.setURL(url)
.setType(type);
return this;
}
duplicate() {
return (new FilteringContext(this));
}
setRealm(a) {
this.realm = a;
return this;
}
setType(a) {
this.type = a;
return this;
}
setURL(a) {
if ( a !== this.url ) {
this.hostname = this.domain = this.ipaddress = undefined;
this.url = a;
}
return this;
}
getHostname() {
if ( this.hostname === undefined ) {
this.hostname = hostnameFromURI(this.url);
}
return this.hostname;
}
setHostname(a) {
if ( a !== this.hostname ) {
this.domain = undefined;
this.hostname = a;
}
return this;
}
getDomain() {
if ( this.domain === undefined ) {
this.domain = domainFromHostname(this.getHostname());
}
return this.domain;
}
setDomain(a) {
this.domain = a;
return this;
}
getIPAddress() {
if ( this.ipaddress !== undefined ) {
return this.ipaddress;
}
const ipaddr = this.getHostname();
const c0 = ipaddr.charCodeAt(0);
if ( c0 === 0x5B /* [ */ ) {
return (this.ipaddress = ipaddr.slice(1, -1));
} else if ( c0 <= 0x39 && c0 >= 0x30 ) {
if ( reIPv4.test(ipaddr) ) {
return (this.ipaddress = ipaddr);
}
}
return (this.ipaddress = '');
}
// Must always be called *after* setURL()
setIPAddress(ipaddr) {
this.ipaddress = ipaddr || undefined;
return this;
}
getDocOrigin() {
if ( this.docOrigin === undefined ) {
this.docOrigin = this.tabOrigin;
}
return this.docOrigin;
}
setDocOrigin(a) {
if ( a !== this.docOrigin ) {
this.docHostname = this.docDomain = undefined;
this.docOrigin = a;
}
return this;
}
setDocOriginFromURL(a) {
return this.setDocOrigin(originFromURI(a));
}
getDocHostname() {
if ( this.docHostname === undefined ) {
this.docHostname = hostnameFromURI(this.getDocOrigin());
}
return this.docHostname;
}
setDocHostname(a) {
if ( a !== this.docHostname ) {
this.docDomain = undefined;
this.docHostname = a;
}
return this;
}
getDocDomain() {
if ( this.docDomain === undefined ) {
this.docDomain = domainFromHostname(this.getDocHostname());
}
return this.docDomain;
}
setDocDomain(a) {
this.docDomain = a;
return this;
}
// The idea is to minimize the amount of work done to figure out whether
// the resource is 3rd-party to the document.
is3rdPartyToDoc() {
let docDomain = this.getDocDomain();
if ( docDomain === '' ) { docDomain = this.docHostname; }
if ( this.domain !== undefined && this.domain !== '' ) {
return this.domain !== docDomain;
}
const hostname = this.getHostname();
if ( hostname.endsWith(docDomain) === false ) { return true; }
const i = hostname.length - docDomain.length;
if ( i === 0 ) { return false; }
return hostname.charCodeAt(i - 1) !== 0x2E /* '.' */;
}
setTabId(a) {
this.tabId = a;
return this;
}
getTabOrigin() {
return this.tabOrigin;
}
setTabOrigin(a) {
if ( a !== this.tabOrigin ) {
this.tabHostname = this.tabDomain = undefined;
this.tabOrigin = a;
}
return this;
}
setTabOriginFromURL(a) {
return this.setTabOrigin(originFromURI(a));
}
getTabHostname() {
if ( this.tabHostname === undefined ) {
this.tabHostname = hostnameFromURI(this.getTabOrigin());
}
return this.tabHostname;
}
setTabHostname(a) {
if ( a !== this.tabHostname ) {
this.tabDomain = undefined;
this.tabHostname = a;
}
return this;
}
getTabDomain() {
if ( this.tabDomain === undefined ) {
this.tabDomain = domainFromHostname(this.getTabHostname());
}
return this.tabDomain;
}
setTabDomain(a) {
this.docDomain = a;
return this;
}
// The idea is to minimize the amount of work done to figure out whether
// the resource is 3rd-party to the top document.
is3rdPartyToTab() {
let tabDomain = this.getTabDomain();
if ( tabDomain === '' ) { tabDomain = this.tabHostname; }
if ( this.domain !== undefined && this.domain !== '' ) {
return this.domain !== tabDomain;
}
const hostname = this.getHostname();
if ( hostname.endsWith(tabDomain) === false ) { return true; }
const i = hostname.length - tabDomain.length;
if ( i === 0 ) { return false; }
return hostname.charCodeAt(i - 1) !== 0x2E /* '.' */;
}
setFilter(a) {
this.filter = a;
return this;
}
pushFilter(a) {
if ( this.filter === undefined ) {
return this.setFilter(a);
}
if ( Array.isArray(this.filter) ) {
this.filter.push(a);
} else {
this.filter = [ this.filter, a ];
}
return this;
}
pushFilters(a) {
if ( this.filter === undefined ) {
return this.setFilter(a);
}
if ( Array.isArray(this.filter) ) {
this.filter.push(...a);
} else {
this.filter = [ this.filter, ...a ];
}
return this;
}
setMethod(a) {
this.method = methodStrToBitMap[a] || 0;
return this;
}
getMethodName() {
return FilteringContext.getMethodName(this.method);
}
static getMethod(a) {
return methodStrToBitMap[a] || 0;
}
static getMethodName(a) {
return methodBitToStrMap.get(a) || '';
}
BEACON = BEACON;
CSP_REPORT = CSP_REPORT;
FONT = FONT;
IMAGE = IMAGE;
IMAGESET = IMAGESET;
MAIN_FRAME = MAIN_FRAME;
MEDIA = MEDIA;
OBJECT = OBJECT;
OBJECT_SUBREQUEST = OBJECT_SUBREQUEST;
PING = PING;
SCRIPT = SCRIPT;
STYLESHEET = STYLESHEET;
SUB_FRAME = SUB_FRAME;
WEBSOCKET = WEBSOCKET;
XMLHTTPREQUEST = XMLHTTPREQUEST;
INLINE_FONT = INLINE_FONT;
INLINE_SCRIPT = INLINE_SCRIPT;
OTHER = OTHER;
FRAME_ANY = FRAME_ANY;
FONT_ANY = FONT_ANY;
INLINE_ANY = INLINE_ANY;
PING_ANY = PING_ANY;
SCRIPT_ANY = SCRIPT_ANY;
METHOD_NONE = METHOD_NONE;
METHOD_CONNECT = METHOD_CONNECT;
METHOD_DELETE = METHOD_DELETE;
METHOD_GET = METHOD_GET;
METHOD_HEAD = METHOD_HEAD;
METHOD_OPTIONS = METHOD_OPTIONS;
METHOD_PATCH = METHOD_PATCH;
METHOD_POST = METHOD_POST;
METHOD_PUT = METHOD_PUT;
static BEACON = BEACON;
static CSP_REPORT = CSP_REPORT;
static FONT = FONT;
static IMAGE = IMAGE;
static IMAGESET = IMAGESET;
static MAIN_FRAME = MAIN_FRAME;
static MEDIA = MEDIA;
static OBJECT = OBJECT;
static OBJECT_SUBREQUEST = OBJECT_SUBREQUEST;
static PING = PING;
static SCRIPT = SCRIPT;
static STYLESHEET = STYLESHEET;
static SUB_FRAME = SUB_FRAME;
static WEBSOCKET = WEBSOCKET;
static XMLHTTPREQUEST = XMLHTTPREQUEST;
static INLINE_FONT = INLINE_FONT;
static INLINE_SCRIPT = INLINE_SCRIPT;
static OTHER = OTHER;
static FRAME_ANY = FRAME_ANY;
static FONT_ANY = FONT_ANY;
static INLINE_ANY = INLINE_ANY;
static PING_ANY = PING_ANY;
static SCRIPT_ANY = SCRIPT_ANY;
static METHOD_NONE = METHOD_NONE;
static METHOD_CONNECT = METHOD_CONNECT;
static METHOD_DELETE = METHOD_DELETE;
static METHOD_GET = METHOD_GET;
static METHOD_HEAD = METHOD_HEAD;
static METHOD_OPTIONS = METHOD_OPTIONS;
static METHOD_PATCH = METHOD_PATCH;
static METHOD_POST = METHOD_POST;
static METHOD_PUT = METHOD_PUT;
};
/******************************************************************************/
|