-
Notifications
You must be signed in to change notification settings - Fork 81
/
NashornSandboxImpl.java
538 lines (458 loc) · 15.5 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package delight.nashornsandbox.internal;
import delight.nashornsandbox.NashornSandbox;
import delight.nashornsandbox.SandboxScriptContext;
import delight.nashornsandbox.SecuredJsCache;
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
import delight.nashornsandbox.exceptions.ScriptMemoryAbuseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.script.Bindings;
import javax.script.Compilable;
import javax.script.CompiledScript;
import javax.script.Invocable;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptException;
import javax.script.SimpleScriptContext;
import java.io.Writer;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* Nashorn sandbox implementation.
*
* <p>
* Created on 2015-08-07
* </p>
*
* @author <a href="mailto:mxro@nowhere.com">mxro</a>
* @author <a href="mailto:mellster2000@yahoo.com">Marco Ellwanger</a>
* @author <a href="mailto:dev@youness.org">Youness SAHOUANE</a>
* @author <a href="mailto:eduveks@gmail.com">Eduardo Velasques</a>
* @author <a href="mailto:philip.borgstrom@gmail.com">philipborg</a>
* @author <a href="mailto:marcin.golebski@verbis.pl">Marcin Golebski</a>
* @version $Id$
*/
@SuppressWarnings("restriction")
public class NashornSandboxImpl implements NashornSandbox {
static final Logger LOG = LoggerFactory.getLogger(NashornSandbox.class);
protected final SandboxClassFilter sandboxClassFilter;
protected final ScriptEngine scriptEngine;
/** Maximum CPU time in milliseconds. */
protected long maxCPUTime = 0L;
/** Maximum memory of executor thread used. */
protected long maxMemory = 0L;
protected ExecutorService executor;
protected boolean allowPrintFunctions = false;
protected boolean allowReadFunctions = false;
protected boolean allowLoadFunctions = false;
protected boolean allowExitFunctions = false;
protected boolean allowGlobalsObjects = false;
protected JsEvaluator evaluator;
protected JsSanitizer sanitizer;
protected AtomicBoolean engineAsserted;
protected Invocable lazyInvocable;
/** The size of the LRU cache of prepared statements. */
protected int maxPreparedStatements;
protected SecuredJsCache suppliedCache;
protected Bindings cached;
public NashornSandboxImpl() {
this(new String[0]);
}
public NashornSandboxImpl(String... params) {
this(null, params);
}
public NashornSandboxImpl(ScriptEngine engine, String... params) {
for (String param : params) {
if (param.equals("--no-java")) {
throw new IllegalArgumentException(
"The engine parameter --no-java is not supported. Using it would interfere with the injected code to test for infinite loops.");
}
}
sandboxClassFilter = createSandboxClassFilter();
this.scriptEngine = engine == null
? createNashornScriptEngineFactory(params)
: engine;
this.maxPreparedStatements = 0;
this.allow(InterruptTest.class);
this.engineAsserted = new AtomicBoolean(false);
}
private SandboxClassFilter createSandboxClassFilter() {
return NashornDetection.createSandboxClassFilter();
}
public ScriptEngine createNashornScriptEngineFactory(String ... params) {
try {
Object nashornScriptEngineFactory = NashornDetection.getNashornScriptEngineFactory();
Class<?> classFilterClass = NashornDetection.getClassFilterClass();
Method getScriptEngine = nashornScriptEngineFactory.getClass().getDeclaredMethod("getScriptEngine", String[].class, ClassLoader.class, classFilterClass);
return (ScriptEngine) getScriptEngine.invoke(nashornScriptEngineFactory, params, this.getClass().getClassLoader(),
classFilterClass.cast(this.sandboxClassFilter));
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
throw new RuntimeException(e);
}
}
private synchronized void assertScriptEngine() {
try {
if (!engineAsserted.get()) {
produceSecureBindings(scriptEngine.getContext());
} else if (!engineBindingUnchanged()) {
resetEngineBindings(scriptEngine.getContext());
}
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
// This will detect whether the current engine bindings match the cached
// protected bindings
private boolean engineBindingUnchanged() {
final Bindings current = scriptEngine.getBindings(ScriptContext.ENGINE_SCOPE);
for (Map.Entry<String, Object> e : cached.entrySet()) {
if (!current.containsKey(e.getKey()) || !Objects.equals(current.get(e.getKey()), e.getValue())) {
return false;
}
}
return true;
}
private void produceSecureBindings(ScriptContext context) {
try {
final StringBuilder sb = new StringBuilder();
cached = context.getBindings(ScriptContext.ENGINE_SCOPE);
sanitizeBindings(cached);
if (!allowExitFunctions) {
sb.append("var quit=function(){};var exit=function(){};");
}
if (!allowPrintFunctions) {
sb.append("var print=function(){};var echo = function(){};");
}
if (!allowReadFunctions) {
sb.append("var readFully=function(){};").append("var readLine=function(){};");
}
if (!allowLoadFunctions) {
sb.append("var load=function(){};var loadWithNewGlobal=function(){};");
}
if (!allowGlobalsObjects) {
// Max 22nd of Feb 2018: I don't think these are strictly necessary since they
// are only available in scripting mode
sb.append("var $ARG=null;var $ENV=null;var $EXEC=null;");
sb.append("var $OPTIONS=null;var $OUT=null;var $ERR=null;var $EXIT=null;");
}
scriptEngine.eval(sb.toString(), context);
resetEngineBindings(context);
engineAsserted.set(true);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
protected void resetEngineBindings(ScriptContext context) {
final Bindings bindings = createBindings();
sanitizeBindings(bindings);
bindings.putAll(cached);
context.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
}
protected void sanitizeBindings(Bindings bindings) {
if (!allowExitFunctions) {
bindings.remove("quit");
bindings.remove("exit");
}
if (!allowPrintFunctions) {
bindings.remove("print");
bindings.remove("echo");
}
if (!allowReadFunctions) {
bindings.remove("readFully");
bindings.remove("readLine");
}
if (!allowLoadFunctions) {
bindings.remove("load");
bindings.remove("loadWithNewGlobal");
}
}
@Override
public SandboxScriptContext createScriptContext() {
ScriptContext context = new SimpleScriptContext();
produceSecureBindings(context);
return new SandboxScriptContext() {
@Override
public ScriptContext getContext() {
return context;
}
};
}
@Override
public Object eval(final String js) throws ScriptCPUAbuseException, ScriptException {
return eval(js, null, null);
}
@Override
public Object eval(String js, Bindings bindings) throws ScriptCPUAbuseException, ScriptException {
return eval(js, null, bindings);
}
@Override
public Object eval(String js, SandboxScriptContext scriptContext) throws ScriptCPUAbuseException, ScriptException {
return eval(js, scriptContext, null);
}
@Override
public Object eval(final String js, final SandboxScriptContext scriptContext, final Bindings bindings)
throws ScriptCPUAbuseException, ScriptException {
assertScriptEngine();
final JsSanitizer sanitizer = getSanitizer();
// see https://github.com/javadelight/delight-nashorn-sandbox/issues/73
final String blockAccessToEngine = "Object.defineProperty(this, 'engine', {});"
+ "Object.defineProperty(this, 'context', {});delete this.__noSuchProperty__;";
final String securedJs;
if (scriptContext == null) {
securedJs = blockAccessToEngine + sanitizer.secureJs(js);
} else {
// Unfortunately, blocking access to the engine property interferes with setting
// a script context needs further investigation
securedJs = sanitizer.secureJs(js);
}
final Bindings securedBindings = secureBindings(bindings);
EvaluateOperation op;
if (scriptContext != null) {
op = new EvaluateOperation(securedJs, scriptContext.getContext(), securedBindings);
} else {
op = new EvaluateOperation(securedJs, null, securedBindings);
}
return executeSandboxedOperation(op);
}
protected Bindings secureBindings(Bindings bindings) {
if (bindings == null)
return null;
bindings.putAll(cached);
return bindings;
}
protected Object executeSandboxedOperation(ScriptEngineOperation op)
throws ScriptCPUAbuseException, ScriptException {
assertScriptEngine();
try {
if (maxCPUTime == 0 && maxMemory == 0) {
return op.executeScriptEngineOperation(scriptEngine);
}
checkExecutorPresence();
final JsEvaluator evaluator = getEvaluator(op);
executor.execute(evaluator);
evaluator.runMonitor();
if (evaluator.isCPULimitExceeded()) {
throw new ScriptCPUAbuseException(
"Script used more than the allowed [" + maxCPUTime + " ms] of CPU time.",
evaluator.isScriptKilled(), evaluator.getException());
} else if (evaluator.isMemoryLimitExceeded()) {
throw new ScriptMemoryAbuseException(
"Script used more than the allowed [" + maxMemory + " B] of memory.",
evaluator.isScriptKilled(), evaluator.getException());
}
if (evaluator.getException() != null) {
throw evaluator.getException();
}
return evaluator.getResult();
} catch (RuntimeException | ScriptException e) {
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
private JsEvaluator getEvaluator(ScriptEngineOperation op) {
return new JsEvaluator(scriptEngine, maxCPUTime, maxMemory, op);
}
private void checkExecutorPresence() {
if (executor == null) {
throw new IllegalStateException("When a CPU time or memory limit is set, an executor "
+ "needs to be provided by calling .setExecutor(...)");
}
}
@Override
public void setMaxCPUTime(final long limit) {
maxCPUTime = limit;
}
@Override
public void setMaxMemory(final long limit) {
maxMemory = limit;
}
protected JsSanitizer getSanitizer() {
if (sanitizer == null) {
if (suppliedCache == null) {
sanitizer = new JsSanitizer(scriptEngine, maxPreparedStatements);
} else {
sanitizer = new JsSanitizer(scriptEngine, suppliedCache);
}
}
return sanitizer;
}
@Override
public void allow(final Class<?> clazz) {
sandboxClassFilter.add(clazz);
}
@Override
public void disallow(final Class<?> clazz) {
sandboxClassFilter.remove(clazz);
}
@Override
public boolean isAllowed(final Class<?> clazz) {
return sandboxClassFilter.contains(clazz);
}
@Override
public void disallowAllClasses() {
sandboxClassFilter.clear();
// this class must always be allowed, see issue 54
// https://github.com/javadelight/delight-nashorn-sandbox/issues/54
allow(InterruptTest.class);
}
@Override
public void inject(final String variableName, final Object object) {
if (object != null && !sandboxClassFilter.contains(object.getClass())) {
allow(object.getClass());
}
scriptEngine.put(variableName, object);
}
@Override
public void setExecutor(final ExecutorService executor) {
this.executor = executor;
}
@Override
public ExecutorService getExecutor() {
return executor;
}
@Override
public Object get(final String variableName) {
return scriptEngine.get(variableName);
}
@Override
public void allowPrintFunctions(final boolean v) {
if (engineAsserted.get()) {
throw new IllegalStateException("Please set this property before calling eval.");
}
allowPrintFunctions = v;
}
@Override
public void allowReadFunctions(final boolean v) {
if (engineAsserted.get()) {
throw new IllegalStateException("Please set this property before calling eval.");
}
allowReadFunctions = v;
}
@Override
public void allowLoadFunctions(final boolean v) {
if (engineAsserted.get()) {
throw new IllegalStateException("Please set this property before calling eval.");
}
allowLoadFunctions = v;
}
@Override
public void allowExitFunctions(final boolean v) {
if (engineAsserted.get()) {
throw new IllegalStateException("Please set this property before calling eval.");
}
allowExitFunctions = v;
}
@Override
public void allowGlobalsObjects(final boolean v) {
if (engineAsserted.get()) {
throw new IllegalStateException("Please set this property before calling eval.");
}
allowGlobalsObjects = v;
}
@Override
public void setWriter(final Writer writer) {
scriptEngine.getContext().setWriter(writer);
}
@Override
public void setMaxPreparedStatements(final int max) {
if (maxPreparedStatements != max) {
sanitizer = null;
}
maxPreparedStatements = max;
}
@Override
public Bindings createBindings() {
return scriptEngine.createBindings();
}
@Override
public Invocable getSandboxedInvocable() {
if (maxMemory == 0 && maxCPUTime == 0) {
return (Invocable) scriptEngine;
}
return getLazySandboxedInvocable();
}
private Invocable getLazySandboxedInvocable() {
if (lazyInvocable == null) {
Invocable sandboxInvocable = new Invocable() {
@Override
public Object invokeMethod(Object thiz, String name, Object... args)
throws ScriptException, NoSuchMethodException {
InvokeOperation op = new InvokeOperation(thiz, name, args);
try {
return executeSandboxedOperation(op);
} catch (ScriptException e) {
throw e;
} catch (Exception e) {
throw new ScriptException(e);
}
}
@Override
public Object invokeFunction(String name, Object... args)
throws ScriptException, NoSuchMethodException {
InvokeOperation op = new InvokeOperation(null, name, args);
try {
return executeSandboxedOperation(op);
} catch (ScriptException e) {
throw e;
} catch (Exception e) {
throw new ScriptException(e);
}
}
@Override
public <T> T getInterface(Object thiz, Class<T> clasz) {
// TODO add proxy wrapper for proper sandboxing
throw new IllegalStateException("Not yet implemented");
}
@Override
public <T> T getInterface(Class<T> clasz) {
// TODO add proxy wrapper for proper sandboxing
throw new IllegalStateException("Not yet implemented");
}
};
lazyInvocable = sandboxInvocable;
}
return lazyInvocable;
}
@Override
public void setScriptCache(SecuredJsCache cache) {
this.suppliedCache = cache;
}
@Override
public CompiledScript compile(final String js) throws ScriptException
{
assertScriptEngine();
final JsSanitizer sanitizer = getSanitizer();
final String securedJs = sanitizer.secureJs(js);
Compilable compilingEngine = (Compilable) this.scriptEngine;
CompiledScript compiledScript = compilingEngine.compile(securedJs);
return compiledScript;
}
@Override
public Object eval(CompiledScript compiledScript) throws ScriptCPUAbuseException, ScriptException
{
return eval(compiledScript, null, null);
}
@Override
public Object eval(CompiledScript compiledScript, Bindings bindings) throws ScriptCPUAbuseException, ScriptException
{
return eval(compiledScript, null, bindings);
}
@Override
public Object eval(CompiledScript compiledScript, ScriptContext scriptContext) throws ScriptCPUAbuseException, ScriptException
{
return eval(compiledScript, scriptContext, null);
}
@Override
public Object eval(CompiledScript compiledScript, ScriptContext scriptContext, Bindings bindings) throws ScriptCPUAbuseException, ScriptException
{
assertScriptEngine();
final Bindings securedBindings = secureBindings(bindings);
EvaluateCompiledOperation op = new EvaluateCompiledOperation(compiledScript, scriptContext, securedBindings);
return executeSandboxedOperation(op);
}
}