ac6c834525
- main.ts awaits DatabaseInitService.init() before app.listen(), ensuring
migrations + seed run before the HTTP server accepts traffic.
- AppModule now uses NestModule with consumer.apply() no longer needed for
CSRF (registered globally via app.use after body parsers in main.ts).
- JwtAuthGuard extended from AuthGuard('jwt') so protected endpoints
actually validate the bearer token.
- Admin controller now uses Zod-validated DTOs for body/path/query:
createUser, updateUserRole, userIdParam, listUsersQuery; with @Public
/ @Roles decorators and AdminGuard applied.
- Multer upload module + controller (POST /api/v1/uploads/category-icon
and /challenge-file) with safe-filename strategy, configured
UPLOAD_SIZE_LIMIT, admin-only via AdminGuard, served via /uploads
static handler.
- ThemeLoaderService now requires all 10 canonical theme ids at startup,
backfilling missing themes from built-ins with a warning; validates the
configured themeKey setting and falls back to 'classic' if invalid;
gracefully tolerates missing setting table during early boot.
- Test suite expanded to 76 tests / 17 suites; new specs:
admin-validation, uploads, theme-required, database-init.
80 lines
3.1 KiB
TypeScript
80 lines
3.1 KiB
TypeScript
process.env.DATABASE_PATH = ':memory:';
|
|
process.env.THEMES_DIR = './themes';
|
|
process.env.FRONTEND_DIST = './frontend/dist';
|
|
|
|
import { RegistrationRateLimitService } from '../../backend/src/common/services/registration-rate-limit.service';
|
|
import { Test } from '@nestjs/testing';
|
|
import { INestApplication } from '@nestjs/common';
|
|
import { HttpAdapterHost } from '@nestjs/core';
|
|
import cookieParser from 'cookie-parser';
|
|
import * as express from 'express';
|
|
import request from 'supertest';
|
|
import { AppModule } from '../../backend/src/app.module';
|
|
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
|
|
import { CsrfMiddleware } from '../../backend/src/common/middleware/csrf.middleware';
|
|
import { ConfigService } from '@nestjs/config';
|
|
import { initDb } from './db-helper';
|
|
|
|
describe('RegistrationRateLimitService', () => {
|
|
it('allows up to 10 per minute per IP', () => {
|
|
const s = new RegistrationRateLimitService();
|
|
const now = 1_000_000;
|
|
for (let i = 0; i < 10; i++) {
|
|
expect(s.isAllowed('1.1.1.1', now + i)).toBe(true);
|
|
s.record('1.1.1.1', now + i);
|
|
}
|
|
expect(s.isAllowed('1.1.1.1', now + 10)).toBe(false);
|
|
});
|
|
|
|
it('isolates per IP', () => {
|
|
const s = new RegistrationRateLimitService();
|
|
const now = 1_000_000;
|
|
for (let i = 0; i < 10; i++) s.record('1.1.1.1', now + i);
|
|
expect(s.isAllowed('2.2.2.2', now + 10)).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe('register-first-admin registration rate limit (integration)', () => {
|
|
let app: INestApplication;
|
|
let limiter: RegistrationRateLimitService;
|
|
|
|
beforeAll(async () => {
|
|
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
|
app = moduleRef.createNestApplication();
|
|
app.use(cookieParser());
|
|
app.use(express.json({ limit: '1mb' }));
|
|
const csrfMw = new CsrfMiddleware(app.get(ConfigService));
|
|
app.use((req: any, res: any, next: any) => csrfMw.use(req, res, next));
|
|
const httpAdapterHost = app.get(HttpAdapterHost);
|
|
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
|
|
await app.init();
|
|
await initDb(app);
|
|
limiter = app.get(RegistrationRateLimitService);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('returns 429 once the per-IP cap is reached', async () => {
|
|
// Prime the limiter for the IP that supertest will use. The default
|
|
// Express socket address is IPv6-mapped IPv4 ('::ffff:127.0.0.1') on
|
|
// most platforms; priming both forms keeps the test stable.
|
|
const now = Date.now();
|
|
for (let i = 0; i < 10; i++) {
|
|
limiter.record('127.0.0.1', now + i);
|
|
limiter.record('::ffff:127.0.0.1', now + i);
|
|
}
|
|
|
|
const server = app.getHttpServer();
|
|
const agent = request.agent(server);
|
|
await agent.get('/api/v1/auth/csrf');
|
|
const cookies: any = agent.jar.getCookies(require('cookiejar').CookieAccessInfo.All);
|
|
const csrf = cookies.find((c: any) => c.name === 'csrf');
|
|
|
|
await agent.post('/api/v1/auth/register-first-admin')
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ username: 'noone', password: 'Sup3rSecret!Pass' })
|
|
.expect(429);
|
|
});
|
|
}); |