Class DefaultSandbox
- All Implemented Interfaces:
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 denylist semantics (allowAllByDefault=true).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 denylist semantics (allowAllByDefault=true). 创建以"拒绝列表"语义为基础(allowAllByDefault=true)的"标准"沙箱。V1.0.4 sec round-4 P2 — security caveat:
standard()uses a denylist, which means any class or method NOT explicitly listed is allowed. The deny set blocksRuntime/ProcessBuilder/System/Thread/ClassLoaderand a handful of reflection methods, but it does NOT block:- Subclasses of denied types (e.g.
MyClassLoader extends ClassLoaderis allowed) - Methods with the same names defined on undenied classes
- Static methods invoked via undenied wrapper classes
- JNI / native methods on third-party classes
For untrusted input, prefer
restrictive()which usesallowAllByDefault =false(allowlist semantics) — only explicitly permitted classes/methods are reachable.standard()is appropriate ONLY when the expression source is fully trusted (e.g. developer-authored config templates) and you want a defense-in-depth net for typos.V1.0.4 sec round-4 P2 安全提示:
standard()用拒绝列表, 即任何未显式列出的类/方法都允许。拒绝集封堵了Runtime/ProcessBuilder/System/Thread/ClassLoader和少数反射方法,但**不**封堵:被拒类的子类(如MyClassLoader extends ClassLoader)、 同名方法定义在未被拒类上的情况、未被拒包装类的静态方法、第三方类的 JNI/native 方法。面对不可信输入请用
restrictive()(allowAllByDefault=false,允许列表语义), 仅显式允许的类/方法可达。standard()仅适合表达式来源完全可信(如开发者编写的配置 模板),并希望对拼写错误等情况有 defense-in-depth 兜底的场景。- Returns:
- the standard sandbox | 标准沙箱
- See Also:
- Subclasses of denied types (e.g.
-
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
-