Skip to content

Commit 15697be

Browse files
committed
fix(desktop): drop the legacy Clerk token file when the private copy exists
Migration left the legacy token file in place whenever the private copy was already there, so the old credential stayed outside the private directory. After the private copy is locked down, the legacy file is removed. ENOENT during that removal is ignored.
1 parent 6f59e34 commit 15697be

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

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

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,26 @@ describePosix("ClerkTokenStore", () => {
8383
openClerkTokenStorage(stateDir, () => idleStorage());
8484
assert.equal(modeOf(directory), 0o700);
8585
assert.equal(modeOf(tokenPath), 0o600);
86-
assert.equal(modeOf(legacyPath), 0o600);
86+
assert.equal(NodeFS.existsSync(legacyPath), false);
8787
assert.equal(NodeFS.readFileSync(tokenPath, "utf8"), "kept");
88-
assert.equal(NodeFS.readFileSync(legacyPath, "utf8"), "stale");
88+
});
89+
});
90+
91+
it("removes the legacy token file when the private copy already exists", async () => {
92+
await withStateDir((stateDir) => {
93+
const directory = NodePath.join(stateDir, "clerk");
94+
const legacyPath = NodePath.join(stateDir, "clerk-tokens.json");
95+
const tokenPath = NodePath.join(directory, "clerk-tokens.json");
96+
NodeFS.mkdirSync(directory, { mode: 0o755 });
97+
writeLoose(legacyPath, "stale-credential");
98+
writeLoose(tokenPath, "kept-credential");
99+
100+
openClerkTokenStorage(stateDir, () => idleStorage());
101+
102+
assert.equal(NodeFS.existsSync(legacyPath), false);
103+
assert.equal(modeOf(directory), 0o700);
104+
assert.equal(modeOf(tokenPath), 0o600);
105+
assert.equal(NodeFS.readFileSync(tokenPath, "utf8"), "kept-credential");
89106
});
90107
});
91108

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ function migrateLegacyClerkTokenFile(stateDir: string, directory: string): void
8080

8181
assertRegularFile(tokenPath, destination);
8282
NodeFS.chmodSync(tokenPath, CLERK_TOKEN_FILE_MODE);
83+
if (legacy === undefined) return;
84+
try {
85+
NodeFS.unlinkSync(legacyPath);
86+
} catch (error) {
87+
if (!isErrno(error, "ENOENT")) throw error;
88+
}
8389
}
8490

8591
function restrictAfter<T>(run: () => Awaitable<T>, tighten: () => void): Awaitable<T> {

0 commit comments

Comments
 (0)