@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,689 @@
|
|
|
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
|
+
|
|
14
|
+
import DuplicateDetector, {
|
|
15
|
+
computeDeduplicationKey,
|
|
16
|
+
} from '../src/duplicate-detector.js';
|
|
17
|
+
import PersistedHashes from '../src/persisted-hashes.js';
|
|
18
|
+
import MemoryPersistentMap from './helpers/memory-map.js';
|
|
19
|
+
|
|
20
|
+
function getUniqueId() {
|
|
21
|
+
getUniqueId.uniqueId = (getUniqueId.uniqueId || 0) + 1;
|
|
22
|
+
return getUniqueId.uniqueId;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/* eslint-disable no-param-reassign */
|
|
26
|
+
function mkMessage({ action = 'test-action', payload, deduplicateBy } = {}) {
|
|
27
|
+
payload = payload || {
|
|
28
|
+
testFieldWithSeq: `test-field-${getUniqueId()}`,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const body = {
|
|
32
|
+
type: 'test-type',
|
|
33
|
+
action,
|
|
34
|
+
payload,
|
|
35
|
+
ver: 3,
|
|
36
|
+
'anti-duplicates': 1,
|
|
37
|
+
};
|
|
38
|
+
return { body, deduplicateBy };
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('#DuplicateDetector', function () {
|
|
42
|
+
let uut;
|
|
43
|
+
let persistedHashes;
|
|
44
|
+
|
|
45
|
+
beforeEach(async function () {
|
|
46
|
+
// in-memory implementation of storage
|
|
47
|
+
const storage = new MemoryPersistentMap();
|
|
48
|
+
|
|
49
|
+
persistedHashes = new PersistedHashes({
|
|
50
|
+
storage,
|
|
51
|
+
storageKey: 'dummy-storage-key',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
uut = new DuplicateDetector(persistedHashes);
|
|
55
|
+
await uut.init();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
afterEach(() => {
|
|
59
|
+
uut.unload();
|
|
60
|
+
uut = null;
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
async function expectSendOK(msg) {
|
|
64
|
+
const result = await uut.trySend(msg);
|
|
65
|
+
expect(result.ok).to.be.true;
|
|
66
|
+
expect(result.rollback).to.be.a('function');
|
|
67
|
+
return result;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
async function expectSendNotOK(msg) {
|
|
71
|
+
const result = await uut.trySend(msg);
|
|
72
|
+
expect(result.ok).to.be.false;
|
|
73
|
+
expect(result.rejectReason).to.be.a('string').that.is.not.empty;
|
|
74
|
+
return result;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
it('should accept a valid message', async function () {
|
|
78
|
+
await expectSendOK(mkMessage());
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('should not accept the same message twice (one arriving after the other)', async function () {
|
|
82
|
+
const msg = mkMessage();
|
|
83
|
+
await expectSendOK(msg);
|
|
84
|
+
await expectSendNotOK(msg);
|
|
85
|
+
await expectSendNotOK(msg);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should not accept the same message twice (both arriving at the same time)', async function () {
|
|
89
|
+
const msg = mkMessage();
|
|
90
|
+
const result = await Promise.all([
|
|
91
|
+
uut.trySend(msg),
|
|
92
|
+
uut.trySend(msg),
|
|
93
|
+
uut.trySend(msg),
|
|
94
|
+
]);
|
|
95
|
+
expect(result.map((x) => x.ok).sort()).to.eql([false, false, true]); // exactly one succeeds
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should allow to send it again after rolling back', async function () {
|
|
99
|
+
const msg = mkMessage();
|
|
100
|
+
const { rollback } = await expectSendOK(msg);
|
|
101
|
+
|
|
102
|
+
await expectSendNotOK(msg); // duplicate
|
|
103
|
+
rollback();
|
|
104
|
+
await expectSendOK(msg);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('should detect duplicated messages even keys differ, but ignore non-key fields', async function () {
|
|
108
|
+
const q = 'some key shared in both messages';
|
|
109
|
+
const deduplicateBy = 'q';
|
|
110
|
+
|
|
111
|
+
await expectSendOK(
|
|
112
|
+
mkMessage({
|
|
113
|
+
payload: {
|
|
114
|
+
q,
|
|
115
|
+
nonKey: 'This is not part of the key',
|
|
116
|
+
},
|
|
117
|
+
deduplicateBy, // but ignore "nonKey"
|
|
118
|
+
}),
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
await expectSendNotOK(
|
|
122
|
+
mkMessage({
|
|
123
|
+
payload: {
|
|
124
|
+
q,
|
|
125
|
+
nonKey:
|
|
126
|
+
'This is a different text, but it is a duplicate nevertheless',
|
|
127
|
+
},
|
|
128
|
+
deduplicateBy,
|
|
129
|
+
}),
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
await expectSendOK(
|
|
133
|
+
mkMessage({
|
|
134
|
+
payload: {
|
|
135
|
+
q: 'another key, so it no longer a duplicate',
|
|
136
|
+
nonKey: 'This is not part of the key',
|
|
137
|
+
},
|
|
138
|
+
deduplicateBy,
|
|
139
|
+
}),
|
|
140
|
+
);
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('#computeDeduplicationKey', function () {
|
|
145
|
+
function computeKeysForPayload(payload1, payload2, by) {
|
|
146
|
+
const action = 'action';
|
|
147
|
+
const ts = '20220923';
|
|
148
|
+
const deduplicateBy = by;
|
|
149
|
+
const key1 = computeDeduplicationKey({
|
|
150
|
+
body: {
|
|
151
|
+
action,
|
|
152
|
+
payload: payload1,
|
|
153
|
+
ts,
|
|
154
|
+
},
|
|
155
|
+
deduplicateBy,
|
|
156
|
+
});
|
|
157
|
+
expect(key1).to.be.a('string').that.is.not.empty;
|
|
158
|
+
|
|
159
|
+
const key2 = computeDeduplicationKey({
|
|
160
|
+
body: {
|
|
161
|
+
action,
|
|
162
|
+
payload: payload2,
|
|
163
|
+
ts,
|
|
164
|
+
},
|
|
165
|
+
deduplicateBy,
|
|
166
|
+
});
|
|
167
|
+
expect(key2).to.be.a('string').that.is.not.empty;
|
|
168
|
+
|
|
169
|
+
return [key1, key2];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function duplicatedPayload(payload1, payload2, by) {
|
|
173
|
+
const [key1, key2] = computeKeysForPayload(payload1, payload2, by);
|
|
174
|
+
if (key1 !== key2) {
|
|
175
|
+
expect.fail(
|
|
176
|
+
[
|
|
177
|
+
`The following two payloads should have been detected as duplicates (key fields=<${by}>):`,
|
|
178
|
+
JSON.stringify(payload1, null, 2),
|
|
179
|
+
JSON.stringify(payload2, null, 2),
|
|
180
|
+
'but their computed keys differ:',
|
|
181
|
+
key1,
|
|
182
|
+
'!=',
|
|
183
|
+
key2,
|
|
184
|
+
].join('\n'),
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function nonDuplicatedPayload(payload1, payload2, by) {
|
|
190
|
+
const [key1, key2] = computeKeysForPayload(payload1, payload2, by);
|
|
191
|
+
if (key1 === key2) {
|
|
192
|
+
expect.fail(
|
|
193
|
+
[
|
|
194
|
+
`The following two payloads should not have been detected as duplicates (key fields=<${by}>):`,
|
|
195
|
+
JSON.stringify(payload1, null, 2),
|
|
196
|
+
JSON.stringify(payload2, null, 2),
|
|
197
|
+
`but their computed key is the same: ${key1}`,
|
|
198
|
+
].join('\n'),
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
it('should differ iff any part of the <action, timestamp, key fields> tuple differs', function () {
|
|
204
|
+
const seen = new Set();
|
|
205
|
+
for (const action of ['action1', 'action2']) {
|
|
206
|
+
for (const ts of ['20010101', '20020202']) {
|
|
207
|
+
for (const key of ['key1', 'key2']) {
|
|
208
|
+
const dedupHash = computeDeduplicationKey({
|
|
209
|
+
body: {
|
|
210
|
+
action,
|
|
211
|
+
payload: {
|
|
212
|
+
key,
|
|
213
|
+
},
|
|
214
|
+
ts,
|
|
215
|
+
},
|
|
216
|
+
});
|
|
217
|
+
expect(seen.has(dedupHash)).to.be.false;
|
|
218
|
+
seen.add(dedupHash);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('should extract the full payload by default', function () {
|
|
225
|
+
for (const deduplicateBy of ['', undefined, null]) {
|
|
226
|
+
expect(
|
|
227
|
+
computeDeduplicationKey({
|
|
228
|
+
body: {
|
|
229
|
+
action: 'action',
|
|
230
|
+
payload: {
|
|
231
|
+
x: 'x42x',
|
|
232
|
+
y: 'y42y',
|
|
233
|
+
},
|
|
234
|
+
ts: '20220923',
|
|
235
|
+
},
|
|
236
|
+
deduplicateBy,
|
|
237
|
+
}),
|
|
238
|
+
)
|
|
239
|
+
.to.be.a('string')
|
|
240
|
+
.that.includes('x42x')
|
|
241
|
+
.and.includes('y42y');
|
|
242
|
+
}
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
it('should detect consider only key fields', function () {
|
|
246
|
+
duplicatedPayload(
|
|
247
|
+
{
|
|
248
|
+
x: 'x',
|
|
249
|
+
y: 'y',
|
|
250
|
+
foo: 42,
|
|
251
|
+
},
|
|
252
|
+
{
|
|
253
|
+
x: 'x',
|
|
254
|
+
y: 'y',
|
|
255
|
+
bar: 42,
|
|
256
|
+
},
|
|
257
|
+
'x,y',
|
|
258
|
+
);
|
|
259
|
+
|
|
260
|
+
for (const by of ['x,y,foo', 'x,y,bar', '']) {
|
|
261
|
+
nonDuplicatedPayload(
|
|
262
|
+
{
|
|
263
|
+
x: 'x',
|
|
264
|
+
y: 'y',
|
|
265
|
+
foo: 42,
|
|
266
|
+
},
|
|
267
|
+
{
|
|
268
|
+
x: 'x',
|
|
269
|
+
y: 'y',
|
|
270
|
+
bar: 42,
|
|
271
|
+
},
|
|
272
|
+
by,
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
it('should supported nested keys', function () {
|
|
278
|
+
duplicatedPayload(
|
|
279
|
+
{
|
|
280
|
+
x: 'x',
|
|
281
|
+
y: {
|
|
282
|
+
z: 'z',
|
|
283
|
+
},
|
|
284
|
+
},
|
|
285
|
+
{
|
|
286
|
+
x: 'x',
|
|
287
|
+
y: {
|
|
288
|
+
z: 'z',
|
|
289
|
+
},
|
|
290
|
+
ignore: 'not part of key',
|
|
291
|
+
},
|
|
292
|
+
'x,y',
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
duplicatedPayload(
|
|
296
|
+
{
|
|
297
|
+
x: 'x',
|
|
298
|
+
y: {
|
|
299
|
+
z: 'z',
|
|
300
|
+
},
|
|
301
|
+
},
|
|
302
|
+
{
|
|
303
|
+
x: 'x',
|
|
304
|
+
y: {
|
|
305
|
+
z: 'z',
|
|
306
|
+
},
|
|
307
|
+
ignore: 'not part of key',
|
|
308
|
+
},
|
|
309
|
+
'x,y.z',
|
|
310
|
+
);
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
it('should match whole objects if the path leads to an object or array', function () {
|
|
314
|
+
nonDuplicatedPayload(
|
|
315
|
+
{
|
|
316
|
+
x: 'x',
|
|
317
|
+
y: {
|
|
318
|
+
z: 'z',
|
|
319
|
+
},
|
|
320
|
+
},
|
|
321
|
+
{
|
|
322
|
+
x: 'x',
|
|
323
|
+
y: {
|
|
324
|
+
z: 'z',
|
|
325
|
+
inner: 'part of key',
|
|
326
|
+
},
|
|
327
|
+
ignore: 'not part of key',
|
|
328
|
+
},
|
|
329
|
+
'x,y',
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
duplicatedPayload(
|
|
333
|
+
{
|
|
334
|
+
x: 'x',
|
|
335
|
+
y: {
|
|
336
|
+
z: {
|
|
337
|
+
bar: { baz: 42 },
|
|
338
|
+
foo: [1, 2, 3],
|
|
339
|
+
},
|
|
340
|
+
},
|
|
341
|
+
},
|
|
342
|
+
{
|
|
343
|
+
x: 'x',
|
|
344
|
+
y: {
|
|
345
|
+
z: {
|
|
346
|
+
bar: { baz: 42 },
|
|
347
|
+
foo: [1, 2, 3],
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
ignore: 'not part of key',
|
|
351
|
+
},
|
|
352
|
+
'x,y',
|
|
353
|
+
);
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
it('should handle arrays', function () {
|
|
357
|
+
nonDuplicatedPayload(
|
|
358
|
+
{
|
|
359
|
+
x: [1.23],
|
|
360
|
+
},
|
|
361
|
+
{
|
|
362
|
+
x: [0],
|
|
363
|
+
},
|
|
364
|
+
'x',
|
|
365
|
+
);
|
|
366
|
+
|
|
367
|
+
duplicatedPayload(
|
|
368
|
+
{
|
|
369
|
+
x: [1.23],
|
|
370
|
+
y: {
|
|
371
|
+
z: [false, true],
|
|
372
|
+
},
|
|
373
|
+
},
|
|
374
|
+
{
|
|
375
|
+
x: [1.23],
|
|
376
|
+
y: {
|
|
377
|
+
z: [false, true],
|
|
378
|
+
},
|
|
379
|
+
},
|
|
380
|
+
'y.z',
|
|
381
|
+
);
|
|
382
|
+
|
|
383
|
+
duplicatedPayload(
|
|
384
|
+
{
|
|
385
|
+
x: [1.23],
|
|
386
|
+
y: {
|
|
387
|
+
z: [false, true],
|
|
388
|
+
ignore: 'some text',
|
|
389
|
+
},
|
|
390
|
+
},
|
|
391
|
+
{
|
|
392
|
+
x: [1.23],
|
|
393
|
+
y: {
|
|
394
|
+
z: [false, true],
|
|
395
|
+
ignore: 'another text',
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
'x,y.z',
|
|
399
|
+
);
|
|
400
|
+
|
|
401
|
+
nonDuplicatedPayload(
|
|
402
|
+
{
|
|
403
|
+
y: {
|
|
404
|
+
z: [false],
|
|
405
|
+
},
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
y: {
|
|
409
|
+
z: [true],
|
|
410
|
+
},
|
|
411
|
+
},
|
|
412
|
+
'y.z',
|
|
413
|
+
);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
it('should ignore the order of fields (non-nested)', function () {
|
|
417
|
+
duplicatedPayload(
|
|
418
|
+
{
|
|
419
|
+
x: 'x',
|
|
420
|
+
y: 'y',
|
|
421
|
+
},
|
|
422
|
+
{
|
|
423
|
+
y: 'y',
|
|
424
|
+
x: 'x',
|
|
425
|
+
},
|
|
426
|
+
);
|
|
427
|
+
duplicatedPayload(
|
|
428
|
+
{
|
|
429
|
+
x: 'x',
|
|
430
|
+
y: 'y',
|
|
431
|
+
},
|
|
432
|
+
{
|
|
433
|
+
y: 'y',
|
|
434
|
+
x: 'x',
|
|
435
|
+
},
|
|
436
|
+
'x,y',
|
|
437
|
+
);
|
|
438
|
+
duplicatedPayload(
|
|
439
|
+
{
|
|
440
|
+
x: 'x',
|
|
441
|
+
y: 'y',
|
|
442
|
+
},
|
|
443
|
+
{
|
|
444
|
+
y: 'y',
|
|
445
|
+
x: 'x',
|
|
446
|
+
},
|
|
447
|
+
'y,x',
|
|
448
|
+
);
|
|
449
|
+
});
|
|
450
|
+
|
|
451
|
+
it('should ignore the order of fields (nested)', function () {
|
|
452
|
+
duplicatedPayload(
|
|
453
|
+
{
|
|
454
|
+
x: 'x',
|
|
455
|
+
y: 'y',
|
|
456
|
+
z: {
|
|
457
|
+
arr: [
|
|
458
|
+
{
|
|
459
|
+
x: 42.1,
|
|
460
|
+
y: [1, 2, 3],
|
|
461
|
+
z: undefined,
|
|
462
|
+
},
|
|
463
|
+
{
|
|
464
|
+
x: [1, 2, 3],
|
|
465
|
+
y: 42,
|
|
466
|
+
z: null,
|
|
467
|
+
},
|
|
468
|
+
],
|
|
469
|
+
foo: 42,
|
|
470
|
+
bar: 'baz',
|
|
471
|
+
yes: true,
|
|
472
|
+
no: false,
|
|
473
|
+
},
|
|
474
|
+
},
|
|
475
|
+
{
|
|
476
|
+
y: 'y',
|
|
477
|
+
z: {
|
|
478
|
+
bar: 'baz',
|
|
479
|
+
no: false,
|
|
480
|
+
yes: true,
|
|
481
|
+
foo: 42,
|
|
482
|
+
arr: [
|
|
483
|
+
{
|
|
484
|
+
z: undefined,
|
|
485
|
+
y: [1, 2, 3],
|
|
486
|
+
x: 42.1,
|
|
487
|
+
},
|
|
488
|
+
{
|
|
489
|
+
y: [1, 2, 3],
|
|
490
|
+
z: null,
|
|
491
|
+
x: 42,
|
|
492
|
+
},
|
|
493
|
+
],
|
|
494
|
+
},
|
|
495
|
+
x: 'x',
|
|
496
|
+
},
|
|
497
|
+
'y,x',
|
|
498
|
+
);
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
it('should expand arrays while matching', function () {
|
|
502
|
+
duplicatedPayload(
|
|
503
|
+
{
|
|
504
|
+
foo: [[{ x: 1, y: 2, z: 3 }]],
|
|
505
|
+
},
|
|
506
|
+
{
|
|
507
|
+
foo: [[{ x: 1, y: 2, z: 3 }]],
|
|
508
|
+
},
|
|
509
|
+
'foo.[].x,foo.[].y,foo.[].z',
|
|
510
|
+
);
|
|
511
|
+
|
|
512
|
+
duplicatedPayload(
|
|
513
|
+
{
|
|
514
|
+
foo: [[{ x: 1, y: 2 }]],
|
|
515
|
+
},
|
|
516
|
+
{
|
|
517
|
+
foo: [[{ x: 1, y: 2, z: 3 }]],
|
|
518
|
+
},
|
|
519
|
+
'foo.[].x,foo.[].y',
|
|
520
|
+
);
|
|
521
|
+
|
|
522
|
+
nonDuplicatedPayload(
|
|
523
|
+
{
|
|
524
|
+
foo: [[{ x: 1, y: 2, z: 3 }]],
|
|
525
|
+
},
|
|
526
|
+
{
|
|
527
|
+
foo: [[{ x: 1, y: 2, z: 3 }], [{ x: 1, y: 2, z: 3 }]],
|
|
528
|
+
},
|
|
529
|
+
'foo.[].[].x',
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
duplicatedPayload(
|
|
533
|
+
{
|
|
534
|
+
foo: [[{ x: 1, y: 1 }]],
|
|
535
|
+
},
|
|
536
|
+
{
|
|
537
|
+
foo: [[{ x: 1, y: 2 }]],
|
|
538
|
+
},
|
|
539
|
+
'foo.[].[].x',
|
|
540
|
+
);
|
|
541
|
+
|
|
542
|
+
nonDuplicatedPayload(
|
|
543
|
+
{
|
|
544
|
+
foo: [[{ x: 1, y: 1 }]],
|
|
545
|
+
},
|
|
546
|
+
{
|
|
547
|
+
foo: [[{ x: 1, y: 2 }]],
|
|
548
|
+
},
|
|
549
|
+
'foo.[].[].x,foo.[].[].y',
|
|
550
|
+
);
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
it('should only consider real matches when expanding arrays', function () {
|
|
554
|
+
duplicatedPayload(
|
|
555
|
+
{
|
|
556
|
+
x: [[{ y: [] }]],
|
|
557
|
+
},
|
|
558
|
+
{
|
|
559
|
+
foo: [],
|
|
560
|
+
},
|
|
561
|
+
'x.[].y.[].z',
|
|
562
|
+
);
|
|
563
|
+
});
|
|
564
|
+
|
|
565
|
+
it('should support matching array indices', function () {
|
|
566
|
+
duplicatedPayload(
|
|
567
|
+
{
|
|
568
|
+
x: [{ y: 'part of key' }, { y: 'some text not part of index' }],
|
|
569
|
+
},
|
|
570
|
+
{
|
|
571
|
+
x: [{ y: 'part of key' }, { y: 'another text not part of index' }],
|
|
572
|
+
},
|
|
573
|
+
'x.0.y',
|
|
574
|
+
);
|
|
575
|
+
|
|
576
|
+
nonDuplicatedPayload(
|
|
577
|
+
{
|
|
578
|
+
x: [{ y: 'part of key' }, { y: 'some text part of index' }],
|
|
579
|
+
},
|
|
580
|
+
{
|
|
581
|
+
x: [{ y: 'part of key' }, { y: 'another text part of index' }],
|
|
582
|
+
},
|
|
583
|
+
'x.[].y',
|
|
584
|
+
);
|
|
585
|
+
|
|
586
|
+
duplicatedPayload(
|
|
587
|
+
{
|
|
588
|
+
x: [[{ y: 'part of key' }, { y: 'some text not part of index' }]],
|
|
589
|
+
},
|
|
590
|
+
{
|
|
591
|
+
x: [[{ y: 'part of key' }, { y: 'another text not part of index' }]],
|
|
592
|
+
},
|
|
593
|
+
'x.0.0.y',
|
|
594
|
+
);
|
|
595
|
+
});
|
|
596
|
+
|
|
597
|
+
// (Mostly for documentation; we are not relying on the following semantic)
|
|
598
|
+
//
|
|
599
|
+
// JavaScripts's null and undefined will be both treated as missing values.
|
|
600
|
+
// In our context, that is reasonable and avoiding it is hard anyway because
|
|
601
|
+
// details of JSON (which is used in the communication layer) will shine through:
|
|
602
|
+
// JSON.stringify([undefined]) === JSON.stringify([null])
|
|
603
|
+
//
|
|
604
|
+
// If changes of the implementation break this tests, feel free to change it.
|
|
605
|
+
// It documents the status-quo of the implemenation, but everything here
|
|
606
|
+
it('should treat null and undefined both as missing value', function () {
|
|
607
|
+
duplicatedPayload(
|
|
608
|
+
{
|
|
609
|
+
x: null,
|
|
610
|
+
y: 'foo',
|
|
611
|
+
},
|
|
612
|
+
{
|
|
613
|
+
x: undefined,
|
|
614
|
+
y: 'foo',
|
|
615
|
+
},
|
|
616
|
+
'x,y',
|
|
617
|
+
);
|
|
618
|
+
duplicatedPayload(
|
|
619
|
+
{
|
|
620
|
+
x: null,
|
|
621
|
+
y: 'foo',
|
|
622
|
+
},
|
|
623
|
+
{
|
|
624
|
+
y: 'foo',
|
|
625
|
+
},
|
|
626
|
+
'x,y',
|
|
627
|
+
);
|
|
628
|
+
duplicatedPayload(
|
|
629
|
+
{
|
|
630
|
+
x: undefined,
|
|
631
|
+
y: 'foo',
|
|
632
|
+
},
|
|
633
|
+
{
|
|
634
|
+
y: 'foo',
|
|
635
|
+
},
|
|
636
|
+
'x,y',
|
|
637
|
+
);
|
|
638
|
+
duplicatedPayload(
|
|
639
|
+
{
|
|
640
|
+
y: 'foo',
|
|
641
|
+
},
|
|
642
|
+
{
|
|
643
|
+
y: 'foo',
|
|
644
|
+
},
|
|
645
|
+
'x,y',
|
|
646
|
+
);
|
|
647
|
+
|
|
648
|
+
// When matching without explicit keys, there are three case:
|
|
649
|
+
// null, undefined and omitted. For technical reasons, we cannot
|
|
650
|
+
// easily merge omitted with the other two. It should not be important,
|
|
651
|
+
// so we can accept the following behavior:
|
|
652
|
+
duplicatedPayload(
|
|
653
|
+
{
|
|
654
|
+
x: null,
|
|
655
|
+
y: 'foo',
|
|
656
|
+
},
|
|
657
|
+
{
|
|
658
|
+
x: undefined,
|
|
659
|
+
y: 'foo',
|
|
660
|
+
},
|
|
661
|
+
);
|
|
662
|
+
nonDuplicatedPayload(
|
|
663
|
+
{
|
|
664
|
+
x: null,
|
|
665
|
+
y: 'foo',
|
|
666
|
+
},
|
|
667
|
+
{
|
|
668
|
+
y: 'foo',
|
|
669
|
+
},
|
|
670
|
+
);
|
|
671
|
+
nonDuplicatedPayload(
|
|
672
|
+
{
|
|
673
|
+
x: undefined,
|
|
674
|
+
y: 'foo',
|
|
675
|
+
},
|
|
676
|
+
{
|
|
677
|
+
y: 'foo',
|
|
678
|
+
},
|
|
679
|
+
);
|
|
680
|
+
duplicatedPayload(
|
|
681
|
+
{
|
|
682
|
+
y: 'foo',
|
|
683
|
+
},
|
|
684
|
+
{
|
|
685
|
+
y: 'foo',
|
|
686
|
+
},
|
|
687
|
+
);
|
|
688
|
+
});
|
|
689
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* WhoTracks.Me
|
|
3
|
+
* https://whotracks.me/
|
|
4
|
+
*
|
|
5
|
+
* Copyright 2017-present Ghostery GmbH. All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
8
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
9
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import * as linkedomCached from 'linkedom/cached';
|
|
13
|
+
import * as linkedomUncached from 'linkedom';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Different implementations of the DOM API.
|
|
17
|
+
*
|
|
18
|
+
* In most cases, all should be close to the browser API, but in edge
|
|
19
|
+
* cases like broken HTML, the results may differ. If possible, we
|
|
20
|
+
* should write code in such a way that works well in all environments.
|
|
21
|
+
*/
|
|
22
|
+
export const mockDocumentWith = {
|
|
23
|
+
// https://github.com/WebReflection/linkedom
|
|
24
|
+
linkedomCached(html) {
|
|
25
|
+
const { window, document } = linkedomCached.parseHTML(html);
|
|
26
|
+
const noop = () => {};
|
|
27
|
+
window.close = window.close || noop;
|
|
28
|
+
return { window, document };
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
// Like linkedom, but with caching disabled (not recommended for our use cases)
|
|
32
|
+
linkedomUncached(html) {
|
|
33
|
+
const { window, document } = linkedomUncached.parseHTML(html);
|
|
34
|
+
const noop = () => {};
|
|
35
|
+
window.close = window.close || noop;
|
|
36
|
+
return { window, document };
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export const allAvailableParsers = Object.keys(mockDocumentWith);
|
|
41
|
+
export const allSupportedParsers = allAvailableParsers;
|