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.
This commit is contained in:
Maurice
2026-10-08 16:51:23 +02:00
parent fa0e6a3eae
commit b3a085df3f
100 changed files with 956 additions and 632 deletions
+2 -2
View File
@@ -201,7 +201,7 @@ describe('Auth e2e (real auth guard + real service + real cookie service + temp
}, 10000);
it('PUT /me/password preserves the remember choice on the re-issued cookie (#1927)', async () => {
resetRateLimits(app); // earlier login cases share the same per-ip 'login' bucket
await resetRateLimits(app); // earlier login cases share the same per-ip 'login' bucket
const seeded = createUser(db as never, { username: 'pw-remember', email: 'pw-remember@example.test' });
const login = await request(server)
.post('/api/auth/login')
@@ -378,7 +378,7 @@ describe('Auth e2e (real auth guard + real service + real cookie service + temp
});
it('login/options gets the same gate on the same derivation chain', async () => {
resetRateLimits(app); // shares the per-ip 'login' bucket with the login cases above
await resetRateLimits(app); // shares the per-ip 'login' bucket with the login cases above
const res = await request(server)
.post('/api/auth/passkey/login/options')
.set('Origin', 'https://trip.example.org')
+1 -1
View File
@@ -233,7 +233,7 @@ describe('Collab e2e (real auth guard + temp SQLite)', () => {
expect(last).toBe(429);
// The counters live on the container singleton, so a spent budget would
// follow this user into every test declared after it.
app.get(RateLimitService).reset('collab_link_preview');
await app.get(RateLimitService).reset('collab_link_preview');
});
it('400 on link-preview without a url', async () => {
+5 -5
View File
@@ -74,8 +74,8 @@ describe('OIDC e2e (real cookie service)', () => {
const oidc = app.get(OidcService);
vi.spyOn(oidc, 'getOidcConfig').mockResolvedValue({ issuer: 'https://idp', clientId: 'c', clientSecret: 's', displayName: 'SSO', discoveryUrl: null });
vi.spyOn(oidc, 'discover').mockResolvedValue({ authorization_endpoint: 'https://idp/auth', userinfo_endpoint: 'https://idp/ui', issuer: 'https://idp' } as never);
vi.spyOn(oidc, 'createState').mockReturnValue({ state: 'st', codeChallenge: 'cc' });
consumeAuthCode = vi.spyOn(oidc, 'consumeAuthCode').mockReturnValue({ token: 'jwt.value' });
vi.spyOn(oidc, 'createState').mockResolvedValue({ state: 'st', codeChallenge: 'cc' });
consumeAuthCode = vi.spyOn(oidc, 'consumeAuthCode').mockResolvedValue({ token: 'jwt.value' });
});
beforeEach(() => { toggles.oidc_login = true; });
@@ -104,7 +104,7 @@ describe('OIDC e2e (real cookie service)', () => {
});
it('GET /exchange sets the httpOnly trek_session cookie + returns the token', async () => {
consumeAuthCode.mockReturnValue({ token: 'jwt.value' });
consumeAuthCode.mockResolvedValue({ token: 'jwt.value' });
const res = await request(server).get('/api/auth/oidc/exchange').query({ code: 'good' });
expect(res.status).toBe(200);
expect(res.body).toEqual({ token: 'jwt.value' });
@@ -113,7 +113,7 @@ describe('OIDC e2e (real cookie service)', () => {
});
it('GET /exchange with a remembered code sets a persistent Max-Age cookie (#1927)', async () => {
consumeAuthCode.mockReturnValue({ token: 'jwt.value', remember: true });
consumeAuthCode.mockResolvedValue({ token: 'jwt.value', remember: true });
const res = await request(server).get('/api/auth/oidc/exchange').query({ code: 'good' });
expect(res.status).toBe(200);
const cookie = (res.headers['set-cookie'] as unknown as string[]).find((c) => c.startsWith('trek_session='))!;
@@ -121,7 +121,7 @@ describe('OIDC e2e (real cookie service)', () => {
});
it('GET /exchange with remember=false sets a browser-session cookie (no Max-Age) (#1927)', async () => {
consumeAuthCode.mockReturnValue({ token: 'jwt.value', remember: false });
consumeAuthCode.mockResolvedValue({ token: 'jwt.value', remember: false });
const res = await request(server).get('/api/auth/oidc/exchange').query({ code: 'good' });
expect(res.status).toBe(200);
const cookie = (res.headers['set-cookie'] as unknown as string[]).find((c) => c.startsWith('trek_session='))!;
@@ -77,10 +77,10 @@ describe('Place enrichment e2e (real auth guard + real validation pipe)', () =>
await app.close();
});
beforeEach(() => {
beforeEach(async () => {
db.prepare('DELETE FROM app_settings').run();
db.prepare('DELETE FROM place_details_cache').run();
app.get(RateLimitService).reset('place_enrichment');
await app.get(RateLimitService).reset('place_enrichment');
// spyOn hands back the same spy on a second call, so the call counts carry
// over between tests unless they are cleared explicitly.
vi.spyOn(maps, 'fetchCommonsCandidates').mockClear().mockResolvedValue([]);
+2 -2
View File
@@ -182,9 +182,9 @@ export function resetTestDb(db: Database.Database): void {
* app.get(RateLimitService) is ambiguous and may hand back the wrong instance — we
* resolve the auth controller and clear the limiter it actually uses.
*/
export function resetRateLimits(app: INestApplication): void {
export function resetRateLimits(app: INestApplication): Promise<void> {
const ctrl = app.get(AuthPublicController, { strict: false }) as unknown as { rl: RateLimitService };
ctrl.rl.reset();
return ctrl.rl.reset();
}
/** Fixed config mock — use with vi.mock('../../src/config', () => TEST_CONFIG) */
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -64,9 +64,9 @@ beforeAll(async () => {
await getRegionGeo(['ZZ']);
}, 60_000);
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -42,10 +42,10 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
// Reset rate limiter state between tests so they don't interfere
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -76,9 +76,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+8 -8
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
// GET /budget/settlement reaches ExchangeRatesService.getRates unconditionally
// (budget.service.ts settlement()), and that is a real fetch to
// api.frankfurter.dev with a 10 s abort. Without this stub the suite talks to
@@ -404,7 +404,7 @@ describe('Reorder budget items', () => {
// Restrict budget_edit to trip_owner only
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_budget_edit', 'trip_owner')").run();
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.put(`/api/trips/${trip.id}/budget/reorder/items`)
@@ -414,7 +414,7 @@ describe('Reorder budget items', () => {
// Restore default
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
});
it('BUDGET-013 — owner can reorder budget items — returns 200', async () => {
@@ -508,7 +508,7 @@ describe('Budget edit permission enforcement', () => {
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_budget_edit', 'trip_owner')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.post(`/api/trips/${trip.id}/budget`)
@@ -517,7 +517,7 @@ describe('Budget edit permission enforcement', () => {
expect(res.status).toBe(403);
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
});
it('BUDGET-018 — member cannot reorder categories when budget_edit is restricted to trip_owner', async () => {
@@ -529,7 +529,7 @@ describe('Budget edit permission enforcement', () => {
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_budget_edit', 'trip_owner')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.put(`/api/trips/${trip.id}/budget/reorder/categories`)
@@ -538,6 +538,6 @@ describe('Budget edit permission enforcement', () => {
expect(res.status).toBe(403);
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
});
});
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -55,9 +55,9 @@ beforeAll(async () => {
if (!fs.existsSync(uploadsDir)) fs.mkdirSync(uploadsDir, { recursive: true });
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -53,9 +53,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
// Enable the collections addon (the controller sits behind AddonGuard).
testDb.prepare(
"INSERT OR REPLACE INTO addons (id, name, description, type, icon, enabled, sort_order) VALUES ('collections', 'Collections', 'Saved places', 'global', 'Bookmark', 1, 40)"
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+3 -3
View File
@@ -37,7 +37,7 @@ beforeAll(async () => {
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => { resetTestDb(testDb); resetRateLimits(nestApp); });
beforeEach(async () => { resetTestDb(testDb); await resetRateLimits(nestApp); });
afterAll(async () => {
await nestApp.close();
testDb.close();
@@ -378,7 +378,7 @@ describe('Delete day', () => {
createDay(testDb, trip.id);
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_day_edit', 'trip_owner')").run();
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
invalidatePermissionsCache();
await invalidatePermissionsCache();
try {
const res = await request(app)
.delete(`/api/trips/${trip.id}/days/${day.id}`)
@@ -387,7 +387,7 @@ describe('Delete day', () => {
expect(testDb.prepare('SELECT id FROM days WHERE id = ?').get(day.id)).toBeDefined();
} finally {
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_day_edit'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
}
});
});
+2 -2
View File
@@ -51,9 +51,9 @@ beforeAll(async () => {
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
// Re-seed allowed_file_types after reset
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
});
+2 -2
View File
@@ -63,9 +63,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
// Providers only count as enabled under an enabled journey addon (migration 84 seeds it off).
setAddonEnabled(testDb, 'journey', true);
// The migrated snapshot seeds photo_providers.immich.enabled = 0 (an admin must
@@ -54,10 +54,10 @@ beforeAll(async () => {
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
invalidatePermissionsCache();
await resetRateLimits(nestApp);
await invalidatePermissionsCache();
// Enable the journey addon.
testDb.prepare(
"INSERT OR REPLACE INTO addons (id, name, description, type, icon, enabled, sort_order) VALUES ('journey', 'Journey', 'Travel journal', 'global', 'Compass', 1, 35)"
+3 -3
View File
@@ -51,10 +51,10 @@ beforeAll(async () => {
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
invalidatePermissionsCache();
await resetRateLimits(nestApp);
await invalidatePermissionsCache();
// Enable the journey addon
testDb.prepare(
"INSERT OR REPLACE INTO addons (id, name, description, type, icon, enabled, sort_order) VALUES ('journey', 'Journey', 'Travel journal', 'global', 'Compass', 1, 35)"
@@ -43,9 +43,9 @@ describe('incoming_leg_transport_mode read-path parity', () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb2);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -64,9 +64,9 @@ beforeAll(async () => {
};
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -79,9 +79,9 @@ beforeAll(async () => {
oauthSvc = new OauthService(t.repo(OauthClients), t.repo(OauthTokens), t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)));
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
@@ -214,9 +214,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
// Providers only count as enabled under an enabled journey addon (migration 84 seeds it off).
setAddonEnabled(testDb, 'journey', true);
// The migrated snapshot seeds photo_providers.immich.enabled = 0 (an admin must
@@ -197,9 +197,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
@@ -52,9 +52,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
// Providers only count as enabled under an enabled journey addon (migration 84 seeds it off).
setAddonEnabled(testDb, 'journey', true);
// The migrated snapshot seeds photo_providers.immich.enabled = 0 (an admin must
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
@@ -52,9 +52,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
@@ -89,9 +89,9 @@ beforeAll(async () => {
containerSideOauth = new OauthService(t.repo(OauthClients), t.repo(OauthTokens), t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)));
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
setMcpEnabled(true);
});
@@ -273,7 +273,7 @@ describe('GET /oauth/authorize — SDK authorizationHandler over trekOAuthProvid
// 2. the consent controller writes the code through the container
// singleton; the module-scoped pending-code map is what lets the
// SDK-side exchange see it.
const code = containerSideOauth.createAuthCode({
const code = await containerSideOauth.createAuthCode({
clientId,
userId: user.id,
redirectUri: 'https://client.example.com/cb',
+16 -16
View File
@@ -87,9 +87,9 @@ beforeAll(async () => {
containerSideOauth = new OauthService(t.repo(OauthClients), t.repo(OauthTokens), t.repo(OauthConsents), await createTestAddonsService(testDb), new AuditService(t.repo(AuditLog), t.repo(Users)));
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
setMcpEnabled(true);
});
@@ -324,7 +324,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const { verifier, challenge } = makePkce();
// Create code for client1
const code = createAuthCode({
const code = await createAuthCode({
clientId: r1.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app1.example.com/cb',
@@ -354,7 +354,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -383,7 +383,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -412,7 +412,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -441,7 +441,7 @@ describe('POST /oauth/token — authorization_code grant', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -504,7 +504,7 @@ describe('POST /oauth/token — refresh_token grant', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -596,7 +596,7 @@ describe('POST /oauth/revoke', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1093,7 +1093,7 @@ describe('Sessions — /api/oauth/sessions', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1211,7 +1211,7 @@ describe('H1 — PKCE format validation', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1269,7 +1269,7 @@ describe('H5 — All invalid_grant cases return identical response body', () =>
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1290,7 +1290,7 @@ describe('H5 — All invalid_grant cases return identical response body', () =>
});
// Redirect URI mismatch (need fresh code since code is single-use)
const code2 = createAuthCode({
const code2 = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1427,7 +1427,7 @@ describe('C3 — Refresh token replay detection', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1475,7 +1475,7 @@ describe('C3 — Refresh token replay detection', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -1515,7 +1515,7 @@ describe('C3 — Refresh token replay detection', () => {
const r = await createOAuthClient(user.id, 'App', ['https://app.example.com/cb'], ['trips:read']);
const { verifier, challenge } = makePkce();
const code = createAuthCode({
const code = await createAuthCode({
clientId: r.client!.client_id as string,
userId: user.id,
redirectUri: 'https://app.example.com/cb',
@@ -149,9 +149,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
seen.length = 0;
answers.clear();
process.env.OIDC_CLIENT_ID = CLIENT_ID;
+13 -13
View File
@@ -80,9 +80,9 @@ beforeAll(async () => {
mockVerifyIdToken = vi.spyOn(oidcSvc, 'verifyIdToken').mockImplementation(async () => undefined as never);
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
vi.clearAllMocks();
// Set OIDC environment variables for each test
@@ -163,7 +163,7 @@ describe('GET /api/auth/oidc/callback', () => {
});
// Create a valid state token
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=authcode123&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -177,7 +177,7 @@ describe('GET /api/auth/oidc/callback', () => {
mockExchangeCode.mockResolvedValueOnce({ access_token: 'test-access-token', id_token: 'fake.id.token', _ok: true, _status: 200 });
mockVerifyIdToken.mockResolvedValueOnce({ ok: true, claims: { sub: 'sub-audited-1' } });
mockGetUserInfo.mockResolvedValueOnce({ sub: 'sub-audited-1', email: 'audited@example.com', name: 'Audited', email_verified: true });
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=authcode123&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -202,7 +202,7 @@ describe('GET /api/auth/oidc/callback', () => {
name: 'New User',
});
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=code999&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -245,7 +245,7 @@ describe('GET /api/auth/oidc/callback', () => {
mockDiscover.mockResolvedValueOnce(MOCK_DISCOVERY_DOC);
mockExchangeCode.mockResolvedValueOnce({ _ok: false, _status: 400 });
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=badcode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -257,7 +257,7 @@ describe('GET /api/auth/oidc/callback', () => {
mockDiscover.mockResolvedValueOnce(MOCK_DISCOVERY_DOC);
mockExchangeCode.mockResolvedValueOnce({ access_token: 'tok', _ok: true, _status: 200 }); // no id_token
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -270,7 +270,7 @@ describe('GET /api/auth/oidc/callback', () => {
mockExchangeCode.mockResolvedValueOnce({ access_token: 'tok', id_token: 'bad.id.token', _ok: true, _status: 200 });
mockVerifyIdToken.mockResolvedValueOnce({ ok: false, error: 'signature_or_claim_mismatch: invalid signature' });
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -288,7 +288,7 @@ describe('GET /api/auth/oidc/callback', () => {
name: 'Alice',
});
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -311,7 +311,7 @@ describe('GET /api/auth/oidc/callback', () => {
name: 'Blocked',
});
const { state } = oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const { state } = await oidcSvc.createState('http://localhost:3001/api/auth/oidc/callback');
const res = await request(app).get(`/api/auth/oidc/callback?code=anycode&state=${state}`).set('Cookie', `trek_oidc_state=${state}`);
@@ -325,7 +325,7 @@ describe('GET /api/auth/oidc/callback', () => {
describe('GET /api/auth/oidc/exchange', () => {
it('OIDC-011: valid auth code returns JWT and sets cookie', async () => {
const fakeToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.test.sig';
const { code, binding } = oidcSvc.createAuthCode(fakeToken);
const { code, binding } = await oidcSvc.createAuthCode(fakeToken);
const res = await request(app)
.get(`/api/auth/oidc/exchange?code=${code}`)
@@ -353,7 +353,7 @@ describe('GET /api/auth/oidc/exchange', () => {
});
it('OIDC-013b: a real code without its binding cookie returns 400 and buys nothing', async () => {
const { code } = oidcSvc.createAuthCode('valid.but.unbound');
const { code } = await oidcSvc.createAuthCode('valid.but.unbound');
const res = await request(app).get(`/api/auth/oidc/exchange?code=${code}`);
@@ -365,7 +365,7 @@ describe('GET /api/auth/oidc/exchange', () => {
it('OIDC-014: auth code is single-use (second use returns 400)', async () => {
const fakeToken = 'test.token.here';
const { code, binding } = oidcSvc.createAuthCode(fakeToken);
const { code, binding } = await oidcSvc.createAuthCode(fakeToken);
const cookie = `trek_oidc_exchange=${binding}`;
// First use: success
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -52,9 +52,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+4 -4
View File
@@ -56,10 +56,10 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
invalidatePermissionsCache();
await resetRateLimits(nestApp);
await invalidatePermissionsCache();
// Re-attached per test: one describe below calls vi.restoreAllMocks() in its
// afterEach, which would otherwise strip these for every later test.
importGoogleList = vi.spyOn(nestApp.get(PlacesService), 'importGoogleList');
@@ -984,7 +984,7 @@ describe('Delete place — permission edge cases', () => {
// Restrict place edits to trip owner only
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_place_edit', 'trip_owner')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.delete(`/api/trips/${trip.id}/places/${place.id}`)
+2 -2
View File
@@ -39,9 +39,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+2 -2
View File
@@ -47,9 +47,9 @@ beforeAll(async () => {
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('allowed_file_types', '*')").run();
});
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+4 -4
View File
@@ -69,9 +69,9 @@ beforeAll(async () => {
placePhotoCache = new PlacePhotoCacheService(testStorage, t.repo(GooglePlacePhotoMeta), t.repo(Places), t.repo(CollectionPlaces));
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
@@ -309,7 +309,7 @@ describe('Shared trip access', () => {
const { invalidatePermissionsCache } = await import('../../src/nest/permissions/permissions-cache');
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_share_manage', 'trip_member')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
try {
const res = await request(app).get(`/api/trips/${trip.id}/share-link`).set('Cookie', authCookie(member.id));
expect(res.status).toBe(200);
@@ -317,7 +317,7 @@ describe('Shared trip access', () => {
} finally {
// Module-scoped cache: leaving it set would decide the next file's tests.
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_share_manage'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
}
});
});
+2 -2
View File
@@ -36,9 +36,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+3 -3
View File
@@ -37,10 +37,10 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
invalidatePermissionsCache();
await resetRateLimits(nestApp);
await invalidatePermissionsCache();
});
afterAll(async () => {
@@ -55,9 +55,9 @@ beforeAll(async () => {
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
closeMcpSessions();
+8 -8
View File
@@ -39,10 +39,10 @@ beforeAll(async () => {
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
invalidatePermissionsCache();
await resetRateLimits(nestApp);
await invalidatePermissionsCache();
});
afterAll(async () => {
await nestApp.close();
@@ -137,7 +137,7 @@ describe('Create trip', () => {
// Restrict trip creation to admins only
testDb.prepare("INSERT INTO app_settings (key, value) VALUES ('perm_trip_create', 'admin')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.post('/api/trips')
@@ -152,7 +152,7 @@ describe('Create trip', () => {
const { user: admin } = createAdmin(testDb);
testDb.prepare("INSERT INTO app_settings (key, value) VALUES ('perm_trip_create', 'admin')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.post('/api/trips')
@@ -375,7 +375,7 @@ describe('Update trip', () => {
// Restrict archiving to trip_owner only (this is actually the default, but set explicitly)
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_trip_archive', 'trip_owner')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.put(`/api/trips/${trip.id}`)
@@ -394,7 +394,7 @@ describe('Update trip', () => {
// Default trip_edit is trip_owner — members should be blocked
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_trip_edit', 'trip_owner')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.put(`/api/trips/${trip.id}`)
@@ -896,7 +896,7 @@ describe('Trip members', () => {
// Restrict member management to trip_owner (default)
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_member_manage', 'trip_owner')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
const res = await request(app)
.post(`/api/trips/${trip.id}/members`)
+2 -2
View File
@@ -53,9 +53,9 @@ beforeAll(async () => {
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
afterAll(async () => {
+3 -3
View File
@@ -1138,13 +1138,13 @@ describe('Tool: freeze_budget_rates', () => {
// budget_edit lowered to the owner, the way the admin permission panel does it.
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_budget_edit', 'trip_owner');
invalidatePermissionsCache();
await invalidatePermissionsCache();
await withHarness(other.id, async (h) => {
const result = await h.client.callTool({ name: 'freeze_budget_rates', arguments: { tripId: trip.id } });
expect(result.isError).toBe(true);
});
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
process.env.DEMO_MODE = 'true';
const { user: demo } = createUser(testDb, { email: 'demo@nomad.app' });
@@ -1160,7 +1160,7 @@ describe('Tool: freeze_budget_rates', () => {
} finally {
spy.mockRestore();
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_budget_edit'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
}
});
@@ -235,7 +235,7 @@ describe('Tool: delete_day', () => {
const day = createDay(testDb, trip.id);
createDay(testDb, trip.id);
testDb.prepare("INSERT OR REPLACE INTO app_settings (key, value) VALUES ('perm_day_edit', 'trip_owner')").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
try {
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({ name: 'delete_day', arguments: { tripId: trip.id, dayId: day.id } });
@@ -244,7 +244,7 @@ describe('Tool: delete_day', () => {
});
} finally {
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_day_edit'").run();
invalidatePermissionsCache();
await invalidatePermissionsCache();
}
});
@@ -62,7 +62,7 @@ interface FeedResult { feed_url: string | null }
interface InviteLink { token: string; expires_at: string | null; created_at: string; url: string }
interface InviteResult { invite_link: InviteLink | null }
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
// trip_invite_tokens is not in the shared helper's RESET_TABLES, and trip ids
// restart after a DELETE-based reset, so a leftover row would attach itself
@@ -71,7 +71,7 @@ beforeEach(() => {
// The permission levels are cached in module state that resetTestDb knows
// nothing about, so the one test that lowers share_manage would otherwise
// keep the lowered level alive for every test after it.
invalidatePermissionsCache();
await invalidatePermissionsCache();
delete process.env.DEMO_MODE;
process.env.APP_URL = BASE;
});
+8 -8
View File
@@ -49,13 +49,13 @@ import { FilesMcp } from '../../../src/nest/files/files.mcp';
import { FILE_CONTENT_MAX } from '../../../src/nest/files/files.service';
import { StorageService } from '../../../src/nest/storage/storage.service';
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
broadcastMock.mockClear();
delete process.env.DEMO_MODE;
// resetTestDb truncates app_settings, but the permission cache is module-scoped
// and would keep serving whatever the previous case configured.
invalidatePermissionsCache();
await invalidatePermissionsCache();
});
afterAll(() => {
@@ -68,9 +68,9 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>)
}
/** Lower a configurable action the way the admin permission panel does. */
function setPermission(action: string, level: string) {
async function setPermission(action: string, level: string): Promise<void> {
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run(`perm_${action}`, level);
invalidatePermissionsCache();
await invalidatePermissionsCache();
}
interface FileRowOverrides {
@@ -435,7 +435,7 @@ describe('Tool: update_trip_file', () => {
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
const file = insertFile(trip.id);
setPermission('file_edit', 'trip_owner');
await setPermission('file_edit', 'trip_owner');
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
@@ -574,7 +574,7 @@ describe('Tool: upload_trip_file', () => {
const { user: member } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
setPermission('file_upload', 'trip_owner');
await setPermission('file_upload', 'trip_owner');
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({ name: 'upload_trip_file', arguments: { tripId: trip.id, filename: 'a.pdf', content: pdf } });
expect(result.isError).toBe(true);
@@ -708,7 +708,7 @@ describe('Tool: link_trip_file', () => {
addTripMember(testDb, trip.id, member.id);
const reservation = createReservation(testDb, trip.id);
const file = insertFile(trip.id);
setPermission('file_edit', 'trip_owner');
await setPermission('file_edit', 'trip_owner');
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
@@ -811,7 +811,7 @@ describe('Tool: unlink_trip_file', () => {
const file = insertFile(trip.id);
testDb.prepare('INSERT INTO file_links (file_id, reservation_id) VALUES (?, ?)').run(file.id, reservation.id);
const linkId = linkRows(file.id)[0].id;
setPermission('file_edit', 'trip_owner');
await setPermission('file_edit', 'trip_owner');
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
+2 -2
View File
@@ -106,7 +106,7 @@ const itinerary = {
],
};
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
geocodeMock.mockReset();
planMock.mockReset();
@@ -114,7 +114,7 @@ beforeEach(() => {
// mockReset would fall back to the real notification write — keep it stubbed.
notifyBookingChangeMock.mockReset().mockImplementation(async () => {});
delete process.env.DEMO_MODE;
invalidatePermissionsCache();
await invalidatePermissionsCache();
});
afterAll(() => testDb.close());
@@ -26,13 +26,13 @@ import { invalidatePermissionsCache } from '../../../src/nest/permissions/permis
import { createUser, createAdmin, createTrip, addTripMember } from '../../helpers/factories';
import { createMcpHarness, parseToolResult, type McpHarness } from '../../helpers/mcp-harness';
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
broadcastMock.mockClear();
delete process.env.DEMO_MODE;
// resetTestDb truncates app_settings, but the permission cache is module-scoped
// and would keep serving whatever the previous case configured.
invalidatePermissionsCache();
await invalidatePermissionsCache();
});
afterAll(() => {
@@ -45,9 +45,9 @@ async function withHarness(userId: number, fn: (h: McpHarness) => Promise<void>)
}
/** Lower a configurable action the way the admin permission panel does. */
function setPermission(action: string, level: string) {
async function setPermission(action: string, level: string): Promise<void> {
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run(`perm_${action}`, level);
invalidatePermissionsCache();
await invalidatePermissionsCache();
}
// ---------------------------------------------------------------------------
@@ -157,7 +157,7 @@ describe('Tool: add_trip_member', () => {
const { user: outsider } = createUser(testDb);
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, collaborator.id);
setPermission('member_manage', 'trip_member');
await setPermission('member_manage', 'trip_member');
await withHarness(collaborator.id, async (h) => {
const result = await h.client.callTool({
name: 'add_trip_member',
@@ -274,7 +274,7 @@ describe('Tool: remove_trip_member', () => {
const trip = createTrip(testDb, owner.id);
addTripMember(testDb, trip.id, member.id);
addTripMember(testDb, trip.id, other.id);
setPermission('member_manage', 'trip_member');
await setPermission('member_manage', 'trip_member');
await withHarness(member.id, async (h) => {
const result = await h.client.callTool({
name: 'remove_trip_member',
@@ -867,7 +867,7 @@ describe('Guest tools stay owner-only', () => {
let guestId = 0;
await withHarness(owner.id, async (h) => { guestId = await makeGuest(h, trip.id, 'Anna'); });
setPermission('member_manage', 'trip_member');
await setPermission('member_manage', 'trip_member');
await withHarness(collaborator.id, async (h) => {
expect((await h.client.callTool({
name: 'create_trip_guest', arguments: { tripId: trip.id, name: 'Bea' },
@@ -606,7 +606,7 @@ describe('trip access and edit permission', () => {
// The permission cache is module-scoped and outlives resetTestDb — drop the row and
// the cache together, or every later case in this file inherits the raised level.
testDb.prepare("DELETE FROM app_settings WHERE key = 'perm_day_edit'").run();
permissions.invalidatePermissionsCache();
await permissions.invalidatePermissionsCache();
}
});
+2 -2
View File
@@ -267,11 +267,11 @@ describe('PasskeyController', () => {
it('throttles registration and login ceremonies once the bucket is exhausted', async () => {
const s = new RateLimitService();
const now = Date.now();
for (let i = 0; i < 5; i++) s.check('mfa', '9.9.9.9', 5, 15 * 60 * 1000, now);
for (let i = 0; i < 5; i++) await s.check('mfa', '9.9.9.9', 5, 15 * 60 * 1000, now);
expect(await thrownAsync(() => pc(s).registerOptions(user, {}, req))).toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
const s2 = new RateLimitService();
for (let i = 0; i < 10; i++) s2.check('login', '9.9.9.9', 10, 15 * 60 * 1000, now);
for (let i = 0; i < 10; i++) await s2.check('login', '9.9.9.9', 10, 15 * 60 * 1000, now);
expect(await thrownAsync(() => pc(s2).loginOptions(req))).toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
});
+27 -27
View File
@@ -60,25 +60,25 @@ beforeEach(() => vi.clearAllMocks());
afterEach(() => { delete process.env.DEMO_MODE; });
describe('RateLimitService', () => {
it('allows up to max then blocks within the window; buckets are isolated', () => {
it('allows up to max then blocks within the window; buckets are isolated', async () => {
const s = rl();
expect(s.check('login', 'ip', 2, 1000, 0)).toBe(true);
expect(s.check('login', 'ip', 2, 1000, 10)).toBe(true);
expect(s.check('login', 'ip', 2, 1000, 20)).toBe(false); // 3rd within window
expect(s.check('mfa', 'ip', 2, 1000, 20)).toBe(true); // different bucket
expect(s.check('login', 'ip', 2, 1000, 2000)).toBe(true); // window elapsed -> reset
expect(await s.check('login', 'ip', 2, 1000, 0)).toBe(true);
expect(await s.check('login', 'ip', 2, 1000, 10)).toBe(true);
expect(await s.check('login', 'ip', 2, 1000, 20)).toBe(false); // 3rd within window
expect(await s.check('mfa', 'ip', 2, 1000, 20)).toBe(true); // different bucket
expect(await s.check('login', 'ip', 2, 1000, 2000)).toBe(true); // window elapsed -> reset
});
it('reset clears a single named bucket, and reset() clears all of them', () => {
it('reset clears a single named bucket, and reset() clears all of them', async () => {
const s = rl();
s.check('login', 'ip', 1, 1000, 0); // login bucket now at its cap
s.check('mfa', 'ip', 1, 1000, 0); // mfa bucket now at its cap
expect(s.check('login', 'ip', 1, 1000, 0)).toBe(false);
s.reset('login'); // only the login bucket
expect(s.check('login', 'ip', 1, 1000, 0)).toBe(true);
expect(s.check('mfa', 'ip', 1, 1000, 0)).toBe(false); // mfa untouched
s.reset(); // everything
expect(s.check('mfa', 'ip', 1, 1000, 0)).toBe(true);
await s.check('login', 'ip', 1, 1000, 0); // login bucket now at its cap
await s.check('mfa', 'ip', 1, 1000, 0); // mfa bucket now at its cap
expect(await s.check('login', 'ip', 1, 1000, 0)).toBe(false);
await s.reset('login'); // only the login bucket
expect(await s.check('login', 'ip', 1, 1000, 0)).toBe(true);
expect(await s.check('mfa', 'ip', 1, 1000, 0)).toBe(false); // mfa untouched
await s.reset(); // everything
expect(await s.check('mfa', 'ip', 1, 1000, 0)).toBe(true);
});
});
@@ -102,7 +102,7 @@ describe('AuthPublicController', () => {
it('invite 429 when rate-limited', async () => {
const s = rl();
s.check('login', '9.9.9.9', 10, 15 * 60 * 1000, Date.now()); // not exhausted yet
await s.check('login', '9.9.9.9', 10, 15 * 60 * 1000, Date.now()); // not exhausted yet
const c = apc(asvc({ validateInviteToken: vi.fn().mockReturnValue({ valid: true, max_uses: 1, used_count: 0, expires_at: null }) } as Partial<AuthService>), s);
expect(await c.invite('tok', req)).toEqual({ valid: true, max_uses: 1, used_count: 0, expires_at: null });
});
@@ -179,7 +179,7 @@ describe('AuthPublicController', () => {
it('reset-password 429 once the dedicated reset bucket is exhausted', async () => {
const s = rl();
const now = Date.now();
for (let i = 0; i < 5; i++) s.check('reset', '9.9.9.9', 5, 15 * 60 * 1000, now);
for (let i = 0; i < 5; i++) await s.check('reset', '9.9.9.9', 5, 15 * 60 * 1000, now);
const c = apc(asvc({ resetPassword: vi.fn() } as Partial<AuthService>), s);
expect(await thrownAsync(() => c.resetPassword(anyBody(), req))).toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
});
@@ -192,7 +192,7 @@ describe('AuthPublicController', () => {
it('demo-login + register + invite throw 429 when the login bucket is exhausted', async () => {
const s = rl();
const now = Date.now();
for (let i = 0; i < 10; i++) s.check('login', '9.9.9.9', 10, 15 * 60 * 1000, now);
for (let i = 0; i < 10; i++) await s.check('login', '9.9.9.9', 10, 15 * 60 * 1000, now);
const c = apc(asvc({ registerUser: vi.fn(), validateInviteToken: vi.fn() } as Partial<AuthService>), s);
expect(await thrownAsync(() => c.register(anyBody(), req, res))).toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
expect(await thrownAsync(() => c.invite('t', req))).toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
@@ -248,23 +248,23 @@ describe('AuthController (authenticated)', () => {
expect(await tok.createMcpToken(user, { name: 'CLI' }, req)).toEqual({ token: 'mcp_x' });
});
it('resource-token 503 when unavailable, else returns the token payload', () => {
expect(thrown(() => ac(asvc({}), rl(), { createResourceToken: vi.fn().mockReturnValue(null) }).resourceToken(user, {}))).toEqual({ status: 503, body: { error: 'Service unavailable' } });
expect(ac(asvc({}), rl(), { createResourceToken: vi.fn().mockReturnValue({ token: 'rt' }) }).resourceToken(user, { purpose: 'download' })).toEqual({ token: 'rt' });
it('resource-token 503 when unavailable, else returns the token payload', async () => {
expect(await thrownAsync(() => ac(asvc({}), rl(), { createResourceToken: vi.fn().mockReturnValue(null) }).resourceToken(user, {}))).toEqual({ status: 503, body: { error: 'Service unavailable' } });
expect(await ac(asvc({}), rl(), { createResourceToken: vi.fn().mockReturnValue({ token: 'rt' }) }).resourceToken(user, { purpose: 'download' })).toEqual({ token: 'rt' });
});
it('ws/resource tokens throttle on their own buckets, so a mint loop cannot drain the store or block login', async () => {
const s = rl();
const now = Date.now();
for (let i = 0; i < 120; i++) s.check('ws_token', String(user.id), 120, 15 * 60 * 1000, now);
for (let i = 0; i < 120; i++) await s.check('ws_token', String(user.id), 120, 15 * 60 * 1000, now);
expect(await thrownAsync(() => ac(asvc({}), s, { createWsToken: vi.fn().mockReturnValue({ token: 'ws' }) }).wsToken(user)))
.toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
// The login bucket is untouched: an exhausted socket loop must not lock the account out.
expect(s.check('login', '9.9.9.9', 5, 15 * 60 * 1000, now)).toBe(true);
expect(await s.check('login', '9.9.9.9', 5, 15 * 60 * 1000, now)).toBe(true);
const s2 = rl();
for (let i = 0; i < 120; i++) s2.check('resource_token', String(user.id), 120, 15 * 60 * 1000, now);
expect(thrown(() => ac(asvc({}), s2, { createResourceToken: vi.fn().mockReturnValue({ token: 'rt' }) }).resourceToken(user, {})))
for (let i = 0; i < 120; i++) await s2.check('resource_token', String(user.id), 120, 15 * 60 * 1000, now);
expect(await thrownAsync(() => ac(asvc({}), s2, { createResourceToken: vi.fn().mockReturnValue({ token: 'rt' }) }).resourceToken(user, {})))
.toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
});
@@ -281,14 +281,14 @@ describe('AuthController (authenticated)', () => {
.toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
expect(await ac(asvc({}), s, { createWsToken: vi.fn().mockReturnValue({ token: 'ws2' }) }).wsToken(other)).toEqual({ token: 'ws2' });
expect(ac(asvc({}), s, { createResourceToken: vi.fn().mockReturnValue({ token: 'rt2' }) }).resourceToken(other, {})).toEqual({ token: 'rt2' });
expect(await ac(asvc({}), s, { createResourceToken: vi.fn().mockReturnValue({ token: 'rt2' }) }).resourceToken(other, {})).toEqual({ token: 'rt2' });
});
it('rate-limited account ops throw 429 once the bucket is exhausted', async () => {
const s = rl();
const now = Date.now();
// exhaust the shared 'login' bucket for this ip (max 5)
for (let i = 0; i < 5; i++) s.check('login', '9.9.9.9', 5, 15 * 60 * 1000, now);
for (let i = 0; i < 5; i++) await s.check('login', '9.9.9.9', 5, 15 * 60 * 1000, now);
const c = ac(asvc({ changePassword: vi.fn() } as Partial<AuthService>), s);
expect(await thrownAsync(() => c.changePassword(user, anyBody(), req, res))).toEqual({ status: 429, body: { error: 'Too many attempts. Please try again later.' } });
});
+24 -2
View File
@@ -740,15 +740,37 @@ describe('linkPreview hardening', () => {
});
vi.stubGlobal('fetch', fetchMock);
const service = await freshSvc();
const limiter = new RateLimitService();
const check = vi.spyOn(limiter, 'check');
const service = await buildCollabService(collabFx.storage, limiter);
const all = Promise.all(Array.from({ length: 20 }, () => service.linkPreview('https://example.com/same', 9)));
release!();
const results = await all;
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(results.every(r => r.title === 'Geteilt')).toBe(true);
// And the nineteen that joined were not charged for a fetch they did not make.
// And the nineteen that joined were not charged for a fetch they did not
// make, although the budget check awaits its store before the fetch starts.
expect(results.some(r => r.rateLimited)).toBe(false);
expect(check).toHaveBeenCalledTimes(1);
});
it('COLLAB-SVC-049: an asker that joined someone out of budget asks again on its own budget', async () => {
const fetchMock = stubFetch({ ok: true, text: async () => '<title>Eigenes Budget</title>' });
const limiter = new RateLimitService();
for (let i = 0; i < 60; i++) await limiter.check('collab_link_preview', '42', 60, 60_000, Date.now());
const service = await buildCollabService(collabFx.storage, limiter);
// 42 is first in line and out of budget; 43 arrives while 42's check runs.
const [spent, fresh] = await Promise.all([
service.linkPreview('https://example.com/shared-link', 42),
service.linkPreview('https://example.com/shared-link', 43),
]);
expect(spent.rateLimited).toBe(true);
expect(fresh.rateLimited).toBeUndefined();
expect(fresh.title).toBe('Eigenes Budget');
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it('COLLAB-SVC-047: a body over the cap, and one behind a failed response, are both released', async () => {
@@ -25,6 +25,7 @@ import { clientRegistrationHandler } from '@modelcontextprotocol/sdk/server/auth
import { InvalidClientMetadataError, ServerError } from '@modelcontextprotocol/sdk/server/auth/errors';
import { TrekClientsStore, TrekOAuthProvider } from '../../../src/nest/oauth/oauth-sdk.provider';
import { OauthModule } from '../../../src/nest/oauth/oauth.module';
import { processPendingCodes } from '../../../src/nest/oauth/oauth.pending-codes';
import type { AddonsService } from '../../../src/nest/addons/addons.service';
import { ALL_SCOPES, DEFAULT_CLIENT_SCOPES, OPT_IN_ONLY_SCOPES } from '../../../src/mcp/scopes';
import type { OauthService } from '../../../src/nest/oauth/oauth.service';
@@ -362,30 +363,30 @@ describe('TrekOAuthProvider.exchangeAuthorizationCode', () => {
const invalid = 'Authorization grant is invalid.';
it('SDKP-040: unknown/expired code throws the uniform grant error', async () => {
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(() => null) }));
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(async () => null) }));
await expect(provider.exchangeAuthorizationCode(clientInfo(), 'code')).rejects.toThrow(invalid);
});
it('SDKP-041: a code minted for another client throws the same error', async () => {
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(() => pending({ clientId: 'someone-else' })) }));
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(async () => pending({ clientId: 'someone-else' })) }));
await expect(provider.exchangeAuthorizationCode(clientInfo(), 'code')).rejects.toThrow(invalid);
});
it('SDKP-042: redirect_uri mismatch throws the same error', async () => {
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(() => pending()) }));
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(async () => pending()) }));
await expect(provider.exchangeAuthorizationCode(clientInfo(), 'code', undefined, 'https://wrong.example.com/cb'))
.rejects.toThrow(invalid);
});
it('SDKP-043: resource mismatch throws the same error', async () => {
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(() => pending()) }));
const { provider } = makeProvider(makeOauth({ consumeAuthCode: vi.fn(async () => pending()) }));
await expect(provider.exchangeAuthorizationCode(
clientInfo(), 'code', undefined, 'https://client.example.com/cb', new URL('https://other.example.com/'),
)).rejects.toThrow(invalid);
});
it('SDKP-044: failed PKCE throws the same error', async () => {
const oauth = makeOauth({ consumeAuthCode: vi.fn(() => pending()), verifyPKCE: vi.fn(() => false) });
const oauth = makeOauth({ consumeAuthCode: vi.fn(async () => pending()), verifyPKCE: vi.fn(() => false) });
const { provider } = makeProvider(oauth);
await expect(provider.exchangeAuthorizationCode(clientInfo(), 'code', 'bad-verifier', 'https://client.example.com/cb'))
.rejects.toThrow(invalid);
@@ -393,7 +394,7 @@ describe('TrekOAuthProvider.exchangeAuthorizationCode', () => {
});
it('SDKP-045: a valid exchange issues tokens and audits the issue', async () => {
const oauth = makeOauth({ consumeAuthCode: vi.fn(() => pending()) });
const oauth = makeOauth({ consumeAuthCode: vi.fn(async () => pending()) });
const audit = makeAudit();
const { provider } = makeProvider(oauth, audit);
const result = await provider.exchangeAuthorizationCode(
@@ -410,14 +411,14 @@ describe('TrekOAuthProvider.exchangeAuthorizationCode', () => {
});
it('SDKP-046: a pending code without a resource issues a null-audience token', async () => {
const oauth = makeOauth({ consumeAuthCode: vi.fn(() => pending({ resource: null })) });
const oauth = makeOauth({ consumeAuthCode: vi.fn(async () => pending({ resource: null })) });
const { provider } = makeProvider(oauth);
await provider.exchangeAuthorizationCode(clientInfo(), 'code', 'verifier');
expect(oauth.issueTokens).toHaveBeenCalledWith('cid-1', 7, ['trips:read'], null, null);
});
it('SDKP-047: a missing codeVerifier is refused, not waved through', async () => {
const oauth = makeOauth({ consumeAuthCode: vi.fn(() => pending()) });
const oauth = makeOauth({ consumeAuthCode: vi.fn(async () => pending()) });
const { provider } = makeProvider(oauth);
await expect(provider.exchangeAuthorizationCode(clientInfo(), 'code')).rejects.toThrow(invalid);
expect(oauth.issueTokens).not.toHaveBeenCalled();
@@ -485,7 +486,7 @@ describe('OauthModule.configure', () => {
})),
};
new OauthModule(addons, provider, clients).configure(consumer as never);
new OauthModule(addons, provider, clients, processPendingCodes).configure(consumer as never);
expect(vi.mocked(authorizationHandler)).toHaveBeenCalledWith({ provider });
expect(vi.mocked(clientRegistrationHandler)).toHaveBeenCalledWith({ clientsStore: clients });
@@ -150,7 +150,7 @@ describe('OauthPublicController /token', () => {
it('429 when the token bucket is exhausted (per ip|client)', async () => {
const s = rl();
for (let i = 0; i < 30; i++) s.check('oauth_token', '7.7.7.7|c', 30, 60000, Date.now());
for (let i = 0; i < 30; i++) await s.check('oauth_token', '7.7.7.7|c', 30, 60000, Date.now());
const res = makeRes();
await opc(osvc(), s).token(reqWith({ client_id: 'c' }), res);
expect(res.statusCode).toBe(429);
@@ -293,7 +293,7 @@ describe('OauthPublicController /userinfo + /revoke', () => {
it('revoke: 429 when the per-ip bucket is exhausted', async () => {
const s = rl();
for (let i = 0; i < 10; i++) s.check('oauth_revoke', '1', 10, 60000, Date.now());
for (let i = 0; i < 10; i++) await s.check('oauth_revoke', '1', 10, 60000, Date.now());
const res = makeRes();
await opc(osvc(), s).revoke({ ip: '1', body: { token: 't', client_id: 'c' } } as Request, res);
expect(res.statusCode).toBe(429);
@@ -375,7 +375,7 @@ describe('OauthApiController', () => {
it('validate: 429 when the per-ip bucket is exhausted', async () => {
const s = rl();
for (let i = 0; i < 30; i++) s.check('oauth_validate', '1.2.3.4', 30, 60000, Date.now());
for (let i = 0; i < 30; i++) await s.check('oauth_validate', '1.2.3.4', 30, 60000, Date.now());
const res = makeRes2();
expect(await thrownAsync(() => new OauthApiController(osvc(), s).validate({ ...req } as Request, {}, res))).toEqual({
status: 429,
+16 -16
View File
@@ -389,7 +389,7 @@ describe('createAuthCode + consumeAuthCode', () => {
const created = await makeClient(user.id);
const clientId = created.client!.client_id as string;
const code = createAuthCode({
const code = await createAuthCode({
clientId,
userId: user.id,
redirectUri: 'https://example.com/callback',
@@ -399,14 +399,14 @@ describe('createAuthCode + consumeAuthCode', () => {
codeChallengeMethod: 'S256',
});
const entry = consumeAuthCode(code);
const entry = await consumeAuthCode(code);
expect(entry).not.toBeNull();
expect(entry!.userId).toBe(user.id);
expect(entry!.clientId).toBe(clientId);
});
it('returns null for non-existent code', async () => {
expect(consumeAuthCode('does-not-exist')).toBeNull();
expect(await consumeAuthCode('does-not-exist')).toBeNull();
});
it('consuming same code twice returns null (one-time use)', async () => {
@@ -414,7 +414,7 @@ describe('createAuthCode + consumeAuthCode', () => {
const created = await makeClient(user.id);
const clientId = created.client!.client_id as string;
const code = createAuthCode({
const code = await createAuthCode({
clientId,
userId: user.id,
redirectUri: 'https://example.com/callback',
@@ -424,8 +424,8 @@ describe('createAuthCode + consumeAuthCode', () => {
codeChallengeMethod: 'S256',
});
consumeAuthCode(code);
expect(consumeAuthCode(code)).toBeNull();
await consumeAuthCode(code);
expect(await consumeAuthCode(code)).toBeNull();
});
});
@@ -1189,24 +1189,24 @@ describe('pending-code store', () => {
};
it('refuses a new code at capacity and the sweep frees it again', async () => {
for (let i = 0; i < MAX_PENDING_CODES; i++) createAuthCode(codeParams);
for (let i = 0; i < MAX_PENDING_CODES; i++) await createAuthCode(codeParams);
expect(createAuthCode(codeParams)).toBeNull();
expect(await createAuthCode(codeParams)).toBeNull();
// Everything in the store is past its 2-minute TTL by then.
sweepPendingCodes(Date.now() + 3 * 60 * 1000);
await sweepPendingCodes(Date.now() + 3 * 60 * 1000);
const afterSweep = createAuthCode(codeParams);
const afterSweep = await createAuthCode(codeParams);
expect(afterSweep).not.toBeNull();
sweepPendingCodes(Date.now() + 3 * 60 * 1000);
await sweepPendingCodes(Date.now() + 3 * 60 * 1000);
});
it('a code past its TTL is consumed as invalid', async () => {
const code = createAuthCode(codeParams)!;
const code = (await createAuthCode(codeParams))!;
const realNow = Date.now;
Date.now = () => realNow() + 3 * 60 * 1000;
try {
expect(consumeAuthCode(code)).toBeNull();
expect(await consumeAuthCode(code)).toBeNull();
} finally {
Date.now = realNow;
}
@@ -1298,7 +1298,7 @@ describe('module-scoped OAuth state', () => {
// the DI singleton, the SDK exchange path reads it back. The map is module-
// scoped, so even a second hand-built instance must see it — two maps would
// kill the authorization-code flow silently.
const code = createAuthCode({
const code = (await createAuthCode({
clientId: 'c',
userId: 42,
redirectUri: 'https://example.com/callback',
@@ -1306,10 +1306,10 @@ describe('module-scoped OAuth state', () => {
resource: null,
codeChallenge: 'x',
codeChallengeMethod: 'S256',
})!;
}))!;
const secondInstance = new OauthService(clientsRepo, tokensRepo, consentsRepo, addonsStub, new AuditService(auditLogRepo, usersRepo));
expect(secondInstance.consumeAuthCode(code)?.userId).toBe(42);
expect((await secondInstance.consumeAuthCode(code))?.userId).toBe(42);
});
});
+25 -25
View File
@@ -18,15 +18,15 @@ function svc(o: Partial<OidcService> = {}): OidcService {
getOidcConfig: vi.fn().mockReturnValue({ issuer: 'https://idp', clientId: 'c', clientSecret: 's', discoveryUrl: null }),
getAppUrl: vi.fn().mockReturnValue('https://app'),
discover: vi.fn().mockResolvedValue({ authorization_endpoint: 'https://idp/auth', userinfo_endpoint: 'https://idp/ui', issuer: 'https://idp' }),
createState: vi.fn().mockReturnValue({ state: 'st', codeChallenge: 'cc' }),
consumeState: vi.fn().mockReturnValue({ redirectUri: 'https://app/api/auth/oidc/callback', codeVerifier: 'cv', inviteToken: undefined }),
createState: vi.fn().mockResolvedValue({ state: 'st', codeChallenge: 'cc' }),
consumeState: vi.fn().mockResolvedValue({ redirectUri: 'https://app/api/auth/oidc/callback', codeVerifier: 'cv', inviteToken: undefined }),
exchangeCodeForToken: vi.fn(),
verifyIdToken: vi.fn(),
getUserInfo: vi.fn(),
findOrCreateUser: vi.fn(),
touchLastLogin: vi.fn(),
generateToken: vi.fn().mockReturnValue('jwt'),
createAuthCode: vi.fn().mockReturnValue({ code: 'ac', binding: 'bnd' }),
createAuthCode: vi.fn().mockResolvedValue({ code: 'ac', binding: 'bnd' }),
consumeAuthCode: vi.fn(),
frontendUrl: vi.fn((p: string) => 'https://app' + p),
setAuthCookie: vi.fn(),
@@ -112,7 +112,7 @@ describe('OidcController /login', () => {
it('passes the invite token from the query into createState', async () => {
const res = makeRes();
const createState = vi.fn().mockReturnValue({ state: 'st', codeChallenge: 'cc' });
const createState = vi.fn().mockResolvedValue({ state: 'st', codeChallenge: 'cc' });
const reqInvite = { query: { invite: 'tok123' }, headers: {} } as unknown as Request;
await ctl(svc({ createState })).login(reqInvite, res);
expect(createState).toHaveBeenCalledWith('https://app/api/auth/oidc/callback', 'tok123', undefined);
@@ -124,7 +124,7 @@ describe('OidcController /login', () => {
[{ remember: '0' }, false],
[{}, undefined],
] as const) {
const createState = vi.fn().mockReturnValue({ state: 'st', codeChallenge: 'cc' });
const createState = vi.fn().mockResolvedValue({ state: 'st', codeChallenge: 'cc' });
const r = makeRes();
await ctl(svc({ createState })).login({ query, headers: {} } as unknown as Request, r);
expect(createState).toHaveBeenCalledWith('https://app/api/auth/oidc/callback', undefined, expected);
@@ -132,7 +132,7 @@ describe('OidcController /login', () => {
});
it('a malformed remember value never blocks the redirect and falls back to the default duration', async () => {
const createState = vi.fn().mockReturnValue({ state: 'st', codeChallenge: 'cc' });
const createState = vi.fn().mockResolvedValue({ state: 'st', codeChallenge: 'cc' });
const res = makeRes();
const reqBad = { query: { invite: 'tok123', remember: 'yes' }, headers: {} } as unknown as Request;
await ctl(svc({ createState })).login(reqBad, res);
@@ -142,8 +142,8 @@ describe('OidcController /login', () => {
it('trims a trailing slash off APP_URL when building the redirect uri', async () => {
const res = makeRes();
const createState = vi.fn().mockReturnValue({ state: 'st', codeChallenge: 'cc' });
await ctl(svc({ getAppUrl: vi.fn().mockReturnValue('https://app///'), createState })).login(req, res);
const createState = vi.fn().mockResolvedValue({ state: 'st', codeChallenge: 'cc' });
await ctl(svc({ getAppUrl: vi.fn().mockResolvedValue('https://app///'), createState })).login(req, res);
expect(createState).toHaveBeenCalledWith('https://app/api/auth/oidc/callback', undefined, undefined);
});
@@ -182,7 +182,7 @@ describe('OidcController /callback', () => {
await ctl(svc()).callback(undefined, 's', undefined, reqCb('s'), r1);
expect(r1.redirectedTo).toBe('https://app/login?oidc_error=missing_params');
const r2 = makeRes();
await ctl(svc({ consumeState: vi.fn().mockReturnValue(null) })).callback('c', 's', undefined, reqCb('s'), r2);
await ctl(svc({ consumeState: vi.fn().mockResolvedValue(null) })).callback('c', 's', undefined, reqCb('s'), r2);
expect(r2.redirectedTo).toBe('https://app/login?oidc_error=invalid_state');
});
@@ -453,10 +453,10 @@ describe('OidcController /callback', () => {
// to a browser-session cookie half a lifetime later.
for (const remember of [true, false, undefined] as const) {
const generateToken = vi.fn().mockReturnValue('jwt');
const createAuthCode = vi.fn().mockReturnValue({ code: 'ac', binding: 'bnd' });
const createAuthCode = vi.fn().mockResolvedValue({ code: 'ac', binding: 'bnd' });
const res = makeRes();
await ctl(svc({
consumeState: vi.fn().mockReturnValue({ redirectUri: 'https://app/api/auth/oidc/callback', codeVerifier: 'cv', inviteToken: undefined, remember }),
consumeState: vi.fn().mockResolvedValue({ redirectUri: 'https://app/api/auth/oidc/callback', codeVerifier: 'cv', inviteToken: undefined, remember }),
exchangeCodeForToken: vi.fn().mockResolvedValue({ _ok: true, access_token: 'at', id_token: 'it' }),
verifyIdToken: vi.fn().mockResolvedValue({ ok: true, claims: { sub: 'u1' } }),
getUserInfo: vi.fn().mockResolvedValue({ email: 'a@b.c', sub: 'u1' }),
@@ -471,57 +471,57 @@ describe('OidcController /callback', () => {
});
describe('OidcController /exchange', () => {
it('400 without a code, 400 on an invalid code, else sets the cookie + returns the token', () => {
it('400 without a code, 400 on an invalid code, else sets the cookie + returns the token', async () => {
const r1 = makeRes();
ctl(svc()).exchange(undefined, reqEx(), r1);
await ctl(svc()).exchange(undefined, reqEx(), r1);
expect(r1.statusCode).toBe(400);
expect(r1.body).toEqual({ error: 'Code required' });
const r2 = makeRes();
ctl(svc({ consumeAuthCode: vi.fn().mockReturnValue({ error: 'invalid_code' }) })).exchange('x', reqEx(), r2);
await ctl(svc({ consumeAuthCode: vi.fn().mockResolvedValue({ error: 'invalid_code' }) })).exchange('x', reqEx(), r2);
expect(r2.statusCode).toBe(400);
expect(r2.body).toEqual({ error: 'invalid_code' });
const r3 = makeRes();
const setAuthCookie = vi.fn();
const req3 = reqEx();
ctl(svc({ consumeAuthCode: vi.fn().mockReturnValue({ token: 'jwt' }), setAuthCookie })).exchange('x', req3, r3);
await ctl(svc({ consumeAuthCode: vi.fn().mockResolvedValue({ token: 'jwt' }), setAuthCookie })).exchange('x', req3, r3);
expect(setAuthCookie).toHaveBeenCalledWith(r3, 'jwt', req3, undefined);
expect(r3.body).toEqual({ token: 'jwt' });
});
it('forwards the stored remember flag to setAuthCookie', () => {
it('forwards the stored remember flag to setAuthCookie', async () => {
for (const remember of [true, false] as const) {
const res = makeRes();
const setAuthCookie = vi.fn();
const r = reqEx();
ctl(svc({ consumeAuthCode: vi.fn().mockReturnValue({ token: 'jwt', remember }), setAuthCookie })).exchange('x', r, res);
await ctl(svc({ consumeAuthCode: vi.fn().mockResolvedValue({ token: 'jwt', remember }), setAuthCookie })).exchange('x', r, res);
expect(setAuthCookie).toHaveBeenCalledWith(res, 'jwt', r, remember);
expect(res.body).toEqual({ token: 'jwt' });
}
});
it('hands the binding cookie to consumeAuthCode and clears it on the way out', () => {
const consumeAuthCode = vi.fn().mockReturnValue({ token: 'jwt' });
it('hands the binding cookie to consumeAuthCode and clears it on the way out', async () => {
const consumeAuthCode = vi.fn().mockResolvedValue({ token: 'jwt' });
const res = makeRes();
ctl(svc({ consumeAuthCode })).exchange('x', reqEx('the-secret'), res);
await ctl(svc({ consumeAuthCode })).exchange('x', reqEx('the-secret'), res);
expect(consumeAuthCode).toHaveBeenCalledWith('x', 'the-secret');
expect(res.clearCookie).toHaveBeenCalledWith('trek_oidc_exchange', expect.objectContaining({ httpOnly: true, path: '/' }));
});
it('passes undefined when the browser has no binding cookie, and still clears it', () => {
const consumeAuthCode = vi.fn().mockReturnValue({ error: 'Invalid or expired code' });
it('passes undefined when the browser has no binding cookie, and still clears it', async () => {
const consumeAuthCode = vi.fn().mockResolvedValue({ error: 'Invalid or expired code' });
const res = makeRes();
// A request from any other context: no cookie jar for this origin at all.
ctl(svc({ consumeAuthCode })).exchange('stolen', { query: {}, headers: {} } as Request, res);
await ctl(svc({ consumeAuthCode })).exchange('stolen', { query: {}, headers: {} } as Request, res);
expect(consumeAuthCode).toHaveBeenCalledWith('stolen', undefined);
expect(res.statusCode).toBe(400);
expect(res.clearCookie).toHaveBeenCalledWith('trek_oidc_exchange', expect.anything());
});
it('clears the binding cookie even when no code was presented', () => {
it('clears the binding cookie even when no code was presented', async () => {
const res = makeRes();
ctl(svc()).exchange(undefined, reqEx(), res);
await ctl(svc()).exchange(undefined, reqEx(), res);
expect(res.clearCookie).toHaveBeenCalledWith('trek_oidc_exchange', expect.anything());
});
});
+78 -56
View File
@@ -100,6 +100,7 @@ import { UserCleanupService } from '../../../src/nest/auth/user-cleanup.service'
import { EphemeralTokenService } from '../../../src/nest/auth/ephemeral-token.service';
import { AllowedFileTypesService } from '../../../src/nest/files/allowed-file-types.service';
import { OidcService } from '../../../src/nest/oidc/oidc.service';
import { InMemoryOidcFlowStore } from '../../../src/nest/oidc/oidc-flow.store';
import { MailerService } from '../../../src/nest/notifications/mailer/mailer.service';
import {
createTestUnitOfWork,
@@ -178,51 +179,51 @@ afterAll(() => {
// ── createState / consumeState ────────────────────────────────────────────────
describe('createState / consumeState', () => {
it('OIDC-SVC-001: createState returns a hex token + PKCE S256 challenge', () => {
const { state, codeChallenge } = svc.createState('https://example.com/callback');
it('OIDC-SVC-001: createState returns a hex token + PKCE S256 challenge', async () => {
const { state, codeChallenge } = await svc.createState('https://example.com/callback');
expect(state).toMatch(/^[0-9a-f]{64}$/);
expect(codeChallenge).toMatch(/^[A-Za-z0-9_-]{43}$/); // base64url SHA-256, no padding
});
it('OIDC-SVC-002: consumeState returns stored data (incl. verifier) and deletes state', () => {
const { state } = svc.createState('https://example.com/callback', 'invite-abc');
const data = svc.consumeState(state);
it('OIDC-SVC-002: consumeState returns stored data (incl. verifier) and deletes state', async () => {
const { state } = await svc.createState('https://example.com/callback', 'invite-abc');
const data = await svc.consumeState(state);
expect(data).not.toBeNull();
expect(data!.redirectUri).toBe('https://example.com/callback');
expect(data!.inviteToken).toBe('invite-abc');
expect(typeof data!.codeVerifier).toBe('string');
expect(data!.codeVerifier.length).toBeGreaterThan(20);
// State is consumed — second call returns null
expect(svc.consumeState(state)).toBeNull();
expect(await svc.consumeState(state)).toBeNull();
});
it('OIDC-SVC-003: consumeState returns null for unknown state', () => {
expect(svc.consumeState('not-a-real-state')).toBeNull();
it('OIDC-SVC-003: consumeState returns null for unknown state', async () => {
expect(await svc.consumeState('not-a-real-state')).toBeNull();
});
it('OIDC-SVC-055: createState stores the remember flag and consumeState returns it', () => {
const { state: sTrue } = svc.createState('https://example.com/cb', undefined, true);
const { state: sFalse } = svc.createState('https://example.com/cb', undefined, false);
const { state: sAbsent } = svc.createState('https://example.com/cb');
expect(svc.consumeState(sTrue)!.remember).toBe(true);
expect(svc.consumeState(sFalse)!.remember).toBe(false);
expect(svc.consumeState(sAbsent)!.remember).toBeUndefined();
it('OIDC-SVC-055: createState stores the remember flag and consumeState returns it', async () => {
const { state: sTrue } = await svc.createState('https://example.com/cb', undefined, true);
const { state: sFalse } = await svc.createState('https://example.com/cb', undefined, false);
const { state: sAbsent } = await svc.createState('https://example.com/cb');
expect((await svc.consumeState(sTrue))!.remember).toBe(true);
expect((await svc.consumeState(sFalse))!.remember).toBe(false);
expect((await svc.consumeState(sAbsent))!.remember).toBeUndefined();
});
it('OIDC-SVC-004: two different states do not conflict', () => {
const { state: s1 } = svc.createState('http://a.example.com');
const { state: s2 } = svc.createState('http://b.example.com');
it('OIDC-SVC-004: two different states do not conflict', async () => {
const { state: s1 } = await svc.createState('http://a.example.com');
const { state: s2 } = await svc.createState('http://b.example.com');
expect(s1).not.toBe(s2);
expect(svc.consumeState(s1)!.redirectUri).toBe('http://a.example.com');
expect(svc.consumeState(s2)!.redirectUri).toBe('http://b.example.com');
expect((await svc.consumeState(s1))!.redirectUri).toBe('http://a.example.com');
expect((await svc.consumeState(s2))!.redirectUri).toBe('http://b.example.com');
});
});
// ── createAuthCode / consumeAuthCode ─────────────────────────────────────────
describe('createAuthCode / consumeAuthCode', () => {
it('OIDC-SVC-005: createAuthCode returns a code and the secret that redeems it', () => {
const { code, binding } = svc.createAuthCode('my.jwt.token');
it('OIDC-SVC-005: createAuthCode returns a code and the secret that redeems it', async () => {
const { code, binding } = await svc.createAuthCode('my.jwt.token');
expect(typeof code).toBe('string');
expect(code.length).toBeGreaterThan(0);
// 32 random bytes as base64url; the code alone must never be enough.
@@ -230,62 +231,62 @@ describe('createAuthCode / consumeAuthCode', () => {
expect(binding).not.toBe(code);
});
it('OIDC-SVC-005b: every code gets its own binding secret', () => {
const a = svc.createAuthCode('t');
const b = svc.createAuthCode('t');
it('OIDC-SVC-005b: every code gets its own binding secret', async () => {
const a = await svc.createAuthCode('t');
const b = await svc.createAuthCode('t');
expect(a.binding).not.toBe(b.binding);
expect(a.code).not.toBe(b.code);
});
it('OIDC-SVC-006: consumeAuthCode returns the stored token', () => {
const { code, binding } = svc.createAuthCode('real.jwt.here');
const result = svc.consumeAuthCode(code, binding);
it('OIDC-SVC-006: consumeAuthCode returns the stored token', async () => {
const { code, binding } = await svc.createAuthCode('real.jwt.here');
const result = await svc.consumeAuthCode(code, binding);
expect('token' in result).toBe(true);
expect((result as { token: string }).token).toBe('real.jwt.here');
});
it('OIDC-SVC-007: auth code is single-use (second consume returns error)', () => {
const { code, binding } = svc.createAuthCode('single.use.token');
svc.consumeAuthCode(code, binding); // first use
const second = svc.consumeAuthCode(code, binding);
it('OIDC-SVC-007: auth code is single-use (second consume returns error)', async () => {
const { code, binding } = await svc.createAuthCode('single.use.token');
await svc.consumeAuthCode(code, binding); // first use
const second = await svc.consumeAuthCode(code, binding);
expect('error' in second).toBe(true);
});
it('OIDC-SVC-008: consumeAuthCode returns error for unknown code', () => {
const result = svc.consumeAuthCode('not-a-real-code', 'whatever');
it('OIDC-SVC-008: consumeAuthCode returns error for unknown code', async () => {
const result = await svc.consumeAuthCode('not-a-real-code', 'whatever');
expect('error' in result).toBe(true);
});
it('OIDC-SVC-008b: a valid code without its binding is refused, and indistinguishable from an unknown one', () => {
const { code } = svc.createAuthCode('bound.token');
expect(svc.consumeAuthCode(code, undefined)).toEqual({ error: 'Invalid or expired code' });
expect(svc.consumeAuthCode('not-a-real-code', undefined)).toEqual({ error: 'Invalid or expired code' });
it('OIDC-SVC-008b: a valid code without its binding is refused, and indistinguishable from an unknown one', async () => {
const { code } = await svc.createAuthCode('bound.token');
expect(await svc.consumeAuthCode(code, undefined)).toEqual({ error: 'Invalid or expired code' });
expect(await svc.consumeAuthCode('not-a-real-code', undefined)).toEqual({ error: 'Invalid or expired code' });
});
it('OIDC-SVC-008c: another browser binding is refused', () => {
const mine = svc.createAuthCode('mine');
const theirs = svc.createAuthCode('theirs');
expect('error' in svc.consumeAuthCode(mine.code, theirs.binding)).toBe(true);
it('OIDC-SVC-008c: another browser binding is refused', async () => {
const mine = await svc.createAuthCode('mine');
const theirs = await svc.createAuthCode('theirs');
expect('error' in (await svc.consumeAuthCode(mine.code, theirs.binding))).toBe(true);
});
it('OIDC-SVC-008d: a wrong binding burns the code, so it cannot be retried', () => {
const { code, binding } = svc.createAuthCode('burn.me');
svc.consumeAuthCode(code, 'not-the-binding');
expect('error' in svc.consumeAuthCode(code, binding)).toBe(true);
it('OIDC-SVC-008d: a wrong binding burns the code, so it cannot be retried', async () => {
const { code, binding } = await svc.createAuthCode('burn.me');
await svc.consumeAuthCode(code, 'not-the-binding');
expect('error' in (await svc.consumeAuthCode(code, binding))).toBe(true);
});
it('OIDC-SVC-008e: a binding that is not even base64 is refused instead of throwing', () => {
const { code } = svc.createAuthCode('t');
expect('error' in svc.consumeAuthCode(code, '!!!!')).toBe(true);
it('OIDC-SVC-008e: a binding that is not even base64 is refused instead of throwing', async () => {
const { code } = await svc.createAuthCode('t');
expect('error' in (await svc.consumeAuthCode(code, '!!!!'))).toBe(true);
});
it('OIDC-SVC-056: auth code round-trips the remember flag', () => {
const cTrue = svc.createAuthCode('t1', true);
const cFalse = svc.createAuthCode('t2', false);
const cAbsent = svc.createAuthCode('t3');
expect((svc.consumeAuthCode(cTrue.code, cTrue.binding) as { remember?: boolean }).remember).toBe(true);
expect((svc.consumeAuthCode(cFalse.code, cFalse.binding) as { remember?: boolean }).remember).toBe(false);
expect((svc.consumeAuthCode(cAbsent.code, cAbsent.binding) as { remember?: boolean }).remember).toBeUndefined();
it('OIDC-SVC-056: auth code round-trips the remember flag', async () => {
const cTrue = await svc.createAuthCode('t1', true);
const cFalse = await svc.createAuthCode('t2', false);
const cAbsent = await svc.createAuthCode('t3');
expect(((await svc.consumeAuthCode(cTrue.code, cTrue.binding)) as { remember?: boolean }).remember).toBe(true);
expect(((await svc.consumeAuthCode(cFalse.code, cFalse.binding)) as { remember?: boolean }).remember).toBe(false);
expect(((await svc.consumeAuthCode(cAbsent.code, cAbsent.binding)) as { remember?: boolean }).remember).toBeUndefined();
});
});
@@ -1401,3 +1402,24 @@ describe('OIDC settings — the lockout guard', () => {
toggles.mockRestore();
});
});
describe('the flow-store sweeps', () => {
it('OIDC-SVC-093: the interval sweeps go through the injected store, and a failed sweep is logged, not thrown', async () => {
vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
const flows = new InMemoryOidcFlowStore();
const sweepStates = vi.spyOn(flows, 'sweepStates').mockRejectedValue(new Error('store offline'));
const sweepCodes = vi.spyOn(flows, 'sweepCodes');
const fresh = new OidcService(auth, membership, await createTestUnitOfWork(testDb), await createTestUsersRepo(testDb), await createTestInviteTokensRepo(testDb), await createTestAppSettingsRepo(testDb), flows);
try {
vi.advanceTimersByTime(60_000);
expect(sweepCodes).toHaveBeenCalled();
expect(sweepStates).toHaveBeenCalledTimes(1);
await vi.waitFor(() => expect(error).toHaveBeenCalledWith(expect.stringContaining('OIDC flow sweep failed: store offline')));
} finally {
fresh.onModuleDestroy();
error.mockRestore();
vi.useRealTimers();
}
});
});
@@ -65,11 +65,11 @@ beforeAll(async () => {
svc = new PermissionsService(appSettings, await createTestUnitOfWork(testDb));
});
beforeEach(() => {
beforeEach(async () => {
vi.clearAllMocks();
testDb.prepare("DELETE FROM app_settings WHERE key LIKE 'perm_%'").run();
t.clear();
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
});
afterAll(async () => {
@@ -172,7 +172,7 @@ describe('corrupt stored levels', () => {
it('PERM-SVC-013: an unrecognized stored level is ignored — every reader falls back to the default', async () => {
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'unknown_level');
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_delete', '');
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
// Since the quirk fix the corrupt rows never enter the cache, so
// getPermissionLevel, getAllPermissions and checkPermission agree on the
// default instead of the old display-default/deny-in-check split.
@@ -186,7 +186,7 @@ describe('corrupt stored levels', () => {
it('PERM-SVC-021: a stored level outside the action\'s allowedLevels is ignored too', async () => {
// trip_edit only allows trip_owner/trip_member — a raw 'everybody' row must not widen it.
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'everybody');
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
expect(await svc.checkPermission('trip_edit', 'user', 10, 30, false)).toBe(false);
});
@@ -206,32 +206,32 @@ describe('load failures', () => {
const spy = vi
.spyOn(appSettings, 'findByKeyPrefix')
.mockRejectedValueOnce(new Error('no such table: app_settings'));
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
expect(logError).toHaveBeenCalledWith('Permissions load failed: no such table: app_settings');
spy.mockRestore();
svc.invalidatePermissionsCache(); // don't leak the failed-read (uncached) state
await svc.invalidatePermissionsCache(); // don't leak the failed-read (uncached) state
});
it('PERM-SVC-024: a failed read serves defaults without installing them, and a later read populates the cache', async () => {
const spy = vi
.spyOn(appSettings, 'findByKeyPrefix')
.mockRejectedValueOnce(new Error('database connection is closed'));
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
expect(logError).toHaveBeenCalledWith('Permissions load failed: database connection is closed');
// Nothing installed: an admin's stricter stored level would otherwise stay
// invisible until somebody invalidated by hand.
expect(getPermissionsCache()).toBe(null);
expect(await getPermissionsCache()).toBe(null);
// The mock is exhausted after the one queued rejection — the next call
// falls through to the real (now-working) repository read.
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_member');
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
expect(getPermissionsCache()).not.toBe(null);
expect(await getPermissionsCache()).not.toBe(null);
spy.mockRestore();
svc.invalidatePermissionsCache(); // don't leak this test's cache to later tests
await svc.invalidatePermissionsCache(); // don't leak this test's cache to later tests
});
it('PERM-SVC-025: a MikroORM ValidationError (context misuse) is a programming error and propagates, uncached — it does not get the "log and serve defaults" treatment', async () => {
@@ -244,14 +244,14 @@ describe('load failures', () => {
const spy = vi
.spyOn(appSettings, 'findByKeyPrefix')
.mockRejectedValueOnce(ValidationError.cannotUseGlobalContext());
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
await expect(svc.getPermissionLevel('trip_edit')).rejects.toThrow(ValidationError);
expect(logError).not.toHaveBeenCalled();
expect(getPermissionsCache()).toBe(null);
expect(await getPermissionsCache()).toBe(null);
spy.mockRestore();
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
});
it('PERM-SVC-023: an all-skipped save writes nothing and leaves the cache untouched', async () => {
@@ -262,7 +262,7 @@ describe('load failures', () => {
// No valid entries → no transaction and no cache flush: the raw row above
// stays invisible until an explicit invalidation.
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
});
});
@@ -272,7 +272,7 @@ describe('load failures', () => {
describe('stored overrides + cache', () => {
it('PERM-SVC-014: stored perm_ row overrides the default after invalidation', async () => {
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_member');
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
// A plain member now passes what defaults to a trip_owner-only action.
expect(await svc.checkPermission('trip_edit', 'user', 10, 20, true)).toBe(true);
@@ -294,7 +294,7 @@ describe('stored overrides + cache', () => {
// Raw SQL write bypasses savePermissions' self-invalidation → stale value served.
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_member');
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
svc.invalidatePermissionsCache();
await svc.invalidatePermissionsCache();
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member');
});
});
@@ -327,7 +327,7 @@ describe('module-scoped permissions cache', () => {
// must serve the fresh value afterwards.
testDb.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run('perm_trip_edit', 'trip_owner');
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_member'); // still cached
invalidateSharedCache();
await invalidateSharedCache();
expect(await svc.getPermissionLevel('trip_edit')).toBe('trip_owner');
expect(await secondInstance.checkPermission('trip_edit', 'user', 10, 20, true)).toBe(false);
});
@@ -40,7 +40,7 @@ describe('PlaceEnrichmentController', () => {
it('ENRICH-042: carries the 429 status and message', async () => {
const rl = new RateLimitService();
const { controller } = make({}, rl);
vi.spyOn(rl, 'check').mockReturnValue(false);
vi.spyOn(rl, 'check').mockResolvedValue(false);
const err = await controller.enrich(USER, REQ, BODY).catch((e: HttpException) => e);
@@ -4,32 +4,53 @@
* cache and the WebSocket rooms. Each has an in-memory implementation that
* behaves exactly as the state did before, is provided by its module, and is
* what a hand-built consumer falls back to; a shared store can replace it in
* the module. PORTS-001 through PORTS-014.
* the module. The ports a shared store could back are async. The process-wide
* ones are also read outside the container (the broadcasts, the backup
* restore's cache flush, the pending-code sweep), so their module installs the
* store the container resolved in a slot those readers use: swapping the
* provider changes every reader. PORTS-001 through PORTS-020.
*/
import { describe, it, expect, vi } from 'vitest';
import { Test } from '@nestjs/testing';
import { RateLimitModule } from '../../../src/nest/common/rate-limit.module';
import { RateLimitService } from '../../../src/nest/common/rate-limit.service';
import { InMemoryRateLimitStore, RateLimitStore } from '../../../src/nest/common/rate-limit.store';
import { ProcessStoreSlot } from '../../../src/nest/common/process-store-slot';
import {
InMemoryPendingCodeStore,
MAX_PENDING_CODES,
PendingCodeStore,
pendingCodesSlot,
processPendingCodes,
runPendingCodeSweep,
sweepPendingCodes,
type PendingCode,
} from '../../../src/nest/oauth/oauth.pending-codes';
import { OauthModule } from '../../../src/nest/oauth/oauth.module';
import { OauthService } from '../../../src/nest/oauth/oauth.service';
import { InMemoryOidcFlowStore, OidcFlowStore } from '../../../src/nest/oidc/oidc-flow.store';
import { OidcModule } from '../../../src/nest/oidc/oidc.module';
import {
InMemoryPermissionsCacheStore,
PermissionsCacheStore,
getPermissionsCache,
invalidatePermissionsCache,
permissionsCacheSlot,
processPermissionsCache,
} from '../../../src/nest/permissions/permissions-cache';
import { PermissionsModule } from '../../../src/nest/permissions/permissions.module';
import { PermissionsService } from '../../../src/nest/permissions/permissions.service';
import { InMemoryRoomRegistry, RoomRegistry, processRooms, type TrekWebSocket } from '../../../src/nest/realtime/ws-state';
import {
InMemoryRoomRegistry,
RoomRegistry,
bookPeers,
broadcast,
broadcastToBook,
joinRoom,
processRooms,
roomsSlot,
type TrekWebSocket,
} from '../../../src/nest/realtime/ws-state';
import { RealtimeGatewayModule } from '../../../src/nest/realtime/realtime-gateway.module';
import { expectRegisteredProvider } from '../../helpers/module-providers';
@@ -50,12 +71,12 @@ function code(over: Partial<PendingCode> = {}): PendingCode {
}
describe('rate limits', () => {
it('PORTS-001: RateLimitService counts through the store it is given', () => {
it('PORTS-001: RateLimitService counts through the store it is given', async () => {
const store = { hit: vi.fn(() => false), reset: vi.fn() } as unknown as RateLimitStore;
const service = new RateLimitService(store);
expect(service.check('login', '1.2.3.4', 5, WINDOW, 1_000)).toBe(false);
expect(await service.check('login', '1.2.3.4', 5, WINDOW, 1_000)).toBe(false);
expect(store.hit).toHaveBeenCalledWith('login', '1.2.3.4', 5, WINDOW, 1_000);
service.reset('login');
await service.reset('login');
expect(store.reset).toHaveBeenCalledWith('login');
});
@@ -64,44 +85,44 @@ describe('rate limits', () => {
try {
const service = moduleRef.get(RateLimitService);
expect((service as unknown as { store: unknown }).store).toBeInstanceOf(InMemoryRateLimitStore);
for (let i = 0; i < 3; i++) expect(service.check('mfa', 'k', 3, WINDOW, 1_000)).toBe(true);
expect(service.check('mfa', 'k', 3, WINDOW, 1_000)).toBe(false);
for (let i = 0; i < 3; i++) expect(await service.check('mfa', 'k', 3, WINDOW, 1_000)).toBe(true);
expect(await service.check('mfa', 'k', 3, WINDOW, 1_000)).toBe(false);
} finally {
await moduleRef.close();
}
});
it('PORTS-003: two hand-built services keep their own counters, as before', () => {
it('PORTS-003: two hand-built services keep their own counters, as before', async () => {
const a = new RateLimitService();
const b = new RateLimitService();
a.check('login', 'k', 1, WINDOW, 1_000);
expect(a.check('login', 'k', 1, WINDOW, 1_000)).toBe(false);
expect(b.check('login', 'k', 1, WINDOW, 1_000)).toBe(true);
await a.check('login', 'k', 1, WINDOW, 1_000);
expect(await a.check('login', 'k', 1, WINDOW, 1_000)).toBe(false);
expect(await b.check('login', 'k', 1, WINDOW, 1_000)).toBe(true);
});
});
describe('OAuth pending codes', () => {
it('PORTS-004: a code is single use, and an expired one is burnt and refused', () => {
it('PORTS-004: a code is single use, and an expired one is burnt and refused', async () => {
const store = new InMemoryPendingCodeStore();
expect(store.put('a', code())).toBe(true);
expect(store.take('a')?.clientId).toBe('c');
expect(store.take('a')).toBeNull();
store.put('old', code({ expiresAt: Date.now() - 1 }));
expect(store.take('old')).toBeNull();
expect(await store.put('a', code())).toBe(true);
expect((await store.take('a'))?.clientId).toBe('c');
expect(await store.take('a')).toBeNull();
await store.put('old', code({ expiresAt: Date.now() - 1 }));
expect(await store.take('old')).toBeNull();
});
it('PORTS-005: the store refuses past its capacity and the sweep frees expired entries', () => {
it('PORTS-005: the store refuses past its capacity and the sweep frees expired entries', async () => {
const store = new InMemoryPendingCodeStore();
for (let i = 0; i < MAX_PENDING_CODES; i++) store.put(`c${i}`, code({ expiresAt: 10 }));
expect(store.put('one-more', code())).toBe(false);
store.sweep(11);
expect(store.put('one-more', code())).toBe(true);
for (let i = 0; i < MAX_PENDING_CODES; i++) await store.put(`c${i}`, code({ expiresAt: 10 }));
expect(await store.put('one-more', code())).toBe(false);
await store.sweep(11);
expect(await store.put('one-more', code())).toBe(true);
});
it('PORTS-006: OauthService issues and redeems codes through the injected store', () => {
it('PORTS-006: OauthService issues and redeems codes through the injected store', async () => {
const store = new InMemoryPendingCodeStore();
const service = new OauthService({} as never, {} as never, {} as never, {} as never, {} as never, store);
const issued = service.createAuthCode({
const issued = await service.createAuthCode({
clientId: 'c',
userId: 1,
redirectUri: 'https://app.example/cb',
@@ -111,41 +132,70 @@ describe('OAuth pending codes', () => {
codeChallengeMethod: 'S256',
});
expect(issued).toMatch(/^[0-9a-f]{64}$/);
expect(processPendingCodes.take(issued!)).toBeNull();
expect(service.consumeAuthCode(issued!)?.clientId).toBe('c');
expect(await processPendingCodes.take(issued!)).toBeNull();
expect((await service.consumeAuthCode(issued!))?.clientId).toBe('c');
});
it('PORTS-007: OauthModule provides the process-wide store, the one a hand-built service defaults to', async () => {
const { OauthModule } = await import('../../../src/nest/oauth/oauth.module');
it('PORTS-007: OauthModule provides the process-wide store, the one a hand-built service defaults to', () => {
expectRegisteredProvider(OauthModule, { provide: PendingCodeStore, useValue: processPendingCodes });
const service = new OauthService({} as never, {} as never, {} as never, {} as never, {} as never);
expect((service as unknown as { pendingCodes: unknown }).pendingCodes).toBe(processPendingCodes);
});
it('PORTS-018: a swapped store reaches the sweep and a hand-built service until the module goes away', async () => {
const fake = new InMemoryPendingCodeStore();
const sweep = vi.spyOn(fake, 'sweep');
const oauthModule = new OauthModule({} as never, {} as never, {} as never, fake);
try {
expect(pendingCodesSlot.get()).toBe(fake);
await sweepPendingCodes(5);
expect(sweep).toHaveBeenCalledWith(5);
const service = new OauthService({} as never, {} as never, {} as never, {} as never, {} as never);
expect((service as unknown as { pendingCodes: unknown }).pendingCodes).toBe(fake);
} finally {
oauthModule.onModuleDestroy();
}
expect(pendingCodesSlot.get()).toBe(processPendingCodes);
});
it('PORTS-020: the interval body logs a failed sweep instead of throwing it into the timer', async () => {
const fake = new InMemoryPendingCodeStore();
vi.spyOn(fake, 'sweep').mockRejectedValue(new Error('store offline'));
const error = vi.spyOn(console, 'error').mockImplementation(() => {});
pendingCodesSlot.install(fake);
try {
await expect(runPendingCodeSweep(5)).resolves.toBeUndefined();
expect(error).toHaveBeenCalledWith(expect.stringContaining('OAuth pending-code sweep failed: store offline'));
} finally {
pendingCodesSlot.release(fake);
error.mockRestore();
}
});
});
describe('OIDC login states and codes', () => {
it('PORTS-008: states and codes are single use', () => {
it('PORTS-008: states and codes are single use', async () => {
const store = new InMemoryOidcFlowStore();
store.putState('s', { createdAt: 1, redirectUri: '/', codeVerifier: 'v' });
expect(store.takeState('s')?.codeVerifier).toBe('v');
expect(store.takeState('s')).toBeNull();
store.putCode('c', { token: 't', created: 1, bindingHash: 'h' });
expect(store.takeCode('c')?.token).toBe('t');
expect(store.takeCode('c')).toBeNull();
await store.putState('s', { createdAt: 1, redirectUri: '/', codeVerifier: 'v' });
expect((await store.takeState('s'))?.codeVerifier).toBe('v');
expect(await store.takeState('s')).toBeNull();
await store.putCode('c', { token: 't', created: 1, bindingHash: 'h' });
expect((await store.takeCode('c'))?.token).toBe('t');
expect(await store.takeCode('c')).toBeNull();
});
it('PORTS-009: the sweeps drop only what is past its TTL', () => {
it('PORTS-009: the sweeps drop only what is past its TTL', async () => {
const store = new InMemoryOidcFlowStore();
store.putState('old', { createdAt: 0, redirectUri: '/', codeVerifier: 'v' });
store.putState('new', { createdAt: 900, redirectUri: '/', codeVerifier: 'v' });
store.putCode('old', { token: 't', created: 0, bindingHash: 'h' });
store.putCode('new', { token: 't', created: 900, bindingHash: 'h' });
store.sweepStates(1_000, 500);
store.sweepCodes(1_000, 500);
expect(store.takeState('old')).toBeNull();
expect(store.takeState('new')).not.toBeNull();
expect(store.takeCode('old')).toBeNull();
expect(store.takeCode('new')).not.toBeNull();
await store.putState('old', { createdAt: 0, redirectUri: '/', codeVerifier: 'v' });
await store.putState('new', { createdAt: 900, redirectUri: '/', codeVerifier: 'v' });
await store.putCode('old', { token: 't', created: 0, bindingHash: 'h' });
await store.putCode('new', { token: 't', created: 900, bindingHash: 'h' });
await store.sweepStates(1_000, 500);
await store.sweepCodes(1_000, 500);
expect(await store.takeState('old')).toBeNull();
expect(await store.takeState('new')).not.toBeNull();
expect(await store.takeCode('old')).toBeNull();
expect(await store.takeCode('new')).not.toBeNull();
});
it('PORTS-010: OidcModule provides the in-memory store', () => {
@@ -159,20 +209,43 @@ describe('the permissions cache', () => {
const appSettings = { findByKeyPrefix: vi.fn(async () => [{ key: 'perm_trip_create', value: 'admin' }]) };
const service = new PermissionsService(appSettings as never, {} as never, store);
expect(await service.getPermissionLevel('trip_create')).toBe('admin');
expect(store.get()?.get('trip_create')).toBe('admin');
expect((await store.get())?.get('trip_create')).toBe('admin');
await service.getPermissionLevel('trip_create');
expect(appSettings.findByKeyPrefix).toHaveBeenCalledTimes(1);
service.invalidatePermissionsCache();
expect(store.get()).toBeNull();
await service.invalidatePermissionsCache();
expect(await store.get()).toBeNull();
});
it('PORTS-012: the module, a hand-built service and the restore path share the process-wide store', () => {
it('PORTS-012: the module, a hand-built service and the restore path share the process-wide store', async () => {
expectRegisteredProvider(PermissionsModule, { provide: PermissionsCacheStore, useValue: processPermissionsCache });
const service = new PermissionsService({} as never, {} as never);
expect((service as unknown as { cacheStore: unknown }).cacheStore).toBe(processPermissionsCache);
processPermissionsCache.set(new Map([['trip_create', 'admin']]));
invalidatePermissionsCache();
expect(processPermissionsCache.get()).toBeNull();
await processPermissionsCache.set(new Map([['trip_create', 'admin']]));
await invalidatePermissionsCache();
expect(await processPermissionsCache.get()).toBeNull();
});
it('PORTS-016: a swapped store is the one the backup restore flushes, until the module goes away', async () => {
const fake = new InMemoryPermissionsCacheStore();
await fake.set(new Map([['trip_create', 'admin']]));
const invalidate = vi.spyOn(fake, 'invalidate');
await processPermissionsCache.set(new Map([['trip_edit', 'trip_member']]));
const permissionsModule = new PermissionsModule(fake);
try {
expect(permissionsCacheSlot.get()).toBe(fake);
// backup.impl.ts flushes through this free function after a restore.
await invalidatePermissionsCache();
expect(invalidate).toHaveBeenCalledTimes(1);
expect(await getPermissionsCache()).toBeNull();
// The default store was not the one flushed.
expect(await processPermissionsCache.get()).not.toBeNull();
const service = new PermissionsService({} as never, {} as never);
expect((service as unknown as { cacheStore: unknown }).cacheStore).toBe(fake);
} finally {
permissionsModule.onModuleDestroy();
await processPermissionsCache.invalidate();
}
expect(permissionsCacheSlot.get()).toBe(processPermissionsCache);
});
});
@@ -200,5 +273,64 @@ describe('the WebSocket rooms', () => {
it('PORTS-014: RealtimeGatewayModule provides the process-wide registry the broadcasts read', () => {
expectRegisteredProvider(RealtimeGatewayModule, { provide: RoomRegistry, useValue: processRooms });
expect(roomsSlot.get()).toBe(processRooms);
});
it('PORTS-017: a swapped registry is the one the broadcasts join and deliver through, until the module goes away', () => {
const fake = new InMemoryRoomRegistry();
const members = vi.spyOn(fake, 'members');
const bookMembers = vi.spyOn(fake, 'bookMembers');
const send = vi.fn();
const live = { readyState: 1, send } as unknown as TrekWebSocket;
const gatewayModule = new RealtimeGatewayModule(fake);
try {
expect(roomsSlot.get()).toBe(fake);
fake.register(live);
joinRoom(live, 41);
fake.joinBook(live, 9);
broadcast(41, 'place:created', { place: { id: 1 } });
expect(members).toHaveBeenCalledWith(41);
expect(send).toHaveBeenCalledWith(JSON.stringify({ type: 'place:created', tripId: 41, place: { id: 1 } }));
broadcastToBook(9, { type: 'book:pointer' });
expect(bookMembers).toHaveBeenCalledWith(9);
expect(send).toHaveBeenCalledWith(JSON.stringify({ journeyId: 9, type: 'book:pointer' }));
// The socket was never registered with a user, so it is not listed as a peer.
expect(bookPeers(9)).toEqual([]);
// Nothing reached the default registry.
expect(processRooms.members(41)).toBeUndefined();
} finally {
gatewayModule.onModuleDestroy();
}
expect(roomsSlot.get()).toBe(processRooms);
});
});
describe('ProcessStoreSlot', () => {
it('PORTS-015: install swaps the store, and release only restores the default for the store that is installed', () => {
const fallback = { name: 'fallback' };
const first = { name: 'first' };
const second = { name: 'second' };
const slot = new ProcessStoreSlot(fallback);
expect(slot.get()).toBe(fallback);
slot.install(first);
slot.install(second);
// A module torn down after a later one installed its own leaves that one in place.
slot.release(first);
expect(slot.get()).toBe(second);
slot.release(second);
expect(slot.get()).toBe(fallback);
});
});
describe('the rate-limit store', () => {
it('PORTS-019: the in-memory store answers through promises with the old counting', async () => {
const store = new InMemoryRateLimitStore();
await expect(store.hit('login', 'k', 1, WINDOW, 1_000)).resolves.toBe(true);
await expect(store.hit('login', 'k', 1, WINDOW, 1_000)).resolves.toBe(false);
await store.reset('login');
await expect(store.hit('login', 'k', 1, WINDOW, 1_000)).resolves.toBe(true);
});
});
@@ -134,7 +134,7 @@ async function thrownAsync(fn: () => Promise<unknown>): Promise<{ status: number
}
function apiController(svc: Partial<PublicApiService>) {
const rl = { check: vi.fn().mockReturnValue(true) } as unknown as RateLimitService;
const rl = { check: vi.fn().mockResolvedValue(true) } as unknown as RateLimitService;
return new PublicApiController(svc as PublicApiService, rl);
}
@@ -36,7 +36,7 @@ const ITEM = {
/** The limiter always allows unless a test says otherwise. */
function makeController(svc: Partial<PublicApiService>, allow = true) {
const rl = { check: vi.fn().mockReturnValue(allow) } as unknown as RateLimitService;
const rl = { check: vi.fn().mockResolvedValue(allow) } as unknown as RateLimitService;
return new PublicApiController(svc as PublicApiService, rl);
}
@@ -17,61 +17,61 @@ function bucketSize(service: RateLimitService, bucket: string): number {
}
describe('RateLimitService', () => {
it('allows up to max attempts in a window, then refuses', () => {
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(s.check('login', '1.2.3.4', 5, WINDOW, now)).toBe(true);
expect(s.check('login', '1.2.3.4', 5, WINDOW, now)).toBe(false);
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', () => {
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++) s.check('login', '1.2.3.4', 5, WINDOW, now);
expect(s.check('login', '1.2.3.4', 5, WINDOW, now + WINDOW)).toBe(true);
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', () => {
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++) s.check('login', `10.0.0.${i}`, 5, WINDOW, 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.
s.check('login', '10.0.0.0', 5, WINDOW, now + WINDOW);
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', () => {
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();
s.check('login', '1.2.3.4', 5, WINDOW, now);
for (let i = 0; i < 3; i++) s.check('mfa', `10.0.0.${i}`, 5, WINDOW, now + WINDOW * (i + 1));
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);
s.check('login', '5.6.7.8', 5, WINDOW, now + WINDOW);
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', () => {
it('keeps a record that is still inside its window when the sweep runs', async () => {
const s = new RateLimitService();
const now = Date.now();
s.check('login', '1.2.3.4', 5, WINDOW, now);
s.check('login', '5.6.7.8', 5, WINDOW, now + WINDOW);
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(s.check('login', '1.2.3.4', 5, WINDOW, now + WINDOW)).toBe(true);
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', () => {
it('reset clears a single bucket, or all of them', async () => {
const s = new RateLimitService();
const now = Date.now();
s.check('login', '1.2.3.4', 5, WINDOW, now);
s.check('mfa', '1.2.3.4', 5, WINDOW, now);
s.reset('login');
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);
s.reset();
await s.reset();
expect(bucketSize(s, 'mfa')).toBe(0);
});
});
@@ -206,7 +206,7 @@ describe('PluginGuards — canEditAs/canCreateAs against a real UsersRepository
// The permissions cache is module-scoped (permissions-cache.ts), not
// per-instance — invalidate before every case so a stored override from
// another describe block in this same worker can't leak in.
permissions.invalidatePermissionsCache();
await permissions.invalidatePermissionsCache();
guards = new PluginGuards(db, permissions, {} as AddonsService, orm.repo(Users));
createUser(testDb, { role: 'user' }); // id 1, the trip owner — unused by these cases directly
memberId = createUser(testDb, { role: 'user' }).user.id; // id 2, a non-owner trip member
@@ -120,7 +120,7 @@ function makeSupervisor(resolveOrm?: () => { em: EntityManager } | undefined) {
describe('PluginSupervisor request context (D6, C3)', () => {
it('CTX-PLUGIN-001: without a resolveOrm thunk, the dispatch THROWS (host-side visibility) but the child still gets answered first (task-6-rereview.md §5 RULING: an unanswered \'req\' hangs the plugin forever — plugin-host-entry.ts\'s pending map has no timeout)', async () => {
permissions.invalidatePermissionsCache();
await permissions.invalidatePermissionsCache();
const { supervisor, sup, sent } = makeSupervisor(); // no resolveOrm — the bug's exact reproduction
const canCreateAsSpy = vi.spyOn(guards, 'canCreateAs');
try {
@@ -147,7 +147,7 @@ describe('PluginSupervisor request context (D6, C3)', () => {
});
it('CTX-PLUGIN-002: wrapped in withRequestContext, the same dispatch enforces the stored override — a "user" role is refused', async () => {
permissions.invalidatePermissionsCache();
await permissions.invalidatePermissionsCache();
const { supervisor, sup, sent } = makeSupervisor(() => t.orm);
await supervisor.onMessage(sup, { k: 'req', id: 'r2', method: 'trips.create', params: { _inv: 'inv-1' } });
+2 -2
View File
@@ -75,9 +75,9 @@ afterAll(async () => {
testDb.close();
});
beforeEach(() => {
beforeEach(async () => {
resetTestDb(testDb);
resetRateLimits(nestApp);
await resetRateLimits(nestApp);
});
/** Buffered WebSocket wrapper that never drops messages. */