fix: Cookie 先校验后落盘,坏 Cookie 不再覆盖正在使用的有效 Cookie(+2 项测试)

This commit is contained in:
houhuan committed 2026-11-16 12:53:18 +08:00
1 parent b19a071a6e
commit 92bd4e1b59
2 files changed
+38 -5

No files matched your search

+15 -5
View File
@@ -124,11 +124,15 @@ class Monitor:
log.error("读取 cookie 文件失败: %s", exc)
return None
def apply_cookies(self, cookies: Dict[str, str], source: str = "file") -> bool:
"""校验并应用 Cookie(供主循环与管理接口共用)。"""
def apply_cookies(self, cookies: Dict[str, str], source: str = "file",
validated: bool = False) -> bool:
"""应用 Cookie(供主循环与管理接口共用)。
validated=True 表示调用方已经校验过,跳过重复的网络校验。
"""
if not cookies:
return False
if not self.client.check_cookie(cookies):
if not validated and not self.client.check_cookie(cookies):
log.warning("来自 %s 的 Cookie 校验未通过", source)
return False
self.client.set_cookies(cookies)
@@ -163,11 +167,17 @@ class Monitor:
return ok
def update_cookies(self, cookies: Dict[str, str]) -> bool:
"""管理接口收到新 Cookie:落盘后立即校验生效。"""
"""管理接口收到新 Cookie:先校验再落盘,避免坏 Cookie 覆盖掉正在用的好 Cookie。"""
from .store import atomic_write_json
if not cookies:
return False
if not self.client.check_cookie(cookies):
log.warning("管理接口提交的 Cookie 校验未通过,已拒绝(保留原有 Cookie)")
return False
atomic_write_json(Path(self.cfg.paths.cookies), cookies)
self._cookie_sig = self._cookie_sig_of_file()
return self.apply_cookies(cookies, source="admin-api")
return self.apply_cookies(cookies, source="admin-api", validated=True)
# ------------------------------------------------------------------ 状态
def status(self) -> Dict[str, Any]: