Search in sources :

Example 1 with MangoScript

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;
}
Also used : ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition) Bindings(javax.script.Bindings) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) LoadFileStorePermission(com.infiniteautomation.mango.spring.script.permissions.LoadFileStorePermission) FileStoreService(com.infiniteautomation.mango.spring.service.FileStoreService) Autowired(org.springframework.beans.factory.annotation.Autowired) MangoScript(com.infiniteautomation.mango.spring.script.MangoScript) LoadWebPermission(com.infiniteautomation.mango.spring.script.permissions.LoadWebPermission) ScriptEngineFactory(javax.script.ScriptEngineFactory) Function(java.util.function.Function) NashornPermission(com.infiniteautomation.mango.spring.script.permissions.NashornPermission) ScriptContext(javax.script.ScriptContext) ScriptEngine(javax.script.ScriptEngine) LoadOtherPermission(com.infiniteautomation.mango.spring.script.permissions.LoadOtherPermission) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) MalformedURLException(java.net.MalformedURLException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) URL(java.net.URL)

Example 2 with MangoScript

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;
}
Also used : ScriptEngineDefinition(com.serotonin.m2m2.module.ScriptEngineDefinition) Bindings(javax.script.Bindings) MalformedURLException(java.net.MalformedURLException) URL(java.net.URL) LoadFileStorePermission(com.infiniteautomation.mango.spring.script.permissions.LoadFileStorePermission) FileStoreService(com.infiniteautomation.mango.spring.service.FileStoreService) Autowired(org.springframework.beans.factory.annotation.Autowired) MangoScript(com.infiniteautomation.mango.spring.script.MangoScript) LoadWebPermission(com.infiniteautomation.mango.spring.script.permissions.LoadWebPermission) ScriptEngineFactory(javax.script.ScriptEngineFactory) Function(java.util.function.Function) NashornPermission(com.infiniteautomation.mango.spring.script.permissions.NashornPermission) ScriptContext(javax.script.ScriptContext) ScriptEngine(javax.script.ScriptEngine) LoadOtherPermission(com.infiniteautomation.mango.spring.script.permissions.LoadOtherPermission) MangoPermission(com.infiniteautomation.mango.permission.MangoPermission) ScriptException(javax.script.ScriptException) ScriptException(javax.script.ScriptException) MalformedURLException(java.net.MalformedURLException) Bindings(javax.script.Bindings) ScriptEngine(javax.script.ScriptEngine) URL(java.net.URL)

Aggregations

MangoPermission (com.infiniteautomation.mango.permission.MangoPermission)2 MangoScript (com.infiniteautomation.mango.spring.script.MangoScript)2 LoadFileStorePermission (com.infiniteautomation.mango.spring.script.permissions.LoadFileStorePermission)2 LoadOtherPermission (com.infiniteautomation.mango.spring.script.permissions.LoadOtherPermission)2 LoadWebPermission (com.infiniteautomation.mango.spring.script.permissions.LoadWebPermission)2 NashornPermission (com.infiniteautomation.mango.spring.script.permissions.NashornPermission)2 FileStoreService (com.infiniteautomation.mango.spring.service.FileStoreService)2 ScriptEngineDefinition (com.serotonin.m2m2.module.ScriptEngineDefinition)2 MalformedURLException (java.net.MalformedURLException)2 URL (java.net.URL)2 Function (java.util.function.Function)2 Bindings (javax.script.Bindings)2 ScriptContext (javax.script.ScriptContext)2 ScriptEngine (javax.script.ScriptEngine)2 ScriptEngineFactory (javax.script.ScriptEngineFactory)2 ScriptException (javax.script.ScriptException)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2