-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtrusted-replace-argument.ts
More file actions
473 lines (429 loc) · 16.9 KB
/
trusted-replace-argument.ts
File metadata and controls
473 lines (429 loc) · 16.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import {
hit,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
matchStackTrace,
getPropertyInChain,
extractRegexAndReplacement,
logMessage,
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
nativeIsNaN,
isEmptyObject,
backupRegExpValues,
restoreRegExpValues,
} from '../helpers';
import { type Source } from './scriptlets';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-replace-argument
*
* @description
* Replaces a specific argument of a native method with a constant value, JSON parsed value
* or a value derived from a regular expression replacement.
*
* Related UBO scriptlet:
* https://github.com/gorhill/ublock/wiki/Resources-Library#trusted-replace-argumentjs-
*
* ### Syntax
*
* <!-- markdownlint-disable line-length -->
*
* ```text
* example.org#%#//scriptlet('trusted-replace-argument', methodPath, [argumentIndex, [argumentValue[, pattern[, stack[, verbose]]]]])
* ```
*
*
* - `methodPath` – required, string path to a native method (joined with `.` if needed). The property must be attached to `window`.
* - `argumentIndex` – required, string index of the argument to replace (0-based).
* - `argumentValue` – required, string value to set for the argument.
* If it starts with `replace:`, it is treated as a replacement pattern in the format `replace:/regex/replacement/`.
* To replace all occurrences of a pattern, the replacement string must include the global flag `g`, like this: `replace:/foo/bar/g`, otherwise only the first occurrence will be replaced.
* If it starts with `json:`, it is treated as a JSON string to parse and set for the argument. For example, `json:{"key": "value"}` will set the argument to an object `{ key: 'value' }`.
* If it does not start with `replace:` or `json:`, it is treated as a constant value to set for the argument, or as one of the following predefined constants:
* - `undefined`
* - `false`
* - `true`
* - `null`
* - `emptyObj` — empty object
* - `emptyArr` — empty array
* - `noopFunc` — function with empty body
* - `noopCallbackFunc` — function returning noopFunc
* - `trueFunc` — function returning true
* - `falseFunc` — function returning false
* - `throwFunc` — function throwing an error
* - `noopPromiseResolve` — function returning Promise object that is resolved with an empty response
* - `noopPromiseReject` — function returning Promise.reject()
* - `pattern` – optional, string or regular expression pattern to match the argument against. If provided, the argument will only be replaced if it matches this pattern.
* - `stack` — optional, string or regular expression that must match the current function call stack trace.
* - `verbose` — optional, string, if set to `'true'`, logs the method arguments. Defaults to `'false'`.
* It may be useful for debugging but it is not allowed for prod versions of filter lists.
*
*
* ### Examples
*
* 1. Set the first argument of `eval` with a constant value `"Replacement"` if the pattern matches:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-argument', 'eval', '0', '"Replacement"', 'Foo bar')
* ```
*
* For instance, the following call will return `"Replacement"`:
*
* ```html
* eval('"Foo bar"');
* ```
*
* 1. Replace the part `foo` of the first argument of `eval` with a `bar` value if the pattern matches:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-argument', 'eval', '0', 'replace:/foo/bar/', 'Text content foo')
* ```
*
* For instance, the following call will return `"Text content bar"`:
*
* ```html
* eval('"Text content foo"');
* ```
*
* 1. Replace all occurrences of the first argument of `JSON.parse` with a constant value `"no_ads"` if the pattern matches:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-argument', 'JSON.parse', '0', 'replace:/ads/no_ads/g', 'ads')
* ```
*
* For instance, the following call:
*
* ```html
* const jsonString = '{ "ads1": 1, "ads2": 2, "content": "fooBar", "ads3": 3 }';
* const result = JSON.parse(jsonString);
* ```
*
* will return the object:
*
* ```json
* {
* no_ads1: 1,
* no_ads2: 2,
* content: 'fooBar',
* no_ads3: 3
* }
* ```
*
* 1. Replace the third argument of `Object.defineProperty` with a JSON object `{"value": "disabled"}` if the pattern matches:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-argument', 'Object.defineProperty', '2', 'json:{"value": "disabled"}', 'enabled')
* ```
*
* For instance, `window.adblock` property for the following call will return `"disabled"`:
*
* ```html
* Object.defineProperty(window, 'adblock', { value: 'enabled' });
* ```
*
* 1. Replace first argument of `MutationObserver` with `noopFunc` if the pattern matches:
*
* ```adblock
* example.org#%#//scriptlet('trusted-replace-argument', 'MutationObserver', '0', 'noopFunc', 'Adblock')
* ```
*
* For instance, `callback` function for the following call will be replaced with `noopFunc`:
*
* ```html
* const callback = () => {
* if(adblock) {
* document.body.innerHTML = 'Adblock detected';
* }
* };
* const observerToPrevent = new MutationObserver(callback);
* ```
*
* <!-- markdownlint-enable line-length -->
*
* @added v2.2.9.
*/
/* eslint-enable max-len */
export function trustedReplaceArgument(
source: Source,
methodPath: string,
argumentIndex: string,
argumentValue: string,
pattern: string,
stack = '',
verbose = 'false',
) {
// If verbose is 'false', methodPath, argumentIndex, and argumentValue are required.
// If verbose is 'true', only methodPath is required. It's only used for logging.
if (
((!methodPath || !argumentIndex || !argumentValue) && verbose === 'false')
|| (!methodPath && verbose === 'true')
) {
return;
}
// Scriptlet should log when "verbose" is set to "true", "methodPath" is set
// and no other parameters are provided.
const SHOULD_LOG_ONLY = verbose === 'true' && !argumentIndex && !argumentValue && !pattern && !stack;
const MARKERS = {
JSON: 'json:',
REPLACE: 'replace:',
};
let constantValue;
let replaceRegexValue: RegExp | string = '';
let shouldReplaceArgument = false;
if (argumentValue.startsWith(MARKERS.REPLACE)) {
const replacementRegexPair = extractRegexAndReplacement(argumentValue);
if (!replacementRegexPair) {
logMessage(source, `Invalid argument value format: ${argumentValue}`);
return;
}
replaceRegexValue = replacementRegexPair.regexPart;
constantValue = replacementRegexPair.replacementPart;
shouldReplaceArgument = true;
} else if (argumentValue.startsWith(MARKERS.JSON)) {
try {
constantValue = JSON.parse(argumentValue.slice(MARKERS.JSON.length));
} catch (error) {
logMessage(source, `Invalid JSON argument value: ${argumentValue}`);
return;
}
} else {
const emptyArr = noopArray();
const emptyObj = noopObject();
if (argumentValue === 'undefined') {
constantValue = undefined;
} else if (argumentValue === 'false') {
constantValue = false;
} else if (argumentValue === 'true') {
constantValue = true;
} else if (argumentValue === 'null') {
constantValue = null;
} else if (argumentValue === 'emptyArr') {
constantValue = emptyArr;
} else if (argumentValue === 'emptyObj') {
constantValue = emptyObj;
} else if (argumentValue === 'noopFunc') {
constantValue = noopFunc;
} else if (argumentValue === 'noopCallbackFunc') {
constantValue = noopCallbackFunc;
} else if (argumentValue === 'trueFunc') {
constantValue = trueFunc;
} else if (argumentValue === 'falseFunc') {
constantValue = falseFunc;
} else if (argumentValue === 'throwFunc') {
constantValue = throwFunc;
} else if (argumentValue === 'noopPromiseResolve') {
constantValue = noopPromiseResolve;
} else if (argumentValue === 'noopPromiseReject') {
constantValue = noopPromiseReject;
} else if (/^-?\d+$/.test(argumentValue)) {
constantValue = parseFloat(argumentValue);
if (nativeIsNaN(constantValue)) {
return;
}
} else {
constantValue = argumentValue;
}
}
const getPathParts = getPropertyInChain as unknown as (base: Window, chain: string) => {
base: Record<string, unknown>;
prop: string;
chain?: string;
};
const { base, chain, prop } = getPathParts(window, methodPath);
// Undefined `chain` indicates successful reaching the end prop.
if (typeof chain !== 'undefined') {
logMessage(source, `Could not reach the end of the prop chain: ${methodPath}`);
return;
}
const nativeMethod = base[prop];
if (!nativeMethod || typeof nativeMethod !== 'function') {
logMessage(source, `Could not retrieve the method: ${methodPath}`);
return;
}
/**
* Converts an object to a JSON string, converting functions to their string representation.
* Required in case if object contains functions, as JSON.stringify does not handle them.
* For example `{ foo: () => 'bar' }` will be stringified as `{}` by default.
*
* @param obj - The object to stringify.
* @returns The JSON string representation of the object, with functions stringified.
*/
const stringifyObject = (obj: unknown) => {
return JSON.stringify(obj, (key, value) => (typeof value === 'function' ? value.toString() : value));
};
/**
* Formats the provided arguments into a readable string for logging purposes.
*
* @param args - The array of arguments to format.
* @param when - Optional. Indicates if the arguments are 'original' or 'modified'.
* Defaults to 'original'. Use 'modified' to indicate arguments where modified after replacement.
* @returns A string representation of the arguments, including their indices and string values.
*/
const createFormattedMessage = (args: unknown[], when = 'original') => {
const formattedArgs = args.map((arg, index) => {
if (typeof arg === 'object' && arg !== null) {
try {
return `${index}: ${stringifyObject(arg)} // Object converted to string`;
} catch (e) {
// If JSON.stringify fails, fall back to String conversion
return `${index}: ${String(arg)} // Object conversion failed`;
}
}
return `${index}: ${String(arg)}`;
});
const modifiedOrOriginal = when === 'modified' ? 'modified' : when;
const message = `${methodPath} ${modifiedOrOriginal} arguments:\n${formattedArgs.join(',\n')}`;
return message;
};
/**
* Checks whether the provided argument matches the specified pattern and stack trace requirements.
*
* @param arg - The argument to check.
* @returns True if the argument matches the pattern and stack trace (if provided), otherwise false.
*/
const checkArgument = (arg: unknown) => {
if (stack && !matchStackTrace(stack, new Error().stack || '')) {
return false;
}
if (pattern) {
if (typeof arg === 'object' && arg !== null) {
// If the argument is an object, convert it to a string for pattern matching.
try {
const argString = stringifyObject(arg);
return !!argString && toRegExp(pattern).test(argString);
} catch (error) {
logMessage(source, `Failed to stringify argument: ${arg}\nError: ${error}`);
}
}
const argumentContent = String(arg);
return !!argumentContent && toRegExp(pattern).test(argumentContent);
}
return true;
};
let isMatchingSuspended = false;
const applyWrapper = (target: Function, thisArg: any, argumentsList: unknown[]) => {
if (isMatchingSuspended) {
isMatchingSuspended = false;
return Reflect.apply(target, thisArg, argumentsList);
}
isMatchingSuspended = true;
// Log the original arguments before modification
if (verbose === 'true') {
const formattedMessage = createFormattedMessage(argumentsList);
logMessage(source, formattedMessage);
}
// If we only need to log the arguments, skip further processing
if (SHOULD_LOG_ONLY) {
isMatchingSuspended = false;
return Reflect.apply(target, thisArg, argumentsList);
}
const argumentToReplace = argumentsList[Number(argumentIndex)];
const shouldSetArgument = checkArgument(argumentToReplace);
if (!shouldSetArgument) {
isMatchingSuspended = false;
return Reflect.apply(target, thisArg, argumentsList);
}
if (typeof argumentToReplace === 'string' && shouldReplaceArgument) {
argumentsList[Number(argumentIndex)] = argumentToReplace
.replace(replaceRegexValue, constantValue as string);
} else {
argumentsList[Number(argumentIndex)] = constantValue;
}
// Log the modified arguments after replacement
if (verbose === 'true') {
const formattedMessage = createFormattedMessage(argumentsList, 'modified');
logMessage(source, formattedMessage);
}
hit(source);
isMatchingSuspended = false;
return Reflect.apply(target, thisArg, argumentsList);
};
const constructWrapper = (target: Function, argumentsList: unknown[], newTarget: any) => {
if (isMatchingSuspended) {
isMatchingSuspended = false;
return Reflect.construct(target, argumentsList, newTarget);
}
isMatchingSuspended = true;
// Log the original arguments before modification
if (verbose === 'true') {
const formattedMessage = createFormattedMessage(argumentsList);
logMessage(source, formattedMessage);
}
// If we only need to log the arguments, skip further processing
if (SHOULD_LOG_ONLY) {
isMatchingSuspended = false;
return Reflect.construct(target, argumentsList, newTarget);
}
const argumentToReplace = argumentsList[Number(argumentIndex)];
const shouldSetArgument = checkArgument(argumentToReplace);
if (!shouldSetArgument) {
isMatchingSuspended = false;
return Reflect.construct(target, argumentsList, newTarget);
}
if (typeof argumentToReplace === 'string' && shouldReplaceArgument) {
argumentsList[Number(argumentIndex)] = argumentToReplace
.replace(replaceRegexValue, constantValue as string);
} else {
argumentsList[Number(argumentIndex)] = constantValue;
}
// Log the modified arguments after replacement
if (verbose === 'true') {
const formattedMessage = createFormattedMessage(argumentsList, 'modified');
logMessage(source, formattedMessage);
}
hit(source);
isMatchingSuspended = false;
return Reflect.construct(target, argumentsList, newTarget);
};
const getWrapper = (target: Function, propName: string, receiver: any) => {
if (propName === 'toString') {
return target.toString.bind(target);
}
return Reflect.get(target, propName, receiver);
};
const objectHandler = {
apply: applyWrapper,
construct: constructWrapper,
get: getWrapper,
};
base[prop] = new Proxy(nativeMethod, objectHandler);
}
export const trustedReplaceArgumentNames = [
'trusted-replace-argument',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedReplaceArgument.primaryName = trustedReplaceArgumentNames[0];
trustedReplaceArgument.injections = [
hit,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
matchStackTrace,
getPropertyInChain,
extractRegexAndReplacement,
logMessage,
// following helpers are needed for helpers above
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
nativeIsNaN,
isEmptyObject,
backupRegExpValues,
restoreRegExpValues,
];