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): 保护配置变更账号作用域

david2a1695c3e436
diff --git a/apps/desktop/src/main/__tests__/wecomGroupNotification.test.ts b/apps/desktop/src/main/__tests__/wecomGroupNotification.test.ts
index 45ca608a39..5d56511a5c 100644
--- a/apps/desktop/src/main/__tests__/wecomGroupNotification.test.ts
+++ b/apps/desktop/src/main/__tests__/wecomGroupNotification.test.ts
@@ -1,4 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
+import { ipcMain } from 'electron';
vi.mock('electron', () => ({
ipcMain: { handle: vi.fn() },
@@ -20,7 +21,12 @@ vi.mock('../security/trustedAppRenderer', () => ({
assertTrustedAppRendererEvent: vi.fn(),
}));
-import { WecomGroupNotificationService, __testing } from '../wecomGroupNotification';
+import {
+ initWecomGroupNotificationIpc,
+ WecomGroupNotificationService,
+ __testing,
+} from '../wecomGroupNotification';
+import { ownerScopedImSecrets } from '../im/ownerScopedStorage';
import { activateImAccountBoundary, deactivateImAccountBoundary } from '../im/accountBoundary';
function response(body: unknown, status = 200): Response {
@@ -248,6 +254,30 @@ describe('WeCom group notification security boundary', () => {
expect(fetchImpl).toHaveBeenCalledWith(firstUrl, expect.any(Object));
});
+ it.each([
+ { channel: 'wecomGroupNotification:set-enabled', args: [false] },
+ { channel: 'wecomGroupNotification:clear', args: [] },
+ ])('rejects $channel when the account generation changes before mutation', async ({
+ channel,
+ args,
+ }) => {
+ type RegisteredHandler = (event: unknown, ...args: unknown[]) => unknown;
+ const handlers = new Map<string, RegisteredHandler>();
+ vi.mocked(ipcMain.handle).mockImplementation((registeredChannel, handler) => {
+ handlers.set(registeredChannel, handler as RegisteredHandler);
+ });
+ initWecomGroupNotificationIpc();
+
+ const handler = handlers.get(channel);
+ expect(handler).toBeDefined();
+ const operation = Promise.resolve(handler?.({}, ...args));
+ deactivateImAccountBoundary();
+
+ await expect(operation).rejects.toThrow('IM account changed');
+ expect(ownerScopedImSecrets.write).not.toHaveBeenCalled();
+ expect(ownerScopedImSecrets.remove).not.toHaveBeenCalled();
+ });
+
it('clears both the webhook and its master switch', () => {
const url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=abcdefgh';
const secrets = createSecrets(url, 'true');
diff --git a/apps/desktop/src/main/wecomGroupNotification.ts b/apps/desktop/src/main/wecomGroupNotification.ts
index 5d65ce3098..6078b99abe 100644
--- a/apps/desktop/src/main/wecomGroupNotification.ts
+++ b/apps/desktop/src/main/wecomGroupNotification.ts
@@ -263,6 +263,14 @@ export class WecomGroupNotificationService implements WecomGroupNotificationPubl
export const wecomGroupNotificationService = new WecomGroupNotificationService();
export function initWecomGroupNotificationIpc(): void {
+ const runAccountScopedMutation = <T>(operation: () => T): Promise<T> => {
+ const accountGeneration = captureImAccountGeneration();
+ if (accountGeneration === null) {
+ return Promise.reject(new ImAccountScopeClosedError());
+ }
+ return runInImAccountGeneration(accountGeneration, async () => operation());
+ };
+
ipcMain.handle('wecomGroupNotification:get-state', (event) => {
assertTrustedAppRendererEvent(event);
return wecomGroupNotificationService.getState();
@@ -289,11 +297,11 @@ export function initWecomGroupNotificationIpc(): void {
ipcMain.handle('wecomGroupNotification:set-enabled', (event, enabled: unknown) => {
assertTrustedAppRendererEvent(event);
if (typeof enabled !== 'boolean') throw new TypeError('enabled must be a boolean');
- return wecomGroupNotificationService.setEnabled(enabled);
+ return runAccountScopedMutation(() => wecomGroupNotificationService.setEnabled(enabled));
});
ipcMain.handle('wecomGroupNotification:clear', (event) => {
assertTrustedAppRendererEvent(event);
- return wecomGroupNotificationService.clear();
+ return runAccountScopedMutation(() => wecomGroupNotificationService.clear());
});
}
diff --git a/packages/lizi-im/src/wecom/__tests__/index.test.ts b/packages/lizi-im/src/wecom/__tests__/index.test.ts
index 14f5dc7149..045db9ebb0 100644
--- a/packages/lizi-im/src/wecom/__tests__/index.test.ts
+++ b/packages/lizi-im/src/wecom/__tests__/index.test.ts
@@ -302,6 +302,7 @@ describe("WecomIM routing and ownership", () => {
payload: { botId: "bot-2", secret: "secret-2" },
},
{ channel: "wecomBot:reconnect", payload: undefined },
+ { channel: "wecomBot:disconnect", payload: undefined },
])(
"does not mutate transport through $channel after the account generation changes",
async ({ channel, payload }) => {
diff --git a/packages/lizi-im/src/wecom/index.ts b/packages/lizi-im/src/wecom/index.ts
index cab870318f..8ada20ddd4 100644
--- a/packages/lizi-im/src/wecom/index.ts
+++ b/packages/lizi-im/src/wecom/index.ts
@@ -205,22 +205,24 @@ export class WecomIM extends BaseIM implements TextChannelIM {
return state();
}),
);
- this.host.ipc.handle("wecomBot:disconnect", async () => {
- this.generation += 1;
- this.client?.disconnect();
- this.client = null;
- this.pendingFrames.clear();
- this.pendingResponses.clear();
- this.inboundTails.clear();
- this.seenMessageIds.clear();
- this.host.secrets.remove(BOT_ID_SECRET_KEY);
- this.host.secrets.remove(BOT_SECRET_SECRET_KEY);
- this.host.secrets.remove(OWNER_USER_ID_SECRET_KEY);
- this.botId = "";
- this.ownerUserId = "";
- this.setStatus({ kind: "idle" });
- return state();
- });
+ this.host.ipc.handle("wecomBot:disconnect", () =>
+ runAccountScoped(async () => {
+ this.generation += 1;
+ this.client?.disconnect();
+ this.client = null;
+ this.pendingFrames.clear();
+ this.pendingResponses.clear();
+ this.inboundTails.clear();
+ this.seenMessageIds.clear();
+ this.host.secrets.remove(BOT_ID_SECRET_KEY);
+ this.host.secrets.remove(BOT_SECRET_SECRET_KEY);
+ this.host.secrets.remove(OWNER_USER_ID_SECRET_KEY);
+ this.botId = "";
+ this.ownerUserId = "";
+ this.setStatus({ kind: "idle" });
+ return state();
+ }),
+ );
}
onMessage(handler: MessageHandler): () => void {
diff --git a/packages/maker-shared/src/__tests__/scheduleForm.test.ts b/packages/maker-shared/src/__tests__/scheduleForm.test.ts
index a783f09152..28e3e1ae6e 100644
--- a/packages/maker-shared/src/__tests__/scheduleForm.test.ts
+++ b/packages/maker-shared/src/__tests__/scheduleForm.test.ts
@@ -85,6 +85,19 @@ describe('mobile schedule form model', () => {
});
});
+ it('writes an explicit false when mobile disables WeCom group notifications', () => {
+ const draft = createMobileScheduleDraft(schedule({
+ notify: { desktop: true, feishu: false, wecomGroup: true },
+ }));
+ const input = buildMobileScheduleInput({ ...draft, notifyWecomGroup: false });
+
+ expect(input.notify).toEqual({
+ desktop: true,
+ feishu: false,
+ wecomGroup: false,
+ });
+ });
+
it('matches desktop heartbeat update semantics for bound sessions', () => {
const draft = createMobileScheduleDraft(schedule({
agentKind: 'codex',
diff --git a/packages/maker-shared/src/scheduleForm.ts b/packages/maker-shared/src/scheduleForm.ts
index 3fc05ccfab..e176366376 100644
--- a/packages/maker-shared/src/scheduleForm.ts
+++ b/packages/maker-shared/src/scheduleForm.ts
@@ -258,7 +258,7 @@ export function buildMobileScheduleInput(draft: MobileScheduleDraft): RemoteSche
notify: {
desktop: draft.notifyDesktop,
feishu: draft.notifyFeishu,
- ...(draft.notifyWecomGroup === true ? { wecomGroup: true } : {}),
+ wecomGroup: draft.notifyWecomGroup === true,
},
};