feat(security): implement AES-256-GCM vault, Panic Lock, Argon2id+TOTP auth, guards, and rotate-key CLI
This commit is contained in:
parent
2c60483a90
commit
3b9e084916
5 changed files with 1249 additions and 0 deletions
81
src/server/security/audit.ts
Normal file
81
src/server/security/audit.ts
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
import type Database from 'better-sqlite3';
|
||||
|
||||
export interface AuditEntryInput {
|
||||
action: string;
|
||||
actorIp: string;
|
||||
sessionIdHash?: string | null;
|
||||
target?: string | null;
|
||||
status: 'ok' | 'denied' | 'error';
|
||||
details?: Record<string, unknown> | string | null;
|
||||
}
|
||||
|
||||
export interface AuditLogRecord {
|
||||
id: number;
|
||||
ts: number;
|
||||
action: string;
|
||||
actor_ip: string;
|
||||
session_id_hash: string | null;
|
||||
target: string | null;
|
||||
status: string;
|
||||
details: string | null;
|
||||
}
|
||||
|
||||
export class AuditService {
|
||||
private readonly insertStmt;
|
||||
|
||||
constructor(private readonly db: Database.Database) {
|
||||
this.insertStmt = this.db.prepare(`
|
||||
INSERT INTO audit_log (ts, action, actor_ip, session_id_hash, target, status, details)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
`);
|
||||
}
|
||||
|
||||
log(entry: AuditEntryInput): void {
|
||||
const detailsText =
|
||||
entry.details == null
|
||||
? null
|
||||
: typeof entry.details === 'string'
|
||||
? entry.details
|
||||
: JSON.stringify(entry.details);
|
||||
|
||||
this.insertStmt.run(
|
||||
Date.now(),
|
||||
entry.action,
|
||||
entry.actorIp,
|
||||
entry.sessionIdHash ?? null,
|
||||
entry.target ?? null,
|
||||
entry.status,
|
||||
detailsText,
|
||||
);
|
||||
}
|
||||
|
||||
list(options: { limit?: number; offset?: number; action?: string } = {}): {
|
||||
total: number;
|
||||
items: AuditLogRecord[];
|
||||
} {
|
||||
const limit = Math.max(1, Math.min(200, options.limit ?? 50));
|
||||
const offset = Math.max(0, options.offset ?? 0);
|
||||
|
||||
if (options.action) {
|
||||
const countRow = this.db
|
||||
.prepare('SELECT COUNT(*) AS cnt FROM audit_log WHERE action = ?')
|
||||
.get(options.action) as { cnt: number };
|
||||
const items = this.db
|
||||
.prepare(
|
||||
'SELECT id, ts, action, actor_ip, session_id_hash, target, status, details FROM audit_log WHERE action = ? ORDER BY ts DESC, id DESC LIMIT ? OFFSET ?',
|
||||
)
|
||||
.all(options.action, limit, offset) as AuditLogRecord[];
|
||||
return { total: countRow.cnt, items };
|
||||
}
|
||||
|
||||
const countRow = this.db
|
||||
.prepare('SELECT COUNT(*) AS cnt FROM audit_log')
|
||||
.get() as { cnt: number };
|
||||
const items = this.db
|
||||
.prepare(
|
||||
'SELECT id, ts, action, actor_ip, session_id_hash, target, status, details FROM audit_log ORDER BY ts DESC, id DESC LIMIT ? OFFSET ?',
|
||||
)
|
||||
.all(limit, offset) as AuditLogRecord[];
|
||||
return { total: countRow.cnt, items };
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue