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): fence rotated credentials across account changes

liugaowei939a659c3bd5
diff --git a/apps/desktop/src/main/model-access/__tests__/gatewayKeyRotation.test.ts b/apps/desktop/src/main/model-access/__tests__/gatewayKeyRotation.test.ts
new file mode 100644
index 0000000000..3425142198
--- /dev/null
+++ b/apps/desktop/src/main/model-access/__tests__/gatewayKeyRotation.test.ts
@@ -0,0 +1,84 @@
+import { readFileSync } from 'node:fs';
+import path from 'node:path';
+import vm from 'node:vm';
+import ts from 'typescript';
+import { describe, expect, it, vi } from 'vitest';
+
+// Execute the production writer with boundary/IO fixtures, without initializing
+// Electron or the model catalog. No copied implementation or source assertions.
+const source = ts.createSourceFile('index.ts', readFileSync(path.resolve(__dirname, '../index.ts'), 'utf8'), ts.ScriptTarget.Latest, true);
+const writer = source.statements.find(node => ts.isFunctionDeclaration(node) && node.name?.text === 'writeXdKeyWithCodexSideEffect')!;
+const compiled = ts.transpileModule(writer.getText(source), { compilerOptions: { target: ts.ScriptTarget.ES2022 } }).outputText;
+
+function harness() {
+ const owner = { scope: 'cloud:account-a:1', pending: false };
+ const prepare = vi.fn(async () => {});
+ const cancel = vi.fn();
+ const finalize = vi.fn(async () => {});
+ const write = vi.fn(() => true);
+ const notify = vi.fn();
+ const context = vm.createContext({
+ activeOwnerScopeKey: () => owner.scope,
+ isAppSessionBoundaryPending: () => owner.pending,
+ prepareCodexForAuthModeChange: prepare,
+ cancelCodexAuthModeChange: cancel,
+ finalizeCodexAfterAuthModeChange: finalize,
+ getProviderSecretStore: () => ({ set: write }),
+ notifyXdProviderKeyChanged: notify,
+ log: { warn: vi.fn() },
+ });
+ const rotate = vm.runInContext(`${compiled}\nwriteXdKeyWithCodexSideEffect`, context) as (key: string) => Promise<boolean>;
+ return { owner, prepare, cancel, finalize, write, notify, rotate };
+}
+
+describe('gateway key rotation writer', () => {
+ it('coordinates every gateway host before writing and leaves OAuth state unchanged', async () => {
+ const h = harness();
+ h.prepare.mockImplementation(async () => { expect(h.write).not.toHaveBeenCalled(); });
+ expect(await h.rotate('invalid-test-key')).toBe(true);
+ expect(h.prepare).toHaveBeenCalledExactlyOnceWith({ allLocalHosts: true, credentialMode: 'gateway-key' });
+ expect(h.write).toHaveBeenCalledExactlyOnceWith('xd', 'invalid-test-key');
+ expect(h.notify).toHaveBeenCalledOnce();
+ expect(h.finalize).toHaveBeenCalledExactlyOnceWith({ refreshAuthState: false });
+ expect(h.cancel).not.toHaveBeenCalled();
+ });
+
+ it('does not start a rotation during account handoff', async () => {
+ const h = harness();
+ h.owner.pending = true;
+ expect(await h.rotate('invalid-test-key')).toBe(false);
+ expect(h.prepare).not.toHaveBeenCalled();
+ expect(h.write).not.toHaveBeenCalled();
+ });
+
+ it.each(['different-owner', 'same-owner-new-generation', 'handoff-pending'])('discards stale credentials after preparation: %s', async change => {
+ const h = harness();
+ h.prepare.mockImplementation(async () => {
+ if (change === 'different-owner') h.owner.scope = 'cloud:account-b:2';
+ if (change === 'same-owner-new-generation') h.owner.scope = 'cloud:account-a:3';
+ if (change === 'handoff-pending') h.owner.pending = true;
+ });
+ expect(await h.rotate('invalid-account-a-key')).toBe(false);
+ expect(h.write).not.toHaveBeenCalled();
+ expect(h.notify).not.toHaveBeenCalled();
+ expect(h.finalize).not.toHaveBeenCalled();
+ expect(h.cancel).toHaveBeenCalledOnce();
+ });
+
+ it('preserves the stored key when the target host is busy', async () => {
+ const h = harness();
+ h.prepare.mockRejectedValue(new Error('busy'));
+ expect(await h.rotate('invalid-test-key')).toBe(false);
+ expect(h.write).not.toHaveBeenCalled();
+ expect(h.finalize).not.toHaveBeenCalled();
+ });
+
+ it('releases the guard when credential storage rejects the write', async () => {
+ const h = harness();
+ h.write.mockReturnValue(false);
+ expect(await h.rotate('invalid-test-key')).toBe(false);
+ expect(h.cancel).toHaveBeenCalledOnce();
+ expect(h.notify).not.toHaveBeenCalled();
+ expect(h.finalize).not.toHaveBeenCalled();
+ });
+});
diff --git a/apps/desktop/src/main/model-access/index.ts b/apps/desktop/src/main/model-access/index.ts
index 819ad2740b..1aae0ab7ff 100644
--- a/apps/desktop/src/main/model-access/index.ts
+++ b/apps/desktop/src/main/model-access/index.ts
@@ -8,6 +8,7 @@ import { app, BrowserWindow, ipcMain } from 'electron';
import { createLogger } from '../logger.js';
import * as authManager from '../authManager.js';
+import { activeOwnerScopeKey, isAppSessionBoundaryPending } from '../appSessionState.js';
import { isExternalOrganizationByokUser } from './organizationManaged.js';
import { serverApiFetch, ServerApiError } from '../serverApiClient.js';
import { getClientEndpoint } from '../clientEndpointsService.js';
@@ -104,6 +105,8 @@ function rotateCredentials(): Promise<CredentialsPayload> {
* 按 host 的实际凭据筛选,覆盖不登记共享 proxy 状态的独立 session。
*/
async function writeXdKeyWithCodexSideEffect(key: string): Promise<boolean> {
+ if (isAppSessionBoundaryPending()) return false;
+ const ownerScope = activeOwnerScopeKey();
try {
await prepareCodexForAuthModeChange({ allLocalHosts: true, credentialMode: 'gateway-key' });
} catch (err) {
@@ -112,6 +115,10 @@ async function writeXdKeyWithCodexSideEffect(key: string): Promise<boolean> {
});
return false;
}
+ if (isAppSessionBoundaryPending() || activeOwnerScopeKey() !== ownerScope) {
+ cancelCodexAuthModeChange();
+ return false;
+ }
const ok = getProviderSecretStore().set('xd', key);
if (!ok) {
cancelCodexAuthModeChange();