Files
TREK/server/tests/unit/nest/rate-limit.service.test.ts
T
Maurice b3a085df3f refactor(server): make the state ports async and route every reader through the provided store
The process-state ports were meant to let a store shared between
processes replace the in-memory one, but they could not. The readers
outside the container (the broadcast functions, the backup restore's
permissions-cache flush, the pending-code sweep, hand-built consumers)
went straight to the module-level in-memory instances, so an overridden
provider was used by the gateway and the services while the broadcasts
and the restore flush kept using the old store. And every port was
synchronous, while a store in the database or on the network is async,
so plugging one in would have changed every signature anyway.

RateLimitStore, PendingCodeStore, OidcFlowStore and
PermissionsCacheStore now return promises, and RateLimitService,
OauthService, OidcService, PermissionsService and their callers await
them. The in-memory stores still decide synchronously, so who gets a
429, a code or a cached level is unchanged. RoomRegistry stays
synchronous: membership holds live sockets and broadcast() is a
synchronous call every domain makes, so several processes need a
fan-out in front of broadcast(), not a different place for the sets.

The process-wide ports get a ProcessStoreSlot. PermissionsModule,
OauthModule and RealtimeGatewayModule install the store the container
resolved and release it on shutdown, and the free functions, the sweep
and hand-built consumers read the slot, so overriding the provider
changes every reader. Tests provide fake stores and check that the
broadcasts, the restore flush and the sweep use them.

Two callers needed more than an await. The transit MCP tools counted in
a RateLimitService of their own that no provider could reach; they now
take the one RateLimitModule provides (their mcp_transit_* buckets never
overlap the REST ones). The link preview claimed its in-flight slot only
after the budget check, which now yields, so simultaneous askers would
each have been charged; it now claims the slot first, and an asker that
joined someone out of budget asks again on its own budget.

The unused putPendingCode/takePendingCode helpers are gone.
2026-10-08 18:41:01 +02:00

78 lines
3.3 KiB
TypeScript

/**
* RateLimitService — the per-IP buckets every auth-adjacent route throttles on.
* The limit behaviour is the legacy one line for line; what is worth pinning
* here is that the maps do not grow forever, because the keys come from
* proxy headers and are therefore attacker-influenced.
*/
import { describe, it, expect } from 'vitest';
import { RateLimitService } from '../../../src/nest/common/rate-limit.service';
import { InMemoryRateLimitStore } from '../../../src/nest/common/rate-limit.store';
const WINDOW = 15 * 60 * 1000;
/** The in-memory store's bucket size, which is the only place the housekeeping is visible. */
function bucketSize(service: RateLimitService, bucket: string): number {
const store = (service as unknown as { store: InMemoryRateLimitStore }).store;
return store.size(bucket);
}
describe('RateLimitService', () => {
it('allows up to max attempts in a window, then refuses', async () => {
const s = new RateLimitService();
const now = Date.now();
for (let i = 0; i < 5; i++) expect(await s.check('login', '1.2.3.4', 5, WINDOW, now)).toBe(true);
expect(await s.check('login', '1.2.3.4', 5, WINDOW, now)).toBe(false);
});
it('starts a fresh window once the old one elapsed', async () => {
const s = new RateLimitService();
const now = Date.now();
for (let i = 0; i < 5; i++) await s.check('login', '1.2.3.4', 5, WINDOW, now);
expect(await s.check('login', '1.2.3.4', 5, WINDOW, now + WINDOW)).toBe(true);
});
it('drops expired records instead of keeping one per address that ever knocked', async () => {
const s = new RateLimitService();
const now = Date.now();
for (let i = 0; i < 100; i++) await s.check('login', `10.0.0.${i}`, 5, WINDOW, now);
expect(bucketSize(s, 'login')).toBe(100);
// One request a window later sweeps the hundred stale keys and leaves its own.
await s.check('login', '10.0.0.0', 5, WINDOW, now + WINDOW);
expect(bucketSize(s, 'login')).toBe(1);
});
it('sweeps each bucket on its own clock, so a busy bucket cannot starve a quiet one', async () => {
const s = new RateLimitService();
const now = Date.now();
await s.check('login', '1.2.3.4', 5, WINDOW, now);
for (let i = 0; i < 3; i++) await s.check('mfa', `10.0.0.${i}`, 5, WINDOW, now + WINDOW * (i + 1));
expect(bucketSize(s, 'login')).toBe(1);
await s.check('login', '5.6.7.8', 5, WINDOW, now + WINDOW);
expect(bucketSize(s, 'login')).toBe(1);
});
it('keeps a record that is still inside its window when the sweep runs', async () => {
const s = new RateLimitService();
const now = Date.now();
await s.check('login', '1.2.3.4', 5, WINDOW, now);
await s.check('login', '5.6.7.8', 5, WINDOW, now + WINDOW);
// The second key was written after the sweep, the first one was expired.
expect(bucketSize(s, 'login')).toBe(1);
expect(await s.check('login', '1.2.3.4', 5, WINDOW, now + WINDOW)).toBe(true);
});
it('reset clears a single bucket, or all of them', async () => {
const s = new RateLimitService();
const now = Date.now();
await s.check('login', '1.2.3.4', 5, WINDOW, now);
await s.check('mfa', '1.2.3.4', 5, WINDOW, now);
await s.reset('login');
expect(bucketSize(s, 'login')).toBe(0);
expect(bucketSize(s, 'mfa')).toBe(1);
await s.reset();
expect(bucketSize(s, 'mfa')).toBe(0);
});
});