-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathabort-on-stack-trace.js
180 lines (169 loc) · 5.22 KB
/
abort-on-stack-trace.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
import {
randomId,
setPropertyAccess,
getPropertyInChain,
createOnErrorHandler,
hit,
isValidStrPattern,
matchStackTrace,
getDescriptorAddon,
logMessage,
shouldAbortInlineOrInjectedScript,
escapeRegExp,
toRegExp,
isEmptyObject,
getNativeRegexpTest,
backupRegExpValues,
restoreRegExpValues,
} from '../helpers';
/* eslint-disable max-len */
/**
* @scriptlet abort-on-stack-trace
*
* @description
* Aborts a script when it attempts to utilize (read or write to) the specified property
* and it's error stack trace contains given value.
*
* Related UBO scriptlet:
* https://github.com/gorhill/uBlock-for-firefox-legacy/commit/7099186ae54e70b588d5e99554a05d783cabc8ff
*
* ### Syntax
*
* ```text
* example.com#%#//scriptlet('abort-on-stack-trace', property, stack)
* ```
*
* - `property` — required, path to a property. The property must be attached to window.
* - `stack` — required, string that must match the current function call stack trace.
* - values to abort inline or injected script, accordingly:
* - `inlineScript`
* - `injectedScript`
*
* ### Examples
*
* 1. Aborts script when it tries to access `window.Ya` and it's error stack trace contains `test.js`
*
* ```adblock
* example.org#%#//scriptlet('abort-on-stack-trace', 'Ya', 'test.js')
* ```
*
* 1. Aborts script when it tries to access `window.Ya.videoAd` and it's error stack trace contains `test.js`
*
* ```adblock
* example.org#%#//scriptlet('abort-on-stack-trace', 'Ya.videoAd', 'test.js')
* ```
*
* 1. Aborts script when stack trace matches with any of these parameters
*
* ```adblock
* example.org#%#//scriptlet('abort-on-stack-trace', 'Ya', 'yandexFuncName')
* example.org#%#//scriptlet('abort-on-stack-trace', 'Ya', 'yandexScriptName')
* ```
*
* 1. Aborts script when it tries to access `window.Ya` and it's an inline script
*
* ```adblock
* example.org#%#//scriptlet('abort-on-stack-trace', 'Ya', 'inlineScript')
* ```
*
* 1. Aborts script when it tries to access `window.Ya` and it's an injected script
*
* ```adblock
* example.org#%#//scriptlet('abort-on-stack-trace', 'Ya', 'injectedScript')
* ```
*
* @added v1.5.0.
*/
/* eslint-enable max-len */
export function abortOnStackTrace(source, property, stack) {
if (!property || !stack) {
return;
}
const rid = randomId();
const abort = () => {
hit(source);
throw new ReferenceError(rid);
};
const setChainPropAccess = (owner, property) => {
const chainInfo = getPropertyInChain(owner, property);
let { base } = chainInfo;
const { prop, chain } = chainInfo;
if (chain) {
const setter = (a) => {
base = a;
if (a instanceof Object) {
setChainPropAccess(a, chain);
}
};
Object.defineProperty(owner, prop, {
get: () => base,
set: setter,
});
return;
}
if (!stack.match(/^(inlineScript|injectedScript)$/) && !isValidStrPattern(stack)) {
logMessage(source, `Invalid parameter: ${stack}`);
return;
}
// Prevent infinite loops when trapping prop used by helpers in getter/setter
const descriptorWrapper = Object.assign(getDescriptorAddon(), {
value: base[prop],
get() {
if (!this.isAbortingSuspended
&& this.isolateCallback(matchStackTrace, stack, new Error().stack)) {
abort();
}
return this.value;
},
set(newValue) {
if (!this.isAbortingSuspended
&& this.isolateCallback(matchStackTrace, stack, new Error().stack)) {
abort();
}
this.value = newValue;
},
});
setPropertyAccess(base, prop, {
// Call wrapped getter and setter to keep isAbortingSuspended & isolateCallback values
get() {
return descriptorWrapper.get.call(descriptorWrapper);
},
set(newValue) {
descriptorWrapper.set.call(descriptorWrapper, newValue);
},
});
};
setChainPropAccess(window, property);
window.onerror = createOnErrorHandler(rid).bind();
}
export const abortOnStackTraceNames = [
'abort-on-stack-trace',
// aliases are needed for matching the related scriptlet converted into our syntax
'abort-on-stack-trace.js',
'ubo-abort-on-stack-trace.js',
'aost.js',
'ubo-aost.js',
'ubo-abort-on-stack-trace',
'ubo-aost',
'abp-abort-on-stack-trace',
];
// eslint-disable-next-line prefer-destructuring
abortOnStackTrace.primaryName = abortOnStackTraceNames[0];
abortOnStackTrace.injections = [
randomId,
setPropertyAccess,
getPropertyInChain,
createOnErrorHandler,
hit,
isValidStrPattern,
escapeRegExp,
matchStackTrace,
getDescriptorAddon,
logMessage,
toRegExp,
isEmptyObject,
getNativeRegexpTest,
shouldAbortInlineOrInjectedScript,
backupRegExpValues,
restoreRegExpValues,
];