Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/electron-token-file-mode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/electron': patch
---

The token file written by `storage()` from `@clerk/electron/storage` is now readable and writable only by its owner (`0600`). It was previously world-writable (`0666`). Existing token files are updated the next time `storage()` is called.
27 changes: 24 additions & 3 deletions packages/electron/src/storage/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { chmodSync } from 'node:fs';

import { safeStorage } from 'electron';
import Store from 'electron-store';
import { beforeEach, describe, expect, it, vi } from 'vitest';
Expand All @@ -21,9 +23,14 @@ vi.mock('electron', () => ({
safeStorage: {},
}));

vi.mock('node:fs', () => ({
chmodSync: vi.fn(),
}));

vi.mock('electron-store', () => ({
default: vi.fn(function () {
return {
path: '/tmp/clerk/clerk-tokens.json',
get: storeGet,
set: storeSet,
delete: storeDelete,
Expand Down Expand Up @@ -72,26 +79,40 @@ describe('storage options', () => {
it('creates an electron-store instance with the default store name', () => {
storage();

expect(Store).toHaveBeenCalledWith({ name: 'clerk-tokens' });
expect(Store).toHaveBeenCalledWith({ name: 'clerk-tokens', configFileMode: 0o600 });
});

it('supports a custom store name', () => {
storage({ name: 'custom-clerk-tokens' });

expect(Store).toHaveBeenCalledWith({ name: 'custom-clerk-tokens' });
expect(Store).toHaveBeenCalledWith({ name: 'custom-clerk-tokens', configFileMode: 0o600 });
});

it('forwards a custom path as electron-store `cwd`', () => {
storage({ path: '/tmp/clerk' });

expect(Store).toHaveBeenCalledWith({ name: 'clerk-tokens', cwd: '/tmp/clerk' });
expect(Store).toHaveBeenCalledWith({ name: 'clerk-tokens', configFileMode: 0o600, cwd: '/tmp/clerk' });
});

it('omits `cwd` when no path is provided', () => {
storage();

expect(Store).toHaveBeenCalledWith(expect.not.objectContaining({ cwd: expect.anything() }));
});

it('restricts an existing token file to owner-only access', () => {
storage();

expect(chmodSync).toHaveBeenCalledWith('/tmp/clerk/clerk-tokens.json', 0o600);
});

it('does not throw when the token file cannot be chmodded', () => {
vi.mocked(chmodSync).mockImplementationOnce(() => {
throw new Error('ENOENT');
});

expect(() => storage()).not.toThrow();
});
});

describe('getItem', () => {
Expand Down
8 changes: 8 additions & 0 deletions packages/electron/src/storage/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { chmodSync } from 'node:fs';

import { safeStorage } from 'electron';
import Store from 'electron-store';

Expand Down Expand Up @@ -125,8 +127,14 @@ async function resolveCipher(): Promise<Cipher | null> {
export function storage(options: StorageOptions = {}): TokenStorage {
const store = new Store<Record<string, string>>({
name: options.name ?? 'clerk-tokens',
configFileMode: 0o600,
...(options.path ? { cwd: options.path } : {}),
});
try {
chmodSync(store.path, 0o600);
} catch {
/* the file does not exist yet, or it belongs to another user */
}
Comment thread
wobsoriano marked this conversation as resolved.
const memoryFallback = new Map<string, string>();
// IPC requests can resolve out of order, so only the latest mutation may update storage.
const mutationVersions = new Map<string, number>();
Expand Down
Loading