-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathtrusted-prune-inbound-object.js
More file actions
151 lines (142 loc) · 4.55 KB
/
trusted-prune-inbound-object.js
File metadata and controls
151 lines (142 loc) · 4.55 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
import {
hit,
matchStackTrace,
getPropertyInChain,
getWildcardPropertyInChain,
logMessage,
isPruningNeeded,
jsonPruner,
getPrunePath,
// following helpers are needed for helpers above
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
isEmptyObject,
} from '../helpers/index';
/* 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')
* ```
*
* For instance, the following call will return `['one']`
*
* ```html
* Object.getOwnPropertyNames({ one: 1, example: true })
* ```
*
* 2. Removes property `ads` from the payload of the Object.keys call
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'Object.keys', 'ads')
* ```
*
* For instance, the following call will return `['one', 'two']`
*
* ```html
* Object.keys({ one: 1, two: 2, ads: true })
* ```
*
* 3. Removes property `foo.bar` from the payload of the JSON.stringify call
*
* ```adblock
* example.org#%#//scriptlet('trusted-prune-inbound-object', 'JSON.stringify', 'foo.bar')
* ```
*
* For instance, the following call will return `'{"foo":{"a":2},"b":3}'`
*
* ```html
* JSON.stringify({ foo: { bar: 1, a: 2 }, b: 3 })
* ```
*
* 4. 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')
* ```
*
* 5. 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);
}
trustedPruneInboundObject.names = [
'trusted-prune-inbound-object',
// trusted scriptlets support no aliases
];
trustedPruneInboundObject.injections = [
hit,
matchStackTrace,
getPropertyInChain,
getWildcardPropertyInChain,
logMessage,
isPruningNeeded,
jsonPruner,
getPrunePath,
// following helpers are needed for helpers above
toRegExp,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
isEmptyObject,
];