Class DefaultSandbox
java.lang.Object
cloud.opencode.base.expression.sandbox.DefaultSandbox
- All Implemented Interfaces:
Sandbox
Default Security Sandbox
默认安全沙箱
Provides configurable security constraints for expression evaluation.
为表达式求值提供可配置的安全约束。
Features | 主要功能:
- Class-level and package-level allow/deny lists - 类级别和包级别的允许/拒绝列表
- Method-level allow/deny lists - 方法级别的允许/拒绝列表
- Configurable expression length, evaluation depth, and time limits - 可配置表达式长度、求值深度和时间限制
- Preset configurations: permissive, restrictive, standard - 预设配置: 宽松、限制、标准
- Builder pattern for custom configurations - 构建器模式用于自定义配置
Usage Examples | 使用示例:
// Use standard sandbox
Sandbox sandbox = DefaultSandbox.standard();
// Custom sandbox
Sandbox custom = DefaultSandbox.builder()
.allowAllByDefault(true)
.addDeniedClass("java.lang.Runtime")
.addDeniedMethod("exec")
.maxEvaluationTime(3000)
.build();
Security | 安全性:
- Thread-safe: Yes, immutable after construction with defensive copies - 线程安全: 是,构造后不可变,使用防御性拷贝
- Null-safe: Yes, null class/method/property returns false - 空值安全: 是,null类/方法/属性返回false
- Deny takes priority over allow - 拒绝优先于允许
- Since:
- JDK 25, opencode-base-expression V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classBuilder for DefaultSandbox DefaultSandbox构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic DefaultSandbox.Builderbuilder()Create a builder 创建构建器intGet the maximum evaluation depth 获取最大求值深度longGet the maximum evaluation time in milliseconds 获取最大求值时间(毫秒)intGet the maximum expression length 获取最大表达式长度booleanisClassAllowed(Class<?> clazz) Check if a class is allowed 检查是否允许访问类booleanisMethodAllowed(Object target, Method method) Check if a method call is allowed 检查是否允许调用方法booleanisPropertyAllowed(Object target, String property) Check if a property access is allowed 检查是否允许访问属性static DefaultSandboxCreate a permissive sandbox that allows everything 创建允许所有操作的宽松沙箱static DefaultSandboxCreate a restrictive sandbox that denies by default 创建默认拒绝的限制性沙箱static DefaultSandboxstandard()Create a standard sandbox with sensible defaults 创建具有合理默认值的标准沙箱Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface Sandbox
isConstructionAllowed
-
Method Details
-
permissive
Create a permissive sandbox that allows everything 创建允许所有操作的宽松沙箱- Returns:
- the permissive sandbox | 宽松沙箱
-
restrictive
Create a restrictive sandbox that denies by default 创建默认拒绝的限制性沙箱- Returns:
- the restrictive sandbox | 限制性沙箱
-
standard
Create a standard sandbox with sensible defaults 创建具有合理默认值的标准沙箱- Returns:
- the standard sandbox | 标准沙箱
-
isClassAllowed
Description copied from interface:SandboxCheck if a class is allowed 检查是否允许访问类- Specified by:
isClassAllowedin interfaceSandbox- Parameters:
clazz- the class | 类- Returns:
- true if allowed | 如果允许返回true
-
isMethodAllowed
Description copied from interface:SandboxCheck if a method call is allowed 检查是否允许调用方法- Specified by:
isMethodAllowedin interfaceSandbox- Parameters:
target- the target object | 目标对象method- the method | 方法- Returns:
- true if allowed | 如果允许返回true
-
isPropertyAllowed
Description copied from interface:SandboxCheck if a property access is allowed 检查是否允许访问属性- Specified by:
isPropertyAllowedin interfaceSandbox- Parameters:
target- the target object | 目标对象property- the property name | 属性名- Returns:
- true if allowed | 如果允许返回true
-
getMaxExpressionLength
public int getMaxExpressionLength()Description copied from interface:SandboxGet the maximum expression length 获取最大表达式长度- Specified by:
getMaxExpressionLengthin interfaceSandbox- Returns:
- the max length, -1 for unlimited | 最大长度,-1表示无限
-
getMaxEvaluationDepth
public int getMaxEvaluationDepth()Description copied from interface:SandboxGet the maximum evaluation depth 获取最大求值深度- Specified by:
getMaxEvaluationDepthin interfaceSandbox- Returns:
- the max depth, -1 for unlimited | 最大深度,-1表示无限
-
getMaxEvaluationTime
public long getMaxEvaluationTime()Description copied from interface:SandboxGet the maximum evaluation time in milliseconds 获取最大求值时间(毫秒)- Specified by:
getMaxEvaluationTimein interfaceSandbox- Returns:
- the max time, -1 for unlimited | 最大时间,-1表示无限
-
builder
-