You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by oe...@apache.org on 2026/04/08 13:53:34 UTC
(streampipes) 01/02: Add groovy sandbox policy
This is an automated email from the ASF dual-hosted git repository.
oehler pushed a commit to branch sandbox-execution-of-untrusted-groovy-code
in repository https://gitbox.apache.org/repos/asf/streampipes.git
commit 7109eaa52137024d73748221cb4c47ee08c9a6c1
Author: Sven Oehler <oe...@web.de>
AuthorDate: Wed Apr 8 15:52:51 2026 +0200
Add groovy sandbox policy
---
.../transformer/groovy/GroovyScriptEngine.java | 12 +-
.../sandbox/RestrictedGroovyClassLoader.java | 59 +++++++
.../transformer/groovy/sandbox/SandboxPolicy.java | 187 +++++++++++++++++++++
.../transformer/groovy/sandbox/ScriptSandbox.java | 51 ++++++
.../ast/AstChecksCompilationCustomizer.java | 39 +++++
.../groovy/sandbox/ast/AstChecksVisitor.java | 109 ++++++++++++
.../sandbox/ast/SecureCustomizerFactory.java | 59 +++++++
.../error/SandboxCompilationMessageResolver.java | 112 ++++++++++++
.../SandboxViolationClassNotFoundException.java | 26 +++
.../sandbox/error/SandboxViolationException.java | 26 +++
10 files changed, 674 insertions(+), 6 deletions(-)
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/GroovyScriptEngine.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/GroovyScriptEngine.java
index 6b20789f7c..1baf7f59d4 100644
--- a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/GroovyScriptEngine.java
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/GroovyScriptEngine.java
@@ -25,12 +25,12 @@ import org.apache.streampipes.connect.transformer.api.TransformationEngine;
import org.apache.streampipes.connect.transformer.api.exception.ScriptCompilationException;
import org.apache.streampipes.connect.transformer.api.exception.ScriptExecutionException;
import org.apache.streampipes.connect.transformer.api.utils.TransformationEngineConversionUtils;
+import org.apache.streampipes.connect.transformer.groovy.sandbox.ScriptSandbox;
import org.apache.streampipes.model.connect.ScriptMetadata;
import groovy.lang.Binding;
import groovy.lang.GroovyShell;
import groovy.lang.Script;
-import org.codehaus.groovy.control.CompilationFailedException;
import org.codehaus.groovy.runtime.InvokerHelper;
import java.util.Map;
@@ -41,19 +41,19 @@ public class GroovyScriptEngine implements TransformationEngine {
public ScriptMetadata metadata() {
return new ScriptMetadata(
"groovy",
- "Groovy",
- "out.collect(input)"
+ "Groovy",
+ "out.collect(input)"
);
}
@Override
public ScriptTransformer compile(String code) throws ScriptCompilationException {
- GroovyShell shell = new GroovyShell();
+ GroovyShell shell = ScriptSandbox.createShell(GroovyScriptEngine.class.getClassLoader());
Script script;
try {
script = shell.parse(code);
- } catch (CompilationFailedException e) {
- throw new ScriptCompilationException("Failed to compile Groovy template", e);
+ } catch (Exception e) {
+ throw new ScriptCompilationException(ScriptSandbox.resolveCompilationErrorMessage(e), e);
}
Class<? extends Script> scriptClass = script.getClass();
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/RestrictedGroovyClassLoader.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/RestrictedGroovyClassLoader.java
new file mode 100644
index 0000000000..adb3e28fca
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/RestrictedGroovyClassLoader.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox;
+
+import org.apache.streampipes.connect.transformer.groovy.sandbox.error.SandboxViolationClassNotFoundException;
+
+import groovy.lang.GroovyClassLoader;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+public final class RestrictedGroovyClassLoader extends GroovyClassLoader {
+
+ public RestrictedGroovyClassLoader(ClassLoader parent, CompilerConfiguration configuration) {
+ super(parent, configuration);
+ }
+
+ @Override
+ public Class<?> loadClass(String name) throws ClassNotFoundException {
+ rejectForbiddenClass(name);
+ return super.loadClass(name);
+ }
+
+ @Override
+ protected Class<?> loadClass(String name, boolean resolve)
+ throws ClassNotFoundException, CompilationFailedException {
+ rejectForbiddenClass(name);
+ return super.loadClass(name, resolve);
+ }
+
+ @Override
+ public Class loadClass(String name, boolean lookupScriptFiles, boolean preferClassOverScript, boolean resolve)
+ throws ClassNotFoundException, CompilationFailedException {
+ rejectForbiddenClass(name);
+ return super.loadClass(name, lookupScriptFiles, preferClassOverScript, resolve);
+ }
+
+ private void rejectForbiddenClass(String className) throws ClassNotFoundException {
+ if (SandboxPolicy.blocksType(className)) {
+ throw new SandboxViolationClassNotFoundException(
+ SandboxPolicy.VIOLATION_MESSAGE + ": access to class '" + className + "' is forbidden");
+ }
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/SandboxPolicy.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/SandboxPolicy.java
new file mode 100644
index 0000000000..18b41e3b51
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/SandboxPolicy.java
@@ -0,0 +1,187 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox;
+
+import groovy.lang.Binding;
+
+import java.util.List;
+import java.util.Set;
+
+public final class SandboxPolicy {
+
+ public static final String VIOLATION_MESSAGE = "Groovy script violates sandbox restrictions";
+
+ private static final List<String> DISALLOWED_IMPORTS = List.of(
+ "java.io.File",
+ "java.io.FileInputStream",
+ "java.io.FileOutputStream",
+ "java.io.RandomAccessFile",
+ "java.net.InetAddress",
+ "java.net.ServerSocket",
+ "java.net.Socket",
+ "java.net.URI",
+ "java.net.URL",
+ "java.lang.Class",
+ "java.lang.ClassLoader",
+ "java.lang.Process",
+ "java.lang.ProcessBuilder",
+ "java.lang.Runtime",
+ "java.lang.System",
+ "java.lang.Thread",
+ "java.nio.channels.FileChannel",
+ "java.nio.file.Files",
+ "java.nio.file.Path",
+ "java.nio.file.Paths",
+ "groovy.lang.GroovyClassLoader",
+ "groovy.lang.GroovyShell",
+ "groovy.transform.ASTTest",
+ "groovy.util.Eval"
+ );
+ private static final List<String> DISALLOWED_STAR_IMPORTS = List.of(
+ "java.io",
+ "java.net",
+ "java.nio",
+ "java.nio.channels",
+ "java.nio.file",
+ "java.lang.reflect",
+ "groovy.io"
+ );
+ private static final List<String> DISALLOWED_STATIC_IMPORTS = List.of(
+ "java.lang.System",
+ "java.lang.Runtime",
+ "java.nio.file.Files",
+ "java.nio.file.Paths"
+ );
+ private static final Set<String> DISABLED_GLOBAL_AST_TRANSFORMATIONS = Set.of(
+ "groovy.grape.GrabAnnotationTransformation",
+ "org.codehaus.groovy.transform.ASTTestTransformation",
+ "org.codehaus.groovy.transform.AnnotationCollectorTransform"
+ );
+ private static final Set<String> DISALLOWED_EXACT_TYPES = Set.of(
+ "java.io.File",
+ "java.io.FileInputStream",
+ "java.io.FileOutputStream",
+ "java.io.RandomAccessFile",
+ "java.net.InetAddress",
+ "java.net.ServerSocket",
+ "java.net.Socket",
+ "java.net.URI",
+ "java.net.URL",
+ "java.lang.Class",
+ "java.lang.ClassLoader",
+ "java.lang.Process",
+ "java.lang.ProcessBuilder",
+ "java.lang.Runtime",
+ "java.lang.System",
+ "java.lang.Thread",
+ "java.nio.channels.FileChannel",
+ "java.nio.file.Files",
+ "java.nio.file.Path",
+ "java.nio.file.Paths",
+ "groovy.lang.GroovyClassLoader",
+ "groovy.lang.GroovyShell",
+ "groovy.transform.ASTTest",
+ "groovy.util.Eval"
+ );
+ private static final Set<String> DISALLOWED_SIMPLE_TYPES = Set.of(
+ "File",
+ "FileInputStream",
+ "FileOutputStream",
+ "RandomAccessFile",
+ "InetAddress",
+ "ServerSocket",
+ "Socket",
+ "URI",
+ "URL",
+ "Class",
+ "ClassLoader",
+ "Process",
+ "ProcessBuilder",
+ "Runtime",
+ "System",
+ "Thread",
+ "FileChannel",
+ "Files",
+ "Path",
+ "Paths",
+ "GroovyClassLoader",
+ "GroovyShell",
+ "ASTTest",
+ "Eval"
+ );
+ private static final List<String> DISALLOWED_TYPE_PREFIXES = List.of(
+ "java.io.",
+ "java.net.",
+ "java.nio.",
+ "java.nio.channels.",
+ "java.nio.file.",
+ "java.lang.reflect.",
+ "groovy.io."
+ );
+ private static final Set<String> DISALLOWED_PROPERTY_NAMES = Set.of(
+ "class",
+ "classLoader",
+ "metaClass"
+ );
+
+ private SandboxPolicy() {
+ }
+
+ public static Binding createBinding() {
+ return new Binding();
+ }
+
+ public static List<String> disallowedImports() {
+ return DISALLOWED_IMPORTS;
+ }
+
+ public static List<String> disallowedStarImports() {
+ return DISALLOWED_STAR_IMPORTS;
+ }
+
+ public static List<String> disallowedStaticImports() {
+ return DISALLOWED_STATIC_IMPORTS;
+ }
+
+ public static Set<String> disabledGlobalAstTransformations() {
+ return DISABLED_GLOBAL_AST_TRANSFORMATIONS;
+ }
+
+ public static Set<String> disallowedPropertyNames() {
+ return DISALLOWED_PROPERTY_NAMES;
+ }
+
+ public static boolean blocksType(String typeName) {
+ if (typeName == null || typeName.isBlank()) {
+ return false;
+ }
+
+ if (DISALLOWED_EXACT_TYPES.contains(typeName) || DISALLOWED_SIMPLE_TYPES.contains(typeName)) {
+ return true;
+ }
+
+ for (String prefix : DISALLOWED_TYPE_PREFIXES) {
+ if (typeName.startsWith(prefix)) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ScriptSandbox.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ScriptSandbox.java
new file mode 100644
index 0000000000..2f26ec4365
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ScriptSandbox.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox;
+
+import org.apache.streampipes.connect.transformer.groovy.sandbox.ast.AstChecksCompilationCustomizer;
+import org.apache.streampipes.connect.transformer.groovy.sandbox.ast.SecureCustomizerFactory;
+import org.apache.streampipes.connect.transformer.groovy.sandbox.error.SandboxCompilationMessageResolver;
+
+import groovy.lang.GroovyShell;
+import org.codehaus.groovy.control.CompilerConfiguration;
+
+public final class ScriptSandbox {
+
+ private ScriptSandbox() {
+ }
+
+ public static GroovyShell createShell(ClassLoader parent) {
+ CompilerConfiguration configuration = new CompilerConfiguration();
+ configuration.setDisabledGlobalASTTransformations(SandboxPolicy.disabledGlobalAstTransformations());
+ configuration.addCompilationCustomizers(
+ SecureCustomizerFactory.create(),
+ new AstChecksCompilationCustomizer()
+ );
+
+ return new GroovyShell(
+ new RestrictedGroovyClassLoader(parent, configuration),
+ SandboxPolicy.createBinding(),
+ configuration
+ );
+ }
+
+ public static String resolveCompilationErrorMessage(Exception error) {
+ return SandboxCompilationMessageResolver.resolve(error);
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/AstChecksCompilationCustomizer.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/AstChecksCompilationCustomizer.java
new file mode 100644
index 0000000000..05a6a7751d
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/AstChecksCompilationCustomizer.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox.ast;
+
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.classgen.GeneratorContext;
+import org.codehaus.groovy.control.CompilationFailedException;
+import org.codehaus.groovy.control.CompilePhase;
+import org.codehaus.groovy.control.SourceUnit;
+import org.codehaus.groovy.control.customizers.CompilationCustomizer;
+
+public final class AstChecksCompilationCustomizer extends CompilationCustomizer {
+
+ public AstChecksCompilationCustomizer() {
+ super(CompilePhase.SEMANTIC_ANALYSIS);
+ }
+
+ @Override
+ public void call(SourceUnit source, GeneratorContext context, ClassNode classNode)
+ throws CompilationFailedException {
+ new AstChecksVisitor(source).visitClass(classNode);
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/AstChecksVisitor.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/AstChecksVisitor.java
new file mode 100644
index 0000000000..a9313467b8
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/AstChecksVisitor.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox.ast;
+
+import org.apache.streampipes.connect.transformer.groovy.sandbox.error.SandboxViolationException;
+import org.apache.streampipes.connect.transformer.groovy.sandbox.SandboxPolicy;
+
+import org.codehaus.groovy.ast.ClassCodeVisitorSupport;
+import org.codehaus.groovy.ast.ClassNode;
+import org.codehaus.groovy.ast.expr.ClassExpression;
+import org.codehaus.groovy.ast.expr.ConstructorCallExpression;
+import org.codehaus.groovy.ast.expr.Expression;
+import org.codehaus.groovy.ast.expr.MethodCallExpression;
+import org.codehaus.groovy.ast.expr.PropertyExpression;
+import org.codehaus.groovy.ast.expr.StaticMethodCallExpression;
+import org.codehaus.groovy.ast.expr.VariableExpression;
+import org.codehaus.groovy.control.SourceUnit;
+
+public final class AstChecksVisitor extends ClassCodeVisitorSupport {
+
+ private final SourceUnit sourceUnit;
+
+ public AstChecksVisitor(SourceUnit sourceUnit) {
+ this.sourceUnit = sourceUnit;
+ }
+
+ @Override
+ protected SourceUnit getSourceUnit() {
+ return sourceUnit;
+ }
+
+ @Override
+ public void visitConstructorCallExpression(ConstructorCallExpression call) {
+ rejectForbiddenType(call.getType(), "constructor access");
+ super.visitConstructorCallExpression(call);
+ }
+
+ @Override
+ public void visitClassExpression(ClassExpression expression) {
+ rejectForbiddenType(expression.getType(), "class access");
+ super.visitClassExpression(expression);
+ }
+
+ @Override
+ public void visitMethodCallExpression(MethodCallExpression call) {
+ rejectForbiddenExpression(call.getObjectExpression(), "method access");
+ super.visitMethodCallExpression(call);
+ }
+
+ @Override
+ public void visitStaticMethodCallExpression(StaticMethodCallExpression call) {
+ rejectForbiddenType(call.getOwnerType(), "static method access");
+ super.visitStaticMethodCallExpression(call);
+ }
+
+ @Override
+ public void visitPropertyExpression(PropertyExpression expression) {
+ String propertyName = expression.getPropertyAsString();
+ if (propertyName != null && SandboxPolicy.disallowedPropertyNames().contains(propertyName)) {
+ throw new SandboxViolationException(
+ SandboxPolicy.VIOLATION_MESSAGE + ": access to property '" + propertyName + "' is forbidden");
+ }
+
+ rejectForbiddenExpression(expression.getObjectExpression(), "property access");
+ super.visitPropertyExpression(expression);
+ }
+
+ private void rejectForbiddenExpression(Expression expression, String operation) {
+ if (expression instanceof ClassExpression classExpression) {
+ rejectForbiddenType(classExpression.getType(), operation);
+ } else if (expression instanceof VariableExpression variableExpression) {
+ if (SandboxPolicy.blocksType(variableExpression.getName())) {
+ throw new SandboxViolationException(
+ SandboxPolicy.VIOLATION_MESSAGE + ": access to class '" + variableExpression.getName() + "' is forbidden");
+ }
+ } else {
+ rejectForbiddenType(expression.getType(), operation);
+ }
+ }
+
+ private void rejectForbiddenType(ClassNode classNode, String operation) {
+ if (classNode != null) {
+ rejectForbiddenType(classNode.getName(), operation);
+ }
+ }
+
+ private void rejectForbiddenType(String typeName, String operation) {
+ if (SandboxPolicy.blocksType(typeName)) {
+ throw new SandboxViolationException(
+ SandboxPolicy.VIOLATION_MESSAGE + ": " + operation + " to '" + typeName + "' is forbidden");
+ }
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/SecureCustomizerFactory.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/SecureCustomizerFactory.java
new file mode 100644
index 0000000000..4447a0ac02
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/ast/SecureCustomizerFactory.java
@@ -0,0 +1,59 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox.ast;
+
+import org.apache.streampipes.connect.transformer.groovy.sandbox.SandboxPolicy;
+
+import org.codehaus.groovy.control.customizers.SecureASTCustomizer;
+
+import java.util.List;
+
+public final class SecureCustomizerFactory {
+
+ private SecureCustomizerFactory() {
+ }
+
+ public static SecureASTCustomizer create() {
+ SecureASTCustomizer customizer = new SecureASTCustomizer();
+ customizer.setClosuresAllowed(true);
+ customizer.setMethodDefinitionAllowed(false);
+ customizer.setPackageAllowed(false);
+ customizer.setIndirectImportCheckEnabled(true);
+ customizer.setDisallowedImports(SandboxPolicy.disallowedImports());
+ customizer.setDisallowedStarImports(SandboxPolicy.disallowedStarImports());
+ customizer.setDisallowedStaticImports(SandboxPolicy.disallowedStaticImports());
+ customizer.setDisallowedStaticStarImports(SandboxPolicy.disallowedStaticImports());
+ customizer.setDisallowedReceiversClasses(List.of(
+ java.io.File.class,
+ java.lang.Class.class,
+ java.lang.ClassLoader.class,
+ java.lang.Process.class,
+ java.lang.ProcessBuilder.class,
+ java.lang.Runtime.class,
+ java.lang.System.class,
+ java.lang.Thread.class,
+ java.net.InetAddress.class,
+ java.net.ServerSocket.class,
+ java.net.Socket.class,
+ java.net.URI.class,
+ java.net.URL.class
+ ));
+ return customizer;
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxCompilationMessageResolver.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxCompilationMessageResolver.java
new file mode 100644
index 0000000000..75de065cef
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxCompilationMessageResolver.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox.error;
+
+import org.apache.streampipes.connect.transformer.groovy.sandbox.SandboxPolicy;
+
+import java.util.Optional;
+
+public final class SandboxCompilationMessageResolver {
+
+ private SandboxCompilationMessageResolver() {
+ }
+
+ public static String resolve(Exception error) {
+ Optional<SandboxViolationException> sandboxViolation = findSandboxViolation(error);
+ if (sandboxViolation.isPresent()) {
+ return sandboxViolation.get().getMessage();
+ }
+
+ return extractViolationMessage(error.getMessage())
+ .or(() -> extractAllowedError(error.getMessage()))
+ .orElse("Failed to compile Groovy template");
+ }
+
+ private static Optional<SandboxViolationException> findSandboxViolation(Throwable error) {
+ Throwable current = error;
+ while (current != null) {
+ if (current instanceof SandboxViolationException sandboxViolation) {
+ return Optional.of(sandboxViolation);
+ } else if (current instanceof SandboxViolationClassNotFoundException sandboxViolation) {
+ return Optional.of(new SandboxViolationException(sandboxViolation.getMessage()));
+ } else if (current instanceof SecurityException securityException) {
+ return toSandboxViolation(securityException.getMessage());
+ }
+ current = current.getCause();
+ }
+
+ return Optional.empty();
+ }
+
+ private static Optional<SandboxViolationException> toSandboxViolation(String message) {
+ if (message == null || message.isBlank()) {
+ return Optional.empty();
+ }
+
+ if (message.contains(SandboxPolicy.VIOLATION_MESSAGE)) {
+ return Optional.of(new SandboxViolationException(message));
+ }
+
+ if (message.contains("is not allowed")) {
+ return Optional.of(new SandboxViolationException(SandboxPolicy.VIOLATION_MESSAGE + ": " + message));
+ }
+
+ return Optional.empty();
+ }
+
+ private static Optional<String> extractViolationMessage(String message) {
+ if (message == null) {
+ return Optional.empty();
+ }
+
+ int messageIndex = message.indexOf(SandboxPolicy.VIOLATION_MESSAGE);
+ if (messageIndex < 0) {
+ return Optional.empty();
+ }
+
+ int lineBreakIndex = message.indexOf('\n', messageIndex);
+ if (lineBreakIndex < 0) {
+ return Optional.of(message.substring(messageIndex).trim());
+ }
+
+ return Optional.of(message.substring(messageIndex, lineBreakIndex).trim());
+ }
+
+ private static Optional<String> extractAllowedError(String message) {
+ if (message == null) {
+ return Optional.empty();
+ }
+
+ int messageIndex = message.indexOf("is not allowed");
+ if (messageIndex < 0) {
+ return Optional.empty();
+ }
+
+ int lineStartIndex = message.lastIndexOf('\n', messageIndex);
+ int start = lineStartIndex < 0 ? 0 : lineStartIndex + 1;
+ int lineBreakIndex = message.indexOf('\n', messageIndex);
+ String extracted = (lineBreakIndex < 0 ? message.substring(start) : message.substring(start, lineBreakIndex)).trim();
+
+ if (extracted.isEmpty()) {
+ return Optional.empty();
+ }
+
+ return Optional.of(SandboxPolicy.VIOLATION_MESSAGE + ": " + extracted);
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxViolationClassNotFoundException.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxViolationClassNotFoundException.java
new file mode 100644
index 0000000000..af7cab2a3f
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxViolationClassNotFoundException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox.error;
+
+public final class SandboxViolationClassNotFoundException extends ClassNotFoundException {
+
+ public SandboxViolationClassNotFoundException(String message) {
+ super(message);
+ }
+}
diff --git a/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxViolationException.java b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxViolationException.java
new file mode 100644
index 0000000000..765e1a901e
--- /dev/null
+++ b/streampipes-connect-transformer-groovy/src/main/java/org/apache/streampipes/connect/transformer/groovy/sandbox/error/SandboxViolationException.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.streampipes.connect.transformer.groovy.sandbox.error;
+
+public final class SandboxViolationException extends SecurityException {
+
+ public SandboxViolationException(String message) {
+ super(message);
+ }
+}