feat(security): add phase8 hardening with rate limits, audit logs, and auth-first simulator flow

This commit is contained in:
2026-01-24 18:45:00 +00:00
parent 6d6c77f77e
commit f6d66c3650
11 changed files with 355 additions and 5 deletions

View File

@@ -0,0 +1,31 @@
import type { NextFunction, Request, Response } from 'express';
type Bucket = {
count: number;
windowStart: number;
};
const buckets = new Map<string, Bucket>();
export const rateLimit = (options: { keyPrefix: string; windowMs: number; max: number }) => {
return (req: Request, res: Response, next: NextFunction): void => {
const key = `${options.keyPrefix}:${req.ip ?? 'unknown'}`;
const now = Date.now();
const current = buckets.get(key);
if (!current || now - current.windowStart > options.windowMs) {
buckets.set(key, { count: 1, windowStart: now });
next();
return;
}
if (current.count >= options.max) {
res.status(429).json({ message: 'Rate limit exceeded. Try again later.' });
return;
}
current.count += 1;
buckets.set(key, current);
next();
};
};