- 新增 platform::db::exec_params,收拢业务层 12 处裸 state.db.query - 新增 check-db-access.sh 护栏:白名单 3 文件,7 处命中 0 越界 - 平台侧需求写入 ssw-platform/docs/SSPF-BUSINESS-DB-ACCESS.md(目标 v0.5.0) - Cargo.toml 仍锁 v0.3.0,132 端点契约与响应形状未变
136 lines
5.1 KiB
Bash
136 lines
5.1 KiB
Bash
#!/usr/bin/env bash
|
||
# 库操作**单一出口**静态护栏(附加尺子,不改「五把尺子」编号体系)
|
||
#
|
||
# 判据:业务代码不许直接碰裸 `PGlite` 句柄(`.query(` / `.exec(` / `.exec_params(`)。
|
||
# 所有 SQL 必须经 `src/platform/db.rs` 的封装函数 ⇒ 等 sspf v0.5.0 把
|
||
# 「持 Db.lock 的 Raw API」交付后,切换面只有那一个文件,不会漏改。
|
||
#
|
||
# 允许清单(每一条都写明理由,只许变短不许变长):
|
||
# src/platform/db.rs —— 唯一封装出口(query_json / query_json_params /
|
||
# exec_params / query_scalar_text / column_types / ensure_schema)
|
||
# src/tdc/schema.rs —— 启动期 on_ready 单线程建表,不参与请求期并发
|
||
# src/main.rs —— on_ready 里的 sspf::Db::exec(本身已持平台锁)
|
||
#
|
||
# 扫描口径:只扫非测试代码(第一处 `#[cfg(test)]` 之前)、剥掉注释(文档注释里的
|
||
# 「错误示例」不算数)。与 check-boundary.sh 同一套口径。
|
||
#
|
||
# 用法:bash scripts/check-db-access.sh [--report]
|
||
# 退出码:0 绿 / 1 有越界 / 2 前置错误
|
||
|
||
set -u
|
||
|
||
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
||
REPORT_ONLY=0
|
||
[ "${1:-}" = "--report" ] && REPORT_ONLY=1
|
||
[ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ] && { sed -n '2,20p' "$0" | sed 's/^# \{0,1\}//'; exit 0; }
|
||
|
||
PY_BIN="${PY_BIN:-$(command -v python3 2>/dev/null || true)}"
|
||
[ -n "${PY_BIN}" ] || { echo "❌ 前置错误:找不到 python3。"; exit 2; }
|
||
[ -d "${ROOT}/src" ] || { echo "❌ 前置错误:${ROOT}/src 不存在。"; exit 2; }
|
||
|
||
"${PY_BIN}" - "${ROOT}" "${REPORT_ONLY}" <<'PYEOF'
|
||
import os, re, sys
|
||
|
||
ROOT = sys.argv[1]
|
||
REPORT_ONLY = sys.argv[2] == "1"
|
||
|
||
ALLOW = {
|
||
"src/platform/db.rs": "唯一封装出口(SQL 全在这里)",
|
||
"src/tdc/schema.rs": "启动期 on_ready 单线程建表",
|
||
"src/main.rs": "on_ready 里的 sspf::Db::exec(已持平台锁)",
|
||
}
|
||
CALL = re.compile(r"\.(?:query|exec|exec_params)\s*\(")
|
||
|
||
|
||
def strip_comments(src: str) -> str:
|
||
"""剥 // 行注释与 /* */ 块注释,保留换行与字符串内容(行号不能漂)。"""
|
||
out = []
|
||
i, n = 0, len(src)
|
||
in_str = in_char = in_line = in_block = False
|
||
while i < n:
|
||
c = src[i]
|
||
nxt = src[i + 1] if i + 1 < n else ""
|
||
if in_line:
|
||
if c == "\n":
|
||
in_line = False
|
||
out.append(c)
|
||
i += 1
|
||
continue
|
||
if in_block:
|
||
if c == "*" and nxt == "/":
|
||
in_block = False
|
||
i += 2
|
||
continue
|
||
out.append(c if c == "\n" else " ")
|
||
i += 1
|
||
continue
|
||
if in_str:
|
||
out.append(c)
|
||
if c == "\\":
|
||
if i + 1 < n:
|
||
out.append(src[i + 1]); i += 2; continue
|
||
elif c == '"':
|
||
in_str = False
|
||
i += 1
|
||
continue
|
||
if in_char:
|
||
out.append(c)
|
||
if c == "\\":
|
||
if i + 1 < n:
|
||
out.append(src[i + 1]); i += 2; continue
|
||
elif c == "'":
|
||
in_char = False
|
||
i += 1
|
||
continue
|
||
if c == "/" and nxt == "/":
|
||
in_line = True; i += 2; continue
|
||
if c == "/" and nxt == "*":
|
||
in_block = True; i += 2; continue
|
||
if c == '"':
|
||
in_str = True; out.append(c); i += 1; continue
|
||
if c == "'":
|
||
# 生命周期标记('a)不是字符字面量:只在后面紧跟单个字符 + ' 时才算
|
||
if i + 2 < n and (src[i + 2] == "'" or (nxt == "\\" and i + 3 < n)):
|
||
in_char = True
|
||
out.append(c); i += 1; continue
|
||
out.append(c)
|
||
i += 1
|
||
return "".join(out)
|
||
|
||
|
||
hits, violations = [], []
|
||
for dirpath, _dirs, files in os.walk(os.path.join(ROOT, "src")):
|
||
for f in sorted(files):
|
||
if not f.endswith(".rs"):
|
||
continue
|
||
path = os.path.join(dirpath, f)
|
||
rel = os.path.relpath(path, ROOT).replace(os.sep, "/")
|
||
with open(path, encoding="utf-8") as fh:
|
||
code = strip_comments(fh.read())
|
||
# 只扫生产代码:第一处 #[cfg(test)] 之后不计入
|
||
cut = code.find("#[cfg(test)]")
|
||
if cut != -1:
|
||
code = code[:cut]
|
||
for lineno, line in enumerate(code.splitlines(), 1):
|
||
if CALL.search(line):
|
||
rec = (rel, lineno, line.strip())
|
||
hits.append(rec)
|
||
if rel not in ALLOW:
|
||
violations.append(rec)
|
||
|
||
print("库操作单一出口自检")
|
||
print(" 允许清单(%d):" % len(ALLOW))
|
||
for k, why in ALLOW.items():
|
||
cnt = sum(1 for r in hits if r[0] == k)
|
||
print(" ✅ %-28s %s(命中 %d 处)" % (k, why, cnt))
|
||
print(" 裸句柄调用命中:%d 处" % len(hits))
|
||
for rel, lineno, text in hits:
|
||
flag = "白名单" if rel in ALLOW else "越界"
|
||
print(" [%s] %s:%d %s" % (flag, rel, lineno, text[:110]))
|
||
|
||
if violations:
|
||
print("❌ 有 %d 处业务代码直接碰裸 PGlite ⇒ 请改走 platform::db::* 的封装" % len(violations))
|
||
sys.exit(0 if REPORT_ONLY else 1)
|
||
print("库操作单一出口自检通过 ✅(0 处越界)")
|
||
PYEOF
|