@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,189 @@
|
|
|
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.js';
|
|
13
|
+
|
|
14
|
+
const SECOND = 1000;
|
|
15
|
+
const MINUTE = 60 * SECOND;
|
|
16
|
+
const HOUR = 60 * MINUTE;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* From time to time, we need to iterate through all keys
|
|
20
|
+
* to actively remove expired ones.
|
|
21
|
+
*/
|
|
22
|
+
const DEFAULT_PRUNE_INTERVAL = 6 * HOUR;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* This should be so high that under normal operations
|
|
26
|
+
* it must be impossible to reach.
|
|
27
|
+
*/
|
|
28
|
+
const THRESHOLD_TO_RESET_ALL_KEYS = 50000;
|
|
29
|
+
|
|
30
|
+
function isExpired(ts) {
|
|
31
|
+
return Date.now() >= ts;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export default class PersistedHashes {
|
|
35
|
+
constructor({ storage, storageKey }) {
|
|
36
|
+
this.storage = storage;
|
|
37
|
+
this.storageKey = storageKey;
|
|
38
|
+
this.entries = new Map();
|
|
39
|
+
this.nextPrune = 0;
|
|
40
|
+
|
|
41
|
+
this._pending = null;
|
|
42
|
+
this._dirty = false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async _ready() {
|
|
46
|
+
if (!this._pending) {
|
|
47
|
+
this._pending = this._loadFromDisk().catch((e) => {
|
|
48
|
+
logger.info(
|
|
49
|
+
'Could not restore previous state. It is normal to see this message on a new profile.',
|
|
50
|
+
e,
|
|
51
|
+
);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
await this._pending;
|
|
55
|
+
this._expireOldEntries();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async has(key) {
|
|
59
|
+
await this._ready();
|
|
60
|
+
|
|
61
|
+
const value = this.entries.get(key);
|
|
62
|
+
if (!value) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
if (isExpired(value)) {
|
|
66
|
+
this.entries.delete(key);
|
|
67
|
+
this._dirty = true;
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async add(key, expireAt) {
|
|
74
|
+
if (!Number.isInteger(expireAt)) {
|
|
75
|
+
throw new Error(`Bad expiredAt timestamp: ${expireAt}`);
|
|
76
|
+
}
|
|
77
|
+
await this._ready();
|
|
78
|
+
|
|
79
|
+
const oldMapping = this.entries.get(key);
|
|
80
|
+
if (oldMapping && !isExpired(oldMapping)) {
|
|
81
|
+
// key did already exist
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
// This case should never be reached. It indicates that we
|
|
86
|
+
// either write far more hashes then expected, or that we
|
|
87
|
+
// fail to clean them up. If the map gets too big, it will start to
|
|
88
|
+
// slow down the application. To protect against that possibility,
|
|
89
|
+
// expire all previous keys.
|
|
90
|
+
if (this.entries.size >= THRESHOLD_TO_RESET_ALL_KEYS) {
|
|
91
|
+
logger.error(
|
|
92
|
+
'The hashes on the profile ran full. Purging now to prevent performance impacts.',
|
|
93
|
+
);
|
|
94
|
+
this.entries.clear();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
this.entries.set(key, expireAt);
|
|
98
|
+
this._dirty = true;
|
|
99
|
+
|
|
100
|
+
// Be more aggressive with flushing add operation, which is the
|
|
101
|
+
// critical operation. In contrast, lost delete options are typically
|
|
102
|
+
// harmless as they can be later retried without information loss.
|
|
103
|
+
setTimeout(() => {
|
|
104
|
+
this.flush();
|
|
105
|
+
}, 0);
|
|
106
|
+
|
|
107
|
+
// key did not exist before
|
|
108
|
+
return true;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
async delete(key) {
|
|
112
|
+
await this._ready();
|
|
113
|
+
|
|
114
|
+
if (this.entries.delete(key)) {
|
|
115
|
+
this._dirty = true;
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async clear() {
|
|
122
|
+
await this._ready();
|
|
123
|
+
|
|
124
|
+
if (this.entries.size > 0) {
|
|
125
|
+
this.entries.clear();
|
|
126
|
+
this._dirty = true;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
async flush() {
|
|
131
|
+
await this._ready();
|
|
132
|
+
|
|
133
|
+
this._pending = this._pending
|
|
134
|
+
.then(async () => {
|
|
135
|
+
if (this._dirty) {
|
|
136
|
+
await this._persist();
|
|
137
|
+
}
|
|
138
|
+
})
|
|
139
|
+
.catch(() => {});
|
|
140
|
+
await this._pending;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async _loadFromDisk() {
|
|
144
|
+
const values = await this.storage.get(this.storageKey);
|
|
145
|
+
if (values) {
|
|
146
|
+
const { nextPrune, hashes } = values;
|
|
147
|
+
if (!Number.isInteger(nextPrune)) {
|
|
148
|
+
throw new Error(`Corrupted nextPrune: ${nextPrune}`);
|
|
149
|
+
}
|
|
150
|
+
if (!Array.isArray(hashes)) {
|
|
151
|
+
throw new Error(`Corrupted hashes: ${hashes}`);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
this.entries = new Map(hashes);
|
|
155
|
+
this.nextPrune = nextPrune;
|
|
156
|
+
this._dirty = false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async _persist() {
|
|
161
|
+
this._dirty = false;
|
|
162
|
+
await this.storage.set(this.storageKey, {
|
|
163
|
+
hashes: [...this.entries],
|
|
164
|
+
nextPrune: this.nextPrune,
|
|
165
|
+
version: 1,
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
_expireOldEntries() {
|
|
170
|
+
const now = Date.now();
|
|
171
|
+
if (now >= this.nextPrune) {
|
|
172
|
+
const oldSize = this.entries.size;
|
|
173
|
+
this.entries = new Map(
|
|
174
|
+
[...this.entries].filter(([, expireAt]) => expireAt > now),
|
|
175
|
+
);
|
|
176
|
+
logger.info(
|
|
177
|
+
'cleanup:',
|
|
178
|
+
oldSize - this.entries.size,
|
|
179
|
+
'hashes have expired',
|
|
180
|
+
);
|
|
181
|
+
this.nextPrune = now + DEFAULT_PRUNE_INTERVAL;
|
|
182
|
+
this._dirty = true;
|
|
183
|
+
|
|
184
|
+
setTimeout(() => {
|
|
185
|
+
this.flush();
|
|
186
|
+
}, 0);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
@@ -0,0 +1,327 @@
|
|
|
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 SeqExecutor from './seq-executor';
|
|
14
|
+
import { sha1 } from './digest';
|
|
15
|
+
import SelfChecks from './self-check';
|
|
16
|
+
|
|
17
|
+
const SECOND = 1000;
|
|
18
|
+
const MINUTE = 60 * SECOND;
|
|
19
|
+
const HOUR = 60 * MINUTE;
|
|
20
|
+
|
|
21
|
+
export default class QuorumChecker {
|
|
22
|
+
constructor({ config, storage, storageKey, bloomFilter, communication }) {
|
|
23
|
+
this.storage = storage;
|
|
24
|
+
this.storageKey = storageKey;
|
|
25
|
+
this.bloomFilter = bloomFilter;
|
|
26
|
+
this.communication = communication;
|
|
27
|
+
if (config.SAFE_QUORUM_CONFIG_ENDPOINT) {
|
|
28
|
+
this.quorumConfigEndpoint = config.SAFE_QUORUM_CONFIG_ENDPOINT;
|
|
29
|
+
} else {
|
|
30
|
+
logger.warn(
|
|
31
|
+
'Quorum endpoints are not configured.',
|
|
32
|
+
'Quorum updates will be skipped and all values will be considered to have not reached quorum.',
|
|
33
|
+
);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// TODO: is two hours a reasonable trade-off?
|
|
37
|
+
this.configTTL = 2 * HOUR;
|
|
38
|
+
|
|
39
|
+
this._configUpdateLock = new SeqExecutor();
|
|
40
|
+
this._finishedInitialSyncWithDisk = false;
|
|
41
|
+
|
|
42
|
+
// start with a valid, but outdated config
|
|
43
|
+
this._persistedState = {
|
|
44
|
+
lastUpdated: 0, // Unix epoch
|
|
45
|
+
bucket: 0, // 0-255
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// This is an imperfect migitation for an edge case:
|
|
49
|
+
//
|
|
50
|
+
// If the client votes but is unable to persist the fact that it voted,
|
|
51
|
+
// it may happen that eventually a value that only the client knows
|
|
52
|
+
// reaches quorum. It may lead to the secret getting shared. To defend
|
|
53
|
+
// against that, we can locally mark the key (in memory only) and let
|
|
54
|
+
// all quorum requests return false. That should provide some limited
|
|
55
|
+
// protection, since the typical scenario is to increment, followed by
|
|
56
|
+
// a quorum check.
|
|
57
|
+
//
|
|
58
|
+
// Still, it is important to remember that it is an edge case:
|
|
59
|
+
// if the extension cannot write to disk, it is likely that more
|
|
60
|
+
// parts will be broken. In terms of unintentionally leaking data,
|
|
61
|
+
// that should, however, help. If it is unable to persist state,
|
|
62
|
+
// data aggregation should also fail; thus, it should not even
|
|
63
|
+
// reach the quorum check.
|
|
64
|
+
this._badKeys = new Set();
|
|
65
|
+
|
|
66
|
+
this._stats = {
|
|
67
|
+
incQuorum: {
|
|
68
|
+
attempts: 0,
|
|
69
|
+
alreadyVoted: 0,
|
|
70
|
+
success: 0,
|
|
71
|
+
skipped: 0,
|
|
72
|
+
errors: 0,
|
|
73
|
+
},
|
|
74
|
+
checkQuorum: {
|
|
75
|
+
attempts: 0,
|
|
76
|
+
success: 0,
|
|
77
|
+
errors: 0,
|
|
78
|
+
results: {
|
|
79
|
+
yes: 0,
|
|
80
|
+
no: 0,
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
config: {
|
|
84
|
+
attempts: 0,
|
|
85
|
+
success: 0,
|
|
86
|
+
errors: 0,
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
async sendQuorumIncrement({ text, now = Date.now() } = {}) {
|
|
92
|
+
this._stats.incQuorum.attempts += 1;
|
|
93
|
+
try {
|
|
94
|
+
const quorumKey = `[incQuorum]|${text}`;
|
|
95
|
+
|
|
96
|
+
// If we already voted but failed to update the state, we should
|
|
97
|
+
// not vote again. Instead, we can try to write again; but if it
|
|
98
|
+
// fails again, count it as an error since it can be safely retried.
|
|
99
|
+
if (this._badKeys.has(text)) {
|
|
100
|
+
try {
|
|
101
|
+
await this.bloomFilter.add(quorumKey);
|
|
102
|
+
logger.debug('Successfully cleared one "badKey" entry');
|
|
103
|
+
this._badKeys.delete(text);
|
|
104
|
+
} catch (e) {
|
|
105
|
+
logger.error('Unable to update bloom filter (when retrying)', e);
|
|
106
|
+
}
|
|
107
|
+
this._stats.incQuorum.alreadyVoted += 1;
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (await this.bloomFilter.mightContain(quorumKey)) {
|
|
112
|
+
this._stats.incQuorum.alreadyVoted += 1;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
await this.updateQuorumConfig();
|
|
116
|
+
if (this._isReadyToSend(now)) {
|
|
117
|
+
const digest = await sha1(text);
|
|
118
|
+
const payload = `?hu=${digest}&oc=${this._persistedState.bucket}`;
|
|
119
|
+
const response = await this.communication.sendInstant({
|
|
120
|
+
action: 'safe-browsing-quorum',
|
|
121
|
+
path: 'incrquorum',
|
|
122
|
+
payload,
|
|
123
|
+
method: 'GET',
|
|
124
|
+
});
|
|
125
|
+
if (!response.ok) {
|
|
126
|
+
throw new Error(
|
|
127
|
+
`Failed to increment quorum (${response.statusText})`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
this._stats.incQuorum.success += 1;
|
|
131
|
+
|
|
132
|
+
// Note that the order is subtle here. Moving the update of the bloom
|
|
133
|
+
// filter below the network request that increments quorum looks like
|
|
134
|
+
// better than the other way around. Network requests can always fail,
|
|
135
|
+
// so being able to recover is more relevant. In contrast, updating the
|
|
136
|
+
// bloom filter is a local write operation, which should be unlikely
|
|
137
|
+
// to fail; especially, since there was a successful read before.
|
|
138
|
+
//
|
|
139
|
+
// If a client voted and failed to update the bloom filter, it may end
|
|
140
|
+
// up voting multiple times. If it changes buckets in-between, an URL
|
|
141
|
+
// may eventually reach quorum.
|
|
142
|
+
try {
|
|
143
|
+
await this.bloomFilter.add(quorumKey);
|
|
144
|
+
} catch (e) {
|
|
145
|
+
logger.error(
|
|
146
|
+
'Failed to update bloom filters (perhaps the storage is broken?). Might lead to double-voting.',
|
|
147
|
+
e,
|
|
148
|
+
);
|
|
149
|
+
this._badKeys.add(text);
|
|
150
|
+
}
|
|
151
|
+
} else {
|
|
152
|
+
logger.warn('Not contributing to quorum since our config is outdated');
|
|
153
|
+
this._stats.incQuorum.skipped += 1;
|
|
154
|
+
}
|
|
155
|
+
} catch (e) {
|
|
156
|
+
logger.error('Failed to increment quorum', e);
|
|
157
|
+
this._stats.incQuorum.errors += 1;
|
|
158
|
+
throw e;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async checkQuorumConsent({ text }) {
|
|
163
|
+
this._stats.checkQuorum.attempts += 1;
|
|
164
|
+
try {
|
|
165
|
+
if (this._badKeys.has(text)) {
|
|
166
|
+
logger.warn(
|
|
167
|
+
'Refusing to check quorum for a key that has been marked as bad',
|
|
168
|
+
);
|
|
169
|
+
this._stats.checkQuorum.errors += 1;
|
|
170
|
+
return false;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const digest = await sha1(text);
|
|
174
|
+
const payload = `?hu=${digest}`;
|
|
175
|
+
|
|
176
|
+
const response = await this.communication.sendInstant({
|
|
177
|
+
action: 'safe-browsing-quorum',
|
|
178
|
+
path: 'checkquorum',
|
|
179
|
+
payload,
|
|
180
|
+
method: 'GET',
|
|
181
|
+
});
|
|
182
|
+
if (!response.ok) {
|
|
183
|
+
throw new Error(`Failed to get quorum (${response.statusText})`);
|
|
184
|
+
}
|
|
185
|
+
const { result } = await response.json();
|
|
186
|
+
if (result === true) {
|
|
187
|
+
this._stats.checkQuorum.results.yes += 1;
|
|
188
|
+
} else if (result === false) {
|
|
189
|
+
this._stats.checkQuorum.results.no += 1;
|
|
190
|
+
} else {
|
|
191
|
+
throw new Error('Unexpected result');
|
|
192
|
+
}
|
|
193
|
+
this._stats.checkQuorum.success += 1;
|
|
194
|
+
return result;
|
|
195
|
+
} catch (e) {
|
|
196
|
+
logger.error('Failed to check quorum consent', e);
|
|
197
|
+
this._stats.checkQuorum.errors += 1;
|
|
198
|
+
throw e;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async updateQuorumConfig({ force = false, now = Date.now() } = {}) {
|
|
203
|
+
if (!this.quorumConfigEndpoint) {
|
|
204
|
+
logger.info('Quorum server not configured. Update skipped.');
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
await this._configUpdateLock.run(async () => {
|
|
208
|
+
if (!this._finishedInitialSyncWithDisk) {
|
|
209
|
+
try {
|
|
210
|
+
await this._restorePersistedState(now);
|
|
211
|
+
} catch (e) {
|
|
212
|
+
logger.warn(
|
|
213
|
+
'Failed to restore the quorum config from disk.',
|
|
214
|
+
'The configuration will be reloaded from the the server...',
|
|
215
|
+
e,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
this._finishedInitialSyncWithDisk = true;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
if (!this._isReadyToSend(now) || force) {
|
|
222
|
+
await this._loadFromServer();
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
_isReadyToSend(now) {
|
|
228
|
+
return (
|
|
229
|
+
this.quorumConfigEndpoint &&
|
|
230
|
+
this._persistedState.lastUpdated + this.configTTL >= now
|
|
231
|
+
);
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
async _restorePersistedState(now) {
|
|
235
|
+
let persistedState = await this.storage.get(this.storageKey);
|
|
236
|
+
if (!persistedState) {
|
|
237
|
+
logger.info(
|
|
238
|
+
'Quorum config config does not exist. This should only happen on the first time the extension is started.',
|
|
239
|
+
);
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
this._ensureValidConfig(persistedState);
|
|
243
|
+
if (persistedState.lastUpdated > now + 5 * MINUTE) {
|
|
244
|
+
logger.warn(
|
|
245
|
+
'The timestamps in the quorum config show indications that the system clock was off. Discarding the config to force a resync:',
|
|
246
|
+
persistedState,
|
|
247
|
+
);
|
|
248
|
+
return;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
this._persistedState = persistedState;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
async _loadFromServer() {
|
|
255
|
+
this._stats.config.attempts += 1;
|
|
256
|
+
const url = this.quorumConfigEndpoint;
|
|
257
|
+
try {
|
|
258
|
+
logger.debug('Fetching quorum config from', url);
|
|
259
|
+
const req = await fetch(url);
|
|
260
|
+
if (!req.ok) {
|
|
261
|
+
throw new Error(req.statusText);
|
|
262
|
+
}
|
|
263
|
+
const { oc: bucket } = await req.json();
|
|
264
|
+
const config = {
|
|
265
|
+
lastUpdated: Date.now(),
|
|
266
|
+
bucket,
|
|
267
|
+
};
|
|
268
|
+
this._ensureValidConfig(config);
|
|
269
|
+
this._stats.config.success += 1;
|
|
270
|
+
if (bucket !== this._persistedState.bucket) {
|
|
271
|
+
logger.debug(
|
|
272
|
+
'Quorum bucket changed from',
|
|
273
|
+
this._persistedState.bucket,
|
|
274
|
+
'to',
|
|
275
|
+
bucket,
|
|
276
|
+
);
|
|
277
|
+
}
|
|
278
|
+
this._persistedState = config;
|
|
279
|
+
} catch (e) {
|
|
280
|
+
logger.error(`Failed to update quorum config (from url=${url})`, e);
|
|
281
|
+
this._stats.config.errors += 1;
|
|
282
|
+
throw e;
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
try {
|
|
286
|
+
await this.storage.set(this.storageKey, this._persistedState);
|
|
287
|
+
} catch (e) {
|
|
288
|
+
logger.warn(
|
|
289
|
+
'Failed to cache quorum configuration (it is safe to continue, but it will be refetched on the next extension start',
|
|
290
|
+
e,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
_ensureValidConfig(state) {
|
|
296
|
+
if (!state) {
|
|
297
|
+
throw new Error('Missing state');
|
|
298
|
+
}
|
|
299
|
+
const { lastUpdated, bucket } = state;
|
|
300
|
+
if (!Number.isInteger(lastUpdated) || lastUpdated < 0) {
|
|
301
|
+
throw new Error(
|
|
302
|
+
`Bad timestamp in quorum config (lastUpdated=${lastUpdated})`,
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
if (!Number.isInteger(bucket) || bucket < 0 || bucket >= 256) {
|
|
306
|
+
throw new Error(`Bad bucket in quorum config (bucket=${bucket})`);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
async selfChecks(check = new SelfChecks()) {
|
|
311
|
+
if (this._badKeys.size > 0) {
|
|
312
|
+
check.warn('badKeys detected');
|
|
313
|
+
}
|
|
314
|
+
for (const op of ['incQuorum', 'checkQuorum', 'config']) {
|
|
315
|
+
const { errors, success } = this._stats[op];
|
|
316
|
+
if (errors > 0) {
|
|
317
|
+
const ratio = errors / (errors + success);
|
|
318
|
+
if (ratio > 0.2) {
|
|
319
|
+
check.warn(`Detected high error rates in "${op}" operation`, {
|
|
320
|
+
[op]: this._stats[op],
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
return check;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
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-disable no-bitwise */
|
|
13
|
+
|
|
14
|
+
export default function random() {
|
|
15
|
+
const values = crypto.getRandomValues(new Uint32Array(2));
|
|
16
|
+
return (2 ** 32 * (values[0] & 0x1fffff) + values[1]) / 2 ** 53;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Returns a floating point (not integer!) that lies between the boundaries.
|
|
21
|
+
*/
|
|
22
|
+
export function randomBetween(minInclusive, maxInclusive) {
|
|
23
|
+
if (!Number.isFinite(minInclusive)) {
|
|
24
|
+
throw new Error(`minInclusive=${minInclusive} must be a finite number`);
|
|
25
|
+
}
|
|
26
|
+
if (!Number.isFinite(maxInclusive)) {
|
|
27
|
+
throw new Error(`maxInclusive=${maxInclusive} must be a finite number`);
|
|
28
|
+
}
|
|
29
|
+
if (maxInclusive < minInclusive) {
|
|
30
|
+
throw new Error(
|
|
31
|
+
`maxInclusive=${maxInclusive} must be at least minInclusive=${minInclusive}`,
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
const diff = maxInclusive - minInclusive;
|
|
35
|
+
if (diff === 0) {
|
|
36
|
+
return minInclusive;
|
|
37
|
+
}
|
|
38
|
+
return minInclusive + random() * diff;
|
|
39
|
+
}
|