Files
FitOS/api/test/integration/trust-proxy.test.ts

45 lines
1.8 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { buildApp, rand, PASSWORD } from '../helpers.js';
// Frontendy proxyja API przez nginx/vite (same-origin), Express ufa jednemu skokowi
// proxy (app.set('trust proxy', 1)). Bez tego kubełek authLimiter (50/15min) liczylby
// sie po IP kontenera nginx — wszyscy uzytkownicy dostaliby 429 zbiorowo.
describe('trust proxy — limity per adres klienta za nginxem', () => {
it('GET /health przechodzi z naglowkiem X-Forwarded-For', async () => {
const app = buildApp();
const res = await app.get('/health').set('X-Forwarded-For', '203.0.113.7');
expect(res.status).toBe(200);
expect(res.body.status).toBe('ok');
});
it('51 logowan z roznych X-Forwarded-For nie wyczerpuje authLimiter (brak 429)', async () => {
const app = buildApp();
const emails: string[] = [];
for (let i = 1; i <= 51; i++) emails.push(`xff-diff-${i}-${rand()}@test.local`);
for (let i = 0; i < 51; i++) {
const res = await app
.post('/api/v1/auth/login')
.set('X-Forwarded-For', `203.0.113.${(i % 254) + 1}`)
.send({ email: emails[i], password: PASSWORD });
expect(res.status, `zalodzenie ${i + 1} z IP 203.0.113.${(i % 254) + 1}`).not.toBe(429);
}
}, 120_000);
it('ten sam X-Forwarded-For po 50 probach dostaje 429 (kubelek per IP, nie per proxy)', async () => {
const app = buildApp();
const emails: string[] = [];
for (let i = 0; i < 51; i++) emails.push(`xff-same-${i}-${rand()}@test.local`);
let lastStatus = 0;
for (let i = 0; i < 51; i++) {
const res = await app
.post('/api/v1/auth/login')
.set('X-Forwarded-For', '198.51.100.7')
.send({ email: emails[i], password: PASSWORD });
lastStatus = res.status;
}
expect(lastStatus).toBe(429);
}, 120_000);
});