@whotracksme/reporting 5.1.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (179) hide show
  1. package/.editorconfig +12 -0
  2. package/.eslintignore +3 -0
  3. package/.eslintrc +16 -0
  4. package/.github/workflows/release.yml +34 -0
  5. package/.github/workflows/tests.yml +20 -0
  6. package/.prettierrc +9 -0
  7. package/.tool-versions +1 -0
  8. package/CHANGELOG.md +795 -0
  9. package/LICENSE +373 -0
  10. package/README.md +1 -0
  11. package/communication/README.md +11 -0
  12. package/communication/example/index.js +43 -0
  13. package/communication/example/manifest.json +16 -0
  14. package/communication/karma.conf.cjs +19 -0
  15. package/communication/package.json +52 -0
  16. package/communication/rollup.common-config.cjs +17 -0
  17. package/communication/rollup.config.cjs +10 -0
  18. package/communication/src/encoding.js +54 -0
  19. package/communication/src/errors.js +84 -0
  20. package/communication/src/index.js +76 -0
  21. package/communication/src/logger.js +41 -0
  22. package/communication/src/padding.js +60 -0
  23. package/communication/src/proxied-http.js +231 -0
  24. package/communication/src/server-public-key-accessor.js +123 -0
  25. package/communication/src/timestamps.js +37 -0
  26. package/communication/src/trusted-clock.js +58 -0
  27. package/communication/src/utils.js +28 -0
  28. package/communication/src/zlib.js +17 -0
  29. package/communication/test/helpers/in-memory-database.js +43 -0
  30. package/communication/test/server-public-key-accessor.spec.js +135 -0
  31. package/communication/test/utils.spec.js +43 -0
  32. package/package.json +50 -0
  33. package/reporting/TODO.md +2 -0
  34. package/reporting/assets/request/bloom_filter.dat +0 -0
  35. package/reporting/assets/request/update.json +1 -0
  36. package/reporting/example/content.js +11 -0
  37. package/reporting/example/index.js +132 -0
  38. package/reporting/example/inspector/index.html +17 -0
  39. package/reporting/example/inspector/index.js +27 -0
  40. package/reporting/example/manifests/chromium.json +37 -0
  41. package/reporting/example/manifests/firefox.json +35 -0
  42. package/reporting/example/rules.json +62 -0
  43. package/reporting/example/setup.js +1 -0
  44. package/reporting/example/tests/test1.html +10 -0
  45. package/reporting/karma.conf.cjs +22 -0
  46. package/reporting/package.json +63 -0
  47. package/reporting/rollup.common-config.cjs +19 -0
  48. package/reporting/rollup.config.cjs +20 -0
  49. package/reporting/src/LRU.js +149 -0
  50. package/reporting/src/activity-estimator.js +298 -0
  51. package/reporting/src/alive-check.js +139 -0
  52. package/reporting/src/alive-message-generator.js +142 -0
  53. package/reporting/src/bloom-filters.js +345 -0
  54. package/reporting/src/country-provider.js +295 -0
  55. package/reporting/src/cyrb53.js +32 -0
  56. package/reporting/src/digest.js +33 -0
  57. package/reporting/src/doublefetch-page-handler.js +548 -0
  58. package/reporting/src/duplicate-detector.js +233 -0
  59. package/reporting/src/errors.js +84 -0
  60. package/reporting/src/event-listener-queue.js +121 -0
  61. package/reporting/src/fixed-size-cache.js +63 -0
  62. package/reporting/src/hash-detector.js +1526 -0
  63. package/reporting/src/html-parser.js +17 -0
  64. package/reporting/src/http.js +265 -0
  65. package/reporting/src/index.js +14 -0
  66. package/reporting/src/job-scheduler.js +912 -0
  67. package/reporting/src/logger.js +121 -0
  68. package/reporting/src/md5.js +334 -0
  69. package/reporting/src/message-sender.js +51 -0
  70. package/reporting/src/network.js +140 -0
  71. package/reporting/src/new-page-approver.js +119 -0
  72. package/reporting/src/page-aggregator.js +185 -0
  73. package/reporting/src/page-quorum-check-handler.js +200 -0
  74. package/reporting/src/page-structure.js +136 -0
  75. package/reporting/src/pagedb.js +671 -0
  76. package/reporting/src/pages.js +1798 -0
  77. package/reporting/src/patterns-updater.js +310 -0
  78. package/reporting/src/patterns.js +416 -0
  79. package/reporting/src/persisted-bitarray.js +265 -0
  80. package/reporting/src/persisted-hashes.js +189 -0
  81. package/reporting/src/quorum-checker.js +327 -0
  82. package/reporting/src/random.js +39 -0
  83. package/reporting/src/reporting.js +290 -0
  84. package/reporting/src/request/config.js +116 -0
  85. package/reporting/src/request/content-script.js +77 -0
  86. package/reporting/src/request/database.js +147 -0
  87. package/reporting/src/request/dnt.js +24 -0
  88. package/reporting/src/request/hash.js +101 -0
  89. package/reporting/src/request/index.js +874 -0
  90. package/reporting/src/request/page-telemetry.js +42 -0
  91. package/reporting/src/request/qs-whitelist2.js +247 -0
  92. package/reporting/src/request/resources/prob.js +601 -0
  93. package/reporting/src/request/steps/block-rules.js +40 -0
  94. package/reporting/src/request/steps/check-context.js +37 -0
  95. package/reporting/src/request/steps/cookie-context.js +209 -0
  96. package/reporting/src/request/steps/oauth-detector.js +101 -0
  97. package/reporting/src/request/steps/page-logger.js +177 -0
  98. package/reporting/src/request/steps/redirect-tagger.js +75 -0
  99. package/reporting/src/request/steps/subdomain-check.js +34 -0
  100. package/reporting/src/request/steps/token-checker/index.js +189 -0
  101. package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
  102. package/reporting/src/request/steps/token-examiner.js +182 -0
  103. package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
  104. package/reporting/src/request/steps/token-telemetry/index.js +234 -0
  105. package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
  106. package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
  107. package/reporting/src/request/time.js +73 -0
  108. package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
  109. package/reporting/src/request/utils/chrome-storage-map.js +282 -0
  110. package/reporting/src/request/utils/subject.js +37 -0
  111. package/reporting/src/request/utils/temp-set.js +56 -0
  112. package/reporting/src/request/utils/tlds.js +55 -0
  113. package/reporting/src/request/utils.js +41 -0
  114. package/reporting/src/request-reporter.js +111 -0
  115. package/reporting/src/sanitizer.js +519 -0
  116. package/reporting/src/search-extractor.js +338 -0
  117. package/reporting/src/self-check.js +105 -0
  118. package/reporting/src/seq-executor.js +43 -0
  119. package/reporting/src/session-storage.js +399 -0
  120. package/reporting/src/url-analyzer.js +161 -0
  121. package/reporting/src/url-cleaner.js +60 -0
  122. package/reporting/src/utils/pacemaker.js +279 -0
  123. package/reporting/src/utils/url.js +114 -0
  124. package/reporting/src/utils.js +345 -0
  125. package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
  126. package/reporting/src/webrequest-pipeline/index.js +287 -0
  127. package/reporting/src/webrequest-pipeline/logger.js +14 -0
  128. package/reporting/src/webrequest-pipeline/page-store.js +282 -0
  129. package/reporting/src/webrequest-pipeline/page.js +140 -0
  130. package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
  131. package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
  132. package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
  133. package/reporting/test/README.md +2 -0
  134. package/reporting/test/activity-estimator.spec.js +356 -0
  135. package/reporting/test/alive-check.spec.js +156 -0
  136. package/reporting/test/alive-message-generator.spec.js +186 -0
  137. package/reporting/test/country-provider.spec.js +263 -0
  138. package/reporting/test/doublefetch-page-handler.spec.js +818 -0
  139. package/reporting/test/duplicate-detector.spec.js +689 -0
  140. package/reporting/test/helpers/dom-parsers.js +41 -0
  141. package/reporting/test/helpers/fake-session-storage.js +69 -0
  142. package/reporting/test/helpers/fast-check-utils.js +28 -0
  143. package/reporting/test/helpers/fetch-mock.js +57 -0
  144. package/reporting/test/helpers/in-memory-database.js +14 -0
  145. package/reporting/test/helpers/memory-map.js +72 -0
  146. package/reporting/test/helpers/pagedb-mocks.js +41 -0
  147. package/reporting/test/index.js +47 -0
  148. package/reporting/test/job-scheduler.spec.js +691 -0
  149. package/reporting/test/network.spec.js +72 -0
  150. package/reporting/test/page-aggregator.spec.js +104 -0
  151. package/reporting/test/page-quorum-check-handler.spec.js +86 -0
  152. package/reporting/test/pagedb.spec.js +250 -0
  153. package/reporting/test/pages.spec.js +693 -0
  154. package/reporting/test/patterns-updater.spec.js +446 -0
  155. package/reporting/test/patterns.spec.js +751 -0
  156. package/reporting/test/persisted-hashes.spec.js +169 -0
  157. package/reporting/test/reporting.spec.js +205 -0
  158. package/reporting/test/request/database.spec.js +59 -0
  159. package/reporting/test/request/hash.spec.js +108 -0
  160. package/reporting/test/request/index.spec.js +378 -0
  161. package/reporting/test/request/qs-whitelist2.spec.js +189 -0
  162. package/reporting/test/request/steps/check-context.spec.js +51 -0
  163. package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
  164. package/reporting/test/request/test-pages.js +1246 -0
  165. package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
  166. package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
  167. package/reporting/test/request/utils/tldts.spec.js +62 -0
  168. package/reporting/test/request/utils.spec.js +168 -0
  169. package/reporting/test/sanitizer.spec.js +1087 -0
  170. package/reporting/test/search-extractor.spec.js +590 -0
  171. package/reporting/test/seq-executor.spec.js +158 -0
  172. package/reporting/test/session-storage.spec.js +400 -0
  173. package/reporting/test/setup.js +24 -0
  174. package/reporting/test/url-analyzer.spec.js +451 -0
  175. package/reporting/test/url-cleaner.spec.js +102 -0
  176. package/reporting/test/utils/pacemaker.spec.js +222 -0
  177. package/reporting/test/utils/url.spec.js +660 -0
  178. package/reporting/test/utils.spec.js +299 -0
  179. package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
