From a8b23bddc25d6036a7ebcb3a860a0b515374da16 Mon Sep 17 00:00:00 2001 From: adilallo <39313955+adilallo@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:43:30 -0700 Subject: [PATCH] Keep expired invitation hashes so a request for a new link still sends mail. Clearing the hash made the expired page report success without sending, and made the invite look accepted. --- docs/guides/ops-runbook.md | 5 +++-- docs/privacy-policy-facts.md | 2 +- lib/server/pruneExpiredRecords.ts | 29 ++++++-------------------- scripts/prune-sessions.ts | 4 ++-- tests/unit/opsPruneRoute.test.ts | 2 -- tests/unit/pruneExpiredRecords.test.ts | 21 +++---------------- 6 files changed, 15 insertions(+), 48 deletions(-) diff --git a/docs/guides/ops-runbook.md b/docs/guides/ops-runbook.md index c909aac..c3d8879 100644 --- a/docs/guides/ops-runbook.md +++ b/docs/guides/ops-runbook.md @@ -294,8 +294,9 @@ Periods the privacy policy states. Source of truth: `node /app/prune-expired.bundle.cjs` (the production bundle of [`scripts/prune-sessions.ts`](../../scripts/prune-sessions.ts)) and deletes expired sessions, unused login / save-progress / email-change -tokens and the drafts they carry, unused invitation tokens, and workshop -inquiries older than 12 months. +tokens and the drafts they carry, and workshop inquiries older than 12 +months. It leaves invitation token hashes in place: an expired link cannot +be used to join, and that hash is how the person requests a new link. Manual runs: diff --git a/docs/privacy-policy-facts.md b/docs/privacy-policy-facts.md index e470cca..680d5cd 100644 --- a/docs/privacy-policy-facts.md +++ b/docs/privacy-policy-facts.md @@ -10,7 +10,7 @@ read this text. Do not switch the route on from this file. | --- | --- | --- | | Login and save-progress links | Work for **60 minutes**. Unused rows, and any draft they carry, are deleted once expired. A daily job runs the delete, so they are gone **within 24 hours** of expiry. | `MAGIC_LINK_TTL_MS`, Cloudron scheduler `prune-expired` | | Email-change links | Same as login links: **60 minutes**, then gone within **24 hours**. | `EMAIL_CHANGE_TTL_MS` | -| Unused invitation tokens | The unused token is cleared once the invite expires. Gone within **24 hours** of expiry. The invitation row stays so it can be sent again. | Daily prune | +| Unused invitation tokens | The link stops granting access when it expires (**60 minutes**). The hash stays on the invitation so the person can request a new link from the expired page, and so the organizer can send it again. The daily job does not delete it. | `STAKEHOLDER_INVITE_TTL_MS` | | Workshop inquiries that do not lead to a workshop | **12 months**, then deleted. | `WORKSHOP_INQUIRY_RETENTION_MS` | | Ask an organizer messages | **Not stored** on CommunityRule. They are emailed to MEDLab. MEDLab deletes mailbox copies after **12 months**. | Mail only | | Unused accounts | **Kept until the person deletes the account.** There is no unused-account timer at launch. | — | diff --git a/lib/server/pruneExpiredRecords.ts b/lib/server/pruneExpiredRecords.ts index 95504e0..e6f0e03 100644 --- a/lib/server/pruneExpiredRecords.ts +++ b/lib/server/pruneExpiredRecords.ts @@ -16,15 +16,6 @@ export interface PruneStore { where: { expiresAt: { lt: Date } }; }) => Promise<{ count: number }>; }; - ruleStakeholder: { - updateMany: (args: { - where: { - inviteTokenHash: { not: null }; - inviteExpiresAt: { lt: Date }; - }; - data: { inviteTokenHash: null; inviteExpiresAt: null }; - }) => Promise<{ count: number }>; - }; workshopInquiry: { deleteMany: (args: { where: { createdAt: { lt: Date } }; @@ -36,7 +27,6 @@ export interface PruneExpiredResult { sessions: number; magicLinkTokens: number; emailChangeTokens: number; - unusedInviteTokens: number; workshopInquiries: number; } @@ -53,8 +43,12 @@ export async function deleteExpiredWorkshopInquiries( /** * Delete expired sessions, login/save-progress tokens (and the draft they - * carry), email-change tokens, unused invitation tokens, and old workshop - * inquiries. Does not delete stakeholder rows or accounts. + * carry), email-change tokens, and old workshop inquiries. + * + * Does not delete stakeholder rows, accounts, or invitation token hashes. + * Verify already rejects an invite once `inviteExpiresAt` has passed. The + * hash is how `/invite/expired` finds that row to send a new link, and a + * null hash is treated as an accepted invite. */ export async function pruneExpiredRecords( db: PruneStore, @@ -69,23 +63,12 @@ export async function pruneExpiredRecords( const emailChangeTokens = await db.emailChangeToken.deleteMany({ where: { expiresAt: { lt: now } }, }); - const unusedInviteTokens = await db.ruleStakeholder.updateMany({ - where: { - inviteTokenHash: { not: null }, - inviteExpiresAt: { lt: now }, - }, - data: { - inviteTokenHash: null, - inviteExpiresAt: null, - }, - }); const workshopInquiries = await deleteExpiredWorkshopInquiries(db, now); return { sessions: sessions.count, magicLinkTokens: magicLinkTokens.count, emailChangeTokens: emailChangeTokens.count, - unusedInviteTokens: unusedInviteTokens.count, workshopInquiries, }; } diff --git a/scripts/prune-sessions.ts b/scripts/prune-sessions.ts index 6b02bff..66b02f8 100644 --- a/scripts/prune-sessions.ts +++ b/scripts/prune-sessions.ts @@ -1,7 +1,7 @@ /** * Prune expired sessions, unused login/save-progress/email-change tokens - * (and any draft they carry), unused invitation tokens, and old workshop - * inquiries. + * (and any draft they carry), and old workshop inquiries. Invitation token + * hashes are left in place so an expired link can still request a new one. * * Usage (from repo root): * node --env-file=.env --import tsx scripts/prune-sessions.ts # global sweep diff --git a/tests/unit/opsPruneRoute.test.ts b/tests/unit/opsPruneRoute.test.ts index b679234..0734961 100644 --- a/tests/unit/opsPruneRoute.test.ts +++ b/tests/unit/opsPruneRoute.test.ts @@ -44,7 +44,6 @@ beforeEach(() => { sessions: 1, magicLinkTokens: 2, emailChangeTokens: 0, - unusedInviteTokens: 3, workshopInquiries: 4, }); retryPendingWorkshopInquiryMailMock.mockResolvedValue(1); @@ -79,7 +78,6 @@ describe("POST /api/ops/prune", () => { sessions: 1, magicLinkTokens: 2, emailChangeTokens: 0, - unusedInviteTokens: 3, workshopInquiries: 4, }); expect(pruneExpiredRecordsMock).toHaveBeenCalledWith( diff --git a/tests/unit/pruneExpiredRecords.test.ts b/tests/unit/pruneExpiredRecords.test.ts index 9ddaad6..bfed006 100644 --- a/tests/unit/pruneExpiredRecords.test.ts +++ b/tests/unit/pruneExpiredRecords.test.ts @@ -6,7 +6,6 @@ function store(counts: { sessions?: number; magicLinkTokens?: number; emailChangeTokens?: number; - unusedInviteTokens?: number; workshopInquiries?: number; }) { const sessionDeleteMany = vi.fn().mockResolvedValue({ @@ -18,9 +17,7 @@ function store(counts: { const emailChangeDeleteMany = vi.fn().mockResolvedValue({ count: counts.emailChangeTokens ?? 0, }); - const inviteUpdateMany = vi.fn().mockResolvedValue({ - count: counts.unusedInviteTokens ?? 0, - }); + const inviteUpdateMany = vi.fn().mockResolvedValue({ count: 0 }); const workshopDeleteMany = vi.fn().mockResolvedValue({ count: counts.workshopInquiries ?? 0, }); @@ -41,13 +38,12 @@ function store(counts: { } describe("pruneExpiredRecords", () => { - it("deletes expired tokens and sessions, and clears unused invite hashes", async () => { + it("deletes expired tokens and sessions without clearing invite hashes", async () => { const now = new Date("2026-11-10T12:00:00.000Z"); const mocks = store({ sessions: 2, magicLinkTokens: 3, emailChangeTokens: 1, - unusedInviteTokens: 4, workshopInquiries: 5, }); @@ -57,7 +53,6 @@ describe("pruneExpiredRecords", () => { sessions: 2, magicLinkTokens: 3, emailChangeTokens: 1, - unusedInviteTokens: 4, workshopInquiries: 5, }); @@ -70,16 +65,7 @@ describe("pruneExpiredRecords", () => { expect(mocks.emailChangeDeleteMany).toHaveBeenCalledWith({ where: { expiresAt: { lt: now } }, }); - expect(mocks.inviteUpdateMany).toHaveBeenCalledWith({ - where: { - inviteTokenHash: { not: null }, - inviteExpiresAt: { lt: now }, - }, - data: { - inviteTokenHash: null, - inviteExpiresAt: null, - }, - }); + expect(mocks.inviteUpdateMany).not.toHaveBeenCalled(); const workshopArg = mocks.workshopDeleteMany.mock.calls[0]?.[0] as { where: { createdAt: { lt: Date } }; @@ -96,7 +82,6 @@ describe("pruneExpiredRecords", () => { mocks.sessionDeleteMany.mock.calls, mocks.magicLinkDeleteMany.mock.calls, mocks.emailChangeDeleteMany.mock.calls, - mocks.inviteUpdateMany.mock.calls, ]); expect(payload).not.toMatch(/tokenHash|email/); });