|
| 1 | +// @effect-diagnostics nodeBuiltinImport:off -- These tests assert real POSIX modes on a temporary directory. |
| 2 | +import * as NodeFS from "node:fs"; |
| 3 | +import * as NodeOS from "node:os"; |
| 4 | +import * as NodePath from "node:path"; |
| 5 | +import { assert, describe, it } from "@effect/vitest"; |
| 6 | + |
| 7 | +import { openClerkTokenStorage, type ClerkTokenStorage } from "./ClerkTokenStore.ts"; |
| 8 | + |
| 9 | +// oxlint-disable-next-line t3code/no-global-process-runtime -- Unix mode bits are the assertion; Windows reports them through ACLs. |
| 10 | +const describePosix = describe.skipIf(process.platform === "win32"); |
| 11 | + |
| 12 | +const modeOf = (path: string): number => NodeFS.statSync(path).mode & 0o777; |
| 13 | + |
| 14 | +function writeLoose(path: string, contents: string): void { |
| 15 | + NodeFS.writeFileSync(path, contents, { mode: 0o666 }); |
| 16 | + NodeFS.chmodSync(path, 0o666); |
| 17 | +} |
| 18 | + |
| 19 | +async function withStateDir(run: (stateDir: string) => Promise<void> | void): Promise<void> { |
| 20 | + const stateDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-clerk-tokens-")); |
| 21 | + try { |
| 22 | + await run(stateDir); |
| 23 | + } finally { |
| 24 | + NodeFS.rmSync(stateDir, { recursive: true, force: true }); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +const idleStorage = (): ClerkTokenStorage => ({ |
| 29 | + getItem: async () => null, |
| 30 | + setItem: async () => undefined, |
| 31 | + removeItem: () => undefined, |
| 32 | +}); |
| 33 | + |
| 34 | +describePosix("ClerkTokenStore", () => { |
| 35 | + it("keeps clerk tokens at 0600 inside a 0700 directory", async () => { |
| 36 | + await withStateDir(async (stateDir) => { |
| 37 | + NodeFS.chmodSync(stateDir, 0o755); |
| 38 | + const directory = NodePath.join(stateDir, "clerk"); |
| 39 | + const legacyPath = NodePath.join(stateDir, "clerk-tokens.json"); |
| 40 | + const tokenPath = NodePath.join(directory, "clerk-tokens.json"); |
| 41 | + NodeFS.mkdirSync(directory, { mode: 0o755 }); |
| 42 | + NodeFS.chmodSync(directory, 0o755); |
| 43 | + writeLoose(legacyPath, '{"__clerk_client_jwt":"enc:existing"}\n'); |
| 44 | + |
| 45 | + const opened: string[] = []; |
| 46 | + const storage = openClerkTokenStorage(stateDir, (cwd) => { |
| 47 | + opened.push(cwd); |
| 48 | + return { |
| 49 | + getItem: async () => { |
| 50 | + writeLoose(tokenPath, "from-get"); |
| 51 | + return "from-get"; |
| 52 | + }, |
| 53 | + setItem: (_key, value) => { |
| 54 | + writeLoose(tokenPath, value); |
| 55 | + }, |
| 56 | + removeItem: () => { |
| 57 | + NodeFS.rmSync(tokenPath, { force: true }); |
| 58 | + }, |
| 59 | + }; |
| 60 | + }); |
| 61 | + |
| 62 | + assert.deepEqual(opened, [directory]); |
| 63 | + assert.equal(modeOf(stateDir), 0o755); |
| 64 | + assert.equal(modeOf(directory), 0o700); |
| 65 | + assert.equal(modeOf(tokenPath), 0o600); |
| 66 | + assert.equal(NodeFS.existsSync(legacyPath), false); |
| 67 | + assert.equal( |
| 68 | + NodeFS.readFileSync(tokenPath, "utf8"), |
| 69 | + '{"__clerk_client_jwt":"enc:existing"}\n', |
| 70 | + ); |
| 71 | + |
| 72 | + assert.equal(await storage.getItem("client"), "from-get"); |
| 73 | + assert.equal(modeOf(tokenPath), 0o600); |
| 74 | + await storage.setItem("client", "from-set"); |
| 75 | + assert.equal(modeOf(tokenPath), 0o600); |
| 76 | + assert.equal(NodeFS.readFileSync(tokenPath, "utf8"), "from-set"); |
| 77 | + storage.removeItem("client"); |
| 78 | + assert.equal(NodeFS.existsSync(tokenPath), false); |
| 79 | + |
| 80 | + writeLoose(tokenPath, "kept"); |
| 81 | + writeLoose(legacyPath, "stale"); |
| 82 | + NodeFS.chmodSync(directory, 0o755); |
| 83 | + openClerkTokenStorage(stateDir, () => idleStorage()); |
| 84 | + assert.equal(modeOf(directory), 0o700); |
| 85 | + assert.equal(modeOf(tokenPath), 0o600); |
| 86 | + assert.equal(modeOf(legacyPath), 0o600); |
| 87 | + assert.equal(NodeFS.readFileSync(tokenPath, "utf8"), "kept"); |
| 88 | + assert.equal(NodeFS.readFileSync(legacyPath, "utf8"), "stale"); |
| 89 | + }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("does not follow a symlinked clerk directory or token file", async () => { |
| 93 | + await withStateDir(async (stateDir) => { |
| 94 | + const outsideDirectory = NodePath.join(stateDir, "outside-dir"); |
| 95 | + NodeFS.mkdirSync(outsideDirectory, { mode: 0o755 }); |
| 96 | + NodeFS.chmodSync(outsideDirectory, 0o755); |
| 97 | + NodeFS.symlinkSync(outsideDirectory, NodePath.join(stateDir, "clerk")); |
| 98 | + |
| 99 | + assert.throws(() => openClerkTokenStorage(stateDir, idleStorage), /must be a real directory/); |
| 100 | + assert.equal(modeOf(outsideDirectory), 0o755); |
| 101 | + NodeFS.unlinkSync(NodePath.join(stateDir, "clerk")); |
| 102 | + |
| 103 | + const outsideFile = NodePath.join(stateDir, "outside.json"); |
| 104 | + NodeFS.writeFileSync(outsideFile, "secret", { mode: 0o644 }); |
| 105 | + NodeFS.chmodSync(outsideFile, 0o644); |
| 106 | + NodeFS.symlinkSync(outsideFile, NodePath.join(stateDir, "clerk-tokens.json")); |
| 107 | + assert.throws(() => openClerkTokenStorage(stateDir, idleStorage), /must be a regular file/); |
| 108 | + assert.equal(modeOf(outsideFile), 0o644); |
| 109 | + }); |
| 110 | + }); |
| 111 | +}); |
0 commit comments