-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathSANDBOX.txt
341 lines (278 loc) · 9.75 KB
/
SANDBOX.txt
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
--------------------------------------------------------------------------------
branch: 'v01'
- version: 1.x
- DOES support closure
- does NOT support sandbox
branch: 'v02'
- version: 2.x
- includes potentially breaking changes
to add support for a sandbox
-------------------------------------------------------------------------------- v02: design
Java:
=====
boolean useJsClosure = script.useJsClosure();
boolean useJsSandbox = script.useJsSandbox();
JS (!useJsClosure):
==================
// userscript
JS (useJsClosure):
==================
(function(unsafeWindow, useSandbox){
useSandbox = (useSandbox && unsafeWindow.Proxy && unsafeWindow.Reflect);
// ====================================================
// placeholder: define locally scoped GM.* and GM_* API
// ====================================================
var GM = GM_info = {};
var var GM_addElement = GM_addStyle = GM_deleteValue = GM_fetch = GM_getResourceText = GM_getResourceURL = GM_getValue = GM_listValues = GM_log = GM_registerMenuCommand = GM_setValue = GM_unregisterMenuCommand = GM_xmlhttpRequest = GM_addValueChangeListener = GM_download = GM_getTab = GM_getTabs = GM_notification = GM_openInTab = GM_removeValueChangeListener = GM_saveTab = GM_setClipboard = GM_webRequest = function(){};
// ====================================================
var _GM_getWindowProxyHander = function(windowOwnProps) {
if (!windowOwnProps || (typeof windowOwnProps !== 'object')) {
windowOwnProps = {};
}
var windowProxyHander = {
defineProperty(target, prop, descriptor) {
unsafeWindow.Object.defineProperty(windowOwnProps, prop, descriptor);
return true;
},
deleteProperty(target, prop) {
if (prop in windowOwnProps) {
delete windowOwnProps[prop];
return true;
}
return false;
},
get(target, prop, receiver) {
var value, thisArg;
if (prop in windowOwnProps) {
value = windowOwnProps[prop];
thisArg = windowOwnProps;
}
else {
value = unsafeWindow.Reflect.get(target, prop);
thisArg = target;
}
return (typeof value === 'function')
? value.bind(thisArg)
: value;
},
getOwnPropertyDescriptor(target, prop) {
if (prop in windowOwnProps) {
return unsafeWindow.Object.getOwnPropertyDescriptor(windowOwnProps, prop);
}
return unsafeWindow.Reflect.getOwnPropertyDescriptor(target, prop);
},
has(target, prop) {
return (prop in windowOwnProps) || (prop in target);
},
ownKeys(target) {
return [
...unsafeWindow.Object.keys(windowOwnProps),
...unsafeWindow.Reflect.ownKeys(target)
];
},
set(target, prop, value, receiver) {
windowOwnProps[prop] = value;
},
};
return windowProxyHander;
};
var _GM_addApiToWindowProxy = function(windowProxy) {
if (typeof windowProxy === 'object') {
var entries = {
// ---------------------
// Greasemonkey API (v4)
// ---------------------
'GM': GM,
// -------------------------
// Greasemonkey API (legacy)
// -------------------------
'GM_addElement': GM_addElement,
'GM_addStyle': GM_addStyle,
'GM_deleteValue': GM_deleteValue,
'GM_fetch': GM_fetch,
'GM_getResourceText': GM_getResourceText,
'GM_getResourceURL': GM_getResourceURL,
'GM_getValue': GM_getValue,
'GM_info': GM_info,
'GM_listValues': GM_listValues,
'GM_log': GM_log,
'GM_registerMenuCommand': GM_registerMenuCommand,
'GM_setValue': GM_setValue,
'GM_unregisterMenuCommand': GM_unregisterMenuCommand,
'GM_xmlhttpRequest': GM_xmlhttpRequest,
// ------------------
// JSMISSINGFUNCTIONS
// ------------------
'GM_addValueChangeListener': GM_addValueChangeListener,
'GM_download': GM_download,
'GM_getTab': GM_getTab,
'GM_getTabs': GM_getTabs,
'GM_notification': GM_notification,
'GM_openInTab': GM_openInTab,
'GM_removeValueChangeListener': GM_removeValueChangeListener,
'GM_saveTab': GM_saveTab,
'GM_setClipboard': GM_setClipboard,
'GM_webRequest': GM_webRequest
};
var keys = Object.keys(entries);
var key, val;
for (var i=0; i < keys.length; i++) {
key = keys[i];
val = entries[key];
windowProxy[key] = val;
}
}
};
var window, self, globalThis;
if (useSandbox) {
window = self = globalThis = new unsafeWindow.Proxy(unsafeWindow, _GM_getWindowProxyHander());
window.window = window.self = window;
_GM_addApiToWindowProxy(window);
}
else {
window = self = globalThis = unsafeWindow;
}
var userscript_wrapper = function(){
// userscript
}
userscript_wrapper.call(window)
})(window, true); // where value of 2nd parameter is set by the Java variable: 'useJsSandbox'
-------------------------------------------------------------------------------- v02: development
update: Script model and DB schema
changes to userscript metadata block:
=====================================
* add properties:
@flag noJsClosure
@flag noJsSandbox
@flags noJsClosure|noJsSandbox
changes to Script model:
========================
* add methods:
script.useJsClosure()
script.useJsSandbox()
* remove methods:
script.isUnwrap()
changes to DB schema in ScriptStoreSQLite:
==========================================
ALTER TABLE 'script' RENAME COLUMN 'unwrap' TO 'flags';
this column is an INTEGER.
previously, it could only hold values: 0 or 1.
which correspond to the boolean presence or absence
of the (now legacy) metadata property: '@unwrap'
where its value (if any) doesn't matter.
the previous usage for the DB field is compatible with its new usage.
static final int noJsClosureFlag = 1;
static final int noJsSandboxFlag = 2;
where:
@unwrap
is equivalent to:
@flag noJsClosure
cross-reference to other implementations:
=========================================
----------------
common behavior:
----------------
* when there is NOT a sandbox:
- 'unsafeWindow' is not defined
- 'window' exposes the real 'window'
* when there IS a sandbox:
- 'unsafeWindow' is defined and exposes the real 'window'
- 'window' is a proxy that restricts access to the real 'window'
--------------
violentmonkey:
--------------
https://violentmonkey.github.io/api/metadata-block/#unwrap
'@unwrap'
does NOT use a closure
does NOT use a sandbox
does NOT include the GM.* or GM_* API
note: '@grant' is ignored
https://violentmonkey.github.io/api/metadata-block/#grant
'@grant none'
DOES use a closure
does NOT use a sandbox
does NOT include the GM.* or GM_* API
'@grant xxx'
conditionally includes a cherry-picked GM.* or GM_* API method
note: has the side-effect that a sandbox is enabled
when no '@grant' is specified
default: '@grant none'
-------------
tampermonkey:
-------------
https://www.tampermonkey.net/documentation.php#meta:unwrap
'@unwrap'
does NOT use a closure
does NOT use a sandbox
note: documentation is unclear regarding GM_* API and '@grant'
test: confirms that behavior is identical to 'violentmonkey'
https://www.tampermonkey.net/documentation.php#meta:grant
'@grant none'
DOES use a closure
does NOT use a sandbox
does NOT include the GM.* or GM_* API
'@grant xxx'
conditionally includes a cherry-picked GM.* or GM_* API method
when no '@grant' is specified
DOES use a closure
DOES use a sandbox
does NOT include the GM.* or GM_* API
usage of flags in my implementation:
====================================
* both a closure and a sandbox ARE used by default,
unless explicitly disabled
* to explicitly disable a closure,
use any of:
'@unwrap'
'@flag noJsClosure'
'@flags noJsClosure'
* a sandbox cannot be used without a closure
* to explicitly disable a sandbox,
either explicitly disable a closure,
or use any of:
'@grant none'
'@flag noJsSandbox'
'@flags noJsSandbox'
---------
behavior:
---------
* when there is NOT a closure:
- there is NOT a sandbox
- 'unsafeWindow' is not defined
- the real 'window' is exposed
by the following globally scoped variables:
* 'this'
* 'window'
* 'self'
- does NOT include the GM.* or GM_* API
* when there IS a closure:
- the real 'window' is exposed
by the following locally scoped variables:
* 'unsafeWindow'
- DOES include the entire GM.* and GM_* API
* when there IS a closure, but NOT a sandbox:
- the real 'window' is exposed
by the following locally scoped variables:
* 'this'
* 'window'
* 'self'
* 'globalThis'
- the entire GM.* and GM_* API is exposed by:
* locally scoped variables
* when there IS a closure, and IS a sandbox:
- a proxy that restricts access to the real 'window' is exposed
by the following locally scoped variables:
* 'this'
* 'window'
* 'self'
* 'globalThis'
- the entire GM.* and GM_* API is exposed by:
* locally scoped variables
* properties of the proxy object
- ex: 'window.GM.*' and 'window.GM_*'
---------
comments:
---------
* aside from using '@grant none' to conditionally disable a sandbox,
the '@grant' metadata property is otherwise ignored
--------------------------------------------------------------------------------