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(auth): rollback session under vault lock

xd-bobof618b4216213
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
index 8d840c2e5a..f7249dd9eb 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
@@ -236,6 +236,15 @@ describe('auth login-flow reset', () => {
);
const loginBody = source.slice(loginStart, loginEnd);
expect(loginBody).toContain('{ recoverInvalidForExplicitLogin: true }');
+ const transitionCommit = loginBody.indexOf('await transition.commit();');
+ const transitionRollback = loginBody.indexOf('await transition.rollback();');
+ expect(transitionCommit).toBeGreaterThan(
+ loginBody.indexOf('await transactAuthAccountVault('),
+ );
+ expect(transitionRollback).toBeGreaterThan(transitionCommit);
+ expect(transitionRollback).toBeLessThan(
+ loginBody.indexOf('{ recoverInvalidForExplicitLogin: true }'),
+ );
});
it('atomically replaces the aggregate saved-account vault', () => {
@@ -255,7 +264,7 @@ describe('auth login-flow reset', () => {
expect(clearBody.indexOf('`${filepath}.bak`')).toBeLessThan(clearBody.indexOf('filepath])'));
});
- it('fails a Desktop account switch before owner teardown when session persistence fails', () => {
+ it('fails a Desktop account switch before owner teardown and rolls back under the vault lock', () => {
const completeStart = source.indexOf('async function completeLogin(');
const completeEnd = source.indexOf('\n}\n\nasync function acceptLoginOutcome', completeStart);
const completeBody = source.slice(completeStart, completeEnd);
@@ -269,6 +278,7 @@ describe('auth login-flow reset', () => {
expect(completeBody.indexOf('restorePersistedAuthSessionIfCurrent(')).toBeGreaterThan(
ownerCommit,
);
+ expect(completeBody.indexOf('rollback: () => {')).toBeGreaterThan(ownerCommit);
expect(completeBody.slice(ownerCommit)).not.toContain(
'writePersistedAuthSession(outcome.refreshToken',
);
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 962272d087..eaedcf227c 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -1069,7 +1069,10 @@ async function commitDesktopLoginSessions(
accountRefreshToken?: string | null;
memberships: readonly (AuthMembership | AccountMembership | StoredAccountMetadata)[];
},
- commitTransition: () => void | Promise<void>,
+ transition: {
+ commit: () => void | Promise<void>;
+ rollback: () => void | Promise<void>;
+ },
): Promise<void> {
await transactAuthAccountVault(
(vault) => {
@@ -1090,7 +1093,18 @@ async function commitDesktopLoginSessions(
});
}
},
- commitTransition,
+ async () => {
+ try {
+ await transition.commit();
+ } catch (error) {
+ // `transactAuthAccountVault` still owns the cross-process lock here.
+ // Restore the compatibility session before the vault rollback can
+ // finish and release that lock, so a passive peer cannot consume the
+ // failed target account's token in the gap between the two restores.
+ await transition.rollback();
+ throw error;
+ }
+ },
{ recoverInvalidForExplicitLogin: true },
);
}
@@ -4197,17 +4211,17 @@ async function completeLogin(
const accountRefreshToken = outcome.accountRefreshToken ?? pendingAccountRefreshToken;
let previousPersistedSession: ReturnType<typeof readPersistedAuthSession> = null;
let activeSessionWritten = false;
- try {
- await commitDesktopLoginSessions(
- {
- pair: outcome,
- realm: committedRealm,
- passportId: nextUser.passportId || undefined,
- accountRefreshToken,
- memberships:
- pendingAccountMemberships.length > 0 ? pendingAccountMemberships : [outcome.membership],
- },
- async () => {
+ await commitDesktopLoginSessions(
+ {
+ pair: outcome,
+ realm: committedRealm,
+ passportId: nextUser.passportId || undefined,
+ accountRefreshToken,
+ memberships:
+ pendingAccountMemberships.length > 0 ? pendingAccountMemberships : [outcome.membership],
+ },
+ {
+ commit: async () => {
// The aggregate account vault, compatibility active session, old-owner
// teardown and final owner publication are one epoch-owned transaction.
// Any cancellation before commit restores both durable records while
@@ -4272,17 +4286,16 @@ async function completeLogin(
}),
});
},
- );
- } catch (error) {
- if (activeSessionWritten) {
- restorePersistedAuthSessionIfCurrent(
- outcome.refreshToken,
- committedRealm,
- previousPersistedSession,
- );
- }
- throw error;
- }
+ rollback: () => {
+ if (!activeSessionWritten) return;
+ restorePersistedAuthSessionIfCurrent(
+ outcome.refreshToken,
+ committedRealm,
+ previousPersistedSession,
+ );
+ },
+ },
+ );
scheduleCanaryFlagSync({
token: outcome.accessToken,
expectedAuthEpoch: loginEpoch,