- axum 0.8 + pglite:12 张表,多应用按 App-Code 头解析 f_system_id 隔离 - 面 A(ssw/react 形状)+ 面 B(官方 JNPF 形状,Vue-Version: 3)双契约并存 - 业务模块 asset/DataResource;面 B 用户 CRUD 9 条已落地 - 六道闸门 + 两把运行时尺子(perm-smoke 43 / perm-smoke-jnpf 23) - scripts/:前端接口清单、页面依赖反查、运行时探针
122 lines
4.3 KiB
Python
122 lines
4.3 KiB
Python
#!/usr/bin/env python3
|
||
"""probe-page.py —— 拿页面接口清单**直接打真实后端**,产出「通/缺」表。
|
||
|
||
为什么要它:`fe-gap.py` 是**静态**比对(后端源码里有没有这个路由字面量),
|
||
而这里问的是**运行时**事实:路由真的注册了吗?守卫真的放行了吗?响应形状对不对?
|
||
两者会不一致(例如 `routes.rs` 里注册了但 matchit 冲突没生效)。
|
||
|
||
用法:
|
||
python3 scripts/probe-page.py docs/fe/page-user.tsv
|
||
python3 scripts/probe-page.py docs/fe/page-user.tsv --base http://127.0.0.1:30000
|
||
|
||
安全口径:路径参数一律替换成 `__probe__`(库里不存在这个 id)⇒
|
||
**任何方法都可以安全打**:路由不存在 ⇒ 404;路由存在但没这条记录 ⇒ 业务码而非写库。
|
||
唯一例外是「集合 POST」(`POST /api/permission/Users`)会真建号,故用空 body
|
||
并标成 `SKIP-WRITE`,只在 `--allow-write` 时才打。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import argparse
|
||
import json
|
||
import pathlib
|
||
import re
|
||
import sys
|
||
import urllib.error
|
||
import urllib.request
|
||
|
||
TOKEN_FILE = pathlib.Path("/tmp/ssw_token.txt")
|
||
PROBE_ID = "__probe__"
|
||
|
||
# 会**新建**数据、且 body 为空也可能成功的集合写接口 ⇒ 默认跳过
|
||
DANGEROUS = {
|
||
("POST", "/api/permission/Users"),
|
||
("POST", "/api/permission/Group"),
|
||
("POST", "/api/permission/Users/workHandover"),
|
||
("POST", "/api/permission/Group/{param}/Actions/AddUser"),
|
||
("POST", "/api/permission/Group/{param}/Actions/DeleteUser"),
|
||
}
|
||
|
||
|
||
def probe(method: str, path: str, base: str, token: str, timeout: float = 8.0):
|
||
url = base + path.replace("{param}", PROBE_ID)
|
||
body = None
|
||
headers = {
|
||
"Authorization": token,
|
||
"Vue-Version": "3",
|
||
"App-Code": "mainSystem",
|
||
}
|
||
if method in ("POST", "PUT"):
|
||
body = b"{}"
|
||
headers["Content-Type"] = "application/json"
|
||
req = urllib.request.Request(url, data=body, method=method, headers=headers)
|
||
try:
|
||
with urllib.request.urlopen(req, timeout=timeout) as resp:
|
||
raw = resp.read().decode("utf-8", "replace")
|
||
return resp.status, raw
|
||
except urllib.error.HTTPError as e:
|
||
return e.code, e.read().decode("utf-8", "replace")
|
||
except Exception as e: # noqa: BLE001 —— 网络层异常同样要变成一行结果
|
||
return -1, str(e)
|
||
|
||
|
||
def verdict(status: int, raw: str) -> tuple[str, str]:
|
||
"""⇒ (判定, 说明)。"""
|
||
if status == -1:
|
||
return "ERR", raw[:60]
|
||
if status == 404:
|
||
return "缺路由", raw[:60]
|
||
try:
|
||
doc = json.loads(raw)
|
||
except json.JSONDecodeError:
|
||
return "非信封", raw[:60]
|
||
code = doc.get("code")
|
||
msg = str(doc.get("msg", ""))[:24]
|
||
if status == 200 and code == 200:
|
||
return "通", msg
|
||
return f"码{code}", msg
|
||
|
||
|
||
def main() -> int:
|
||
ap = argparse.ArgumentParser()
|
||
ap.add_argument("tsv")
|
||
ap.add_argument("--base", default="http://127.0.0.1:30000")
|
||
ap.add_argument("--allow-write", action="store_true")
|
||
args = ap.parse_args()
|
||
|
||
token = TOKEN_FILE.read_text().strip() if TOKEN_FILE.exists() else ""
|
||
if not token:
|
||
sys.exit(f"缺 token:{TOKEN_FILE}(先跑一次面 B 登录)")
|
||
|
||
rows = []
|
||
with open(args.tsv, encoding="utf-8") as fh:
|
||
fh.readline()
|
||
for line in fh:
|
||
p = line.rstrip("\n").split("\t")
|
||
if len(p) < 3:
|
||
continue
|
||
rows.append((p[1].upper(), p[2]))
|
||
|
||
# 同一个 (method,path) 只打一次(并发/多次 import 会重复)
|
||
uniq = sorted(set(rows))
|
||
|
||
print(f"{'METHOD':<7}{'PATH':<56}{'HTTP':>5} {'判定':<8}说明")
|
||
print("-" * 92)
|
||
stats: dict[str, int] = {}
|
||
for method, path in uniq:
|
||
if (method, path) in DANGEROUS and not args.allow_write:
|
||
print(f"{method:<7}{path:<56}{'-':>5} {'跳过写':<8}需 --allow-write")
|
||
stats["跳过写"] = stats.get("跳过写", 0) + 1
|
||
continue
|
||
status, raw = probe(method, path, args.base, token)
|
||
v, note = verdict(status, raw)
|
||
stats[v] = stats.get(v, 0) + 1
|
||
print(f"{method:<7}{path:<56}{status:>5} {v:<8}{note}")
|
||
print("-" * 92)
|
||
print(" ".join(f"{k}:{v}" for k, v in sorted(stats.items())))
|
||
return 0
|
||
|
||
|
||
if __name__ == "__main__":
|
||
raise SystemExit(main())
|