use of org.kohsuke.groovy.sandbox.SandboxTransformer in project hale by halestudio.
the class GroovySandboxTest method setUp.
/**
* Sets up the Groovy shell and interceptor.
*/
@Before
public void setUp() {
CompilerConfiguration cc = new CompilerConfiguration();
// enable invoke dynamic support (simiar to in when scripts are created)
cc.getOptimizationOptions().put(CompilerConfiguration.INVOKEDYNAMIC, true);
cc.addCompilationCustomizers(new SandboxTransformer());
shell = new GroovyShell(cc);
interceptor = new RestrictiveGroovyInterceptor(Collections.<Class<?>>emptySet(), Collections.<Class<?>>emptySet(), Collections.<AllowedPrefix>emptyList());
}
use of org.kohsuke.groovy.sandbox.SandboxTransformer in project hale by halestudio.
the class DefaultGroovyService method createShell.
@Override
public GroovyShell createShell(Binding binding) {
// TODO use a specific classloader?
CompilerConfiguration cc = new CompilerConfiguration();
// enable invoke dynamic support
cc.getOptimizationOptions().put(CompilerConfiguration.INVOKEDYNAMIC, true);
// add pre-defined imports
ImportCustomizer importCustomizer = new ImportCustomizer();
// add extension-defined imports
configureImportsFromExtensions(importCustomizer);
cc.addCompilationCustomizers(importCustomizer);
/*
* Disable handling Groovy Grape annotations.
*
* This mainly serves the purpose to allow external Groovy snippets to
* use Grapes, but have them disabled when imported into hale.
*
* If at some point we support Grapes within hale studio, we will want
* to change this behavior. Then we will need to think about how we can
* deal with conflicts on the classpath.
*/
if (cc.getDisabledGlobalASTTransformations() == null) {
cc.setDisabledGlobalASTTransformations(new HashSet<String>());
}
cc.getDisabledGlobalASTTransformations().add("groovy.grape.GrabAnnotationTransformation");
if (isRestrictionActive()) {
// configure restriction
cc.addCompilationCustomizers(new SandboxTransformer());
cc.setScriptBaseClass(SecureScript.class.getName());
}
if (binding != null)
return new GroovyShell(binding, cc);
else
return new GroovyShell(cc);
}
use of org.kohsuke.groovy.sandbox.SandboxTransformer in project CzechIdMng by bcvsolutions.
the class DefaultGroovyScriptService method buildScript.
private Script buildScript(String source) {
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.setVerbose(false);
compilerConfiguration.setDebug(false);
compilerConfiguration.addCompilationCustomizers(new SandboxTransformer());
//
GroovyShell shell = new GroovyShell(compilerConfiguration);
return shell.parse(source);
}
use of org.kohsuke.groovy.sandbox.SandboxTransformer in project ontrack by nemerosa.
the class ExpressionEngineImpl method resolve.
public String resolve(final String expression, Map<String, ?> parameters) {
SandboxTransformer sandboxTransformer = new SandboxTransformer();
SecureASTCustomizer secure = new SecureASTCustomizer();
secure.setClosuresAllowed(false);
secure.setMethodDefinitionAllowed(false);
CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
compilerConfiguration.addCompilationCustomizers(sandboxTransformer, secure);
Binding binding = new Binding(parameters);
GroovyShell shell = new GroovyShell(binding, compilerConfiguration);
// Sandbox registration (thread level)
GroovyValueFilter sandboxFilter = new GroovyValueFilter() {
@Override
public Object filter(Object o) {
if (o == null || o instanceof String || o instanceof GString || o.getClass().getName().equals("Script1")) {
return o;
} else if (o instanceof Class) {
throw new ExpressionCompilationException(expression, String.format("%n- %s class cannot be accessed.", ((Class) o).getName()));
} else {
throw new ExpressionCompilationException(expression, String.format("%n- %s class cannot be accessed.", o.getClass().getName()));
}
}
};
try {
sandboxFilter.register();
Object result = shell.evaluate(expression);
if (result == null) {
return null;
} else if (!(result instanceof String)) {
throw new ExpressionNotStringException(expression);
} else {
return (String) result;
}
} catch (MissingPropertyException e) {
throw new ExpressionCompilationException(expression, "No such property: " + e.getProperty());
} catch (MultipleCompilationErrorsException e) {
StringWriter s = new StringWriter();
PrintWriter p = new PrintWriter(s);
@SuppressWarnings("unchecked") List<Message> errors = e.getErrorCollector().getErrors();
errors.forEach((Message message) -> writeErrorMessage(p, message));
throw new ExpressionCompilationException(expression, s.toString());
} finally {
sandboxFilter.unregister();
}
}
Aggregations