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): fence OAuth callbacks during login cancellation

chenhonga4ae10869740
diff --git a/apps/mobile/src/auth/AuthContext.tsx b/apps/mobile/src/auth/AuthContext.tsx
index 1c252ee2b0..00dba7c5bb 100644
--- a/apps/mobile/src/auth/AuthContext.tsx
+++ b/apps/mobile/src/auth/AuthContext.tsx
@@ -430,6 +430,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
const accountDeletionReceiptRealmRef = useRef<AuthRegion | null>(null);
const pendingAccountDeletionRestoredRef = useRef(false);
const loginInitializationRef = useRef<{ epoch: number; promise: Promise<boolean> } | null>(null);
+ const oauthCancelledRef = useRef(false);
const loginActionInFlightRef = useRef<Promise<boolean> | null>(null);
const loginActionInFlightEpochRef = useRef<number | null>(null);
const browserCompletionRef = useRef<Promise<void> | null>(null);
@@ -1687,6 +1688,11 @@ export function AuthProvider({ children }: { children: ReactNode }) {
callbackUrl: string,
expectedLoginFlowEpoch = loginFlowEpochRef.current,
): Promise<void> => {
+ // A late Linking event captures the current epoch, so the epoch alone
+ // cannot fence an attempt whose credential deletion is pending or failed.
+ if (oauthCancelledRef.current) {
+ return Promise.reject(authCodeError('AUTH_FLOW_SUPERSEDED'));
+ }
if (
browserCompletionRef.current &&
browserCompletionEpochRef.current === expectedLoginFlowEpoch
@@ -1846,9 +1852,13 @@ export function AuthProvider({ children }: { children: ReactNode }) {
return promise;
}
loginInitializationRef.current = null;
+ if (action.type === 'reset') oauthCancelledRef.current = true;
// Cancelling a browser login must invalidate its in-flight callback before
// any await or deduplication. Other login actions retain their lifecycle.
- if (action.type === 'reset' && loginStateRef.current?.step === 'browser-redirect') {
+ if (action.type === 'reset' && (
+ loginStateRef.current?.step === 'browser-redirect' ||
+ (browserCompletionRef.current && browserCompletionEpochRef.current === loginFlowEpochRef.current)
+ )) {
loginFlowEpochRef.current += 1;
}
const expectedLoginFlowEpoch = loginFlowEpochRef.current;
@@ -1913,6 +1923,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
} satisfies PendingOAuth),
);
assertLoginFlowCurrent(expectedLoginFlowEpoch);
+ oauthCancelledRef.current = false;
updateLoginState(
reduceAuthFlow(input.previousState, {
type: 'browser-started',
@@ -1968,7 +1979,8 @@ export function AuthProvider({ children }: { children: ReactNode }) {
if (!additionalLoginRef.current) resetMobileSessionRealm();
pendingAccountDeletionRestoredRef.current = false;
setAccountDeletionRestored(false);
- await deleteSecureItem(PENDING_OAUTH_KEY).catch(() => undefined);
+ // Do not expose another login until cancellation has reached storage.
+ await deleteSecureItem(PENDING_OAUTH_KEY);
assertLoginFlowCurrent(expectedLoginFlowEpoch);
const providers = await authClientFor(
did,
@@ -2343,6 +2355,7 @@ export function AuthProvider({ children }: { children: ReactNode }) {
} catch (error) {
if (loginFlowEpochRef.current !== expectedLoginFlowEpoch) return false;
const code = authErrorCode(error);
+ if (action.type === 'reset') updateLoginState({ step: 'error', code });
if (
code === 'INVALID_LOGIN_TICKET' ||
code === 'INVALID_BIND_TICKET' ||
diff --git a/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx b/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx
index aaca0baf5c..f2afd93301 100644
--- a/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx
+++ b/apps/mobile/src/auth/__tests__/browserAuthSession.test.tsx
@@ -7,6 +7,7 @@ const native = vi.hoisted(() => ({
platform: 'android',
storage: new Map<string, string>(),
readSecure: vi.fn(),
+ deleteSecure: vi.fn(),
links: new Set<(event: { url: string }) => void>(),
open: vi.fn(),
exchange: vi.fn(),
@@ -66,7 +67,7 @@ vi.mock('@cindy/auth-client/fixtures', () => ({ resolveLoginScenarioFetch: () =>
vi.mock('@/auth/secureStorage', () => ({
getSecureItem: native.readSecure,
setSecureItem: async (key: string, value: string) => { native.storage.set(key, value); },
- deleteSecureItem: async (key: string) => { native.storage.delete(key); },
+ deleteSecureItem: native.deleteSecure,
}));
vi.mock('@/auth/pkce', () => ({
createPkcePair: async () => ({ codeVerifier: 'fixture-verifier', codeChallenge: 'fixture-challenge' }),
@@ -128,6 +129,12 @@ function LoginScreenLifecycle() {
return null;
}
+async function restartProvider() {
+ await act(async () => root.unmount());
+ root = createRoot(document.createElement('div'));
+ await act(async () => { root.render(<AuthProvider><Probe /></AuthProvider>); });
+}
+
async function mountLoginScreen(key: string) {
await act(async () => {
root.render(<AuthProvider><Probe /><LoginScreenLifecycle key={key} /></AuthProvider>);
@@ -165,6 +172,7 @@ describe('browser auth session lifecycle (real AuthProvider, mocked native/netwo
native.initialUrl.mockReset().mockResolvedValue(null);
native.requestCode.mockReset().mockResolvedValue(undefined);
native.storage.clear();
+ native.deleteSecure.mockReset().mockImplementation(async (key: string) => { native.storage.delete(key); });
native.readSecure.mockReset().mockImplementation(async (key: string) => native.storage.get(key) ?? null);
native.state = 0;
native.open.mockReset().mockResolvedValue({ type: 'dismiss' });
@@ -206,13 +214,59 @@ describe('browser auth session lifecycle (real AuthProvider, mocked native/netwo
expect(auth.loginState?.step).toBe('identifier');
await emitLink(oldCallback);
expect(native.exchange).not.toHaveBeenCalled();
- expect(auth.authError).toBe('INVALID_AUTH_CODE');
+ expect(auth.authError).toBeNull();
await act(async () => { await auth.dispatchLoginAction({ type: 'reset' }); });
await start();
await emitLink(callbackUrl());
expect(native.exchange).toHaveBeenCalledTimes(1);
});
+ it.each(['late-link', 'in-flight'] as const)('keeps failed cancellation fenced until storage deletion can be retried (%s)', async timing => {
+ await start();
+ const oldCallback = callbackUrl();
+ const pending = native.storage.get(pendingKey);
+ let complete!: (value: typeof verifiedOutcome) => void;
+ if (timing === 'in-flight') {
+ native.exchange.mockImplementationOnce(() => new Promise(resolve => { complete = resolve; }));
+ await emitLink(oldCallback);
+ }
+ native.deleteSecure.mockRejectedValueOnce(new Error('fixture storage unavailable'));
+ await act(async () => { expect(await auth.dispatchLoginAction({ type: 'reset' })).toBe(false); });
+ expect(native.storage.get(pendingKey)).toBe(pending);
+ expect(auth.loginState?.step).toBe('error');
+ const error = auth.authError;
+ await mountLoginScreen('remount-after-failed-cancel');
+ await emitLink(oldCallback);
+ if (timing === 'in-flight') await act(async () => { complete(verifiedOutcome); });
+ expect(native.exchange).toHaveBeenCalledTimes(timing === 'in-flight' ? 1 : 0);
+ expect(auth.loginState?.step).toBe('error');
+ expect(auth.authError).toBe(error);
+ await act(async () => { expect(await auth.dispatchLoginAction({ type: 'reset' })).toBe(true); });
+ expect(native.storage.has(pendingKey)).toBe(false);
+ expect(auth.loginState?.step).toBe('identifier');
+ await start();
+ await emitLink(callbackUrl());
+ expect(native.exchange).toHaveBeenCalledTimes(timing === 'in-flight' ? 2 : 1);
+ expect(auth.loginState?.step).toBe('sso-verification');
+ });
+
+ it('ignores a callback that starts while cancellation is still deleting credentials', async () => {
+ await start();
+ const url = callbackUrl();
+ let deleted!: () => void;
+ native.deleteSecure.mockImplementationOnce(() => new Promise<void>(resolve => {
+ deleted = () => { native.storage.delete(pendingKey); resolve(); };
+ }));
+ let reset!: Promise<boolean>;
+ await act(async () => { reset = auth.dispatchLoginAction({ type: 'reset' }); });
+ await emitLink(url);
+ expect(native.exchange).not.toHaveBeenCalled();
+ expect(auth.isBusy).toBe(true);
+ await act(async () => { deleted(); expect(await reset).toBe(true); });
+ expect(auth.loginState?.step).toBe('identifier');
+ expect(auth.authError).toBeNull();
+ });
+
it('an earlier attempt cannot consume the new attempt after cancel and retry', async () => {
await start();
const oldCallback = callbackUrl();
@@ -508,7 +562,7 @@ describe('browser auth session lifecycle (real AuthProvider, mocked native/netwo
});
it.each(['callback', 'cancel'] as const)('does not restore a stale browser wait after %s takes ownership during storage read', async owner => {
- await act(async () => { await auth.cancelAddAccount(); });
+ await restartProvider();
const pending = JSON.stringify({ state: 'fixture-cold-state', codeVerifier: 'fixture-verifier',
deviceId: 'fixture-device', realm: 'cn', label: 'Fixture SSO', createdAt: Date.now() });
native.storage.set(pendingKey, pending);
@@ -527,7 +581,7 @@ describe('browser auth session lifecycle (real AuthProvider, mocked native/netwo
it.each(['pending', 'finished', 'failed-provider'] as const)(
'does not let slow initialization overwrite a cold callback (%s)', async timing => {
- await act(async () => { await auth.cancelAddAccount(); });
+ await restartProvider();
let finishProviders!: (value: unknown) => void;
let failProviders!: (error: Error) => void;
native.providers.mockImplementationOnce(() => new Promise((resolve, reject) => {
@@ -648,7 +702,7 @@ describe('browser auth session lifecycle (real AuthProvider, mocked native/netwo
action: { type: 'request-sso-verification-code' }, request: native.requestVerification },
{ name: 'back', outcome: verifiedOutcome, action: { type: 'reset' }, request: native.providers },
] as const)('executes $name immediately while initialization is still awaiting providers', async fixture => {
- await act(async () => { await auth.cancelAddAccount(); });
+ await restartProvider();
let finishProviders!: (value: unknown) => void;
native.providers.mockImplementationOnce(() => new Promise(resolve => { finishProviders = resolve; }));
native.exchange.mockResolvedValueOnce(fixture.outcome);
@@ -671,7 +725,7 @@ describe('browser auth session lifecycle (real AuthProvider, mocked native/netwo
});
it.each(['success', 'failure'] as const)('ignores late initialization %s while the next user request is running', async result => {
- await act(async () => { await auth.cancelAddAccount(); });
+ await restartProvider();
let finish!: (value: unknown) => void;
let fail!: (error: Error) => void;
native.providers.mockImplementationOnce(() => new Promise((resolve, reject) => { finish = resolve; fail = reject; }));