@whotracksme/reporting 7.12.3 → 8.0.0

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.
Files changed (31) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/communication/test/helpers/in-memory-database.js +45 -0
  3. package/package.json +1 -1
  4. package/reporting/src/hash-detector-v2/index.js +164 -0
  5. package/reporting/src/hash-detector-v2/prob.js +599 -0
  6. package/reporting/src/hash-detector-v2.js +18 -0
  7. package/reporting/src/observable.js +43 -0
  8. package/reporting/src/pages.js +11 -12
  9. package/reporting/src/paused-domains-reporter.js +50 -0
  10. package/reporting/src/persisted-counters.js +158 -0
  11. package/reporting/src/popularity-estimator.js +625 -0
  12. package/reporting/src/popularity-vote-handler.js +219 -0
  13. package/reporting/src/popularity-vote-sanitizer.js +384 -0
  14. package/reporting/src/random.js +90 -1
  15. package/reporting/src/reporting.js +98 -13
  16. package/reporting/src/utils.js +41 -1
  17. package/reporting/test/activity-estimator.spec.js +3 -2
  18. package/reporting/test/alive-check.spec.js +2 -2
  19. package/reporting/test/country-provider.spec.js +2 -2
  20. package/reporting/test/index.js +6 -0
  21. package/reporting/test/job-scheduler.spec.js +2 -2
  22. package/reporting/test/observable.spec.js +72 -0
  23. package/reporting/test/page-aggregator.spec.js +3 -0
  24. package/reporting/test/patterns-updater.spec.js +2 -2
  25. package/reporting/test/persisted-counters.spec.js +132 -0
  26. package/reporting/test/popularity-estimator.spec.js +710 -0
  27. package/reporting/test/popularity-vote-handler.spec.js +420 -0
  28. package/reporting/test/popularity-vote-sanitizer.spec.js +635 -0
  29. package/reporting/test/random.spec.js +157 -0
  30. package/reporting/test/reporting.spec.js +202 -180
  31. package/reporting/test/utils.spec.js +42 -0
