From 42ecd79e19e1e1029228ad605e424f73ee868a42 Mon Sep 17 00:00:00 2001 From: Jidekin Date: Thu, 19 Nov 2026 08:23:15 +0800 Subject: [PATCH] =?UTF-8?q?fix(config,test):=20=E8=A1=A5=20auth=5Ffail=5Fb?= =?UTF-8?q?an=5Fseconds=20=E8=BF=9B=E9=9D=A2=E6=9D=BF=20+=20=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=B8=8E=E5=B0=81=E7=A6=81=E4=BA=92=E4=B8=8D=E5=B9=B2?= =?UTF-8?q?=E6=89=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 上一轮「封禁列表」功能的两个收尾漏洞(跑测试时暴露): 1. 配置项漏搬面板:server.auth_fail_ban_seconds 在 yaml 与 config.py 里都有, 但 settings.py 的 _SERVER_KEYS / config_to_view 没带上 → 面板保存一次配置 就会把它写丢(回退默认 3600)。test_config_view_masks_secrets_and_roundtrips 正是这道闸。 2. 测试被自己的封禁功能打红:测试会故意制造大量鉴权失败,而「鉴权失败自动封禁」 会在同一进程内把 testclient 拉黑 60 分钟,导致后半场测试全变 429 (test_disabled_flag... / test_download_rejects... 等)。测试启动时关掉封禁 (只保留限流)并清空计数,生产逻辑不变。 3. 顺手更新过时样本:官方把 longcat-2.5-preview-free 改名成 longcat-2.5-free, 免费模型测试的样本改成"现网确实免费的那几个之一",避免上游改名再把测试打红。 效果(tests/test_gateway.py 直跑):改动前 206 passed / 19 failed, 改动后 214 passed / 11 failed。剩余 11 个为既有失败(日志/账号/视频/CORS 等, 多为跨用例状态污染,单独复现 test_full_view_put_rewrites_nothing 为 0 差异), 不在本轮范围。 --- app/settings.py | 6 ++++++ tests/test_gateway.py | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/settings.py b/app/settings.py index d44b74f..fd230b5 100644 --- a/app/settings.py +++ b/app/settings.py @@ -141,6 +141,9 @@ def config_to_view(cfg: GatewayConfig) -> dict[str, Any]: "webhook_allow_private": s.webhook_allow_private, "docs_enabled": s.docs_enabled, "auth_fail_limit_per_min": s.auth_fail_limit_per_min, + # 2026-11-19 补:封禁功能(2026-11-06)加了配置项但面板视图没跟上, + # 一旦在面板里保存配置,这一行就会被写丢(回退成默认 3600)。 + "auth_fail_ban_seconds": s.auth_fail_ban_seconds, }, "auth": {"session_ttl_minutes": cfg.auth.session_ttl_minutes}, "circuit": { @@ -479,6 +482,9 @@ _SERVER_KEYS = ( "host", "port", "timeout", "retry", "health_auth", "hot_reload", "hot_reload_interval", "metrics_enabled", "rate_limit_rps", "max_body_size", "cors_origins", "graceful_shutdown", "batch_max_concurrency", "batch_max_items", "webhook_allow_private", "docs_enabled", + # 2026-11-19 补:封禁功能(2026-11-06)落盘了这两个键但忘了搬上面板, + # 导致「文件里有、面板里没有」—— 保存一次面板就会把它们丢掉。 + "auth_fail_limit_per_min", "auth_fail_ban_seconds", "auth_fail_limit_per_min", ) _CIRCUIT_KEYS = ("enabled", "failure_threshold", "cooldown", "window") diff --git a/tests/test_gateway.py b/tests/test_gateway.py index 1dc0e4c..5e82e7f 100644 --- a/tests/test_gateway.py +++ b/tests/test_gateway.py @@ -61,6 +61,16 @@ if _TC is not None: _AUTOBEARER = "" try: import app.main as _m + # 2026-11-19:测试会**故意**制造大量鉴权失败,而 2026-11-06 起的「鉴权失败自动封禁」 + # 会在同一进程内把 testclient 这个 IP 拉黑 60 分钟 → 后半场测试全变 429。 + # 这是测试隔离问题,不是被测行为:测试里关掉封禁(限流仍生效),生产逻辑不动。 + try: + _m._cfg().server.auth_fail_ban_seconds = 0 + _m._BANNED.clear() + _m._BANNED_FOREVER.clear() + _m._AUTH_FAILS.clear() + except Exception: + pass _c = _TC(_m.app) _r = _c.post("/auth/login", json={"username": "admin", "password": "admin"}) if _r.status_code == 200: @@ -1716,7 +1726,10 @@ def test_models_payload_marks_free_models(): # 2026-10-12:样本从 space-bunny-free 换成 longcat-2.5-preview-free —— 前者已于 # 2026-10-07 被官方改名成 space-bunny 并转收费(价格表 $0.15/$0.60), # 每日同步据此去掉了它的 free 标记(现网免费位=longcat-2.5-preview-free / step-5-preview-free)。 - assert "longcat-2.5-preview-free" in free_ids, f"免费模型没标出来:{free_ids}" + # 2026-11-19:官方把 longcat-2.5-preview-free 改名为 longcat-2.5-free(每日同步已跟进), + # 样本改成「现网确实免费的那几个之一」,避免上游改名再把测试打红。 + assert any(x in free_ids for x in ("longcat-2.5-free", "hy4-flash-free", + "iceman-contributor-free")), f"免费模型没标出来:{free_ids}" # 反向:付费模型必须是 free=false(不能因为字段缺失而误标) paid = next(x for x in data if x["id"] == "deepseek-v4.1-flash") assert paid["free"] is False, paid