@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.
- package/.editorconfig +12 -0
- package/.eslintignore +3 -0
- package/.eslintrc +16 -0
- package/.github/workflows/release.yml +34 -0
- package/.github/workflows/tests.yml +20 -0
- package/.prettierrc +9 -0
- package/.tool-versions +1 -0
- package/CHANGELOG.md +795 -0
- package/LICENSE +373 -0
- package/README.md +1 -0
- package/communication/README.md +11 -0
- package/communication/example/index.js +43 -0
- package/communication/example/manifest.json +16 -0
- package/communication/karma.conf.cjs +19 -0
- package/communication/package.json +52 -0
- package/communication/rollup.common-config.cjs +17 -0
- package/communication/rollup.config.cjs +10 -0
- package/communication/src/encoding.js +54 -0
- package/communication/src/errors.js +84 -0
- package/communication/src/index.js +76 -0
- package/communication/src/logger.js +41 -0
- package/communication/src/padding.js +60 -0
- package/communication/src/proxied-http.js +231 -0
- package/communication/src/server-public-key-accessor.js +123 -0
- package/communication/src/timestamps.js +37 -0
- package/communication/src/trusted-clock.js +58 -0
- package/communication/src/utils.js +28 -0
- package/communication/src/zlib.js +17 -0
- package/communication/test/helpers/in-memory-database.js +43 -0
- package/communication/test/server-public-key-accessor.spec.js +135 -0
- package/communication/test/utils.spec.js +43 -0
- package/package.json +50 -0
- package/reporting/TODO.md +2 -0
- package/reporting/assets/request/bloom_filter.dat +0 -0
- package/reporting/assets/request/update.json +1 -0
- package/reporting/example/content.js +11 -0
- package/reporting/example/index.js +132 -0
- package/reporting/example/inspector/index.html +17 -0
- package/reporting/example/inspector/index.js +27 -0
- package/reporting/example/manifests/chromium.json +37 -0
- package/reporting/example/manifests/firefox.json +35 -0
- package/reporting/example/rules.json +62 -0
- package/reporting/example/setup.js +1 -0
- package/reporting/example/tests/test1.html +10 -0
- package/reporting/karma.conf.cjs +22 -0
- package/reporting/package.json +63 -0
- package/reporting/rollup.common-config.cjs +19 -0
- package/reporting/rollup.config.cjs +20 -0
- package/reporting/src/LRU.js +149 -0
- package/reporting/src/activity-estimator.js +298 -0
- package/reporting/src/alive-check.js +139 -0
- package/reporting/src/alive-message-generator.js +142 -0
- package/reporting/src/bloom-filters.js +345 -0
- package/reporting/src/country-provider.js +295 -0
- package/reporting/src/cyrb53.js +32 -0
- package/reporting/src/digest.js +33 -0
- package/reporting/src/doublefetch-page-handler.js +548 -0
- package/reporting/src/duplicate-detector.js +233 -0
- package/reporting/src/errors.js +84 -0
- package/reporting/src/event-listener-queue.js +121 -0
- package/reporting/src/fixed-size-cache.js +63 -0
- package/reporting/src/hash-detector.js +1526 -0
- package/reporting/src/html-parser.js +17 -0
- package/reporting/src/http.js +265 -0
- package/reporting/src/index.js +14 -0
- package/reporting/src/job-scheduler.js +912 -0
- package/reporting/src/logger.js +121 -0
- package/reporting/src/md5.js +334 -0
- package/reporting/src/message-sender.js +51 -0
- package/reporting/src/network.js +140 -0
- package/reporting/src/new-page-approver.js +119 -0
- package/reporting/src/page-aggregator.js +185 -0
- package/reporting/src/page-quorum-check-handler.js +200 -0
- package/reporting/src/page-structure.js +136 -0
- package/reporting/src/pagedb.js +671 -0
- package/reporting/src/pages.js +1798 -0
- package/reporting/src/patterns-updater.js +310 -0
- package/reporting/src/patterns.js +416 -0
- package/reporting/src/persisted-bitarray.js +265 -0
- package/reporting/src/persisted-hashes.js +189 -0
- package/reporting/src/quorum-checker.js +327 -0
- package/reporting/src/random.js +39 -0
- package/reporting/src/reporting.js +290 -0
- package/reporting/src/request/config.js +116 -0
- package/reporting/src/request/content-script.js +77 -0
- package/reporting/src/request/database.js +147 -0
- package/reporting/src/request/dnt.js +24 -0
- package/reporting/src/request/hash.js +101 -0
- package/reporting/src/request/index.js +874 -0
- package/reporting/src/request/page-telemetry.js +42 -0
- package/reporting/src/request/qs-whitelist2.js +247 -0
- package/reporting/src/request/resources/prob.js +601 -0
- package/reporting/src/request/steps/block-rules.js +40 -0
- package/reporting/src/request/steps/check-context.js +37 -0
- package/reporting/src/request/steps/cookie-context.js +209 -0
- package/reporting/src/request/steps/oauth-detector.js +101 -0
- package/reporting/src/request/steps/page-logger.js +177 -0
- package/reporting/src/request/steps/redirect-tagger.js +75 -0
- package/reporting/src/request/steps/subdomain-check.js +34 -0
- package/reporting/src/request/steps/token-checker/index.js +189 -0
- package/reporting/src/request/steps/token-checker/token-domain.js +129 -0
- package/reporting/src/request/steps/token-examiner.js +182 -0
- package/reporting/src/request/steps/token-telemetry/cached-entry-pipeline.js +210 -0
- package/reporting/src/request/steps/token-telemetry/index.js +234 -0
- package/reporting/src/request/steps/token-telemetry/key-pipeline.js +109 -0
- package/reporting/src/request/steps/token-telemetry/token-pipeline.js +105 -0
- package/reporting/src/request/time.js +73 -0
- package/reporting/src/request/utils/bloom-filter-packed.js +107 -0
- package/reporting/src/request/utils/chrome-storage-map.js +282 -0
- package/reporting/src/request/utils/subject.js +37 -0
- package/reporting/src/request/utils/temp-set.js +56 -0
- package/reporting/src/request/utils/tlds.js +55 -0
- package/reporting/src/request/utils.js +41 -0
- package/reporting/src/request-reporter.js +111 -0
- package/reporting/src/sanitizer.js +519 -0
- package/reporting/src/search-extractor.js +338 -0
- package/reporting/src/self-check.js +105 -0
- package/reporting/src/seq-executor.js +43 -0
- package/reporting/src/session-storage.js +399 -0
- package/reporting/src/url-analyzer.js +161 -0
- package/reporting/src/url-cleaner.js +60 -0
- package/reporting/src/utils/pacemaker.js +279 -0
- package/reporting/src/utils/url.js +114 -0
- package/reporting/src/utils.js +345 -0
- package/reporting/src/webrequest-pipeline/cname-uncloak.js +110 -0
- package/reporting/src/webrequest-pipeline/index.js +287 -0
- package/reporting/src/webrequest-pipeline/logger.js +14 -0
- package/reporting/src/webrequest-pipeline/page-store.js +282 -0
- package/reporting/src/webrequest-pipeline/page.js +140 -0
- package/reporting/src/webrequest-pipeline/pipeline.js +249 -0
- package/reporting/src/webrequest-pipeline/utils/webrequest.js +113 -0
- package/reporting/src/webrequest-pipeline/webrequest-context.js +153 -0
- package/reporting/test/README.md +2 -0
- package/reporting/test/activity-estimator.spec.js +356 -0
- package/reporting/test/alive-check.spec.js +156 -0
- package/reporting/test/alive-message-generator.spec.js +186 -0
- package/reporting/test/country-provider.spec.js +263 -0
- package/reporting/test/doublefetch-page-handler.spec.js +818 -0
- package/reporting/test/duplicate-detector.spec.js +689 -0
- package/reporting/test/helpers/dom-parsers.js +41 -0
- package/reporting/test/helpers/fake-session-storage.js +69 -0
- package/reporting/test/helpers/fast-check-utils.js +28 -0
- package/reporting/test/helpers/fetch-mock.js +57 -0
- package/reporting/test/helpers/in-memory-database.js +14 -0
- package/reporting/test/helpers/memory-map.js +72 -0
- package/reporting/test/helpers/pagedb-mocks.js +41 -0
- package/reporting/test/index.js +47 -0
- package/reporting/test/job-scheduler.spec.js +691 -0
- package/reporting/test/network.spec.js +72 -0
- package/reporting/test/page-aggregator.spec.js +104 -0
- package/reporting/test/page-quorum-check-handler.spec.js +86 -0
- package/reporting/test/pagedb.spec.js +250 -0
- package/reporting/test/pages.spec.js +693 -0
- package/reporting/test/patterns-updater.spec.js +446 -0
- package/reporting/test/patterns.spec.js +751 -0
- package/reporting/test/persisted-hashes.spec.js +169 -0
- package/reporting/test/reporting.spec.js +205 -0
- package/reporting/test/request/database.spec.js +59 -0
- package/reporting/test/request/hash.spec.js +108 -0
- package/reporting/test/request/index.spec.js +378 -0
- package/reporting/test/request/qs-whitelist2.spec.js +189 -0
- package/reporting/test/request/steps/check-context.spec.js +51 -0
- package/reporting/test/request/steps/oauth-detector.spec.js +339 -0
- package/reporting/test/request/test-pages.js +1246 -0
- package/reporting/test/request/utils/bloom-filter-packed.spec.js +37 -0
- package/reporting/test/request/utils/chrome-storage-map.spec.js +28 -0
- package/reporting/test/request/utils/tldts.spec.js +62 -0
- package/reporting/test/request/utils.spec.js +168 -0
- package/reporting/test/sanitizer.spec.js +1087 -0
- package/reporting/test/search-extractor.spec.js +590 -0
- package/reporting/test/seq-executor.spec.js +158 -0
- package/reporting/test/session-storage.spec.js +400 -0
- package/reporting/test/setup.js +24 -0
- package/reporting/test/url-analyzer.spec.js +451 -0
- package/reporting/test/url-cleaner.spec.js +102 -0
- package/reporting/test/utils/pacemaker.spec.js +222 -0
- package/reporting/test/utils/url.spec.js +660 -0
- package/reporting/test/utils.spec.js +299 -0
- package/reporting/test/webrequest-pipeline/pipeline.spec.js +251 -0
|
@@ -0,0 +1,295 @@
|
|
|
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
|
+
import { randBetween, clamp } from './utils';
|
|
14
|
+
|
|
15
|
+
const SECOND = 1000;
|
|
16
|
+
const MINUTE = 60 * SECOND;
|
|
17
|
+
const HOUR = 60 * MINUTE;
|
|
18
|
+
const DAY = 24 * HOUR;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* If you need to introduce incompatible changes to the the state
|
|
22
|
+
* persistence, you can bump this number to clear the persisted cache.
|
|
23
|
+
* It will be as if you start with a fresh installation of the extension.
|
|
24
|
+
*
|
|
25
|
+
* (If you are not sure whether the change is incompatible, it is
|
|
26
|
+
* a good idea to be conservative and bump this number anyway; it will
|
|
27
|
+
* only introduce little overhead.)
|
|
28
|
+
*/
|
|
29
|
+
const DB_VERSION = 1;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Responsible for fetching the country information from the config API endpoint
|
|
33
|
+
* and keeping it up-to-date. Although you can force updates through the "update",
|
|
34
|
+
* it should not be needed to explicitly call it.
|
|
35
|
+
*
|
|
36
|
+
* WARNING: When using this class, only access data through the "getSafeCountryCode"
|
|
37
|
+
* function. Why? Because otherwise, it is easy to accidentially allow fingerprinting
|
|
38
|
+
* (see the comments in "_sanitizeCountry" for details).
|
|
39
|
+
*/
|
|
40
|
+
export default class CountryProvider {
|
|
41
|
+
constructor({ config, storage, storageKey }) {
|
|
42
|
+
this.allowedCountryCodes = config.ALLOWED_COUNTRY_CODES;
|
|
43
|
+
if (!this.allowedCountryCodes) {
|
|
44
|
+
throw new Error('config.ALLOWED_COUNTRY_CODES not set');
|
|
45
|
+
}
|
|
46
|
+
this.configApiEndpoint = config.CONFIG_URL;
|
|
47
|
+
if (!this.configApiEndpoint) {
|
|
48
|
+
throw new Error('config.CONFIG_URL not set');
|
|
49
|
+
}
|
|
50
|
+
this.storage = storage;
|
|
51
|
+
this.storageKey = storageKey;
|
|
52
|
+
this.ctryInfo = null; // guaranteed to exist after "init()"
|
|
53
|
+
|
|
54
|
+
// This would result in approximately one request per day
|
|
55
|
+
// (not counting retry attempts if there were errors).
|
|
56
|
+
this.cacheExpiration = {
|
|
57
|
+
min: 22 * HOUR,
|
|
58
|
+
max: 26 * HOUR,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async init({ now = Date.now() } = {}) {
|
|
63
|
+
if (this.ctryInfo) {
|
|
64
|
+
logger.debug('CountryProvider is already initialized');
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const cachedEntry = await this.storage.get(this.storageKey);
|
|
70
|
+
if (cachedEntry) {
|
|
71
|
+
this.ctryInfo = this._validateCtryInfo(cachedEntry, now);
|
|
72
|
+
this._nonBlockingUpdateInBackground(now);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
} catch (e) {
|
|
76
|
+
logger.warn('Failed to get a value from cache', e);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// This is a rare situation. Typically, it can be reached only once in a fresh
|
|
80
|
+
// installation, through schema updates, or in exceptional error situations
|
|
81
|
+
// where corrupted data was persisted. Since the cache is empty, we will now do
|
|
82
|
+
// a blocking update to start with a reliable country value.
|
|
83
|
+
logger.info(
|
|
84
|
+
'Country information not cached. This should only happen on the first time the extension is started.',
|
|
85
|
+
);
|
|
86
|
+
this.ctryInfo = {
|
|
87
|
+
dbVersion: DB_VERSION,
|
|
88
|
+
unsafeCtryFromApi: '--',
|
|
89
|
+
safeCtry: '--',
|
|
90
|
+
lastSuccessAt: 0, // Unix timestamp
|
|
91
|
+
lastAttemptAt: 0, // Unix timestamp
|
|
92
|
+
skipAttemptsUntil: 0, // Unix timestamp
|
|
93
|
+
failedAttemptsInARows: 0,
|
|
94
|
+
};
|
|
95
|
+
try {
|
|
96
|
+
await this.update({ now });
|
|
97
|
+
} catch (e) {
|
|
98
|
+
logger.warn('Unable to get country information. Falling back to "--"', e);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
getSafeCountryCode({ skipUpdate = false, now = Date.now() } = {}) {
|
|
103
|
+
if (!this.ctryInfo) {
|
|
104
|
+
throw new Error('Illegal state: forgot to call init()');
|
|
105
|
+
}
|
|
106
|
+
if (!skipUpdate) {
|
|
107
|
+
// Schedule an update if needed. That is done to guarantee that updates
|
|
108
|
+
// will run from time to time. In Manifest V3, this is mostly theoretical,
|
|
109
|
+
// since service workers will die quickly and "init()" calls also trigger
|
|
110
|
+
// updates. Nevertheless, there is no downsize of including the update
|
|
111
|
+
// check here as well.
|
|
112
|
+
//
|
|
113
|
+
// Note: It is clear that the update here will at best affect the next call of
|
|
114
|
+
// "getSafeCountryCode" (even that is not guaranteed though). In the rare case
|
|
115
|
+
// that you need to ensure that an update finished, the caller should instead
|
|
116
|
+
// explicitly trigger an update ("await countryProvider.update()") before
|
|
117
|
+
// calling "getSafeCountryCode".
|
|
118
|
+
this._nonBlockingUpdateInBackground(now);
|
|
119
|
+
}
|
|
120
|
+
return this.ctryInfo.safeCtry;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Normally, it should not be needed to call this function directly.
|
|
125
|
+
*
|
|
126
|
+
* It is safe though to call it in a loop if needed. Multiple calls
|
|
127
|
+
* will not lead to multiple HTTP requests and the performance impact
|
|
128
|
+
* should not be noticeable in most scenarios.
|
|
129
|
+
*/
|
|
130
|
+
async update({ force = false, now = Date.now() } = {}) {
|
|
131
|
+
if (this._pendingUpdate) {
|
|
132
|
+
logger.debug('update already in progress...');
|
|
133
|
+
await this._pendingUpdate;
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
if (!force && now < this.ctryInfo.skipAttemptsUntil) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
this._pendingUpdate = (async () => {
|
|
140
|
+
try {
|
|
141
|
+
const url = this.configApiEndpoint;
|
|
142
|
+
try {
|
|
143
|
+
const response = await fetch(url, {
|
|
144
|
+
method: 'GET',
|
|
145
|
+
cache: 'no-cache',
|
|
146
|
+
credentials: 'omit',
|
|
147
|
+
});
|
|
148
|
+
if (!response.ok) {
|
|
149
|
+
throw new Error(`Failed to reach ${url}: ${response.statusText}`);
|
|
150
|
+
}
|
|
151
|
+
const { location } = await response.json();
|
|
152
|
+
const cooldown = randBetween(
|
|
153
|
+
this.cacheExpiration.min,
|
|
154
|
+
this.cacheExpiration.max,
|
|
155
|
+
);
|
|
156
|
+
this.ctryInfo = this._validateCtryInfo(
|
|
157
|
+
{
|
|
158
|
+
dbVersion: DB_VERSION,
|
|
159
|
+
unsafeCtryFromApi: location,
|
|
160
|
+
safeCtry: this._sanitizeCountry(location),
|
|
161
|
+
lastSuccessAt: now,
|
|
162
|
+
lastAttemptAt: now,
|
|
163
|
+
skipAttemptsUntil: now + cooldown,
|
|
164
|
+
failedAttemptsInARows: 0,
|
|
165
|
+
},
|
|
166
|
+
now,
|
|
167
|
+
);
|
|
168
|
+
logger.debug('Updated country information:', this.ctryInfo);
|
|
169
|
+
} catch (e) {
|
|
170
|
+
const failedAttemptsInARows = this.ctryInfo.failedAttemptsInARows + 1;
|
|
171
|
+
const avgCooldown = failedAttemptsInARows * (30 * SECOND);
|
|
172
|
+
const noisyCooldown = randBetween(
|
|
173
|
+
avgCooldown / 1.5,
|
|
174
|
+
1.5 * avgCooldown,
|
|
175
|
+
);
|
|
176
|
+
const cooldown = clamp({
|
|
177
|
+
value: noisyCooldown,
|
|
178
|
+
min: 3 * SECOND,
|
|
179
|
+
max: 3 * DAY,
|
|
180
|
+
});
|
|
181
|
+
this.ctryInfo = {
|
|
182
|
+
...this.ctryInfo,
|
|
183
|
+
failedAttemptsInARows,
|
|
184
|
+
skipAttemptsUntil: now + cooldown,
|
|
185
|
+
};
|
|
186
|
+
logger.warn(
|
|
187
|
+
'Failed to update country. Cooldown until:',
|
|
188
|
+
new Date(this.ctryInfo.skipAttemptsUntil),
|
|
189
|
+
e,
|
|
190
|
+
);
|
|
191
|
+
throw e;
|
|
192
|
+
}
|
|
193
|
+
} finally {
|
|
194
|
+
await this._syncCacheToDisk();
|
|
195
|
+
this._pendingUpdate = null;
|
|
196
|
+
}
|
|
197
|
+
})();
|
|
198
|
+
await this._pendingUpdate;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
_nonBlockingUpdateInBackground(now = Date.now()) {
|
|
202
|
+
this.update({ now }).catch((e) => {
|
|
203
|
+
logger.debug(
|
|
204
|
+
`Unable to update country (${e}). Continuing with existing settings:`,
|
|
205
|
+
this.ctryInfo,
|
|
206
|
+
);
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async _syncCacheToDisk() {
|
|
211
|
+
try {
|
|
212
|
+
await this.storage.set(this.storageKey, this.ctryInfo);
|
|
213
|
+
} catch (e) {
|
|
214
|
+
logger.error('Unable to update cache:', this.storageKey, e);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* As long as there are enough other users, revealing the country
|
|
220
|
+
* will not compromise anonymity. Only if the user base is too low
|
|
221
|
+
* (e.g., Liechtenstein), we have to be careful. In that case,
|
|
222
|
+
* do not reveal the country, otherwise fingerprinting attacks
|
|
223
|
+
* could be possible.
|
|
224
|
+
*
|
|
225
|
+
* The server side will already hide countries with few users.
|
|
226
|
+
* But as the expected number of users varies between products,
|
|
227
|
+
* the client can additionally filter the list further (the
|
|
228
|
+
* information for it is provided by the config).
|
|
229
|
+
*
|
|
230
|
+
* Including the unmodified country (without sanitizing) in messages,
|
|
231
|
+
* is also not recommended, since it would allow fingerprinting attacks
|
|
232
|
+
* should an attacker get control of the server: The attacker could modify
|
|
233
|
+
* the API to return unique identifiers instead of the country. If the
|
|
234
|
+
* client would include these ID in messages, the server could then link
|
|
235
|
+
* messages from the same user.
|
|
236
|
+
*/
|
|
237
|
+
_sanitizeCountry(ctry) {
|
|
238
|
+
return this.allowedCountryCodes.includes(ctry) ? ctry : '--';
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Runs some sanity checks on the data. You can use it to reject corrupted
|
|
243
|
+
* data that has been fetch from the API, or has been loaded from the storage.
|
|
244
|
+
*
|
|
245
|
+
* If this function throws, the new value will be dropped. If available,
|
|
246
|
+
* the client will continue to the last good value; otherwise, it will
|
|
247
|
+
* fallback to "--".
|
|
248
|
+
*/
|
|
249
|
+
_validateCtryInfo(ctryInfo, now) {
|
|
250
|
+
const {
|
|
251
|
+
dbVersion,
|
|
252
|
+
unsafeCtryFromApi,
|
|
253
|
+
safeCtry,
|
|
254
|
+
lastSuccessAt,
|
|
255
|
+
lastAttemptAt,
|
|
256
|
+
skipAttemptsUntil,
|
|
257
|
+
} = ctryInfo;
|
|
258
|
+
|
|
259
|
+
if (dbVersion !== DB_VERSION) {
|
|
260
|
+
throw new Error(
|
|
261
|
+
`Data schema changed (expected: ${DB_VERSION}, but got ${dbVersion})`,
|
|
262
|
+
);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// Normally countries are something like "us", "de", "fr".
|
|
266
|
+
// Clients should refuse to store long strings. Though client-side
|
|
267
|
+
// filtering through the ALLOWED_COUNTRY_CODES list will prevent
|
|
268
|
+
// fingerprinting attacks from a malicious server, rejecting still
|
|
269
|
+
// makes sense; most likely it is a bug in the server and
|
|
270
|
+
// continuing with the previous confirmed value is arguably better.
|
|
271
|
+
const checkCtry = (ctry) => {
|
|
272
|
+
if (typeof ctry !== 'string' || !ctry || ctry.length > 4) {
|
|
273
|
+
const msg = `Rejecting data with corrupted country: ${ctry}`;
|
|
274
|
+
logger.warn(msg, ctryInfo);
|
|
275
|
+
throw new Error(msg);
|
|
276
|
+
}
|
|
277
|
+
};
|
|
278
|
+
checkCtry(unsafeCtryFromApi);
|
|
279
|
+
checkCtry(safeCtry);
|
|
280
|
+
|
|
281
|
+
// If we see timestamps that are in the future, or the
|
|
282
|
+
// cooldowns that will prevent updates for months,
|
|
283
|
+
// we should drop the data to force a reset.
|
|
284
|
+
if (
|
|
285
|
+
lastSuccessAt > now + 5 * MINUTE ||
|
|
286
|
+
lastAttemptAt > now + 5 * MINUTE ||
|
|
287
|
+
skipAttemptsUntil > now + 90 * DAY
|
|
288
|
+
) {
|
|
289
|
+
const msg = 'Rejecting data with corrupted timestamps';
|
|
290
|
+
logger.warn(msg, ctryInfo);
|
|
291
|
+
throw new Error(msg);
|
|
292
|
+
}
|
|
293
|
+
return ctryInfo;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
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
|
+
// Source: https://github.com/bryc/code/blob/6ea6fb59526d6e24d815cb076434868bbd5b8892/jshash/experimental/cyrb53.js#L7C1-L19C3
|
|
13
|
+
/*
|
|
14
|
+
cyrb53 (c) 2018 bryc (github.com/bryc)
|
|
15
|
+
License: Public domain. Attribution appreciated.
|
|
16
|
+
A fast and simple 53-bit string hash function with decent collision resistance.
|
|
17
|
+
Largely inspired by MurmurHash2/3, but with a focus on speed/simplicity.
|
|
18
|
+
*/
|
|
19
|
+
export default function cyrb53(str, seed = 0) {
|
|
20
|
+
let h1 = 0xdeadbeef ^ seed,
|
|
21
|
+
h2 = 0x41c6ce57 ^ seed;
|
|
22
|
+
for (let i = 0, ch; i < str.length; i++) {
|
|
23
|
+
ch = str.charCodeAt(i);
|
|
24
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
25
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
26
|
+
}
|
|
27
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507);
|
|
28
|
+
h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
29
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507);
|
|
30
|
+
h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
31
|
+
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
32
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
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
|
+
/**
|
|
13
|
+
* Note the MDN warning:
|
|
14
|
+
* > SHA-1 is now considered vulnerable and should not be used for cryptographic applications.
|
|
15
|
+
*
|
|
16
|
+
* So, why do we use it? The quorum services uses it to convert URLs (or other strings) to a
|
|
17
|
+
* hard-to-reverse, fixed-length digest. sha1 should be still difficult enough to reverse
|
|
18
|
+
* (in practice, going the other way and enumerating URLs should be easier); collision attacks
|
|
19
|
+
* are also unlikely, since to find a collision, an attacker would first need to guess the hash
|
|
20
|
+
* of the URL (or string). But when knowing already the cleartext URL (or its hash), then it
|
|
21
|
+
* can be directly use to trick clients into sharing them.
|
|
22
|
+
*
|
|
23
|
+
* Still, migration the procotol (phasing out sha1) would make sense, especially to modern, faster
|
|
24
|
+
* algorithms (e.g Blake2, Blake3). Unfortunately, the browser support is still lacking; switching
|
|
25
|
+
* instead to widely supported successors like SHA-256 does not seem worth it (having also the
|
|
26
|
+
* drawback of needing more space (not a show-stopper, but a concern on the server side).
|
|
27
|
+
*/
|
|
28
|
+
export async function sha1(str) {
|
|
29
|
+
const dataUint8 = new TextEncoder().encode(str);
|
|
30
|
+
const hashBuffer = await crypto.subtle.digest('SHA-1', dataUint8);
|
|
31
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
32
|
+
return hashArray.map((b) => b.toString(16).padStart(2, '0')).join('');
|
|
33
|
+
}
|