@@ -0,0 +1,157 @@
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 { expect } from 'chai';
13
+ import * as fc from 'fast-check';
14
+
15
+ import { randomSafeIntBetween, shuffleInPlace } from '../src/random.js';
16
+
17
+ describe('#randomSafeIntBetween', function () {
18
+ it('should reach all buckets of six-side dime', function () {
19
+ const bucket = {
20
+ 1: 0,
21
+ 2: 0,
22
+ 3: 0,
23
+ 4: 0,
24
+ 5: 0,
25
+ 6: 0,
26
+ };
27
+ for (let i = 0; i < 6 * 1000; i += 1) {
28
+ bucket[randomSafeIntBetween(1, 6)] += 1;
29
+ }
30
+
31
+ for (let i = 1; i <= 6; i += 1) {
32
+ expect(bucket[i]).to.be.greaterThan(800).and.lessThan(1200);
33
+ }
34
+ });
35
+
36
+ it('should reach all buckets of { 0, 1, 2 }', function () {
37
+ const bucket = [0, 0, 0];
38
+ for (let i = 0; i < 3 * 1000; i += 1) {
39
+ bucket[randomSafeIntBetween(0, 2)] += 1;
40
+ }
41
+
42
+ for (const hits of bucket) {
43
+ expect(hits).to.be.greaterThan(800).and.lessThan(1200);
44
+ }
45
+ });
46
+
47
+ describe('should support the full range of safe integers', function () {
48
+ it('{ Number.MIN_SAFE_INTEGER, .., Number.MAX_SAFE_INTEGER }', function () {
49
+ let above0 = 0;
50
+ let below0 = 0;
51
+ for (let i = 0; i < 2 * 1000; i += 1) {
52
+ const num = randomSafeIntBetween(
53
+ Number.MIN_SAFE_INTEGER,
54
+ Number.MAX_SAFE_INTEGER,
55
+ );
56
+ expect(Number.isSafeInteger(num)).to.be.true;
57
+ if (num > 0) {
58
+ above0 += 1;
59
+ } else if (num < 0) {
60
+ below0 += 1;
61
+ }
62
+ }
63
+
64
+ expect(above0 + below0).to.be.greaterThan(998);
65
+ expect(above0).to.be.greaterThan(800).and.lessThan(1200);
66
+ expect(below0).to.be.greaterThan(800).and.lessThan(1200);
67
+ });
68
+
69
+ describe('testing edge case that are barely within or barely outside the safe integers range', function () {
70
+ [-3, -2, -1, 0, 1].forEach((min) => {
71
+ [0, 1, 2, 3]
72
+ .map((maxDec) => Number.MAX_SAFE_INTEGER - maxDec)
73
+ .forEach((max) => {
74
+ it(`{ ${min}, .., ${max} }`, function () {
75
+ const middle = (max - min) / 2;
76
+ let above = 0;
77
+ let below = 0;
78
+ for (let i = 0; i < 2 * 1000; i += 1) {
79
+ const num = randomSafeIntBetween(min, max);
80
+ expect(Number.isSafeInteger(num)).to.be.true;
81
+
82
+ if (num > middle) {
83
+ above += 1;
84
+ } else if (num < middle) {
85
+ below += 1;
86
+ }
87
+ }
88
+
89
+ expect(above + below).to.be.greaterThan(998);
90
+ expect(above).to.be.greaterThan(800).and.lessThan(1200);
91
+ expect(below).to.be.greaterThan(800).and.lessThan(1200);
92
+ });
93
+ });
94
+ });
95
+ });
96
+ });
97
+
98
+ describe('[property based testing]', function () {
99
+ it('should support intervals where min equals max', function () {
100
+ fc.assert(
101
+ fc.property(fc.maxSafeInteger(), (num) => {
102
+ return randomSafeIntBetween(num, num) === num;
103
+ }),
104
+ );
105
+ });
106
+
107
+ it('should return an integer between min and max', function () {
108
+ fc.assert(
109
+ fc.property(fc.maxSafeInteger(), fc.maxSafeInteger(), (min, max) => {
110
+ if (min > max) {
111
+ return true;
112
+ }
113
+
114
+ const result = randomSafeIntBetween(min, max);
115
+ return Number.isSafeInteger(result) && result >= min && result <= max;
116
+ }),
117
+ );
118
+ });
119
+ });
120
+ });
121
+
122
+ describe('#shuffleInPlace', function () {
123
+ it('should have uniform distribution of elements in each position for small arrays', function () {
124
+ const counts = [
125
+ [0, 0, 0],
126
+ [0, 0, 0],
127
+ [0, 0, 0],
128
+ ];
129
+
130
+ for (let i = 0; i < 3 * 1000; i++) {
131
+ const [x, y, z] = shuffleInPlace([0, 1, 2]);
132
+ counts[0][x] += 1;
133
+ counts[1][y] += 1;
134
+ counts[2][z] += 1;
135
+ }
136
+
137
+ for (let i = 0; i < 3; i += 1) {
138
+ for (let j = 0; j < 3; j += 1) {
139
+ expect(counts[i][j])
140
+ .to.be.greaterThan(1000 * 0.8)
141
+ .and.lessThan(1000 * 1.2);
142
+ }
143
+ }
144
+ });
145
+
146
+ describe('[property based testing]', function () {
147
+ it('should produce a permutation of the input array: sort(shuffle(arr)) == sort(arr)', function () {
148
+ fc.assert(
149
+ fc.property(fc.array(fc.integer()), (arr) => {
150
+ const shuffled = shuffleInPlace([...arr]).sort((x, y) => x - y);
151
+ const notShuffled = [...arr].sort((x, y) => x - y);
152
+ expect(shuffled).to.eql(notShuffled);
153
+ }),
154
+ );
155
+ });
156
+ });
157
+ });
@@ -14,6 +14,8 @@ import sinon from 'sinon';
14
14
 
