Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(wecom): guard cached image reuse

davidb7622d9be61e
diff --git a/apps/desktop/src/main/im/host.ts b/apps/desktop/src/main/im/host.ts
index 57e8288624..c37c60687e 100644
--- a/apps/desktop/src/main/im/host.ts
+++ b/apps/desktop/src/main/im/host.ts
@@ -104,9 +104,13 @@ const host: IMHost = {
mimeType: hit.mimeType,
};
},
- getCachedImage: async (integration, token) => {
+ getCachedImage: async (integration, token, options) => {
const hit = await integrationCacheGet(integrationCacheKey(integration, token));
if (!hit) return null;
+ // Cache lookup can outlive logout/account replacement. Re-check the
+ // transport-owned boundary immediately before pinning so stale media is
+ // not promoted without a message/session owner.
+ if (options?.shouldReuse?.() === false) return null;
// 命中路径不走 cacheImage,但 IM 复用的可能是 MCP 侧 isCache=true 的缓存
// blob(feishu 两边有意共用 `feishu:<token>` 命名空间)——IM 语义是用户
// 附件,同 cacheImage 口径降级为非 cache(review P1);降级失败只警告,
diff --git a/packages/lizi-im/src/types.ts b/packages/lizi-im/src/types.ts
index ef330252f7..a285c82829 100644
--- a/packages/lizi-im/src/types.ts
+++ b/packages/lizi-im/src/types.ts
@@ -105,6 +105,10 @@ export interface IMHostMediaCache {
getCachedImage(
integration: 'feishu' | 'discord' | 'telegram' | 'dingtalk' | 'wecom',
token: string,
+ options?: {
+ /** Re-check transport/account ownership after the async lookup, before host-side pinning. */
+ shouldReuse?: () => boolean;
+ },
): Promise<{ absPath: string; url: string; mimeType: string } | null>;
/** host 托管媒体 URL(cindy-media://)→ 绝对路径;认不出返回 null(出站上传用)。 */
resolveMediaUrl(url: string): string | null;
diff --git a/packages/lizi-im/src/wecom/__tests__/index.test.ts b/packages/lizi-im/src/wecom/__tests__/index.test.ts
index 032b053127..84d98fce36 100644
--- a/packages/lizi-im/src/wecom/__tests__/index.test.ts
+++ b/packages/lizi-im/src/wecom/__tests__/index.test.ts
@@ -534,6 +534,68 @@ describe("WecomIM routing and ownership", () => {
expect(cacheImage).not.toHaveBeenCalled();
});
+ it("does not pin or download a cached image after the owning account boundary closes", async () => {
+ const { host, secrets } = createHost();
+ secrets.set("wecom-owner-user-id", "owner");
+ const accountToken = { id: 1 };
+ let accountActive = true;
+ host.accountScope = {
+ capture: () => (accountActive ? accountToken : null),
+ isCurrent: (token) => accountActive && token === accountToken,
+ run: async (_token, operation) => operation(),
+ };
+ const client = new FakeClient();
+ const lookup = deferred<{
+ absPath: string;
+ url: string;
+ mimeType: string;
+ }>();
+ const pinCachedImage = vi.fn();
+ const getCachedImage = vi.fn(
+ async (
+ _integration: "wecom",
+ _token: string,
+ options?: { shouldReuse?: () => boolean },
+ ) => {
+ const cached = await lookup.promise;
+ if (options?.shouldReuse?.() === false) return null;
+ pinCachedImage();
+ return cached;
+ },
+ );
+ host.media = {
+ getCachedImage,
+ cacheImage: vi.fn(),
+ resolveMediaUrl: vi.fn(() => null),
+ };
+ const im = new WecomIM(host, { clientFactory: () => client as never });
+
+ await im.init();
+ client.emit("message.image", {
+ body: {
+ msgid: "closed-account-cached-image",
+ aibotid: "bot-1",
+ chattype: "single",
+ from: { userid: "owner" },
+ msgtype: "image",
+ image: { url: "https://example.invalid/image" },
+ },
+ });
+ await vi.waitFor(() => expect(getCachedImage).toHaveBeenCalledOnce());
+
+ accountActive = false;
+ lookup.resolve({
+ absPath: "C:\\managed\\photo.jpg",
+ url: "cindy-media://image",
+ mimeType: "image/jpeg",
+ });
+ await flush();
+ await flush();
+
+ expect(pinCachedImage).not.toHaveBeenCalled();
+ expect(client.downloadFile).not.toHaveBeenCalled();
+ });
+
it("does not persist an inbound file after the transport generation changes", async () => {
const { host, secrets } = createHost();
secrets.set("wecom-owner-user-id", "owner");
diff --git a/packages/lizi-im/src/wecom/index.ts b/packages/lizi-im/src/wecom/index.ts
index 7c31297660..4ca8c1a399 100644
--- a/packages/lizi-im/src/wecom/index.ts
+++ b/packages/lizi-im/src/wecom/index.ts
@@ -742,7 +742,10 @@ export class WecomIM extends BaseIM implements TextChannelIM {
image: { url: string; aeskey?: string },
): Promise<IMAttachment | null> {
const token = `${messageId}:${index}`;
- const cached = await this.host.media?.getCachedImage("wecom", token);
+ const cached = await this.host.media?.getCachedImage("wecom", token, {
+ shouldReuse: () => this.isCurrent(client, generation),
+ });
+ if (!this.isCurrent(client, generation)) return null;
if (cached) {
return {
kind: "image",