fix: 5 个安全/正确性问题(第三轮)

1. (High) 启动加载历史消息无用户过滤
   → 有 current_user_id 时按用户过滤
2. (High) 长期记忆跨用户共享
   → read_for/write_for 按 user_id 隔离
3. (Medium) 摘要读取无用户过滤 — 待后续
4. (Medium) WeChat API 未检查错误
   → notifyStart 检查 ret!=0
   → getUpdates 检查 ret/errcode
   → post_json 加 error_for_status()
5. (Low) set_auth 未更新 base_url
   → &mut self + 实际赋值
This commit is contained in:
2026-06-02 11:54:34 +08:00
parent 6dd12425d6
commit 7ab5052642
4 changed files with 56 additions and 31 deletions
+15 -3
View File
@@ -219,6 +219,10 @@ impl WeChatClient {
.await
.map_err(|e| format!("解析 notifyStart 响应失败: {}", e))?;
if _resp.ret != 0 {
return Err(format!("notifyStart 失败: ret={} errmsg={:?}", _resp.ret, _resp.errmsg));
}
info!("监听器已注册");
Ok(())
}
@@ -257,7 +261,14 @@ impl WeChatClient {
.post_json(&url, &body, Some(&token))
.await
{
Ok(resp) => resp.json().await.map_err(|e| format!("解析 getUpdates 响应失败: {}", e))?,
Ok(resp) => {
let resp: GetUpdatesResp = resp.json().await.map_err(|e| format!("解析 getUpdates 响应失败: {}", e))?;
if resp.ret != 0 || resp.errcode.unwrap_or(0) != 0 {
tracing::warn!("getUpdates 返回错误: ret={} errcode={:?} errmsg={:?}",
resp.ret, resp.errcode, resp.errmsg);
}
resp
}
Err(e) => {
// 如果超时,返回空响应
if e.contains("超时") || e.contains("timeout") {
@@ -318,11 +329,11 @@ impl WeChatClient {
}
/// 设置 auth 状态(从持久化恢复)
pub async fn set_auth(&self, token: &str, account_id: &str, base_url: &str) {
pub async fn set_auth(&mut self, token: &str, account_id: &str, base_url: &str) {
*self.token.lock().await = token.to_string();
*self.account_id.lock().await = account_id.to_string();
if !base_url.is_empty() {
// 只更新 base_url 如果传入了非空值
self.base_url = base_url.to_string();
}
}
@@ -425,6 +436,7 @@ impl WeChatClient {
.body(body_str)
.send()
.await
.and_then(|r| r.error_for_status())
.map_err(|e| {
if e.is_timeout() {
format!("请求超时: {}", url)