/
NashornSandboxImpl.java
403 lines (373 loc) · 15 KB
/
NashornSandboxImpl.java
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package delight.nashornsandbox.internal;
import com.google.common.base.Objects;
import delight.async.Value;
import delight.nashornsandbox.NashornSandbox;
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
import delight.nashornsandbox.internal.BeautifyJs;
import delight.nashornsandbox.internal.InterruptTest;
import delight.nashornsandbox.internal.MonitorThread;
import delight.nashornsandbox.internal.SandboxClassFilter;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import jdk.nashorn.api.scripting.NashornScriptEngineFactory;
import jdk.nashorn.api.scripting.ScriptObjectMirror;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Exceptions;
@SuppressWarnings("all")
public class NashornSandboxImpl implements NashornSandbox {
private SandboxClassFilter sandboxClassFilter;
private final Map<String, Object> globalVariables;
private ScriptEngine scriptEngine;
private Long maxCPUTimeInMs = Long.valueOf(0L);
private ExecutorService exectuor;
private boolean allowPrintFunctions = false;
private boolean allowReadFunctions = false;
private boolean allowLoadFunctions = false;
private boolean allowExitFunctions = false;
private boolean allowGlobalsObjects = false;
public void assertScriptEngine() {
try {
boolean _notEquals = (!Objects.equal(this.scriptEngine, null));
if (_notEquals) {
return;
}
final NashornScriptEngineFactory factory = new NashornScriptEngineFactory();
ScriptEngine _scriptEngine = factory.getScriptEngine(this.sandboxClassFilter);
this.scriptEngine = _scriptEngine;
this.scriptEngine.eval("var window = {};");
this.scriptEngine.eval(BeautifyJs.CODE);
Set<Map.Entry<String, Object>> _entrySet = this.globalVariables.entrySet();
for (final Map.Entry<String, Object> entry : _entrySet) {
String _key = entry.getKey();
Object _value = entry.getValue();
this.scriptEngine.put(_key, _value);
}
String _xifexpression = null;
if ((!this.allowPrintFunctions)) {
_xifexpression = (("" + "quit = function() {};\n") + "exit = function() {};\n");
} else {
_xifexpression = "";
}
String _plus = ("\n" + _xifexpression);
String _plus_1 = (_plus + "\n");
String _xifexpression_1 = null;
if ((!this.allowPrintFunctions)) {
_xifexpression_1 = (("" + "print = function() {};\n") + "echo = function() {};\n");
} else {
_xifexpression_1 = "";
}
String _plus_2 = (_plus_1 + _xifexpression_1);
String _plus_3 = (_plus_2 + "\n");
String _xifexpression_2 = null;
if ((!this.allowReadFunctions)) {
_xifexpression_2 = (("" + "readFully = function() {};\n") + "readLine = function() {};\n");
} else {
_xifexpression_2 = "";
}
String _plus_4 = (_plus_3 + _xifexpression_2);
String _plus_5 = (_plus_4 + "\n");
String _xifexpression_3 = null;
if ((!this.allowLoadFunctions)) {
_xifexpression_3 = (("" + "load = function() {};\n") + "loadWithNewGlobal = function() {};\n");
} else {
_xifexpression_3 = "";
}
String _plus_6 = (_plus_5 + _xifexpression_3);
String _plus_7 = (_plus_6 + "\n");
String _xifexpression_4 = null;
if ((!this.allowGlobalsObjects)) {
_xifexpression_4 = ((((((("" + "$ARG = null;\n") + "$ENV = null;\n") + "$EXEC = null;\n") + "$OPTIONS = null;\n") +
"$OUT = null;\n") + "$ERR = null;\n") + "$EXIT = null;\n");
} else {
_xifexpression_4 = "";
}
String _plus_8 = (_plus_7 + _xifexpression_4);
String _plus_9 = (_plus_8 + "\n");
this.scriptEngine.eval(_plus_9);
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
private static String replaceGroup(final String str, final String regex, final String replacementForGroup2) {
final Pattern pattern = Pattern.compile(regex);
final Matcher matcher = pattern.matcher(str);
final StringBuffer sb = new StringBuffer();
while (matcher.find()) {
matcher.appendReplacement(sb, ("$1" + replacementForGroup2));
}
matcher.appendTail(sb);
return sb.toString();
}
private static String injectInterruptionCalls(final String str, final int randomToken) {
String _xblockexpression = null;
{
String res = str.replaceAll(";\\n", ((";intCheckForInterruption" + Integer.valueOf(randomToken)) + "();\n"));
String _replaceGroup = NashornSandboxImpl.replaceGroup(res, "(while \\([^\\)]*)(\\) \\{)", ((") {intCheckForInterruption" + Integer.valueOf(randomToken)) + "();\n"));
res = _replaceGroup;
String _replaceGroup_1 = NashornSandboxImpl.replaceGroup(res, "(for \\([^\\)]*)(\\) \\{)", ((") {intCheckForInterruption" + Integer.valueOf(randomToken)) + "();\n"));
res = _replaceGroup_1;
String _replaceAll = res.replaceAll("\\} while \\(", (("\nintCheckForInterruption" + Integer.valueOf(randomToken)) + "();\n\\} while \\("));
_xblockexpression = res = _replaceAll;
}
return _xblockexpression;
}
@Override
public Object eval(final String js) {
try {
Object _xblockexpression = null;
{
this.assertScriptEngine();
if (((this.maxCPUTimeInMs).longValue() == 0)) {
return this.scriptEngine.eval(js);
}
Object _xsynchronizedexpression = null;
synchronized (this) {
Object _xblockexpression_1 = null;
{
final Value<Object> resVal = new Value<Object>(null);
final Value<Throwable> exceptionVal = new Value<Throwable>(null);
final MonitorThread monitorThread = new MonitorThread(((this.maxCPUTimeInMs).longValue() * 1000000));
boolean _equals = Objects.equal(this.exectuor, null);
if (_equals) {
throw new IllegalStateException(
"When a CPU time limit is set, an executor needs to be provided by calling .setExecutor(...)");
}
final Object monitor = new Object();
final Runnable _function = new Runnable() {
@Override
public void run() {
try {
boolean _contains = js.contains("intCheckForInterruption");
if (_contains) {
throw new IllegalArgumentException(
"Script contains the illegal string [intCheckForInterruption]");
}
Object _eval = NashornSandboxImpl.this.scriptEngine.eval("window.js_beautify;");
final ScriptObjectMirror jsBeautify = ((ScriptObjectMirror) _eval);
Object _call = jsBeautify.call("beautify", js);
final String beautifiedJs = ((String) _call);
Random _random = new Random();
int _nextInt = _random.nextInt();
final int randomToken = Math.abs(_nextInt);
StringConcatenation _builder = new StringConcatenation();
_builder.append("var InterruptTest = Java.type(\'");
String _name = InterruptTest.class.getName();
_builder.append(_name, "");
_builder.append("\');");
_builder.newLineIfNotEmpty();
_builder.append("var isInterrupted = InterruptTest.isInterrupted;");
_builder.newLine();
_builder.append("var intCheckForInterruption");
_builder.append(randomToken, "");
_builder.append(" = function() {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("if (isInterrupted()) {");
_builder.newLine();
_builder.append("\t ");
_builder.append("throw new Error(\'Interrupted");
_builder.append(randomToken, "\t ");
_builder.append("\')");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("};");
_builder.newLine();
String _injectInterruptionCalls = NashornSandboxImpl.injectInterruptionCalls(beautifiedJs, randomToken);
final String securedJs = (_builder.toString() + _injectInterruptionCalls);
final Thread mainThread = Thread.currentThread();
Thread _currentThread = Thread.currentThread();
monitorThread.setThreadToMonitor(_currentThread);
final Runnable _function = new Runnable() {
@Override
public void run() {
mainThread.interrupt();
}
};
monitorThread.setOnInvalidHandler(_function);
monitorThread.start();
try {
final Object res = NashornSandboxImpl.this.scriptEngine.eval(securedJs);
resVal.set(res);
} catch (final Throwable _t) {
if (_t instanceof ScriptException) {
final ScriptException e = (ScriptException)_t;
String _message = e.getMessage();
boolean _contains_1 = _message.contains(("Interrupted" + Integer.valueOf(randomToken)));
if (_contains_1) {
monitorThread.notifyOperationInterrupted();
} else {
exceptionVal.set(e);
monitorThread.stopMonitor();
synchronized (monitor) {
monitor.notify();
}
return;
}
} else {
throw Exceptions.sneakyThrow(_t);
}
} finally {
monitorThread.stopMonitor();
synchronized (monitor) {
monitor.notify();
}
}
} catch (final Throwable _t_1) {
if (_t_1 instanceof Throwable) {
final Throwable t = (Throwable)_t_1;
exceptionVal.set(t);
monitorThread.stopMonitor();
synchronized (monitor) {
monitor.notify();
}
} else {
throw Exceptions.sneakyThrow(_t_1);
}
}
}
};
this.exectuor.execute(_function);
synchronized (monitor) {
monitor.wait();
}
boolean _isCPULimitExceeded = monitorThread.isCPULimitExceeded();
if (_isCPULimitExceeded) {
String notGraceful = "";
boolean _gracefullyInterrputed = monitorThread.gracefullyInterrputed();
boolean _not = (!_gracefullyInterrputed);
if (_not) {
notGraceful = " The operation could not be gracefully interrupted.";
}
Throwable _get = exceptionVal.get();
throw new ScriptCPUAbuseException(
((("Script used more than the allowed [" + this.maxCPUTimeInMs) + " ms] of CPU time. ") + notGraceful), _get);
}
Throwable _get_1 = exceptionVal.get();
boolean _notEquals = (!Objects.equal(_get_1, null));
if (_notEquals) {
throw exceptionVal.get();
}
_xblockexpression_1 = resVal.get();
}
_xsynchronizedexpression = _xblockexpression_1;
}
_xblockexpression = _xsynchronizedexpression;
}
return _xblockexpression;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
@Override
public NashornSandbox setMaxCPUTime(final long limit) {
NashornSandboxImpl _xblockexpression = null;
{
this.maxCPUTimeInMs = Long.valueOf(limit);
_xblockexpression = this;
}
return _xblockexpression;
}
@Override
public NashornSandbox allow(final Class<?> clazz) {
NashornSandboxImpl _xblockexpression = null;
{
String _name = clazz.getName();
this.sandboxClassFilter.add(_name);
_xblockexpression = this;
}
return _xblockexpression;
}
@Override
public void disallow(final Class<?> clazz) {
String _name = clazz.getName();
this.sandboxClassFilter.remove(_name);
}
@Override
public boolean isAllowed(final Class<?> clazz) {
String _name = clazz.getName();
return this.sandboxClassFilter.contains(_name);
}
@Override
public void disallowAllClasses() {
this.sandboxClassFilter.clear();
}
@Override
public NashornSandbox inject(final String variableName, final Object object) {
NashornSandboxImpl _xblockexpression = null;
{
this.globalVariables.put(variableName, object);
Class<?> _class = object.getClass();
String _name = _class.getName();
boolean _contains = this.sandboxClassFilter.contains(_name);
boolean _not = (!_contains);
if (_not) {
Class<?> _class_1 = object.getClass();
this.allow(_class_1);
}
boolean _notEquals = (!Objects.equal(this.scriptEngine, null));
if (_notEquals) {
this.scriptEngine.put(variableName, object);
}
_xblockexpression = this;
}
return _xblockexpression;
}
@Override
public NashornSandbox setExecutor(final ExecutorService executor) {
NashornSandboxImpl _xblockexpression = null;
{
this.exectuor = executor;
_xblockexpression = this;
}
return _xblockexpression;
}
@Override
public ExecutorService getExecutor() {
return this.exectuor;
}
@Override
public Object get(final String variableName) {
Object _xblockexpression = null;
{
this.assertScriptEngine();
_xblockexpression = this.scriptEngine.get(variableName);
}
return _xblockexpression;
}
@Override
public void allowPrintFunctions(final boolean v) {
this.allowPrintFunctions = v;
}
@Override
public void allowReadFunctions(final boolean v) {
this.allowReadFunctions = v;
}
@Override
public void allowLoadFunctions(final boolean v) {
this.allowLoadFunctions = v;
}
@Override
public void allowExitFunctions(final boolean v) {
this.allowExitFunctions = v;
}
@Override
public void allowGlobalsObjects(final boolean v) {
this.allowGlobalsObjects = v;
}
public NashornSandboxImpl() {
SandboxClassFilter _sandboxClassFilter = new SandboxClassFilter();
this.sandboxClassFilter = _sandboxClassFilter;
HashMap<String, Object> _hashMap = new HashMap<String, Object>();
this.globalVariables = _hashMap;
this.allow(InterruptTest.class);
}
}