@whotracksme/reporting 7.12.3 → 8.0.1

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 (44) hide show
  1. package/.eslintignore +1 -0
  2. package/CHANGELOG.md +24 -0
  3. package/communication/test/helpers/in-memory-database.js +45 -0
  4. package/package.json +1 -1
  5. package/reporting/README.md +26 -0
  6. package/reporting/example/README.md +65 -0
  7. package/reporting/example/logs/.gitignore +2 -0
  8. package/reporting/example/run.mjs +288 -0
  9. package/reporting/karma.conf.cjs +3 -2
  10. package/reporting/package.json +8 -2
  11. package/reporting/rollup.common-config.cjs +7 -1
  12. package/reporting/scenarios/README.md +77 -0
  13. package/reporting/src/hash-detector-v2/index.js +164 -0
  14. package/reporting/src/hash-detector-v2/prob.js +599 -0
  15. package/reporting/src/hash-detector-v2.js +18 -0
  16. package/reporting/src/observable.js +43 -0
  17. package/reporting/src/pages.js +11 -12
  18. package/reporting/src/paused-domains-reporter.js +50 -0
  19. package/reporting/src/persisted-counters.js +158 -0
  20. package/reporting/src/popularity-estimator.js +625 -0
  21. package/reporting/src/popularity-vote-handler.js +219 -0
  22. package/reporting/src/popularity-vote-sanitizer.js +384 -0
  23. package/reporting/src/random.js +90 -1
  24. package/reporting/src/reporting.js +98 -13
  25. package/reporting/src/utils.js +41 -1
  26. package/reporting/test/README.md +42 -2
  27. package/reporting/test/activity-estimator.spec.js +3 -2
  28. package/reporting/test/alive-check.spec.js +2 -2
  29. package/reporting/test/country-provider.spec.js +2 -2
  30. package/reporting/test/helpers/scenarios.js +4 -1
  31. package/reporting/test/index.js +6 -0
  32. package/reporting/test/job-scheduler.spec.js +2 -2
  33. package/reporting/test/observable.spec.js +72 -0
  34. package/reporting/test/page-aggregator.spec.js +3 -0
  35. package/reporting/test/patterns-updater.spec.js +2 -2
  36. package/reporting/test/persisted-counters.spec.js +132 -0
  37. package/reporting/test/popularity-estimator.spec.js +710 -0
  38. package/reporting/test/popularity-vote-handler.spec.js +420 -0
  39. package/reporting/test/popularity-vote-sanitizer.spec.js +635 -0
  40. package/reporting/test/random.spec.js +157 -0
  41. package/reporting/test/reporting.spec.js +202 -180
  42. package/reporting/test/request/index.test.js +6 -2
  43. package/reporting/test/request.js +14 -0
  44. package/reporting/test/utils.spec.js +42 -0
