@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,121 @@
|
|
|
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 SUPPORTED_LOG_LEVELS = new Map([
|
|
13
|
+
['debug', 1],
|
|
14
|
+
['info', 2], // alias for 'log'
|
|
15
|
+
['log', 2],
|
|
16
|
+
['warn', 3], // alias for 'warning'
|
|
17
|
+
['warning', 3],
|
|
18
|
+
['error', 4],
|
|
19
|
+
['off', 5], // disable all logging
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
const loggers = new Set();
|
|
23
|
+
|
|
24
|
+
const noop = () => {};
|
|
25
|
+
|
|
26
|
+
let DEFAULT_LOG_LEVEL = 'log';
|
|
27
|
+
|
|
28
|
+
export class Logger {
|
|
29
|
+
static get(name, ...options) {
|
|
30
|
+
return new Logger(name, ...options);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
constructor(prefix, { level = DEFAULT_LOG_LEVEL } = {}) {
|
|
34
|
+
this.prefix = prefix;
|
|
35
|
+
this.logLevel = level;
|
|
36
|
+
|
|
37
|
+
// Define loggers
|
|
38
|
+
this._debug = console.debug || noop;
|
|
39
|
+
this._log = console.log || noop;
|
|
40
|
+
this._warning = console.warn || noop;
|
|
41
|
+
this._error = console.error || noop;
|
|
42
|
+
|
|
43
|
+
if (prefix) {
|
|
44
|
+
const prefix = `WTM [${this.prefix}]`;
|
|
45
|
+
this._debug = this._debug.bind(null, `${prefix} debug:`);
|
|
46
|
+
this._log = this._log.bind(null, `${prefix} log:`);
|
|
47
|
+
this._warning = this._warning.bind(null, `${prefix} warn:`);
|
|
48
|
+
this._error = this._error.bind(null, `${prefix} error:`);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
loggers.add(this);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
setLevel(level) {
|
|
55
|
+
this.setLevel = level;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
isEnabledFor(level) {
|
|
59
|
+
const intLevel = SUPPORTED_LOG_LEVELS.get(level) || -1;
|
|
60
|
+
return intLevel >= SUPPORTED_LOG_LEVELS.get(this.logLevel);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get debug() {
|
|
64
|
+
if (this.isEnabledFor('debug')) {
|
|
65
|
+
return this._debug;
|
|
66
|
+
}
|
|
67
|
+
return noop;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
get info() {
|
|
71
|
+
return this.log;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
get log() {
|
|
75
|
+
if (this.isEnabledFor('log')) {
|
|
76
|
+
return this._log;
|
|
77
|
+
}
|
|
78
|
+
return noop;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get warn() {
|
|
82
|
+
return this.warning;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
get warning() {
|
|
86
|
+
if (this.isEnabledFor('warn')) {
|
|
87
|
+
return this._warning;
|
|
88
|
+
}
|
|
89
|
+
return noop;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
get error() {
|
|
93
|
+
if (this.isEnabledFor('error')) {
|
|
94
|
+
return this._error;
|
|
95
|
+
}
|
|
96
|
+
return noop;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export default Logger.get('reporting', { level: 'info' });
|
|
101
|
+
|
|
102
|
+
export function setLogLevel(level, { prefix = '*' } = {}) {
|
|
103
|
+
if (!SUPPORTED_LOG_LEVELS.has(level)) {
|
|
104
|
+
throw new Error(`Unknow log level '${level}'`);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (prefix === '*') {
|
|
108
|
+
DEFAULT_LOG_LEVEL = level;
|
|
109
|
+
}
|
|
110
|
+
loggers.forEach((logger) => {
|
|
111
|
+
if (prefix === '*' || prefix === logger.prefix) {
|
|
112
|
+
logger.logLevel = level;
|
|
113
|
+
}
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export function describeLoggers() {
|
|
118
|
+
return Object.fromEntries(
|
|
119
|
+
[...loggers].map((logger) => [logger.prefix, logger.logLevel]),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
@@ -0,0 +1,334 @@
|
|
|
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 MapCache from './fixed-size-cache';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Largely based on js-md5 (https://github.com/emn178/js-md5)
|
|
16
|
+
* by Chen, Yi-Cyuan [emn178@gmail.com]
|
|
17
|
+
*/
|
|
18
|
+
/* eslint-disable no-mixed-operators, no-bitwise, no-plusplus */
|
|
19
|
+
|
|
20
|
+
const state = new Int32Array(4);
|
|
21
|
+
const buffer = new ArrayBuffer(68);
|
|
22
|
+
const buffer8 = new Uint8Array(buffer);
|
|
23
|
+
const blocks = new Uint32Array(buffer);
|
|
24
|
+
const extra = new Int32Array([128, 32768, 8388608, -2147483648]);
|
|
25
|
+
const hexChars = '0123456789abcdef'.split('');
|
|
26
|
+
|
|
27
|
+
let finalized = false;
|
|
28
|
+
let first = true;
|
|
29
|
+
let hashed = false;
|
|
30
|
+
let bytes = 0;
|
|
31
|
+
let start = 0;
|
|
32
|
+
let lastByteIndex;
|
|
33
|
+
|
|
34
|
+
function add32(a, b) {
|
|
35
|
+
return (a + b) & 0xffffffff;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function ff(a, b, c, d, x, s, t) {
|
|
39
|
+
const n = (a + ((b & c) | (~b & d)) + (x >>> 0) + t) | 0;
|
|
40
|
+
return (((n << s) | (n >>> (32 - s))) + b) | 0;
|
|
41
|
+
}
|
|
42
|
+
function gg(a, b, c, d, x, s, t) {
|
|
43
|
+
const n = (a + ((b & d) | (c & ~d)) + (x >>> 0) + t) | 0;
|
|
44
|
+
return (((n << s) | (n >>> (32 - s))) + b) | 0;
|
|
45
|
+
}
|
|
46
|
+
function hh(a, b, c, d, x, s, t) {
|
|
47
|
+
const n = (a + (b ^ c ^ d) + (x >>> 0) + t) | 0;
|
|
48
|
+
return (((n << s) | (n >>> (32 - s))) + b) | 0;
|
|
49
|
+
}
|
|
50
|
+
function ii(a, b, c, d, x, s, t) {
|
|
51
|
+
const n = (a + (c ^ (b | ~d)) + (x >>> 0) + t) | 0;
|
|
52
|
+
return (((n << s) | (n >>> (32 - s))) + b) | 0;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function hash() {
|
|
56
|
+
let a;
|
|
57
|
+
let b;
|
|
58
|
+
let c;
|
|
59
|
+
let d;
|
|
60
|
+
|
|
61
|
+
if (first) {
|
|
62
|
+
a = blocks[0] - 680876937;
|
|
63
|
+
a = (((a << 7) | (a >>> 25)) - 271733879) << 0;
|
|
64
|
+
d = (-1732584194 ^ (a & 2004318071)) + blocks[1] - 117830708;
|
|
65
|
+
d = (((d << 12) | (d >>> 20)) + a) << 0;
|
|
66
|
+
c = (-271733879 ^ (d & (a ^ -271733879))) + blocks[2] - 1126478375;
|
|
67
|
+
c = (((c << 17) | (c >>> 15)) + d) << 0;
|
|
68
|
+
b = (a ^ (c & (d ^ a))) + blocks[3] - 1316259209;
|
|
69
|
+
b = (((b << 22) | (b >>> 10)) + c) << 0;
|
|
70
|
+
} else {
|
|
71
|
+
a = state[0];
|
|
72
|
+
b = state[1];
|
|
73
|
+
c = state[2];
|
|
74
|
+
d = state[3];
|
|
75
|
+
a = ff(a, b, c, d, blocks[0], 7, -680876936);
|
|
76
|
+
d = ff(d, a, b, c, blocks[1], 12, -389564586);
|
|
77
|
+
c = ff(c, d, a, b, blocks[2], 17, 606105819);
|
|
78
|
+
b = ff(b, c, d, a, blocks[3], 22, -1044525330);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
a = ff(a, b, c, d, blocks[4], 7, -176418897);
|
|
82
|
+
d = ff(d, a, b, c, blocks[5], 12, 1200080426);
|
|
83
|
+
c = ff(c, d, a, b, blocks[6], 17, -1473231341);
|
|
84
|
+
b = ff(b, c, d, a, blocks[7], 22, -45705983);
|
|
85
|
+
a = ff(a, b, c, d, blocks[8], 7, 1770035416);
|
|
86
|
+
d = ff(d, a, b, c, blocks[9], 12, -1958414417);
|
|
87
|
+
c = ff(c, d, a, b, blocks[10], 17, -42063);
|
|
88
|
+
b = ff(b, c, d, a, blocks[11], 22, -1990404162);
|
|
89
|
+
a = ff(a, b, c, d, blocks[12], 7, 1804603682);
|
|
90
|
+
d = ff(d, a, b, c, blocks[13], 12, -40341101);
|
|
91
|
+
c = ff(c, d, a, b, blocks[14], 17, -1502002290);
|
|
92
|
+
b = ff(b, c, d, a, blocks[15], 22, 1236535329);
|
|
93
|
+
|
|
94
|
+
a = gg(a, b, c, d, blocks[1], 5, -165796510);
|
|
95
|
+
d = gg(d, a, b, c, blocks[6], 9, -1069501632);
|
|
96
|
+
c = gg(c, d, a, b, blocks[11], 14, 643717713);
|
|
97
|
+
b = gg(b, c, d, a, blocks[0], 20, -373897302);
|
|
98
|
+
a = gg(a, b, c, d, blocks[5], 5, -701558691);
|
|
99
|
+
d = gg(d, a, b, c, blocks[10], 9, 38016083);
|
|
100
|
+
c = gg(c, d, a, b, blocks[15], 14, -660478335);
|
|
101
|
+
b = gg(b, c, d, a, blocks[4], 20, -405537848);
|
|
102
|
+
a = gg(a, b, c, d, blocks[9], 5, 568446438);
|
|
103
|
+
d = gg(d, a, b, c, blocks[14], 9, -1019803690);
|
|
104
|
+
c = gg(c, d, a, b, blocks[3], 14, -187363961);
|
|
105
|
+
b = gg(b, c, d, a, blocks[8], 20, 1163531501);
|
|
106
|
+
a = gg(a, b, c, d, blocks[13], 5, -1444681467);
|
|
107
|
+
d = gg(d, a, b, c, blocks[2], 9, -51403784);
|
|
108
|
+
c = gg(c, d, a, b, blocks[7], 14, 1735328473);
|
|
109
|
+
b = gg(b, c, d, a, blocks[12], 20, -1926607734);
|
|
110
|
+
|
|
111
|
+
a = hh(a, b, c, d, blocks[5], 4, -378558);
|
|
112
|
+
d = hh(d, a, b, c, blocks[8], 11, -2022574463);
|
|
113
|
+
c = hh(c, d, a, b, blocks[11], 16, 1839030562);
|
|
114
|
+
b = hh(b, c, d, a, blocks[14], 23, -35309556);
|
|
115
|
+
a = hh(a, b, c, d, blocks[1], 4, -1530992060);
|
|
116
|
+
d = hh(d, a, b, c, blocks[4], 11, 1272893353);
|
|
117
|
+
c = hh(c, d, a, b, blocks[7], 16, -155497632);
|
|
118
|
+
b = hh(b, c, d, a, blocks[10], 23, -1094730640);
|
|
119
|
+
a = hh(a, b, c, d, blocks[13], 4, 681279174);
|
|
120
|
+
d = hh(d, a, b, c, blocks[0], 11, -358537222);
|
|
121
|
+
c = hh(c, d, a, b, blocks[3], 16, -722521979);
|
|
122
|
+
b = hh(b, c, d, a, blocks[6], 23, 76029189);
|
|
123
|
+
a = hh(a, b, c, d, blocks[9], 4, -640364487);
|
|
124
|
+
d = hh(d, a, b, c, blocks[12], 11, -421815835);
|
|
125
|
+
c = hh(c, d, a, b, blocks[15], 16, 530742520);
|
|
126
|
+
b = hh(b, c, d, a, blocks[2], 23, -995338651);
|
|
127
|
+
|
|
128
|
+
a = ii(a, b, c, d, blocks[0], 6, -198630844);
|
|
129
|
+
d = ii(d, a, b, c, blocks[7], 10, 1126891415);
|
|
130
|
+
c = ii(c, d, a, b, blocks[14], 15, -1416354905);
|
|
131
|
+
b = ii(b, c, d, a, blocks[5], 21, -57434055);
|
|
132
|
+
a = ii(a, b, c, d, blocks[12], 6, 1700485571);
|
|
133
|
+
d = ii(d, a, b, c, blocks[3], 10, -1894986606);
|
|
134
|
+
c = ii(c, d, a, b, blocks[10], 15, -1051523);
|
|
135
|
+
b = ii(b, c, d, a, blocks[1], 21, -2054922799);
|
|
136
|
+
a = ii(a, b, c, d, blocks[8], 6, 1873313359);
|
|
137
|
+
d = ii(d, a, b, c, blocks[15], 10, -30611744);
|
|
138
|
+
c = ii(c, d, a, b, blocks[6], 15, -1560198380);
|
|
139
|
+
b = ii(b, c, d, a, blocks[13], 21, 1309151649);
|
|
140
|
+
a = ii(a, b, c, d, blocks[4], 6, -145523070);
|
|
141
|
+
d = ii(d, a, b, c, blocks[11], 10, -1120210379);
|
|
142
|
+
c = ii(c, d, a, b, blocks[2], 15, 718787259);
|
|
143
|
+
b = ii(b, c, d, a, blocks[9], 21, -343485551);
|
|
144
|
+
|
|
145
|
+
if (first) {
|
|
146
|
+
state[0] = add32(a, 1732584193);
|
|
147
|
+
state[1] = add32(b, -271733879);
|
|
148
|
+
state[2] = add32(c, -1732584194);
|
|
149
|
+
state[3] = add32(d, +271733878);
|
|
150
|
+
first = false;
|
|
151
|
+
} else {
|
|
152
|
+
state[0] = add32(state[0], a);
|
|
153
|
+
state[1] = add32(state[1], b);
|
|
154
|
+
state[2] = add32(state[2], c);
|
|
155
|
+
state[3] = add32(state[3], d);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function init() {
|
|
160
|
+
blocks.fill(0);
|
|
161
|
+
state.fill(0);
|
|
162
|
+
bytes = 0;
|
|
163
|
+
start = 0;
|
|
164
|
+
finalized = false;
|
|
165
|
+
first = true;
|
|
166
|
+
hashed = false;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function update(message) {
|
|
170
|
+
if (finalized) {
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const length = message.length;
|
|
174
|
+
let code;
|
|
175
|
+
let index = 0;
|
|
176
|
+
let i;
|
|
177
|
+
|
|
178
|
+
while (index < length) {
|
|
179
|
+
if (hashed) {
|
|
180
|
+
hashed = false;
|
|
181
|
+
blocks[0] = blocks[16];
|
|
182
|
+
blocks[1] = 0;
|
|
183
|
+
blocks[2] = 0;
|
|
184
|
+
blocks[3] = 0;
|
|
185
|
+
blocks[4] = 0;
|
|
186
|
+
blocks[5] = 0;
|
|
187
|
+
blocks[6] = 0;
|
|
188
|
+
blocks[7] = 0;
|
|
189
|
+
blocks[8] = 0;
|
|
190
|
+
blocks[9] = 0;
|
|
191
|
+
blocks[10] = 0;
|
|
192
|
+
blocks[11] = 0;
|
|
193
|
+
blocks[12] = 0;
|
|
194
|
+
blocks[13] = 0;
|
|
195
|
+
blocks[14] = 0;
|
|
196
|
+
blocks[15] = 0;
|
|
197
|
+
blocks[16] = 0;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
for (i = start; index < length && i < 64; ++index) {
|
|
201
|
+
code = message.charCodeAt(index);
|
|
202
|
+
if (code < 0x80) {
|
|
203
|
+
buffer8[i++] = code;
|
|
204
|
+
} else if (code < 0x800) {
|
|
205
|
+
buffer8[i++] = 0xc0 | (code >> 6);
|
|
206
|
+
buffer8[i++] = 0x80 | (code & 0x3f);
|
|
207
|
+
} else if (code < 0xd800 || code >= 0xe000) {
|
|
208
|
+
buffer8[i++] = 0xe0 | (code >> 12);
|
|
209
|
+
buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
|
|
210
|
+
buffer8[i++] = 0x80 | (code & 0x3f);
|
|
211
|
+
} else {
|
|
212
|
+
code =
|
|
213
|
+
0x10000 +
|
|
214
|
+
(((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff));
|
|
215
|
+
buffer8[i++] = 0xf0 | (code >> 18);
|
|
216
|
+
buffer8[i++] = 0x80 | ((code >> 12) & 0x3f);
|
|
217
|
+
buffer8[i++] = 0x80 | ((code >> 6) & 0x3f);
|
|
218
|
+
buffer8[i++] = 0x80 | (code & 0x3f);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
lastByteIndex = i;
|
|
222
|
+
bytes += i - start;
|
|
223
|
+
if (i >= 64) {
|
|
224
|
+
start = i - 64;
|
|
225
|
+
hash();
|
|
226
|
+
hashed = true;
|
|
227
|
+
} else {
|
|
228
|
+
start = i;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function finalize() {
|
|
234
|
+
if (finalized) {
|
|
235
|
+
return;
|
|
236
|
+
}
|
|
237
|
+
finalized = true;
|
|
238
|
+
const i = lastByteIndex;
|
|
239
|
+
blocks[i >> 2] |= extra[i & 3];
|
|
240
|
+
if (i >= 56) {
|
|
241
|
+
if (!hashed) {
|
|
242
|
+
hash();
|
|
243
|
+
}
|
|
244
|
+
blocks[0] = blocks[16];
|
|
245
|
+
blocks[1] = 0;
|
|
246
|
+
blocks[2] = 0;
|
|
247
|
+
blocks[3] = 0;
|
|
248
|
+
blocks[4] = 0;
|
|
249
|
+
blocks[5] = 0;
|
|
250
|
+
blocks[6] = 0;
|
|
251
|
+
blocks[7] = 0;
|
|
252
|
+
blocks[8] = 0;
|
|
253
|
+
blocks[9] = 0;
|
|
254
|
+
blocks[10] = 0;
|
|
255
|
+
blocks[11] = 0;
|
|
256
|
+
blocks[12] = 0;
|
|
257
|
+
blocks[13] = 0;
|
|
258
|
+
blocks[14] = 0;
|
|
259
|
+
blocks[15] = 0;
|
|
260
|
+
blocks[16] = 0;
|
|
261
|
+
}
|
|
262
|
+
blocks[14] = bytes << 3;
|
|
263
|
+
hash();
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function hex(x) {
|
|
267
|
+
const h0 = x[0];
|
|
268
|
+
const h1 = x[1];
|
|
269
|
+
const h2 = x[2];
|
|
270
|
+
const h3 = x[3];
|
|
271
|
+
return (
|
|
272
|
+
hexChars[(h0 >> 4) & 0x0f] +
|
|
273
|
+
hexChars[h0 & 0x0f] +
|
|
274
|
+
hexChars[(h0 >> 12) & 0x0f] +
|
|
275
|
+
hexChars[(h0 >> 8) & 0x0f] +
|
|
276
|
+
hexChars[(h0 >> 20) & 0x0f] +
|
|
277
|
+
hexChars[(h0 >> 16) & 0x0f] +
|
|
278
|
+
hexChars[(h0 >> 28) & 0x0f] +
|
|
279
|
+
hexChars[(h0 >> 24) & 0x0f] +
|
|
280
|
+
hexChars[(h1 >> 4) & 0x0f] +
|
|
281
|
+
hexChars[h1 & 0x0f] +
|
|
282
|
+
hexChars[(h1 >> 12) & 0x0f] +
|
|
283
|
+
hexChars[(h1 >> 8) & 0x0f] +
|
|
284
|
+
hexChars[(h1 >> 20) & 0x0f] +
|
|
285
|
+
hexChars[(h1 >> 16) & 0x0f] +
|
|
286
|
+
hexChars[(h1 >> 28) & 0x0f] +
|
|
287
|
+
hexChars[(h1 >> 24) & 0x0f] +
|
|
288
|
+
hexChars[(h2 >> 4) & 0x0f] +
|
|
289
|
+
hexChars[h2 & 0x0f] +
|
|
290
|
+
hexChars[(h2 >> 12) & 0x0f] +
|
|
291
|
+
hexChars[(h2 >> 8) & 0x0f] +
|
|
292
|
+
hexChars[(h2 >> 20) & 0x0f] +
|
|
293
|
+
hexChars[(h2 >> 16) & 0x0f] +
|
|
294
|
+
hexChars[(h2 >> 28) & 0x0f] +
|
|
295
|
+
hexChars[(h2 >> 24) & 0x0f] +
|
|
296
|
+
hexChars[(h3 >> 4) & 0x0f] +
|
|
297
|
+
hexChars[h3 & 0x0f] +
|
|
298
|
+
hexChars[(h3 >> 12) & 0x0f] +
|
|
299
|
+
hexChars[(h3 >> 8) & 0x0f] +
|
|
300
|
+
hexChars[(h3 >> 20) & 0x0f] +
|
|
301
|
+
hexChars[(h3 >> 16) & 0x0f] +
|
|
302
|
+
hexChars[(h3 >> 28) & 0x0f] +
|
|
303
|
+
hexChars[(h3 >> 24) & 0x0f]
|
|
304
|
+
);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function md5(message) {
|
|
308
|
+
init();
|
|
309
|
+
update(message);
|
|
310
|
+
finalize();
|
|
311
|
+
return hex(state);
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const md5Cache = new MapCache(md5, 1600);
|
|
315
|
+
|
|
316
|
+
export default function cachedMD5(s) {
|
|
317
|
+
if (!s) return '';
|
|
318
|
+
return md5Cache.get(s);
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* First 16 characters of md5 hash.
|
|
323
|
+
* This is used in various places where increasing the chance of hash collision is a desireable
|
|
324
|
+
* property.
|
|
325
|
+
*
|
|
326
|
+
* By truncating to 16 hex characters, a hash of 64 bits remains.
|
|
327
|
+
* Expect collisions (with a likelihood of more than 50%) after
|
|
328
|
+
* approximately 2^32 (4.3 billion) messages.
|
|
329
|
+
*
|
|
330
|
+
* @param s
|
|
331
|
+
*/
|
|
332
|
+
export function truncatedHash(s) {
|
|
333
|
+
return cachedMD5(s).slice(0, 16);
|
|
334
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import logger from './logger';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Responsible for sending WTM reports to the servers.
|
|
16
|
+
* To improve anonymity, data is sent through a 3rd party
|
|
17
|
+
* to hide the sender's IP address.
|
|
18
|
+
*
|
|
19
|
+
* Note: There is no fundamental reason why hpnv2 could not be used
|
|
20
|
+
* on Mobile as well, but it is a project on its own to port it.
|
|
21
|
+
*/
|
|
22
|
+
export default class MessageSender {
|
|
23
|
+
constructor({ duplicateDetector, communication, jobScheduler }) {
|
|
24
|
+
this.duplicateDetector = duplicateDetector;
|
|
25
|
+
this.communication = communication;
|
|
26
|
+
|
|
27
|
+
jobScheduler.registerHandler('send-message', async (job) => {
|
|
28
|
+
await this.send(job.args);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async send(message) {
|
|
33
|
+
logger.debug('Preparing to send message:', message);
|
|
34
|
+
const { ok, rollback, rejectReason } = await this.duplicateDetector.trySend(
|
|
35
|
+
message,
|
|
36
|
+
);
|
|
37
|
+
if (!ok) {
|
|
38
|
+
logger.info('Rejected by duplicate detector:', rejectReason, message);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
try {
|
|
42
|
+
// Note: assume fire-and-forget message here
|
|
43
|
+
await this.communication.send(message.body);
|
|
44
|
+
logger.info('Successfully sent message:', message.body);
|
|
45
|
+
} catch (e) {
|
|
46
|
+
// rollback to allow future resending attempts
|
|
47
|
+
await rollback();
|
|
48
|
+
throw e;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import logger from './logger';
|
|
13
|
+
|
|
14
|
+
const SECOND = 1000;
|
|
15
|
+
const MINUTE = 60 * SECOND;
|
|
16
|
+
const HOUR = 60 * MINUTE;
|
|
17
|
+
const DAY = 24 * HOUR;
|
|
18
|
+
|
|
19
|
+
// Simple function to detect IP addresses that are non-public.
|
|
20
|
+
// Local to the machine or link-only (belonging to a local network).
|
|
21
|
+
//
|
|
22
|
+
// The function assumes that ip addresses are well-formed. That should be
|
|
23
|
+
// sufficient, since all IP addresses are coming from the webRequest API.
|
|
24
|
+
//
|
|
25
|
+
// If there are errors, the function should err on the side of being too
|
|
26
|
+
// conservative. In other words, classifying a public IPs to be local is
|
|
27
|
+
// better than classifying a local IP as public.
|
|
28
|
+
export function isLocalIP(ip) {
|
|
29
|
+
const isIPv6 = ip.includes(':');
|
|
30
|
+
if (isIPv6) {
|
|
31
|
+
if (ip === '::1' || ip.startsWith('fd:') || ip.startsWith('fe:')) {
|
|
32
|
+
return true;
|
|
33
|
+
}
|
|
34
|
+
const ipParts = ip.split(':');
|
|
35
|
+
return (
|
|
36
|
+
ipParts[0].startsWith('fd') ||
|
|
37
|
+
ipParts[0].startsWith('fe') ||
|
|
38
|
+
ipParts.every((d, i) => {
|
|
39
|
+
if (i === ipParts.length - 1) {
|
|
40
|
+
// last group of address
|
|
41
|
+
return d === '1';
|
|
42
|
+
}
|
|
43
|
+
return d === '0' || !d;
|
|
44
|
+
})
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// IPv4
|
|
49
|
+
if (
|
|
50
|
+
ip.startsWith('127.') ||
|
|
51
|
+
ip.startsWith('0.') ||
|
|
52
|
+
ip.startsWith('10.') ||
|
|
53
|
+
ip.startsWith('192.168.')
|
|
54
|
+
) {
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
if (ip.startsWith('172.')) {
|
|
58
|
+
const part2 = parseInt(ip.split('.')[1], 10);
|
|
59
|
+
return part2 >= 16 && part2 < 32;
|
|
60
|
+
}
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// There are two unsolved problems:
|
|
65
|
+
// - What to do if there is no IP resolution cached yet?
|
|
66
|
+
// (Currently, it assumes that the page is private; but most of the
|
|
67
|
+
// time this assumption will be wrong, so it looks overly conservative.
|
|
68
|
+
// Perhaps, extending the API to return three values: yes/no/unknown
|
|
69
|
+
// could help. But it will push the complexity to the caller.)
|
|
70
|
+
// - Perhaps adding all seen URLs from private IPs to the bloom filter
|
|
71
|
+
// could be a solution
|
|
72
|
+
export class DnsResolver {
|
|
73
|
+
constructor() {
|
|
74
|
+
this.dns = new Map();
|
|
75
|
+
this._ttlInMs = DAY; // TODO: reconsider (if possible, make it small)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
isPrivateURL(url) {
|
|
79
|
+
const hostname = this._tryParseHostname(url);
|
|
80
|
+
return !hostname || this.isPrivateHostname(hostname);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
isPrivateHostname(hostname) {
|
|
84
|
+
if (hostname === 'localhost') {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
const entry = this.dns.get(hostname);
|
|
88
|
+
return entry?.ip && isLocalIP(entry.ip);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
cacheDnsResolution({ url, hostname, ip, now = Date.now() }) {
|
|
92
|
+
const hostname_ = hostname || this._tryParseHostname(url);
|
|
93
|
+
if (hostname_) {
|
|
94
|
+
this.dns.set(hostname_, { ip, updatedAt: now });
|
|
95
|
+
return true;
|
|
96
|
+
}
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
expireEntries(now = Date.now()) {
|
|
101
|
+
for (const [key, entry] of this.dns) {
|
|
102
|
+
let isExpired = entry.updatedAt + this._ttlInMs <= now;
|
|
103
|
+
if (entry.updatedAt > now + DAY) {
|
|
104
|
+
logger.warn('Clock jumped');
|
|
105
|
+
isExpired = true;
|
|
106
|
+
}
|
|
107
|
+
if (isExpired) {
|
|
108
|
+
this.dns.delete(key);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
_tryParseHostname(url) {
|
|
114
|
+
if (!url.startsWith('https://') && !url.startsWith('http://')) {
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
return new URL(url).hostname;
|
|
119
|
+
} catch (e) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// Returns an object that can be safely stored in Session API.
|
|
125
|
+
// It can be later restored by calling "restore", for instance:
|
|
126
|
+
//
|
|
127
|
+
// const data = obj.serialize();
|
|
128
|
+
// ...
|
|
129
|
+
// obj.restore(data);
|
|
130
|
+
//
|
|
131
|
+
serialize({ now = Date.now() } = {}) {
|
|
132
|
+
this.expireEntries(now);
|
|
133
|
+
return [...this.dns];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
restore(entries = [], { now = Date.now() } = {}) {
|
|
137
|
+
this.dns = new Map(entries);
|
|
138
|
+
this.expireEntries(now);
|
|
139
|
+
}
|
|
140
|
+
}
|