Merge feat/groovy-sandbox-hardening: 强化 Groovy 沙箱拦 RCE 原语
Some checks failed
ci / build-test-parity (push) Has been cancelled
Some checks failed
ci / build-test-parity (push) Has been cancelled
2026-06-25 安全评审 DEFER 的 MEDIUM:硬化 SecureASTCustomizer/MethodCallExpressionChecker(反射/进程执行/元编程/动态求值全拒),收口所有 Groovy sink(ScriptReplay/EditGrid/服务流)的残余沙箱弱点。TDD+1428 脚本语料零回归,全 reactor 绿。 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -1,27 +1,117 @@
|
|||||||
package com.dsasp.server.core.script.secure;
|
package com.dsasp.server.core.script.secure;
|
||||||
|
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.codehaus.groovy.ast.ClassNode;
|
||||||
import org.codehaus.groovy.ast.expr.ClassExpression;
|
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.Expression;
|
||||||
import org.codehaus.groovy.ast.expr.MethodCallExpression;
|
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.control.customizers.SecureASTCustomizer;
|
import org.codehaus.groovy.control.customizers.SecureASTCustomizer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* F2-13: 原样从 .groovy 转写为 Java,消除 gmavenplus 在 JDK26 下的 ASM 兼容性问题。
|
* Groovy 沙箱表达式白/黑名单检查器(编译期生效)。
|
||||||
* 语义与原 .groovy 完全一致:禁止脚本调用 System.exit()。
|
*
|
||||||
|
* <p><b>历史</b>:F2-13 从 .groovy 转写为 Java;原实现仅禁 {@code System.exit()}。
|
||||||
|
*
|
||||||
|
* <p><b>P0 加固(2026-06-25 安全评审 MEDIUM)</b>:{@code SecureASTCustomizer} 的 import 白名单挡不住
|
||||||
|
* 「内联全限定名 / 反射 / 元编程 / GDK 进程执行」等 RCE 原语。本检查器在编译期拒绝这些危险表达式,
|
||||||
|
* 覆盖 {@link MethodCallExpression}/{@link StaticMethodCallExpression}/{@link ConstructorCallExpression}/
|
||||||
|
* {@link PropertyExpression}。判据来自真实脚本语料核实(1428 改写脚本对下列原语<strong>零使用</strong>,
|
||||||
|
* 故拦截不误伤业务脚本;{@code System.out}/业务对象 {@code getProperty} 等照常放行)。
|
||||||
|
*
|
||||||
|
* <p>纪律:纯 Groovy AST 逻辑,不依赖 {@code com.eip4.*}(守双层制边界)。
|
||||||
*/
|
*/
|
||||||
public class MethodCallExpressionChecker implements SecureASTCustomizer.ExpressionChecker {
|
public class MethodCallExpressionChecker implements SecureASTCustomizer.ExpressionChecker {
|
||||||
|
|
||||||
|
/** 危险接收类:其上任何方法调用/构造一律拒(全限定名匹配)。 */
|
||||||
|
private static final Set<String> DANGEROUS_TYPES_FQN = Set.of(
|
||||||
|
"java.lang.Runtime", "java.lang.ProcessBuilder", "java.lang.Process",
|
||||||
|
"java.lang.ClassLoader", "java.lang.Thread", "java.lang.ThreadGroup",
|
||||||
|
"groovy.lang.GroovyShell", "groovy.lang.GroovyClassLoader", "groovy.lang.GroovyCodeSource",
|
||||||
|
"groovy.util.Eval",
|
||||||
|
"javax.script.ScriptEngineManager", "javax.script.ScriptEngine",
|
||||||
|
"java.lang.reflect.Method", "java.lang.reflect.Field",
|
||||||
|
"java.lang.reflect.Constructor", "java.lang.reflect.AccessibleObject");
|
||||||
|
|
||||||
|
/** 危险接收类:简单名匹配(覆盖未解析到全限定名的 ClassExpression;均无业务同名冲突)。 */
|
||||||
|
private static final Set<String> DANGEROUS_TYPES_SIMPLE = Set.of(
|
||||||
|
"Runtime", "ProcessBuilder", "Process", "ClassLoader", "Thread", "ThreadGroup",
|
||||||
|
"GroovyShell", "GroovyClassLoader", "GroovyCodeSource", "Eval", "ScriptEngineManager");
|
||||||
|
|
||||||
|
/** 通用危险方法名:无论接收者一律拒(语料零使用,均为明确 RCE/反射/元编程原语)。 */
|
||||||
|
private static final Set<String> DANGEROUS_METHODS = Set.of(
|
||||||
|
"exec", "execute", "executeMethods",
|
||||||
|
"forName", "newInstance", "loadClass", "defineClass",
|
||||||
|
"getClassLoader", "setAccessible", "invoke", "invokeMethod",
|
||||||
|
"getDeclaredMethod", "getDeclaredMethods", "getDeclaredField", "getDeclaredFields",
|
||||||
|
"getDeclaredConstructor", "getDeclaredConstructors",
|
||||||
|
"getMetaClass", "setMetaClass");
|
||||||
|
|
||||||
|
/** System 上的危险方法(接收者为 System 时拒);其余如 out/err/currentTimeMillis/nanoTime 放行。 */
|
||||||
|
private static final Set<String> DANGEROUS_SYSTEM_METHODS = Set.of(
|
||||||
|
"exit", "getenv", "getProperty", "getProperties", "setProperty", "setProperties",
|
||||||
|
"clearProperty", "load", "loadLibrary", "setSecurityManager", "inheritedChannel", "console");
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isAuthorized(Expression expression) {
|
public boolean isAuthorized(Expression expression) {
|
||||||
if (expression instanceof MethodCallExpression mce) {
|
if (expression instanceof MethodCallExpression mce) {
|
||||||
|
String method = mce.getMethodAsString();
|
||||||
|
// 1) 接收者是类引用(含内联全限定名):System 仅拒危险方法,其余危险类拒所有方法
|
||||||
if (mce.getObjectExpression() instanceof ClassExpression ce) {
|
if (mce.getObjectExpression() instanceof ClassExpression ce) {
|
||||||
if (ce.getType().getName().equals(System.class.getName())) {
|
ClassNode t = ce.getType();
|
||||||
if ("exit".equals(mce.getMethodAsString())) {
|
if (isSystem(t)) {
|
||||||
return false;
|
return !DANGEROUS_SYSTEM_METHODS.contains(method);
|
||||||
}
|
}
|
||||||
|
if (isDangerousType(t)) {
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// 2) 通用危险方法名(无论接收者),挡反射/进程执行/元编程链
|
||||||
|
if (method != null && DANGEROUS_METHODS.contains(method)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (expression instanceof StaticMethodCallExpression smce) {
|
||||||
|
ClassNode t = smce.getOwnerType();
|
||||||
|
String method = smce.getMethod();
|
||||||
|
if (isSystem(t)) {
|
||||||
|
return !DANGEROUS_SYSTEM_METHODS.contains(method);
|
||||||
|
}
|
||||||
|
if (isDangerousType(t)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return method == null || !DANGEROUS_METHODS.contains(method);
|
||||||
|
}
|
||||||
|
if (expression instanceof ConstructorCallExpression cce) {
|
||||||
|
// new ProcessBuilder/GroovyShell/Thread/ScriptEngineManager/... 一律拒
|
||||||
|
return !isDangerousType(cce.getType());
|
||||||
|
}
|
||||||
|
if (expression instanceof PropertyExpression pe) {
|
||||||
|
// 元编程:obj.metaClass(读写均拒)
|
||||||
|
return !"metaClass".equals(pe.getPropertyAsString());
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isSystem(ClassNode t) {
|
||||||
|
return t != null && "java.lang.System".equals(t.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static boolean isDangerousType(ClassNode t) {
|
||||||
|
if (t == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
String fqn = t.getName();
|
||||||
|
if (fqn != null) {
|
||||||
|
if (DANGEROUS_TYPES_FQN.contains(fqn) || fqn.startsWith("java.lang.reflect.")) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
String simple = t.getNameWithoutPackage();
|
||||||
|
return simple != null && DANGEROUS_TYPES_SIMPLE.contains(simple);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,112 @@
|
|||||||
|
package com.dsasp.server.core.script.secure;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||||
|
|
||||||
|
import javax.script.Compilable;
|
||||||
|
import javax.script.ScriptEngine;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Groovy 沙箱硬化(P0 followup,2026-06-25 安全评审 MEDIUM):
|
||||||
|
* SecureASTCustomizer 的 import 白名单挡不住「内联全限定名 / 反射 / 元编程 / GDK 进程执行」等 RCE 原语,
|
||||||
|
* 旧 MethodCallExpressionChecker 仅挡 {@code System.exit()}。本测试以真实编译验证强化后的双向语义:
|
||||||
|
* <ul>
|
||||||
|
* <li><b>危险载荷必拒</b>:Runtime/ProcessBuilder/Class.forName/反射/GroovyShell/GDK execute/危险 System 方法
|
||||||
|
* 在<strong>编译期</strong>被 SecureASTCustomizer + 强化 checker 拒绝(compile 抛异常)。</li>
|
||||||
|
* <li><b>合法用法必放</b>:System.out、业务对象 getProperty(语料 17 处)、BigDecimal、joda、业务动词
|
||||||
|
* 照常编译通过(不误伤 1428 改写脚本语料)。</li>
|
||||||
|
* </ul>
|
||||||
|
* 注:直接经 factory 编译以暴露沙箱拒因,不走 {@code ScriptingEnginesManager.evaluateGroovy}(其吞编译异常降级)。
|
||||||
|
*/
|
||||||
|
class SecureGroovyScriptEngineFactoryTest {
|
||||||
|
|
||||||
|
/** 经强化沙箱编译脚本;被拒(编译抛异常)返回 true。 */
|
||||||
|
private boolean blocked(String script) {
|
||||||
|
ScriptEngine engine = new SecureGroovyScriptEngineFactory().getScriptEngine();
|
||||||
|
try {
|
||||||
|
((Compilable) engine).compile(script);
|
||||||
|
return false;
|
||||||
|
} catch (Exception e) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertBlocked(String script) {
|
||||||
|
org.junit.jupiter.api.Assertions.assertTrue(blocked(script),
|
||||||
|
"危险脚本必须被沙箱拒绝(编译期): " + script);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assertAllowed(String script) {
|
||||||
|
assertDoesNotThrow(() -> {
|
||||||
|
ScriptEngine engine = new SecureGroovyScriptEngineFactory().getScriptEngine();
|
||||||
|
((Compilable) engine).compile(script);
|
||||||
|
}, "合法脚本不得被误伤: " + script);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ───────────────────────── 危险载荷必拒 ─────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("进程执行:Runtime.exec / ProcessBuilder / GDK String.execute 全拒")
|
||||||
|
void blocksProcessExecution() {
|
||||||
|
assertBlocked("Runtime.getRuntime().exec('id')");
|
||||||
|
assertBlocked("java.lang.Runtime.getRuntime().exec(['sh','-c','id'] as String[])");
|
||||||
|
assertBlocked("new ProcessBuilder('id').start()");
|
||||||
|
assertBlocked("'id'.execute()");
|
||||||
|
assertBlocked("['sh','-c','id'].execute()");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("反射 / 类加载:Class.forName / newInstance / getClassLoader / invoke 全拒")
|
||||||
|
void blocksReflection() {
|
||||||
|
assertBlocked("Class.forName('java.lang.Runtime')");
|
||||||
|
assertBlocked("Class.forName('java.lang.Runtime').newInstance()");
|
||||||
|
assertBlocked("def c = 'x'.getClass(); c.getClassLoader()");
|
||||||
|
assertBlocked("'x'.class.getDeclaredMethods()");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("动态求值:GroovyShell / Eval 全拒")
|
||||||
|
void blocksDynamicEval() {
|
||||||
|
assertBlocked("new GroovyShell().evaluate('1+1')");
|
||||||
|
assertBlocked("Eval.me('1+1')");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("System 危险方法拒(exit/getenv/getProperty/setProperty)")
|
||||||
|
void blocksDangerousSystem() {
|
||||||
|
assertBlocked("System.exit(0)");
|
||||||
|
assertBlocked("System.getenv('PATH')");
|
||||||
|
assertBlocked("System.getProperty('user.home')");
|
||||||
|
assertBlocked("System.setProperty('x','y')");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("元编程:metaClass 篡改拒")
|
||||||
|
void blocksMetaprogramming() {
|
||||||
|
assertBlocked("String.metaClass.foo = { -> 'pwned' }");
|
||||||
|
}
|
||||||
|
|
||||||
|
// ───────────────────────── 合法用法必放(防误伤语料) ─────────────────────────
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("System.out 调试输出放行(语料 13 处)")
|
||||||
|
void allowsSystemOut() {
|
||||||
|
assertAllowed("System.out.println('hello')");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("业务对象 getProperty 放行(语料 17 处,接收者非 System)")
|
||||||
|
void allowsBusinessGetProperty() {
|
||||||
|
assertAllowed("def m = ['x':1]; m.getProperty('x')");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("BigDecimal / joda / 业务动词放行")
|
||||||
|
void allowsBenignBusinessScript() {
|
||||||
|
assertAllowed("new BigDecimal('1.5') + new BigDecimal('2.5')");
|
||||||
|
assertAllowed("import org.joda.time.DateTime; DateTime.now().plusDays(1)");
|
||||||
|
assertAllowed("def x = getFieldValue('A'); enableButton('B', true); bindField('C', x); return x");
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user