@@ -0,0 +1,164 @@
1
+ /* This Source Code Form is subject to the terms of the Mozilla Public
2
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
3
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
+
5
+ import prob from './prob.js';
6
+
7
+ // Prefilters — instant classification for unambiguous patterns
8
+ const RE_PURE_NUMBERS = /^\d+$/;
9
+ const RE_RESOLUTION = /^\d+x\d+$/;
10
+ const RE_UK_POSTCODE = /^[A-Z]{1,2}\d[A-Z\d]?\d[A-Z]{2}$/i;
11
+ const RE_UUID =
12
+ /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
13
+ const RE_PERCENT_ENCODED = /%[0-9A-Fa-f]{2}/;
14
+
15
+ export class HashProb {
16
+ constructor() {
17
+ this.probHashLogM = prob.logM;
18
+ this.classifierWeights = prob.classifierWeights;
19
+ this.probHashChars = {};
20
+ 'abcdefghijklmnopqrstuvwxyz1234567890.- '.split('').forEach((e, idx) => {
21
+ this.probHashChars[e] = idx;
22
+ });
23
+ }
24
+
25
+ isHashProb(str) {
26
+ if (!this.probHashLogM) {
27
+ return 0;
28
+ }
29
+ let logProb = 0.0;
30
+ let transC = 0;
31
+ str = str.toLowerCase().replace(/[^a-z0-9.\- ]/g, '');
32
+ for (let i = 0; i < str.length - 1; i += 1) {
33
+ const pos1 = this.probHashChars[str[i]];
34
+ const pos2 = this.probHashChars[str[i + 1]];
35
+
36
+ logProb += this.probHashLogM[pos1][pos2];
37
+ transC += 1;
38
+ }
39
+ if (transC > 0) {
40
+ return Math.exp(logProb / transC);
41
+ }
42
+ return Math.exp(logProb);
43
+ }
44
+
45
+ isHash(str) {
46
+ // Do not consider small strings
47
+ if (str.length < 6) {
48
+ return false;
49
+ }
50
+
51
+ // Layer 1: URL-decode preprocessing (always applies)
52
+ if (str.includes('%') && RE_PERCENT_ENCODED.test(str)) {
53
+ try {
54
+ str = decodeURIComponent(str);
55
+ } catch (e) {
56
+ /* keep original */
57
+ }
58
+ if (str.length < 6) {
59
+ return false;
60
+ }
61
+ }
62
+
63
+ // Instant false: known safe formats
64
+ if (RE_PURE_NUMBERS.test(str)) return false;
65
+ if (RE_RESOLUTION.test(str)) return false;
66
+ if (RE_UK_POSTCODE.test(str)) return false;
67
+
68
+ // Instant true: UUID is the one pattern the LR classifier sometimes misses
69
+ if (RE_UUID.test(str)) return true;
70
+
71
+ // Multi-feature logistic regression
72
+ const bigramProb = this.isHashProb(str);
73
+ const features = extractFeatures(str, bigramProb);
74
+ const w = this.classifierWeights;
75
+
76
+ const score =
77
+ w[0] +
78
+ w[1] * features.bigramProb +
79
+ w[2] * features.entropy +
80
+ w[3] * features.hexRatio +
81
+ w[4] * features.vowelRatio +
82
+ w[5] * features.transitions;
83
+
84
+ const probability = 1 / (1 + Math.exp(-score));
85
+ return probability > (w[6] ?? 0.5);
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Extract all 5 features from a string in a single pass.
91
+ * All features are O(n) in string length.
92
+ */
93
+ function extractFeatures(str, bigramProb) {
94
+ const lower = str.toLowerCase();
95
+ const len = lower.length;
96
+
97
+ // Character frequency counts for entropy + hex ratio + vowel ratio
98
+ const freq = {};
99
+ let hexCount = 0;
100
+ let vowelCount = 0;
101
+ let alphaCount = 0;
102
+ let transitionCount = 0;
103
+
104
+ // Previous character class: 0=letter, 1=digit, -1=other
105
+ let prevClass = -1;
106
+
107
+ for (let i = 0; i < len; i++) {
108
+ const c = lower[i];
109
+ const code = lower.charCodeAt(i);
110
+
111
+ // Frequency counting for entropy
112
+ freq[c] = (freq[c] || 0) + 1;
113
+
114
+ // Hex character ratio
115
+ if (
116
+ (code >= 48 && code <= 57) || // 0-9
117
+ (code >= 97 && code <= 102)
118
+ ) {
119
+ // a-f
120
+ hexCount++;
121
+ }
122
+
123
+ // Vowel ratio (among alphabetic chars)
124
+ if (code >= 97 && code <= 122) {
125
+ // a-z
126
+ alphaCount++;
127
+ if (c === 'a' || c === 'e' || c === 'i' || c === 'o' || c === 'u') {
128
+ vowelCount++;
129
+ }
130
+ }
131
+
132
+ // Char-class transitions (letter <-> digit)
133
+ let curClass;
134
+ if (code >= 97 && code <= 122) {
135
+ curClass = 0; // letter
136
+ } else if (code >= 48 && code <= 57) {
137
+ curClass = 1; // digit
138
+ } else {
139
+ curClass = -1; // other
140
+ }
141
+
142
+ if (prevClass >= 0 && curClass >= 0 && prevClass !== curClass) {
143
+ transitionCount++;
144
+ }
145
+ if (curClass >= 0) {
146
+ prevClass = curClass;
147
+ }
148
+ }
149
+
150
+ // Shannon entropy: -sum(p(c) * log2(p(c)))
151
+ let entropy = 0;
152
+ for (const c in freq) {
153
+ const p = freq[c] / len;
154
+ entropy -= p * Math.log2(p);
155
+ }
156
+
157
+ return {
158
+ bigramProb,
159
+ entropy,
160
+ hexRatio: len > 0 ? hexCount / len : 0,
161
+ vowelRatio: alphaCount > 0 ? vowelCount / alphaCount : 0,
162
+ transitions: len > 1 ? transitionCount / (len - 1) : 0,
163
+ };
164
+ }