Files
BetterDesk/web-nodejs/tests/databaseSessionStore.test.js
T
da3dsoul d2c75b9794 feat(auth): make panel sessions per account configurable
Add SESSION_MAX_PER_USER to control how many concurrent panel sessions an
account may hold. A new login ends only the least recently active sessions
beyond the limit.

- 1 (default): unchanged single-session behaviour
- N: keep the N most recently active sessions
- 0: unlimited

The Docker compose files now pass SESSION_IDLE_TIMEOUT, SESSION_MAX_AGE,
SESSION_IDLE_WARN_MS and SESSION_MAX_PER_USER through to the container;
previously the session policy could not be changed in Docker deployments.
The security-audit hardening checklist reports the configured limit.
2026-11-07 20:48:18 -05:00

211 lines
7.7 KiB
JavaScript

'use strict';
const fs = require('fs');
const os = require('os');
const path = require('path');
const { DatabaseSessionStore } = require('../services/databaseSessionStore');
const { END_REASONS } = require('../lib/sessionPolicy');
function invoke(method, ...args) {
return new Promise((resolve, reject) => {
method(...args, (err, value) => (err ? reject(err) : resolve(value)));
});
}
describe('DatabaseSessionStore', () => {
let tempDir;
let store;
let previousDbType;
let previousDatabaseUrl;
beforeEach(async () => {
previousDbType = process.env.DB_TYPE;
previousDatabaseUrl = process.env.DATABASE_URL;
process.env.DB_TYPE = 'sqlite';
delete process.env.DATABASE_URL;
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'betterdesk-session-store-'));
store = new DatabaseSessionStore({
config: { dbPath: path.join(tempDir, 'db_v2.sqlite3') },
ttlMs: 24 * 60 * 60 * 1000,
idleTimeoutMs: 60_000,
maxAgeMs: 24 * 60 * 60 * 1000
});
await store.ready;
});
afterEach(async () => {
if (store) await store.close();
fs.rmSync(tempDir, { recursive: true, force: true });
store = null;
if (previousDbType === undefined) delete process.env.DB_TYPE;
else process.env.DB_TYPE = previousDbType;
if (previousDatabaseUrl === undefined) delete process.env.DATABASE_URL;
else process.env.DATABASE_URL = previousDatabaseUrl;
});
it('persists, expires and destroys console sessions in SQLite', async () => {
const sid = 'session-id';
const session = {
user: { id: 7, username: 'alice', role: 'operator' },
userId: 7,
createdAt: Date.now(),
lastActivityAt: Date.now(),
cookie: { expires: new Date(Date.now() + 60_000).toISOString() }
};
await invoke(store.set.bind(store), sid, session);
expect(await invoke(store.get.bind(store), sid)).toEqual(session);
await invoke(store.touch.bind(store), sid, {
...session,
lastActivityAt: Date.now(),
cookie: { expires: new Date(Date.now() + 120_000).toISOString() }
});
expect(await invoke(store.get.bind(store), sid)).toEqual(session);
await invoke(store.destroy.bind(store), sid);
expect(await invoke(store.get.bind(store), sid)).toBeNull();
});
it('enforces one active session per account and keeps history', async () => {
const now = Date.now();
const base = {
userId: 42,
user: { id: 42, username: 'bob', role: 'admin' },
createdAt: now,
lastActivityAt: now,
loginIp: '203.0.113.10',
loginUserAgent: 'TestAgent/1.0',
cookie: { expires: new Date(now + 30_000).toISOString() }
};
await invoke(store.set.bind(store), 'sid-a', base);
await store.activateExclusiveSession('sid-a', {
userId: 42,
username: 'bob',
ip: '203.0.113.10',
userAgent: 'TestAgent/1.0',
createdAt: now,
lastActivityAt: now
});
await invoke(store.set.bind(store), 'sid-b', {
...base,
loginIp: '198.51.100.20',
loginUserAgent: 'OtherAgent/2.0'
});
const result = await store.activateExclusiveSession('sid-b', {
userId: 42,
username: 'bob',
ip: '198.51.100.20',
userAgent: 'OtherAgent/2.0',
createdAt: now,
lastActivityAt: now
});
expect(result.replaced).toHaveLength(1);
expect(result.replaced[0].sid).toBe('sid-a');
expect(await invoke(store.get.bind(store), 'sid-a')).toBeNull();
expect(await invoke(store.get.bind(store), 'sid-b')).toBeTruthy();
const history = await store.listUserSessions(42, { includeEnded: true });
expect(history.length).toBeGreaterThanOrEqual(2);
const ended = history.find((r) => r.sid === 'sid-a');
expect(ended.end_reason).toBe(END_REASONS.REPLACED);
expect(Number(ended.is_active)).toBe(0);
});
async function loginAs(target, sid, userId, lastActivityAt) {
const session = {
userId,
user: { id: userId, username: `user${userId}`, role: 'admin' },
createdAt: lastActivityAt,
lastActivityAt,
cookie: { expires: new Date(Date.now() + 30_000).toISOString() }
};
await invoke(target.set.bind(target), sid, session);
return target.activateExclusiveSession(sid, {
userId,
username: `user${userId}`,
createdAt: lastActivityAt,
lastActivityAt
});
}
async function storeWithLimit(maxSessionsPerUser) {
await store.close();
store = new DatabaseSessionStore({
config: { dbPath: path.join(tempDir, 'db_v2.sqlite3') },
ttlMs: 24 * 60 * 60 * 1000,
idleTimeoutMs: 60_000,
maxAgeMs: 24 * 60 * 60 * 1000,
maxSessionsPerUser
});
await store.ready;
return store;
}
it('allows unlimited concurrent sessions when maxSessionsPerUser is 0', async () => {
const s = await storeWithLimit(0);
const now = Date.now();
for (const sid of ['u-a', 'u-b', 'u-c']) {
const result = await loginAs(s, sid, 50, now);
expect(result.replaced).toHaveLength(0);
}
for (const sid of ['u-a', 'u-b', 'u-c']) {
expect(await invoke(s.get.bind(s), sid)).toBeTruthy();
}
});
it('keeps the N most recently active sessions and replaces the oldest', async () => {
const s = await storeWithLimit(2);
const now = Date.now();
await loginAs(s, 'n-old', 51, now - 3000);
await loginAs(s, 'n-mid', 51, now - 2000);
const result = await loginAs(s, 'n-new', 51, now - 1000);
expect(result.replaced.map((r) => r.sid)).toEqual(['n-old']);
expect(await invoke(s.get.bind(s), 'n-old')).toBeNull();
expect(await invoke(s.get.bind(s), 'n-mid')).toBeTruthy();
expect(await invoke(s.get.bind(s), 'n-new')).toBeTruthy();
expect((await s.getSessionRecord('n-old')).end_reason).toBe(END_REASONS.REPLACED);
});
it('does not let one account\'s limit end another account\'s sessions', async () => {
const s = await storeWithLimit(1);
const now = Date.now();
await loginAs(s, 'x-1', 60, now);
const result = await loginAs(s, 'y-1', 61, now);
expect(result.replaced).toHaveLength(0);
expect(await invoke(s.get.bind(s), 'x-1')).toBeTruthy();
});
it('rejects idle sessions on get()', async () => {
const sid = 'idle-sid';
const created = Date.now() - 10 * 60 * 1000;
const session = {
userId: 9,
user: { id: 9, username: 'idle', role: 'viewer' },
createdAt: created,
lastActivityAt: created,
cookie: { expires: new Date(Date.now() + 60_000).toISOString() }
};
await invoke(store.set.bind(store), sid, session);
await store.activateExclusiveSession(sid, {
userId: 9,
username: 'idle',
createdAt: created,
lastActivityAt: created
});
// Force last activity beyond idleTimeoutMs (60s floor)
store.sqlite.prepare(
'UPDATE panel_sessions SET last_activity_at = ?, created_at = ? WHERE sid = ?'
).run(Date.now() - 5 * 60 * 1000, created, sid);
expect(await invoke(store.get.bind(store), sid)).toBeNull();
const row = await store.getSessionRecord(sid);
expect(row.end_reason).toBe(END_REASONS.IDLE);
});
});