"""用户侧水平越权 / 密钥外流的回归自测。 覆盖: - V1 新注册者不得继承 Hub 上已存在的 user_id 命名空间(发号跳过占用号段) - V2 Hub 恢复文件 meta 时校验 uploaded_by,外人不洗白 - V2 跨用户下载 / 删除一律 404(不是 403,避免 key 存在性 oracle) - V6 下载成功 / 越权拒绝都落审计 - V8 XTC_ALLOW_REGISTER=0 关注册、XTC_REGISTER_INVITE 校验邀请码 运行:python _security_selftest.py 全程不联网:Hub 相关函数被替换为内存实现。 """ from __future__ import annotations import asyncio import os import sys import tempfile _TMP = tempfile.mkdtemp(prefix="xtc_selftest_") os.environ["XTC_DB_PATH"] = os.path.join(_TMP, "xtc.db") os.environ["XTC_BUCKET_DIR"] = os.path.join(_TMP, "bucket") os.environ["XTC_DEV_MODE"] = "1" os.environ["XTC_JWT_SECRET"] = "selftest-secret-0123456789abcdef" for _k in ("HF_TOKEN", "HF_CONFIG_REPO", "XTC_HF_TOKEN", "XTC_HF_CONFIG_REPO"): os.environ.pop(_k, None) # 本地缓存写到临时目录,别污染盘根 from app import hf_storage # noqa: E402 hf_storage._local_root = lambda: __import__("pathlib").Path(_TMP) / "bucket" # type: ignore[assignment] (hf_storage._local_root()).mkdir(parents=True, exist_ok=True) from fastapi.testclient import TestClient # noqa: E402 from app.api import user_files as uf # noqa: E402 from app.database import get_conn, init_db # noqa: E402 from app.main import app # noqa: E402 from app.services import usage_store, user_files_store # noqa: E402 FAILS: list[str] = [] def check(name: str, cond: bool, extra: str = "") -> None: if cond: print(f" [PASS] {name}") else: print(f" [FAIL] {name} {extra}") FAILS.append(name) def register(client: TestClient, username: str, password: str = "Passw0rd!"): r = client.post("/u/api/register", json={"username": username, "password": password}) return r class _SettingsProxy: """把真实 settings 包一层,只覆盖测试关心的字段。""" def __init__(self, real, **over): self._real = real self._over = over def __getattr__(self, item): if item in self._over: return self._over[item] return getattr(self._real, item) def main() -> int: init_db() # 干净起点 with get_conn() as conn: for t in ("file_meta", "user_accounts", "user_tokens", "audit_log"): conn.execute(f"DELETE FROM {t}") client = TestClient(app) print("\n[1] 注册两个用户(alice / bob),确认 id 不同") ra = register(client, "alice") rb = register(client, "bob") check("alice 注册成功", ra.status_code == 200, str(ra.text[:200])) check("bob 注册成功", rb.status_code == 200, str(rb.text[:200])) ida = int(ra.json()["user"]["id"]) idb = int(rb.json()["user"]["id"]) tok_a = ra.json()["token"] tok_b = rb.json()["token"] check("id 唯一", ida != idb, f"{ida} vs {idb}") print("\n[2] alice 上传文件,bob 拿 key 去下载 → 必须 404 且落审计") up = client.post( "/u/api/upload", headers={"Authorization": f"Bearer {tok_a}"}, files={"file": ("secret.txt", b"alice-private-content", "text/plain")}, ) check("上传成功", up.status_code == 200, str(up.text[:300])) key = up.json().get("key", "") check("拿到 key", bool(key), str(up.text[:200])) own = client.get(f"/u/api/files/{key}", headers={"Authorization": f"Bearer {tok_a}"}) check("本人下载 200", own.status_code == 200, str(own.status_code)) check("本人下载内容正确", own.content == b"alice-private-content") cross = client.get(f"/u/api/files/{key}", headers={"Authorization": f"Bearer {tok_b}"}) check("跨用户下载 404(不是 403)", cross.status_code == 404, str(cross.status_code)) check("跨用户下载不泄露内容", b"alice-private-content" not in cross.content) rej = usage_store.list_audit(action="user.file.download_rejected", hours=1) check("越权下载已审计", len(rej) >= 1, f"rows={len(rej)}") okdl = usage_store.list_audit(action="user.file.download", hours=1) check("正常下载已审计", len(okdl) >= 1, f"rows={len(okdl)}") cross_del = client.delete(f"/u/api/files/{key}", headers={"Authorization": f"Bearer {tok_b}"}) check("跨用户删除 404", cross_del.status_code == 404, str(cross_del.status_code)) still = client.get(f"/u/api/files/{key}", headers={"Authorization": f"Bearer {tok_a}"}) check("越权删除后文件仍在", still.status_code == 200, str(still.status_code)) print("\n[3] V2:Hub 恢复时丢弃 uploaded_by mismatch 的 meta") foreign_meta = { "key": "deadbeef" * 8, "filename": "victim.bin", "alias": "victim", "mime": "application/octet-stream", "size": 11, "sha256": "00" * 32, "uploaded_at": 1, "uploaded_by": str(ida), # 属于 alice } orig_metas = user_files_store.restore_file_metas orig_hub_enabled = uf.is_hub_enabled async def fake_metas(user_id, _m=foreign_meta): return [dict(_m)] user_files_store.restore_file_metas = fake_metas # type: ignore[assignment] uf.is_hub_enabled = lambda: True # type: ignore[assignment] restored = asyncio.run(uf._restore_files_from_hub_async(str(idb))) check("bob 恢复 alice 的 meta 被全部拒绝", restored == 0, f"restored={restored}") with get_conn() as conn: row = conn.execute( "SELECT uploaded_by FROM file_meta WHERE key = ?", (foreign_meta["key"],) ).fetchone() check("被拒的 meta 未落库", row is None, str(dict(row) if row else None)) rej2 = usage_store.list_audit(action="restore.ownership_rejected", hours=1) check("恢复拒绝已审计", len(rej2) >= 1, f"rows={len(rej2)}") print("\n[4] V2:download 走 Hub 单条恢复分支也不能洗白") r = client.get(f"/u/api/files/{foreign_meta['key']}", headers={"Authorization": f"Bearer {tok_b}"}) check("Hub 恢复分支跨用户 404", r.status_code == 404, str(r.status_code)) r2 = client.get(f"/u/api/files/{foreign_meta['key']}", headers={"Authorization": f"Bearer {tok_a}"}) check("Hub 恢复分支本人可用(200 或 410,取决于内容是否存在)", r2.status_code in (200, 410), str(r2.status_code)) user_files_store.restore_file_metas = orig_metas # type: ignore[assignment] uf.is_hub_enabled = orig_hub_enabled # type: ignore[assignment] print("\n[5] V1:发号跳过 Hub 上已被占用的 user_id") orig_occ = uf._hub_occupied_user_ids orig_enabled = uf.is_hub_enabled uf._hub_occupied_user_ids = lambda: {1, 3, 9} uf.is_hub_enabled = lambda: True # type: ignore[assignment] with get_conn() as conn: local_max = int(conn.execute("SELECT MAX(id) AS m FROM user_accounts").fetchone()["m"] or 0) nxt = uf._next_safe_user_id() check("新 id 大于本地最大值", nxt > local_max, f"next={nxt} local_max={local_max}") check("新 id 避开所有已占用号", nxt not in {1, 3, 9}, f"next={nxt}") check("新 id == max(占用, 本地)+1", nxt == max(local_max, 9) + 1, f"next={nxt}") # 真注册一次,确认写入的是安全 id rc = register(client, "carol") check("carol 注册成功", rc.status_code == 200, str(rc.text[:200])) check("carol 的 id 未被占用号污染", int(rc.json()["user"]["id"]) > max(local_max, 9), str(rc.json().get("user"))) files_c = client.get("/u/api/files", headers={"Authorization": f"Bearer {rc.json()['token']}"}) check("carol 看不到任何文件", files_c.status_code == 200 and files_c.json()["count"] == 0, str(files_c.text[:200])) uf._hub_occupied_user_ids = orig_occ # type: ignore[assignment] uf.is_hub_enabled = orig_enabled # type: ignore[assignment] print("\n[6] V8:注册开关 + 邀请码") orig_get_settings = uf.get_settings real = orig_get_settings() uf.get_settings = lambda: _SettingsProxy(real, allow_register=False) # type: ignore[assignment] r_off = register(client, "dave") check("关注册后 403", r_off.status_code == 403, str(r_off.status_code)) uf.get_settings = lambda: _SettingsProxy( # type: ignore[assignment] real, allow_register=True, register_invite="invite-xyz" ) r_bad = register(client, "dave") check("邀请码错误 403", r_bad.status_code == 403, str(r_bad.status_code)) r_ok = client.post( "/u/api/register", json={"username": "dave", "password": "Passw0rd!", "invite": "invite-xyz"}, ) check("邀请码正确 200", r_ok.status_code == 200, str(r_ok.text[:200])) uf.get_settings = orig_get_settings # type: ignore[assignment] print("\n[7] 注销护栏:缺 password 必须 400") r_del = client.delete("/u/api/account", headers={"Authorization": f"Bearer {tok_b}"}) check("缺 password 400", r_del.status_code == 400, str(r_del.status_code)) print("\n" + "=" * 60) if FAILS: print(f"FAILED: {len(FAILS)} 项 -> {FAILS}") return 1 print("ALL PASS") return 0 if __name__ == "__main__": sys.exit(main())