@@ -0,0 +1,282 @@
1
+ // TODO @chrmod: share with ghostery-extension
2
+ /**
3
+ * Ghostery Browser Extension
4
+ * https://www.ghostery.com/
5
+ *
6
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
7
+ *
8
+ * This Source Code Form is subject to the terms of the Mozilla Public
9
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
10
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
11
+ */
12
+
13
+ // This global provides an API like an ES Map but will sync
14
+ // with local storage from time to time. That is done to prevent
15
+ // the loss of all stats when the browser terminates the execution
16
+ // context (background script or service worker).
17
+ export default class ChromeStorageMap {
18
+ constructor({
19
+ sessionApi = typeof chrome !== 'undefined' && chrome?.storage?.session,
20
+ storageKey,
21
+ softFlushIntervalInMs = 200,
22
+ hardFlushIntervalInMs = 1000,
23
+ ttlInMs = 7 * 24 * 60 * 60 * 1000 /* 1 week */,
24
+ maxEntries = 5000,
25
+ }) {
26
+ this.sessionApi = sessionApi;
27
+ this._inMemoryMap = new Map();
28
+
29
+ if (!storageKey) {
30
+ throw new Error('Missing storage key');
31
+ }
32
+ this.storageKey = storageKey;
33
+ this._initialSyncComplete = false;
34
+ this.maxEntries = maxEntries;
35
+
36
+ // Make sure old entries that were not cleaned up are eventually
37
+ // removed. Otherwise, we could exceed the local storage quota.
38
+ // Plus, when the maps get big, serializing and deserializing
39
+ // may become expensive. If the actively triggered clean up works,
40
+ // there should be no need to make this expiration too aggressive.
41
+ this.ttlInMs = ttlInMs;
42
+ this._ttlMap = new Map();
43
+
44
+ // Flush handling logic: the difference between both limits is that
45
+ // the soft limit does not guarantee that a flush will eventually
46
+ // be performed. After each write operation, it will reset the soft
47
+ // timeout and then flush. Thus, if you keep writing, it will never
48
+ // flush. The hard limit, on the other hand, forces that data gets
49
+ // persisted, but could result in ill-timed write operations.
50
+ //
51
+ // If there are bursts of operations, ideally you want to flush
52
+ // at the end of the burst. The soft limit will result in that,
53
+ // while the hard limit mitigates the risk that the script
54
+ // gets killed before the data gets persisted.
55
+ //
56
+ // Rule of thumbs:
57
+ // * The soft limit should be lower then the hard limit
58
+ // * The hard limit should not be set too high. Remember, it is
59
+ // the protection against the browser unpredictably killing
60
+ // the execution.
61
+ this.softFlushIntervalInMs = softFlushIntervalInMs;
62
+ this.hardFlushIntervalInMs = hardFlushIntervalInMs;
63
+ this._scheduledFlush = null;
64
+ this._lastFlush = Date.now();
65
+ this._dirty = false;
66
+
67
+ // Assumption: there should be enough time during startup to load
68
+ // the persisted map. Otherwise, the state will be inconsistent
69
+ // whenever the script is loaded (it will eventually become consistent,
70
+ // but that will not help if the browser kills it quickly).
71
+ //
72
+ // (If that assumption does not hold, _warnIfOutOfSync will detect
73
+ // and log it. A potential improvement could be to treat the
74
+ // in-memory map as the source of truth in that scenario.)
75
+ this.isReady = new Promise((resolve, reject) => {
76
+ this.sessionApi.get([this.storageKey], (result) => {
77
+ if (chrome.runtime.lastError) {
78
+ reject(chrome.runtime.lastError);
79
+ } else {
80
+ const { entries = {}, ttl = {} } = result[this.storageKey] || {};
81
+ this._inMemoryMap = new Map(Object.entries(entries));
82
+ this._ttlMap = new Map(Object.entries(ttl));
83
+ this._initialSyncComplete = true;
84
+ this._expireOldEntries();
85
+ resolve();
86
+ }
87
+ });
88
+ });
89
+ }
90
+
91
+ get(_key) {
92
+ this._warnIfOutOfSync();
93
+ const key = this.normalizeKey(_key);
94
+ if (this._expireOldEntry(key)) {
95
+ return;
96
+ }
97
+ return this._inMemoryMap.get(key);
98
+ }
99
+
100
+ entries() {
101
+ this._warnIfOutOfSync();
102
+ this._expireOldEntries();
103
+ return this._inMemoryMap.entries();
104
+ }
105
+
106
+ values() {
107
+ this._warnIfOutOfSync();
108
+ this._expireOldEntries();
109
+ return Object.values(this._inMemoryMap);
110
+ }
111
+
112
+ has(_key) {
113
+ this._warnIfOutOfSync();
114
+ const key = this.normalizeKey(_key);
115
+ if (this._expireOldEntry(key)) {
116
+ return;
117
+ }
118
+ return this._inMemoryMap.has(key);
119
+ }
120
+
121
+ forEach(callback) {
122
+ this._warnIfOutOfSync();
123
+ this._expireOldEntries();
124
+ this._inMemoryMap.forEach(callback);
125
+ }
126
+
127
+ set(_key, value) {
128
+ this._warnIfOutOfSync();
129
+
130
+ // This should never trigger. Yet if the maps run full (perhaps
131
+ // as a side-effect of a bug), better reset then continuing with
132
+ // these huge maps.
133
+ if (
134
+ this._inMemoryMap.size >= this.maxEntries ||
135
+ this._ttlMap.size >= this.maxEntries
136
+ ) {
137
+ console.warn(
138
+ 'ChromeStorageMap: Maps are running full (maybe you found a bug?). Purging data to prevent performance impacts.',
139
+ );
140
+ this._inMemoryMap.clear();
141
+ this._ttlMap.clear();
142
+ }
143
+ const key = this.normalizeKey(_key);
144
+ this._inMemoryMap.set(key, value);
145
+ this._ttlMap.set(key, Date.now() + this.ttlInMs);
146
+ this._markAsDirty();
147
+ }
148
+
149
+ delete(_key) {
150
+ this._warnIfOutOfSync();
151
+ const key = this.normalizeKey(_key);
152
+ const wasDeleted = this._inMemoryMap.delete(key);
153
+ if (wasDeleted) {
154
+ this._ttlMap.delete(key);
155
+ this._markAsDirty();
156
+ }
157
+ return wasDeleted;
158
+ }
159
+
160
+ clear() {
161
+ this._warnIfOutOfSync();
162
+ this._inMemoryMap.clear();
163
+ this._ttlMap.clear();
164
+
165
+ this._scheduleAction(
166
+ new Promise((resolve, reject) => {
167
+ this.sessionApi.remove(this.storageKey, () => {
168
+ if (chrome.runtime.lastError) {
169
+ reject(chrome.runtime.lastError);
170
+ } else {
171
+ resolve();
172
+ }
173
+ });
174
+ }),
175
+ );
176
+ this._dirty = false;
177
+ }
178
+
179
+ // Normalize numbers as strings to prevent nasty pitfalls
180
+ // (ES6 maps support numbers, but after serializing and
181
+ // deserializing, we end up with strings and cannot find
182
+ // the "number" key)
183
+ normalizeKey(key) {
184
+ if (typeof key === 'number') {
185
+ return key.toString();
186
+ }
187
+ if (typeof key === 'string') {
188
+ return key;
189
+ }
190
+ throw new Error(`Unexpected key type (type: ${typeof key}, value: ${key})`);
191
+ }
192
+
193
+ _warnIfOutOfSync() {
194
+ if (!this._initialSyncComplete) {
195
+ console.warn(
196
+ `AutoSyncingMap "${this.storageKey}": out of sync (loading is too slow...)`,
197
+ );
198
+ }
199
+ }
200
+
201
+ _expireOldEntries() {
202
+ const now = Date.now();
203
+ let count = 0;
204
+ for (const [key, expireAt] of this._ttlMap.entries()) {
205
+ if (now >= expireAt) {
206
+ this._inMemoryMap.delete(key);
207
+ this._ttlMap.delete(key);
208
+ count += 1;
209
+ }
210
+ }
211
+
212
+ if (count > 0) {
213
+ this._markAsDirty();
214
+ }
215
+ return count;
216
+ }
217
+
218
+ _expireOldEntry(key) {
219
+ const now = Date.now();
220
+ const expireAt = this._ttlMap.get(key);
221
+ if (expireAt && now >= expireAt) {
222
+ this.delete(key);
223
+ return true;
224
+ }
225
+ return false;
226
+ }
227
+
228
+ _markAsDirty() {
229
+ const now = Date.now();
230
+ if (!this._dirty) {
231
+ this._lastFlush = now;
232
+ this._dirty = true;
233
+ }
234
+
235
+ const nextForcedFlush = this._lastFlush + this.hardFlushIntervalInMs;
236
+ clearTimeout(this._scheduledFlush);
237
+ if (now >= nextForcedFlush) {
238
+ this._flush();
239
+ this._scheduledFlush = null;
240
+ } else {
241
+ this._scheduledFlush = setTimeout(() => {
242
+ this._flush();
243
+ this._scheduledFlush = null;
244
+ }, Math.min(this.softFlushIntervalInMs, nextForcedFlush - now));
245
+ }
246
+ }
247
+
248
+ _flush() {
249
+ if (!this._dirty) {
250
+ return;
251
+ }
252
+
253
+ this._scheduleAction(
254
+ new Promise((resolve, reject) => {
255
+ if (!this._dirty) {
256
+ resolve();
257
+ return;
258
+ }
259
+
260
+ this._dirty = false;
261
+ const serialized = {
262
+ entries: Object.fromEntries(this._inMemoryMap),
263
+ ttl: Object.fromEntries(this._ttlMap),
264
+ };
265
+ this.sessionApi.set({ [this.storageKey]: serialized }, () => {
266
+ if (chrome.runtime.lastError) {
267
+ reject(chrome.runtime.lastError);
268
+ } else {
269
+ this._lastFlush = Date.now();
270
+ resolve();
271
+ }
272
+ });
273
+ }),
274
+ );
275
+ }
276
+
277
+ _scheduleAction(action) {
278
+ const lastSyncPoint = this.isReady;
279
+ this.isReady = lastSyncPoint.then(action).catch(console.error);
280
+ return this.isReady;
281
+ }
282
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ import logger from '../../logger';
13
+
14
+ export default class Subject {
15
+ constructor() {
16
+ this.listeners = new Set();
17
+ }
18
+
19
+ subscribe(callback) {
20
+ this.listeners.add(callback);
21
+ return {
22
+ unsubscribe: () => {
23
+ this.listeners.delete(callback);
24
+ },
25
+ };
26
+ }
27
+
28
+ pub(message) {
29
+ this.listeners.forEach((listener) => {
30
+ try {
31
+ listener(message);
32
+ } catch (e) {
33
+ logger.error('Subject failed to notify listener', e);
34
+ }
35
+ });
36
+ }
37
+ }
@@ -0,0 +1,56 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ /* eslint func-names: 'off' */
13
+ /* eslint prefer-arrow-callback: 'off' */
14
+
15
+ import pacemaker from '../../utils/pacemaker';
16
+
17
+ /** Set like class whose members are removed after a specific amount of time
18
+ */
19
+ export default class TempSet {
20
+ constructor() {
21
+ this._items = new Set();
22
+ this._timeouts = new Set();
23
+ }
24
+
25
+ contains(item) {
26
+ return this._items.has(item);
27
+ }
28
+
29
+ has(item) {
30
+ return this.contains(item);
31
+ }
32
+
33
+ add(item, ttl) {
34
+ this._items.add(item);
35
+ const timeout = pacemaker.setTimeout(
36
+ function () {
37
+ this.delete(item);
38
+ this._timeouts.delete(timeout);
39
+ }.bind(this),
40
+ ttl || 0,
41
+ );
42
+ this._timeouts.add(timeout);
43
+ }
44
+
45
+ delete(item) {
46
+ this._items.delete(item);
47
+ }
48
+
49
+ clear() {
50
+ for (const t of this._timeouts) {
51
+ pacemaker.clearTimeout(t);
52
+ }
53
+ this._timeouts.clear();
54
+ this._items.clear();
55
+ }
56
+ }
@@ -0,0 +1,55 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ import * as tldts from 'tldts-experimental';
13
+
14
+ const TLDTS_OPTIONS = {
15
+ detectIp: true,
16
+ extractHostname: true,
17
+ mixedInputs: false,
18
+ validateHostname: true,
19
+ validHosts: ['localhost'],
20
+ };
21
+
22
+ function parse(url) {
23
+ const parsed = tldts.parse(url, TLDTS_OPTIONS);
24
+
25
+ if (parsed.isIp) {
26
+ parsed.domain = parsed.hostname;
27
+ }
28
+
29
+ return parsed;
30
+ }
31
+
32
+ const getGeneralDomain = (url) => parse(url).domain;
33
+ const getPublicSuffix = tldts.getPublicSuffix;
34
+ const getDomainWithoutSuffix = tldts.getDomainWithoutSuffix;
35
+ const extractHostname = tldts.getHostname;
36
+
37
+ function sameGeneralDomain(uri1, uri2) {
38
+ if (uri1 === uri2) {
39
+ return true;
40
+ }
41
+
42
+ const domain1 = getGeneralDomain(uri1);
43
+ const domain2 = getGeneralDomain(uri2);
44
+
45
+ return domain1 !== null && domain2 !== null && domain1 === domain2;
46
+ }
47
+
48
+ export {
49
+ extractHostname,
50
+ getDomainWithoutSuffix,
51
+ getGeneralDomain,
52
+ getPublicSuffix,
53
+ parse,
54
+ sameGeneralDomain,
55
+ };
@@ -0,0 +1,41 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ export function truncateDomain(host, depth) {
13
+ const generalDomain = host.domain;
14
+
15
+ if (
16
+ host.isIp ||
17
+ host.hostname === generalDomain ||
18
+ generalDomain === null ||
19
+ generalDomain.length === 0
20
+ ) {
21
+ return host.hostname;
22
+ }
23
+
24
+ const subdomains = host.subdomain.split('.').filter((p) => p.length > 0);
25
+ return `${subdomains
26
+ .slice(Math.max(subdomains.length - depth, 0))
27
+ .join('.')}.${generalDomain}`;
28
+ }
29
+
30
+ export function shuffle(s) {
31
+ const a = s.split('');
32
+ const n = a.length;
33
+
34
+ for (let i = n - 1; i > 0; i -= 1) {
35
+ const j = Math.floor(Math.random() * (i + 1));
36
+ const tmp = a[i];
37
+ a[i] = a[j];
38
+ a[j] = tmp;
39
+ }
40
+ return a.join('');
41
+ }
@@ -0,0 +1,111 @@
1
+ /**
2
+ * WhoTracks.Me
3
+ * https://whotracks.me/
4
+ *
5
+ * Copyright 2017-present Ghostery GmbH. All rights reserved.
6
+ *
7
+ * This Source Code Form is subject to the terms of the Mozilla Public
8
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
9
+ * file, You can obtain one at http://mozilla.org/MPL/2.0
10
+ */
11
+
12
+ import RequestMonitor from './request/index';
13
+ import Config from './request/config';
14
+ import Database from './request/database';
15
+
16
+ export default class RequestReporter {
17
+ constructor(
18
+ settings,
19
+ {
20
+ communication,
21
+ countryProvider,
22
+ trustedClock,
23
+ webRequestPipeline,
24
+ onTrackerInteraction,
25
+ getBrowserInfo,
26
+ isRequestAllowed,
27
+ },
28
+ ) {
29
+ this.settings = settings;
30
+ this.communication = communication;
31
+ this.countryProvider = countryProvider;
32
+ this.trustedClock = trustedClock;
33
+ this.webRequestPipeline = webRequestPipeline;
34
+ this.onTrackerInteraction = onTrackerInteraction;
35
+ this.getBrowserInfo = getBrowserInfo;
36
+ this.isRequestAllowed = isRequestAllowed;
37
+ }
38
+
39
+ async init() {
40
+ this.db = new Database();
41
+ await this.db.init();
42
+
43
+ this.config = new Config(this.settings, {
44
+ db: this.db,
45
+ trustedClock: this.trustedClock,
46
+ });
47
+ this.requestMonitor = new RequestMonitor(this.settings, {
48
+ db: this.db,
49
+ webRequestPipeline: this.webRequestPipeline,
50
+ trustedClock: this.trustedClock,
51
+ countryProvider: this.countryProvider,
52
+ communication: this.communication,
53
+ onTrackerInteraction: this.onTrackerInteraction,
54
+ getBrowserInfo: this.getBrowserInfo,
55
+ isRequestAllowed: this.isRequestAllowed,
56
+ });
57
+
58
+ await this.config.init();
59
+ await this.requestMonitor.init(this.config);
60
+ }
61
+
62
+ unload() {
63
+ if (this.requestMonitor) {
64
+ this.requestMonitor.unload();
65
+ this.requestMonitor = null;
66
+ }
67
+ this.webRequestPipeline.unload();
68
+ }
69
+
70
+ addPipelineStep(stage, opts) {
71
+ if (
72
+ !this.requestMonitor.pipelines ||
73
+ !this.requestMonitor.pipelines[stage]
74
+ ) {
75
+ return Promise.reject(
76
+ new Error(`Could not add pipeline step: ${stage}, ${opts.name}`),
77
+ );
78
+ }
79
+
80
+ return this.requestMonitor.pipelines[stage].addPipelineStep(opts);
81
+ }
82
+
83
+ removePipelineStep(stage, name) {
84
+ if (
85
+ this.requestMonitor &&
86
+ this.requestMonitor.pipelines &&
87
+ this.requestMonitor.pipelines[stage]
88
+ ) {
89
+ this.requestMonitor.pipelines[stage].removePipelineStep(name);
90
+ }
91
+ }
92
+
93
+ recordClick(event, context, href, sender) {
94
+ this.requestMonitor.pipelineSteps.cookieContext.setContextFromEvent(
95
+ event,
96
+ context,
97
+ href,
98
+ sender,
99
+ );
100
+ this.requestMonitor.pipelineSteps.oAuthDetector.recordClick(
101
+ event,
102
+ context,
103
+ href,
104
+ sender,
105
+ );
106
+ }
107
+
108
+ clearCache() {
109
+ this.requestMonitor.clearCache();
110
+ }
111
+ }