use of com.infiniteautomation.mango.spring.script.MangoScript in project ma-core-public by MangoAutomation.
the class NashornScriptEngineDefinition method createEngine.
@Override
public ScriptEngine createEngine(ScriptEngineFactory engineFactory, MangoScript script) {
ScriptEngine engine = createScriptEngine(engineFactory, permissionService.hasAdminRole(script) ? null : c -> false);
Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
Object originalLoad = engineBindings.get("load");
if (!permissionService.hasAdminRole(script)) {
// remove exit and quit functions from bindings
for (String key : KEYS_TO_REMOVE) {
engineBindings.remove(key);
}
// make the engine and context inaccessible
try {
engine.eval("Object.defineProperty(this, 'engine', {}); Object.defineProperty(this, 'context', {});");
} catch (ScriptException e) {
throw new RuntimeException(e);
}
}
Function<Object, Object> replacementLoad = source -> {
URL url = null;
if (source instanceof URL) {
url = (URL) source;
} else if (source instanceof String && ((String) source).indexOf(':') >= 0) {
try {
url = new URL((String) source);
} catch (MalformedURLException e) {
// ignore
}
}
if (url != null) {
String protocol = url.getProtocol();
boolean isFileStore = "filestore".equals(protocol);
boolean isWeb = "http".equals(protocol) || "https".equals(protocol);
if (isFileStore && permissionService.hasPermission(script, loadFileStorePermission.getPermission()) || isWeb && permissionService.hasPermission(script, loadWebPermission.getPermission())) {
return callFunction(originalLoad, null, source);
}
}
permissionService.ensurePermission(script, loadOtherPermission.getPermission());
return callFunction(originalLoad, null, source);
};
engineBindings.put("load", replacementLoad);
return engine;
}
use of com.infiniteautomation.mango.spring.script.MangoScript in project ma-core-public by infiniteautomation.
the class NashornScriptEngineDefinition method createEngine.
@Override
public ScriptEngine createEngine(ScriptEngineFactory engineFactory, MangoScript script) {
ScriptEngine engine = createScriptEngine(engineFactory, permissionService.hasAdminRole(script) ? null : c -> false);
Bindings engineBindings = engine.getBindings(ScriptContext.ENGINE_SCOPE);
Object originalLoad = engineBindings.get("load");
if (!permissionService.hasAdminRole(script)) {
// remove exit and quit functions from bindings
for (String key : KEYS_TO_REMOVE) {
engineBindings.remove(key);
}
// make the engine and context inaccessible
try {
engine.eval("Object.defineProperty(this, 'engine', {}); Object.defineProperty(this, 'context', {});");
} catch (ScriptException e) {
throw new RuntimeException(e);
}
}
Function<Object, Object> replacementLoad = source -> {
URL url = null;
if (source instanceof URL) {
url = (URL) source;
} else if (source instanceof String && ((String) source).indexOf(':') >= 0) {
try {
url = new URL((String) source);
} catch (MalformedURLException e) {
// ignore
}
}
if (url != null) {
String protocol = url.getProtocol();
boolean isFileStore = "filestore".equals(protocol);
boolean isWeb = "http".equals(protocol) || "https".equals(protocol);
if (isFileStore && permissionService.hasPermission(script, loadFileStorePermission.getPermission()) || isWeb && permissionService.hasPermission(script, loadWebPermission.getPermission())) {
return callFunction(originalLoad, null, source);
}
}
permissionService.ensurePermission(script, loadOtherPermission.getPermission());
return callFunction(originalLoad, null, source);
};
engineBindings.put("load", replacementLoad);
return engine;
}
Aggregations