LifeMate — Personal, Family & Learning Companion

feat/weekly-planner-android
chore/phase1-repository-cleanup deploy/stable-web-apk deployment-static design/mobile-accessibility-v2 design/mobile-navigation-review docs/child-education-access-decision docs/offline-data-rescue-options docs/owner-guide-and-delivery-plan docs/phase1-policy-options docs/public-review-live-2026-10-15 feat/aparat-learning-library feat/child-device-safety feat/child-identity-schema feat/flutter-mobile-navigation feat/learning-content-operations feat/local-signing-preflight feat/online-family-stage0 feat/personal-encrypted-recovery feat/private-planner-series-api feat/public-review-page feat/standalone-weekly-planner feat/weekly-planner-android final-feature-1/iran-family-school fix/auth-abuse-expiry-safety fix/auth-expiry-after-lock fix/backend-request-safety fix/lifeguide-bootstrap-naming-ci fix/lifeguide-pages-qa-base-preflight fix/lifeguide-qa-pages-env-deployment fix/local-task-persistence fix/managed-child-session-boundary fix/personal-jalali-dates fix/personal-notification-controls fix/personal-upgrade-delivery-guard fix/sms-approval-gates fix/study-duration-display infra/cloudflare-neon-migration infra/lifeguide-cloudflare-qa infra/lifeguide-github-pages-qa infra/lifeguide-supabase-qa main phase-1/foundation-completion phase-2/product-foundation-identity phase-3/planner-school-family-core phase-4/ai-learning-wellbeing phase-5/hardening-observability-release test/android-reminder-acceptance test/personal-notification-settings test/personal-recovery-acceptance test/role-entry-apk
js 46 lines 1.97 KB
Raw
1 import test from 'node:test';
2 import assert from 'node:assert/strict';
3 import express from 'express';
4 import { once } from 'node:events';
5 import { configureTrustedProxy, rateLimit } from '../src/operations.js';
6
7 async function serve(trusted, run) {
8 const app = express();
9 configureTrustedProxy(app, trusted);
10 app.use(rateLimit);
11 app.get('/v1/auth/proxy-test', (req, res) => res.json({ ip: req.ip }));
12 const server = app.listen(0, '127.0.0.1');
13 await once(server, 'listening');
14 try { await run(`http://127.0.0.1:${server.address().port}/v1/auth/proxy-test`); }
15 finally { await new Promise(resolve => server.close(resolve)); }
16 }
17
18 test('untrusted clients cannot forge forwarded addresses', async () => {
19 await serve('', async url => {
20 const response = await fetch(url, { headers: {'x-forwarded-for': '192.0.2.1'} });
21 assert.equal((await response.json()).ip, '127.0.0.1');
22 });
23 });
24
25 test('trusted single gateway separates client rate buckets and ignores extra left hops', async () => {
26 const previous = [process.env.NODE_ENV, process.env.RATE_LIMIT_AUTH_MAX];
27 process.env.NODE_ENV = 'production';
28 process.env.RATE_LIMIT_AUTH_MAX = '2';
29 try {
30 await serve('127.0.0.1/32', async url => {
31 const first = {'x-forwarded-for': '192.0.2.100, 192.0.2.2'};
32 assert.equal((await (await fetch(url, {headers: first})).json()).ip, '192.0.2.2');
33 assert.equal((await fetch(url, {headers: first})).status, 200);
34 assert.equal((await fetch(url, {headers: first})).status, 429);
35 assert.equal((await fetch(url, {headers: {'x-forwarded-for': '192.0.2.3'}})).status, 200);
36 });
37 } finally {
38 for (const [index, key] of ['NODE_ENV', 'RATE_LIMIT_AUTH_MAX'].entries()) {
39 if (previous[index] === undefined) delete process.env[key]; else process.env[key] = previous[index];
40 }
41 }
42 });
43
44 test('blank defaults distrust proxy and invalid broad wildcard fails closed', () => {
45 assert.throws(() => configureTrustedProxy(express(), '*'), /Invalid trusted proxy/);
46 });