-
Notifications
You must be signed in to change notification settings - Fork 101
/
engine.ts
1481 lines (1275 loc) · 44 KB
/
engine.ts
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
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Copyright (c) 2017-present Ghostery GmbH. All rights reserved.
*
* 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 https://mozilla.org/MPL/2.0/.
*/
import type { IMessageFromBackground } from '@ghostery/adblocker-content';
import Config from '../config.js';
import { StaticDataView, sizeOfASCII, sizeOfByte, sizeOfBool } from '../data-view.js';
import { EventEmitter } from '../events.js';
import {
adsAndTrackingLists,
adsLists,
Fetch,
fetchLists,
fetchResources,
fullLists,
} from '../fetch.js';
import { HTMLSelector } from '../html-filtering.js';
import CosmeticFilter, { normalizeSelector } from '../filters/cosmetic.js';
import NetworkFilter from '../filters/network.js';
import { block } from '../filters/dsl.js';
import { FilterType, IListDiff, IPartialRawDiff, parseFilters } from '../lists.js';
import Request from '../request.js';
import Resources from '../resources.js';
import CosmeticFilterBucket from './bucket/cosmetic.js';
import NetworkFilterBucket from './bucket/network.js';
import HTMLBucket from './bucket/html.js';
import { Metadata, IPatternLookupResult } from './metadata.js';
import Preprocessor, { Env } from '../preprocessor.js';
import PreprocessorBucket from './bucket/preprocessor.js';
import IFilter from '../filters/interface.js';
import { ICategory } from './metadata/categories.js';
import { IOrganization } from './metadata/organizations.js';
import { IPattern } from './metadata/patterns.js';
export const ENGINE_VERSION = 699;
function shouldApplyHideException(filters: NetworkFilter[]): boolean {
if (filters.length === 0) {
return false;
}
// Get $Xhide filter with highest priority:
// $Xhide,important > $Xhide > @@$Xhide
let genericHideFilter: NetworkFilter | undefined;
let currentScore = 0;
for (const filter of filters) {
// To encode priority between filters, we create a bitmask with the following:
// $important,Xhide = 100 (takes precedence)
// $Xhide = 010 (exception to @@$Xhide)
// @@$Xhide = 001 (forbids Xhide filters)
const score: number = (filter.isImportant() ? 4 : 0) | (filter.isException() ? 1 : 2);
// Highest `score` has precedence
if (score >= currentScore) {
currentScore = score;
genericHideFilter = filter;
}
}
if (genericHideFilter === undefined) {
return false;
}
// Check that there is at least one $generichide match and no exception
return genericHideFilter.isException();
}
export interface BlockingResponse {
match: boolean;
redirect:
| undefined
| {
body: string;
contentType: string;
dataUrl: string;
};
exception: NetworkFilter | undefined;
filter: NetworkFilter | undefined;
metadata: IPatternLookupResult[] | undefined;
}
export interface Caching {
path: string;
read: (path: string) => Promise<Uint8Array>;
write: (path: string, buffer: Uint8Array) => Promise<void>;
}
type NetworkFilterMatchingContext = {
request: Request;
filterType: FilterType.NETWORK;
};
type CosmeticFilterMatchingContext =
| {
url: string;
callerContext: any; // Additional context given from user
filterType: FilterType.COSMETIC;
}
| {
request: Request; // For HTML Filters
filterType: FilterType.COSMETIC;
};
type NetworkFilterMatchEvent = (request: Request, result: BlockingResponse) => void;
export type EngineEventHandlers = {
'request-allowed': NetworkFilterMatchEvent;
'request-blocked': NetworkFilterMatchEvent;
'request-redirected': NetworkFilterMatchEvent;
'request-whitelisted': NetworkFilterMatchEvent;
'csp-injected': (request: Request, csps: string) => void;
'html-filtered': (htmlSelectors: HTMLSelector[], url: string) => void;
'script-injected': (script: string, url: string) => void;
'style-injected': (style: string, url: string) => void;
'filter-matched': (
match: {
filter?: CosmeticFilter | NetworkFilter | undefined;
exception?: CosmeticFilter | NetworkFilter | undefined;
},
context: CosmeticFilterMatchingContext | NetworkFilterMatchingContext,
) => any;
};
export default class FilterEngine extends EventEmitter<EngineEventHandlers> {
private static fromCached<T extends typeof FilterEngine>(
this: T,
init: () => Promise<InstanceType<T>>,
caching?: Caching,
): Promise<InstanceType<T>> {
if (caching === undefined) {
return init();
}
const { path, read, write } = caching;
return read(path)
.then((buffer) => this.deserialize(buffer) as InstanceType<T>)
.catch(() => init().then((engine) => write(path, engine.serialize()).then(() => engine)));
}
public static empty<T extends FilterEngine>(
this: new (...args: any[]) => T,
config: Partial<Config> = {},
): T {
return new this({ config });
}
/**
* Create an instance of `FiltersEngine` (or subclass like `ElectronBlocker`,
* etc.), from the list of subscriptions provided as argument (e.g.:
* EasyList).
*
* Lists are fetched using the instance of `fetch` provided as a first
* argument. Optionally resources.txt and config can be provided.
*/
public static fromLists<T extends typeof FilterEngine>(
this: T,
fetch: Fetch,
urls: string[],
config: Partial<Config> = {},
caching?: Caching,
): Promise<InstanceType<T>> {
return this.fromCached(() => {
const listsPromises = fetchLists(fetch, urls);
const resourcesPromise = fetchResources(fetch);
return Promise.all([listsPromises, resourcesPromise]).then(([lists, resources]) => {
const engine = this.parse(lists.join('\n'), config);
if (resources !== undefined) {
engine.updateResources(resources, '' + resources.length);
}
return engine as InstanceType<T>;
});
}, caching);
}
/**
* Initialize blocker of *ads only*.
*
* Attempt to initialize a blocking engine using a pre-built version served
* from Ghostery's CDN. If this fails (e.g.: if no pre-built engine is available
* for this version of the library), then falls-back to using `fromLists(...)`
* method with the same subscriptions.
*/
public static fromPrebuiltAdsOnly<T extends typeof FilterEngine>(
this: T,
fetchImpl: Fetch = fetch,
caching?: Caching,
): Promise<InstanceType<T>> {
return this.fromLists(fetchImpl, adsLists, {}, caching);
}
/**
* Same as `fromPrebuiltAdsOnly(...)` but also contains rules to block
* tracking (i.e.: using extra lists such as EasyPrivacy and more).
*/
public static fromPrebuiltAdsAndTracking<T extends typeof FilterEngine>(
this: T,
fetchImpl: Fetch = fetch,
caching?: Caching,
): Promise<InstanceType<T>> {
return this.fromLists(fetchImpl, adsAndTrackingLists, {}, caching);
}
/**
* Same as `fromPrebuiltAdsAndTracking(...)` but also contains annoyances
* rules to block things like cookie notices.
*/
public static fromPrebuiltFull<T extends typeof FilterEngine>(
this: T,
fetchImpl: Fetch = fetch,
caching?: Caching,
): Promise<InstanceType<T>> {
return this.fromLists(fetchImpl, fullLists, {}, caching);
}
public static fromTrackerDB<T extends typeof FilterEngine>(
this: T,
rawJsonDump: any,
options: Partial<Config> = {},
): InstanceType<T> {
const config = new Config(options);
const metadata = new Metadata(rawJsonDump);
const filters: string[] = [];
for (const pattern of metadata.getPatterns()) {
filters.push(...pattern.filters);
}
const engine = this.parse(filters.join('\n'), config);
engine.metadata = metadata;
return engine as InstanceType<T>;
}
/**
* Merges compatible engines into one.
*
* This action references objects from the source engines, including
* network filters, cosmetic filters, preprocessors, metadata, and lists.
* These objects are not deep-copied, so modifying them directly can have
* unintended side effects.
* However, resources are deep-copied from the first engine.
*
* Optionally, you can specify a second parameter to skip merging specific resources.
* If resource merging is skipped, the resulting engine will be assigned empty resources.
*/
public static merge<T extends typeof FilterEngine>(
this: T,
engines: InstanceType<T>[],
{
skipResources = false,
}: {
skipResources?: boolean;
} = {},
): InstanceType<T> {
if (!engines || engines.length < 2) {
throw new Error('merging engines requires at least two engines');
}
const config = engines[0].config;
const lists = new Map();
const networkFilters: Map<number, NetworkFilter> = new Map();
const cosmeticFilters: Map<number, CosmeticFilter> = new Map();
const preprocessors: Preprocessor[] = [];
const metadata: {
organizations: Record<string, IOrganization>;
categories: Record<string, ICategory>;
patterns: Record<string, IPattern>;
} = {
organizations: {},
categories: {},
patterns: {},
};
type ConfigKey = keyof {
[Key in keyof Config as Config[Key] extends boolean ? Key : never]: Config[Key];
};
const compatibleConfigKeys: ConfigKey[] = [];
const configKeysMustMatch: ConfigKey[] = (Object.keys(config) as (keyof Config)[]).filter(
function (key): key is ConfigKey {
return (
typeof config[key] === 'boolean' && !compatibleConfigKeys.includes(key as ConfigKey)
);
},
);
for (const engine of engines) {
// Validate the config
for (const configKey of configKeysMustMatch) {
if (config[configKey] !== engine.config[configKey]) {
throw new Error(`config "${configKey}" of all merged engines must be the same`);
}
}
const filters = engine.getFilters();
for (const networkFilter of filters.networkFilters) {
networkFilters.set(networkFilter.getId(), networkFilter);
}
for (const cosmeticFilter of filters.cosmeticFilters) {
cosmeticFilters.set(cosmeticFilter.getId(), cosmeticFilter);
}
for (const preprocessor of engine.preprocessors.preprocessors) {
preprocessors.push(preprocessor);
}
for (const [key, value] of engine.lists) {
if (lists.has(key)) {
continue;
}
lists.set(key, value);
}
if (engine.metadata !== undefined) {
for (const organization of engine.metadata.organizations.getValues()) {
if (metadata.organizations[organization.key] === undefined) {
metadata.organizations[organization.key] = organization;
}
}
for (const category of engine.metadata.categories.getValues()) {
if (metadata.categories[category.key] === undefined) {
metadata.categories[category.key] = category;
}
}
for (const pattern of engine.metadata.patterns.getValues()) {
if (metadata.patterns[pattern.key] === undefined) {
metadata.patterns[pattern.key] = pattern;
}
}
}
}
const engine = new this({
networkFilters: Array.from(networkFilters.values()),
cosmeticFilters: Array.from(cosmeticFilters.values()),
preprocessors,
lists,
config,
}) as InstanceType<T>;
if (
Object.keys(metadata.categories).length +
Object.keys(metadata.organizations).length +
Object.keys(metadata.patterns).length !==
0
) {
engine.metadata = new Metadata(metadata);
}
if (skipResources !== true) {
for (const engine of engines.slice(1)) {
if (engine.resources.checksum !== engines[0].resources.checksum) {
throw new Error(
`resource checksum of all merged engines must match with the first one: "${engines[0].resources.checksum}" but got: "${engine.resources.checksum}"`,
);
}
}
engine.resources = Resources.copy(engines[0].resources);
}
return engine;
}
public static parse<T extends FilterEngine>(
this: new (...args: any[]) => T,
filters: string,
options: Partial<Config> = {},
): T {
const config = new Config(options);
return new this({
...parseFilters(filters, config),
config,
});
}
public static deserialize<T extends FilterEngine>(
this: new (...args: any[]) => T,
serialized: Uint8Array,
): T {
const buffer = StaticDataView.fromUint8Array(serialized, {
enableCompression: false,
});
// Before starting deserialization, we make sure that the version of the
// serialized engine is the same as the current source code. If not, we
// start fresh and create a new engine from the lists.
const serializedEngineVersion = buffer.getUint16();
if (ENGINE_VERSION !== serializedEngineVersion) {
throw new Error(
`serialized engine version mismatch, expected ${ENGINE_VERSION} but got ${serializedEngineVersion}`,
);
}
// Create a new engine with same options
const config = Config.deserialize(buffer);
// Optionally turn compression ON
if (config.enableCompression) {
buffer.enableCompression();
}
// Also make sure that the built-in checksum is correct. This allows to
// detect data corruption and start fresh if the serialized version was
// altered.
if (config.integrityCheck) {
const currentPos = buffer.pos;
buffer.pos = serialized.length - 4;
const checksum = buffer.checksum();
const expected = buffer.getUint32();
if (checksum !== expected) {
throw new Error(
`serialized engine checksum mismatch, expected ${expected} but got ${checksum}`,
);
}
buffer.pos = currentPos;
}
const engine = new this({ config });
// Deserialize resources
engine.resources = Resources.deserialize(buffer);
// Deserialize lists
const lists = new Map();
const numberOfLists = buffer.getUint16();
for (let i = 0; i < numberOfLists; i += 1) {
lists.set(buffer.getASCII(), buffer.getASCII());
}
engine.lists = lists;
// Deserialize preprocessors
engine.preprocessors = PreprocessorBucket.deserialize(buffer);
// Deserialize buckets
engine.importants = NetworkFilterBucket.deserialize(buffer, config);
engine.redirects = NetworkFilterBucket.deserialize(buffer, config);
engine.filters = NetworkFilterBucket.deserialize(buffer, config);
engine.exceptions = NetworkFilterBucket.deserialize(buffer, config);
engine.csp = NetworkFilterBucket.deserialize(buffer, config);
engine.cosmetics = CosmeticFilterBucket.deserialize(buffer, config);
engine.hideExceptions = NetworkFilterBucket.deserialize(buffer, config);
engine.htmlFilters = HTMLBucket.deserialize(buffer, config);
// Optionally deserialize metadata
const hasMetadata = buffer.getBool();
if (hasMetadata) {
engine.metadata = Metadata.deserialize(buffer);
}
buffer.seekZero();
return engine;
}
public lists: Map<string, string>;
public preprocessors: PreprocessorBucket;
public csp: NetworkFilterBucket;
public hideExceptions: NetworkFilterBucket;
public exceptions: NetworkFilterBucket;
public importants: NetworkFilterBucket;
public redirects: NetworkFilterBucket;
public filters: NetworkFilterBucket;
public cosmetics: CosmeticFilterBucket;
public htmlFilters: HTMLBucket;
public metadata: Metadata | undefined;
public resources: Resources;
public readonly config: Config;
constructor({
// Optionally initialize the engine with filters
cosmeticFilters = [],
networkFilters = [],
preprocessors = [],
config = new Config(),
lists = new Map(),
}: {
cosmeticFilters?: CosmeticFilter[];
networkFilters?: NetworkFilter[];
preprocessors?: Preprocessor[];
lists?: Map<string, string>;
config?: Partial<Config>;
} = {}) {
super(); // init super-class EventEmitter
this.config = new Config(config);
// Subscription management: disabled by default
this.lists = lists;
// Preprocessors
this.preprocessors = new PreprocessorBucket({});
// $csp=
this.csp = new NetworkFilterBucket({ config: this.config });
// $elemhide
// $generichide
// $specifichide
this.hideExceptions = new NetworkFilterBucket({ config: this.config });
// @@filter
this.exceptions = new NetworkFilterBucket({ config: this.config });
// $important
this.importants = new NetworkFilterBucket({ config: this.config });
// $redirect
this.redirects = new NetworkFilterBucket({ config: this.config });
// All other filters
this.filters = new NetworkFilterBucket({ config: this.config });
// Cosmetic filters
this.cosmetics = new CosmeticFilterBucket({ config: this.config });
// HTML filters
this.htmlFilters = new HTMLBucket({ config: this.config });
// Injections
this.resources = new Resources();
if (networkFilters.length !== 0 || cosmeticFilters.length !== 0) {
this.update({
newCosmeticFilters: cosmeticFilters,
newNetworkFilters: networkFilters,
newPreprocessors: preprocessors,
});
}
}
private isFilterExcluded(filter: IFilter): boolean {
return this.preprocessors.isFilterExcluded(filter);
}
public updateEnv(env: Env) {
this.preprocessors.updateEnv(env);
}
/**
* Estimate the number of bytes needed to serialize this instance of
* `FiltersEngine` using the `serialize(...)` method. It is used internally
* by `serialize(...)` to allocate a buffer of the right size and you should
* not have to call it yourself most of the time.
*
* There are cases where we cannot estimate statically the exact size of the
* resulting buffer (due to alignement which needs to be performed); this
* method will return a safe estimate which will always be at least equal to
* the real number of bytes needed, or bigger (usually of a few bytes only:
* ~20 bytes is to be expected).
*/
public getSerializedSize(): number {
let estimatedSize: number =
sizeOfByte() + // engine version
this.config.getSerializedSize() +
this.resources.getSerializedSize() +
this.preprocessors.getSerializedSize() +
this.filters.getSerializedSize() +
this.exceptions.getSerializedSize() +
this.importants.getSerializedSize() +
this.redirects.getSerializedSize() +
this.csp.getSerializedSize() +
this.cosmetics.getSerializedSize() +
this.hideExceptions.getSerializedSize() +
this.htmlFilters.getSerializedSize() +
4; // checksum
// Estimate size of `this.lists` which stores information of checksum for each list.
for (const [name, checksum] of this.lists) {
estimatedSize += sizeOfASCII(name) + sizeOfASCII(checksum);
}
estimatedSize += sizeOfBool();
if (this.metadata !== undefined) {
estimatedSize += this.metadata.getSerializedSize();
}
return estimatedSize;
}
/**
* Creates a binary representation of the full engine. It can be stored
* on-disk for faster loading of the adblocker. The `deserialize` static
* method of Engine can be used to restore the engine.
*/
public serialize(array?: Uint8Array): Uint8Array {
const buffer = StaticDataView.fromUint8Array(
array || new Uint8Array(this.getSerializedSize()),
this.config,
);
buffer.pushUint16(ENGINE_VERSION);
// Config
this.config.serialize(buffer);
// Resources (js, resources)
this.resources.serialize(buffer);
// Serialize the state of lists (names and checksums)
buffer.pushUint16(this.lists.size);
for (const [name, value] of Array.from(this.lists.entries()).sort()) {
buffer.pushASCII(name);
buffer.pushASCII(value);
}
// Preprocessors
this.preprocessors.serialize(buffer);
// Filters buckets
this.importants.serialize(buffer);
this.redirects.serialize(buffer);
this.filters.serialize(buffer);
this.exceptions.serialize(buffer);
this.csp.serialize(buffer);
this.cosmetics.serialize(buffer);
this.hideExceptions.serialize(buffer);
this.htmlFilters.serialize(buffer);
// Optionally serialize metadata
buffer.pushBool(this.metadata !== undefined);
if (this.metadata !== undefined) {
this.metadata.serialize(buffer);
}
// Optionally append a checksum at the end
if (this.config.integrityCheck) {
buffer.pushUint32(buffer.checksum());
}
return buffer.subarray();
}
/**
* Update engine with new filters or resources.
*/
public loadedLists(): string[] {
return Array.from(this.lists.keys());
}
public hasList(name: string, checksum: string): boolean {
return this.lists.has(name) && this.lists.get(name) === checksum;
}
/**
* Update engine with `resources.txt` content.
*/
public updateResources(data: string, checksum: string): boolean {
if (this.resources.checksum === checksum) {
return false;
}
this.resources = Resources.parse(data, { checksum });
return true;
}
public getFilters(): { networkFilters: NetworkFilter[]; cosmeticFilters: CosmeticFilter[] } {
const cosmeticFilters: CosmeticFilter[] = this.cosmetics.getFilters();
const networkFilters: NetworkFilter[] = [
...this.filters.getFilters(),
...this.exceptions.getFilters(),
...this.importants.getFilters(),
...this.redirects.getFilters(),
...this.csp.getFilters(),
...this.hideExceptions.getFilters(),
];
for (const filter of this.htmlFilters.getFilters()) {
if (filter.isNetworkFilter()) {
networkFilters.push(filter);
} else if (filter.isCosmeticFilter()) {
cosmeticFilters.push(filter);
}
}
return {
cosmeticFilters,
networkFilters,
};
}
/**
* Update engine with new filters as well as optionally removed filters.
*/
public update(
{
newNetworkFilters = [],
newCosmeticFilters = [],
newPreprocessors = [],
removedCosmeticFilters = [],
removedNetworkFilters = [],
removedPreprocessors = [],
}: Partial<IListDiff>,
env: Env = new Env(),
): boolean {
let updated: boolean = false;
// Update preprocessors
if (
this.config.loadPreprocessors &&
(newPreprocessors.length !== 0 || removedPreprocessors.length !== 0)
) {
updated = true;
this.preprocessors.update(
{
added: newPreprocessors,
removed: removedPreprocessors,
},
env,
);
}
const htmlFilters: (CosmeticFilter | NetworkFilter)[] = [];
// Update cosmetic filters
if (
this.config.loadCosmeticFilters &&
(newCosmeticFilters.length !== 0 || removedCosmeticFilters.length !== 0)
) {
updated = true;
const cosmeticFitlers: CosmeticFilter[] = [];
for (const filter of newCosmeticFilters) {
if (filter.isHtmlFiltering()) {
htmlFilters.push(filter);
} else {
cosmeticFitlers.push(filter);
}
}
this.cosmetics.update(
cosmeticFitlers,
removedCosmeticFilters.length === 0 ? undefined : new Set(removedCosmeticFilters),
this.config,
);
}
// Update network filters
if (
this.config.loadNetworkFilters &&
(newNetworkFilters.length !== 0 || removedNetworkFilters.length !== 0)
) {
updated = true;
const filters: NetworkFilter[] = [];
const csp: NetworkFilter[] = [];
const exceptions: NetworkFilter[] = [];
const importants: NetworkFilter[] = [];
const redirects: NetworkFilter[] = [];
const hideExceptions: NetworkFilter[] = [];
for (const filter of newNetworkFilters) {
// NOTE: it's important to check for $generichide, $elemhide,
// $specifichide and $csp before exceptions and important as we store
// all of them in the same filter bucket. The check for exceptions is
// done at match-time directly.
if (filter.isCSP()) {
csp.push(filter);
} else if (filter.isHtmlFilteringRule()) {
htmlFilters.push(filter);
} else if (filter.isGenericHide() || filter.isSpecificHide()) {
hideExceptions.push(filter);
} else if (filter.isException()) {
exceptions.push(filter);
} else if (filter.isImportant()) {
importants.push(filter);
} else if (filter.isRedirect()) {
redirects.push(filter);
} else {
filters.push(filter);
}
}
const removedNetworkFiltersSet: Set<number> | undefined =
removedNetworkFilters.length === 0 ? undefined : new Set(removedNetworkFilters);
// Update buckets in-place
this.importants.update(importants, removedNetworkFiltersSet);
this.redirects.update(redirects, removedNetworkFiltersSet);
this.filters.update(filters, removedNetworkFiltersSet);
if (this.config.loadExceptionFilters === true) {
this.exceptions.update(exceptions, removedNetworkFiltersSet);
}
if (this.config.loadCSPFilters === true) {
this.csp.update(csp, removedNetworkFiltersSet);
}
this.hideExceptions.update(hideExceptions, removedNetworkFiltersSet);
}
if (
this.config.enableHtmlFiltering &&
(htmlFilters.length !== 0 ||
removedNetworkFilters.length !== 0 ||
removedCosmeticFilters.length !== 0)
) {
const removeFilters = new Set([...removedNetworkFilters, ...removedCosmeticFilters]);
this.htmlFilters.update(htmlFilters, removeFilters);
}
return updated;
}
public updateFromDiff({ added, removed, preprocessors }: IPartialRawDiff, env?: Env): boolean {
const newCosmeticFilters: CosmeticFilter[] = [];
const newNetworkFilters: NetworkFilter[] = [];
const newPreprocessors: Preprocessor[] = [];
const removedCosmeticFilters: CosmeticFilter[] = [];
const removedNetworkFilters: NetworkFilter[] = [];
const removedPreprocessors: Preprocessor[] = [];
if (removed !== undefined && removed.length !== 0) {
const { networkFilters, cosmeticFilters } = parseFilters(removed.join('\n'), this.config);
Array.prototype.push.apply(removedCosmeticFilters, cosmeticFilters);
Array.prototype.push.apply(removedNetworkFilters, networkFilters);
}
if (added !== undefined && added.length !== 0) {
const { networkFilters, cosmeticFilters } = parseFilters(added.join('\n'), this.config);
Array.prototype.push.apply(newCosmeticFilters, cosmeticFilters);
Array.prototype.push.apply(newNetworkFilters, networkFilters);
}
if (preprocessors !== undefined) {
for (const [condition, details] of Object.entries(preprocessors)) {
if (details.removed !== undefined && details.removed.length !== 0) {
const { networkFilters, cosmeticFilters } = parseFilters(
details.removed.join('\n'),
this.config,
);
const filterIDs = new Set<number>(
([] as number[])
.concat(cosmeticFilters.map((filter) => filter.getId()))
.concat(networkFilters.map((filter) => filter.getId())),
);
removedPreprocessors.push(
new Preprocessor({
condition,
filterIDs,
}),
);
}
if (details.added !== undefined && details.added.length !== 0) {
const { networkFilters, cosmeticFilters } = parseFilters(
details.added.join('\n'),
this.config,
);
const filterIDs = new Set<number>(
([] as number[])
.concat(cosmeticFilters.map((filter) => filter.getId()))
.concat(networkFilters.map((filter) => filter.getId())),
);
newPreprocessors.push(
new Preprocessor({
condition,
filterIDs,
}),
);
}
}
}
return this.update(
{
newCosmeticFilters,
newNetworkFilters,
newPreprocessors,
removedCosmeticFilters: removedCosmeticFilters.map((f) => f.getId()),
removedNetworkFilters: removedNetworkFilters.map((f) => f.getId()),
removedPreprocessors,
},
env,
);
}
/**
* Return a list of HTML filtering rules.
*/
public getHtmlFilters(request: Request): HTMLSelector[] {
const htmlSelectors: HTMLSelector[] = [];
if (this.config.enableHtmlFiltering === false) {
return htmlSelectors;
}
const { networkFilters, exceptions, cosmeticFilters, unhides } =
this.htmlFilters.getHTMLFilters(request, this.isFilterExcluded.bind(this));
if (cosmeticFilters.length !== 0) {
const unhideMap = new Map(unhides.map((unhide) => [unhide.getSelector(), unhide]));
for (const filter of cosmeticFilters) {
const extended = filter.getExtendedSelector();
if (extended === undefined) {
continue;
}
const unhide = unhideMap.get(filter.getSelector());
if (unhide === undefined) {
htmlSelectors.push(extended);
}
this.emit(
'filter-matched',
{ filter, exception: unhide },
{
request,
filterType: FilterType.COSMETIC,
},
);
}
}
if (networkFilters.length !== 0) {
const exceptionsMap = new Map();
let replaceDisabledException;
for (const exception of exceptions) {
const optionValue = exception.optionValue;
if (optionValue === '') {
replaceDisabledException = exception;
break;
}
exceptionsMap.set(optionValue, exception);
}
for (const filter of networkFilters) {
const modifier = filter.getHtmlModifier();
if (modifier === null) {
continue;
}
const exception = replaceDisabledException || exceptionsMap.get(filter.optionValue);
this.emit(
'filter-matched',
{ filter, exception },
{
request,
filterType: FilterType.NETWORK,
},
);
if (exception === undefined) {
htmlSelectors.push(['replace', modifier]);
}
}
}
if (htmlSelectors.length !== 0) {
this.emit('html-filtered', htmlSelectors, request.url);
}
return htmlSelectors;
}
/**
* Given `hostname` and `domain` of a page (or frame), return the list of
* styles and scripts to inject in the page.
*/
public getCosmeticsFilters({
// Page information
url,
hostname,
domain,
// DOM information
classes,
hrefs,
ids,
// Allows to specify which rules to return
getBaseRules = true,
getInjectionRules = true,
getExtendedRules = true,
getRulesFromDOM = true,
getRulesFromHostname = true,
hidingStyle,
callerContext,
}: {
url: string;
hostname: string;
domain: string | null | undefined;
classes?: string[] | undefined;