@whotracksme/reporting 11.1.1 → 12.1.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.
- package/CHANGELOG.md +39 -0
- package/package.json +1 -1
- package/reporting/README.md +13 -0
- package/reporting/example/README.md +4 -0
- package/reporting/example/e2e/README.md +265 -0
- package/reporting/example/e2e/console-capture.js +67 -0
- package/reporting/example/e2e/control.js +449 -0
- package/reporting/example/e2e/hub.mjs +380 -0
- package/reporting/example/e2e/launch.mjs +92 -0
- package/reporting/example/e2e/mock-extension.mjs +106 -0
- package/reporting/example/e2e/pages/private-fixture.html +16 -0
- package/reporting/example/e2e/run.mjs +226 -0
- package/reporting/example/e2e/scenarios/privacy-guards.json +26 -0
- package/reporting/example/e2e/scenarios/smoke.json +15 -0
- package/reporting/example/e2e/verify.mjs +159 -0
- package/reporting/example/index.js +55 -46
- package/reporting/example/manifests/chromium.json +1 -0
- package/reporting/example/storage.js +65 -0
- package/reporting/package.json +5 -1
- package/reporting/rollup.config.cjs +1 -1
- package/reporting/snapshots/0001/snapshot.json +13 -13
- package/reporting/snapshots/0003/snapshot.json +5 -5
- package/reporting/snapshots/0005/snapshot.json +13 -13
- package/reporting/src/doublefetch-page-handler.js +30 -20
- package/reporting/src/doublefetch-unbreak.js +186 -0
- package/reporting/src/http.js +197 -19
- package/reporting/src/index.js +1 -0
- package/reporting/src/offscreen/doublefetch/capture.js +90 -0
- package/reporting/src/patterns.js +142 -2
- package/reporting/src/request/index.js +6 -0
- package/reporting/src/request/page-store.js +1 -0
- package/reporting/src/request/support.js +29 -0
- package/reporting/src/search-extractor.js +8 -2
- package/reporting/src/utils.js +19 -0
- package/reporting/test/doublefetch-unbreak.spec.js +133 -0
- package/reporting/test/http.spec.js +113 -0
- package/reporting/test/index.js +1 -0
- package/reporting/test/patterns.spec.js +239 -0
- package/reporting/test/search-extractor.spec.js +19 -1
- package/reporting/test/setup.unit.js +9 -0
|
@@ -17,6 +17,7 @@ import { fastHash } from './utils';
|
|
|
17
17
|
import random from './random';
|
|
18
18
|
import { timezoneAgnosticDailyExpireAt } from './cooldowns';
|
|
19
19
|
import { anonymousHttpGet } from './http';
|
|
20
|
+
import { findSafeCookies } from './doublefetch-unbreak';
|
|
20
21
|
import { lookupBuiltinTransform } from './patterns';
|
|
21
22
|
import { BadPatternError } from './errors';
|
|
22
23
|
|
|
@@ -53,7 +54,8 @@ function runSelector(item, selector, attr, baseURI) {
|
|
|
53
54
|
return null;
|
|
54
55
|
}
|
|
55
56
|
|
|
56
|
-
|
|
57
|
+
// Note: exported for tests only
|
|
58
|
+
export function runTransforms(value, transformSteps = []) {
|
|
57
59
|
if (!Array.isArray(transformSteps)) {
|
|
58
60
|
throw new BadPatternError(
|
|
59
61
|
'Transform definitions must be an array (of arrays).',
|
|
@@ -72,8 +74,11 @@ function runTransforms(value, transformSteps = []) {
|
|
|
72
74
|
const [name, ...args] = step;
|
|
73
75
|
const transform = lookupBuiltinTransform(name);
|
|
74
76
|
tmpValue = transform(tmpValue, ...args);
|
|
77
|
+
if (tmpValue === undefined || tmpValue === null) {
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
75
80
|
}
|
|
76
|
-
return tmpValue
|
|
81
|
+
return tmpValue;
|
|
77
82
|
}
|
|
78
83
|
|
|
79
84
|
function findFirstMatch(rootItem, selectorDef, baseURI) {
|
|
@@ -138,6 +143,7 @@ export default class SearchExtractor {
|
|
|
138
143
|
const { url, ...params } = doublefetchRequest;
|
|
139
144
|
const html = await anonymousHttpGet(url, {
|
|
140
145
|
...params,
|
|
146
|
+
safecookie: await findSafeCookies(url, params),
|
|
141
147
|
treat429AsPermanentError: true,
|
|
142
148
|
});
|
|
143
149
|
doc = await parseHtml(html);
|
package/reporting/src/utils.js
CHANGED
|
@@ -345,6 +345,25 @@ export function includesMiddleChar(str, char) {
|
|
|
345
345
|
return false;
|
|
346
346
|
}
|
|
347
347
|
|
|
348
|
+
/**
|
|
349
|
+
* Checks if one string (subsequence) can be derived from another string (sequence)
|
|
350
|
+
* by only removing characters. This means that the subsequence appears in the
|
|
351
|
+
* sequence in the same relative order but not necessarily consecutively.
|
|
352
|
+
*
|
|
353
|
+
* Example: "anana" is a subsequence of "banana", but "ab" is not.
|
|
354
|
+
*/
|
|
355
|
+
export function isSubSequence({ sequence, subsequence }) {
|
|
356
|
+
let i = 0;
|
|
357
|
+
let j = 0;
|
|
358
|
+
while (i < subsequence.length && j < sequence.length) {
|
|
359
|
+
if (subsequence[i] === sequence[j]) {
|
|
360
|
+
i++;
|
|
361
|
+
}
|
|
362
|
+
j++;
|
|
363
|
+
}
|
|
364
|
+
return i === subsequence.length;
|
|
365
|
+
}
|
|
366
|
+
|
|
348
367
|
/**
|
|
349
368
|
* Drop-in replacement for JSON.parse if you need to handle untrusted data.
|
|
350
369
|
*
|
|
@@ -0,0 +1,133 @@
|
|
|
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 { findSafeCookies } from '../src/doublefetch-unbreak.js';
|
|
15
|
+
|
|
16
|
+
const URL_ = 'https://www.example.test/search?q=foo';
|
|
17
|
+
|
|
18
|
+
describe('#findSafeCookies', function () {
|
|
19
|
+
let lookups;
|
|
20
|
+
let jar;
|
|
21
|
+
let originalCookies;
|
|
22
|
+
|
|
23
|
+
beforeEach(function () {
|
|
24
|
+
lookups = [];
|
|
25
|
+
jar = {};
|
|
26
|
+
originalCookies = chrome.cookies;
|
|
27
|
+
chrome.cookies = {
|
|
28
|
+
getAll: async (details) => {
|
|
29
|
+
lookups.push(details);
|
|
30
|
+
const value = jar[details.name];
|
|
31
|
+
return value === undefined ? [] : [{ name: details.name, value }];
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
afterEach(function () {
|
|
37
|
+
chrome.cookies = originalCookies;
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const collect = (template) =>
|
|
41
|
+
findSafeCookies(URL_, {
|
|
42
|
+
steps: [{ headers: { Cookie: template } }],
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it('should not touch the cookie jar unless asked to', async function () {
|
|
46
|
+
const found = await collect('SOCS=fixed;X={{cookie:foo}}');
|
|
47
|
+
expect(found.size).to.eql(0);
|
|
48
|
+
expect(lookups).to.eql([]);
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it('should look up only what was asked for', async function () {
|
|
52
|
+
jar = { 'aws-waf-token': 'AWS', cf_clearance: 'CF' };
|
|
53
|
+
const found = await collect('t={{safecookie:aws-waf-token}}');
|
|
54
|
+
expect(lookups.map((x) => x.name)).to.eql(['aws-waf-token']);
|
|
55
|
+
expect(found.get('aws-waf-token')).to.eql('AWS');
|
|
56
|
+
expect(found.has('cf_clearance')).to.eql(false);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('should refuse cookies that are not on the unbreaking list', async function () {
|
|
60
|
+
jar = { SID: 'secret', sessionid: 'secret' };
|
|
61
|
+
const found = await collect(
|
|
62
|
+
'a={{safecookie:SID}};b={{safecookie:sessionid}}',
|
|
63
|
+
);
|
|
64
|
+
expect(lookups).to.eql([]);
|
|
65
|
+
expect(found.size).to.eql(0);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('should scope the lookup to the URL and to its own cookie store', async function () {
|
|
69
|
+
jar = { 'aws-waf-token': 'AWS' };
|
|
70
|
+
await collect('t={{safecookie:aws-waf-token}}');
|
|
71
|
+
expect(lookups).to.have.length(1);
|
|
72
|
+
expect(lookups[0].url).to.eql(URL_);
|
|
73
|
+
// no "storeId" means the extension's own store: not incognito, no container
|
|
74
|
+
expect(lookups[0]).to.not.have.property('storeId');
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('should skip a cookie that is not set, or that cannot be read', async function () {
|
|
78
|
+
const ask = () => collect('t={{safecookie:aws-waf-token}}');
|
|
79
|
+
expect((await ask()).size).to.eql(0);
|
|
80
|
+
|
|
81
|
+
chrome.cookies.getAll = async () => {
|
|
82
|
+
throw new Error('no permission');
|
|
83
|
+
};
|
|
84
|
+
expect((await ask()).size).to.eql(0);
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('should collect from the shared headers, every step and the rescue config', async function () {
|
|
88
|
+
jar = { 'aws-waf-token': 'AWS', cf_clearance: 'CF', __cf_bm: 'BM' };
|
|
89
|
+
const found = await findSafeCookies(URL_, {
|
|
90
|
+
headers: { Cookie: 'a={{safecookie:aws-waf-token}}' },
|
|
91
|
+
steps: [{}, { headers: { Cookie: 'b={{safecookie:cf_clearance}}' } }],
|
|
92
|
+
onError: { headers: { Cookie: 'c={{safecookie:__cf_bm}}' } },
|
|
93
|
+
});
|
|
94
|
+
expect([...found.keys()].sort()).to.eql([
|
|
95
|
+
'__cf_bm',
|
|
96
|
+
'aws-waf-token',
|
|
97
|
+
'cf_clearance',
|
|
98
|
+
]);
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should collect a cookie that a step only "requires"', async function () {
|
|
102
|
+
jar = { 'aws-waf-token': 'AWS' };
|
|
103
|
+
const found = await findSafeCookies(URL_, {
|
|
104
|
+
steps: [{ requires: ['{{safecookie:aws-waf-token}}'] }],
|
|
105
|
+
});
|
|
106
|
+
expect(found.get('aws-waf-token')).to.eql('AWS');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('should refuse untrusted cookies in "requires" entries', async function () {
|
|
110
|
+
jar = { sessionid: 'secret' };
|
|
111
|
+
const found = await findSafeCookies(URL_, {
|
|
112
|
+
steps: [{ requires: ['{{safecookie:sessionid}}'] }],
|
|
113
|
+
});
|
|
114
|
+
expect(lookups).to.eql([]);
|
|
115
|
+
expect(found.size).to.eql(0);
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should collect from "requires" inside the rescue config', async function () {
|
|
119
|
+
jar = { cf_clearance: 'CF' };
|
|
120
|
+
const found = await findSafeCookies(URL_, {
|
|
121
|
+
onError: { steps: [{ requires: ['{{safecookie:cf_clearance}}'] }] },
|
|
122
|
+
});
|
|
123
|
+
expect(found.get('cf_clearance')).to.eql('CF');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('should support alternatives ("||")', async function () {
|
|
127
|
+
jar = { cf_clearance: 'CF' };
|
|
128
|
+
const found = await collect(
|
|
129
|
+
'x={{cookie:missing||safecookie:cf_clearance}}',
|
|
130
|
+
);
|
|
131
|
+
expect(found.get('cf_clearance')).to.eql('CF');
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -117,6 +117,32 @@ describe('#replacePlaceholders', function () {
|
|
|
117
117
|
});
|
|
118
118
|
});
|
|
119
119
|
|
|
120
|
+
describe('#replacePlaceholders with borrowed cookies', function () {
|
|
121
|
+
const ctx = {
|
|
122
|
+
cookie: new Map(),
|
|
123
|
+
safecookie: new Map([['aws-waf-token', 'the-token']]),
|
|
124
|
+
};
|
|
125
|
+
const resolve = (template) => replacePlaceholders({ C: template }, ctx).C;
|
|
126
|
+
|
|
127
|
+
it('should resolve a borrowed cookie', function () {
|
|
128
|
+
expect(resolve('t={{safecookie:aws-waf-token}}')).to.eql('t=the-token');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('should be empty if the cookie was not borrowed', function () {
|
|
132
|
+
expect(resolve('{{safecookie:cf_clearance}}')).to.eql('');
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it('should take part in "||" alternatives', function () {
|
|
136
|
+
expect(resolve('{{cookie:absent||safecookie:aws-waf-token}}')).to.eql(
|
|
137
|
+
'the-token',
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('should still reject an unsupported placeholder type', function () {
|
|
142
|
+
expect(() => resolve('{{cookies:x}}')).to.throw(/Unsupported expression/);
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
120
146
|
describe('#findPlaceholders', function () {
|
|
121
147
|
it('should work with empty strings', function () {
|
|
122
148
|
expect(findPlaceholders('')).to.eql([]);
|
|
@@ -155,6 +181,93 @@ describe('#findPlaceholders', function () {
|
|
|
155
181
|
});
|
|
156
182
|
});
|
|
157
183
|
|
|
184
|
+
describe('#buildDependencyGraph with a prefilled context', function () {
|
|
185
|
+
const borrowed = { safecookie: new Map([['aws-waf-token', 'the-token']]) };
|
|
186
|
+
const nextStep = { headers: { Cookie: 't={{safecookie:aws-waf-token}}' } };
|
|
187
|
+
|
|
188
|
+
it('should not wait for a borrowed cookie', function () {
|
|
189
|
+
expect(buildDependencyGraph(nextStep, borrowed).allReady).to.eql(true);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it('should not wait for a value that an earlier step captured', function () {
|
|
193
|
+
const ctx = { cookie: new Map([['FOO', 'captured by an earlier step']]) };
|
|
194
|
+
expect(
|
|
195
|
+
buildDependencyGraph({ headers: { Cookie: '{{cookie:FOO}}' } }, ctx)
|
|
196
|
+
.allReady,
|
|
197
|
+
).to.eql(true);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should not wait for a borrowed cookie that is absent', function () {
|
|
201
|
+
// it is fixed before the first request, so it will never arrive
|
|
202
|
+
expect(
|
|
203
|
+
buildDependencyGraph(nextStep, { safecookie: new Map() }).allReady,
|
|
204
|
+
).to.eql(true);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('should still wait if another source could provide it', function () {
|
|
208
|
+
const graph = buildDependencyGraph(
|
|
209
|
+
{ headers: { Cookie: 'x={{safecookie:absent||cookie:FOO}}' } },
|
|
210
|
+
{ safecookie: new Map() },
|
|
211
|
+
);
|
|
212
|
+
expect(graph.allReady).to.eql(false);
|
|
213
|
+
|
|
214
|
+
graph.onChange('cookie', 'FOO', 'arrived with the response');
|
|
215
|
+
expect(graph.allReady).to.eql(true);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('should still wait for the parts that are missing', function () {
|
|
219
|
+
const graph = buildDependencyGraph(
|
|
220
|
+
{
|
|
221
|
+
headers: {
|
|
222
|
+
Cookie: 't={{safecookie:aws-waf-token}};S={{param:from-response}}',
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
borrowed,
|
|
226
|
+
);
|
|
227
|
+
expect(graph.allReady).to.eql(false);
|
|
228
|
+
|
|
229
|
+
let onReadyCalled = 0;
|
|
230
|
+
graph.onReady = () => {
|
|
231
|
+
onReadyCalled += 1;
|
|
232
|
+
};
|
|
233
|
+
graph.onChange('param', 'from-response', 'dummy value');
|
|
234
|
+
expect(onReadyCalled).to.eql(1);
|
|
235
|
+
expect(graph.allReady).to.eql(true);
|
|
236
|
+
});
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
describe('#buildDependencyGraph with "requires"', function () {
|
|
240
|
+
const noCookies = { safecookie: new Map() };
|
|
241
|
+
|
|
242
|
+
it('should wait for a required value that no header uses', function () {
|
|
243
|
+
const graph = buildDependencyGraph(
|
|
244
|
+
{ requires: ['{{param:from-response}}'], headers: { Cookie: 'x=1' } },
|
|
245
|
+
noCookies,
|
|
246
|
+
);
|
|
247
|
+
expect(graph.allReady).to.eql(false);
|
|
248
|
+
|
|
249
|
+
graph.onChange('param', 'from-response', 'arrived with the second request');
|
|
250
|
+
expect(graph.allReady).to.eql(true);
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
it('should wait even if the step has no headers at all', function () {
|
|
254
|
+
expect(
|
|
255
|
+
buildDependencyGraph({ requires: ['{{param:never-seen}}'] }, noCookies)
|
|
256
|
+
.allReady,
|
|
257
|
+
).to.eql(false);
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('should not wait for a required borrowed cookie that is absent', function () {
|
|
261
|
+
// it can never arrive, so the step runs and fails on its own check
|
|
262
|
+
expect(
|
|
263
|
+
buildDependencyGraph(
|
|
264
|
+
{ requires: ['{{safecookie:cf_clearance}}'] },
|
|
265
|
+
noCookies,
|
|
266
|
+
).allReady,
|
|
267
|
+
).to.eql(true);
|
|
268
|
+
});
|
|
269
|
+
});
|
|
270
|
+
|
|
158
271
|
describe('#buildDependencyGraph', function () {
|
|
159
272
|
it('should be immediately ready if there is no next step', function () {
|
|
160
273
|
expect(buildDependencyGraph(undefined).allReady).to.eql(true);
|
package/reporting/test/index.js
CHANGED
|
@@ -10,6 +10,7 @@ import './patterns-updater.spec.js';
|
|
|
10
10
|
import './job-scheduler.spec.js';
|
|
11
11
|
import './country-provider.spec.js';
|
|
12
12
|
import './persisted-hashes.spec.js';
|
|
13
|
+
import './doublefetch-unbreak.spec.js';
|
|
13
14
|
import './url-analyzer.spec.js';
|
|
14
15
|
import './alive-check.spec.js';
|
|
15
16
|
import './alive-message-generator.spec.js';
|
|
@@ -13,6 +13,7 @@ import { expect } from 'chai';
|
|
|
13
13
|
import fc from 'fast-check';
|
|
14
14
|
|
|
15
15
|
import Patterns, { lookupBuiltinTransform } from '../src/patterns.js';
|
|
16
|
+
import { isSubSequence } from '../src/utils.js';
|
|
16
17
|
|
|
17
18
|
describe('Test builtin primitives', function () {
|
|
18
19
|
describe('#queryParam', function () {
|
|
@@ -915,6 +916,234 @@ describe('Test builtin primitives', function () {
|
|
|
915
916
|
});
|
|
916
917
|
});
|
|
917
918
|
});
|
|
919
|
+
|
|
920
|
+
describe('#removeFirstMatch', function () {
|
|
921
|
+
let uut;
|
|
922
|
+
|
|
923
|
+
beforeEach(function () {
|
|
924
|
+
uut = lookupBuiltinTransform('removeFirstMatch');
|
|
925
|
+
});
|
|
926
|
+
|
|
927
|
+
describe('core functionality', function () {
|
|
928
|
+
it('["xABBBy ABz", "AB+"] -> "xy ABz"', function () {
|
|
929
|
+
expect(uut('xABBBy ABz', 'AB+')).to.eql('xy ABz');
|
|
930
|
+
});
|
|
931
|
+
|
|
932
|
+
it('should return the text unchanged if there is no match', function () {
|
|
933
|
+
expect(uut('some text', 'XYZ')).to.eql('some text');
|
|
934
|
+
});
|
|
935
|
+
});
|
|
936
|
+
|
|
937
|
+
describe('robustness on untrusted data', function () {
|
|
938
|
+
it('should fail if input is not [string, string] (more parameters are ignored)', function () {
|
|
939
|
+
expect(() => uut()).to.throw();
|
|
940
|
+
expect(() => uut('too few args')).to.throw();
|
|
941
|
+
expect(() => uut({ wrong: 'types' }, 42)).to.throw();
|
|
942
|
+
});
|
|
943
|
+
|
|
944
|
+
it('should not fail on arbitrary text and patterns', function () {
|
|
945
|
+
fc.assert(
|
|
946
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
947
|
+
expect(uut(text, pattern)).to.be.a('string');
|
|
948
|
+
}),
|
|
949
|
+
);
|
|
950
|
+
});
|
|
951
|
+
|
|
952
|
+
it('should only remove characters from the text (the result never grows)', function () {
|
|
953
|
+
fc.assert(
|
|
954
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
955
|
+
const subsequence = uut(text, pattern);
|
|
956
|
+
expect(isSubSequence({ sequence: text, subsequence })).to.be.true;
|
|
957
|
+
}),
|
|
958
|
+
);
|
|
959
|
+
});
|
|
960
|
+
});
|
|
961
|
+
});
|
|
962
|
+
|
|
963
|
+
describe('#removeAllMatches', function () {
|
|
964
|
+
let uut;
|
|
965
|
+
|
|
966
|
+
beforeEach(function () {
|
|
967
|
+
uut = lookupBuiltinTransform('removeAllMatches');
|
|
968
|
+
});
|
|
969
|
+
|
|
970
|
+
describe('core functionality', function () {
|
|
971
|
+
it('["xABBBy ABz", "AB+"] -> "xy z"', function () {
|
|
972
|
+
expect(uut('xABBBy ABz', 'AB+')).to.eql('xy z');
|
|
973
|
+
});
|
|
974
|
+
|
|
975
|
+
it('should return the text unchanged if there is no match', function () {
|
|
976
|
+
expect(uut('some text', 'XYZ')).to.eql('some text');
|
|
977
|
+
});
|
|
978
|
+
});
|
|
979
|
+
|
|
980
|
+
describe('robustness on untrusted data', function () {
|
|
981
|
+
it('should fail if input is not [string, string] (more parameters are ignored)', function () {
|
|
982
|
+
expect(() => uut()).to.throw();
|
|
983
|
+
expect(() => uut('too few args')).to.throw();
|
|
984
|
+
expect(() => uut({ wrong: 'types' }, 42)).to.throw();
|
|
985
|
+
});
|
|
986
|
+
|
|
987
|
+
it('should not fail on arbitrary text and patterns', function () {
|
|
988
|
+
fc.assert(
|
|
989
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
990
|
+
expect(uut(text, pattern)).to.be.a('string');
|
|
991
|
+
}),
|
|
992
|
+
);
|
|
993
|
+
});
|
|
994
|
+
|
|
995
|
+
it('should only remove characters from the text (the result never grows)', function () {
|
|
996
|
+
fc.assert(
|
|
997
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
998
|
+
const subsequence = uut(text, pattern);
|
|
999
|
+
expect(isSubSequence({ sequence: text, subsequence })).to.be.true;
|
|
1000
|
+
}),
|
|
1001
|
+
);
|
|
1002
|
+
});
|
|
1003
|
+
});
|
|
1004
|
+
});
|
|
1005
|
+
|
|
1006
|
+
describe('#selectFirstMatch', function () {
|
|
1007
|
+
let uut;
|
|
1008
|
+
|
|
1009
|
+
beforeEach(function () {
|
|
1010
|
+
uut = lookupBuiltinTransform('selectFirstMatch');
|
|
1011
|
+
});
|
|
1012
|
+
|
|
1013
|
+
describe('core functionality', function () {
|
|
1014
|
+
it('["xABBBy ABz", "AB+"] -> "ABBB"', function () {
|
|
1015
|
+
expect(uut('xABBBy ABz', 'AB+')).to.eql('ABBB');
|
|
1016
|
+
});
|
|
1017
|
+
|
|
1018
|
+
it('should return an empty string if there is no match', function () {
|
|
1019
|
+
expect(uut('some text', 'XYZ')).to.eql('');
|
|
1020
|
+
});
|
|
1021
|
+
});
|
|
1022
|
+
|
|
1023
|
+
describe('robustness on untrusted data', function () {
|
|
1024
|
+
it('should fail if input is not [string, string] (more parameters are ignored)', function () {
|
|
1025
|
+
expect(() => uut()).to.throw();
|
|
1026
|
+
expect(() => uut('too few args')).to.throw();
|
|
1027
|
+
expect(() => uut({ wrong: 'types' }, 42)).to.throw();
|
|
1028
|
+
});
|
|
1029
|
+
|
|
1030
|
+
it('should not fail on arbitrary text and patterns', function () {
|
|
1031
|
+
fc.assert(
|
|
1032
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
1033
|
+
expect(uut(text, pattern)).to.be.a('string');
|
|
1034
|
+
}),
|
|
1035
|
+
);
|
|
1036
|
+
});
|
|
1037
|
+
|
|
1038
|
+
it('should only remove characters from the text (the result never grows)', function () {
|
|
1039
|
+
fc.assert(
|
|
1040
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
1041
|
+
const subsequence = uut(text, pattern);
|
|
1042
|
+
expect(isSubSequence({ sequence: text, subsequence })).to.be.true;
|
|
1043
|
+
}),
|
|
1044
|
+
);
|
|
1045
|
+
});
|
|
1046
|
+
});
|
|
1047
|
+
});
|
|
1048
|
+
|
|
1049
|
+
describe('#selectAllMatches', function () {
|
|
1050
|
+
let uut;
|
|
1051
|
+
|
|
1052
|
+
beforeEach(function () {
|
|
1053
|
+
uut = lookupBuiltinTransform('selectAllMatches');
|
|
1054
|
+
});
|
|
1055
|
+
|
|
1056
|
+
describe('core functionality', function () {
|
|
1057
|
+
it('["xABBBy ABz", "AB+"] -> "ABBBAB"', function () {
|
|
1058
|
+
expect(uut('xABBBy ABz', 'AB+')).to.eql('ABBBAB');
|
|
1059
|
+
});
|
|
1060
|
+
|
|
1061
|
+
it('should return an empty string if there is no match', function () {
|
|
1062
|
+
expect(uut('some text', 'XYZ')).to.eql('');
|
|
1063
|
+
});
|
|
1064
|
+
|
|
1065
|
+
it('should support patterns that can match the empty string', function () {
|
|
1066
|
+
expect(uut('baaacaa', 'a*')).to.eql('aaaaa');
|
|
1067
|
+
});
|
|
1068
|
+
|
|
1069
|
+
it('should behave like a naive match(...).join("") implementation', function () {
|
|
1070
|
+
fc.assert(
|
|
1071
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
1072
|
+
let expected = '';
|
|
1073
|
+
try {
|
|
1074
|
+
expected = (text.match(new RegExp(pattern, 'g')) || []).join('');
|
|
1075
|
+
} catch (e) {
|
|
1076
|
+
// invalid regular expression: expect the value to be cleared
|
|
1077
|
+
}
|
|
1078
|
+
expect(uut(text, pattern)).to.eql(expected);
|
|
1079
|
+
}),
|
|
1080
|
+
);
|
|
1081
|
+
});
|
|
1082
|
+
});
|
|
1083
|
+
|
|
1084
|
+
describe('robustness on untrusted data', function () {
|
|
1085
|
+
it('should fail if input is not [string, string] (more parameters are ignored)', function () {
|
|
1086
|
+
expect(() => uut()).to.throw();
|
|
1087
|
+
expect(() => uut('too few args')).to.throw();
|
|
1088
|
+
expect(() => uut({ wrong: 'types' }, 42)).to.throw();
|
|
1089
|
+
});
|
|
1090
|
+
|
|
1091
|
+
it('should not fail on arbitrary text and patterns', function () {
|
|
1092
|
+
fc.assert(
|
|
1093
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
1094
|
+
expect(uut(text, pattern)).to.be.a('string');
|
|
1095
|
+
}),
|
|
1096
|
+
);
|
|
1097
|
+
});
|
|
1098
|
+
|
|
1099
|
+
it('should only remove characters from the text (the result never grows)', function () {
|
|
1100
|
+
fc.assert(
|
|
1101
|
+
fc.property(fc.fullUnicodeString(), fc.string(), (text, pattern) => {
|
|
1102
|
+
const subsequence = uut(text, pattern);
|
|
1103
|
+
expect(isSubSequence({ sequence: text, subsequence })).to.be.true;
|
|
1104
|
+
}),
|
|
1105
|
+
);
|
|
1106
|
+
});
|
|
1107
|
+
});
|
|
1108
|
+
});
|
|
1109
|
+
|
|
1110
|
+
describe('#maxLength', function () {
|
|
1111
|
+
let uut;
|
|
1112
|
+
|
|
1113
|
+
beforeEach(function () {
|
|
1114
|
+
uut = lookupBuiltinTransform('maxLength');
|
|
1115
|
+
});
|
|
1116
|
+
|
|
1117
|
+
it('should keep text up to the given size, but null out longer text', function () {
|
|
1118
|
+
expect(uut('foo', 3)).to.eql('foo');
|
|
1119
|
+
expect(uut('foo!', 3)).to.be.null;
|
|
1120
|
+
expect(uut('', 0)).to.eql('');
|
|
1121
|
+
});
|
|
1122
|
+
|
|
1123
|
+
it('should fail if input is not [string, integer]', function () {
|
|
1124
|
+
expect(() => uut()).to.throw();
|
|
1125
|
+
expect(() => uut('foo', 'not an int')).to.throw();
|
|
1126
|
+
});
|
|
1127
|
+
});
|
|
1128
|
+
|
|
1129
|
+
describe('#minLength', function () {
|
|
1130
|
+
let uut;
|
|
1131
|
+
|
|
1132
|
+
beforeEach(function () {
|
|
1133
|
+
uut = lookupBuiltinTransform('minLength');
|
|
1134
|
+
});
|
|
1135
|
+
|
|
1136
|
+
it('should keep text of at least the given size, but null out shorter text', function () {
|
|
1137
|
+
expect(uut('foo', 3)).to.eql('foo');
|
|
1138
|
+
expect(uut('fo', 3)).to.be.null;
|
|
1139
|
+
expect(uut('', 0)).to.eql('');
|
|
1140
|
+
});
|
|
1141
|
+
|
|
1142
|
+
it('should fail if input is not [string, integer]', function () {
|
|
1143
|
+
expect(() => uut()).to.throw();
|
|
1144
|
+
expect(() => uut('foo', 'not an int')).to.throw();
|
|
1145
|
+
});
|
|
1146
|
+
});
|
|
918
1147
|
});
|
|
919
1148
|
|
|
920
1149
|
describe('Patterns', function () {
|
|
@@ -1028,6 +1257,16 @@ describe('Patterns', function () {
|
|
|
1028
1257
|
});
|
|
1029
1258
|
});
|
|
1030
1259
|
|
|
1260
|
+
it('the render configuration of a dynamic step', function () {
|
|
1261
|
+
shouldForward({
|
|
1262
|
+
steps: [{ dynamic: true, waitFor: ['#results'], delay: 1000 }],
|
|
1263
|
+
});
|
|
1264
|
+
});
|
|
1265
|
+
|
|
1266
|
+
it('timeout', function () {
|
|
1267
|
+
shouldForward({ timeout: 20000 });
|
|
1268
|
+
});
|
|
1269
|
+
|
|
1031
1270
|
it('emptyHtml', function () {
|
|
1032
1271
|
shouldForward({ emptyHtml: true });
|
|
1033
1272
|
shouldForward({ emptyHtml: false });
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
*/
|
|
11
11
|
|
|
12
12
|
import { expect } from 'chai';
|
|
13
|
-
import SearchExtractor from '../src/search-extractor.js';
|
|
13
|
+
import SearchExtractor, { runTransforms } from '../src/search-extractor.js';
|
|
14
14
|
import {
|
|
15
15
|
allSupportedParsers,
|
|
16
16
|
mockDocumentWith,
|
|
@@ -666,3 +666,21 @@ ${total} tests run: ${passed} passed, ${failed} failed (${ratio}% passed)
|
|
|
666
666
|
});
|
|
667
667
|
});
|
|
668
668
|
});
|
|
669
|
+
|
|
670
|
+
describe('#runTransforms', function () {
|
|
671
|
+
it('should apply all steps if none of them returns null', function () {
|
|
672
|
+
expect(runTransforms(' foo•bar ', [['split', '•', 0], ['trim']])).to.eql(
|
|
673
|
+
'foo',
|
|
674
|
+
);
|
|
675
|
+
});
|
|
676
|
+
|
|
677
|
+
it('should stop the chain without an error when a step returns null', function () {
|
|
678
|
+
expect(runTransforms('no separator here', [['split', '•', 0], ['trim']])).to
|
|
679
|
+
.be.null;
|
|
680
|
+
});
|
|
681
|
+
|
|
682
|
+
it('should return null for missing values', function () {
|
|
683
|
+
expect(runTransforms(null, [['trim']])).to.be.null;
|
|
684
|
+
expect(runTransforms(undefined, [['trim']])).to.be.null;
|
|
685
|
+
});
|
|
686
|
+
});
|
|
@@ -18,6 +18,15 @@ import chrome from 'sinon-chrome';
|
|
|
18
18
|
chai.use(chaiAsPromised);
|
|
19
19
|
chai.use(sinonChai);
|
|
20
20
|
|
|
21
|
+
// Node's own navigator.userAgent would fail the documentId support check.
|
|
22
|
+
Object.defineProperty(globalThis, 'navigator', {
|
|
23
|
+
value: {
|
|
24
|
+
userAgent:
|
|
25
|
+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
|
|
26
|
+
},
|
|
27
|
+
configurable: true,
|
|
28
|
+
});
|
|
29
|
+
|
|
21
30
|
// Fill in missing APIs if sinon-chrome did not provide them
|
|
22
31
|
chrome.webRequest.OnBeforeRequestOptions ||= {
|
|
23
32
|
blocking: 'blocking',
|