import { ConfigService } from '@nestjs/config'; import type { Redis } from 'ioredis'; import request from 'supertest'; import type { Env } from '../src/config/env'; import { DEFAULT_LIMIT } from '../src/rate-limit/rate-limit.module'; import { REDIS } from '../src/redis/redis.module'; import { createTestApp, TestAccounts, type TestApp } from './helpers'; // The other specs run with the limits lifted; this one is about them. The // setting is read when the app's modules load, which is before anything // below this line would run. vi.hoisted(() => { process.env.RATE_LIMIT = 'true'; }); describe('Rate limits (e2e)', () => { let app: TestApp; let accounts: TestAccounts; beforeAll(async () => { app = await createTestApp(); // Sign-ins are counted per address, and every run comes from this // machine: start from nothing, or a second run within the minute fails. const redis = app.get(REDIS); const counted = await redis.keys('rate:*'); if (counted.length > 0) await redis.del(counted); accounts = new TestAccounts(app, '@rate-limit-e2e.test'); await accounts.cleanup(); }); afterAll(async () => { await accounts.cleanup(); await app.close(); }); it('counts per person and per route, and says when to come back', async () => { const [ada, ben] = await Promise.all([ accounts.signUp('Ada'), accounts.signUp('Ben'), ]); // Creating a realm is allowed 5 times a minute. for (let count = 1; count <= 5; count++) { const made = await ada.agent .post('/realms') .send({ name: `Realm ${count}` }) .expect(201); expect(made.headers['x-ratelimit-remaining']).toBe(String(5 - count)); } const refused = await ada.agent .post('/realms') .send({ name: 'One too many' }) .expect(429); const wait = Number(refused.headers['retry-after']); expect(wait).toBeGreaterThan(0); expect(wait).toBeLessThanOrEqual(60); // Her other routes still answer, and so does the same route for Ben. const listed = await ada.agent.get('/realms').expect(200); expect(listed.body).toHaveLength(5); expect(listed.headers['x-ratelimit-limit']).toBe( String(DEFAULT_LIMIT.limit), ); await ben.agent.post('/realms').send({ name: "Ben's" }).expect(201); }); it('leaves the health checks alone', async () => { const checked = await request(app.getHttpServer()) .get('/health') .expect(200); expect(checked.headers).not.toHaveProperty('x-ratelimit-limit'); }); it('slows down guessing a password', async () => { const cal = await accounts.signUp('Cal'); const [origin] = app .get>(ConfigService) .getOrThrow('TRUSTED_ORIGINS', { infer: true }); const guess = () => request(app.getHttpServer()) .post('/api/auth/sign-in/username') .set('Origin', origin) .send({ username: cal.username, password: 'not-the-password' }); // Sign-in allows an address 10 tries a minute. Other specs' sign-ins // don't count here: their limits are off, so nothing is recorded. const answers: number[] = []; for (let count = 0; count < 12; count++) { answers.push((await guess()).status); } expect(answers.slice(0, 10)).toEqual(Array(10).fill(401)); expect(answers.slice(10)).toEqual([429, 429]); const refused = await guess(); expect(Number(refused.headers['x-retry-after'])).toBeGreaterThan(0); }); });