Delete unused login and invite tokens on a daily job, and keep their URLs out of logs.

Expired sign-in, save-progress, email-change, and invitation tokens (and any draft they carry) are cleared by a Cloudron scheduler task. Page timing logs record the path without the query string, and mail failures log a request id instead of an address.
This commit is contained in:
adilallo committed 2026-11-10 15:34:33 -07:00
1 parent 73d0856004
commit 46c34a6323
28 files changed
+636 -45

No files matched your search

+103
View File
@@ -0,0 +1,103 @@
import { describe, expect, it, vi } from "vitest";
import { WORKSHOP_INQUIRY_RETENTION_MS } from "../../lib/privacyRetention";
import { pruneExpiredRecords } from "../../lib/server/pruneExpiredRecords";
function store(counts: {
sessions?: number;
magicLinkTokens?: number;
emailChangeTokens?: number;
unusedInviteTokens?: number;
workshopInquiries?: number;
}) {
const sessionDeleteMany = vi.fn().mockResolvedValue({
count: counts.sessions ?? 0,
});
const magicLinkDeleteMany = vi.fn().mockResolvedValue({
count: counts.magicLinkTokens ?? 0,
});
const emailChangeDeleteMany = vi.fn().mockResolvedValue({
count: counts.emailChangeTokens ?? 0,
});
const inviteUpdateMany = vi.fn().mockResolvedValue({
count: counts.unusedInviteTokens ?? 0,
});
const workshopDeleteMany = vi.fn().mockResolvedValue({
count: counts.workshopInquiries ?? 0,
});
return {
db: {
session: { deleteMany: sessionDeleteMany },
magicLinkToken: { deleteMany: magicLinkDeleteMany },
emailChangeToken: { deleteMany: emailChangeDeleteMany },
ruleStakeholder: { updateMany: inviteUpdateMany },
workshopInquiry: { deleteMany: workshopDeleteMany },
},
sessionDeleteMany,
magicLinkDeleteMany,
emailChangeDeleteMany,
inviteUpdateMany,
workshopDeleteMany,
};
}
describe("pruneExpiredRecords", () => {
it("deletes expired tokens and sessions, and clears unused 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,
});
const result = await pruneExpiredRecords(mocks.db, now);
expect(result).toEqual({
sessions: 2,
magicLinkTokens: 3,
emailChangeTokens: 1,
unusedInviteTokens: 4,
workshopInquiries: 5,
});
expect(mocks.sessionDeleteMany).toHaveBeenCalledWith({
where: { expiresAt: { lt: now } },
});
expect(mocks.magicLinkDeleteMany).toHaveBeenCalledWith({
where: { expiresAt: { lt: now } },
});
expect(mocks.emailChangeDeleteMany).toHaveBeenCalledWith({
where: { expiresAt: { lt: now } },
});
expect(mocks.inviteUpdateMany).toHaveBeenCalledWith({
where: {
inviteTokenHash: { not: null },
inviteExpiresAt: { lt: now },
},
data: {
inviteTokenHash: null,
inviteExpiresAt: null,
},
});
const workshopArg = mocks.workshopDeleteMany.mock.calls[0]?.[0] as {
where: { createdAt: { lt: Date } };
};
expect(
now.getTime() - workshopArg.where.createdAt.lt.getTime(),
).toBe(WORKSHOP_INQUIRY_RETENTION_MS);
});
it("does not look up tokens by hash or email", async () => {
const mocks = store({});
await pruneExpiredRecords(mocks.db, new Date());
const payload = JSON.stringify([
mocks.sessionDeleteMany.mock.calls,
mocks.magicLinkDeleteMany.mock.calls,
mocks.emailChangeDeleteMany.mock.calls,
mocks.inviteUpdateMany.mock.calls,
]);
expect(payload).not.toMatch(/tokenHash|email/);
});
});