[Bug] 平台适配器 logo 破图:`logo_token` 是一次性令牌,被前端当静态资源重复请求后即 404
问题描述
WebUI「机器人 → 消息平台」页面中,声明了 logo_path 的插件平台适配器其 logo 显示为破图(<img> 加载失败,回退显示 alt 文本 "bot")。同一个 logo 文件本身没有问题——路径解析正确、文件存在、注册也成功,破图发生在图片被第二次请求的时候。
影响面:所有声明 logo_path 的平台适配器插件(不是某一个插件的问题)。内置适配器全部未声明 logo_path,因此这条路径此前一直没被触发。
根因:三个环节叠加。
1. 令牌是一次性的(astrbot/core/file_token_service.py)
async def check_token_expired(self, file_token: str) -> bool:
async with self.lock:
await self._cleanup_expired_tokens()
return file_token not in self.staged_files # 只看 token 在不在
async def handle_file(self, file_token: str) -> str:
"""根据令牌获取文件路径,使用后令牌失效。"""
...
file_path, _ = self.staged_files.pop(file_token) # 取用即失效
于是「已被消费」恰好等价于「已失效」。
2. 平台表单侧命中缓存就直接复用,不校验存活(astrbot/dashboard/services/config_service.py:1042-1050)
cache_key = f"{platform.name}:{platform.logo_path}"
if cache_key in self._logo_token_cache:
self._set_platform_logo_token(
platform_default_tmpl,
platform.name,
self._logo_token_cache[cache_key], # ← 直接复用,可能已被消费
)
logger.debug(f"Using cached logo token for platform {platform.name}")
return
注册时用的是 timeout=3600,而缓存 _logo_token_cache 存活更久,于是每次读配置都会把一个已作废的 token 重新发给前端,永远不会重新注册。
3. 对照:插件列表侧的写法是正确的(astrbot/dashboard/services/plugin_service.py:283-290)
if token := self._logo_cache.get(logo_path):
if not await file_token_service.check_token_expired(token):
return token # ← 先校验存活
token = await file_token_service.register_file(logo_path, timeout=300)
同一个 FileTokenService,一侧校验、一侧不校验 —— 这就是插件列表 logo 正常、平台表单 logo 破图的原因。
4. 前端把它当静态资源地址(dashboard/src/api/v1.ts:1019、dashboard/src/views/PlatformPage.vue:431-441)
tokenUrl(fileToken: string) { return `/api/v1/files/tokens/${encodeURIComponent(fileToken)}`; }
<img :src="getPlatformIconFor(platform)" />
浏览器对同一个 URL 会重复发起请求(列表项 + 选中项 + 重渲染),而服务端只允许成功一次。
建议方向:复用缓存前先 await file_token_service.check_token_expired(cached_token),失效则重新注册(代价是多读一次几 KB 的小文件,可忽略)。
AstrBot 版本
v4.28.2(现象在 v4.28.1 与 v4.28.2 均存在;代码位置与行号核对于 master @ 6d50e1e3)
错误日志
服务端不会为此打日志 —— 令牌失效直接转成 404 返回:
# astrbot/dashboard/api/files.py:39-43
async def _serve_token_file(file_token: str, service: FileService):
try:
return FileResponse(await service.resolve_token_file(file_token))
except FileServiceError as exc:
raise HTTPException(status_code=404) from exc # 不打日志
浏览器 Network 可见:
GET /api/v1/files/tokens/fb84a267-2919-4b16-b652-dada82b22eee -> 404
异常消息(astrbot/dashboard/services/file_service.py:11-15 将 KeyError 转为 FileServiceError):
Invalid or expired file token: fb84a267-2919-4b16-b652-dada82b22eee
检查清单
- [x] 已在 Issue 列表中搜索过相关问题,未发现同一问题
- [x] 已尝试禁用所有插件 —— 触发条件需要一个声明
logo_path的平台适配器插件(内置适配器均未声明该字段,因此这条路径此前未被触发);但缺陷位于核心的令牌消费逻辑(dashboard/services与file_token_service),并非插件代码的问题 - [x] 报告的问题与 AstrBot 本体相关(缺陷位于
dashboard/services与file_token_service,非插件代码) - [x] 已阅读并同意贡献者行为准则