Skip to content

Commit 7aafded

Browse files
jeprecatedclaude
andcommitted
fix(desktop): saved connections survive the Beta rename on macOS and Linux
Electron names the macOS Keychain item and the Linux keyring entry that hold the safeStorage key after the app name Chromium starts with, which was the packaged productName. Renaming the product from "T3 Code (Alpha)" to "T3 Code (Beta)" switched keys, so connection-catalog.json no longer decrypted and the app showed no environments or projects. Pin that name before startup yields to what each channel shipped with ("T3 Code (Alpha)", or "T3 Code (Nightly)"), independent of branding. Catalogs already written under "T3 Code (Beta)" decrypt through the existing browser-import key readers and are re-encrypted under the pinned name. Windows keeps its key in the userData profile and is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 2fae0f9 commit 7aafded

7 files changed

Lines changed: 369 additions & 11 deletions

‎apps/desktop/src/app/DesktopConnectionCatalogStore.test.ts‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import * as DesktopSavedEnvironments from "../settings/DesktopSavedEnvironments.
1717
import * as DesktopConfig from "./DesktopConfig.ts";
1818
import * as DesktopConnectionCatalogStore from "./DesktopConnectionCatalogStore.ts";
1919
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
20+
import * as DesktopSafeStorageIdentity from "./DesktopSafeStorageIdentity.ts";
2021

2122
const textDecoder = new TextDecoder();
2223
const textEncoder = new TextEncoder();
@@ -50,6 +51,20 @@ function layerSafeStorageFor(available: boolean, failDecrypt: Ref.Ref<boolean> |
5051
} satisfies ElectronSafeStorage.ElectronSafeStorage["Service"]);
5152
}
5253

54+
const layerPreviousSafeStorageIdentity = Layer.succeed(
55+
DesktopSafeStorageIdentity.DesktopSafeStorageIdentity,
56+
{
57+
decryptWithPreviousName: (value) => {
58+
const decoded = textDecoder.decode(value);
59+
return Effect.succeed(
60+
decoded.startsWith("previous:")
61+
? Option.some(decoded.slice("previous:".length))
62+
: Option.none(),
63+
);
64+
},
65+
} satisfies DesktopSafeStorageIdentity.DesktopSafeStorageIdentity["Service"],
66+
);
67+
5368
function layerFor(
5469
baseDir: string,
5570
encryptionAvailable = true,
@@ -75,6 +90,7 @@ function layerFor(
7590
const layerDependencies = Layer.mergeAll(
7691
layerEnvironment,
7792
layerSafeStorage,
93+
layerPreviousSafeStorageIdentity,
7894
NodeServices.layer,
7995
fileSystemLayer,
8096
);
@@ -440,4 +456,54 @@ describe("DesktopConnectionCatalogStore", () => {
440456
assert.deepStrictEqual(yield* store.get, Option.some('{"schemaVersion":1,"targets":[]}'));
441457
}).pipe(Effect.provide(NodeServices.layer), Effect.scoped),
442458
);
459+
460+
const writeEncryptedCatalog = (encryptedCatalog: string) =>
461+
Effect.gen(function* () {
462+
const path = yield* Path.Path;
463+
const environment = yield* DesktopEnvironment.DesktopEnvironment;
464+
const fileSystem = yield* FileSystem.FileSystem;
465+
const catalogPath = path.join(environment.stateDir, "connection-catalog.json");
466+
const contents = `${JSON.stringify({
467+
version: 1,
468+
encryptedCatalog: Base64.encode(textEncoder.encode(encryptedCatalog)),
469+
})}\n`;
470+
yield* fileSystem.makeDirectory(environment.stateDir, { recursive: true });
471+
yield* fileSystem.writeFileString(catalogPath, contents);
472+
return { catalogPath, contents };
473+
});
474+
475+
it.effect("re-encrypts a catalog written under the previous app name", () =>
476+
withStore(
477+
Effect.gen(function* () {
478+
const fileSystem = yield* FileSystem.FileSystem;
479+
const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore;
480+
const catalog = '{"schemaVersion":1,"targets":[]}';
481+
const { catalogPath } = yield* writeEncryptedCatalog(`previous:${catalog}`);
482+
483+
assert.deepStrictEqual(yield* store.get, Option.some(catalog));
484+
485+
const document = JSON.parse(yield* fileSystem.readFileString(catalogPath));
486+
const encrypted = yield* Effect.fromResult(Base64.decode(document.encryptedCatalog));
487+
assert.equal(textDecoder.decode(encrypted), `encrypted:${catalog}`);
488+
}),
489+
),
490+
);
491+
492+
it.effect("keeps the catalog when the previous name yields no catalog", () =>
493+
withStore(
494+
Effect.gen(function* () {
495+
const fileSystem = yield* FileSystem.FileSystem;
496+
const store = yield* DesktopConnectionCatalogStore.DesktopConnectionCatalogStore;
497+
const { catalogPath, contents } = yield* writeEncryptedCatalog("previous:\u0007garbage");
498+
499+
const error = yield* store.get.pipe(Effect.flip);
500+
assert.instanceOf(
501+
error,
502+
DesktopConnectionCatalogStore.DesktopConnectionCatalogStoreProtectionError,
503+
);
504+
assert.equal(error.operation, "decrypt-catalog");
505+
assert.equal(yield* fileSystem.readFileString(catalogPath), contents);
506+
}),
507+
),
508+
);
443509
});

