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(desktop): verify session agentKind before prompt prediction

Shinkud5a06d8ea89d
diff --git a/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts b/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
index 334a0237fe..e1dd7ae554 100644
--- a/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/predictPromptIpcBoundary.test.ts
@@ -12,7 +12,7 @@ const h = vi.hoisted(() => ({
trusted: true,
predict: vi.fn(async (_params: unknown) => '下一步做什么'),
/** 模拟 DB 返回的 session row */
- sessionRow: { remoteHostId: null } as { remoteHostId: string | null } | undefined,
+ sessionRow: { remoteHostId: null, agentKind: null } as { remoteHostId: string | null; source?: string | null; agentKind: string | null } | undefined,
}));
function nativeImageEmpty() {
@@ -193,7 +193,7 @@ beforeEach(() => {
vi.clearAllMocks();
h.handlers.clear();
h.trusted = true;
- h.sessionRow = { remoteHostId: null };
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc' };
h.predict.mockResolvedValue('下一步做什么');
registerMakerTitleIpc();
});
@@ -270,18 +270,26 @@ describe('maker:predict-prompt — DB 防御纵深(远程会话拒绝)', () => {
});
it('session 为远程会话(remoteHostId 非空)时静默返回 null', async () => {
- h.sessionRow = { remoteHostId: 'ssh-host-1' };
+ h.sessionRow = { remoteHostId: 'ssh-host-1', agentKind: 'cc' };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
expect(h.predict).not.toHaveBeenCalled();
});
- it('本地 session(remoteHostId=null)正常执行预测', async () => {
- h.sessionRow = { remoteHostId: null };
+ it('本地 session(remoteHostId=null, agentKind 匹配)正常执行预测', async () => {
+ h.sessionRow = { remoteHostId: null, agentKind: 'cc' };
await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: '下一步做什么' });
expect(h.predict).toHaveBeenCalledTimes(1);
});
+
+ it('agentKind 不匹配时静默返回 null,不触发付费调用', async () => {
+ // renderer 上报 claude-code,DB 存的是 codex
+ h.sessionRow = { remoteHostId: null, agentKind: 'codex' };
+
+ await expect(invokePredict(VALID_REQUEST)).resolves.toEqual({ prompt: null });
+ expect(h.predict).not.toHaveBeenCalled();
+ });
});
describe('maker:predict-prompt — 多窗口去重', () => {
diff --git a/apps/desktop/src/main/maker-ipc/title.ts b/apps/desktop/src/main/maker-ipc/title.ts
index 7e16bddca1..279b76b780 100644
--- a/apps/desktop/src/main/maker-ipc/title.ts
+++ b/apps/desktop/src/main/maker-ipc/title.ts
@@ -430,12 +430,23 @@ function parsePredictPromptRequest(raw: unknown): PromptPredictionParams {
// 同时拒绝 review session:reviewer 会话的 composer 被禁用(disabled),不可编辑也不可发送,
// 对其做预测是浪费付费调用。
const [sessionRow] = await getDbClient()
- .drizzle.select({ remoteHostId: sessions.remoteHostId, source: sessions.source })
+ .drizzle.select({ remoteHostId: sessions.remoteHostId, source: sessions.source, agentKind: sessions.agentKind })
.from(sessions)
.where(eq(sessions.id, params.sessionId));
if (!sessionRow || sessionRow.remoteHostId || sessionRow.source === 'review') {
return { prompt: null };
}
+ // 防御纵深:校验 renderer 上报的 agentKind 与 DB 记录一致,避免受信 renderer 绕过
+ // UI 守卫或 stale UI 状态将 Claude session 的对话内容发送给 Codex provider(反之亦然)。
+ const dbAgentKind = dbToMakerAgentKind(sessionRow.agentKind);
+ if (dbAgentKind !== params.agentKind) {
+ log.warn('predict-prompt agentKind mismatch — rejecting', {
+ sessionId: params.sessionId,
+ rendererAgentKind: params.agentKind,
+ dbAgentKind,
+ });
+ return { prompt: null };
+ }
// 多窗口去重:同一 session 同时只能有一笔预测在途,避免 openSessionInNewWindow
// 等多窗口场景下重复触发付费 provider 调用。主进程级 Set 跨窗口可见。
if (_predictingPromptSessions.has(params.sessionId)) {