test(security): persist panic-lock across restarts and add multi-auth/recovery E2E tests
This commit is contained in:
parent
90a8bf8553
commit
21176265aa
4 changed files with 559 additions and 13 deletions
|
|
@ -4,6 +4,7 @@ import path from 'node:path';
|
|||
import * as OTPAuth from 'otpauth';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import { openDatabase } from '../src/server/db/database.js';
|
||||
import { SecuritySettingsSchema } from '../src/shared/schemas/settings.schema.js';
|
||||
import { AuthService } from '../src/server/security/auth.js';
|
||||
import { RateLimiter, resolveClientIp } from '../src/server/security/guards.js';
|
||||
|
||||
|
|
@ -104,13 +105,19 @@ describe('AuthService, TOTP Replay, Step-Up, Safe Mode & Guards', () => {
|
|||
db.close();
|
||||
});
|
||||
|
||||
it('enforces 5-minute step-up elevation, step-up replay block, and drops elevation on Safe Mode', async () => {
|
||||
it('enforces 5-minute step-up elevation, step-up replay block, and drops elevation on Safe Mode when configured strictly', async () => {
|
||||
vi.useFakeTimers({ toFake: ['Date'] });
|
||||
const t0 = 1_700_000_010_000;
|
||||
vi.setSystemTime(t0);
|
||||
|
||||
const strictSecurity = SecuritySettingsSchema.parse({
|
||||
stepUpPolicy: '5m',
|
||||
elevateOnLogin: false,
|
||||
keepElevationOnSafeMode: false,
|
||||
});
|
||||
|
||||
const db = makeTempDb();
|
||||
const auth = new AuthService(db);
|
||||
const auth = new AuthService(db, { getSecuritySettings: () => strictSecurity });
|
||||
const enroll = await auth.beginBootstrap('admin');
|
||||
const code0 = generateTotp(enroll.secretBase32, t0);
|
||||
const boot = await auth.completeBootstrap({
|
||||
|
|
@ -120,16 +127,17 @@ describe('AuthService, TOTP Replay, Step-Up, Safe Mode & Guards', () => {
|
|||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
});
|
||||
expect(boot.session.elevatedUntil).toBeNull();
|
||||
|
||||
// Step-up tracks replay independently from login step
|
||||
const stepUp1 = auth.stepUp(boot.session.idHash, code0);
|
||||
const stepUp1 = await auth.stepUp(boot.session.idHash, code0);
|
||||
expect(stepUp1).toEqual({
|
||||
ok: true,
|
||||
elevatedUntil: t0 + 5 * 60 * 1000,
|
||||
});
|
||||
|
||||
// Replaying exact same step-up code is denied
|
||||
const stepUpReplay = auth.stepUp(boot.session.idHash, code0);
|
||||
const stepUpReplay = await auth.stepUp(boot.session.idHash, code0);
|
||||
expect(stepUpReplay).toEqual({ ok: false, reason: 'totp_replayed' });
|
||||
|
||||
// Enabling Safe Mode clears elevatedUntil immediately
|
||||
|
|
@ -140,12 +148,12 @@ describe('AuthService, TOTP Replay, Step-Up, Safe Mode & Guards', () => {
|
|||
// Step-up is blocked while Safe Mode is ON
|
||||
vi.setSystemTime(t0 + 31_000);
|
||||
const code1 = generateTotp(enroll.secretBase32, t0 + 31_000);
|
||||
const whileSafe = auth.stepUp(boot.session.idHash, code1);
|
||||
const whileSafe = await auth.stepUp(boot.session.idHash, code1);
|
||||
expect(whileSafe).toEqual({ ok: false, reason: 'safe_mode_active' });
|
||||
|
||||
// Turning Safe Mode OFF allows step-up again
|
||||
auth.setSafeMode(boot.session.idHash, false);
|
||||
const stepUp2 = auth.stepUp(boot.session.idHash, code1);
|
||||
const stepUp2 = await auth.stepUp(boot.session.idHash, code1);
|
||||
expect(stepUp2.ok).toBe(true);
|
||||
|
||||
// Elevation expires automatically after 5 minutes
|
||||
|
|
@ -156,6 +164,31 @@ describe('AuthService, TOTP Replay, Step-Up, Safe Mode & Guards', () => {
|
|||
db.close();
|
||||
});
|
||||
|
||||
it('grants default 1h elevation on bootstrap/login and preserves elevation across Safe Mode toggle', async () => {
|
||||
vi.useFakeTimers({ toFake: ['Date'] });
|
||||
const t0 = 1_700_000_010_000;
|
||||
vi.setSystemTime(t0);
|
||||
|
||||
const db = makeTempDb();
|
||||
const auth = new AuthService(db);
|
||||
const enroll = await auth.beginBootstrap('admin');
|
||||
const boot = await auth.completeBootstrap({
|
||||
username: 'admin',
|
||||
password: 'balanced-default-password',
|
||||
totpCode: generateTotp(enroll.secretBase32, t0),
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'vitest',
|
||||
});
|
||||
|
||||
expect(boot.session.elevatedUntil).toBe(t0 + 60 * 60 * 1000);
|
||||
|
||||
const safeSession = auth.setSafeMode(boot.session.idHash, true);
|
||||
expect(safeSession?.safeMode).toBe(true);
|
||||
expect(safeSession?.elevatedUntil).toBe(t0 + 60 * 60 * 1000);
|
||||
|
||||
db.close();
|
||||
});
|
||||
|
||||
it('applies exponential rate-limit backoff and honors CF-Connecting-IP only when trusted', () => {
|
||||
const limiter = new RateLimiter();
|
||||
for (let i = 0; i < 4; i++) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue