-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathevaldata-prune.js
150 lines (143 loc) · 4.34 KB
/
evaldata-prune.js
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
import {
hit,
matchStackTrace,
getWildcardPropertyInChain,
logMessage,
toRegExp,
isPruningNeeded,
jsonPruner,
getPrunePath,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet evaldata-prune
*
* @description
* Removes specified properties from the result of calling eval (if payloads contains `Object`) and returns to the caller.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/commit/c8de9041917b61035171e454df886706f27fc4f3
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('evaldata-prune'[, propsToRemove [, obligatoryProps [, stack]]])
* ```
*
* - `propsToRemove` — optional, string of space-separated properties to remove
* - `obligatoryProps` — optional, string of space-separated properties
* which must be all present for the pruning to occur
* - `stack` — optional, string or regular expression that must match the current function call stack trace;
* if regular expression is invalid it will be skipped
*
* > Note please that you can use wildcard `*` for chain property name,
* > e.g. `ad.*.src` instead of `ad.0.src ad.1.src ad.2.src`.
*
* ### Examples
*
* 1. Removes property `example` from the payload of the eval call
*
* ```adblock
* example.org#%#//scriptlet('evaldata-prune', 'example')
* ```
*
* For instance, the following call will return `{ one: 1}`
*
* ```html
* eval({ one: 1, example: true })
* ```
*
* 2. If there are no specified properties in the payload of eval call, pruning will NOT occur
*
* ```adblock
* example.org#%#//scriptlet('evaldata-prune', 'one', 'obligatoryProp')
* ```
*
* For instance, the following call will return `{ one: 1, two: 2}`
*
* ```html
* JSON.parse('{"one":1,"two":2}')
* ```
*
* 3. A property in a list of properties can be a chain of properties
*
* ```adblock
* example.org#%#//scriptlet('evaldata-prune', 'a.b', 'ads.url.first')
* ```
*
* 4. Removes property `content.ad` from the payload of eval call if its error stack trace contains `test.js`
*
* ```adblock
* example.org#%#//scriptlet('evaldata-prune', 'content.ad', '', 'test.js')
* ```
*
* 5. A property in a list of properties can be a chain of properties with wildcard in it
*
* ```adblock
* example.org#%#//scriptlet('evaldata-prune', 'content.*.media.src', 'content.*.media.ad')
* ```
*
* 6. Call with no arguments will log the current hostname and object payload at the console
*
* ```adblock
* example.org#%#//scriptlet('evaldata-prune')
* ```
*
* 7. Call with only second argument will log the current hostname and matched object payload at the console
*
* ```adblock
* example.org#%#//scriptlet('evaldata-prune', '', '"id":"117458"')
* ```
*
* @added v1.9.37.
*/
/* eslint-enable max-len */
export function evalDataPrune(source, propsToRemove, requiredInitialProps, stack) {
const prunePaths = getPrunePath(propsToRemove);
const requiredPaths = getPrunePath(requiredInitialProps);
const nativeObjects = {
nativeStringify: window.JSON.stringify,
};
const evalWrapper = (target, thisArg, args) => {
let data = Reflect.apply(target, thisArg, args);
if (typeof data === 'object') {
data = jsonPruner(source, data, prunePaths, requiredPaths, stack, nativeObjects);
}
return data;
};
const evalHandler = {
apply: evalWrapper,
};
// eslint-disable-next-line no-eval
window.eval = new Proxy(window.eval, evalHandler);
}
export const evalDataPruneNames = [
'evaldata-prune',
// aliases are needed for matching the related scriptlet converted into our syntax
'evaldata-prune.js',
'ubo-evaldata-prune.js',
'ubo-evaldata-prune',
];
// eslint-disable-next-line prefer-destructuring
evalDataPrune.primaryName = evalDataPruneNames[0];
evalDataPrune.injections = [
hit,
matchStackTrace,
getWildcardPropertyInChain,
logMessage,
toRegExp,
isPruningNeeded,
jsonPruner,
getPrunePath,
// following helpers are needed for helpers above
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
];