15
15
  import Reporting from '../src/reporting.js';
16
16
 
17
+ import InMemoryDatabase from './helpers/in-memory-database.js';
18
+
17
19
  function waitForPromisesToFinish() {
18
20
  return new Promise((resolve) => setTimeout(resolve, 0));
19
21
  }
@@ -21,184 +23,204 @@ function waitForPromisesToFinish() {
21
23
  describe('#Reporting', function () {
22
24
  let uut;
23
25
 
24
- beforeEach(async function () {
25
- const config = {
26
- ALLOWED_COUNTRY_CODES: ['us', 'de'],
27
- PATTERNS_URL: 'https://some-patterns-endpoint.test',
28
- CONFIG_URL: 'https://some-config-endpoint.test',
29
- };
30
- const storage = {
31
- get: () => undefined, // assume nothing was stored yet
32
- flush: () => {},
33
- };
34
- const communication = {
35
- async send() {},
36
- trustedClock: {},
37
- };
38
- const connectDatabase = () => {};
39
- sinon.stub(window, 'fetch').callsFake(async (url) => ({
40
- ok: false,
41
- statusText: `Stub server has been configured to fail (this is expected): request to ${url}`,
42
- }));
43
- uut = new Reporting({
44
- config,
45
- storage,
46
- communication,
47
- connectDatabase,
48
- });
49
- });
50
-
51
- afterEach(function () {
52
- try {
53
- uut.unload();
54
- uut = null;
55
- } finally {
56
- window.fetch.restore();
57
- }
58
- });
59
-
60
- describe('should load and unload correctly', function () {
61
- it('happy path', async () => {
62
- expect(uut.isActive).to.be.false;
63
- await uut.init();
64
- expect(uut.isActive).to.be.true;
65
- uut.unload();
66
- expect(uut.isActive).to.be.false;
67
- });
68
-
69
- it('multiple inits should be OK', async () => {
70
- await Promise.all([uut.init(), uut.init(), uut.init()]);
71
- expect(uut.isActive).to.be.true;
72
- });
73
-
74
- it('multiple unloads should be OK', async () => {
75
- uut.unload();
76
- uut.unload();
77
- uut.unload();
78
- expect(uut.isActive).to.be.false;
79
- });
80
-
81
- it('multiple mixed init/unloads should be OK', async () => {
82
- const pending = [];
83
- pending.push(uut.init());
84
- pending.push(uut.init());
85
- pending.push(uut.init());
86
-
87
- uut.unload();
88
- expect(uut.isActive).to.be.false;
89
-
90
- pending.push(uut.init());
91
- uut.unload();
92
- expect(uut.isActive).to.be.false;
93
-
94
- uut.unload();
95
- expect(uut.isActive).to.be.false;
96
-
97
- await Promise.all(pending);
98
- uut.unload();
99
- expect(uut.isActive).to.be.false;
100
-
101
- await uut.init();
102
- expect(uut.isActive).to.be.true;
103
-
104
- uut.unload();
105
- expect(uut.isActive).to.be.false;
106
- });
107
-
108
- describe('should make ensure that "unload" at the end always wins', () => {
109
- it('when calling init once', async () => {
110
- const pending = uut.init();
111
- uut.unload();
112
- expect(uut.isActive).to.be.false;
113
-
114
- await pending;
115
- await waitForPromisesToFinish();
116
- expect(uut.isActive).to.be.false;
117
- });
118
-
119
- it('when calling init multiple times', async () => {
120
- const pending = [];
121
- pending.push(uut.init());
122
- pending.push(uut.init());
123
- pending.push(uut.init());
124
- uut.unload();
125
- expect(uut.isActive).to.be.false;
126
-
127
- await Promise.all(pending);
128
- await waitForPromisesToFinish();
129
- expect(uut.isActive).to.be.false;
130
-
131
- // and addition unload operations should not change anything
132
- uut.unload();
133
- expect(uut.isActive).to.be.false;
134
- uut.unload();
135
- uut.unload();
136
- expect(uut.isActive).to.be.false;
137
- });
138
-
139
- it('when calling unload multiple times', async () => {
140
- const pending = [];
141
- pending.push(uut.init());
142
- pending.push(uut.init());
143
- pending.push(uut.init());
144
- uut.unload();
145
- expect(uut.isActive).to.be.false;
146
- uut.unload();
147
- expect(uut.isActive).to.be.false;
148
-
149
- await Promise.all(pending);
150
- await waitForPromisesToFinish();
151
- expect(uut.isActive).to.be.false;
152
-
153
- // and addition unload operations should not change anything
154
- uut.unload();
155
- expect(uut.isActive).to.be.false;
156
- uut.unload();
157
- uut.unload();
158
- expect(uut.isActive).to.be.false;
159
- });
160
- });
161
-
162
- describe('should make ensure that "init" at the end always wins', () => {
163
- it('in a simple example', async () => {
164
- const pending = [];
165
- pending.push(uut.init());
166
- uut.unload();
167
- expect(uut.isActive).to.be.false;
168
- pending.push(uut.init());
169
-
170
- await Promise.all([pending]);
171
- await waitForPromisesToFinish();
172
- expect(uut.isActive).to.be.true;
173
- });
174
-
175
- it('in a complex example', async () => {
176
- const pending = [];
177
- pending.push(uut.init());
178
- uut.unload();
179
- expect(uut.isActive).to.be.false;
180
- pending.push(uut.init());
181
- pending.push(uut.init());
182
- uut.unload();
183
- expect(uut.isActive).to.be.false;
184
- pending.push(uut.init());
185
- pending.push(uut.init());
186
- uut.unload();
187
- expect(uut.isActive).to.be.false;
188
- uut.unload();
189
- expect(uut.isActive).to.be.false;
190
- pending.push(uut.init());
191
-
192
- await Promise.all([pending]);
193
- await waitForPromisesToFinish();
194
- expect(uut.isActive).to.be.true;
195
-
196
- await uut.init();
197
- expect(uut.isActive).to.be.true;
198
-
199
- await uut.init();
200
- expect(uut.isActive).to.be.true;
201
- });
202
- });
203
- });
26
+ for (const withPauseState of [true, false]) {
27
+ describe(
28
+ withPauseState
29
+ ? '"pauseState" config present'
30
+ : '"pauseState" config absent',
31
+
32
+ function () {
33
+ beforeEach(async function () {
34
+ const config = {
35
+ ALLOWED_COUNTRY_CODES: ['us', 'de'],
36
+ PATTERNS_URL: 'https://some-patterns-endpoint.test',
37
+ CONFIG_URL: 'https://some-config-endpoint.test',
38
+ };
39
+ const storage = {
40
+ get: async () => undefined, // assume nothing was stored yet
41
+ };
42
+ const communication = {
43
+ async send() {},
44
+ trustedClock: {},
45
+ };
46
+ const connectDatabase = (/*prefix*/) => new InMemoryDatabase();
47
+ sinon.stub(window, 'fetch').callsFake(async (url) => ({
48
+ ok: false,
49
+ statusText: `Stub server has been configured to fail (this is expected): request to ${url}`,
50
+ }));
51
+
52
+ // optional components
53
+ let pauseState;
54
+ if (withPauseState) {
55
+ pauseState = {
56
+ getFilteringMode: () => 'default',
57
+ isHostnamePaused: (/*hostname*/) => false,
58
+ connectHostnamePausingEvents: (/*notify*/) => {},
59
+ };
60
+ }
61
+ uut = new Reporting({
62
+ config,
63
+ storage,
64
+ communication,
65
+ connectDatabase,
66
+ pauseState,
67
+ });
68
+ });
69
+
70
+ afterEach(function () {
71
+ try {
72
+ uut.unload();
73
+ uut = null;
74
+ } finally {
75
+ window.fetch.restore();
76
+ }
77
+ });
78
+
79
+ describe('should load and unload correctly', function () {
80
+ it('happy path', async () => {
81
+ expect(uut.isActive).to.be.false;
82
+ await uut.init();
83
+ expect(uut.isActive).to.be.true;
84
+ uut.unload();
85
+ expect(uut.isActive).to.be.false;
86
+ });
87
+
88
+ it('multiple inits should be OK', async () => {
89
+ await Promise.all([uut.init(), uut.init(), uut.init()]);
90
+ expect(uut.isActive).to.be.true;
91
+ });
92
+
93
+ it('multiple unloads should be OK', async () => {
94
+ uut.unload();
95
+ uut.unload();
96
+ uut.unload();
97
+ expect(uut.isActive).to.be.false;
98
+ });
99
+
100
+ it('multiple mixed init/unloads should be OK', async () => {
101
+ const pending = [];
102
+ pending.push(uut.init());
103
+ pending.push(uut.init());
104
+ pending.push(uut.init());
105
+
106
+ uut.unload();
107
+ expect(uut.isActive).to.be.false;
108
+
109
+ pending.push(uut.init());
110
+ uut.unload();
111
+ expect(uut.isActive).to.be.false;
112
+
113
+ uut.unload();
114
+ expect(uut.isActive).to.be.false;
115
+
116
+ await Promise.all(pending);
117
+ uut.unload();
118
+ expect(uut.isActive).to.be.false;
119
+
120
+ await uut.init();
121
+ expect(uut.isActive).to.be.true;
122
+
123
+ uut.unload();
124
+ expect(uut.isActive).to.be.false;
125
+ });
126
+
127
+ describe('should make ensure that "unload" at the end always wins', () => {
128
+ it('when calling init once', async () => {
129
+ const pending = uut.init();
130
+ uut.unload();
131
+ expect(uut.isActive).to.be.false;
132
+
133
+ await pending;
134
+ await waitForPromisesToFinish();
135
+ expect(uut.isActive).to.be.false;
136
+ });
137
+
138
+ it('when calling init multiple times', async () => {
139
+ const pending = [];
140
+ pending.push(uut.init());
141
+ pending.push(uut.init());
142
+ pending.push(uut.init());
143
+ uut.unload();
144
+ expect(uut.isActive).to.be.false;
145
+
146
+ await Promise.all(pending);
147
+ await waitForPromisesToFinish();
148
+ expect(uut.isActive).to.be.false;
149
+
150
+ // and addition unload operations should not change anything
151
+ uut.unload();
152
+ expect(uut.isActive).to.be.false;
153
+ uut.unload();
154
+ uut.unload();
155
+ expect(uut.isActive).to.be.false;
156
+ });
157
+
158
+ it('when calling unload multiple times', async () => {
159
+ const pending = [];
160
+ pending.push(uut.init());
161
+ pending.push(uut.init());
162
+ pending.push(uut.init());
163
+ uut.unload();
164
+ expect(uut.isActive).to.be.false;
165
+ uut.unload();
166
+ expect(uut.isActive).to.be.false;
167
+
168
+ await Promise.all(pending);
169
+ await waitForPromisesToFinish();
170
+ expect(uut.isActive).to.be.false;
171
+
172
+ // and addition unload operations should not change anything
173
+ uut.unload();
174
+ expect(uut.isActive).to.be.false;
175
+ uut.unload();
176
+ uut.unload();
177
+ expect(uut.isActive).to.be.false;
178
+ });
179
+ });
180
+
181
+ describe('should make ensure that "init" at the end always wins', () => {
182
+ it('in a simple example', async () => {
183
+ const pending = [];
184
+ pending.push(uut.init());
185
+ uut.unload();
186
+ expect(uut.isActive).to.be.false;
187
+ pending.push(uut.init());
188
+
189
+ await Promise.all([pending]);
190
+ await waitForPromisesToFinish();
191
+ expect(uut.isActive).to.be.true;
192
+ });
193
+
194
+ it('in a complex example', async () => {
195
+ const pending = [];
196
+ pending.push(uut.init());
197
+ uut.unload();
198
+ expect(uut.isActive).to.be.false;
199
+ pending.push(uut.init());
200
+ pending.push(uut.init());
201
+ uut.unload();
202
+ expect(uut.isActive).to.be.false;
203
+ pending.push(uut.init());
204
+ pending.push(uut.init());
205
+ uut.unload();
206
+ expect(uut.isActive).to.be.false;
207
+ uut.unload();
208
+ expect(uut.isActive).to.be.false;
209
+ pending.push(uut.init());
210
+
211
+ await Promise.all([pending]);
212
+ await waitForPromisesToFinish();
213
+ expect(uut.isActive).to.be.true;
214
+
215
+ await uut.init();
216
+ expect(uut.isActive).to.be.true;
217
+
218
+ await uut.init();
219
+ expect(uut.isActive).to.be.true;
220
+ });
221
+ });
222
+ });
223
+ },
224
+ );
225
+ }
204
226
  });
@@ -16,6 +16,7 @@ import {
16
16
  chunk,
17
17
  flattenObject,
18
18
  equalityCanBeProven,
19
+ includesMiddleChar,
19
20
  parseUntrustedJSON,
20
21
  lazyInitAsync,
21
22
  } from '../src/utils.js';
@@ -154,6 +155,47 @@ describe('#equalityCanBeProven', function () {
154
155
  });
155
156
  });
