-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-prune-inbound-object.js
271 lines (261 loc) · 6.27 KB
/
trusted-prune-inbound-object.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
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
import {
hit,
matchStackTrace,
getPropertyInChain,
getWildcardPropertyInChain,
logMessage,
isPruningNeeded,
jsonPruner,
getPrunePath,
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
isEmptyObject,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
} from '../helpers';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-prune-inbound-object
*
* @description
* Removes listed properties from the result of calling specific function (if payload contains `Object`)
* and returns to the caller.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock/commit/1c9da227d7
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-prune-inbound-object', functionName[, propsToRemove [, obligatoryProps [, stack]]])
* ```
*
* - `functionName` — required, the name of the function to trap, it must have an object as an argument
* - `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 Object.getOwnPropertyNames call
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'Object.getOwnPropertyNames', 'example')
* ```
*
* Function call:
*
* ```js
* Object.getOwnPropertyNames({ one: 1, example: true })
* ```
*
* Input:
*
* ```json
* {
* "one": 1,
* "example": true
* }
* ```
*
* Output:
*
* ```json
* ["one"]
* ```
*
* 1. Removes property `ads` from the payload of the Object.keys call
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'Object.keys', 'ads')
* ```
*
* Function call:
*
* ```js
* Object.keys({ one: 1, two: 2, ads: true })
* ```
*
* Input:
*
* ```json
* {
* "one": 1,
* "two": 2,
* "ads": true
* }
* ```
*
* Output:
*
* ```json
* ["one", "two"]
* ```
*
* 1. Removes property `foo.bar` from the payload of the JSON.stringify call
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'JSON.stringify', 'foo.bar')
* ```
*
* Function call:
*
* ```js
* JSON.stringify({ foo: { bar: 1, a: 2 }, b: 3 })
* ```
*
* Input:
*
* ```json
* {
* "foo": {
* "bar": 1,
* "a": 2
* },
* "b": 3
* }
* ```
*
* Output:
*
* ```json
* {"foo":{"a":2},"b":3}
* ```
*
* 1. Removes property `foo.bar` from the payload of the JSON.stringify call if its error stack trace contains `test.js`
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'JSON.stringify', 'foo.bar', '', 'test.js')
* ```
*
* Function call:
*
* ```js
* JSON.stringify({ foo: { bar: 1, a: 2 }, b: 3 })
* ```
*
* Input:
*
* ```json
* {
* "foo": {
* "bar": 1,
* "a": 2
* },
* "b": 3
* }
* ```
*
* Output (if `test.js` in stack):
*
* ```json
* {"foo":{"a":2},"b":3}
* ```
*
* 1. Removes all `slots` properties at any depth during Object.entries call
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'Object.entries', '*.slots')
* ```
*
* Function call:
*
* ```js
* Object.entries({
* ad: { slots: [1, 2], type: "banner" },
* main: { title: "News" }
* })
* ```
*
* Input:
*
* ```json
* {
* "ad": {
* "slots": [1, 2],
* "type": "banner"
* },
* "main": {
* "slots": [3, 4],
* "title": "News"
* }
* }
* ```
*
* Output:
*
* ```json
* [
* ["ad", { "type": "banner" }],
* ["main", { "title": "News" }]
* ]
* ```
*
* 1. Call with only first and third argument will log the current hostname and matched payload at the console
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'JSON.stringify', '', 'bar', '')
* ```
*
* @added v1.9.91.
*/
/* eslint-enable max-len */
export function trustedPruneInboundObject(source, functionName, propsToRemove, requiredInitialProps, stack = '') {
if (!functionName) {
return;
}
const nativeObjects = {
nativeStringify: window.JSON.stringify,
};
const { base, prop } = getPropertyInChain(window, functionName);
if (!base || !prop || typeof base[prop] !== 'function') {
const message = `${functionName} is not a function`;
logMessage(source, message);
return;
}
const prunePaths = getPrunePath(propsToRemove);
const requiredPaths = getPrunePath(requiredInitialProps);
const objectWrapper = (target, thisArg, args) => {
let data = args[0];
if (typeof data === 'object') {
data = jsonPruner(source, data, prunePaths, requiredPaths, stack, nativeObjects);
args[0] = data;
}
return Reflect.apply(target, thisArg, args);
};
const objectHandler = {
apply: objectWrapper,
};
base[prop] = new Proxy(base[prop], objectHandler);
}
export const trustedPruneInboundObjectNames = [
'trusted-prune-inbound-object',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedPruneInboundObject.primaryName = trustedPruneInboundObjectNames[0];
trustedPruneInboundObject.injections = [
hit,
matchStackTrace,
getPropertyInChain,
getWildcardPropertyInChain,
logMessage,
isPruningNeeded,
jsonPruner,
getPrunePath,
// following helpers are needed for helpers above
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
isEmptyObject,
backupRegExpValues,
restoreRegExpValues,
isKeyInObject,
];