Skip to content

Commit 7dd9e7e

Browse files
committed
fix(desktop): migrate Clerk tokens from relative legacy paths
1 parent 63fbcb2 commit 7dd9e7e

3 files changed

Lines changed: 60 additions & 21 deletions

File tree

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import * as NodeFS from "node:fs";
33
import * as NodeOS from "node:os";
44
import * as NodePath from "node:path";
55
import { assert, describe, it } from "@effect/vitest";
6+
import { vi } from "vite-plus/test";
7+
8+
vi.mock("node:fs", { spy: true });
69

710
import { openClerkTokenStorage, type ClerkTokenStorage } from "./ClerkTokenStore.ts";
811

@@ -122,6 +125,48 @@ describePosix("ClerkTokenStore", () => {
122125
});
123126
});
124127

128+
it("migrates relative state paths from Electron userData, not the working directory", async () => {
129+
await withStateDir((stateDir) => {
130+
const relativeStateDir = NodePath.relative(process.cwd(), stateDir);
131+
const userData = NodePath.join(
132+
stateDir,
133+
"electron",
134+
...relativeStateDir.split(NodePath.sep).map(() => "profile"),
135+
);
136+
const legacyStateDir = NodePath.join(userData, relativeStateDir);
137+
const legacyPath = NodePath.join(legacyStateDir, "clerk-tokens.json");
138+
NodeFS.mkdirSync(legacyStateDir, { recursive: true });
139+
writeLoose(legacyPath, "existing-session");
140+
const unrelatedPath = NodePath.join(stateDir, "clerk-tokens.json");
141+
writeLoose(unrelatedPath, "not-the-electron-session");
142+
143+
openClerkTokenStorage(relativeStateDir, idleStorage, userData);
144+
145+
const tokenPath = NodePath.join(stateDir, "clerk", "clerk-tokens.json");
146+
assert.equal(NodeFS.readFileSync(tokenPath, "utf8"), "existing-session");
147+
assert.equal(modeOf(tokenPath), 0o600);
148+
assert.equal(NodeFS.existsSync(legacyPath), false);
149+
assert.equal(NodeFS.readFileSync(unrelatedPath, "utf8"), "not-the-electron-session");
150+
});
151+
});
152+
153+
it("migrates tokens when the legacy directory is on another filesystem", async () => {
154+
await withStateDir((stateDir) => {
155+
const legacyPath = NodePath.join(stateDir, "clerk-tokens.json");
156+
writeLoose(legacyPath, "existing-session");
157+
using _rename = vi.spyOn(NodeFS, "renameSync").mockImplementation(() => {
158+
throw Object.assign(new Error("cross-device link"), { code: "EXDEV" });
159+
});
160+
161+
openClerkTokenStorage(stateDir, idleStorage);
162+
163+
const tokenPath = NodePath.join(stateDir, "clerk", "clerk-tokens.json");
164+
assert.equal(NodeFS.readFileSync(tokenPath, "utf8"), "existing-session");
165+
assert.equal(modeOf(tokenPath), 0o600);
166+
assert.equal(NodeFS.existsSync(legacyPath), false);
167+
});
168+
});
169+
125170
it("does not follow a symlinked clerk directory or token file", async () => {
126171
await withStateDir(async (stateDir) => {
127172
const outsideDirectory = NodePath.join(stateDir, "outside-dir");

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

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,7 @@
22
import * as NodeFS from "node:fs";
33
import * as NodePath from "node:path";
44

5-
/**
6-
* `@clerk/electron/storage` persists tokens with electron-store. Its `conf`
7-
* dependency writes `clerk-tokens.json` using `configFileMode` `0o666` and
8-
* applies that mode with chmod, so the process umask never tightens it and the
9-
* next `store.set` puts `0666` back. A `0700` directory is the boundary that
10-
* survives those writes. `0600` is reapplied after every storage call, and an
11-
* existing token file in the state directory is tightened and moved in.
12-
*
13-
* electron-store 8 joins a relative `cwd` onto `app.getPath('userData')`, so
14-
* `stateDir` is resolved before the private directory is created. Otherwise the
15-
* token file would miss that directory.
16-
*
17-
* Windows ignores Unix mode bits except for the read-only attribute. Other
18-
* users are kept out by the profile ACL; `0600` still leaves the owner write bit set.
19-
*/
20-
5+
// electron-store chmods every write to 0666; the private directory protects tokens during writes.
216
const CLERK_TOKEN_DIRECTORY_MODE = 0o700;
227
const CLERK_TOKEN_FILE_MODE = 0o600;
238
const CLERK_TOKEN_DIRECTORY_NAME = "clerk";
@@ -71,7 +56,9 @@ function migrateLegacyClerkTokenFile(stateDir: string, directory: string): void
7156
if (destination === undefined) {
7257
if (legacy === undefined) return;
7358
try {
74-
NodeFS.renameSync(legacyPath, tokenPath);
59+
NodeFS.copyFileSync(legacyPath, tokenPath, NodeFS.constants.COPYFILE_EXCL);
60+
NodeFS.chmodSync(tokenPath, CLERK_TOKEN_FILE_MODE);
61+
NodeFS.unlinkSync(legacyPath);
7562
} catch (error) {
7663
if (!isErrno(error, "ENOENT")) throw error;
7764
}
@@ -105,6 +92,7 @@ function restrictAfter<T>(run: () => Awaitable<T>, tighten: () => void): Awaitab
10592
export function openClerkTokenStorage(
10693
stateDir: string,
10794
createStorage: (directory: string) => ClerkTokenStorage,
95+
userDataPath = ".",
10896
): ClerkTokenStorage {
10997
const absoluteStateDir = NodePath.resolve(stateDir);
11098
const directory = NodePath.join(absoluteStateDir, CLERK_TOKEN_DIRECTORY_NAME);
@@ -114,7 +102,8 @@ export function openClerkTokenStorage(
114102
throw new Error(`Clerk token directory "${directory}" must be a real directory.`);
115103
}
116104
NodeFS.chmodSync(directory, CLERK_TOKEN_DIRECTORY_MODE);
117-
migrateLegacyClerkTokenFile(absoluteStateDir, directory);
105+
// electron-store rebases relative paths under Electron userData, not the working directory.
106+
migrateLegacyClerkTokenFile(NodePath.resolve(userDataPath, stateDir), directory);
118107
const tokenPath = NodePath.join(directory, CLERK_TOKEN_FILE_NAME);
119108
restrictClerkTokenFile(tokenPath);
120109

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,13 @@ export const desktopClerkFrontendApiHostname = resolveDesktopClerkFrontendApiHos
8484
: __T3CODE_BUILD_CLERK_PUBLISHABLE_KEY__,
8585
);
8686

87-
function createDesktopClerkBridge(stateDir: string, isDevelopment: boolean) {
87+
function createDesktopClerkBridge(stateDir: string, isDevelopment: boolean, userDataPath: string) {
8888
return createClerkBridge({
89-
storage: openClerkTokenStorage(stateDir, (directory) => storage({ path: directory })),
89+
storage: openClerkTokenStorage(
90+
stateDir,
91+
(directory) => storage({ path: directory }),
92+
userDataPath,
93+
),
9094
manageSingleInstanceLock: false,
9195
passkeys: true,
9296
renderer: {
@@ -122,7 +126,8 @@ export const make = Effect.gen(function* () {
122126

123127
yield* Effect.acquireRelease(
124128
Effect.try({
125-
try: () => createDesktopClerkBridge(environment.stateDir, environment.isDevelopment),
129+
try: () =>
130+
createDesktopClerkBridge(environment.stateDir, environment.isDevelopment, userDataPath),
126131
catch: (cause) =>
127132
new DesktopClerkBridgeInitializationError({
128133
stateDir: environment.stateDir,

0 commit comments

Comments
 (0)