Files
eip4-platform/scripts/p0-alter-acceptance.sh
zhengwr 8b28595f9e 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>
2026-06-21 13:56:14 +08:00

72 lines
4.3 KiB
Bash

#!/usr/bin/env bash
# P0② 引擎 ALTER 安全通道 · 端到端验收
#
# 证明:已建实体经 POST /api/meta/entity/{table}/attributes 加列后,
# ① 元数据(SYS_ENTITYATTRIBUTE)有新列;② 物理表真的 ALTER 加了列(psql 直查 information_schema);
# ③ 受控守卫拒绝重复列/不存在的表(负控)。
#
# 用法(先以 dev profile 起 8094,见 scripts/run-dev.sh / five-ring-acceptance.sh):
# BASE=http://localhost:8094 bash scripts/p0-alter-acceptance.sh
set -uo pipefail
BASE=${BASE:-http://localhost:8094}
ACCT=${ACCT:-admin}; PW=${PW:-111111}
PGURL=${PGURL:-postgresql://zhengwr@localhost:5433/eip4dev}
TS=$(date +%s)
TABLE="EIP4PT_ALT${TS}"
TBL_LC=$(echo "$TABLE" | tr '[:upper:]' '[:lower:]')
FAIL=0
jqpy() { python3 -c "import sys,json;d=json.load(sys.stdin,strict=False);print(eval(\"d$1\"))" 2>/dev/null; }
say() { echo "$@"; }
ok() { echo " ✓ $*"; }
bad() { echo " ✗ $*"; FAIL=1; }
TOK=$(curl -s -X POST "$BASE/api/auth/login" -H 'Content-Type: application/json' \
-d "{\"account\":\"$ACCT\",\"password\":\"$PW\"}" | jqpy "['token']")
H=(-H "Authorization: Bearer $TOK" -H 'Content-Type: application/json')
[ -n "${TOK:-}" ] && [ "$TOK" != "None" ] && ok "login token len=${#TOK}" || { bad "login 失败"; exit 1; }
say "[1] 建实体 $TABLE(仅 TITLE)"
ST=$(curl -s -X POST "$BASE/api/meta/entity" "${H[@]}" -d "{
\"tablename\":\"$TABLE\",\"name\":\"ALTER验收\",
\"attributes\":[{\"column\":\"TITLE\",\"display\":\"标题\",\"type\":\"string\",\"length\":200,\"mandatory\":true}]}" | jqpy "['status']")
[ "$ST" = "0" ] && ok "建表 status=0" || bad "建表失败 status=$ST"
say "[2] 加列:AMOUNT(number)/QTY(integer)/NOTE(string,len 300)/OWNER(reference→SYS_USER)"
R=$(curl -s -X POST "$BASE/api/meta/entity/$TABLE/attributes" "${H[@]}" -d '{
"attributes":[{"column":"AMOUNT","display":"金额","type":"number"},
{"column":"QTY","display":"数量","type":"integer"},
{"column":"NOTE","display":"备注","type":"string","length":300},
{"column":"OWNER","display":"负责人","type":"reference","referenceTable":"SYS_USER"}]}')
ST=$(echo "$R" | jqpy "['status']")
ADDED=$(echo "$R" | jqpy "['data']['addedColumns']")
[ "$ST" = "0" ] && ok "加列 status=0 added=$ADDED" || bad "加列失败: $R"
say "[3] 元数据核对:GET attributes 含新列"
COLS=$(curl -s "$BASE/api/meta/entity/$TABLE/attributes" "${H[@]}" | python3 -c "import sys,json;d=json.load(sys.stdin);print(' '.join(c['column'] for c in d['data']['columns']))" 2>/dev/null)
for c in AMOUNT QTY NOTE OWNER; do
echo "$COLS" | grep -q "$c" && ok "元数据有列 $c" || bad "元数据缺列 $c (cols=$COLS)"
done
say "[4] 物理表核对:psql 直查 information_schema(证明 ALTER 真生效)"
if command -v psql >/dev/null 2>&1; then
PHYS=$(psql "$PGURL" -At -c "SELECT column_name||':'||data_type FROM information_schema.columns WHERE table_schema='eip3' AND table_name='$TBL_LC' AND column_name IN ('amount','qty','note','owner') ORDER BY column_name;" 2>/dev/null)
echo "$PHYS" | grep -q "amount:numeric" && ok "物理列 amount numeric" || bad "物理列 amount 缺/类型错: $PHYS"
echo "$PHYS" | grep -q "qty:integer" && ok "物理列 qty integer" || bad "物理列 qty 缺/类型错"
echo "$PHYS" | grep -qE "note:character varying" && ok "物理列 note varchar" || bad "物理列 note 缺/类型错"
echo "$PHYS" | grep -qE "owner:character" && ok "物理列 owner character(36)" || bad "物理列 owner 缺/类型错"
else
echo " (psql 不可用,跳过物理核对)"
fi
say "[5] 负控:重复加 AMOUNT → 拒;加到不存在的表 → 拒"
ST=$(curl -s -X POST "$BASE/api/meta/entity/$TABLE/attributes" "${H[@]}" -d '{"attributes":[{"column":"AMOUNT","type":"number"}]}' | jqpy "['status']")
[ "$ST" != "0" ] && ok "重复列被拒 status=$ST" || bad "重复列未被拒"
ST=$(curl -s -X POST "$BASE/api/meta/entity/EIP4PT_NOPE${TS}/attributes" "${H[@]}" -d '{"attributes":[{"column":"X","type":"number"}]}' | jqpy "['status']")
[ "$ST" != "0" ] && ok "不存在的表被拒 status=$ST" || bad "不存在的表未被拒"
say "[6] 清理"
curl -s -X DELETE "$BASE/api/meta/entity/$TABLE" "${H[@]}" >/dev/null && ok "删除 $TABLE"
echo ""
[ "$FAIL" = "0" ] && echo "[完成] P0② 引擎 ALTER 安全通道 端到端验收 ✅ 全绿" || { echo "[失败] 存在未通过项 ✗"; exit 1; }