Class GroovySandbox
java.lang.Object
org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.GroovySandbox
Allows Groovy scripts (including Groovy Templates) to be run inside a sandbox.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceHandle for exiting the dynamic scope of the Groovy sandbox. -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic FormValidationcheckScriptForCompilationErrors(String script, groovy.lang.GroovyClassLoader classLoader) Checks a script for compilation errors in a sandboxed environment, without going all the way to actual class creation or initialization.static org.codehaus.groovy.control.CompilerConfigurationPrepares a compiler configuration that rejects certain AST transformations.static ClassLoaderPrepares a classloader for Groovy shell for sandboxing.static org.codehaus.groovy.control.CompilerConfigurationPrepares a compiler configuration the sandbox.enter()Starts a dynamic scope within which calls will be sandboxed.static ObjectDeprecated.static ObjectDeprecated.static voidrunInSandbox(Runnable r, Whitelist whitelist) Deprecated.useenter()static <V> VrunInSandbox(Callable<V> c, Whitelist whitelist) Deprecated.useenter()Compiles and runs a script within the sandbox.withApprovalContext(ApprovalContext context) Specify an approval context.withTaskListener(TaskListener listener) Specify a place to print messages.withWhitelist(Whitelist whitelist) Specify a whitelist.
-
Field Details
-
LOGGER
-
-
Constructor Details
-
GroovySandbox
public GroovySandbox()Creates a sandbox with default settings.
-
-
Method Details
-
withWhitelist
Specify a whitelist. By defaultWhitelist.all()is used.- Returns:
this
-
withApprovalContext
Specify an approval context. By defaultApprovalContext.create()is used.- Returns:
this
-
withTaskListener
Specify a place to print messages. By default nothing is printed.- Returns:
this
-
enter
Starts a dynamic scope within which calls will be sandboxed.- Returns:
- a scope object, useful for putting this into a
try-with-resources block
-
runScript
Compiles and runs a script within the sandbox.- Parameters:
shell- the shell to be used; seecreateSecureCompilerConfiguration()and similar methodsscriptText- the script to run- Returns:
- the return value of the script
-
createSecureCompilerConfiguration
@NonNull public static org.codehaus.groovy.control.CompilerConfiguration createSecureCompilerConfiguration()Prepares a compiler configuration the sandbox.CAUTION: When creating
GroovyShellwith thisCompilerConfiguration, you also have to usecreateSecureClassLoader(ClassLoader)to wrap a classloader of your choice into sandbox-aware one.Otherwise the classloader that you provide to
GroovyShellmight have its own copy of groovy-sandbox, which lets the code escape the sandbox.- Returns:
- a compiler configuration set up to use the sandbox
-
createBaseCompilerConfiguration
@NonNull public static org.codehaus.groovy.control.CompilerConfiguration createBaseCompilerConfiguration()Prepares a compiler configuration that rejects certain AST transformations. Used bycreateSecureCompilerConfiguration(). -
createSecureClassLoader
Prepares a classloader for Groovy shell for sandboxing. SeecreateSecureCompilerConfiguration()for the discussion. -
runInSandbox
@Deprecated public static void runInSandbox(@NonNull Runnable r, @NonNull Whitelist whitelist) throws RejectedAccessException Deprecated.useenter()Runs a block in the sandbox. You must have usedcreateSecureCompilerConfiguration()to prepare the Groovy shell. Userun(groovy.lang.Script, org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist)instead whenever possible.- Parameters:
r- a block of code during whose execution all calls are interceptedwhitelist- the whitelist to use, such asWhitelist.all()- Throws:
RejectedAccessException- in case an attempted call was not whitelisted
-
runInSandbox
@Deprecated public static <V> V runInSandbox(@NonNull Callable<V> c, @NonNull Whitelist whitelist) throws Exception Deprecated.useenter()Runs a function in the sandbox. You must have usedcreateSecureCompilerConfiguration()to prepare the Groovy shell. Userun(groovy.lang.Script, org.jenkinsci.plugins.scriptsecurity.sandbox.Whitelist)instead whenever possible.- Parameters:
c- a block of code during whose execution all calls are interceptedwhitelist- the whitelist to use, such asWhitelist.all()- Returns:
- the return value of the block
- Throws:
RejectedAccessException- in case an attempted call was not whitelistedException- in case the block threw some other exception
-
run
@Deprecated public static Object run(@NonNull groovy.lang.Script script, @NonNull Whitelist whitelist) throws RejectedAccessException Deprecated.- Throws:
RejectedAccessException
-
run
@Deprecated public static Object run(@NonNull groovy.lang.GroovyShell shell, @NonNull String script, @NonNull Whitelist whitelist) throws RejectedAccessException Deprecated.Runs a script in the sandbox. You must have usedcreateSecureCompilerConfiguration()to prepare the Groovy shell.- Parameters:
shell- a shell ready forGroovyShell.parse(String)script- a scriptwhitelist- the whitelist to use, such asWhitelist.all()- Returns:
- the value produced by the script, if any
- Throws:
RejectedAccessException- in case an attempted call was not whitelisted
-
checkScriptForCompilationErrors
@NonNull public static FormValidation checkScriptForCompilationErrors(String script, groovy.lang.GroovyClassLoader classLoader) Checks a script for compilation errors in a sandboxed environment, without going all the way to actual class creation or initialization.- Parameters:
script- The script to checkclassLoader- TheGroovyClassLoaderto use during compilation.- Returns:
- The
FormValidationfor the compilation check.
-
runScript(groovy.lang.GroovyShell, java.lang.String)