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): stop retrying absent legacy secrets

Yuewei-LL37a23eb744b6
diff --git a/apps/desktop/src/main/cindy-brain/__tests__/legacyMigrationRead.test.ts b/apps/desktop/src/main/cindy-brain/__tests__/legacyMigrationRead.test.ts
new file mode 100644
index 0000000000..42413d52e8
--- /dev/null
+++ b/apps/desktop/src/main/cindy-brain/__tests__/legacyMigrationRead.test.ts
@@ -0,0 +1,39 @@
+import { describe, expect, it, vi } from 'vitest';
+
+import { readLegacyEncryptedValue } from '../legacyMigrationRead.js';
+
+describe('readLegacyEncryptedValue', () => {
+ it('classifies an absent legacy file as missing before checking decryption availability', () => {
+ const isDecryptionAvailable = vi.fn(() => false);
+ const result = readLegacyEncryptedValue(
+ () => {
+ throw Object.assign(new Error('missing'), { code: 'ENOENT' });
+ },
+ isDecryptionAvailable,
+ () => 'unused',
+ );
+
+ expect(result).toEqual({ status: 'missing' });
+ expect(isDecryptionAvailable).not.toHaveBeenCalled();
+ });
+
+ it('keeps an existing ciphertext retryable while decryption is unavailable', () => {
+ const decrypt = vi.fn(() => 'unused');
+ const result = readLegacyEncryptedValue(
+ () => 'ciphertext',
+ () => false,
+ decrypt,
+ );
+
+ expect(result).toEqual({ status: 'retryable-failure' });
+ expect(decrypt).not.toHaveBeenCalled();
+ });
+
+ it('returns the decrypted value when ciphertext and the decryptor are available', () => {
+ expect(readLegacyEncryptedValue(
+ () => 'ciphertext',
+ () => true,
+ () => 'legacy-token',
+ )).toEqual({ status: 'available', value: 'legacy-token' });
+ });
+});
diff --git a/apps/desktop/src/main/cindy-brain/index.ts b/apps/desktop/src/main/cindy-brain/index.ts
index 58c8dadb15..4a6644d104 100644
--- a/apps/desktop/src/main/cindy-brain/index.ts
+++ b/apps/desktop/src/main/cindy-brain/index.ts
@@ -192,6 +192,7 @@ import {
LEGACY_MIGRATION_MISSING,
LEGACY_MIGRATION_RETRYABLE_FAILURE,
legacyMigrationAvailable,
+ readLegacyEncryptedValue,
type LegacyMigrationRead,
} from './legacyMigrationRead.js';
import { GHOST_SCHEME, ghostExternalLinkUrls, parseGhostPartition } from '../../shared/ghost.js';
@@ -4723,17 +4724,11 @@ function readLegacyJson<T>(
}
function readLegacyEncryptedSecret(file: string): LegacyMigrationRead<string> {
- try {
- if (!safeStorage.isEncryptionAvailable()) return LEGACY_MIGRATION_RETRYABLE_FAILURE;
- const value = safeStorage.decryptString(Buffer.from(fs.readFileSync(file, 'utf-8'), 'base64'));
- return value.length > 0
- ? legacyMigrationAvailable(value)
- : LEGACY_MIGRATION_RETRYABLE_FAILURE;
- } catch (error) {
- return (error as NodeJS.ErrnoException).code === 'ENOENT'
- ? LEGACY_MIGRATION_MISSING
- : LEGACY_MIGRATION_RETRYABLE_FAILURE;
- }
+ return readLegacyEncryptedValue(
+ () => fs.readFileSync(file, 'utf-8'),
+ () => safeStorage.isEncryptionAvailable(),
+ (encoded) => safeStorage.decryptString(Buffer.from(encoded, 'base64')),
+ );
}
export function registerGhostIpc(): void {
diff --git a/apps/desktop/src/main/cindy-brain/legacyMigrationRead.ts b/apps/desktop/src/main/cindy-brain/legacyMigrationRead.ts
index daccb184aa..cc9dce5fac 100644
--- a/apps/desktop/src/main/cindy-brain/legacyMigrationRead.ts
+++ b/apps/desktop/src/main/cindy-brain/legacyMigrationRead.ts
@@ -16,3 +16,32 @@ export const LEGACY_MIGRATION_MISSING: LegacyMigrationRead<never> = { status: 'm
export const LEGACY_MIGRATION_RETRYABLE_FAILURE: LegacyMigrationRead<never> = {
status: 'retryable-failure',
};
+
+/**
+ * Read legacy ciphertext before consulting the platform decryptor. A missing
+ * file is a completed no-op even while safeStorage is unavailable; only a
+ * ciphertext that exists but cannot currently be decrypted remains retryable.
+ */
+export function readLegacyEncryptedValue(
+ readEncoded: () => string,
+ isDecryptionAvailable: () => boolean,
+ decrypt: (encoded: string) => string,
+): LegacyMigrationRead<string> {
+ let encoded: string;
+ try {
+ encoded = readEncoded();
+ } catch (error) {
+ return (error as NodeJS.ErrnoException).code === 'ENOENT'
+ ? LEGACY_MIGRATION_MISSING
+ : LEGACY_MIGRATION_RETRYABLE_FAILURE;
+ }
+ if (!isDecryptionAvailable()) return LEGACY_MIGRATION_RETRYABLE_FAILURE;
+ try {
+ const value = decrypt(encoded);
+ return value.length > 0
+ ? legacyMigrationAvailable(value)
+ : LEGACY_MIGRATION_RETRYABLE_FAILURE;
+ } catch {
+ return LEGACY_MIGRATION_RETRYABLE_FAILURE;
+ }
+}
diff --git a/apps/desktop/src/main/secrets/__tests__/providerSecretStore.test.ts b/apps/desktop/src/main/secrets/__tests__/providerSecretStore.test.ts
index e3a3a58019..0fc861fd07 100644
--- a/apps/desktop/src/main/secrets/__tests__/providerSecretStore.test.ts
+++ b/apps/desktop/src/main/secrets/__tests__/providerSecretStore.test.ts
@@ -35,6 +35,7 @@ vi.mock('../../appSessionState', () => ({
import {
createProviderSecretStore,
readCustomProviderKeyForMutation,
+ readGhostSecretStrict,
readGhostSecretTailFromIo,
setProviderSecretsClearedListener,
type SecretStorageIo,
@@ -175,6 +176,21 @@ describe('providerSecrets registry', () => {
sessionState.dataOwnerId = previousOwnerId;
}
});
+
+ it('strict Ghost reconciliation treats a missing file as absent when encryption is unavailable', () => {
+ const previousMode = sessionState.mode;
+ const previousOwnerId = sessionState.dataOwnerId;
+ sessionState.mode = 'cloud';
+ sessionState.dataOwnerId = `missing-ghost-owner-${process.pid}`;
+ try {
+ expect(
+ readGhostSecretStrict(`missing-ghost-${process.pid}`, 'oauth_accounts'),
+ ).toBeNull();
+ } finally {
+ sessionState.mode = previousMode;
+ sessionState.dataOwnerId = previousOwnerId;
+ }
+ });
});
describe('providerSecretStore', () => {
diff --git a/apps/desktop/src/main/secrets/providerSecretStore.ts b/apps/desktop/src/main/secrets/providerSecretStore.ts
index 360c5eda60..eec8387aee 100644
--- a/apps/desktop/src/main/secrets/providerSecretStore.ts
+++ b/apps/desktop/src/main/secrets/providerSecretStore.ts
@@ -646,16 +646,18 @@ export function readGhostSecretStrict(ghostId: string, secretKey: string): strin
ghostSecretStorageKey(ghostId, secretKey),
);
if (!physicalKey) return null;
- if (!safeStorage.isEncryptionAvailable()) {
- throw new Error('safeStorage is unavailable');
- }
const filepath = path.join(secretDir(), `${physicalKey}.enc`);
+ let encoded: string;
try {
- return safeStorage.decryptString(Buffer.from(fs.readFileSync(filepath, 'utf-8'), 'base64'));
+ encoded = fs.readFileSync(filepath, 'utf-8');
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return null;
throw error;
}
+ if (!safeStorage.isEncryptionAvailable()) {
+ throw new Error('safeStorage is unavailable');
+ }
+ return safeStorage.decryptString(Buffer.from(encoded, 'base64'));
}
/**