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(mobile): clear committed invitation history during switch logout

DavidShend9324e129131
diff --git a/apps/mobile/src/__tests__/accountDeletion.test.ts b/apps/mobile/src/__tests__/accountDeletion.test.ts
index 0ebe76a2ff..a6c99cccc2 100644
--- a/apps/mobile/src/__tests__/accountDeletion.test.ts
+++ b/apps/mobile/src/__tests__/accountDeletion.test.ts
@@ -16,7 +16,7 @@ describe("mobile account deletion", () => {
const body = context.slice(start, context.indexOf('const terminateSession', start));
const capture = body.indexOf('const invitationHistoryOwner = getMobileAuthOwner();');
const invalidate = body.indexOf('setMobileAuthOwner(null);');
- const clear = body.indexOf('clearClipboardInvitationHistory(invitationHistoryOwner.accountKey)');
+ const clear = body.indexOf('clearClipboardInvitationHistory(invitationHistoryAccountKey)');
expect(capture).toBeGreaterThan(-1);
expect(invalidate).toBeGreaterThan(capture);
expect(clear).toBeGreaterThan(invalidate);
diff --git a/apps/mobile/src/__tests__/clipboardInvitationHistory.test.ts b/apps/mobile/src/__tests__/clipboardInvitationHistory.test.ts
index 3a8cb33ac4..b79a3f6cb5 100644
--- a/apps/mobile/src/__tests__/clipboardInvitationHistory.test.ts
+++ b/apps/mobile/src/__tests__/clipboardInvitationHistory.test.ts
@@ -1,4 +1,8 @@
import { afterEach, beforeEach, expect, it, vi } from 'vitest';
+import { readFileSync } from 'node:fs';
+import { resolve } from 'node:path';
+import { accountVaultKey, type AuthRegion } from '@cindy/auth-client';
+import { getMobileAuthOwner, setMobileAuthOwner, invalidateMobileAuthOwnerForSwitch } from '@/auth/authOwnerGeneration';
const storage = vi.hoisted(() => ({ values: new Map<string, string>(), getItem: vi.fn(), setItem: vi.fn(), removeItem: vi.fn() }));
vi.mock('@react-native-async-storage/async-storage', () => ({ default: storage }));
@@ -18,6 +22,57 @@ beforeEach(() => {
});
afterEach(async () => { await __testing.flush(); vi.useRealTimers(); });
+// Execute the actual provider's synchronous logout prelude with committed
+// identity refs, real owner fencing and real history storage logic. Native auth
+// cleanup starts after this block and is irrelevant to choosing the owner key.
+const authSource = readFileSync(resolve(process.cwd(), 'src/auth/AuthContext.tsx'), 'utf8');
+const logoutStart = authSource.indexOf('const clearLocalSession = useCallback');
+const historyStart = authSource.indexOf('const invitationHistoryOwner =', logoutStart);
+const historyEnd = authSource.indexOf('setAccountGeneration(', historyStart);
+if (logoutStart < 0 || historyStart < 0 || historyEnd < historyStart) throw new Error('Logout history prelude not found');
+const logoutHistory = new Function(
+ 'getMobileAuthOwner', 'setMobileAuthOwner', 'clearClipboardInvitationHistory',
+ 'accountVaultKey', 'userRef', 'activeAuthRealmRef',
+ authSource.slice(historyStart, historyEnd) + '\nreturn clearInvitationHistory;',
+);
+
+it.each([
+ { realm: 'global', phase: 'active' },
+ { realm: 'global', phase: 'switching' },
+ { realm: 'cn', phase: 'switching' },
+ { realm: 'cn', phase: 'rollback' },
+ { realm: 'cn', phase: 'switched' },
+ { realm: 'global', phase: 'no-user' },
+] as const)('terminal logout clears only the committed history ($realm / $phase)', async ({ realm, phase }) => {
+ const keys = [accountVaultKey('global', 'guest'), accountVaultKey('cn', 'guest'), accountVaultKey('global', 'other')];
+ for (const ownerKey of keys) await rememberClipboardInvitation(ownerKey, digest);
+ const userRef: { current: { id: string } | null } = { current: { id: 'guest' } };
+ const realmRef: { current: AuthRegion } = { current: realm };
+ setMobileAuthOwner('guest', realm);
+ if (phase !== 'active') invalidateMobileAuthOwnerForSwitch();
+ if (phase === 'rollback') setMobileAuthOwner('guest', realm);
+ if (phase === 'switched') {
+ userRef.current = { id: 'other' }; realmRef.current = 'global';
+ setMobileAuthOwner('other', 'global');
+ }
+ if (phase === 'no-user') userRef.current = null;
+ try {
+ // Switch invalidation, successful activation and rollback themselves retain
+ // all histories. Only terminal logout starts account-scoped removal.
+ for (const ownerKey of keys) expect(await hasSeenClipboardInvitation(ownerKey, digest)).toBe(true);
+ const expectedClearedKey = userRef.current ? accountVaultKey(realmRef.current, userRef.current.id) : '';
+ const clearing = logoutHistory(getMobileAuthOwner, setMobileAuthOwner, clearClipboardInvitationHistory, accountVaultKey, userRef, realmRef);
+ expect(getMobileAuthOwner().accountKey).toBe('');
+ expect(getMobileAuthOwner().switching).toBeUndefined();
+ await clearing;
+ __testing.reset(); // Cold start must not restore the terminated account.
+ for (const ownerKey of keys) {
+ expect(await hasSeenClipboardInvitation(ownerKey, digest)).toBe(ownerKey !== expectedClearedKey);
+ expect(storage.values.has(__testing.storageKey(ownerKey))).toBe(ownerKey !== expectedClearedKey);
+ }
+ } finally { setMobileAuthOwner(null); }
+});
+
it('stores only SHA-256 digests and timestamps and reloads them after losing all memory', async () => {
await rememberClipboardInvitation(account, digest);
const raw = storage.values.get(key)!;
diff --git a/apps/mobile/src/auth/AuthContext.tsx b/apps/mobile/src/auth/AuthContext.tsx
index 460ed52180..c2f94c16b8 100644
--- a/apps/mobile/src/auth/AuthContext.tsx
+++ b/apps/mobile/src/auth/AuthContext.tsx
@@ -2663,8 +2663,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
// 残留由 server 侧 APNs 410 回收与换账号重注册的让位逻辑兜底。
// Invalidate in-flight remote creates before any async logout cleanup begins.
const invitationHistoryOwner = getMobileAuthOwner();
+ // Switching temporarily empties the owner fence; the committed user/realm
+ // still identify the session being terminated until the new owner is applied.
+ const invitationHistoryAccountKey = invitationHistoryOwner.accountKey || (userRef.current
+ ? accountVaultKey(activeAuthRealmRef.current, userRef.current.id)
+ : '');
setMobileAuthOwner(null);
- const clearInvitationHistory = clearClipboardInvitationHistory(invitationHistoryOwner.accountKey).catch(() => undefined);
+ const clearInvitationHistory = clearClipboardInvitationHistory(invitationHistoryAccountKey).catch(() => undefined);
setAccountGeneration((value) => value + 1);
// 同步失效认证代次,必须早于第一个 await。否则推送 token 注销的网络等待窗口内,
// 迟到的 canary / XD beta 探测仍会把旧账号结果写回本地。