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

+91
View File
@@ -0,0 +1,91 @@
import { WORKSHOP_INQUIRY_RETENTION_MS } from "../privacyRetention";
export interface PruneStore {
session: {
deleteMany: (args: {
where: { expiresAt: { lt: Date }; userId?: string };
}) => Promise<{ count: number }>;
};
magicLinkToken: {
deleteMany: (args: {
where: { expiresAt: { lt: Date } };
}) => Promise<{ count: number }>;
};
emailChangeToken: {
deleteMany: (args: {
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 } };
}) => Promise<{ count: number }>;
};
}
export interface PruneExpiredResult {
sessions: number;
magicLinkTokens: number;
emailChangeTokens: number;
unusedInviteTokens: number;
workshopInquiries: number;
}
export async function deleteExpiredWorkshopInquiries(
db: Pick<PruneStore, "workshopInquiry">,
now = new Date(),
): Promise<number> {
const cutoff = new Date(now.getTime() - WORKSHOP_INQUIRY_RETENTION_MS);
const result = await db.workshopInquiry.deleteMany({
where: { createdAt: { lt: cutoff } },
});
return result.count;
}
/**
* 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.
*/
export async function pruneExpiredRecords(
db: PruneStore,
now = new Date(),
): Promise<PruneExpiredResult> {
const sessions = await db.session.deleteMany({
where: { expiresAt: { lt: now } },
});
const magicLinkTokens = await db.magicLinkToken.deleteMany({
where: { expiresAt: { lt: now } },
});
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,
};
}