Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01VDJVpPiGiWjJP1yVvvGhzQ
This commit is contained in:
@@ -1,42 +1,47 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { mkdtemp, readFile, rm, stat } from 'node:fs/promises';
|
||||
import { mkdtemp, readFile, rm, stat, writeFile } from 'node:fs/promises';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { createSessionStore } from '../session-store.js';
|
||||
|
||||
const SECRET = 'unit-test-runtime-secret';
|
||||
|
||||
test('sessions survive a restart; the file has no raw session id, no caches and no expired sessions', async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'pm-board-sessions-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const path = join(dir, 'sessions.json');
|
||||
let clock = 1_000_000;
|
||||
const store = createSessionStore(path, { now: () => clock });
|
||||
store.set('raw-cookie-value', { claims: { employee_number: '100' }, expiresAt: clock + 1000, accessToken: 'a', refreshToken: 'r', rosterCache: { records: ['secret'] }, permissionCache: { value: 1 } });
|
||||
const store = createSessionStore(path, { now: () => clock, secret: SECRET });
|
||||
store.set('raw-cookie-value', { claims: { employee_number: '100' }, expiresAt: clock + 1000, accessToken: 'ACCESS-TOKEN-VALUE', refreshToken: 'REFRESH-TOKEN-VALUE', rosterCache: { records: ['secret'] }, permissionCache: { value: 1 } });
|
||||
store.set('old-cookie', { claims: { employee_number: '200' }, expiresAt: clock - 1 });
|
||||
store.flush();
|
||||
const text = await readFile(path, 'utf8');
|
||||
assert.ok(!text.includes('raw-cookie-value'));
|
||||
assert.ok(!text.includes('rosterCache') && !text.includes('secret') && !text.includes('permissionCache'));
|
||||
assert.ok(!text.includes('"200"'));
|
||||
// 登入資料加密:檔案裡看不到 token、工號。
|
||||
assert.ok(!text.includes('REFRESH-TOKEN-VALUE') && !text.includes('ACCESS-TOKEN-VALUE') && !text.includes('employee_number'));
|
||||
assert.equal(JSON.parse(text).version, 2);
|
||||
assert.equal((await stat(path)).mode & 0o777, 0o600);
|
||||
|
||||
const restarted = createSessionStore(path, { now: () => clock });
|
||||
const restarted = createSessionStore(path, { now: () => clock, secret: SECRET });
|
||||
assert.equal(restarted.load(), 1);
|
||||
assert.equal(restarted.get('raw-cookie-value').claims.employee_number, '100');
|
||||
assert.equal(restarted.get('raw-cookie-value').refreshToken, 'r');
|
||||
assert.equal(restarted.get('raw-cookie-value').refreshToken, 'REFRESH-TOKEN-VALUE');
|
||||
assert.equal(restarted.get('old-cookie'), undefined);
|
||||
|
||||
// 登出後重新啟動,也不會回來。
|
||||
restarted.delete('raw-cookie-value');
|
||||
restarted.flush();
|
||||
assert.equal(createSessionStore(path, { now: () => clock }).load(), 0);
|
||||
assert.equal(createSessionStore(path, { now: () => clock, secret: SECRET }).load(), 0);
|
||||
|
||||
// 過期的不載入。
|
||||
const later = createSessionStore(path, { now: () => clock });
|
||||
const later = createSessionStore(path, { now: () => clock, secret: SECRET });
|
||||
later.set('x', { claims: {}, expiresAt: clock + 10 });
|
||||
later.flush();
|
||||
clock += 20;
|
||||
assert.equal(createSessionStore(path, { now: () => clock }).load(), 0);
|
||||
assert.equal(createSessionStore(path, { now: () => clock, secret: SECRET }).load(), 0);
|
||||
});
|
||||
|
||||
test('without a data folder (local preview) sessions stay in memory only', () => {
|
||||
@@ -46,3 +51,35 @@ test('without a data folder (local preview) sessions stay in memory only', () =>
|
||||
assert.ok(store.get('a'));
|
||||
store.flush();
|
||||
});
|
||||
|
||||
test('a different secret cannot read the file; without a secret nothing is written', async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'pm-board-sessions-key-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const path = join(dir, 'sessions.json');
|
||||
const store = createSessionStore(path, { secret: SECRET });
|
||||
store.set('cookie', { claims: { employee_number: '100' }, expiresAt: Date.now() + 60_000, refreshToken: 'REFRESH' });
|
||||
store.flush();
|
||||
assert.equal(createSessionStore(path, { secret: 'another-runtime-secret' }).load(), 0);
|
||||
assert.equal(createSessionStore(path, { secret: SECRET }).load(), 1);
|
||||
|
||||
const other = join(dir, 'none.json');
|
||||
const memoryOnly = createSessionStore(other, { secret: '' });
|
||||
memoryOnly.set('cookie', { claims: {}, expiresAt: Date.now() + 60_000 });
|
||||
memoryOnly.flush();
|
||||
await assert.rejects(readFile(other, 'utf8'), { code: 'ENOENT' });
|
||||
});
|
||||
|
||||
test('an old unencrypted file is loaded once and rewritten encrypted', async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), 'pm-board-sessions-old-'));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const path = join(dir, 'sessions.json');
|
||||
const { createHash } = await import('node:crypto');
|
||||
const id = createHash('sha256').update('legacy-cookie').digest('hex');
|
||||
await writeFile(path, JSON.stringify({ version: 1, sessions: { [id]: { claims: { employee_number: '100' }, expiresAt: Date.now() + 60_000, refreshToken: 'OLD-PLAIN-TOKEN' } } }));
|
||||
const store = createSessionStore(path, { secret: SECRET });
|
||||
assert.equal(store.load(), 1);
|
||||
assert.equal(store.get('legacy-cookie').refreshToken, 'OLD-PLAIN-TOKEN');
|
||||
const text = await readFile(path, 'utf8');
|
||||
assert.equal(JSON.parse(text).version, 2);
|
||||
assert.ok(!text.includes('OLD-PLAIN-TOKEN'));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user