feat(db,schemas): add migration 004, runtime throttle profile, and v1.1 Zod schemas

This commit is contained in:
tyuXX 2026-11-27 22:15:50 +00:00
parent d7c3e11d7e
commit afc2d6358a
30 changed files with 792 additions and 118 deletions

View file

@ -3,7 +3,7 @@ import os from 'node:os';
import path from 'node:path';
import zlib from 'node:zlib';
import * as OTPAuth from 'otpauth';
import { afterEach, describe, expect, it } from 'vitest';
import { afterEach, describe, expect, it, vi } from 'vitest';
import WebSocket from 'ws';
import { createGCoordinatorServer } from '../src/server/app.js';
@ -115,7 +115,9 @@ describe('Bundle size budget & full-stack Express + WS lifecycle', () => {
expect(health.status).toBe(200);
await expect(openWs(wsUrl, {})).rejects.toThrow('WS_HTTP_401');
// 2. Bootstrap Admin + TOTP
// 2. Bootstrap Admin + TOTP (offset 65s back so step-up tests below have fresh TOTP steps)
vi.useFakeTimers({ toFake: ['Date'] });
vi.setSystemTime(Date.now() - 65_000);
const bootInfo = await api('/api/auth/bootstrap');
expect(bootInfo.status).toBe(200);
const secretBase32 = String(bootInfo.data.secretBase32);
@ -138,6 +140,7 @@ describe('Bundle size budget & full-stack Express + WS lifecycle', () => {
initialSafeMode: false,
},
});
vi.setSystemTime(Date.now() + 31_000);
expect(bootDone.status).toBe(201);
csrfToken = String(bootDone.data.csrfToken);
const recoveryCodes = bootDone.data.recoveryCodes as string[];
@ -198,17 +201,14 @@ describe('Bundle size budget & full-stack Express + WS lifecycle', () => {
(await api('/api/auth/safe-mode', { method: 'POST', body: { enabled: false } })).status,
).toBe(200);
// 6. Vault Init + Drop Elevation -> Step-Up guard + Generate Ed25519 key
// 6. Vault Init + Step-Up guard + Drop Elevation + Generate Ed25519 key
const vInit = await api('/api/vault/init', { method: 'POST', body: {} });
expect(vInit.status).toBe(201);
const generatedMasterKey = String(vInit.data.generatedKeyHex);
// Default balanced mode elevates on bootstrap/login; drop elevation to verify guard enforcement
const statusBeforeDrop = await api('/api/auth/status');
expect(typeof statusBeforeDrop.data.elevatedUntil).toBe('number');
const dropped = await api('/api/auth/drop-elevation', { method: 'POST' });
expect(dropped.status).toBe(200);
expect(dropped.data.elevatedUntil).toBeNull();
// v1.1 defaults elevateOnLogin=false; step up first, then verify drop-elevation clears it
const statusInitial = await api('/api/auth/status');
expect(statusInitial.data.elevatedUntil).toBeNull();
const noStepUp = await api('/api/vault/generate-ed25519', {
method: 'POST',
@ -223,6 +223,23 @@ describe('Bundle size budget & full-stack Express + WS lifecycle', () => {
});
expect(stepUpRes.status).toBe(200);
const statusBeforeDrop = await api('/api/auth/status');
expect(typeof statusBeforeDrop.data.elevatedUntil).toBe('number');
const dropped = await api('/api/auth/drop-elevation', { method: 'POST' });
expect(dropped.status).toBe(200);
expect(dropped.data.elevatedUntil).toBeNull();
const afterDropDenied = await api('/api/vault/generate-ed25519', {
method: 'POST',
body: { id: 'prod-key', name: 'Prod Key' },
});
expect(afterDropDenied.status).toBe(403);
vi.useRealTimers();
expect(
(await api('/api/auth/step-up', { method: 'POST', body: { code: totpGen.generate() } })).status,
).toBe(200);
const genRes = await api('/api/vault/generate-ed25519', {
method: 'POST',
body: { id: 'prod-key', name: 'Prod Key' },