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(feishu): reject stale companion recipients on personal fallback

Chris7341b8759050
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts
index 5b3b02757e..f3970a1971 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/collabSendOutcome.test.ts
@@ -22,6 +22,11 @@ const mockState = vi.hoisted(() => ({
) => ({ ok: true as const })),
capturedProvidersConfig: null as null | Record<string, unknown>,
afterSelectCompanion: null as null | (() => void),
+ personalFeishu: {
+ getOwnerOpenId: () => 'ou_personal',
+ sendMarkdownText: vi.fn(async () => ({ messageId: 'om_personal_text' })),
+ sendFile: vi.fn(async () => ({ ok: true, messageId: 'om_personal_file' })),
+ },
companionFeishu: {
getOwnerOpenId: () => 'ou_companion',
sendMarkdownText: vi.fn(async () => ({ messageId: 'om_companion_text' })),
@@ -77,10 +82,7 @@ vi.mock('../../scheduler-host/index.js', () => ({
}));
vi.mock('../../im/index.js', () => ({
- feishuIm: {
- getOwnerOpenId: () => null,
- sendFile: vi.fn(),
- },
+ feishuIm: mockState.personalFeishu,
}));
vi.mock('../../im/shared/botImConnectionRuntime', () => ({
@@ -277,6 +279,18 @@ describe('collab send outcome semantics', () => {
}
}
}
+
+ // Same-account archive removes canonical membership without changing the
+ // DB snapshot. An app-scoped recipient must not cross to personal credentials.
+ await client.exec("UPDATE bot_session_links SET role = 'history' WHERE session_id = 'background-receipt'");
+ for (const send of [
+ () => feishu.sendMessage(owner!, 'must not switch bots', session.sessionId, session),
+ () => feishu.sendFile(owner!, '/tmp/report.txt', 'report.txt', session),
+ ]) {
+ expect(await send()).toMatchObject({ ok: false, reason: 'SEND_FAIL' });
+ }
+ expect(mockState.personalFeishu.sendMarkdownText).not.toHaveBeenCalled();
+ expect(mockState.personalFeishu.sendFile).not.toHaveBeenCalled();
} finally {
commitVolatileAppSession('signed-out');
clearCurrentDbClient(client);
diff --git a/apps/desktop/src/main/mcp-integrations/__tests__/feishuTransportRouting.test.ts b/apps/desktop/src/main/mcp-integrations/__tests__/feishuTransportRouting.test.ts
index 4bdec5d6c2..5b18d5beb2 100644
--- a/apps/desktop/src/main/mcp-integrations/__tests__/feishuTransportRouting.test.ts
+++ b/apps/desktop/src/main/mcp-integrations/__tests__/feishuTransportRouting.test.ts
@@ -65,6 +65,26 @@ describe('Feishu transport with worker database', () => {
await expect(resolve({ sessionId: 'canonical' })).rejects.toThrow('Companion Feishu connection unavailable');
});
+ // PR #6443: canonical membership can disappear between recipient lookup and
+ // dispatch. Unit tier; keep the real worker DB and mutate only the link role.
+ it('rejects a companion recipient after its canonical association disappears', async () => {
+ const { im } = await resolve({ sessionId: 'canonical' });
+ const recipient = im.getOwnerOpenId()!;
+ await client.exec("UPDATE bot_session_links SET role = 'history' WHERE session_id = 'canonical'");
+ try {
+ await expect(resolve({ sessionId: 'canonical' }, recipient)).rejects.toThrow('Feishu recipient changed');
+ expect((await resolve({ sessionId: 'desktop' }, 'ou_personal')).im).toBe(personal);
+ // Legacy personal Feishu sessions can explicitly address a non-owner DM
+ // or group without using the owner fallback; do not restrict those replies.
+ for (const feishuChatId of ['ou_explicit', 'oc_group']) {
+ expect((await resolve({ sessionId: 'desktop', vendorOptions: { feishuChatId } }, feishuChatId)).im)
+ .toBe(personal);
+ }
+ } finally {
+ await client.exec("UPDATE bot_session_links SET role = 'canonical' WHERE session_id = 'canonical'");
+ }
+ });
+
it('rejects a lookup that completes across an account switch', async () => {
const pending = resolve({ sessionId: 'canonical' });
setCurrentDbClient(client, 'account-b');
diff --git a/apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts b/apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts
index aa153fed3a..3ac423539b 100644
--- a/apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts
+++ b/apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts
@@ -1,11 +1,12 @@
import { and, eq } from 'drizzle-orm';
+import type { FeishuBotHostSession } from '@cindy/mcps';
import { activeOwnerScopeKey, isAppSessionBoundaryPending } from '../appSessionState.js';
import { getCurrentDbClientSnapshot, type CurrentDbClientSnapshot } from '../localDb/client/current.js';
import { botSessionLinks, sessions } from '../localDb/schema.js';
import type { FeishuToolTransport } from '../im/shared/channelToolScope.js';
-type Caller = { sessionId?: string; sessionInstanceId?: string } | undefined;
+type Caller = FeishuBotHostSession | undefined;
type ResolvedTransport = { im: FeishuToolTransport; assertCurrent: () => void };
/** Resolve each notification against the current account and live connection.
@@ -30,7 +31,21 @@ export function createFeishuTransportResolver(deps: {
assertCurrent();
const scope = deps.readTurnScope(session);
if (scope) return { im: scope.im, assertCurrent };
- if (!session?.sessionId) return { im: deps.personalIm, assertCurrent };
+ const personalTransport = (): ResolvedTransport => {
+ const assertPersonalCurrent = () => {
+ assertCurrent();
+ // Canonical membership may disappear between owner lookup and send.
+ // Never use personal credentials for another app's resolved open_id.
+ // Explicit host-recorded reply targets retain their existing behavior.
+ if (ownerOpenId && ownerOpenId !== deps.personalIm.getOwnerOpenId() &&
+ ownerOpenId !== session?.vendorOptions?.feishuChatId) {
+ throw new Error('Feishu recipient changed');
+ }
+ };
+ assertPersonalCurrent();
+ return { im: deps.personalIm, assertCurrent: assertPersonalCurrent };
+ };
+ if (!session?.sessionId) return personalTransport();
snapshot = getCurrentDbClientSnapshot();
if (!snapshot) throw new Error('DbClient not ready');
@@ -46,7 +61,7 @@ export function createFeishuTransportResolver(deps: {
.limit(1)
.get();
assertCurrent();
- if (!row?.botId) return { im: deps.personalIm, assertCurrent };
+ if (!row?.botId) return personalTransport();
const im = deps.getCompanionTransport(row.botId, ownerOpenId);
if (!im) throw new Error('Companion Feishu connection unavailable');
return { im, assertCurrent };
diff --git a/docs/dev-rules/im-turn-flow.md b/docs/dev-rules/im-turn-flow.md
index 1d910954ce..08f52b0c97 100644
--- a/docs/dev-rules/im-turn-flow.md
+++ b/docs/dev-rules/im-turn-flow.md
@@ -67,7 +67,9 @@ Telegram / Slack / X)是同一件事的两套实现:把渠道里的一条消
主人的飞书连接;发送时校验连接的 owner 与已解析收件人一致。已识别伙伴但没有匹配的可用连接、
数据库查询失败、账号边界处于 pending 或 owner 代次变化,均拒绝发送,不回落个人 bot;查询前后
及实际调用发送前都复核账号边界、owner 代次与所用 DB 快照。没有 canonical 伙伴归属的普通
-任务仍保留设置页个人 bot 的主人通知能力。此路径不向主任务持久写入 `feishuChatId`,也不生成租约
+任务仍保留设置页个人 bot 的主人通知能力。使用个人 bot 发送时,已解析收件人必须匹配它当前的主人
+或宿主任务记录的显式 `feishuChatId`;两阶段之间 canonical 关联消失时,不将伙伴的收件人交给个人
+凭证发送。此路径不向主任务持久写入 `feishuChatId`,也不生成租约
或授予轮次内原生 API 权限;`cindyBotImChannels` 只控制工具挂载,不作为归属或授权依据。
实现见 [`feishuTransportRouting.ts`](../../apps/desktop/src/main/mcp-integrations/feishuTransportRouting.ts)
与 [`botImConnectionRuntime.ts`](../../apps/desktop/src/main/im/shared/botImConnectionRuntime.ts),
diff --git a/packages/lizi-mcps/src/types.ts b/packages/lizi-mcps/src/types.ts
index edafdcef55..5e2277799f 100644
--- a/packages/lizi-mcps/src/types.ts
+++ b/packages/lizi-mcps/src/types.ts
@@ -103,6 +103,8 @@ export interface FeishuBotSendMessageResult {
export interface FeishuBotHostSession {
sessionId?: string;
sessionInstanceId?: string;
+ /** Trusted host session metadata; preserves an explicit Feishu reply target. */
+ vendorOptions?: Readonly<Record<string, unknown>>;
}
/**