@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,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@whotracksme/reporting",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"scripts": {
|
|
6
|
+
"start": "cp example/manifests/chromium.json example/manifest.json && web-ext run -s ./example -t chromium",
|
|
7
|
+
"start.firefox": "cp example/manifests/firefox.json example/manifest.json && web-ext run -s ./example -t firefox-desktop",
|
|
8
|
+
"build": "rollup -c",
|
|
9
|
+
"watch": "rollup -c -w",
|
|
10
|
+
"test": "karma start --single-run --browsers ChromeHeadless karma.conf.cjs"
|
|
11
|
+
},
|
|
12
|
+
"author": "Ghostery GmbH",
|
|
13
|
+
"license": "MPL-2.0",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"@rollup/plugin-commonjs": "^23.0.0",
|
|
16
|
+
"@rollup/plugin-json": "^5.0.0",
|
|
17
|
+
"@rollup/plugin-node-resolve": "^13.3.0",
|
|
18
|
+
"chai": "^4.3.6",
|
|
19
|
+
"chai-as-promised": "^7.1.1",
|
|
20
|
+
"fast-check": "^3.3.0",
|
|
21
|
+
"karma": "^6.4.1",
|
|
22
|
+
"karma-chai": "^0.1.0",
|
|
23
|
+
"karma-chrome-launcher": "^3.1.1",
|
|
24
|
+
"karma-mocha": "^2.0.1",
|
|
25
|
+
"karma-mocha-reporter": "^2.2.5",
|
|
26
|
+
"karma-rollup-preprocessor": "^7.0.8",
|
|
27
|
+
"karma-sinon": "^1.0.5",
|
|
28
|
+
"mocha": "^9.2.2",
|
|
29
|
+
"rollup": "^2.79.1",
|
|
30
|
+
"rollup-plugin-polyfill-node": "^0.8.0",
|
|
31
|
+
"rollup-plugin-sourcemaps": "^0.6.3",
|
|
32
|
+
"sinon": "^15.2.0",
|
|
33
|
+
"sinon-chai": "^3.7.0",
|
|
34
|
+
"sinon-chrome": "^3.0.1",
|
|
35
|
+
"web-ext": "^6.8.0"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"@cliqz/url-parser": "^1.1.5",
|
|
39
|
+
"date-fns": "^2.29.3",
|
|
40
|
+
"idb": "^7.1.1",
|
|
41
|
+
"linkedom": "^0.16.11",
|
|
42
|
+
"tldts-experimental": "^6.0.11"
|
|
43
|
+
},
|
|
44
|
+
"exports": {
|
|
45
|
+
".": "./src/index.js",
|
|
46
|
+
"./content-script": "./src/request/content-script.js"
|
|
47
|
+
},
|
|
48
|
+
"directories": {
|
|
49
|
+
"test": "test"
|
|
50
|
+
},
|
|
51
|
+
"repository": {
|
|
52
|
+
"type": "git",
|
|
53
|
+
"url": "git+https://github.com/whotracksme/webextension-packages.git"
|
|
54
|
+
},
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/whotracksme/webextension-packages/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/whotracksme/webextension-packages#readme",
|
|
59
|
+
"engines": {
|
|
60
|
+
"npm": ">=7.0.0",
|
|
61
|
+
"node": ">=16.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const { nodeResolve } = require('@rollup/plugin-node-resolve');
|
|
2
|
+
const sourcemaps = require('rollup-plugin-sourcemaps');
|
|
3
|
+
const nodePolyfills = require('rollup-plugin-polyfill-node');
|
|
4
|
+
const commonjs = require('@rollup/plugin-commonjs');
|
|
5
|
+
const json = require('@rollup/plugin-json');
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
plugins: [nodePolyfills(), nodeResolve(), sourcemaps(), commonjs(), json()],
|
|
9
|
+
external: ['chai', 'sinon'],
|
|
10
|
+
output: {
|
|
11
|
+
globals: {
|
|
12
|
+
chai: 'chai',
|
|
13
|
+
sinon: 'sinon',
|
|
14
|
+
},
|
|
15
|
+
format: 'iife',
|
|
16
|
+
name: 'Test',
|
|
17
|
+
sourcemap: 'inline',
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
const commonConfig = require('./rollup.common-config.cjs');
|
|
2
|
+
|
|
3
|
+
module.exports = [
|
|
4
|
+
{
|
|
5
|
+
...commonConfig,
|
|
6
|
+
input: 'example/index.js',
|
|
7
|
+
output: {
|
|
8
|
+
...commonConfig.output,
|
|
9
|
+
file: 'example/index.bundle.js',
|
|
10
|
+
},
|
|
11
|
+
},
|
|
12
|
+
{
|
|
13
|
+
...commonConfig,
|
|
14
|
+
input: 'example/content.js',
|
|
15
|
+
output: {
|
|
16
|
+
...commonConfig.output,
|
|
17
|
+
file: 'example/content.bundle.js',
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
];
|
|
@@ -0,0 +1,149 @@
|
|
|
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
|
+
/* Create a new node (key, value) to store in the cache */
|
|
13
|
+
function newNode(key, value) {
|
|
14
|
+
return {
|
|
15
|
+
prev: null,
|
|
16
|
+
next: null,
|
|
17
|
+
key,
|
|
18
|
+
value,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export default class LRU {
|
|
23
|
+
reset() {
|
|
24
|
+
this.cache.clear();
|
|
25
|
+
this.head = null;
|
|
26
|
+
this.tail = null;
|
|
27
|
+
this.size = 0;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
constructor(size) {
|
|
31
|
+
this.cache = new Map();
|
|
32
|
+
this.maxSize = size;
|
|
33
|
+
this.reset();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/*
|
|
37
|
+
* Check if value associated with `key` is stored in cache.
|
|
38
|
+
* Does not update position of the entry.
|
|
39
|
+
*
|
|
40
|
+
* @param key
|
|
41
|
+
*/
|
|
42
|
+
has(key) {
|
|
43
|
+
return this.cache.has(key);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/* Retrieve value associated with `key` from cache. If it doesn't
|
|
47
|
+
* exist, return `undefined`, otherwise, update position of the
|
|
48
|
+
* entry to "most recent seen".
|
|
49
|
+
*
|
|
50
|
+
* @param key - Key of value we want to get.
|
|
51
|
+
*/
|
|
52
|
+
get(key) {
|
|
53
|
+
const node = this.cache.get(key);
|
|
54
|
+
|
|
55
|
+
if (node !== undefined) {
|
|
56
|
+
this.touch(node);
|
|
57
|
+
return node.value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/* Associate a new `value` to `key` in cache. If `key` isn't already
|
|
64
|
+
* present in cache, create a new node at the position "most recent seen".
|
|
65
|
+
* Otherwise, change the value associated with `key` and refresh the
|
|
66
|
+
* position of the entry to "most recent seen".
|
|
67
|
+
*
|
|
68
|
+
* @param key - Key add to the cache.
|
|
69
|
+
* @param value - Value associated with key.
|
|
70
|
+
*/
|
|
71
|
+
set(key, value) {
|
|
72
|
+
let node = this.cache.get(key);
|
|
73
|
+
|
|
74
|
+
if (node !== undefined) {
|
|
75
|
+
// Hit - update value
|
|
76
|
+
node.value = value;
|
|
77
|
+
this.touch(node);
|
|
78
|
+
} else {
|
|
79
|
+
// Miss - Create a new node
|
|
80
|
+
node = newNode(key, value);
|
|
81
|
+
|
|
82
|
+
// Forget about oldest node
|
|
83
|
+
if (this.size >= this.maxSize && this.tail !== null) {
|
|
84
|
+
this.cache.delete(this.tail.key);
|
|
85
|
+
this.remove(this.tail);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
this.cache.set(key, node);
|
|
89
|
+
this.pushFront(node);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
toMap() {
|
|
94
|
+
return new Map(
|
|
95
|
+
Array.from(this.cache.values()).map(({ key, value }) => [key, value]),
|
|
96
|
+
);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// Private interface (Linked List)
|
|
100
|
+
|
|
101
|
+
/* Refresh timestamp of `node` by moving it to the front of the list.
|
|
102
|
+
* It the becomes the (key, value) seen most recently.
|
|
103
|
+
*/
|
|
104
|
+
touch(node) {
|
|
105
|
+
this.remove(node);
|
|
106
|
+
this.pushFront(node);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/* Remove `node` from the list. */
|
|
110
|
+
remove(node) {
|
|
111
|
+
if (node !== null) {
|
|
112
|
+
// Update previous node
|
|
113
|
+
if (node.prev === null) {
|
|
114
|
+
this.head = node.next;
|
|
115
|
+
} else {
|
|
116
|
+
node.prev.next = node.next;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Update next node
|
|
120
|
+
if (node.next === null) {
|
|
121
|
+
this.tail = node.prev;
|
|
122
|
+
} else {
|
|
123
|
+
node.next.prev = node.prev;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
this.size -= 1;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/* Add `node` in front of the list (most recent element). */
|
|
131
|
+
pushFront(node) {
|
|
132
|
+
// Replace first node of the list
|
|
133
|
+
node.prev = null;
|
|
134
|
+
node.next = this.head;
|
|
135
|
+
|
|
136
|
+
if (this.head !== null) {
|
|
137
|
+
this.head.prev = node;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
this.head = node;
|
|
141
|
+
|
|
142
|
+
// Case: List was empty
|
|
143
|
+
if (this.tail === null) {
|
|
144
|
+
this.tail = node;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
this.size += 1;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
@@ -0,0 +1,298 @@
|
|
|
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 {
|
|
14
|
+
requireParam,
|
|
15
|
+
requireInt,
|
|
16
|
+
requireIntOrNull,
|
|
17
|
+
requireString,
|
|
18
|
+
requireStringOrNull,
|
|
19
|
+
} from './utils';
|
|
20
|
+
import SelfCheck from './self-check';
|
|
21
|
+
|
|
22
|
+
const SECOND = 1000;
|
|
23
|
+
const MINUTE = 60 * SECOND;
|
|
24
|
+
|
|
25
|
+
const BUCKET_SIZE_IN_MS = 524288; // === nextPow2(5 * MINUTE);
|
|
26
|
+
const MAX_ACTIVE_BUCKETS = 7; // roughly one hour
|
|
27
|
+
|
|
28
|
+
const MAX_UNCONFIRMED_ACTIVITY = 1 * MINUTE;
|
|
29
|
+
const LOAD_BOOST = 5 * SECOND;
|
|
30
|
+
const MAX_SCORE = 20 * MINUTE;
|
|
31
|
+
|
|
32
|
+
const MAX_ACCEPTED_DRIFT = 2 * MINUTE;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Estimate the activity of a page. Given a URL, it will return
|
|
36
|
+
* a number between 0 (no activity) and 1 (maximum threshold).
|
|
37
|
+
*
|
|
38
|
+
* The score be understood as an estimation of the site's activity;
|
|
39
|
+
* it should correlate with relevance. In contrast to the "Pages" class,
|
|
40
|
+
* which provides the input events, the ActivityEstimator operates on URLs,
|
|
41
|
+
* instead of tabs.
|
|
42
|
+
*
|
|
43
|
+
* For active pages, the score increases when the tab activates, or when the
|
|
44
|
+
* page dynamically loads content. Inactive pages are excluded. The score is
|
|
45
|
+
* intentionally imprecise; but given enough clients, it is intended to serve
|
|
46
|
+
* as another metric to rank page popularity.
|
|
47
|
+
*
|
|
48
|
+
* The data in this class is hold in memory only (with the option to serialize
|
|
49
|
+
* to in-memory session store). Thus, even though the "Pages" class should
|
|
50
|
+
* already performed some URL filtering before (e.g. incognito tabs), it is
|
|
51
|
+
* not strictly required. From the perspective of the ActivityEstimator,
|
|
52
|
+
* sites that the "Pages" class filters will be consisted always inactive.
|
|
53
|
+
*/
|
|
54
|
+
export default class ActivityEstimator {
|
|
55
|
+
constructor({ onActivityUpdated, onInternalStateChanged }) {
|
|
56
|
+
this._onActivityUpdated = requireParam(onActivityUpdated);
|
|
57
|
+
this._updateSet = new Set();
|
|
58
|
+
this._pendingUpdate = null;
|
|
59
|
+
|
|
60
|
+
this._onInternalStateChanged = requireParam(onInternalStateChanged);
|
|
61
|
+
this._pendingFlush = null;
|
|
62
|
+
|
|
63
|
+
this.reset();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
reset() {
|
|
67
|
+
this._state = {
|
|
68
|
+
lastUpdated: 0, // Unix epoch
|
|
69
|
+
activeUrl: null,
|
|
70
|
+
|
|
71
|
+
// Entries look like this:
|
|
72
|
+
// {
|
|
73
|
+
// idx: 1627243,
|
|
74
|
+
// start: 1706288157869,
|
|
75
|
+
// urls: {
|
|
76
|
+
// 'https://example.com/': {
|
|
77
|
+
// loads: 1,
|
|
78
|
+
// accum: 0,
|
|
79
|
+
// since: 1706288157869
|
|
80
|
+
// }
|
|
81
|
+
// }
|
|
82
|
+
// }
|
|
83
|
+
buckets: [],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
updateActiveUrl(url, now = Date.now()) {
|
|
88
|
+
this._internalUpdateActiveUrl(requireStringOrNull(url), now);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
dynamicLoadDetected(url, now = Date.now()) {
|
|
92
|
+
if (!url) {
|
|
93
|
+
throw new Error('Illegal state: URL is required');
|
|
94
|
+
}
|
|
95
|
+
const bucket = this._activateBucket(now);
|
|
96
|
+
this._internalUpdateActiveUrl(url, now);
|
|
97
|
+
bucket.urls[url].loads += 1;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
_internalUpdateActiveUrl(url, now) {
|
|
101
|
+
const bucket = this._activateBucket(now);
|
|
102
|
+
const lastUrl = this._state.activeUrl;
|
|
103
|
+
if (bucket.urls[lastUrl] && bucket.urls[lastUrl].since !== null) {
|
|
104
|
+
bucket.urls[lastUrl].accum += now - bucket.urls[lastUrl].since;
|
|
105
|
+
bucket.urls[lastUrl].since = null;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (url) {
|
|
109
|
+
if (bucket.urls[url]) {
|
|
110
|
+
bucket.urls[url].loads += 1;
|
|
111
|
+
if (bucket.urls[url].since === null) {
|
|
112
|
+
bucket.urls[url].since = now;
|
|
113
|
+
}
|
|
114
|
+
} else {
|
|
115
|
+
let accum = 0;
|
|
116
|
+
if (url === lastUrl) {
|
|
117
|
+
const diff = now - Math.max(this._state.lastUpdated, bucket.start);
|
|
118
|
+
accum += Math.min(Math.max(diff, 0), MAX_UNCONFIRMED_ACTIVITY);
|
|
119
|
+
}
|
|
120
|
+
bucket.urls[url] = {
|
|
121
|
+
loads: 1,
|
|
122
|
+
accum,
|
|
123
|
+
since: now,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
this._state.activeUrl = url;
|
|
128
|
+
this._state.lastUpdated = now;
|
|
129
|
+
this._markDirty();
|
|
130
|
+
|
|
131
|
+
this._addPendingUpdate(lastUrl);
|
|
132
|
+
this._addPendingUpdate(url);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
_addPendingUpdate(url) {
|
|
136
|
+
if (url) {
|
|
137
|
+
this._updateSet.add(url);
|
|
138
|
+
if (this._pendingUpdate === null) {
|
|
139
|
+
this._pendingUpdate = setTimeout(() => {
|
|
140
|
+
this._pendingUpdate = null;
|
|
141
|
+
const urls = [...this._updateSet];
|
|
142
|
+
if (urls.length > 0) {
|
|
143
|
+
this._updateSet.clear();
|
|
144
|
+
this._onActivityUpdated(urls);
|
|
145
|
+
}
|
|
146
|
+
}, 0);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
estimate(url, now = Date.now()) {
|
|
152
|
+
let totalAccum = 0;
|
|
153
|
+
let totalLoadBoosts = 0;
|
|
154
|
+
const expiredStart = now - (MAX_ACTIVE_BUCKETS + 1) * BUCKET_SIZE_IN_MS;
|
|
155
|
+
for (let i = 0; i < this._state.buckets.length; i += 1) {
|
|
156
|
+
const { start, urls } = this._state.buckets[i];
|
|
157
|
+
if (start < expiredStart) {
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
if (urls[url]) {
|
|
161
|
+
const { accum, loads, since } = urls[url];
|
|
162
|
+
totalAccum += accum;
|
|
163
|
+
if (since !== null && now > since) {
|
|
164
|
+
const tillEndOfBucket = BUCKET_SIZE_IN_MS - (since - start);
|
|
165
|
+
totalAccum += Math.min(
|
|
166
|
+
now - since,
|
|
167
|
+
tillEndOfBucket,
|
|
168
|
+
MAX_UNCONFIRMED_ACTIVITY,
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
if (loads > 0) {
|
|
172
|
+
totalLoadBoosts += LOAD_BOOST;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const score = totalAccum + totalLoadBoosts;
|
|
178
|
+
return Math.min(score / MAX_SCORE, 1.0);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
_markDirty() {
|
|
182
|
+
if (this._pendingFlush === null) {
|
|
183
|
+
this._pendingFlush = setTimeout(() => {
|
|
184
|
+
this.flush();
|
|
185
|
+
}, 0);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
flush() {
|
|
190
|
+
if (this._pendingFlush !== null) {
|
|
191
|
+
this._pendingFlush = null;
|
|
192
|
+
this._onInternalStateChanged();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
serialize() {
|
|
197
|
+
return this._state;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
restore(state) {
|
|
201
|
+
this._state = ActivityEstimator.ensureValidState(state);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
_activateBucket(ts) {
|
|
205
|
+
if (this._state.buckets.length > 0) {
|
|
206
|
+
if (
|
|
207
|
+
ts >= this._state.buckets[0].start &&
|
|
208
|
+
ts < this._state.buckets[0].start + BUCKET_SIZE_IN_MS
|
|
209
|
+
) {
|
|
210
|
+
return this._state.buckets[0];
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (ts < this._state.buckets[0].start) {
|
|
214
|
+
const drift = this._state.buckets[0].start - ts;
|
|
215
|
+
if (drift < MAX_ACCEPTED_DRIFT) {
|
|
216
|
+
logger.warn(
|
|
217
|
+
'Clock jumped backwards: continue since the drift is below the threshold:',
|
|
218
|
+
{ driftInMs: drift, thresholdInMs: MAX_ACCEPTED_DRIFT },
|
|
219
|
+
);
|
|
220
|
+
return this._state.buckets[0];
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
logger.warn(
|
|
224
|
+
'Clock jumped backwards: purging data structure and starting fresh',
|
|
225
|
+
);
|
|
226
|
+
this.reset();
|
|
227
|
+
this._markDirty();
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const idx = Math.floor(ts / BUCKET_SIZE_IN_MS);
|
|
232
|
+
const start = idx * BUCKET_SIZE_IN_MS;
|
|
233
|
+
this._expireBuckets(idx);
|
|
234
|
+
|
|
235
|
+
logger.debug('[estimator] new bucket:', idx);
|
|
236
|
+
this._state.buckets.unshift({ idx, start, urls: {} });
|
|
237
|
+
this._markDirty();
|
|
238
|
+
return this._state.buckets[0];
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
_toBucket(ts) {
|
|
242
|
+
const idx = Math.floor(ts / BUCKET_SIZE_IN_MS);
|
|
243
|
+
const start = idx * BUCKET_SIZE_IN_MS;
|
|
244
|
+
const end = start + BUCKET_SIZE_IN_MS;
|
|
245
|
+
return { idx, start, end };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
_expireBuckets(idx) {
|
|
249
|
+
while (this._state.buckets.length > 0) {
|
|
250
|
+
const last = this._state.buckets[this._state.buckets.length - 1];
|
|
251
|
+
if (last.idx < idx - MAX_ACTIVE_BUCKETS) {
|
|
252
|
+
logger.debug('[estimator] expired bucket:', last.idx);
|
|
253
|
+
this._state.buckets.pop();
|
|
254
|
+
this._markDirty();
|
|
255
|
+
} else {
|
|
256
|
+
break;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
static ensureValidState(state) {
|
|
262
|
+
const { lastUpdated, activeUrl, buckets } = state || {};
|
|
263
|
+
requireInt(lastUpdated, 'lastUpdated');
|
|
264
|
+
requireStringOrNull(activeUrl, 'activeUrl');
|
|
265
|
+
|
|
266
|
+
for (const { idx, start, urls } of buckets) {
|
|
267
|
+
requireInt(idx, 'bucket[].idx');
|
|
268
|
+
requireInt(start, 'bucket[].start');
|
|
269
|
+
for (const [url, { loads, accum, since }] of Object.entries(urls)) {
|
|
270
|
+
requireString(url, 'bucket[].urls[key]');
|
|
271
|
+
requireInt(loads, 'bucket[].urls->loads');
|
|
272
|
+
requireInt(accum, 'bucket[].urls->accum');
|
|
273
|
+
requireIntOrNull(since, 'bucket[].urls->since');
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
return state;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
selfChecks(check = new SelfCheck()) {
|
|
280
|
+
try {
|
|
281
|
+
ActivityEstimator.ensureValidState(this._state);
|
|
282
|
+
} catch (e) {
|
|
283
|
+
check.fail('Invalid state', {
|
|
284
|
+
state: this._state,
|
|
285
|
+
reason: e.message,
|
|
286
|
+
});
|
|
287
|
+
return check;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
if (this._state.buckets.length > MAX_ACTIVE_BUCKETS) {
|
|
291
|
+
check.fail('bucket overrun', {
|
|
292
|
+
buckets: [...this._state.buckets],
|
|
293
|
+
threshold: MAX_ACTIVE_BUCKETS,
|
|
294
|
+
});
|
|
295
|
+
}
|
|
296
|
+
return check;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
@@ -0,0 +1,139 @@
|
|
|
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 { requireString, requireParam } from './utils';
|
|
14
|
+
import random from './random';
|
|
15
|
+
|
|
16
|
+
const HOUR = 1000 * 60 * 60;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Responsible for sending "alive" messages. Alive messages exist
|
|
20
|
+
* to monitor whether the client side of WhoTracks.me reporting
|
|
21
|
+
* is healthy.
|
|
22
|
+
*
|
|
23
|
+
* By separating alive messages from more sensitive messages (e.g.
|
|
24
|
+
* one that may contain a URL), we can include additional information
|
|
25
|
+
* (like the browser) without risking to introduce fingerprinting.
|
|
26
|
+
* Given that there is a correlation between the number of sent
|
|
27
|
+
* "alive" messages and the number of other messages that enables us
|
|
28
|
+
* to detect scenerios like a message drop after a browser release.
|
|
29
|
+
*
|
|
30
|
+
* Note that putting information like the browser directly inside
|
|
31
|
+
* all (non-alive) messages would solve the monitoring problem
|
|
32
|
+
* but at the cost of potentially introducing fingerprinting.
|
|
33
|
+
* Separating monitoring (alive) and other messages limits the
|
|
34
|
+
* risk: even if you could link "alive" messages, they do not
|
|
35
|
+
* contain sensitive information.
|
|
36
|
+
*/
|
|
37
|
+
export default class AliveCheck {
|
|
38
|
+
constructor({
|
|
39
|
+
communication,
|
|
40
|
+
countryProvider,
|
|
41
|
+
trustedClock,
|
|
42
|
+
aliveMessageGenerator,
|
|
43
|
+
storage,
|
|
44
|
+
storageKey,
|
|
45
|
+
}) {
|
|
46
|
+
this.communication = requireParam(communication);
|
|
47
|
+
this.countryProvider = requireParam(countryProvider);
|
|
48
|
+
this.trustedClock = requireParam(trustedClock);
|
|
49
|
+
this.aliveMessageGenerator = requireParam(aliveMessageGenerator);
|
|
50
|
+
this.storage = requireParam(storage);
|
|
51
|
+
this.storageKey = requireString(storageKey);
|
|
52
|
+
this._skipChecksUntil = 0; // Unix timestamp
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Guaranteed to finish fast and never fail. Callers can safely execute it from
|
|
57
|
+
* at any place. Since "alive" messages should correlate with activity, this
|
|
58
|
+
* should be called from places like page visits (triggered by human interaction).
|
|
59
|
+
*
|
|
60
|
+
* What counts as "activity" is currently not specified. A vague definition is
|
|
61
|
+
* that if a user keeps actively using the browser, it should trigger an "alive"
|
|
62
|
+
* message eventually; but if there is no user interaction and the device is idle,
|
|
63
|
+
* it should eventually stop sending signals.
|
|
64
|
+
*/
|
|
65
|
+
ping(now = Date.now()) {
|
|
66
|
+
this._check(now).catch((e) => {
|
|
67
|
+
logger.warn('Error while processing alive checks', e);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async _check(now = Date.now()) {
|
|
72
|
+
if (this._pendingUpdate || now < this._skipChecksUntil) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this._pendingUpdate = (async () => {
|
|
77
|
+
try {
|
|
78
|
+
logger.debug('Checking whether a hourly ping should be sent...');
|
|
79
|
+
const lastSent = await this.storage.get(this.storageKey);
|
|
80
|
+
|
|
81
|
+
const updateSentAt = () =>
|
|
82
|
+
this.storage.set(this.storageKey, { sentAt: now });
|
|
83
|
+
|
|
84
|
+
if (!lastSent) {
|
|
85
|
+
logger.info(
|
|
86
|
+
'Last alive check not found. This should only happen on the first time the extension is started.',
|
|
87
|
+
);
|
|
88
|
+
await updateSentAt();
|
|
89
|
+
return;
|
|
90
|
+
} else if (lastSent.sentAt > now) {
|
|
91
|
+
const now_ = new Date(now);
|
|
92
|
+
const sentAt_ = new Date(lastSent.sentAt);
|
|
93
|
+
logger.warn(
|
|
94
|
+
`Clock jump detected (now=${now_} but sentAt=${sentAt_} is in the future). Resetting counter.`,
|
|
95
|
+
);
|
|
96
|
+
await updateSentAt();
|
|
97
|
+
return;
|
|
98
|
+
} else if (lastSent.sentAt + HOUR > now) {
|
|
99
|
+
logger.debug(
|
|
100
|
+
'Checking whether a hourly ping should be sent... No reached yet.',
|
|
101
|
+
);
|
|
102
|
+
this._skipChecksUntil = lastSent.sentAt + HOUR;
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
logger.debug('Hourly ping reached. Sending...');
|
|
107
|
+
await this._reportAlive();
|
|
108
|
+
this._skipChecksUntil = now + HOUR;
|
|
109
|
+
logger.debug('Hourly ping successfully sent. Saving state...');
|
|
110
|
+
await updateSentAt();
|
|
111
|
+
logger.debug('Hourly ping succeeded.');
|
|
112
|
+
} finally {
|
|
113
|
+
this._pendingUpdate = null;
|
|
114
|
+
}
|
|
115
|
+
})();
|
|
116
|
+
await this._pendingUpdate;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async _reportAlive() {
|
|
120
|
+
const ctry = this.countryProvider.getSafeCountryCode();
|
|
121
|
+
const hour = this.trustedClock.getTimeAsYYYYMMDDHH();
|
|
122
|
+
const payload = await this.aliveMessageGenerator.generateMessage(
|
|
123
|
+
ctry,
|
|
124
|
+
hour,
|
|
125
|
+
);
|
|
126
|
+
const message = {
|
|
127
|
+
action: 'wtm.alive',
|
|
128
|
+
payload,
|
|
129
|
+
ver: 3, // Note: no need to keep this number in sync among messages
|
|
130
|
+
'anti-duplicates': Math.floor(random() * 10000000),
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Note that it is intentional here to bypass the job scheduler.
|
|
134
|
+
// Alive signals should serve as a health check; thus it is best to
|
|
135
|
+
// send in "fire-and-forget" style (without delays or retries).
|
|
136
|
+
logger.debug('Sending alive:', message);
|
|
137
|
+
await this.communication.send(message);
|
|
138
|
+
}
|
|
139
|
+
}
|