@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,76 @@
|
|
|
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 ServerPublicKeyAccessor from './server-public-key-accessor.js';
|
|
13
|
+
import ProxiedHttp from './proxied-http.js';
|
|
14
|
+
import { sortObjectKeys } from './utils.js';
|
|
15
|
+
import { InvalidMessageError } from './errors.js';
|
|
16
|
+
import { TrustedClock } from './trusted-clock.js';
|
|
17
|
+
|
|
18
|
+
export default class AnonymousCommunication {
|
|
19
|
+
constructor({ config, connectDatabase }) {
|
|
20
|
+
this.cacheDatabase = connectDatabase('cache');
|
|
21
|
+
this.serverPublicKeyAccessor = new ServerPublicKeyAccessor({
|
|
22
|
+
config,
|
|
23
|
+
database: this.cacheDatabase,
|
|
24
|
+
});
|
|
25
|
+
this.config = config;
|
|
26
|
+
if (!config.CHANNEL) {
|
|
27
|
+
throw new Error('CHANNEL is missing on the config object');
|
|
28
|
+
}
|
|
29
|
+
this.proxiedHttp = new ProxiedHttp(config, this.serverPublicKeyAccessor);
|
|
30
|
+
this.trustedClock = new TrustedClock();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
async send(msg) {
|
|
34
|
+
if (!msg || typeof msg !== 'object') {
|
|
35
|
+
throw new InvalidMessageError('Input message must be an object');
|
|
36
|
+
}
|
|
37
|
+
if (!msg.action) {
|
|
38
|
+
throw new InvalidMessageError('Mandatory field "action" is missing');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Add meta fields (channel, ts) if absent.
|
|
42
|
+
//
|
|
43
|
+
// Implementation details:
|
|
44
|
+
// * Avoid accessing the clock if it is not needed, as the operation may
|
|
45
|
+
// fail in rare situations if the clock is out of sync
|
|
46
|
+
// * Sorting keys prevents that details about how the message was
|
|
47
|
+
// constructed leaks through the JSON representation to the server
|
|
48
|
+
const ts = msg.ts || this.trustedClock.getTimeAsYYYYMMDD();
|
|
49
|
+
const fullMessage = {
|
|
50
|
+
channel: this.config.CHANNEL,
|
|
51
|
+
ts,
|
|
52
|
+
...msg,
|
|
53
|
+
};
|
|
54
|
+
return this.proxiedHttp.send({
|
|
55
|
+
body: JSON.stringify(sortObjectKeys(fullMessage)),
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async sendInstant({ action, path = '', payload = '', method = 'POST' }) {
|
|
60
|
+
if (!action) {
|
|
61
|
+
throw new InvalidMessageError('Mandatory field "action" is missing');
|
|
62
|
+
}
|
|
63
|
+
if (typeof path !== 'string') {
|
|
64
|
+
throw new InvalidMessageError('"path" must be a string');
|
|
65
|
+
}
|
|
66
|
+
if (typeof payload !== 'string') {
|
|
67
|
+
throw new InvalidMessageError('"payload" must be a string');
|
|
68
|
+
}
|
|
69
|
+
if (!method || typeof method !== 'string') {
|
|
70
|
+
throw new InvalidMessageError('"method" must be a string');
|
|
71
|
+
}
|
|
72
|
+
return this.proxiedHttp.send({
|
|
73
|
+
body: JSON.stringify({ action, path, payload, method }),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
@@ -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
|
+
const noop = () => {};
|
|
13
|
+
|
|
14
|
+
class Logger {
|
|
15
|
+
static get(name) {
|
|
16
|
+
return new Logger(name);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
constructor(name) {
|
|
20
|
+
this.prefix = `WTM [${name}]`;
|
|
21
|
+
this.enable();
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
enable() {
|
|
25
|
+
this.debug = console.debug.bind(console, this.prefix);
|
|
26
|
+
this.log = console.log.bind(console, this.prefix);
|
|
27
|
+
this.info = console.info.bind(console, this.prefix);
|
|
28
|
+
this.warn = console.warn.bind(console, this.prefix);
|
|
29
|
+
this.error = console.error.bind(console, this.prefix);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
disable() {
|
|
33
|
+
this.debug = noop;
|
|
34
|
+
this.log = noop;
|
|
35
|
+
this.info = noop;
|
|
36
|
+
this.warn = noop;
|
|
37
|
+
this.error = noop;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export default Logger.get('anonymous-communication');
|
|
@@ -0,0 +1,60 @@
|
|
|
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
|
+
import { inflate, deflate } from './zlib.js';
|
|
15
|
+
import { TooBigMsgError } from './errors.js';
|
|
16
|
+
|
|
17
|
+
// https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
|
|
18
|
+
function nextPow2(_v) {
|
|
19
|
+
let v = _v | 0;
|
|
20
|
+
v -= 1;
|
|
21
|
+
v |= v >> 1;
|
|
22
|
+
v |= v >> 2;
|
|
23
|
+
v |= v >> 4;
|
|
24
|
+
v |= v >> 8;
|
|
25
|
+
v |= v >> 16;
|
|
26
|
+
v += 1;
|
|
27
|
+
return v;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function encodeLength(length) {
|
|
31
|
+
// We could also encode length = 32767 = (1 << 15) - 1,
|
|
32
|
+
// but since the message overhead is 2 bytes, in that case
|
|
33
|
+
// the final message would go to the 64KB bucket, which
|
|
34
|
+
// we don't want, so enforcing length < (1 << 15) - 1.
|
|
35
|
+
if (length >= (1 << 15) - 1) {
|
|
36
|
+
throw new TooBigMsgError('Message is too big');
|
|
37
|
+
}
|
|
38
|
+
return (1 << 15) | length;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function decodeLength(data) {
|
|
42
|
+
if (data < 1 << 15) {
|
|
43
|
+
throw new Error('Wrong encoded length');
|
|
44
|
+
}
|
|
45
|
+
return data & ((1 << 15) - 1);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export function encodeWithPadding(message) {
|
|
49
|
+
const compressed = deflate(message);
|
|
50
|
+
const paddedSize = Math.max(1 << 10, nextPow2(2 + compressed.length));
|
|
51
|
+
const data = new Uint8Array(paddedSize);
|
|
52
|
+
new DataView(data.buffer).setUint16(0, encodeLength(compressed.length));
|
|
53
|
+
data.set(compressed, 2);
|
|
54
|
+
return data;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export function decodeWithPadding(data) {
|
|
58
|
+
const compressedLength = decodeLength(new DataView(data.buffer).getUint16());
|
|
59
|
+
return inflate(data.slice(2, 2 + compressedLength));
|
|
60
|
+
}
|
|
@@ -0,0 +1,231 @@
|
|
|
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 { encodeWithPadding } from './padding.js';
|
|
13
|
+
import { fromBase64, toBase64, fromUTF8, toUTF8 } from './encoding.js';
|
|
14
|
+
import { inflate } from './zlib.js';
|
|
15
|
+
import { ProtocolError } from './errors.js';
|
|
16
|
+
|
|
17
|
+
const ANONYMOUS_COMMUNICATION_PROTOCOL_VERSION = 1;
|
|
18
|
+
const ANONYMOUS_COMMUNICATION_ECDH_P256_AES_128_GCM = 0xea;
|
|
19
|
+
|
|
20
|
+
async function exportKey(key) {
|
|
21
|
+
return new Uint8Array(await crypto.subtle.exportKey('raw', key));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async function sha256(data) {
|
|
25
|
+
return new Uint8Array(await crypto.subtle.digest({ name: 'SHA-256' }, data));
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function randomInt() {
|
|
29
|
+
// Consider using true random here
|
|
30
|
+
return Math.floor(Math.random() * Number.MAX_SAFE_INTEGER);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Responsible for sending WhoTracksMe message to the servers.
|
|
35
|
+
*
|
|
36
|
+
* In our context, there are two additional side-goals:
|
|
37
|
+
*
|
|
38
|
+
* 1) Anonymity: the server should NOT be able to link messages
|
|
39
|
+
* to the same client
|
|
40
|
+
* 2) Duplicate detection: the server should be able to detect it
|
|
41
|
+
* when malicious clients send duplicated messages
|
|
42
|
+
*
|
|
43
|
+
* These goals seem to be in conflict, but it can be supported through a
|
|
44
|
+
* cryptographic algorithm called "Direct Anonymous Attestation". The
|
|
45
|
+
* client can include a zero-knowledge proof that it has not sent the
|
|
46
|
+
* message before (see https://github.com/whotracksme/anonymous-credentials).
|
|
47
|
+
*
|
|
48
|
+
* (TODO: anonymous-credentials hasn't been ported to Manifest V3. It relies
|
|
49
|
+
* on pairing-based cryptography, which we implemented with web assembly
|
|
50
|
+
* in Manifest V2. Perhaps web assembly will be support in Manifest V3
|
|
51
|
+
* eventually but it is hard to predict at this point. For details, see
|
|
52
|
+
* https://bugs.chromium.org/p/chromium/issues/detail?id=1173354)
|
|
53
|
+
*
|
|
54
|
+
* When it comes to anonymity, there is still the challenge that the server
|
|
55
|
+
* could link messages by meta data from the network layer (e.g. the IP address).
|
|
56
|
+
* To prevent that, messages can be routed through a trusted-party (e.g. Tor or
|
|
57
|
+
* through third-party proxies). Since the trusted-party should neither be able to
|
|
58
|
+
* read or modify the content of the messages, each message needs to be end-to-end
|
|
59
|
+
* encrypted and techniques to defend against statistical attacks (guessing messages
|
|
60
|
+
* based on their length) should be applied as well.
|
|
61
|
+
*
|
|
62
|
+
* In our current implementation, we are using AES-128-GCM with a non-iteractive
|
|
63
|
+
* Diffie-Hellman key exchange (server keys being rotated once a key). In addition,
|
|
64
|
+
* payloads are padded to power-of-2 buckets to defend against traffic analysis.
|
|
65
|
+
*
|
|
66
|
+
* Notes:
|
|
67
|
+
* - It is important to stress that the assumption here is that messages are
|
|
68
|
+
* already free of identifiers and are safe against fingerprinting attacks.
|
|
69
|
+
* If you send messages that can be linked based on their content, then
|
|
70
|
+
* stripping network layer information will not magically make it safe.
|
|
71
|
+
* - Our goal is to follow a "privacy by design" architecture: the server should
|
|
72
|
+
* not have access to more information beside the message payload.
|
|
73
|
+
* But independent of whether the traffic is sent directly or through a
|
|
74
|
+
* trusted-party, Ghostery will not log the IP of the sender.
|
|
75
|
+
*/
|
|
76
|
+
export default class ProxiedHttp {
|
|
77
|
+
constructor(config, serverPublicKeyAccessor) {
|
|
78
|
+
this.viaProxyEndpointTemplate = config.COLLECTOR_PROXY_URL;
|
|
79
|
+
this.serverPublicKeyAccessor = serverPublicKeyAccessor;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async send({ body }) {
|
|
83
|
+
const { ciphertext, iv, secret, clientPublicKey, serverPublicKeyDate } =
|
|
84
|
+
await this.encrypt(body);
|
|
85
|
+
|
|
86
|
+
// layout:
|
|
87
|
+
// * algorithm type (1 byte)
|
|
88
|
+
// * client ECDH public key (65 bytes; the size of the key after export)
|
|
89
|
+
// * initialization vector (12 byte)
|
|
90
|
+
const encryptionHeader = new Uint8Array(1 + 65 + 12);
|
|
91
|
+
encryptionHeader[0] = ANONYMOUS_COMMUNICATION_ECDH_P256_AES_128_GCM;
|
|
92
|
+
encryptionHeader.set(clientPublicKey, 1);
|
|
93
|
+
encryptionHeader.set(iv, 1 + 65);
|
|
94
|
+
|
|
95
|
+
const headers = {
|
|
96
|
+
'Content-Type': 'application/octet-stream',
|
|
97
|
+
Version: ANONYMOUS_COMMUNICATION_PROTOCOL_VERSION.toString(),
|
|
98
|
+
Encryption: toBase64(encryptionHeader),
|
|
99
|
+
'Key-Date': serverPublicKeyDate,
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const response = await fetch(this._chooseRandomProxyUrl(), {
|
|
103
|
+
method: 'POST',
|
|
104
|
+
headers,
|
|
105
|
+
credentials: 'omit',
|
|
106
|
+
cache: 'no-store',
|
|
107
|
+
redirect: 'manual',
|
|
108
|
+
body: ciphertext,
|
|
109
|
+
});
|
|
110
|
+
if (!response.ok) {
|
|
111
|
+
throw new Error(`Failed to send data (${response.statusText})`);
|
|
112
|
+
}
|
|
113
|
+
let data = new Uint8Array(await response.arrayBuffer());
|
|
114
|
+
|
|
115
|
+
const serverIV = response.headers.get('Encryption-IV');
|
|
116
|
+
if (serverIV) {
|
|
117
|
+
const decrypted = await crypto.subtle.decrypt(
|
|
118
|
+
{
|
|
119
|
+
name: 'AES-GCM',
|
|
120
|
+
iv: fromBase64(serverIV),
|
|
121
|
+
tagLength: 128,
|
|
122
|
+
},
|
|
123
|
+
secret,
|
|
124
|
+
data,
|
|
125
|
+
);
|
|
126
|
+
data = new Uint8Array(decrypted);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// Depending on the message type, we need to decompress the data.
|
|
130
|
+
// In the Anonymous Communication protocol, the type is implicitely defined:
|
|
131
|
+
// * if the data starts with 0x7B (=== '{'), it can be directly consume
|
|
132
|
+
// (e.g. fire-and-forget messages will always return '{}')
|
|
133
|
+
// * otherwise, decompress it
|
|
134
|
+
// (format: "<size: 4-byte unsigned int>:<data: "size" bytes>")
|
|
135
|
+
if (data[0] !== 0x7b) {
|
|
136
|
+
const size = new DataView(data.buffer).getUint32();
|
|
137
|
+
if (4 + size > data.length) {
|
|
138
|
+
throw new ProtocolError('Overflow in data received from the server');
|
|
139
|
+
}
|
|
140
|
+
data = inflate(data.subarray(4, 4 + size));
|
|
141
|
+
}
|
|
142
|
+
const { status, body: body_ } = JSON.parse(fromUTF8(data));
|
|
143
|
+
return new Response(body_, { status });
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
_chooseRandomProxyUrl() {
|
|
147
|
+
const MIN_PROXY_NUM = 1;
|
|
148
|
+
const MAX_PROXY_NUM = 100;
|
|
149
|
+
const NUM_PROXIES = MAX_PROXY_NUM - MIN_PROXY_NUM + 1;
|
|
150
|
+
const proxyNum = (randomInt() % NUM_PROXIES) + MIN_PROXY_NUM;
|
|
151
|
+
return this.viaProxyEndpointTemplate.replace('*', proxyNum);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async generateClientECDHKey() {
|
|
155
|
+
const { publicKey, privateKey } = await crypto.subtle.generateKey(
|
|
156
|
+
{ name: 'ECDH', namedCurve: 'P-256' },
|
|
157
|
+
true,
|
|
158
|
+
['deriveKey'],
|
|
159
|
+
);
|
|
160
|
+
return {
|
|
161
|
+
publicKey: await exportKey(publicKey),
|
|
162
|
+
privateKey,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async negotiateSecret() {
|
|
167
|
+
// Setup:
|
|
168
|
+
// 1) get the server's public key for today
|
|
169
|
+
// 2) compute a new public/private key pair
|
|
170
|
+
const [serverKey, clientKeys] = await Promise.all([
|
|
171
|
+
this.serverPublicKeyAccessor.getKey(),
|
|
172
|
+
this.generateClientECDHKey(),
|
|
173
|
+
]);
|
|
174
|
+
const { publicKey: serverPublicKey, date: serverPublicKeyDate } = serverKey;
|
|
175
|
+
const { publicKey: clientPublicKey, privateKey: clientPrivateKey } =
|
|
176
|
+
clientKeys;
|
|
177
|
+
|
|
178
|
+
// Perform ECDH to get a curve point (on P-256, which is assumed
|
|
179
|
+
// have an effective security strength of at least 128 bits).
|
|
180
|
+
// To derive the symmetric key for AES-128-GCM, first hash with
|
|
181
|
+
// SHA-256, then take 16 bytes resulting in the desired 128 bit key.
|
|
182
|
+
const derivedKey = await crypto.subtle.deriveKey(
|
|
183
|
+
{ name: 'ECDH', namedCurve: 'P-256', public: serverPublicKey },
|
|
184
|
+
clientPrivateKey,
|
|
185
|
+
{ name: 'AES-GCM', length: 256 },
|
|
186
|
+
true,
|
|
187
|
+
['encrypt', 'decrypt'],
|
|
188
|
+
);
|
|
189
|
+
const rawDerived = await exportKey(derivedKey);
|
|
190
|
+
const raw128bitKey = (await sha256(rawDerived)).subarray(0, 16);
|
|
191
|
+
const secret = await crypto.subtle.importKey(
|
|
192
|
+
'raw',
|
|
193
|
+
raw128bitKey,
|
|
194
|
+
{ name: 'AES-GCM', length: 128 },
|
|
195
|
+
false,
|
|
196
|
+
['encrypt', 'decrypt'],
|
|
197
|
+
);
|
|
198
|
+
return { secret, clientPublicKey, serverPublicKeyDate };
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* When sending through proxies, we have to create a secure channel.
|
|
203
|
+
* Like ANONYMOUS_COMMUNICATION, we use an Integrated Encryption Scheme (IES). First, exchange
|
|
204
|
+
* a symmentric key (through ECDH). Then derive a AES key and encrypt
|
|
205
|
+
* the data with AES-128-GCM.
|
|
206
|
+
*
|
|
207
|
+
* In addition, we should take counter-measures against traffic analysis.
|
|
208
|
+
* To achieve that, payloads are padded to the next power-of-2 bucket size
|
|
209
|
+
* (with a minimum size of 1K).
|
|
210
|
+
*/
|
|
211
|
+
async encrypt(plaintext) {
|
|
212
|
+
const { secret, clientPublicKey, serverPublicKeyDate } =
|
|
213
|
+
await this.negotiateSecret();
|
|
214
|
+
const iv = crypto.getRandomValues(new Uint8Array(12));
|
|
215
|
+
|
|
216
|
+
// note: we are assuming JSON messages, here
|
|
217
|
+
const unpaddedPlaintext = toUTF8(
|
|
218
|
+
typeof plaintext === 'string' ? plaintext : JSON.stringify(plaintext),
|
|
219
|
+
);
|
|
220
|
+
const data = encodeWithPadding(unpaddedPlaintext);
|
|
221
|
+
const ciphertext = new Uint8Array(
|
|
222
|
+
await crypto.subtle.encrypt(
|
|
223
|
+
{ name: 'AES-GCM', iv, tagLength: 128 },
|
|
224
|
+
secret,
|
|
225
|
+
data,
|
|
226
|
+
),
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
return { ciphertext, iv, secret, clientPublicKey, serverPublicKeyDate };
|
|
230
|
+
}
|
|
231
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
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
|
+
import { getTimeAsYYYYMMDD } from './timestamps.js';
|
|
14
|
+
import { fromBase64 } from './encoding.js';
|
|
15
|
+
|
|
16
|
+
function isYYYYMMDD(date) {
|
|
17
|
+
return typeof date === 'string' && /^[0-9]{8}$/.test(date);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export default class ServerPublicKeyAccessor {
|
|
21
|
+
constructor({ config, database }) {
|
|
22
|
+
// Note: do not go through proxies when fetching keys; otherwise,
|
|
23
|
+
// the proxy could replace it, and the key exchange would be insecure.
|
|
24
|
+
this.collectorUrl = config.COLLECTOR_DIRECT_URL;
|
|
25
|
+
this.database = database;
|
|
26
|
+
this.storageKey = 'server-ecdh-keys';
|
|
27
|
+
this._knownKeys = new Map();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
async getKey(today = getTimeAsYYYYMMDD()) {
|
|
31
|
+
if (!this._knownKeys.get(today)) {
|
|
32
|
+
if (!this._pending) {
|
|
33
|
+
this._pending = this._updateCache(today);
|
|
34
|
+
this._pending
|
|
35
|
+
.catch(() => {})
|
|
36
|
+
.then(() => {
|
|
37
|
+
this._pending = null;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
await this._pending;
|
|
41
|
+
}
|
|
42
|
+
const key = this._knownKeys.get(today);
|
|
43
|
+
if (key) {
|
|
44
|
+
return { date: today, publicKey: key.imported };
|
|
45
|
+
}
|
|
46
|
+
// did not get anything after refreshing the cache -> give up
|
|
47
|
+
throw new Error(`No server's public key was found for today=${today}`);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async _updateCache(today) {
|
|
51
|
+
// try to load from disk
|
|
52
|
+
let knownKeys;
|
|
53
|
+
try {
|
|
54
|
+
const keysFromDisk = await this.database
|
|
55
|
+
.get(this.storageKey)
|
|
56
|
+
.catch(() => null);
|
|
57
|
+
if (keysFromDisk && keysFromDisk.some(([date]) => date === today)) {
|
|
58
|
+
logger.debug('Server keys on disk are still valid');
|
|
59
|
+
knownKeys = await this.importAndVerifyPubKeys(keysFromDisk);
|
|
60
|
+
} else {
|
|
61
|
+
logger.info(
|
|
62
|
+
'Server keys on disk need to be refetched. Expected:',
|
|
63
|
+
today,
|
|
64
|
+
);
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {
|
|
67
|
+
logger.warn(
|
|
68
|
+
`Ignoring corrupted server keys (storageKey: ${this.storageKey}). Reload from network.`,
|
|
69
|
+
e,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// not found on disk or outdated -> fetch from server
|
|
74
|
+
if (!knownKeys) {
|
|
75
|
+
const url = `${this.collectorUrl}/config?fields=pubKeys`;
|
|
76
|
+
logger.info('Fetching new server public keys from', url);
|
|
77
|
+
const response = await fetch(url, {
|
|
78
|
+
method: 'GET',
|
|
79
|
+
credentials: 'omit',
|
|
80
|
+
redirect: 'manual',
|
|
81
|
+
});
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
throw new Error(
|
|
84
|
+
`Failed to get config (${response.statusText}) from url=${url}`,
|
|
85
|
+
);
|
|
86
|
+
}
|
|
87
|
+
const { pubKeys } = await response.json();
|
|
88
|
+
logger.info('Fetched server public keys:', pubKeys);
|
|
89
|
+
|
|
90
|
+
const allKeys = Object.keys(pubKeys)
|
|
91
|
+
.filter(isYYYYMMDD)
|
|
92
|
+
.map((date) => [date, fromBase64(pubKeys[date])]);
|
|
93
|
+
knownKeys = await this.importAndVerifyPubKeys(allKeys);
|
|
94
|
+
|
|
95
|
+
// update disk cache
|
|
96
|
+
try {
|
|
97
|
+
const entry = [...knownKeys].map(([date, { key }]) => [date, key]);
|
|
98
|
+
await this.database.set(this.storageKey, entry);
|
|
99
|
+
} catch (e) {
|
|
100
|
+
logger.warn('Failed to cache server keys to disk.', e);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this._knownKeys = knownKeys;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async importAndVerifyPubKeys(allKeys) {
|
|
108
|
+
return new Map(
|
|
109
|
+
await Promise.all(
|
|
110
|
+
allKeys.map(async ([date, key]) => {
|
|
111
|
+
const imported = await crypto.subtle.importKey(
|
|
112
|
+
'raw',
|
|
113
|
+
key,
|
|
114
|
+
{ name: 'ECDH', namedCurve: 'P-256' },
|
|
115
|
+
false,
|
|
116
|
+
[],
|
|
117
|
+
);
|
|
118
|
+
return [date, { key, imported }];
|
|
119
|
+
}),
|
|
120
|
+
),
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
@@ -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
|
+
/**
|
|
13
|
+
* Contains functions to generate timestamps used in WhoTracksMe messages.
|
|
14
|
+
* As a general rule, all timestamps in the context of WhoTracksMe will be in UTC.
|
|
15
|
+
*
|
|
16
|
+
* To mitigate the risk of fingerprinting based on clock drift, messages
|
|
17
|
+
* should not include high resolution timestamps, but instead should be rounded.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
export function getTrustedUtcTime() {
|
|
21
|
+
// Note: the approach that is used on Desktop to prevent messages
|
|
22
|
+
// being sent out with a wrong system clock cannot be ported
|
|
23
|
+
// as it is inheritely based on frequent wakeups.
|
|
24
|
+
//
|
|
25
|
+
// For now, assume that the system clock is accurate enough.
|
|
26
|
+
// The reason for this function is only to have all sensitive time operations
|
|
27
|
+
// at one place if we want to add some heuristics here.
|
|
28
|
+
return new Date();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getTimeAsYYYYMMDD(now) {
|
|
32
|
+
const ts = now || getTrustedUtcTime();
|
|
33
|
+
return ts
|
|
34
|
+
.toISOString()
|
|
35
|
+
.replace(/[^0-9]/g, '')
|
|
36
|
+
.slice(0, 8);
|
|
37
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
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 { ClockOutOfSync } from './errors.js';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Contains functions to generate timestamps used in WhoTracksMe messages.
|
|
16
|
+
* As a general rule, all timestamps in the context of WhoTracksMe will be in UTC.
|
|
17
|
+
*
|
|
18
|
+
* To mitigate the risk of fingerprinting based on clock drift, messages
|
|
19
|
+
* should not include high resolution timestamps, but instead should be truncated.
|
|
20
|
+
*/
|
|
21
|
+
export class TrustedClock {
|
|
22
|
+
checkTime() {
|
|
23
|
+
// TODO: Trust that the local system time is correct. On most systems,
|
|
24
|
+
// that assumption should hold. it is a non-trivial problem to improve
|
|
25
|
+
// it under Manifest V3 constraints (ideas like running a parallel
|
|
26
|
+
// clock with setTimeout/setInterval is not a solution because of the
|
|
27
|
+
// lack of persistent background page; the alarm API is too imprecise;
|
|
28
|
+
// maybe a reliable clock could be built by observing server time-stamps).
|
|
29
|
+
return {
|
|
30
|
+
inSync: true,
|
|
31
|
+
unixEpoche: Date.now(),
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
now() {
|
|
36
|
+
const { inSync, unixEpoche } = this.checkTime();
|
|
37
|
+
if (!inSync) {
|
|
38
|
+
throw new ClockOutOfSync();
|
|
39
|
+
}
|
|
40
|
+
return unixEpoche;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getTimeAsYYYYMMDD(now) {
|
|
44
|
+
const ts = now ?? this.now();
|
|
45
|
+
return new Date(ts)
|
|
46
|
+
.toISOString()
|
|
47
|
+
.replace(/[^0-9]/g, '')
|
|
48
|
+
.slice(0, 8);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
getTimeAsYYYYMMDDHH(now) {
|
|
52
|
+
const ts = now ?? this.now();
|
|
53
|
+
return new Date(ts)
|
|
54
|
+
.toISOString()
|
|
55
|
+
.replace(/[^0-9]/g, '')
|
|
56
|
+
.slice(0, 10);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
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
|
+
* Use case: when running object through JSON.stringify, it
|
|
14
|
+
* should not be distinguishable in which order the object keys
|
|
15
|
+
* where added.
|
|
16
|
+
*
|
|
17
|
+
* Note: The current implement only operates on the first level.
|
|
18
|
+
* In our context, that should be suffient, since
|
|
19
|
+
* anonymous-communication will not add nested structures.
|
|
20
|
+
* The only nested structure is the payload, which means the
|
|
21
|
+
* message creator will be responsible for the ordering.
|
|
22
|
+
*
|
|
23
|
+
* Should that become a burden, we could extend this function
|
|
24
|
+
* to recursively sort the object keys.
|
|
25
|
+
*/
|
|
26
|
+
export function sortObjectKeys(obj) {
|
|
27
|
+
return Object.fromEntries(Object.entries(obj).sort());
|
|
28
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
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 pako from 'pako';
|
|
13
|
+
|
|
14
|
+
const inflate = pako.inflate.bind(pako);
|
|
15
|
+
const deflate = pako.deflate.bind(pako);
|
|
16
|
+
|
|
17
|
+
export { inflate, deflate };
|