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): keep account vault mutations fail closed

xd-bobo29f83b009069
diff --git a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
index 467e2637bb..7af8f5b7db 100644
--- a/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
+++ b/apps/desktop/src/main/__tests__/authLoginFlowReset.test.ts
@@ -183,6 +183,8 @@ describe('auth login-flow reset', () => {
expect(helperBody).toContain('removePassportSessionIfCurrent(');
expect(source).toContain("'write-failed'");
expect(source).toContain("'CREDENTIAL_STORE_UNAVAILABLE'");
+ expect(source).toContain('isPersistedSecretAbsent(AUTH_ACCOUNT_VAULT_KEY)');
+ expect(source).toContain('readAuthAccountVault({ allowUnreadable: true })');
const syncStart = source.indexOf('export async function syncSavedAccounts()');
const syncEnd = source.indexOf('\n}\n\nexport async function switchSavedAccount', syncStart);
diff --git a/apps/desktop/src/main/authManager.ts b/apps/desktop/src/main/authManager.ts
index 325d85a823..61d503452c 100644
--- a/apps/desktop/src/main/authManager.ts
+++ b/apps/desktop/src/main/authManager.ts
@@ -559,9 +559,18 @@ function isStoredAccountMetadata(value: unknown): value is StoredAccountMetadata
);
}
-function readAuthAccountVault(): AuthAccountVault {
+function readAuthAccountVault(options: { allowUnreadable?: boolean } = {}): AuthAccountVault {
const raw = readSafe(AUTH_ACCOUNT_VAULT_KEY);
- if (!raw) return emptyAuthAccountVault();
+ if (raw === null) {
+ if (isPersistedSecretAbsent(AUTH_ACCOUNT_VAULT_KEY)) return emptyAuthAccountVault();
+ log.warn('encrypted auth account vault exists but is temporarily unreadable');
+ if (options.allowUnreadable) return emptyAuthAccountVault();
+ throw new AuthApiError(
+ 'CREDENTIAL_STORE_UNAVAILABLE',
+ 503,
+ 'Saved account credentials are temporarily unavailable',
+ );
+ }
try {
const parsed = JSON.parse(raw) as Partial<AuthAccountVault>;
if (
@@ -615,7 +624,12 @@ function readAuthAccountVault(): AuthAccountVault {
};
} catch (error) {
log.warn('encrypted auth account vault is invalid; ignoring it without deleting', error);
- return emptyAuthAccountVault();
+ if (options.allowUnreadable) return emptyAuthAccountVault();
+ throw new AuthApiError(
+ 'CREDENTIAL_STORE_UNAVAILABLE',
+ 503,
+ 'Saved account credentials could not be read safely',
+ );
}
}
@@ -2494,7 +2508,7 @@ function accountSummaryFromMetadata(
}
export function listSavedAccounts(): DesktopAccountSwitcherSnapshot {
- const vault = readAuthAccountVault();
+ const vault = readAuthAccountVault({ allowUnreadable: true });
const byKey = new Map<string, StoredAccountMetadata>();
for (const [key, resource] of Object.entries(vault.resources)) {
byKey.set(key, resource.metadata);
@@ -4299,7 +4313,9 @@ export async function logout(): Promise<void> {
}
const currentAccessToken = accessToken;
const currentAuthBaseUrl = authServerUrl(activeAuthRealm);
- const savedVault = readAuthAccountVault();
+ // Logout remains available even if the vault cannot be decrypted: local
+ // credential files are removed directly and remote revocation is best effort.
+ const savedVault = readAuthAccountVault({ allowUnreadable: true });
const currentAccountKey = currentUser
? accountVaultKey(activeAuthRealm, currentUser.id)
: savedVault.activeAccountKey;
diff --git a/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx b/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx
index 61b4989be6..a225aa1cfa 100644
--- a/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx
+++ b/apps/desktop/src/renderer/components/sidebar/AccountSwitcherDialog.tsx
@@ -192,9 +192,7 @@ export function AccountSwitcherDialog({
<button
key={account.accountKey}
type="button"
- disabled={
- account.isCurrent || syncing || switchingKey !== null || !mutationAllowed
- }
+ disabled={account.isCurrent || switchingKey !== null || !mutationAllowed}
onClick={() => void handleSwitch(account)}
className={cn(
'flex w-full items-center gap-3 rounded-lg px-3 py-2.5 text-left transition-colors',
@@ -235,7 +233,7 @@ export function AccountSwitcherDialog({
<div className="mt-4 border-t border-[var(--border-default)] pt-4">
<button
type="button"
- disabled={!mutationAllowed || syncing || switchingKey !== null}
+ disabled={!mutationAllowed || switchingKey !== null}
onClick={onAddAccount}
className="flex w-full items-center justify-center gap-2 rounded-lg border border-[var(--border-default)] bg-[var(--surface-elevated)] px-4 py-2.5 text-14 font-medium text-[var(--text-primary)] transition-colors hover:bg-[var(--surface-hover)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--focus-ring)] disabled:cursor-not-allowed disabled:opacity-60"
>
diff --git a/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx b/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx
index e0a7640de2..545df189f2 100644
--- a/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx
+++ b/apps/desktop/src/renderer/components/sidebar/__tests__/AccountSwitcherDialog.test.tsx
@@ -128,6 +128,31 @@ describe('AccountSwitcherDialog', () => {
expect(onAddAccount).toHaveBeenCalledOnce();
});
+ it('keeps switching and add-account actions available during background sync', async () => {
+ syncAccounts.mockReturnValueOnce(new Promise(() => {}));
+ const onAddAccount = vi.fn();
+ render(
+ <AccountSwitcherDialog
+ open
+ onOpenChange={vi.fn()}
+ onAddAccount={onAddAccount}
+ triggerRef={createRef<HTMLButtonElement>()}
+ />,
+ );
+
+ const switchButton = await screen.findByRole('button', {
+ name: /Example CorpOrganization Cindy/,
+ });
+ const addButton = screen.getByRole('button', {
+ name: 'sidebar.accountSwitcher.addAccount',
+ });
+ expect((switchButton as HTMLButtonElement).disabled).toBe(false);
+ expect((addButton as HTMLButtonElement).disabled).toBe(false);
+
+ fireEvent.click(addButton);
+ expect(onAddAccount).toHaveBeenCalledOnce();
+ });
+
it('asks before switching when a task is running and keeps the account dialog open on cancel', async () => {
const onOpenChange = vi.fn();
runningSnapshot.set('running-session', { isRunning: true });