-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathtrusted-set-constant.js
287 lines (270 loc) · 8.77 KB
/
trusted-set-constant.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import {
hit,
inferValue,
logMessage,
noopArray,
noopObject,
noopCallbackFunc,
noopFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
getPropertyInChain,
setPropertyAccess,
toRegExp,
matchStackTrace,
nativeIsNaN,
isEmptyObject,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
} from '../helpers';
/* eslint-disable max-len */
/**
* @trustedScriptlet trusted-set-constant
*
* @description
* Creates a constant property and assigns it a specified value.
*
* > Actually, it's not a constant. Please note, that it can be rewritten with a value of a different type.
*
* > If empty object is present in chain it will be trapped until chain leftovers appear.
*
* > Use [set-constant](./about-scriptlets.md#set-constant) to set predefined values and functions.
*
* ### Syntax
*
* ```text
* example.org#%#//scriptlet('trusted-set-constant', property, value[, stack])
* ```
*
* - `property` — required, path to a property (joined with `.` if needed). The property must be attached to `window`.
* - `value` — required, an arbitrary value to be set; value type is being inferred from the argument,
* e.g '500' will be set as number; to set string type value wrap argument into another pair of quotes: `'"500"'`;
* - `stack` — optional, string or regular expression that must match the current function call stack trace;
* if regular expression is invalid it will be skipped
*
* ### Examples
*
* 1. Set property values of different types
*
* ```adblock
* ! Set string value wrapping argument into another pair of quotes
* example.org#%#//scriptlet('trusted-set-constant', 'click_r', '"null"')
*
* ✔ window.click_r === 'null'
* ✔ typeof window.click_r === 'string'
*
* ! Set inferred null value
* example.org#%#//scriptlet('trusted-set-constant', 'click_r', 'null')
*
* ✔ window.click_r === null
* ✔ typeof window.click_r === 'object'
*
* ! Set number type value
* example.org#%#//scriptlet('trusted-set-constant', 'click_r', '48')
*
* ✔ window.click_r === 48
* ✔ typeof window.click_r === 'number'
*
* ! Set array or object as property value, argument should be a JSON string
* example.org#%#//scriptlet('trusted-set-constant', 'click_r', '[1,"string"]')
* example.org#%#//scriptlet('trusted-set-constant', 'click_r', '{"aaa":123,"bbb":{"ccc":"string"}}')
* ```
*
* 1. Use script stack matching to set value
*
* ```adblock
* ! `document.first` will return `1` if the method is related to `checking.js`
* example.org#%#//scriptlet('trusted-set-constant', 'document.first', '1', 'checking.js')
*
* ✔ document.first === 1 // if the condition described above is met
* ```
*
* @added v1.8.2.
*/
/* eslint-enable max-len */
export function trustedSetConstant(source, property, value, stack) {
if (!property
|| !matchStackTrace(stack, new Error().stack)) {
return;
}
let constantValue;
try {
constantValue = inferValue(value);
} catch (e) {
logMessage(source, e);
return;
}
let canceled = false;
const mustCancel = (value) => {
if (canceled) {
return canceled;
}
canceled = value !== undefined
&& constantValue !== undefined
&& typeof value !== typeof constantValue
&& value !== null;
return canceled;
};
/**
* Safely sets property on a given object
*
* IMPORTANT! this duplicates corresponding func in set-constant scriptlet as
* reorganizing this to common helpers will most definitely complicate debugging
*
* @param {object} base arbitrary reachable object
* @param {string} prop property name
* @param {boolean} configurable if set property should be configurable
* @param {object} handler custom property descriptor object
* @returns {boolean} true if prop was trapped successfully
*/
const trapProp = (base, prop, configurable, handler) => {
if (!handler.init(base[prop])) {
return false;
}
const origDescriptor = Object.getOwnPropertyDescriptor(base, prop);
let prevSetter;
// This is required to prevent scriptlets overwrite each over
if (origDescriptor instanceof Object) {
// This check is required to avoid defining non-configurable props
if (!origDescriptor.configurable) {
const message = `Property '${prop}' is not configurable`;
logMessage(source, message);
return false;
}
base[prop] = constantValue;
if (origDescriptor.set instanceof Function) {
prevSetter = origDescriptor.set;
}
}
Object.defineProperty(base, prop, {
configurable,
get() {
return handler.get();
},
set(a) {
if (prevSetter !== undefined) {
prevSetter(a);
}
handler.set(a);
},
});
return true;
};
/**
* Traverses given chain to set constant value to its end prop
* Chains that yet include non-object values (e.g null) are valid and will be
* traversed when appropriate chain member is set by an external script
*
* IMPORTANT! this duplicates corresponding func in set-constant scriptlet as
* reorganizing this to common helpers will most definitely complicate debugging
*
* @param {object} owner object that owns chain
* @param {string} property chain of owner properties
*/
const setChainPropAccess = (owner, property) => {
const chainInfo = getPropertyInChain(owner, property);
const { base } = chainInfo;
const { prop, chain } = chainInfo;
// Handler method init is used to keep track of factual value
// and apply mustCancel() check only on end prop
const inChainPropHandler = {
factValue: undefined,
init(a) {
this.factValue = a;
return true;
},
get() {
return this.factValue;
},
set(a) {
// Prevent breakage due to loop assignments like win.obj = win.obj
if (this.factValue === a) {
return;
}
this.factValue = a;
if (a instanceof Object) {
setChainPropAccess(a, chain);
}
},
};
const endPropHandler = {
init(a) {
if (mustCancel(a)) {
return false;
}
return true;
},
get() {
return constantValue;
},
set(a) {
if (!mustCancel(a)) {
return;
}
constantValue = a;
},
};
// End prop case
if (!chain) {
const isTrapped = trapProp(base, prop, false, endPropHandler);
if (isTrapped) {
hit(source);
}
return;
}
// Null prop in chain
if (base !== undefined && base[prop] === null) {
trapProp(base, prop, true, inChainPropHandler);
return;
}
// Empty object prop in chain
if ((base instanceof Object || typeof base === 'object') && isEmptyObject(base)) {
trapProp(base, prop, true, inChainPropHandler);
}
// Defined prop in chain
const propValue = owner[prop];
if (propValue instanceof Object || (typeof propValue === 'object' && propValue !== null)) {
setChainPropAccess(propValue, chain);
}
// Undefined prop in chain
trapProp(base, prop, true, inChainPropHandler);
};
setChainPropAccess(window, property);
}
export const trustedSetConstantNames = [
'trusted-set-constant',
// trusted scriptlets support no aliases
];
// eslint-disable-next-line prefer-destructuring
trustedSetConstant.primaryName = trustedSetConstantNames[0];
trustedSetConstant.injections = [
hit,
inferValue,
logMessage,
noopArray,
noopObject,
noopFunc,
noopCallbackFunc,
trueFunc,
falseFunc,
throwFunc,
noopPromiseReject,
noopPromiseResolve,
getPropertyInChain,
setPropertyAccess,
toRegExp,
matchStackTrace,
nativeIsNaN,
isEmptyObject,
getNativeRegexpTest,
// following helpers should be imported and injected
// because they are used by helpers above
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
];