feat(P0②): net-new 裸 JDBC 旁路收口 —— CREATE/DROP DDL 守卫白名单

补齐 DDL 三件套(原仅 ALTER 有 AlterTableSqlGuard):

- CreateTableSqlGuard(legacy-core,纯静态)+ 接入 executeCreateSql:
  原 startsWith("CREATE ") 漏过 CREATE TRIGGER/FUNCTION/INDEX/EXTENSION
  及单语句 CTAS(CREATE TABLE X AS SELECT...)。守卫收窄为引擎仅有的两条
  生成路径:CREATE TABLE(<列>) [+ devDB 的 INSERT INTO order_entity 第二句],
  其余(注释/子查询/AS/USING/跨表 DML/LIKE/INHERITS/PARTITION/多语句)一律拒。

- DropTableSqlGuard + DataDicService.executeDropSql,dropEntity 改走它:
  原裸拼 "DROP TABLE IF EXISTS "+t 仅 startsWith("EIP4"),对 PathVariable
  EIP4X;DROP TABLE SYS_USER 是真实分号注入面。守卫只接表名,整串白名单
  matches(EIP4[A-Z0-9_]{0,37}) → 拼安全 DROP,从根上关闭注入。

对抗审查(security-reviewer,40+ payload)收口 3 项:
- PocWriteTestController 检查 executeCreateSql 返回值(原吞掉守卫拒因)
- appendColumnDdl 数值列 DEFAULT 只许数值字面量(防 raw-append 注入)
- LIKE/INHERITS/PARTITION 补入 DENY,与「其余一律拒」契约一致

验证:legacy-core+metadata 478 单测 0 失败(新增 Guard 14+7、生成器 +2);
真库 e2e scripts/p0-createdrop-acceptance.sh 全绿(建表/删表/非EIP4负控),
p0-alter-acceptance.sh 无回归。

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-22 10:15:41 +08:00
parent 1d1ae9c717
commit 2da1a0ef29
10 changed files with 574 additions and 13 deletions

View File

@@ -270,7 +270,12 @@ public class MetaController {
List<String> ids = jdbcTemplate.queryForList(
"SELECT SYS_ENTITY_ID FROM SYS_ENTITY WHERE TABLENAME=?", String.class, t);
String entityId = ids.isEmpty() ? null : ids.get(0);
jdbcTemplate.execute("DROP TABLE IF EXISTS " + t);
// P0②:DROP 收口到保真层 DropTableSqlGuard(整串白名单),关闭旧 startsWith("EIP4") 的分号注入面。
// drop 在删元数据之前——拒绝时提前返回,不留孤儿元数据。
String dropErr = dataDicService.executeDropSql(t);
if (dropErr != null) {
return err("删除物理表失败: " + dropErr);
}
int a = jdbcTemplate.update(
"DELETE FROM SYS_ENTITYATTRIBUTE WHERE ENTITYID IN (SELECT SYS_ENTITY_ID FROM SYS_ENTITY WHERE TABLENAME=?)", t);
int e = jdbcTemplate.update("DELETE FROM SYS_ENTITY WHERE TABLENAME=?", t);
@@ -535,7 +540,12 @@ public class MetaController {
String defValue = value(attr, "DEFAULTVALUE");
if (defValue != null && !defValue.isBlank()
&& ("INTEGER".equals(ref) || "BOOLEAN".equals(ref) || "DECIMAL".equals(ref) || "MONEY".equals(ref))) {
sql.append(" DEFAULT ").append(defValue);
// P0②(defense-in-depth):这些列均为数值存储,DEFAULT 只能是数值字面量。
// 拒非数值(防 raw-append 的潜在注入,如 DEFAULT pg_sleep(10) / DEFAULT (SELECT ...))。
if (!defValue.strip().matches("-?\\d+(\\.\\d+)?")) {
throw new IllegalStateException("非法 DEFAULT(数值列只允许数值字面量): " + defValue);
}
sql.append(" DEFAULT ").append(defValue.strip());
}
sql.append(", \n");
if (attr.get_ValueAsBoolean("ISIDENTIFIER")) {

View File

@@ -97,7 +97,12 @@ public class PocWriteTestController {
// 3. 动态建表(引擎生成 DDL → 执行)
Entity newEnt = (Entity) dataDicService.getSYSEntity(newEntityId);
String ddl = newEnt.createTableSql();
dataDicService.executeCreateSql(ddl);
// P0②:executeCreateSql 经 CreateTableSqlGuard,被拒时返回错误串(未执行)。
// 必须检查返回值,否则会误报建表成功并吞掉守卫拒因。
String ddlErr = dataDicService.executeCreateSql(ddl);
if (ddlErr != null) {
return fail(report, "建表被守卫拒绝: " + ddlErr);
}
tableCreated = true;
Integer exists = jdbcTemplate.queryForObject(
"SELECT COUNT(*) FROM information_schema.tables WHERE LOWER(table_name)=LOWER(?)",

View File

@@ -1,6 +1,7 @@
package com.eip4.platform.metadata.web;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -46,6 +47,25 @@ class MetaControllerDdlTest {
"外键不得自指 EIP4_ORDER");
}
@Test
@DisplayName("放行:数值列的数值字面量 DEFAULT")
void allowsNumericDefaultLiteral() {
String ddl = MetaController.createTableDdl("EIP4_X", List.of(
attr("EIP4_X_ID", "CHAR", null, true),
attrWithDefault("FLAG", "INTEGER", "0")
), Map.of());
assertTrue(ddl.contains("FLAG integer DEFAULT 0"), "实际: " + ddl);
}
@Test
@DisplayName("拒绝:数值列的非数值 DEFAULT(防 raw-append 注入,如 pg_sleep / 子查询)")
void rejectsNonNumericDefault() {
assertThrows(IllegalStateException.class, () -> MetaController.createTableDdl("EIP4_X", List.of(
attr("EIP4_X_ID", "CHAR", null, true),
attrWithDefault("FLAG", "INTEGER", "pg_sleep(10)")
), Map.of()));
}
private static GeneralPOJO attr(String column, String referenceId, Integer length, boolean mandatory) {
GeneralPOJO po = mock(GeneralPOJO.class);
when(po.get_Value("COLUMNNAME")).thenReturn(column);
@@ -54,4 +74,12 @@ class MetaControllerDdlTest {
when(po.get_ValueAsBoolean("ISMANDATORY")).thenReturn(mandatory);
return po;
}
private static GeneralPOJO attrWithDefault(String column, String referenceId, String defaultValue) {
GeneralPOJO po = mock(GeneralPOJO.class);
when(po.get_Value("COLUMNNAME")).thenReturn(column);
when(po.get_Value("REFERENCEID")).thenReturn(referenceId);
when(po.get_Value("DEFAULTVALUE")).thenReturn(defaultValue);
return po;
}
}