@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,209 @@
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 { truncatedHash } from '../../md5';
13
+ import pacemaker from '../../utils/pacemaker';
14
+ import { parse } from '../../utils/url';
15
+ import { sameGeneralDomain, getGeneralDomain } from '../utils/tlds';
16
+ import logger from '../../logger';
17
+ import ChromeStorageMap from '../utils/chrome-storage-map';
18
+
19
+ const TIME_AFTER_LINK = 5 * 1000;
20
+ const TIME_CLEANING_CACHE = 3 * 60 * 1000; // 3 minutes
21
+ const TIME_ACTIVE = 20 * 1000;
22
+ // how long to keep trust entries which have not been triggered
23
+ const UNUSED_TRUST_TIMEOUT = 2 * 60 * 1000; // 2 minutes
24
+ // how long to keep trust entries which have been used
25
+ const USED_TRUST_TIMEOUT = 15 * 50 * 1000; // 15 minutes
26
+ const CLEAN_COOKIE_CACHE_TIMEOUT = 2 * 60 * 1000;
27
+
28
+ export default class CookieContext {
29
+ constructor(config, qsWhitelist) {
30
+ this.config = config;
31
+ this.qsWhitelist = qsWhitelist;
32
+
33
+ this.contextFromEvent = null;
34
+ this.visitCache = new ChromeStorageMap({
35
+ storageKey: 'wtm-request-reporting:cookie-context:visit-cache',
36
+ ttlInMs: TIME_CLEANING_CACHE,
37
+ });
38
+
39
+ this.trustedThirdParties = new ChromeStorageMap({
40
+ storageKey: 'wtm-request-reporting:cookie-context:trusted-third-parties',
41
+ ttlInMs: USED_TRUST_TIMEOUT,
42
+ });
43
+ }
44
+
45
+ async init() {
46
+ await this.visitCache.isReady;
47
+ await this.trustedThirdParties.isReady;
48
+ this.cleanCookieCache();
49
+ this._pmclean = pacemaker.register(this.cleanCookieCache.bind(this), {
50
+ timeout: CLEAN_COOKIE_CACHE_TIMEOUT,
51
+ });
52
+ }
53
+
54
+ unload() {
55
+ this._pmclean = pacemaker.clearTimeout(this._pmclean);
56
+ }
57
+
58
+ cleanCookieCache() {
59
+ // trusted domain pairs
60
+ const now = Date.now();
61
+ this.trustedThirdParties.forEach((counter, key) => {
62
+ const timeoutAt =
63
+ counter.ts +
64
+ (counter.c > 0 ? USED_TRUST_TIMEOUT : UNUSED_TRUST_TIMEOUT);
65
+ if (now > timeoutAt) {
66
+ this.trustedThirdParties.delete(key);
67
+ }
68
+ });
69
+ }
70
+
71
+ _addTrustLink(fromFirstParty, toThirdParty) {
72
+ if (sameGeneralDomain(fromFirstParty, toThirdParty)) {
73
+ return;
74
+ }
75
+ // don't trust trackers
76
+ if (
77
+ this.qsWhitelist.isTrackerDomain(
78
+ truncatedHash(getGeneralDomain(toThirdParty)),
79
+ )
80
+ ) {
81
+ return;
82
+ }
83
+ const key = `${fromFirstParty}:${toThirdParty}`;
84
+ if (!this.trustedThirdParties.has(key)) {
85
+ this.trustedThirdParties.set(key, { c: 0 });
86
+ }
87
+ this.trustedThirdParties.get(key).ts = Date.now();
88
+ }
89
+
90
+ assignCookieTrust(state) {
91
+ if (state.isMainFrame && state.getReferrer()) {
92
+ const referrer = parse(state.getReferrer());
93
+ if (!referrer) {
94
+ return true;
95
+ }
96
+ const trustedHost = state.urlParts.hostname;
97
+ const trustedOn = referrer.hostname;
98
+
99
+ // this domain is now trusted by the referrer
100
+ this._addTrustLink(trustedOn, trustedHost);
101
+ }
102
+ return true;
103
+ }
104
+
105
+ checkCookieTrust(state) {
106
+ const stage = state.statusCode !== undefined ? 'set_cookie' : 'cookie';
107
+ const sourceHost = state.tabUrlParts.hostname;
108
+ const requestHost = state.urlParts.hostname;
109
+ const key = `${sourceHost}:${requestHost}`;
110
+ if (this.config.cookieTrustReferers && this.trustedThirdParties.has(key)) {
111
+ const trustCounter = this.trustedThirdParties.get(key);
112
+ trustCounter.c += 1;
113
+ trustCounter.ts = Date.now();
114
+
115
+ state.incrementStat(`${stage}_allow_trust`);
116
+ return false;
117
+ }
118
+ return true;
119
+ }
120
+
121
+ checkVisitCache(state) {
122
+ // check if the response has been received yet
123
+ const stage = state.statusCode !== undefined ? 'set_cookie' : 'cookie';
124
+ const tabId = state.tabId;
125
+ const diff =
126
+ Date.now() - (this.visitCache.get(`${tabId}:${state.hostGD}`) || 0);
127
+ if (
128
+ diff < TIME_ACTIVE &&
129
+ this.visitCache.get(`${tabId}:${state.sourceGD}`)
130
+ ) {
131
+ state.incrementStat(`${stage}_allow_visitcache`);
132
+ return false;
133
+ }
134
+ return true;
135
+ }
136
+
137
+ checkContextFromEvent(state) {
138
+ if (this.contextFromEvent) {
139
+ const stage = state.statusCode !== undefined ? 'set_cookie' : 'cookie';
140
+ const time = Date.now();
141
+ const url = state.url;
142
+ const tabId = state.tabId;
143
+ const urlParts = state.urlParts;
144
+ const sourceGD = state.tabUrlParts.generalDomain;
145
+ const hostGD = state.urlParts.generalDomain;
146
+
147
+ const diff = time - (this.contextFromEvent.ts || 0);
148
+ if (diff < TIME_AFTER_LINK) {
149
+ if (
150
+ hostGD === this.contextFromEvent.cGD &&
151
+ sourceGD === this.contextFromEvent.pageGD
152
+ ) {
153
+ this.visitCache.set(`${tabId}:${hostGD}`, time);
154
+ state.incrementStat(`${stage}_allow_userinit_same_context_gd`);
155
+ return false;
156
+ }
157
+ const pu = url.split(/[?&;]/)[0];
158
+ if (this.contextFromEvent.html.indexOf(pu) !== -1) {
159
+ // the url is in pu
160
+ if (urlParts && urlParts.hostname && urlParts.hostname !== '') {
161
+ this.visitCache.set(`${tabId}:${hostGD}`, time);
162
+ state.incrementStat(`${stage}_allow_userinit_same_gd_link`);
163
+ return false;
164
+ }
165
+ }
166
+ // last try, guess the possible domain from script src;
167
+ if (
168
+ !this.contextFromEvent.cGD &&
169
+ this.contextFromEvent.possibleCGD.has(hostGD)
170
+ ) {
171
+ this.visitCache.set(`${tabId}:${hostGD}`, time);
172
+ state.incrementStat(`${stage}_allow_userinit_same_script_gd`);
173
+ return false;
174
+ }
175
+ }
176
+ }
177
+ return true;
178
+ }
179
+
180
+ extractPossilbeContextGD(links) {
181
+ return new Set(links.map((link) => parse(link).generalDomain));
182
+ }
183
+
184
+ setContextFromEvent(ev, contextHTML, herf, sender) {
185
+ let cGD = null;
186
+ let pageGD = null;
187
+ const html = contextHTML || '';
188
+
189
+ try {
190
+ pageGD = parse(sender.tab.url).generalDomain;
191
+ cGD = parse(ev.target.baseURI).generalDomain;
192
+ } catch (ee) {
193
+ logger.error('CookieContext could not parse URL', ee);
194
+ // empty
195
+ }
196
+ if (!pageGD || cGD === pageGD) {
197
+ return;
198
+ }
199
+ // Try to guess the possible domain from scripts src
200
+ const possibleCGD = this.extractPossilbeContextGD(ev.target.linksSrc);
201
+ this.contextFromEvent = {
202
+ html,
203
+ ts: Date.now(),
204
+ cGD,
205
+ pageGD,
206
+ possibleCGD,
207
+ };
208
+ }
209
+ }
@@ -0,0 +1,101 @@
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 { parse } from '../../utils/url';
13
+ import Subject from '../utils/subject';
14
+ import ChromeStorageMap from '../utils/chrome-storage-map';
15
+
16
+ const DEFAULT_OPTIONS = {
17
+ CLICK_TIMEOUT: 5 * 60 * 1000,
18
+ VISIT_TIMEOUT: 4 * 60 * 1000,
19
+ };
20
+
21
+ export default class OAuthDetector {
22
+ constructor(options = DEFAULT_OPTIONS) {
23
+ Object.assign(this, DEFAULT_OPTIONS, options);
24
+
25
+ this.clickActivity = new ChromeStorageMap({
26
+ storageKey: 'wtm-request-reporting:oauth-detector:click-activity',
27
+ ttlInMs: this.CLICK_TIMEOUT,
28
+ });
29
+ this.siteActivitiy = new ChromeStorageMap({
30
+ storageKey: 'wtm-request-reporting:oauth-detector:site-activity',
31
+ ttlInMs: this.VISIT_TIMEOUT,
32
+ });
33
+ this.subjectMainFrames = new Subject();
34
+ this.tabClicks = new Subject();
35
+ }
36
+
37
+ recordClick(ev, contextHTML, href, sender) {
38
+ this.tabClicks.pub(sender.tab);
39
+ }
40
+
41
+ async init() {
42
+ await this.clickActivity.isReady;
43
+ await this.siteActivitiy.isReady;
44
+ this.tabActivitySubscription = this.tabClicks.subscribe((event) => {
45
+ this.clickActivity.set(event.id, event.url);
46
+ });
47
+ this.pageOpenedSubscription = this.subjectMainFrames.subscribe((event) => {
48
+ this.siteActivitiy.set(event.hostname, event.tabId);
49
+ });
50
+ }
51
+
52
+ unload() {
53
+ if (this.tabActivitySubscription) {
54
+ this.tabActivitySubscription.unsubscribe();
55
+ }
56
+ if (this.pageOpenedSubscription) {
57
+ this.pageOpenedSubscription.unsubscribe();
58
+ }
59
+ }
60
+
61
+ checkMainFrames(state) {
62
+ if (state.isMainFrame) {
63
+ this.subjectMainFrames.pub({
64
+ tabId: state.tabId,
65
+ hostname: state.urlParts.hostname,
66
+ });
67
+ }
68
+ }
69
+
70
+ /**
71
+ * Pipeline step to check if this request is part of a OAuth flow. This is done by
72
+ * checking that the following three conditions are met:
73
+ * - The third-party url path contains '/oauth' or '/authorize'
74
+ * - The user has recently clicked in the source tab (i.e. the site wanting to authenticate the
75
+ * user)
76
+ * - The user has recently visited the oauth domain (the authentication provider)
77
+ * @param state
78
+ * @returns false if the request is an oauth request, true otherwise
79
+ */
80
+ checkIsOAuth(state, type) {
81
+ const isOAuthFlow = ['/oauth', '/authorize'].some((pattern) =>
82
+ state.urlParts.pathname.includes(pattern),
83
+ );
84
+
85
+ if (
86
+ isOAuthFlow &&
87
+ this.clickActivity.get(state.tabId) &&
88
+ this.siteActivitiy.get(state.urlParts.hostname)
89
+ ) {
90
+ const clickedPage = parse(this.clickActivity.get(state.tabId));
91
+ if (
92
+ clickedPage !== null &&
93
+ clickedPage.hostname === state.tabUrlParts.hostname
94
+ ) {
95
+ state.incrementStat(`${type}_allow_oauth`);
96
+ return false;
97
+ }
98
+ }
99
+ return true;
100
+ }
101
+ }
@@ -0,0 +1,177 @@
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 no-param-reassign: 'off' */
13
+ import { truncateDomain } from '../utils';
14
+
15
+ // maps string (web-ext) to int (FF cpt). Anti-tracking still uses these legacy types.
16
+ const TYPE_LOOKUP = {
17
+ other: 1,
18
+ script: 2,
19
+ image: 3,
20
+ stylesheet: 4,
21
+ object: 5,
22
+ main_frame: 6,
23
+ sub_frame: 7,
24
+ xbl: 9,
25
+ ping: 10,
26
+ xmlhttprequest: 11,
27
+ object_subrequest: 12,
28
+ xml_dtd: 13,
29
+ font: 14,
30
+ media: 15,
31
+ websocket: 16,
32
+ csp_report: 17,
33
+ xslt: 18,
34
+ beacon: 19,
35
+ imageset: 21,
36
+ web_manifest: 22,
37
+ };
38
+
39
+ export default class PageLogger {
40
+ constructor(config) {
41
+ this.config = config;
42
+ // maintain the request metadabase between pipeline steps
43
+ this._requestCounters = new Map();
44
+ }
45
+
46
+ _attachCounters(state) {
47
+ const stats = state.page.getStatsForDomain(
48
+ truncateDomain(state.urlParts.domainInfo, 2),
49
+ );
50
+ state.reqLog = stats;
51
+ const incrementStat = (statName, c) => {
52
+ stats[statName] = (stats[statName] || 0) + (c || 1);
53
+ };
54
+ const setStat = (statName, value) => {
55
+ stats[statName] = value;
56
+ };
57
+ state.incrementStat = incrementStat;
58
+ state.setStat = setStat;
59
+
60
+ if (state.requestId) {
61
+ this._requestCounters.set(state.requestId, {
62
+ incrementStat,
63
+ setStat,
64
+ });
65
+ }
66
+ }
67
+
68
+ _loadStatCounters(state) {
69
+ const meta = this._requestCounters.get(state.requestId);
70
+ Object.keys(meta).forEach((k) => {
71
+ state[k] = meta[k];
72
+ });
73
+ }
74
+
75
+ onBeforeRequest(state) {
76
+ this._attachCounters(state);
77
+ const { incrementStat, urlParts } = state;
78
+
79
+ incrementStat('c');
80
+ if (urlParts.search.length > 0) {
81
+ incrementStat('has_qs');
82
+ }
83
+ if (urlParts.hasParameterString() > 0) {
84
+ incrementStat('has_ps');
85
+ }
86
+ if (urlParts.hash.length > 0) {
87
+ incrementStat('has_fragment');
88
+ }
89
+ if (state.method === 'POST') {
90
+ incrementStat('has_post');
91
+ }
92
+
93
+ incrementStat(`type_${TYPE_LOOKUP[state.type] || 'unknown'}`);
94
+
95
+ // log protocol (secure or not)
96
+ const isHTTP = (protocol) => protocol === 'http:' || protocol === 'https:';
97
+ const scheme = isHTTP(urlParts.protocol) ? urlParts.scheme : 'other';
98
+ incrementStat(`scheme_${scheme}`);
99
+
100
+ // find frame depth
101
+ incrementStat(`window_depth_${Math.min(state.frameAncestors.length, 2)}`);
102
+
103
+ if (state.url.indexOf(this.config.placeHolder) > -1) {
104
+ incrementStat('hasPlaceHolder');
105
+ }
106
+ }
107
+
108
+ onBeforeSendHeaders(state) {
109
+ if (state.requestId && this._requestCounters.has(state.requestId)) {
110
+ this._loadStatCounters(state);
111
+ } else {
112
+ this._attachCounters(state);
113
+ }
114
+ // referer stats
115
+ const referrer = state.getReferrer();
116
+ if (referrer && referrer.indexOf(state.tabUrl) > -1) {
117
+ state.incrementStat('referer_leak_header');
118
+ }
119
+ if (
120
+ state.url.indexOf(state.tabUrlParts.hostname) > -1 ||
121
+ state.url.indexOf(encodeURIComponent(state.tabUrlParts.hostname)) > -1
122
+ ) {
123
+ state.incrementStat('referer_leak_site');
124
+ if (
125
+ state.url.indexOf(state.tabUrlParts.pathname) > -1 ||
126
+ state.url.indexOf(encodeURIComponent(state.tabUrlParts.pathname)) > -1
127
+ ) {
128
+ state.incrementStat('referer_leak_path');
129
+ }
130
+ }
131
+ // check if the request contains a cookie
132
+ state.cookieData = state.getCookieData();
133
+ const hasCookie = state.cookieData && state.cookieData.length > 5;
134
+ if (hasCookie) {
135
+ state.incrementStat('cookie_set');
136
+ }
137
+ state.hasCookie = hasCookie;
138
+ return true;
139
+ }
140
+
141
+ onHeadersReceived(state) {
142
+ if (state.requestId && this._requestCounters.has(state.requestId)) {
143
+ this._loadStatCounters(state);
144
+ } else {
145
+ this._attachCounters(state);
146
+ }
147
+ state.incrementStat('resp_ob');
148
+ state.incrementStat(
149
+ 'content_length',
150
+ parseInt(state.getResponseHeader('Content-Length'), 10) || 0,
151
+ );
152
+ state.incrementStat(`status_${state.statusCode}`);
153
+
154
+ const setCookie = state.getResponseHeader('Set-Cookie');
155
+ const hasSetCookie = setCookie && setCookie.length > 5;
156
+ if (hasSetCookie) {
157
+ state.incrementStat('set_cookie_set');
158
+ // log samesite=none: explicit cross site cookies
159
+ if (setCookie.toLowerCase().indexOf('samesite=none') !== -1) {
160
+ state.incrementStat('set_cookie_samesite_none');
161
+ }
162
+ }
163
+ state.hasSetCookie = hasSetCookie;
164
+
165
+ return true;
166
+ }
167
+
168
+ reattachStatCounter(state) {
169
+ const { requestId } = state;
170
+ if (requestId && this._requestCounters.has(requestId)) {
171
+ this._loadStatCounters(state);
172
+ this._requestCounters.delete(requestId);
173
+ return true;
174
+ }
175
+ return false;
176
+ }
177
+ }
@@ -0,0 +1,75 @@
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 TempSet from '../utils/temp-set';
13
+
14
+ const CACHE_TIMEOUT = 10 * 1000;
15
+
16
+ /**
17
+ * Caches 302 redirects so that we can ensure that the resulting request is properly
18
+ * passed through the token logic.
19
+ */
20
+ export default class RedirectTagger {
21
+ constructor() {
22
+ this.redirectCache = new TempSet();
23
+ this.redirectTaggerCache = new TempSet();
24
+ }
25
+
26
+ isFromRedirect(url) {
27
+ return this.redirectCache.has(url);
28
+ }
29
+
30
+ checkRedirectStatus(state) {
31
+ if (state.statusCode === 302) {
32
+ const location = state.getResponseHeader('Location');
33
+ if (!location) {
34
+ // 302 without "Location" in header?
35
+ console.log(state, '302 without "Location" in header?');
36
+ return true;
37
+ }
38
+ if (location.startsWith('/')) {
39
+ // relative redirect
40
+ const redirectUrl = `${state.urlParts.protocol}://${state.urlParts.hostname}${location}`;
41
+ this.redirectCache.add(redirectUrl, CACHE_TIMEOUT);
42
+ } else if (
43
+ location.startsWith('http://') ||
44
+ location.startsWith('https://')
45
+ ) {
46
+ // absolute redirect
47
+ this.redirectCache.add(location, CACHE_TIMEOUT);
48
+ }
49
+ }
50
+ return true;
51
+ }
52
+
53
+ checkRedirect(details) {
54
+ if (details.isRedirect && details.requestId !== undefined) {
55
+ this.redirectTaggerCache.add(details.requestId, CACHE_TIMEOUT);
56
+ return false;
57
+ }
58
+ return true;
59
+ }
60
+
61
+ confirmRedirect(details) {
62
+ if (
63
+ details.requestId !== undefined &&
64
+ this.redirectTaggerCache.has(details.requestId)
65
+ ) {
66
+ return false;
67
+ }
68
+
69
+ if (details.isMainFrame && details.isRedirect) {
70
+ return false;
71
+ }
72
+
73
+ return true;
74
+ }
75
+ }
@@ -0,0 +1,34 @@
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 default class SubdomainCheck {
13
+ constructor(config) {
14
+ this.config = config;
15
+ }
16
+
17
+ checkBadSubdomain(state, response) {
18
+ const subdomainRewriteRules = this.config.subdomainRewriteRules || {};
19
+ const requestHost = state.urlParts.hostname;
20
+ const rules = Object.keys(subdomainRewriteRules);
21
+ for (let i = 0; i < rules.length; i += 1) {
22
+ const rule = rules[i];
23
+ if (requestHost.endsWith(rule)) {
24
+ const newUrl = state.url.replace(
25
+ requestHost,
26
+ subdomainRewriteRules[rule],
27
+ );
28
+ response.redirectTo(newUrl);
29
+ return false;
30
+ }
31
+ }
32
+ return true;
33
+ }
34
+ }