Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
fix(bots): guard OAuth credential commits against withdrawn cards
zqchris0249f6a57efd
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/ghostOauthAccounts.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/ghostOauthAccounts.test.tsindex 2a2ebe67c3..90222e82db 100644--- a/apps/desktop/src/main/cindy-brain/__tests__/ghostOauthAccounts.test.ts+++ b/apps/desktop/src/main/cindy-brain/__tests__/ghostOauthAccounts.test.ts@@ -753,6 +753,26 @@ describe('missingAuthScopes(快照推断)', () => {});describe('connectAccount', () => {+ it('does not commit OAuth tokens when the card is invalidated during identity lookup', async () => {+ const vault = memoryVault({ [`${KEY}-client-id`]: 'cid' });+ const before = new Map(vault.data);+ let current = true;+ const mgr = new GhostOauthAccountManager({+ vault, openExternal: autoBrowser(),+ fetchImpl: (async (input) => {+ if (String(input) === DECL.tokenUrl)+ return jsonResponse({ access_token: 'fake-access', refresh_token: 'fake-refresh', expires_in: 3600 });+ current = false;+ return jsonResponse({ email: '[email protected]' });+ }) as typeof fetch,+ });+ await expect(mgr.connectAccount(GHOST, KEY, DECL, {+ assertCurrent: () => { if (!current) throw new Error('card withdrawn'); },+ })).rejects.toThrow('card withdrawn');+ expect(vault.data).toEqual(before);+ expect(mgr.listAccounts(GHOST, KEY)).toHaveLength(0);+ });+it('端口回收器只对第一方官方意识放行(第三方 redirectPort 不许借刀杀进程)', async () => {const blocker = http.createServer();const heldPort = await new Promise<number>((resolve) => {diff --git a/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.ts b/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.tsindex f999b91f82..2ff243174d 100644--- a/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.ts+++ b/apps/desktop/src/main/cindy-brain/ghostOauthAccounts.ts@@ -873,6 +873,8 @@ export class GhostOauthAccountManager {deliveryHosts?: readonly string[];/** Main-only handoff for reopening the current authorization page. */onAuthorizationUrl?: (url: string) => void;+ /** Main-only caller boundary, checked inside the credential mutation lock. */+ assertCurrent?: () => void;},): Promise<GhostOauthConnectResult> {if (decl.tokenBroker !== undefined && !this.isTokenBrokerAuthorized(ghostId)) {@@ -972,6 +974,7 @@ export class GhostOauthAccountManager {}return this.withMutationLock(ghostId, async () => {+ opts?.assertCurrent?.();// Identity/avatar fetches are asynchronous as well. Recheck inside the// same strict mutation lock as the first vault read/write so a package// update cannot replace the declaration between validation and commit.diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.tsindex 539eb647d5..23fbe1d764 100644--- a/apps/desktop/src/main/cindy-brain/index.ts+++ b/apps/desktop/src/main/cindy-brain/index.ts@@ -4977,6 +4977,7 @@ export async function executeGhostSetupAction(args: {action: GhostSetupAllowedAction;responseTarget?: GhostSetupInteractionResponseTarget;onAuthorizationUrl?: (url: string) => void;+ assertCurrent?: () => void;}): Promise<GhostSetupActionResult> {const ghost = findAvailableGhost(args.ghostId);if (!ghost) {@@ -5013,7 +5014,7 @@ export async function executeGhostSetupAction(args: {args.ghostId,secretKey,decl,- { deliveryHosts: runtimeManifest.network?.hosts, onAuthorizationUrl: args.onAuthorizationUrl },+ { deliveryHosts: runtimeManifest.network?.hosts, onAuthorizationUrl: args.onAuthorizationUrl, assertCurrent: args.assertCurrent },);return connected.ok? { ok: true }diff --git a/apps/desktop/src/main/maker-host/__tests__/grokAuthRejectionRecovery.test.ts b/apps/desktop/src/main/maker-host/__tests__/grokAuthRejectionRecovery.test.tsindex 24fbf650ca..0a4ceaa023 100644--- a/apps/desktop/src/main/maker-host/__tests__/grokAuthRejectionRecovery.test.ts+++ b/apps/desktop/src/main/maker-host/__tests__/grokAuthRejectionRecovery.test.ts@@ -51,6 +51,8 @@ vi.mock('../outbound-fetch.js', () => ({}));import {+ CallbackListener,+ runGrokOAuthLogin,getGrokOAuthCredentialGeneration,logoutGrok,recoverGrokAuthAfterRejection,@@ -344,3 +346,25 @@ describe('recoverGrokAuthAfterRejection', () => {expect(fetchMock).not.toHaveBeenCalled();});});++it('preserves credentials when the authorizing card is withdrawn during token exchange', async () => {+ seedCredentials();+ const before = store.get(SECRET_ID);+ const generation = getGrokOAuthCredentialGeneration();+ let current = true;+ vi.spyOn(CallbackListener.prototype, 'start').mockResolvedValue(56121);+ vi.spyOn(CallbackListener.prototype, 'waitForCode').mockResolvedValue('fake-code');+ vi.stubGlobal('fetch', vi.fn(async (_input, init) => {+ if (init?.method === 'POST') {+ current = false;+ return tokenResponse(200, { access_token: 'new-fake-access', refresh_token: 'new-fake-refresh' });+ }+ return tokenResponse(200, {});+ }));+ const result = await runGrokOAuthLogin({+ assertCurrent: () => { if (!current) throw new Error('card withdrawn'); },+ });+ expect(result).toMatchObject({ ok: false, reason: 'card withdrawn' });+ expect(store.get(SECRET_ID)).toBe(before);+ expect(getGrokOAuthCredentialGeneration()).toBe(generation);+});diff --git a/apps/desktop/src/main/maker-host/grok-oauth-login.ts b/apps/desktop/src/main/maker-host/grok-oauth-login.tsindex bc16838ded..188708a7d8 100644--- a/apps/desktop/src/main/maker-host/grok-oauth-login.ts+++ b/apps/desktop/src/main/maker-host/grok-oauth-login.ts@@ -509,6 +509,8 @@ export async function runGrokOAuthLogin(opts?: {onProgress?: (msg: string) => void;/** Host-only callback; URL must never be persisted in chat. */onAuthorizationUrl?: (url: string) => void;+ /** Main-only caller boundary, rechecked after token exchange before persistence. */+ assertCurrent?: () => void;}): Promise<GrokOAuthLoginResult> {cancelGrokOAuthLogin(); // 同一时刻只允许一个登录流@@ -587,6 +589,7 @@ export async function runGrokOAuthLogin(opts?: {// token exchange 的 fetch 带 signal,但 res.json() / nonce 校验期间到达的 abort// 不会中断已 resolve 的响应体 —— 落盘前最后检查,保证"已取消"的登录绝不写凭证。if (abort.signal.aborted) throw new Error('login_cancelled');+ opts?.assertCurrent?.();writeBlob(blobFromTokenResponse(tok));bindNativeProviderAuth('xai');advanceGrokOAuthCredentialGeneration();diff --git a/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts b/apps/desktop/src/main/maker-ipc/botAuthorizationHost.tsindex 8eb0f506ab..09ed2544d2 100644--- a/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts+++ b/apps/desktop/src/main/maker-ipc/botAuthorizationHost.ts@@ -159,7 +159,7 @@ export function initializeBotAuthorizationHost(async execute(_action, _sender, _value, onAuthorizationUrl, assertCurrent) {await assertSession(sessionId);assertCurrent?.();- const result = await runGrokOAuthLogin({ onAuthorizationUrl });+ const result = await runGrokOAuthLogin({ onAuthorizationUrl, assertCurrent });await assertSession(sessionId);if (result.ok) {bus.emit('host:grok', { source: 'oauth' });@@ -245,6 +245,7 @@ export function initializeBotAuthorizationHost(action,responseTarget: sender,onAuthorizationUrl,+ assertCurrent,});if (result.ok && action.kind === 'oauth_connect') reconnected = true;return result;