From b5ac0b3e2da5fe14f7e50907f881ad74c9159eb3 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Thu, 9 Apr 2026 18:44:23 -0700 Subject: [PATCH] Consolidate saved environment secret storage - Store encrypted bearer tokens alongside the saved-environment registry - Preserve secrets when rewriting registry metadata - Update desktop IPC paths and coverage --- apps/desktop/src/clientPersistence.test.ts | 53 ++++++++--- apps/desktop/src/clientPersistence.ts | 104 ++++++++++++++++----- apps/desktop/src/main.ts | 7 +- 3 files changed, 122 insertions(+), 42 deletions(-) diff --git a/apps/desktop/src/clientPersistence.test.ts b/apps/desktop/src/clientPersistence.test.ts index df1b5d550b..b6b80c2e38 100644 --- a/apps/desktop/src/clientPersistence.test.ts +++ b/apps/desktop/src/clientPersistence.test.ts @@ -84,12 +84,12 @@ describe("clientPersistence", () => { }); it("persists encrypted saved environment secrets when encryption is available", () => { - const secretsPath = makeTempPath("saved-environment-secrets.json"); + const registryPath = makeTempPath("saved-environments.json"); const secretStorage = makeSecretStorage(true); expect( writeSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secret: "bearer-token", secretStorage, @@ -98,7 +98,7 @@ describe("clientPersistence", () => { expect( readSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secretStorage, }), @@ -106,11 +106,11 @@ describe("clientPersistence", () => { }); it("preserves existing secrets when encryption is unavailable", () => { - const secretsPath = makeTempPath("saved-environment-secrets.json"); + const registryPath = makeTempPath("saved-environments.json"); const availableSecretStorage = makeSecretStorage(true); writeSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secret: "bearer-token", secretStorage: availableSecretStorage, @@ -118,7 +118,7 @@ describe("clientPersistence", () => { expect( writeSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secret: "next-token", secretStorage: makeSecretStorage(false), @@ -127,7 +127,7 @@ describe("clientPersistence", () => { expect( readSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secretStorage: availableSecretStorage, }), @@ -135,24 +135,24 @@ describe("clientPersistence", () => { }); it("removes saved environment secrets", () => { - const secretsPath = makeTempPath("saved-environment-secrets.json"); + const registryPath = makeTempPath("saved-environments.json"); const secretStorage = makeSecretStorage(true); writeSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secret: "bearer-token", secretStorage, }); removeSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, }); expect( readSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secretStorage, }), @@ -160,12 +160,12 @@ describe("clientPersistence", () => { }); it("treats malformed secrets documents as empty", () => { - const secretsPath = makeTempPath("saved-environment-secrets.json"); - fs.writeFileSync(secretsPath, "{}\n", "utf8"); + const registryPath = makeTempPath("saved-environments.json"); + fs.writeFileSync(registryPath, "{}\n", "utf8"); expect( readSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, secretStorage: makeSecretStorage(true), }), @@ -173,9 +173,32 @@ describe("clientPersistence", () => { expect(() => removeSavedEnvironmentSecret({ - secretsPath, + registryPath, environmentId: savedRegistryRecord.environmentId, }), ).not.toThrow(); }); + + it("preserves encrypted secrets when metadata is rewritten", () => { + const registryPath = makeTempPath("saved-environments.json"); + const secretStorage = makeSecretStorage(true); + + writeSavedEnvironmentSecret({ + registryPath, + environmentId: savedRegistryRecord.environmentId, + secret: "bearer-token", + secretStorage, + }); + + writeSavedEnvironmentRegistry(registryPath, [savedRegistryRecord]); + + expect(readSavedEnvironmentRegistry(registryPath)).toEqual([savedRegistryRecord]); + expect( + readSavedEnvironmentSecret({ + registryPath, + environmentId: savedRegistryRecord.environmentId, + secretStorage, + }), + ).toBe("bearer-token"); + }); }); diff --git a/apps/desktop/src/clientPersistence.ts b/apps/desktop/src/clientPersistence.ts index 00b0a56bca..6f44515b7c 100644 --- a/apps/desktop/src/clientPersistence.ts +++ b/apps/desktop/src/clientPersistence.ts @@ -8,12 +8,13 @@ interface ClientSettingsDocument { readonly settings: ClientSettings; } -interface SavedEnvironmentRegistryDocument { - readonly records: readonly PersistedSavedEnvironmentRecord[]; +interface PersistedSavedEnvironmentStorageRecord extends PersistedSavedEnvironmentRecord { + readonly encryptedBearerToken?: string; } -interface SavedEnvironmentSecretsDocument { - readonly byId: Readonly>; +interface SavedEnvironmentRegistryDocument { + readonly records: readonly PersistedSavedEnvironmentStorageRecord[]; + readonly encryptedBearerTokenById: Readonly>; } export interface DesktopSecretStorage { @@ -41,9 +42,55 @@ function writeJsonFile(filePath: string, value: unknown): void { FS.renameSync(tempPath, filePath); } -function readSecretsDocument(filePath: string): SavedEnvironmentSecretsDocument { - const parsed = readJsonFile(filePath); - return Predicate.isObject(parsed) && Predicate.isObject(parsed.byId) ? parsed : { byId: {} }; +function isPersistedSavedEnvironmentStorageRecord( + value: unknown, +): value is PersistedSavedEnvironmentStorageRecord { + return ( + Predicate.isObject(value) && + typeof value.environmentId === "string" && + typeof value.label === "string" && + typeof value.httpBaseUrl === "string" && + typeof value.wsBaseUrl === "string" && + typeof value.createdAt === "string" && + (value.lastConnectedAt === null || typeof value.lastConnectedAt === "string") && + (value.encryptedBearerToken === undefined || typeof value.encryptedBearerToken === "string") + ); +} + +function readSavedEnvironmentRegistryDocument(filePath: string): SavedEnvironmentRegistryDocument { + const parsed = readJsonFile(filePath); + if (!Predicate.isObject(parsed)) { + return { records: [], encryptedBearerTokenById: {} }; + } + + const records = Array.isArray(parsed.records) + ? parsed.records.filter(isPersistedSavedEnvironmentStorageRecord) + : []; + const encryptedBearerTokenById = Predicate.isObject(parsed.encryptedBearerTokenById) + ? Object.fromEntries( + Object.entries(parsed.encryptedBearerTokenById).filter( + (entry): entry is [string, string] => typeof entry[1] === "string", + ), + ) + : {}; + + return { + records, + encryptedBearerTokenById, + }; +} + +function toPersistedSavedEnvironmentRecord( + record: PersistedSavedEnvironmentStorageRecord, +): PersistedSavedEnvironmentRecord { + return { + environmentId: record.environmentId, + label: record.label, + httpBaseUrl: record.httpBaseUrl, + wsBaseUrl: record.wsBaseUrl, + createdAt: record.createdAt, + lastConnectedAt: record.lastConnectedAt, + }; } export function readClientSettings(settingsPath: string): ClientSettings | null { @@ -57,22 +104,30 @@ export function writeClientSettings(settingsPath: string, settings: ClientSettin export function readSavedEnvironmentRegistry( registryPath: string, ): readonly PersistedSavedEnvironmentRecord[] { - return readJsonFile(registryPath)?.records ?? []; + return readSavedEnvironmentRegistryDocument(registryPath).records.map((record) => + toPersistedSavedEnvironmentRecord(record), + ); } export function writeSavedEnvironmentRegistry( registryPath: string, records: readonly PersistedSavedEnvironmentRecord[], ): void { - writeJsonFile(registryPath, { records } satisfies SavedEnvironmentRegistryDocument); + const currentDocument = readSavedEnvironmentRegistryDocument(registryPath); + const encryptedBearerTokenById = currentDocument.encryptedBearerTokenById; + writeJsonFile(registryPath, { + records, + encryptedBearerTokenById, + } satisfies SavedEnvironmentRegistryDocument); } export function readSavedEnvironmentSecret(input: { - readonly secretsPath: string; + readonly registryPath: string; readonly environmentId: string; readonly secretStorage: DesktopSecretStorage; }): string | null { - const encoded = readSecretsDocument(input.secretsPath).byId[input.environmentId]; + const document = readSavedEnvironmentRegistryDocument(input.registryPath); + const encoded = document.encryptedBearerTokenById[input.environmentId]; if (!encoded) { return null; } @@ -89,36 +144,39 @@ export function readSavedEnvironmentSecret(input: { } export function writeSavedEnvironmentSecret(input: { - readonly secretsPath: string; + readonly registryPath: string; readonly environmentId: string; readonly secret: string; readonly secretStorage: DesktopSecretStorage; }): boolean { - const document = readSecretsDocument(input.secretsPath); - const { [input.environmentId]: _previous, ...remaining } = document.byId; + const document = readSavedEnvironmentRegistryDocument(input.registryPath); if (!input.secretStorage.isEncryptionAvailable()) { return false; } - writeJsonFile(input.secretsPath, { - byId: { - ...remaining, + writeJsonFile(input.registryPath, { + records: document.records.map((record) => toPersistedSavedEnvironmentRecord(record)), + encryptedBearerTokenById: { + ...document.encryptedBearerTokenById, [input.environmentId]: input.secretStorage.encryptString(input.secret).toString("base64"), }, - } satisfies SavedEnvironmentSecretsDocument); + } satisfies SavedEnvironmentRegistryDocument); return true; } export function removeSavedEnvironmentSecret(input: { - readonly secretsPath: string; + readonly registryPath: string; readonly environmentId: string; }): void { - const document = readSecretsDocument(input.secretsPath); - if (!(Predicate.isObject(document.byId) && input.environmentId in document.byId)) { + const document = readSavedEnvironmentRegistryDocument(input.registryPath); + if (!(input.environmentId in document.encryptedBearerTokenById)) { return; } - const { [input.environmentId]: _removed, ...remaining } = document.byId; - writeJsonFile(input.secretsPath, { byId: remaining } satisfies SavedEnvironmentSecretsDocument); + const { [input.environmentId]: _removed, ...remaining } = document.encryptedBearerTokenById; + writeJsonFile(input.registryPath, { + records: document.records.map((record) => toPersistedSavedEnvironmentRecord(record)), + encryptedBearerTokenById: remaining, + } satisfies SavedEnvironmentRegistryDocument); } diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts index aa94b8d47e..88527d4b96 100644 --- a/apps/desktop/src/main.ts +++ b/apps/desktop/src/main.ts @@ -97,7 +97,6 @@ const STATE_DIR = Path.join(BASE_DIR, "userdata"); const DESKTOP_SETTINGS_PATH = Path.join(STATE_DIR, "desktop-settings.json"); const CLIENT_SETTINGS_PATH = Path.join(STATE_DIR, "client-settings.json"); const SAVED_ENVIRONMENT_REGISTRY_PATH = Path.join(STATE_DIR, "saved-environments.json"); -const SAVED_ENVIRONMENT_SECRETS_PATH = Path.join(STATE_DIR, "saved-environment-secrets.json"); const DESKTOP_SCHEME = "t3"; const ROOT_DIR = Path.resolve(__dirname, "../../.."); const isDevelopment = Boolean(process.env.VITE_DEV_SERVER_URL); @@ -1411,7 +1410,7 @@ function registerIpcHandlers(): void { } return readSavedEnvironmentSecret({ - secretsPath: SAVED_ENVIRONMENT_SECRETS_PATH, + registryPath: SAVED_ENVIRONMENT_REGISTRY_PATH, environmentId: rawEnvironmentId, secretStorage: getDesktopSecretStorage(), }); @@ -1430,7 +1429,7 @@ function registerIpcHandlers(): void { } return writeSavedEnvironmentSecret({ - secretsPath: SAVED_ENVIRONMENT_SECRETS_PATH, + registryPath: SAVED_ENVIRONMENT_REGISTRY_PATH, environmentId: rawEnvironmentId, secret: rawSecret, secretStorage: getDesktopSecretStorage(), @@ -1447,7 +1446,7 @@ function registerIpcHandlers(): void { } removeSavedEnvironmentSecret({ - secretsPath: SAVED_ENVIRONMENT_SECRETS_PATH, + registryPath: SAVED_ENVIRONMENT_REGISTRY_PATH, environmentId: rawEnvironmentId, }); },