fix: harden auth against brute force, add auth audit logging, fix cookie/TLS gaps
Security review findings (HIGH-1/2/3, MEDIUM): - Rate limiting via @nestjs/throttler: global 100 req/min default, stricter 10 req/min on login and refresh. Also closes the MEDIUM finding (public batch-lookup enumeration) since it now sits behind the global limit too. Trust the K8s ingress's single proxy hop so throttling keys off the real client IP, not the ingress pod's IP. - Auth events (login success/failure, logout, token refresh) are now audit-logged via new AuditAction values, closing the gap where the project's own audit-log principle never actually covered the Auth module. - Refresh-token cookie now defaults to Secure=true (opt out only via COOKIE_SECURE=false for local HTTP dev), instead of keying off NODE_ENV which was never set in the K8s deploy templates. Added a TLS block + cert-manager annotation to both the backend and frontend ingress templates, which previously had no HTTPS termination configured at all.
This commit is contained in:
1 parent
1a7331b6b2
commit
f1298eff08
16 files changed
+224
-15
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { INestApplication } from '@nestjs/common';
|
||||
import * as bcrypt from 'bcryptjs';
|
||||
import request from 'supertest';
|
||||
import { App } from 'supertest/types';
|
||||
import { PrismaService } from '../src/prisma/prisma.service';
|
||||
import { createTestApp } from './utils/create-test-app';
|
||||
|
||||
const LOGIN_THROTTLE_LIMIT = 10;
|
||||
|
||||
describe('Auth login throttling (e2e)', () => {
|
||||
let app: INestApplication<App>;
|
||||
let prisma: PrismaService;
|
||||
const email = `auth-throttle-e2e-${Date.now()}@example.com`;
|
||||
const password = 'SuperSecret123!';
|
||||
let userId: string;
|
||||
|
||||
beforeAll(async () => {
|
||||
app = await createTestApp();
|
||||
prisma = app.get(PrismaService);
|
||||
|
||||
const passwordHash = await bcrypt.hash(password, 12);
|
||||
const user = await prisma.user.create({
|
||||
data: { email, passwordHash, name: 'Throttle E2E User', role: 'STAFF' },
|
||||
});
|
||||
userId = user.id;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await prisma.refreshToken.deleteMany({ where: { userId } });
|
||||
await prisma.auditLog.deleteMany({ where: { userId } });
|
||||
await prisma.user.delete({ where: { id: userId } });
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('rejects login attempts beyond the per-minute limit with 429', async () => {
|
||||
for (let attempt = 0; attempt < LOGIN_THROTTLE_LIMIT; attempt += 1) {
|
||||
await request(app.getHttpServer())
|
||||
.post('/api/v1/auth/login')
|
||||
.send({ email, password: 'wrong-password' })
|
||||
.expect(401);
|
||||
}
|
||||
|
||||
await request(app.getHttpServer())
|
||||
.post('/api/v1/auth/login')
|
||||
.send({ email, password: 'wrong-password' })
|
||||
.expect(429);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user