diff --git a/app/audit.py b/app/audit.py index dfa76da..f9f20d5 100644 --- a/app/audit.py +++ b/app/audit.py @@ -1,8 +1,10 @@ """操作审计:谁在什么时候改了哪个菜单的数据,成功还是被拒。 -写入口只有一个:`/api` 子应用上的 HTTP 中间件。它在每个请求结束后落一行, -所以 401/403(越权尝试)和 4xx/5xx(失败的写操作)同样留痕。 -只记写操作 + 登录/退出,GET 不落库,避免日志被列表刷新刷爆。 +写入口只有一个:`/api` 子应用上的 HTTP 中间件。它在每个请求结束后落一行: +- 写操作(POST/PUT/PATCH/DELETE)+ 登录/退出/改密/下线:成功失败都记; +- 403 一律改记 `action=deny`(**含 GET**——越权探测也要留痕); +- 500:业务代码抛穿中间件时按 `status=500` 补记,不丢失败的写操作; +- 其余 GET 不落库,避免日志被列表刷新刷爆。 业务代码可以用 `audit.note(request, "……")` 补一句说明,日志的「说明」列直接可读。 """ from __future__ import annotations @@ -34,14 +36,14 @@ def client_ip(request: Request) -> str | None: def action_for(path: str, method: str) -> str | None: - """返回 None 表示这个请求不进审计。""" + """返回 None 表示这个请求不进审计(403 例外,中间件会改记 deny)。""" clean = path.rstrip("/") if clean in SPECIAL_ACTION: return SPECIAL_ACTION[clean] + if clean.endswith("/revoke"): # 先于 METHOD_ACTION:否则被 POST→create 吃掉,revoke 永远 0 条 + return "revoke" if method in METHOD_ACTION: return METHOD_ACTION[method] - if clean.endswith("/revoke"): - return "revoke" return None @@ -91,26 +93,33 @@ def audit_middleware_factory(): async def audit(request: Request, call_next): from .security import resolve_rule # 避免循环导入 + def record(action: str, status: int) -> None: + rule = resolve_rule(request.url.path.rstrip("/"), request.method) + state = getattr(request, "state", None) + detail = getattr(state, "audit_detail", None) + if status >= 400: + detail = f"{detail};" if detail else "" + detail += f"HTTP {status}" + try: + with SessionLocal() as db: + write(db, user=getattr(state, "user", None), request=request, + status=status, action=action, menu=rule[0] if rule else None, + detail=detail, username=getattr(state, "audit_username", None)) + except Exception: # 审计绝不能把业务请求带崩 + pass + action = action_for(request.url.path, request.method) - response = await call_next(request) - if action is None: - return response - path = request.url.path.rstrip("/") - rule = resolve_rule(path, request.method) - menu = rule[0] if rule else None - state = getattr(request, "state", None) - user = getattr(state, "user", None) - actor = getattr(state, "audit_username", None) - detail = getattr(state, "audit_detail", None) - if response.status_code >= 400: - detail = f"{detail};" if detail else "" - detail += f"HTTP {response.status_code}" try: - with SessionLocal() as db: - write(db, user=user, request=request, status=response.status_code, - action=action, menu=menu, detail=detail, username=actor) - except Exception: # 审计绝不能把业务请求带崩 - pass + response = await call_next(request) + except Exception: + if action is not None: # 写操作抛穿:先补一条 500 再让上层处理 + record(action, 500) + raise + status = response.status_code + if status == 403: + action = "deny" # 越权统一记 deny,含被拒的 GET + if action is not None: + record(action, status) return response return audit diff --git a/app/routers/rbac.py b/app/routers/rbac.py index 5ebdb44..20bccbf 100644 --- a/app/routers/rbac.py +++ b/app/routers/rbac.py @@ -95,6 +95,7 @@ def meta(): {"value": "logout", "label": "退出"}, {"value": "password", "label": "改密码"}, {"value": "revoke", "label": "下线"}, + {"value": "deny", "label": "越权"}, ], } diff --git a/app/schema_patch.py b/app/schema_patch.py index d38ee76..1f4dc58 100644 --- a/app/schema_patch.py +++ b/app/schema_patch.py @@ -27,7 +27,14 @@ STALE_COLUMNS: tuple[tuple[str, str], ...] = ( def drop_stale_columns(engine: Engine) -> list[str]: - """删除 STALE_COLUMNS 里登记的残留列,返回实际删掉的(表.列)。幂等。""" + """删除 STALE_COLUMNS 里登记的残留列,返回实际删掉的(表.列)。幂等。 + + 这个函数在 main.py 导入期执行,任何一列失败都不能掀翻整个服务: + 索引 / UNIQUE / 视图引用会让 DROP COLUMN 报错(老库常态),逐列 try 后跳过即可—— + 死列留着不碍业务,服务起不来才碍。 + """ + if engine.dialect.name != "sqlite": # MySQL 等目标库不做 SQLite 式清列 + return [] from sqlite3 import sqlite_version_info # 先看驱动能力,低版本直接跳过 if sqlite_version_info < (3, 35): print("[warn] SQLite < 3.35 不支持 DROP COLUMN,死列保留(不影响功能,只是占位)") @@ -39,9 +46,12 @@ def drop_stale_columns(engine: Engine) -> list[str]: continue if col not in {c["name"] for c in insp.get_columns(table)}: continue - with engine.begin() as conn: - conn.execute(text(f"ALTER TABLE {table} DROP COLUMN {col}")) - dropped.append(f"{table}.{col}") + try: + with engine.begin() as conn: + conn.execute(text(f"ALTER TABLE {table} DROP COLUMN {col}")) + dropped.append(f"{table}.{col}") + except Exception as e: # 索引/UNIQUE/视图引用等:留着死列继续启动,日志说清人话 + print(f"[warn] 跳过死列 {table}.{col}:删不掉({e}),不影响使用") return dropped diff --git a/app/security.py b/app/security.py index 38e3b4d..ec8fcf2 100644 --- a/app/security.py +++ b/app/security.py @@ -173,13 +173,22 @@ def ensure_seed(db: Session) -> list[str]: db.add(UserRole(user_id=user.id, role_id=admin_role.id)) notes.append(f"已创建管理员账号 {config.ADMIN_USERNAME}(请尽快在用户设置里改密码)") else: - # 保证系统里至少有一个能管权限的账号,不会出现把自己锁在门外的情况 + # 保证系统里至少有一个「启用 + 能管权限」的账号,不会把自己锁在门外。 + # 兜底顺序:启但没挂管理员的最早账号 → 全员停用时启用最早账号;插入前先查重,避免撞 user_roles 唯一键。 guarded = [u for u in db.query(User).filter(User.active.is_(True)).all() if u.menus.get("rbac") == "rw"] if not guarded: - first = db.query(User).order_by(User.id).first() - db.add(UserRole(user_id=first.id, role_id=admin_role.id)) - notes.append(f"已把「管理员」角色补给 {first.username},避免没人能管权限") + active = db.query(User).filter(User.active.is_(True)).order_by(User.id).all() + target = active[0] if active else db.query(User).order_by(User.id).first() + if target is not None: + dup = db.query(UserRole).filter_by(user_id=target.id, role_id=admin_role.id).first() + if not dup: + db.add(UserRole(user_id=target.id, role_id=admin_role.id)) + revived = "" + if not target.active: + target.active = True + revived = "并重新启用" + notes.append(f"已把「管理员」角色{revived}补给 {target.username},避免没人能管权限") db.commit() return notes @@ -226,19 +235,23 @@ def guard(request: Request, db: Session = Depends(get_db)) -> User | None: if request.method not in READ_METHODS and user.menus.get("rbac") != "rw": raise HTTPException(status_code=403, detail=f"「{MENU_LABELS['rbac']}」是只读权限,不能修改") return user - # 录入页跨菜单依赖:录入定制项目必须能读定制元数据 / 项目清单并写定制交付记录。 - # 只给「更新录入」权限的账号也要能完成录入,这三个接口按 custom / intake 两者中较高的级别放行; + # 录入页跨菜单依赖:录入定制项目必须能读定制元数据 / 项目清单,并写交付记录(含 AI 整理 / AI 落库)。 + # 只给「更新录入」权限的账号也要能完成录入,这几个接口按 custom / intake 两者中较高的级别放行; # 定制项目其余接口(详情、修改、归档……)仍严格走 custom 菜单。 - assist = ((request.method in READ_METHODS and path in ("/api/custom/meta", "/api/custom/projects")) - or (request.method == "POST" and path == "/api/custom/updates")) + assist_reads = ("/api/custom/meta", "/api/custom/projects") + assist_writes = ("/api/custom/updates", "/api/custom/ai/parse", "/api/custom/ai/commit") + assist = ((request.method in READ_METHODS and path in assist_reads) + or (request.method == "POST" and path in assist_writes)) if assist: need_write = request.method not in READ_METHODS have = max((user.menus.get(m, "") for m in ("custom", "intake")), key=lambda x: LEVEL_RANK.get(x, 0)) if have and (not need_write or have == "rw"): return user - raise HTTPException(status_code=403, - detail=f"录入定制项目需要「{MENU_LABELS['intake']}」或「{MENU_LABELS['custom']}」的可编辑权限") + pair = f"「{MENU_LABELS['intake']}」或「{MENU_LABELS['custom']}」" + raise HTTPException(status_code=403, detail=( + f"操作定制项目需要{pair}的可编辑权限" if need_write + else f"查看定制项目需要{pair}的查看权限")) rule = resolve_rule(path, request.method) if rule is None: return user diff --git a/tests/test_api_contract.py b/tests/test_api_contract.py index 3f41ec8..6f603f0 100644 --- a/tests/test_api_contract.py +++ b/tests/test_api_contract.py @@ -63,6 +63,12 @@ def test_录入页跨菜单依赖(h): assert c.get("/api/custom/projects").status_code == 200 r = c.post("/api/custom/updates", json={"custom_project_id": cp["id"], "period_id": pid, "content": "只凭录入权限写的交付记录"}) assert r.status_code == 200, r.text + # AI 录入两路也在 assist 里:录入页 AI 整理 / 落库对「只更新录入」账号要通 + pr = c.post("/api/custom/ai/parse", json={"custom_project_id": cp["id"], "period_id": pid, "raw_text": "本周完成部署", "use_ai": False}) + assert pr.status_code == 200, pr.text + parsed = pr.json()["parsed"] + cm = c.post("/api/custom/ai/commit", json={"custom_project_id": cp["id"], "period_id": pid, "raw_text": "本周完成部署", "parsed": parsed, "mode": "rule"}) + assert cm.status_code == 200, cm.text # 定制项目本体操作不在 assist 白名单里 assert c.get(f"/api/custom/projects/{cp['id']}").status_code == 403 assert c.post("/api/custom/projects", json={"name": "越界新建"}).status_code == 403 @@ -124,3 +130,72 @@ def test_ensure_columns_补列与幂等(h): assert ensure_columns(eng, md) == ["demo.extra"] assert "extra" in {c["name"] for c in inspect(eng).get_columns("demo")} assert ensure_columns(eng, md) == [] # 幂等 + +def test_STALE_COLUMNS白名单与模型不冲突(h): + """守卫:白名单里登记的「死列」必须真的不在模型里——防止将来误把活列登记进去, + 启动时被 DROP COLUMN 无声清掉。清理完成后也应把条目移出白名单。""" + from app.db import Base + from app.schema_patch import STALE_COLUMNS + for table, col in STALE_COLUMNS: + t = Base.metadata.tables.get(table) + assert t is None or col not in t.columns, f"{table}.{col} 还在模型里,不该登记为死列" + + +def test_drop_stale_columns_索引挡路只警告不崩(tmp_path): + """DROP COLUMN 撞上索引/唯一约束是老库常态:逐列 try,跳过继续,绝不掀翻启动。""" + from sqlalchemy import create_engine, inspect, text + from app.schema_patch import STALE_COLUMNS, drop_stale_columns + table, col = STALE_COLUMNS[0] + # ① 列上有索引 → 返回 [](跳过),不抛 + e1 = create_engine(f"sqlite:///{tmp_path / 'idx.db'}") + with e1.begin() as conn: + conn.execute(text(f"CREATE TABLE {table} (id INTEGER PRIMARY KEY, {col} TEXT)")) + conn.execute(text(f"CREATE INDEX ix_stale_{col} ON {table}({col})")) + assert drop_stale_columns(e1) == [] + assert col in {c["name"] for c in inspect(e1).get_columns(table)} + # ② 干净列 → 删掉且幂等;③ 表不存在 → 静默跳过 + e2 = create_engine(f"sqlite:///{tmp_path / 'ok.db'}") + with e2.begin() as conn: + conn.execute(text(f"CREATE TABLE {table} (id INTEGER PRIMARY KEY, {col} TEXT)")) + assert drop_stale_columns(e2) == [f"{table}.{col}"] + assert drop_stale_columns(e2) == [] + e3 = create_engine(f"sqlite:///{tmp_path / 'empty.db'}") + assert drop_stale_columns(e3) == [] + + +def test_assist_只读与写操作报错分句(h): + """零权限账号读 assist 接口 → 提示「查看权限」;只读账号写 → 提示「可编辑权限」。""" + z = h.user_client("assist_zero", menus={}) + r = z.get("/api/custom/meta") + assert r.status_code == 403 and "查看" in r.json()["detail"] and "编辑" not in r.json()["detail"] + ro = h.user_client("assist_ro", menus={"intake": "ro"}) + assert ro.get("/api/custom/meta").status_code == 200 # 读走较高级别,ro 就够 + r = ro.post("/api/custom/updates", json={"custom_project_id": 1, "period_id": 1, "content": "x"}) + assert r.status_code == 403 and "可编辑" in r.json()["detail"] + # 编辑/删除单条交付记录不属 assist 三接口,仍严格走 custom + assert ro.patch("/api/custom/updates/1", json={"content": "y"}).status_code == 403 + assert ro.delete("/api/custom/updates/1").status_code == 403 + + +def test_写操作抛500也留审计(h): + """业务代码抛穿中间件时,写操作必须按 status=500 补一条,不能丢痕。""" + from fastapi import FastAPI + from fastapi.testclient import TestClient + from starlette.routing import Mount + api_app = next(rt.app for rt in h.app.routes if isinstance(rt, Mount) and rt.path == "/api") + + def boom(): + raise RuntimeError("测试炸点") + + route = api_app.router.routes + before = len(route) + api_app.add_route("/boom-test", boom, methods=["POST"]) + try: + c = TestClient(h.app, raise_server_exceptions=False) + c.post("/api/boom-test") + finally: + del route[before:] + logs = h.admin.get("/api/rbac/logs", params={"action": "create"}).json() + hit = [x for x in logs["items"] if x["path"] == "/api/boom-test"] + assert hit and hit[0]["status"] == 500 + diff --git a/tests/test_rbac.py b/tests/test_rbac.py index 5e0b35f..429e5ef 100644 --- a/tests/test_rbac.py +++ b/tests/test_rbac.py @@ -14,7 +14,7 @@ def test_权限元信息给全前端要的三样(h): assert all(m["label"] for m in meta["menus"]) assert [lv["value"] for lv in meta["levels"]] == ["rw", "ro", ""] assert {a["value"] for a in meta["actions"]} == { - "create", "update", "delete", "login", "logout", "password", "revoke"} + "create", "update", "delete", "login", "logout", "password", "revoke", "deny"} def test_内置角色与管理员账号自动就位(h): @@ -300,27 +300,57 @@ def test_写操作与越权尝试都留痕并能筛选(h): # 被拒的那次删除也留了痕,说明里带上了状态码 denied_role = [x for x in logs["items"] if x["status"] == 400] assert denied_role and "HTTP 400" in denied_role[0]["detail"] - # 越权尝试同样进日志,并带上被拒的说明 + # 越权尝试同样进日志:一律记 action=deny(含 GET),并带上被拒的说明 h.user("bad_try", menus={"board": "ro"}) c = h.login("bad_try") assert c.post("/api/projects", json={"name": "越权"}).status_code == 403 + assert c.get("/api/projects").status_code == 403 # 只读越权探测也要留痕 denied = h.admin.get("/api/rbac/logs", - params={"username": "bad_try", "action": "create"}).json() - assert denied["total"] == 1 - assert denied["items"][0]["status"] == 403 and "HTTP 403" in denied["items"][0]["detail"] - assert denied["items"][0]["menu"] == "projects" and denied["items"][0]["action"] == "create" + params={"username": "bad_try", "action": "deny"}).json() + assert denied["total"] == 2 + assert {x["method"] for x in denied["items"]} == {"POST", "GET"} + assert all(x["status"] == 403 and "HTTP 403" in x["detail"] for x in denied["items"]) + assert {x["menu"] for x in denied["items"]} == {"projects"} + assert not h.admin.get("/api/rbac/logs", + params={"username": "bad_try", "action": "create"}).json()["items"] # 失败的登录也按被尝试的账号记一笔 h.anon.post("/api/auth/login", json={"username": "bad_try", "password": "错的答案"}) by_action = h.admin.get("/api/rbac/logs", params={"action": "login"}).json() assert any(x["status"] == 401 and "登录失败" in (x["detail"] or "") for x in by_action["items"]) - # 筛选参数与分页 - assert h.admin.get("/api/rbac/logs", params={"action": "revoke"}).json()["total"] == 0 + # 筛选参数与分页:强制下线按语义记 revoke(不再被 POST→create 吞掉) + uid = [x["id"] for x in h.admin.get("/api/rbac/users").json() if x["username"] == "bad_try"][0] + assert h.admin.post(f"/api/rbac/users/{uid}/revoke", json={}).status_code == 200 + revoked = h.admin.get("/api/rbac/logs", params={"action": "revoke"}).json() + assert revoked["total"] >= 1 and revoked["items"][0]["path"].endswith("/revoke") page1 = h.admin.get("/api/rbac/logs", params={"limit": 1, "offset": 0}).json() page2 = h.admin.get("/api/rbac/logs", params={"limit": 1, "offset": 1}).json() assert page1["total"] == page2["total"] and page1["items"][0]["id"] != page2["items"][0]["id"] assert h.admin.get("/api/rbac/logs", params={"days": 0}).status_code == 422 +def test_ensure_seed_锁死自救(h, db): + """「至少一人能管权限」兜底:优先补给启用账号;全员停用则复活最早账号; + 重复跑不炸(查重后插入,不撞 user_roles 唯一键)。""" + from app import security + from app.models import User, UserRole + backup = h.user("seed_backup") # 启用、无 rbac 权限 + admin = db.query(User).filter(User.username == "admin").one() # 接口不许自停用,直改库模拟 + admin.active = False + db.commit() + notes = security.ensure_seed(db) + assert any("seed_backup" in n for n in notes), notes + # 再跑一次:seed_backup 已是 rw,不重复挂角色也不报错 + assert security.ensure_seed(db) == [] + # 全员停用 → 复活最早账号(admin),且不再重复插角色 + db.query(User).update({User.active: False}) + db.commit() + notes = security.ensure_seed(db) + assert any("重新启用" in n for n in notes), notes + db.expire_all() + assert db.get(User, admin.id).active is True + assert db.query(UserRole).filter_by(user_id=admin.id).count() >= 1 + + def test_清空日志(h): h.role("要被记一笔", {"board": "ro"}) assert h.admin.get("/api/rbac/logs").json()["total"] >= 1