‎apps/desktop/src/app/DesktopConnectionCatalogStore.ts‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import * as Schema from "effect/Schema";
2525
import * as ElectronSafeStorage from "../electron/ElectronSafeStorage.ts";
2626
import * as DesktopSavedEnvironments from "../settings/DesktopSavedEnvironments.ts";
2727
import * as DesktopEnvironment from "./DesktopEnvironment.ts";
28+
import * as DesktopSafeStorageIdentity from "./DesktopSafeStorageIdentity.ts";
2829

2930
const EncryptedConnectionCatalogDocument = Schema.Struct({
3031
version: Schema.Literal(1),
@@ -45,6 +46,10 @@ const RuntimeConnectionCatalogDocumentJson = Schema.fromJsonString(
4546
const encodeRuntimeConnectionCatalogDocumentJson = Schema.encodeEffect(
4647
RuntimeConnectionCatalogDocumentJson,
4748
);
49+
// A wrong key can still unpad cleanly, so recovered plaintext must look like a catalog.
50+
const decodeCatalogJsonOption = Schema.decodeOption(
51+
Schema.fromJsonString(Schema.Struct({ schemaVersion: Schema.Literal(1) })),
52+
);
4853

4954
const DesktopConnectionCatalogStoreWriteOperation = Schema.Literals([
5055
"create-temporary-file-name",
@@ -382,6 +387,7 @@ export const make = Effect.gen(function* () {
382387
const fileSystem = yield* FileSystem.FileSystem;
383388
const path = yield* Path.Path;
384389
const safeStorage = yield* ElectronSafeStorage.ElectronSafeStorage;
390+
const safeStorageIdentity = yield* DesktopSafeStorageIdentity.DesktopSafeStorageIdentity;
385391
const crypto = yield* Crypto.Crypto;
386392
const savedEnvironments = yield* DesktopSavedEnvironments.DesktopSavedEnvironments;
387393
const catalogPath = path.join(environment.stateDir, "connection-catalog.json");
@@ -471,6 +477,33 @@ export const make = Effect.gen(function* () {
471477
return Option.some(encoded);
472478
});
473479

480+
// Re-encrypting under the current name means only the first read after an
481+
// upgrade pays for the previous key lookup.
482+
const recoverPreviousIdentityCatalog = Effect.fn(
483+
"desktop.connectionCatalogStore.recoverPreviousIdentityCatalog",
484+
)(function* (
485+
encryptedCatalog: Uint8Array,
486+
cause: ElectronSafeStorage.ElectronSafeStorageDecryptError,
487+
) {
488+
const recovered = Option.filter(
489+
yield* safeStorageIdentity.decryptWithPreviousName(encryptedCatalog),
490+
(catalog) => Option.isSome(decodeCatalogJsonOption(catalog)),
491+
);
492+
if (Option.isNone(recovered)) {
493+
return yield* new DesktopConnectionCatalogStoreProtectionError({
494+
operation: "decrypt-catalog",
495+
catalogPath,
496+
cause,
497+
});
498+
}
499+
yield* writeCatalog(recovered.value).pipe(
500+
Effect.catch((error) =>
501+
Effect.logWarning("Could not re-encrypt the connection catalog.", { catalogPath, error }),
502+
),
503+
);
504+
return recovered.value;
505+
});
506+
474507
return DesktopConnectionCatalogStore.of({
475508
get: Effect.gen(function* () {
476509
const document = yield* readDocument(fileSystem, catalogPath);
@@ -482,16 +515,9 @@ export const make = Effect.gen(function* () {
482515
}
483516
const decrypted = yield* decodeSecretBytes(catalogPath, document.value.encryptedCatalog).pipe(
484517
Effect.flatMap((encryptedCatalog) =>
485-
safeStorage.decryptString(encryptedCatalog).pipe(
486-
Effect.mapError(
487-
(cause) =>
488-
new DesktopConnectionCatalogStoreProtectionError({
489-
operation: "decrypt-catalog",
490-
catalogPath,
491-
cause,
492-
}),
493-
),
494-
),
518+
safeStorage
519+
.decryptString(encryptedCatalog)
520+
.pipe(Effect.catch((cause) => recoverPreviousIdentityCatalog(encryptedCatalog, cause))),
495521
),
496522
);
497523
return Option.some(decrypted);

‎apps/desktop/src/app/DesktopPreReadyPlatform.test.ts‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {
1111
hasSwitchMock,
1212
registerSchemesMock,
1313
setDesktopNameMock,
14+
setNameMock,
1415
mkdirSyncMock,
1516
writeFileSyncMock,
1617
copyFileSyncMock,
@@ -21,6 +22,7 @@ const {
2122
hasSwitchMock: vi.fn(),
2223
registerSchemesMock: vi.fn(),
2324
setDesktopNameMock: vi.fn(),
25+
setNameMock: vi.fn(),
2426
mkdirSyncMock: vi.fn(),
2527
writeFileSyncMock: vi.fn(),
2628
copyFileSyncMock: vi.fn(),
@@ -30,6 +32,7 @@ const {
3032
vi.mock("electron", () => ({
3133
app: {
3234
setDesktopName: setDesktopNameMock,
35+
setName: setNameMock,
3336
getVersion: () => "0.0.37",
3437
isPackaged: true,
3538
getAppPath: () => "/tmp/.mount_T3/resources/app.asar",
@@ -64,6 +67,7 @@ describe("DesktopPreReadyPlatform", () => {
6467
hasSwitchMock.mockReset();
6568
registerSchemesMock.mockReset();
6669
setDesktopNameMock.mockReset();
70+
setNameMock.mockReset();
6771
mkdirSyncMock.mockReset();
6872
writeFileSyncMock.mockReset();
6973
copyFileSyncMock.mockReset();
@@ -186,6 +190,27 @@ describe("DesktopPreReadyPlatform", () => {
186190
).pipe(Effect.ensuring(Effect.sync(() => vi.unstubAllEnvs())));
187191
});
188192

193+
it.effect.each<{ readonly platform: NodeJS.Platform; readonly names: Array<string> }>([
194+
{ platform: "linux", names: ["T3 Code (Alpha)"] },
195+
{ platform: "darwin", names: ["T3 Code (Alpha)"] },
196+
{ platform: "win32", names: [] },
197+
])("names $platform safe storage before startup yields", ({ platform, names }) => {
198+
getSwitchValueMock.mockReturnValue("");
199+
return Effect.scoped(
200+
Effect.gen(function* () {
201+
const namesBeforeYield = Promise.resolve().then(() =>
202+
setNameMock.mock.calls.map(([name]) => name),
203+
);
204+
yield* Layer.build(
205+
DesktopPreReadyPlatform.layer.pipe(
206+
Layer.provide(Layer.succeed(HostProcess.Platform, platform)),
207+
),
208+
);
209+
assert.deepEqual(yield* Effect.promise(() => namesBeforeYield), names);
210+
}),
211+
);
212+
});
213+
189214
it.effect("keeps startup available when the early desktop entry cannot be written", () => {
190215
getSwitchValueMock.mockReturnValue("");
191216
mkdirSyncMock.mockImplementation(() => {

‎apps/desktop/src/app/DesktopPreReadyPlatform.ts‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as HostProcess from "@t3tools/shared/HostProcess";
1212
import * as DesktopEarlyElectronStartup from "./DesktopEarlyElectronStartup.ts";
1313
import { resolveDesktopAppBranding } from "./DesktopEnvironment.ts";
1414
import { renderUrlHandlerDesktopEntry } from "./DesktopLinuxUrlHandler.ts";
15+
import { resolveSafeStorageIdentity } from "./DesktopSafeStorageIdentity.ts";
1516
import * as ElectronProtocol from "../electron/ElectronProtocol.ts";
1617

1718
export interface DesktopPreReadyCommandLineReader {
@@ -54,6 +55,11 @@ export const make = Effect.gen(function* () {
5455
const platform = yield* HostProcess.Platform;
5556
const homeDirectory = yield* HostProcess.HomeDirectory;
5657
return yield* Effect.sync((): DesktopPreReadyElectronOptions["Service"] => {
58+
// Chromium reads the safeStorage key's name before startup first yields.
59+
// DesktopAppIdentity's later rename only changes the branded app name.
60+
if (platform === "darwin" || platform === "linux") {
61+
Electron.app.setName(resolveSafeStorageIdentity(Electron.app.getVersion()).name);
62+
}
5763
const linuxPasswordStoreCommandLine =
5864
platform === "linux"
5965
? readCommandLineSwitchValue(Electron.app.commandLine, "password-store")

0 commit comments

Comments
 (0)