156
157
 
158
+ describe('#includesMiddleChar', function () {
159
+ it('should detect inner matches', function () {
160
+ expect(includesMiddleChar('a+b', '+')).to.be.true;
161
+ expect(includesMiddleChar('a+b+c', '+')).to.be.true;
162
+ expect(includesMiddleChar('+a+b+c', '+')).to.be.true;
163
+ expect(includesMiddleChar('a+b+c+', '+')).to.be.true;
164
+ expect(includesMiddleChar('+a+b+c+', '+')).to.be.true;
165
+ expect(includesMiddleChar('++a++b++c++', '+')).to.be.true;
166
+ expect(includesMiddleChar('+a+b+', '+')).to.be.true;
167
+ });
168
+
169
+ it('should ignore leading matches', function () {
170
+ expect(includesMiddleChar('+ab', '+')).to.be.false;
171
+ expect(includesMiddleChar('++ab', '+')).to.be.false;
172
+ expect(includesMiddleChar('++ab', '+')).to.be.false;
173
+ });
174
+
175
+ it('should ignore trailing matches', function () {
176
+ expect(includesMiddleChar('ab+', '+')).to.be.false;
177
+ expect(includesMiddleChar('ab++', '+')).to.be.false;
178
+ expect(includesMiddleChar('ab+++', '+')).to.be.false;
179
+ });
180
+
181
+ it('should handle empty inputs', function () {
182
+ expect(includesMiddleChar('', '+')).be.false;
183
+ });
184
+
185
+ it('should handle inputs with no other chars', function () {
186
+ expect(includesMiddleChar('+', '+')).be.false;
187
+ expect(includesMiddleChar('++', '+')).be.false;
188
+ expect(includesMiddleChar('+++', '+')).be.false;
189
+ });
190
+
191
+ it('should handle non-matching inputs', function () {
192
+ expect(includesMiddleChar('foo', '+')).be.false;
193
+ expect(includesMiddleChar('+a+', '+')).be.false;
194
+ expect(includesMiddleChar('+ab+', '+')).be.false;
195
+ expect(includesMiddleChar('+++a+++', '+')).be.false;
196
+ });
197
+ });
198
+
157
199
  describe('#parseUntrustedJSON', function () {
158
200
  describe('should be a drop-in replacement for JSON.parse for safe data', function () {
159
201
  for (const json of [