Files
HIPCTF2/tests/backend/registration-rate-limit.spec.ts
T
OpenVelo Agent af3c24275d Scaffold HIPCTF platform (NestJS + Angular)
- Backend: NestJS 10 + TypeORM (better-sqlite3) feature-modular layout.
  - Entities: user, setting, category, challenge, challenge_file, solve,
    refresh_token, blog_post. Auto-run migrations + idempotent 6-system-
    category seed on startup.
  - Argon2id password hashing with policy check; JWT access + rotating
    refresh tokens (HttpOnly cookie); CSRF middleware (SameSite + custom
    X-CSRF-Token header); global JWT auth guard with @Public() opt-out;
    per-IP login backoff + per-IP registration rate limit.
  - Endpoints: auth (login/refresh/logout/csrf), users (first-admin
    registration), system (bootstrap/event status/SSE), admin (guarded
    user CRUD with last-admin invariant), frontend module (uploads +
    SPA fallback).
  - Security: helmet+CSP+HSTS-gated-by-TLS, CORS allowlist, structured
    global exception filter, Zod request validation pipes, OpenAPI 3.1
    served at /api/docs and /api/docs-json, 10 canonical themes under
    backend/themes/.
- Frontend: Angular 17 standalone components, lazy-loaded feature routes,
  signals, functional HttpInterceptorFn (csrf + auth), functional
  CanActivateFn auth guard, HttpOnly-cookie-based auth service.
- Tests: Jest + supertest, 46 tests across 13 suites covering
  migrations, env schema, theme loader, event status, login backoff,
  registration rate limit, ApiError shape, bootstrap integration,
  auth refresh rotation, admin guard, last-admin invariant, SSE flat
  payloads. Single-command runner: `npm test`.
2026-07-21 13:25:49 +00:00

70 lines
2.6 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 request from 'supertest';
import { AppModule } from '../../backend/src/app.module';
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
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();
const httpAdapterHost = app.get(HttpAdapterHost);
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
await app.init();
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.post('/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);
});
});