feat(P0②): 引擎ALTER安全通道 + AI安全门enforced

- AlterTableSqlGuard(legacy-core,纯静态白名单):只允许 加列/同族扩长/改可空,
  拒 DROP COLUMN/RENAME/SET NOT NULL/多语句/跨族/小写绕过;放legacy-core避免反向依赖
- DataDicService.executeAlterSql 受控通道(过Guard再execute,defense-in-depth)
- MetaController POST /api/meta/entity/{table}/attributes 加列端点
  (saveAttr元数据+alterAddColumnDdl镜像appendColumnDdl类型映射+evictByTable+补索引,恒可空)
- AiAgentService.applyLocalRelease 织入 AiToolRegistry.check:安全门 advisory→enforced
  (local.apply工具身份,非本机/越权写路径403)
- 单测 Guard15/alter2/gate22(112基线无回归);scripts/p0-alter-acceptance.sh 真库验收

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-21 13:56:14 +08:00
parent 624ffe8a15
commit 8b28595f9e
9 changed files with 530 additions and 0 deletions

View File

@@ -406,6 +406,16 @@ public class AiAgentService {
public AiApplyRun applyLocalRelease(String taskId, String changeSetId, String baseUrl) {
getTask(taskId);
// P0②(c) 安全门 enforced:把 AiToolRegistry 策略引擎织入真实写路径。此前 check() 仅挂在
// /api/ai/tools/check-access 咨询端点(只读返决策),写路径仅靠 requireAdmin + localhost 兜底,
// scope/localOnly/productionWrite 策略形同 advisory。这里以 local.apply 工具身份过门:
// targetEnv=baseUrl,非本机 / 越权 → 403,localReleaseExecutor.apply 不被触发。
AiToolAccessDecision gate = toolRegistry.check(new AiToolAccessRequest(
"agent-developer", "local.apply", List.of("ai:local:apply"), baseUrl, false));
if (gate == null || !gate.allowed()) {
throw new ResponseStatusException(HttpStatus.FORBIDDEN,
"本地应用被安全门拒绝: " + (gate == null ? "no-decision" : String.join("; ", gate.blockers())));
}
AiChangeSet changeSet = findChangeSet(taskId, changeSetId);
if (!"READY_FOR_REVIEW".equals(changeSet.status())) {
throw new ResponseStatusException(HttpStatus.CONFLICT, "变更包状态不可本地应用: " + changeSet.status());

View File

@@ -48,6 +48,17 @@ class AiAgentServiceTest {
assertEquals(3, service.listAudit(task.id()).size());
}
@Test
@DisplayName("P0②(c) 安全门 enforced:非本机 baseUrl 的本地应用被拒(FORBIDDEN),写路径不被触发")
void localApplyRejectedForNonLocalBaseUrl() {
AiAgentService service = newService();
var task = service.createTask("安全门测试");
// 非本机 targetEnv → AiToolRegistry.check 不通过(local-only + production-write 拦截)
var ex = assertThrows(ResponseStatusException.class,
() -> service.applyLocalRelease(task.id(), "any-change-set", "http://prod-host:9000"));
assertEquals(org.springframework.http.HttpStatus.FORBIDDEN, ex.getStatusCode());
}
@Test
@DisplayName("无效 schema 草稿会阻止生成变更包")
void invalidSchemaBlocksChangeSet() throws Exception {