mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
fix(oauth): purge tokens whose refresh token expired a month ago
Every refresh writes a new oauth_tokens row and nothing ever deleted the old ones, so an MCP client refreshing hourly grew the table, and every backup with it, without end. A nightly job now deletes tokens whose refresh token expired more than 30 days ago, leaves first, and keeps any row a live chain still points at for replay detection. Also types the backup's per-entry store flag.
This commit is contained in:
@@ -529,4 +529,34 @@ describe('OauthTokensRepository', () => {
|
||||
expect(row.scopes).toBe('["kept-scope"]'); // untouched by either read
|
||||
});
|
||||
});
|
||||
|
||||
describe('deleteExpiredBefore (retention)', () => {
|
||||
const longAgo = '2026-01-01T00:00:00.000Z';
|
||||
const cutoff = '2026-06-01T00:00:00.000Z';
|
||||
const ids = () => (testDb.prepare('SELECT id FROM oauth_tokens ORDER BY id').all() as { id: number }[]).map((r) => r.id);
|
||||
|
||||
it('OAUTHTOKREPO-040: deletes a whole expired chain, parents after their children', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
seedClient(user.id, 'ret-1');
|
||||
const root = seedToken({ clientId: 'ret-1', userId: user.id, refreshExpiresAt: longAgo, revokedAt: longAgo });
|
||||
const mid = seedToken({ clientId: 'ret-1', userId: user.id, refreshExpiresAt: longAgo, revokedAt: longAgo, parentId: root });
|
||||
seedToken({ clientId: 'ret-1', userId: user.id, refreshExpiresAt: longAgo, parentId: mid });
|
||||
|
||||
expect(await withRequestContext(t.orm, () => tokens.deleteExpiredBefore(cutoff))).toBe(3);
|
||||
expect(ids()).toEqual([]);
|
||||
});
|
||||
|
||||
it('OAUTHTOKREPO-041: keeps an expired parent while a live token still names it', async () => {
|
||||
const { user } = createUser(testDb);
|
||||
seedClient(user.id, 'ret-2');
|
||||
const root = seedToken({ clientId: 'ret-2', userId: user.id, refreshExpiresAt: longAgo, revokedAt: longAgo });
|
||||
const live = seedToken({ clientId: 'ret-2', userId: user.id, parentId: root });
|
||||
const unrelated = seedToken({ clientId: 'ret-2', userId: user.id, refreshExpiresAt: longAgo });
|
||||
|
||||
expect(await withRequestContext(t.orm, () => tokens.deleteExpiredBefore(cutoff))).toBe(1);
|
||||
expect(ids()).toEqual([root, live].sort((a, b) => a - b));
|
||||
expect(ids()).not.toContain(unrelated);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const logMock = vi.hoisted(() => ({ logInfo: vi.fn(), logError: vi.fn(), logWarn: vi.fn(), logDebug: vi.fn() }));
|
||||
vi.mock('../../../src/nest/audit/audit-log.logger', () => logMock);
|
||||
|
||||
import { OauthTokenRetentionJob, OAUTH_TOKEN_RETENTION_DAYS } from '../../../src/nest/oauth/oauth-token-retention.job';
|
||||
import type { OauthTokensRepository } from '../../../src/db/repositories/OauthTokens.repository';
|
||||
import type { CronRegistrarService } from '../../../src/nest/scheduling/cron-registrar.service';
|
||||
|
||||
function make(deleteExpiredBefore: (cutoff: string) => Promise<number>, enabled = true) {
|
||||
const registrar = { isEnabled: vi.fn(() => enabled), register: vi.fn() };
|
||||
const job = new OauthTokenRetentionJob({ deleteExpiredBefore } as unknown as OauthTokensRepository, registrar as unknown as CronRegistrarService);
|
||||
return { job, registrar };
|
||||
}
|
||||
|
||||
describe('OauthTokenRetentionJob', () => {
|
||||
it('OAUTHRET-001: registers a nightly job, and nothing under the test gate', () => {
|
||||
const { job, registrar } = make(vi.fn());
|
||||
job.onApplicationBootstrap();
|
||||
expect(registrar.register).toHaveBeenCalledWith('oauth-token-retention', '30 3 * * *', expect.any(Function));
|
||||
|
||||
const gated = make(vi.fn(), false);
|
||||
gated.job.onApplicationBootstrap();
|
||||
expect(gated.registrar.register).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('OAUTHRET-002: deletes what expired more than the retention period ago and logs the count', async () => {
|
||||
const del = vi.fn().mockResolvedValue(4);
|
||||
await make(del).job.tick(new Date('2026-10-07T00:00:00.000Z'));
|
||||
expect(OAUTH_TOKEN_RETENTION_DAYS).toBe(30);
|
||||
expect(del).toHaveBeenCalledWith('2026-09-07T00:00:00.000Z');
|
||||
expect(logMock.logInfo).toHaveBeenCalledWith('OAuth token retention: removed 4 expired token(s)');
|
||||
});
|
||||
|
||||
it('OAUTHRET-003: a failing purge is logged, not thrown', async () => {
|
||||
await make(vi.fn().mockRejectedValue(new Error('database is locked'))).job.tick();
|
||||
expect(logMock.logError).toHaveBeenCalledWith('OAuth token retention: database is locked');
|
||||
});
|
||||
});
|
||||
@@ -44,6 +44,7 @@ import { ExchangeRatesService } from '../../../src/nest/budget/exchange-rates.se
|
||||
import { RealtimeService } from '../../../src/nest/realtime/realtime.service';
|
||||
import { BudgetService } from '../../../src/nest/budget/budget.service';
|
||||
import type { BudgetItemsRepository } from '../../../src/db/repositories/BudgetItems.repository';
|
||||
import type { BudgetSettlementsRepository } from '../../../src/db/repositories/BudgetSettlements.repository';
|
||||
import type { JourneyShareTokensRepository } from '../../../src/db/repositories/JourneyShareTokens.repository';
|
||||
import type { JourneysRepository } from '../../../src/db/repositories/Journeys.repository';
|
||||
import type { JourneyEntriesRepository } from '../../../src/db/repositories/JourneyEntries.repository';
|
||||
@@ -181,7 +182,7 @@ describe('erasePluginUserData', () => {
|
||||
// call — stubs are enough for those.
|
||||
const slimSvc = new UserCleanupService(
|
||||
(await sharedTestOrm(slim)).em, budget, await createTestUnitOfWork(slim), await createTestUsersRepo(slim),
|
||||
{} as unknown as TripMembersRepository, {} as unknown as BudgetItemsRepository,
|
||||
{} as unknown as TripMembersRepository, {} as unknown as BudgetItemsRepository, {} as unknown as BudgetSettlementsRepository,
|
||||
{} as unknown as JourneyShareTokensRepository, {} as unknown as JourneysRepository,
|
||||
{} as unknown as JourneyEntriesRepository, {} as unknown as JourneyContributorsRepository,
|
||||
// Plan 3h Task 6: a real repository (never a stub cast — `erasePluginUserData`
|
||||
|
||||
Reference in New Issue
Block a user