From 46c34a63236ac7add08828449f7d5482107ea930 Mon Sep 17 00:00:00 2001 From: adilallo <39313955+adilallo@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:34:33 -0700 Subject: [PATCH 1/3] 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. --- .env.example | 3 + CONTRIBUTING.md | 3 +- CloudronManifest.json | 8 +- Dockerfile | 5 + app/api/auth/magic-link/request/route.ts | 4 +- app/api/ops/prune/route.ts | 21 ++++ app/api/rules/route.ts | 1 - app/api/user/email-change/request/route.ts | 7 +- app/api/web-vitals/route.ts | 3 +- docs/guides/backend-roadmap.md | 4 +- docs/guides/ops-backend-deploy.md | 6 +- docs/guides/ops-runbook.md | 66 +++++++++++- lib/privacyRetention.ts | 16 +++ lib/server/env.ts | 6 ++ lib/server/mail.ts | 5 +- lib/server/pathOnlyUrl.ts | 15 +++ lib/server/pruneExpiredRecords.ts | 91 ++++++++++++++++ lib/server/ruleStakeholderInviteOps.ts | 1 - lib/server/session.ts | 6 +- lib/server/workshopSignup.ts | 8 +- lib/workshopSignup.ts | 3 - scripts/prune-sessions.ts | 48 ++++++--- tests/unit/authMagicLinkRequestRoute.test.ts | 26 +++++ tests/unit/env.test.ts | 15 +++ tests/unit/opsPruneRoute.test.ts | 89 ++++++++++++++++ tests/unit/pathOnlyUrl.test.ts | 35 +++++++ tests/unit/pruneExpiredRecords.test.ts | 103 +++++++++++++++++++ tests/unit/webVitalsPostRoute.test.ts | 83 +++++++++++++++ 28 files changed, 636 insertions(+), 45 deletions(-) create mode 100644 app/api/ops/prune/route.ts create mode 100644 lib/privacyRetention.ts create mode 100644 lib/server/pathOnlyUrl.ts create mode 100644 lib/server/pruneExpiredRecords.ts create mode 100644 tests/unit/opsPruneRoute.test.ts create mode 100644 tests/unit/pathOnlyUrl.test.ts create mode 100644 tests/unit/pruneExpiredRecords.test.ts create mode 100644 tests/unit/webVitalsPostRoute.test.ts diff --git a/.env.example b/.env.example index 9351644..a1ed6ab 100644 --- a/.env.example +++ b/.env.example @@ -30,6 +30,9 @@ WORKSHOP_INQUIRY_ADMIN_TOKEN= # Bearer token for POST /api/workshop-inquiries/purge (retry unsent mail, delete inquiries older than 12 months). WORKSHOP_INQUIRY_PURGE_TOKEN= +# Bearer token for POST /api/ops/prune (expired sessions, unused links, old workshop inquiries). Unset hides the route. +OPS_PRUNE_TOKEN= + # Set to `true` to sync the create-flow draft with `/api/drafts/me` when the user is signed in. # Server draft sync (default on). Set to `false` to disable PUT/GET /api/drafts/me. NEXT_PUBLIC_ENABLE_BACKEND_SYNC= diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7a56c98..f52eb00 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -117,7 +117,8 @@ Facet semantics and the recommendation matrix: | --- | --- | --- | | POST | `/api/organizer-inquiry` | "Ask an organizer" form submission. | | POST | `/api/use-cases/[slug]/duplicate` | Duplicate a use-case demo rule. | -| GET, POST | `/api/web-vitals` | Read / ingest web vitals. Storage mode set by `WEB_VITALS_STORAGE` (`local` in dev, `external` in prod). | +| GET, POST | `/api/web-vitals` | Read / ingest web vitals. Storage mode set by `WEB_VITALS_STORAGE` (`local` in dev, `external` in prod). Page timing logs store origin + path, without the query string. | +| POST | `/api/ops/prune` | Expired sessions, unused login/invite/email-change tokens, and old workshop inquiries. Requires `OPS_PRUNE_TOKEN`. Hidden when unset. | ## Testing diff --git a/CloudronManifest.json b/CloudronManifest.json index 1e8ae51..9a855d1 100644 --- a/CloudronManifest.json +++ b/CloudronManifest.json @@ -12,7 +12,13 @@ "addons": { "postgresql": {}, "sendmail": {}, - "localstorage": {} + "localstorage": {}, + "scheduler": { + "prune-expired": { + "schedule": "20 7 * * *", + "command": "gosu node:node node /app/prune-expired.bundle.cjs" + } + } }, "website": "https://communityrule.info", "contactEmail": "hello@communityrule.info" diff --git a/Dockerfile b/Dockerfile index 02db462..c32e82f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -34,6 +34,10 @@ RUN ./node_modules/.bin/esbuild prisma/seed.ts \ --bundle --platform=node --format=cjs \ --outfile=prisma/seed.bundle.cjs \ --external:@prisma/client +RUN ./node_modules/.bin/esbuild scripts/prune-sessions.ts \ + --bundle --platform=node --format=cjs \ + --outfile=prune-expired.bundle.cjs \ + --external:@prisma/client FROM base AS runner # openssl: Prisma engines. gosu: privilege drop in start.sh after chown. @@ -48,6 +52,7 @@ COPY --from=builder --chown=node:node /app/public ./public COPY --from=builder --chown=node:node /app/.next/standalone ./ COPY --from=builder --chown=node:node /app/.next/static ./.next/static COPY --from=builder --chown=node:node /app/prisma ./prisma +COPY --from=builder --chown=node:node /app/prune-expired.bundle.cjs ./prune-expired.bundle.cjs # Facet/template seed JSON — NOT under /app/data (localstorage mount overlays that). COPY --from=builder --chown=node:node /app/data ./seed-data ENV SEED_DATA_DIR=/app/seed-data diff --git a/app/api/auth/magic-link/request/route.ts b/app/api/auth/magic-link/request/route.ts index 5db8b76..33b7cd0 100644 --- a/app/api/auth/magic-link/request/route.ts +++ b/app/api/auth/magic-link/request/route.ts @@ -23,8 +23,8 @@ import { safeInternalPath } from "../../../../../lib/safeInternalPath"; import { getPublicOrigin } from "../../../../../lib/server/publicOrigin"; import { magicLinkRequestBodySchema } from "../../../../../lib/server/validation/createFlowSchemas"; import { jsonFromZodError } from "../../../../../lib/server/validation/zodHttp"; +import { MAGIC_LINK_TTL_MS } from "../../../../../lib/privacyRetention"; -const MAGIC_LINK_TTL_MS = 60 * 60 * 1000; const EMAIL_MIN_INTERVAL_MS = 60 * 1000; const IP_MIN_INTERVAL_MS = 20 * 1000; const SCOPE = "auth.magicLink.request"; @@ -106,7 +106,7 @@ export const POST = apiRoute(SCOPE, async (request: NextRequest, _ctx, { request try { await sendMagicLinkEmail(email, verifyUrl); } catch (err) { - logRouteError(SCOPE, requestId, err, { phase: "sendMagicLinkEmail", email }); + logRouteError(SCOPE, requestId, err, { phase: "sendMagicLinkEmail" }); await prisma.magicLinkToken.deleteMany({ where: { email } }); return errorJson("mail_failed", "Could not send email", 502); } diff --git a/app/api/ops/prune/route.ts b/app/api/ops/prune/route.ts new file mode 100644 index 0000000..cda8eaa --- /dev/null +++ b/app/api/ops/prune/route.ts @@ -0,0 +1,21 @@ +import { NextRequest, NextResponse } from "next/server"; +import { apiRoute } from "../../../../lib/server/apiRoute"; +import { prisma } from "../../../../lib/server/db"; +import { getOpsPruneToken, isDatabaseConfigured } from "../../../../lib/server/env"; +import { pruneExpiredRecords } from "../../../../lib/server/pruneExpiredRecords"; +import { dbUnavailable, notFound, unauthorized } from "../../../../lib/server/responses"; +import { retryPendingWorkshopInquiryMail } from "../../../../lib/server/workshopSignup"; + +export const POST = apiRoute("ops.prune", async (request: NextRequest) => { + const token = getOpsPruneToken(); + if (!token) return notFound(); + + const header = request.headers.get("authorization") ?? ""; + if (header !== `Bearer ${token}`) return unauthorized(); + if (!isDatabaseConfigured()) return dbUnavailable(); + + const retried = await retryPendingWorkshopInquiryMail(); + const pruned = await pruneExpiredRecords(prisma); + + return NextResponse.json({ ok: true, retried, ...pruned }); +}); diff --git a/app/api/rules/route.ts b/app/api/rules/route.ts index b972262..6b38103 100644 --- a/app/api/rules/route.ts +++ b/app/api/rules/route.ts @@ -234,7 +234,6 @@ export const POST = apiRoute( } catch (err) { logRouteError("rules.publish", requestId, err, { phase: "sendRuleStakeholderInviteEmail", - email: inv.email, }); } } diff --git a/app/api/user/email-change/request/route.ts b/app/api/user/email-change/request/route.ts index 44f3d43..60deaf4 100644 --- a/app/api/user/email-change/request/route.ts +++ b/app/api/user/email-change/request/route.ts @@ -24,8 +24,8 @@ import { getPublicOrigin } from "../../../../../lib/server/publicOrigin"; import { readLimitedJson } from "../../../../../lib/server/validation/requestBody"; import { emailChangeRequestBodySchema } from "../../../../../lib/server/validation/userEmailChangeSchemas"; import { jsonFromZodError } from "../../../../../lib/server/validation/zodHttp"; +import { EMAIL_CHANGE_TTL_MS } from "../../../../../lib/privacyRetention"; -const EMAIL_CHANGE_TTL_MS = 60 * 60 * 1000; const EMAIL_MIN_INTERVAL_MS = 60 * 1000; const IP_MIN_INTERVAL_MS = 20 * 1000; const SCOPE = "user.emailChange.request"; @@ -122,10 +122,7 @@ export const POST = apiRoute(SCOPE, async (request: NextRequest, _ctx, { request try { await sendEmailChangeEmail(newEmail, verifyUrl); } catch (err) { - logRouteError(SCOPE, requestId, err, { - phase: "sendEmailChangeEmail", - newEmail, - }); + logRouteError(SCOPE, requestId, err, { phase: "sendEmailChangeEmail" }); await prisma.emailChangeToken.deleteMany({ where: { userId: user.id } }); return errorJson("mail_failed", "Could not send email", 502); } diff --git a/app/api/web-vitals/route.ts b/app/api/web-vitals/route.ts index 8bf77f8..7e6fa00 100644 --- a/app/api/web-vitals/route.ts +++ b/app/api/web-vitals/route.ts @@ -10,6 +10,7 @@ import { import { readLimitedJson } from "../../../lib/server/validation/requestBody"; import { webVitalIngestSchema } from "../../../lib/server/validation/webVitalsSchema"; import { jsonFromZodError } from "../../../lib/server/validation/zodHttp"; +import { pathOnlyUrl } from "../../../lib/server/pathOnlyUrl"; function normalizeTimestamp(raw: string | number): string { if (typeof raw === "number" && Number.isFinite(raw)) { @@ -47,7 +48,7 @@ export const POST = apiRoute("webVitals.post", async (request: NextRequest) => { value: body.data.value, rating: body.data.rating, }, - url: body.url, + url: pathOnlyUrl(body.url), userAgent: body.userAgent, timestamp: normalizeTimestamp(body.timestamp), receivedAt: new Date().toISOString(), diff --git a/docs/guides/backend-roadmap.md b/docs/guides/backend-roadmap.md index 1f59fd5..e100424 100644 --- a/docs/guides/backend-roadmap.md +++ b/docs/guides/backend-roadmap.md @@ -98,7 +98,7 @@ Plain-English entities (names can evolve): **RuleTemplate — recommendation matrix (after v1 list):** Product may author templates in **spreadsheets** (e.g. one row per governance pattern, columns for **matching dimensions** such as group size, organization type, location, maturity, plus long-form fields for create-flow prefill). That implies: **normalized schema or versioned JSON** for dimensions × template fit (✓/✗, weights, or scores), an **import path** (export `.xlsx` / Sheets → validate → DB or build-time artifact), and **`GET /api/templates` (or a sibling route)** that accepts **user- or wizard-selected facets** and returns a **ranked or filtered** set. **Out of scope for first ship** of Tickets 7–8 (seed + display list); tracked as **Ticket 16** in [docs/backend-linear-tickets.md](backend-linear-tickets.md) and Linear **[CR-88](https://linear.app/community-rule/issue/CR-88/backend-template-recommendation-matrix-facet-data-seed-and-apis-no)** (**Done** — committed JSON + seed; no runtime `.xlsx`). Prefer **batch import** over live Google Sheets API in production unless ops explicitly wants sync. -**Session lifecycle (shipped, [CR-85](https://linear.app/community-rule/issue/CR-85/backend-custom-session-lifecycle-cleanup-invalidation-policy)):** **Multi-device** policy — a new sign-in does **not** invalidate the user's other valid sessions. **Exception — verified email change ([CR-103](https://linear.app/community-rule/issue/CR-103/backend-change-account-email-verify-new-address-conflict-session)):** after `User.email` updates via `/api/user/email-change/verify`, all other sessions for that user are revoked; the current browser keeps its session when it opened the verify link with a valid cookie, otherwise a **new** session is issued on the device that completed verify. **Cleanup is lazy and cron-free:** every `createSessionForUser` prunes that user's expired rows (uses `@@index([userId])`); ~5% of sign-ins also run a global sweep so rows from users who never return remain bounded over months. Cleanup failures are logged but never fail the sign-in. **Rotation** on other privilege-sensitive actions is deferred to v1.1. See the ADR comment block at the top of [`lib/server/session.ts`](../../lib/server/session.ts). Revisit a small auth library (e.g. Auth.js, Lucia) only if maintaining custom code becomes costly. +**Session lifecycle (shipped, [CR-85](https://linear.app/community-rule/issue/CR-85/backend-custom-session-lifecycle-cleanup-invalidation-policy)):** **Multi-device** policy — a new sign-in does **not** invalidate the user's other valid sessions. **Exception — verified email change ([CR-103](https://linear.app/community-rule/issue/CR-103/backend-change-account-email-verify-new-address-conflict-session)):** after `User.email` updates via `/api/user/email-change/verify`, all other sessions for that user are revoked; the current browser keeps its session when it opened the verify link with a valid cookie, otherwise a **new** session is issued on the device that completed verify. **Cleanup is lazy plus a daily job:** every `createSessionForUser` prunes that user's expired rows (uses `@@index([userId])`); ~5% of sign-ins also run a global sweep so rows from users who never return remain bounded over months. Cloudron also runs [`scripts/prune-sessions.ts`](../../scripts/prune-sessions.ts) daily. Cleanup failures on sign-in are logged but never fail the sign-in. **Rotation** on other privilege-sensitive actions is deferred to v1.1. See the ADR comment block at the top of [`lib/server/session.ts`](../../lib/server/session.ts). Revisit a small auth library (e.g. Auth.js, Lucia) only if maintaining custom code becomes costly. **RuleDraft future (not v1):** versioning, multiple drafts per user, easier corruption recovery—only if product needs them. @@ -110,7 +110,7 @@ Align JSON shapes with `app/(app)/create/types.ts` as it matures. - **Decision:** **Custom** database-backed sessions + **email magic link**; cookies are **httpOnly**; session and magic-link tokens are hashed at rest. - **Rate limiting (magic-link request):** **In-memory** is acceptable for a **single Node process**. It does **not** coordinate across instances—**add a shared limiter (e.g. Redis)** before horizontal scaling or serious abuse exposure. -- **Lifecycle policy (shipped, [CR-85](https://linear.app/community-rule/issue/CR-85/backend-custom-session-lifecycle-cleanup-invalidation-policy)):** multi-device (sign-in does not revoke other valid sessions); **email change ([CR-103](https://linear.app/community-rule/issue/CR-103/backend-change-account-email-verify-new-address-conflict-session))** revokes other sessions as documented in [`lib/server/session.ts`](../../lib/server/session.ts) §4; lazy expired-row cleanup on every sign-in (per-user prune + ~5% global sweep) — no cron required. Token rotation deferred to v1.1. Canonical comment block lives at the top of [`lib/server/session.ts`](../../lib/server/session.ts). +- **Lifecycle policy (shipped, [CR-85](https://linear.app/community-rule/issue/CR-85/backend-custom-session-lifecycle-cleanup-invalidation-policy)):** multi-device (sign-in does not revoke other valid sessions); **email change ([CR-103](https://linear.app/community-rule/issue/CR-103/backend-change-account-email-verify-new-address-conflict-session))** revokes other sessions as documented in [`lib/server/session.ts`](../../lib/server/session.ts) §4; lazy expired-row cleanup on every sign-in (per-user prune + ~5% global sweep), plus a daily Cloudron scheduler job. Token rotation deferred to v1.1. Canonical comment block lives at the top of [`lib/server/session.ts`](../../lib/server/session.ts). - Do **not** treat “switch to NextAuth/Lucia” as required for v1; document the custom lifecycle above instead. --- diff --git a/docs/guides/ops-backend-deploy.md b/docs/guides/ops-backend-deploy.md index 6074ed9..99e419c 100644 --- a/docs/guides/ops-backend-deploy.md +++ b/docs/guides/ops-backend-deploy.md @@ -98,9 +98,9 @@ per-app in the manifest and provisioned at install time. [`CloudronManifest.json`](../../CloudronManifest.json) (`memoryLimit: 805306368`). The legacy LAMP app ran at 512 MiB; raise further only if Next.js standalone OOMs under load. -- Backups: Cloudron's automatic backups are already on for the host - (legacy app shows weekly snapshots ~451 MB each). Same default - applies to new apps. +- Backups: Cloudron's automatic backups are already on for the host. + Set the backup site retention to **`30 daily`** so backups match the + privacy policy (see [`ops-runbook.md`](ops-runbook.md) §9.3). - TLS for Cloudron app hostnames: handled by Cloudron (Let's Encrypt). - **Mail DNS (SPF/DKIM):** *not* automatic for this domain. Cloudron's DNS provider for `communityrule.info` is Namecheap, but outbound mail diff --git a/docs/guides/ops-runbook.md b/docs/guides/ops-runbook.md index 48962f6..c909aac 100644 --- a/docs/guides/ops-runbook.md +++ b/docs/guides/ops-runbook.md @@ -246,6 +246,8 @@ persist across image updates unless changed: | `SMTP_FROM` | Visible From on sign-in emails (e.g. `Community Rule `). | | `NEXT_PUBLIC_ENABLE_BACKEND_SYNC` | `true` in staging/production — Postgres draft persistence. | | `UPLOAD_ROOT` | `/app/data/uploads` on Cloudron — required for file uploads. | +| `OPS_PRUNE_TOKEN` | Bearer token for `POST /api/ops/prune`. Unset hides the route. Optional; the daily scheduler runs the prune inside the container without it. | +| `WORKSHOP_INQUIRY_PURGE_TOKEN` | Bearer token for `POST /api/workshop-inquiries/purge`. Unset hides that route. | Full detail: [`ops-backend-deploy.md` §3](ops-backend-deploy.md#3-environment-variables). @@ -279,8 +281,70 @@ That is expected. Recipients still authenticate the visible `From:` (`staging.ap `SMTP_FROM` should stay the Cloudron mailbox (`staging.app@communityrule.info` on staging, `hello@communityrule.info` on the apex app). The app falls back to `CLOUDRON_MAIL_FROM` if `SMTP_FROM` is unset. -## 9. Related docs +## 9. Retention (logs, backups, unused links) +Periods the privacy policy states. Source of truth: +[`docs/privacy-policy-facts.md`](../privacy-policy-facts.md) and +[`lib/privacyRetention.ts`](../../lib/privacyRetention.ts). + +### 9.1 Unused links and sessions + +[`CloudronManifest.json`](../../CloudronManifest.json) registers a +**scheduler** task `prune-expired` at **07:20 UTC** daily. It runs +`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. + +Manual runs: + +```bash +# inside the container (no token) +cloudron exec --app staging.communityrule.info -- \ + node /app/prune-expired.bundle.cjs + +# from outside, if OPS_PRUNE_TOKEN is set +curl -sS -X POST \ + -H "Authorization: Bearer $OPS_PRUNE_TOKEN" \ + https://staging.communityrule.info/api/ops/prune +``` + +Local: + +```bash +node --env-file=.env --import tsx scripts/prune-sessions.ts +``` + +If the scheduler addon is missing after an image update, add the same +command under the app **Cron** tab (`20 7 * * *`, UTC). + +### 9.2 Logs + +App logs (stdout) and Cloudron request logs are deleted after **14 +days**. That is the platform default and the shortest period Cloudron +allows. + +Page timing logs (`POST /api/web-vitals` in `external` mode) record +origin + path only. Query strings are stripped before the line is +written, so invitation and login tokens never land in app logs. + +Cloudron's reverse-proxy request logs still include the query string. +They follow the same 14-day window. Do not export those logs off the +host for longer than 14 days. + +Sign-in mail failures log a request id, not the email address. + +### 9.3 Backups + +Set the Cloudron backup site to **`30 daily`** (*Backups* → *Schedule & +Retention*). Confirm this on staging and production. Cloudron also keeps +a pre-update snapshot for up to three weeks. + +## 10. Related docs + +- [`../privacy-policy-facts.md`](../privacy-policy-facts.md) — periods + and hosting facts for the privacy policy. - [`ops-backend-deploy.md`](ops-backend-deploy.md) — first install, cutover plan, legacy rules archive, build/push deep dive. - [`backend-roadmap.md`](backend-roadmap.md) — migrations policy (§8), diff --git a/lib/privacyRetention.ts b/lib/privacyRetention.ts new file mode 100644 index 0000000..671c762 --- /dev/null +++ b/lib/privacyRetention.ts @@ -0,0 +1,16 @@ +/** + * Periods the privacy policy states. Change a value here, then update + * `docs/privacy-policy-facts.md` so counsel reads the same numbers. + */ + +const DAY_MS = 24 * 60 * 60 * 1000; + +/** How long a login or save-progress link works. */ +export const MAGIC_LINK_TTL_MINUTES = 60; +export const MAGIC_LINK_TTL_MS = MAGIC_LINK_TTL_MINUTES * 60 * 1000; + +/** How long an email-change confirm link works. Same window as login. */ +export const EMAIL_CHANGE_TTL_MS = MAGIC_LINK_TTL_MS; + +/** Workshop inquiries that do not lead to a workshop. */ +export const WORKSHOP_INQUIRY_RETENTION_MS = 365 * DAY_MS; diff --git a/lib/server/env.ts b/lib/server/env.ts index 09381ab..fdd09fe 100644 --- a/lib/server/env.ts +++ b/lib/server/env.ts @@ -30,6 +30,12 @@ export function isDatabaseConfigured(): boolean { return Boolean(getDatabaseUrl()); } +/** Bearer token for POST /api/ops/prune. Unset hides the route. */ +export function getOpsPruneToken(): string | null { + const raw = process.env.OPS_PRUNE_TOKEN?.trim(); + return raw && raw.length > 0 ? raw : null; +} + /** Named admins who may open a private Rule. Comma-separated emails. */ export function getRuleAdminEmails(): string[] { const raw = process.env.RULE_ADMIN_EMAILS?.trim(); diff --git a/lib/server/mail.ts b/lib/server/mail.ts index aad88b3..d68b5c9 100644 --- a/lib/server/mail.ts +++ b/lib/server/mail.ts @@ -5,6 +5,7 @@ import { buildWorkshopInboxMail, type WorkshopInquiryMailInput, } from "../workshopSignupMail"; +import { MAGIC_LINK_TTL_MINUTES } from "../privacyRetention"; import { getSmtpUrl } from "./env"; import { buildRuleStakeholderInviteMail, @@ -85,7 +86,7 @@ export async function sendMagicLinkEmail( ): Promise { const { text, html } = buildVerifyLinkParts( verifyUrl, - "Open this link to log in (it expires in 60 minutes):", + `Open this link to log in (it expires in ${MAGIC_LINK_TTL_MINUTES} minutes):`, "If you did not request this, you can ignore this email.\n\nSent because you saved a draft at communityrule.info", "Log in", ); @@ -174,7 +175,7 @@ export async function sendEmailChangeEmail( ): Promise { const { text, html } = buildVerifyLinkParts( verifyUrl, - "You asked to change the email on your Community Rule account.\n\nOpen this link to confirm the new address (it expires in 60 minutes):", + `You asked to change the email on your Community Rule account.\n\nOpen this link to confirm the new address (it expires in ${MAGIC_LINK_TTL_MINUTES} minutes):`, "If you did not request this change, you can ignore this email. Your current login is unchanged until you confirm.", "Confirm email", ); diff --git a/lib/server/pathOnlyUrl.ts b/lib/server/pathOnlyUrl.ts new file mode 100644 index 0000000..d21147d --- /dev/null +++ b/lib/server/pathOnlyUrl.ts @@ -0,0 +1,15 @@ +/** + * Origin + path, with query string and hash removed. + * Invitation and login tokens travel in the query string; share slugs are path. + */ +export function pathOnlyUrl(raw: string): string { + try { + const url = new URL(raw); + return `${url.origin}${url.pathname}`; + } catch { + const cut = [raw.indexOf("?"), raw.indexOf("#")] + .filter((index) => index >= 0) + .sort((a, b) => a - b)[0]; + return cut === undefined ? raw : raw.slice(0, cut); + } +} diff --git a/lib/server/pruneExpiredRecords.ts b/lib/server/pruneExpiredRecords.ts new file mode 100644 index 0000000..95504e0 --- /dev/null +++ b/lib/server/pruneExpiredRecords.ts @@ -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, + now = new Date(), +): Promise { + 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 { + 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, + }; +} diff --git a/lib/server/ruleStakeholderInviteOps.ts b/lib/server/ruleStakeholderInviteOps.ts index 946ca50..b4bd854 100644 --- a/lib/server/ruleStakeholderInviteOps.ts +++ b/lib/server/ruleStakeholderInviteOps.ts @@ -54,7 +54,6 @@ export async function createRuleStakeholderInviteAndSendMail(opts: { } catch (err) { logRouteError(opts.scope, opts.requestId, err, { phase: "sendRuleStakeholderInviteEmail", - email: opts.email, }); try { await prisma.ruleStakeholder.delete({ where: { id: row.id } }); diff --git a/lib/server/session.ts b/lib/server/session.ts index 03c5867..67beb6c 100644 --- a/lib/server/session.ts +++ b/lib/server/session.ts @@ -21,11 +21,13 @@ import { hashSessionToken, newSessionToken } from "./hash"; * 2. **Rotation: deferred.** No token rotation on privilege-sensitive * actions in v1. Revisit if/when product requires it (ticket calls * this v1.1). - * 3. **Cleanup: lazy, two-tier, no cron.** Every sign-in prunes the + * 3. **Cleanup: lazy plus a daily job.** Every sign-in prunes the * signing user's own expired rows (cheap — uses `@@index([userId])`). * A small fraction of sign-ins (`SESSION_GLOBAL_PRUNE_PROB`) also runs * a global sweep so rows from users who never return are still bounded - * over months. Cleanup is best-effort: a prune failure never fails the + * over months. Cloudron also runs `scripts/prune-sessions.ts` daily + * (expired sessions, unused links, and old workshop inquiries). + * Cleanup on sign-in is best-effort: a prune failure never fails the * sign-in itself. * 4. **Email change (CR-103).** After a verified email update, revoke every * `Session` for that `userId` **except** the current browser's session when diff --git a/lib/server/workshopSignup.ts b/lib/server/workshopSignup.ts index ad09340..fa0f4db 100644 --- a/lib/server/workshopSignup.ts +++ b/lib/server/workshopSignup.ts @@ -7,11 +7,11 @@ import { import { rateLimitKey } from "./rateLimit"; import { logger } from "../logger"; import { - WORKSHOP_INQUIRY_RETENTION_MS, WORKSHOP_MIN_FILL_MS, suggestWorkshopRoute, type WorkshopFormValues, } from "../workshopSignup"; +import { deleteExpiredWorkshopInquiries } from "./pruneExpiredRecords"; import { parseWorkshopSignupPayload, type ParsedWorkshopSignup, @@ -223,9 +223,5 @@ export async function retryPendingWorkshopInquiryMail(): Promise { /** Delete inquiries older than the retention period. */ export async function purgeExpiredWorkshopInquiries(now = new Date()): Promise { if (!isDatabaseConfigured()) return 0; - const cutoff = new Date(now.getTime() - WORKSHOP_INQUIRY_RETENTION_MS); - const result = await prisma.workshopInquiry.deleteMany({ - where: { createdAt: { lt: cutoff } }, - }); - return result.count; + return deleteExpiredWorkshopInquiries(prisma, now); } diff --git a/lib/workshopSignup.ts b/lib/workshopSignup.ts index 474d444..5cf7f1e 100644 --- a/lib/workshopSignup.ts +++ b/lib/workshopSignup.ts @@ -12,9 +12,6 @@ export const WORKSHOP_STARTED_AT_FIELD = "startedAt"; /** Ignore submits that arrive faster than a person can fill the form. */ export const WORKSHOP_MIN_FILL_MS = 4_000; -/** Proposed retention. Inquiries that do not lead to a workshop are deleted after this. */ -export const WORKSHOP_INQUIRY_RETENTION_MS = 365 * 24 * 60 * 60 * 1000; - export const WORKSHOP_CHALLENGE_IDS = [ "members-rarely-vote", "preparing-the-board", diff --git a/scripts/prune-sessions.ts b/scripts/prune-sessions.ts index 517c933..6b02bff 100644 --- a/scripts/prune-sessions.ts +++ b/scripts/prune-sessions.ts @@ -1,37 +1,57 @@ /** - * Manual prune-expired-sessions script for CR-85 verification. + * Prune expired sessions, unused login/save-progress/email-change tokens + * (and any draft they carry), unused invitation tokens, and old workshop + * inquiries. * * Usage (from repo root): * node --env-file=.env --import tsx scripts/prune-sessions.ts # global sweep - * node --env-file=.env --import tsx scripts/prune-sessions.ts # per-user + * node --env-file=.env --import tsx scripts/prune-sessions.ts # sessions for one user * * Intentionally does NOT import `lib/server/session` — that module pulls in * `next/headers` which requires a Next request context. For ad-hoc DB * surgery we talk to Prisma directly. */ import { PrismaClient } from "@prisma/client"; +import { pruneExpiredRecords } from "../lib/server/pruneExpiredRecords"; const prisma = new PrismaClient(); async function main() { const userId = process.argv[2]; const now = new Date(); - const before = await prisma.session.count(); - const { count } = await prisma.session.deleteMany({ - where: { - expiresAt: { lt: now }, - ...(userId ? { userId } : {}), - }, - }); - const after = await prisma.session.count(); + + if (userId) { + const before = await prisma.session.count({ where: { userId } }); + const { count } = await prisma.session.deleteMany({ + where: { + expiresAt: { lt: now }, + userId, + }, + }); + const after = await prisma.session.count({ where: { userId } }); + console.log( + JSON.stringify( + { + scope: userId, + now: now.toISOString(), + sessionsBefore: before, + pruned: count, + sessionsAfter: after, + }, + null, + 2, + ), + ); + return; + } + + const result = await pruneExpiredRecords(prisma, now); console.log( JSON.stringify( { - scope: userId ?? "global", + scope: "global", now: now.toISOString(), - sessionsBefore: before, - pruned: count, - sessionsAfter: after, + ...result, }, null, 2, diff --git a/tests/unit/authMagicLinkRequestRoute.test.ts b/tests/unit/authMagicLinkRequestRoute.test.ts index 897e9a1..18ee53c 100644 --- a/tests/unit/authMagicLinkRequestRoute.test.ts +++ b/tests/unit/authMagicLinkRequestRoute.test.ts @@ -34,6 +34,7 @@ vi.mock("../../lib/server/rateLimit", () => ({ rateLimitKey: () => ({ ok: true }), })); +import { logger } from "../../lib/logger"; import { POST } from "../../app/api/auth/magic-link/request/route"; beforeEach(() => { @@ -185,4 +186,29 @@ describe("POST /api/auth/magic-link/request", () => { expect(body.error.code).toBe("mail_failed"); expect(deleteManyMock).toHaveBeenCalledTimes(2); }); + + it("logs a request id rather than the email when mail fails", async () => { + const errorSpy = vi.spyOn(logger, "error").mockImplementation(() => {}); + try { + sendMagicLinkEmailMock.mockRejectedValueOnce(new Error("smtp down")); + const res = await POST( + new NextRequest("https://x.test/api/auth/magic-link/request", { + method: "POST", + body: JSON.stringify({ email: "member@example.com" }), + headers: { + "content-type": "application/json", + "x-request-id": "req-no-email", + }, + }), + undefined, + ); + expect(res.status).toBe(502); + expect(errorSpy).toHaveBeenCalled(); + const payload = errorSpy.mock.calls[0]?.[0] as Record; + expect(payload.requestId).toBe("req-no-email"); + expect(JSON.stringify(payload)).not.toContain("member@example.com"); + } finally { + errorSpy.mockRestore(); + } + }); }); diff --git a/tests/unit/env.test.ts b/tests/unit/env.test.ts index b04648c..cf8c369 100644 --- a/tests/unit/env.test.ts +++ b/tests/unit/env.test.ts @@ -1,6 +1,7 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { getDatabaseUrl, + getOpsPruneToken, getSmtpUrl, isDatabaseConfigured, } from "../../lib/server/env"; @@ -11,6 +12,7 @@ const ENV_KEYS = [ "CLOUDRON_MAIL_SMTP_PORT", "CLOUDRON_MAIL_SMTP_USERNAME", "CLOUDRON_MAIL_SMTP_PASSWORD", + "OPS_PRUNE_TOKEN", ] as const; const ORIGINAL_ENV = Object.fromEntries( @@ -97,3 +99,16 @@ describe("getSmtpUrl", () => { expect(getSmtpUrl()).toBe("smtp://apikey:@smtp.example.com:25"); }); }); + +describe("getOpsPruneToken", () => { + it("returns null when unset or blank", () => { + expect(getOpsPruneToken()).toBeNull(); + process.env.OPS_PRUNE_TOKEN = " "; + expect(getOpsPruneToken()).toBeNull(); + }); + + it("returns the trimmed token", () => { + process.env.OPS_PRUNE_TOKEN = " secret-token "; + expect(getOpsPruneToken()).toBe("secret-token"); + }); +}); diff --git a/tests/unit/opsPruneRoute.test.ts b/tests/unit/opsPruneRoute.test.ts new file mode 100644 index 0000000..b679234 --- /dev/null +++ b/tests/unit/opsPruneRoute.test.ts @@ -0,0 +1,89 @@ +import { NextRequest } from "next/server"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const isDatabaseConfiguredMock = vi.fn(); +const getOpsPruneTokenMock = vi.fn(); +const pruneExpiredRecordsMock = vi.fn(); +const retryPendingWorkshopInquiryMailMock = vi.fn(); + +vi.mock("../../lib/server/env", () => ({ + isDatabaseConfigured: () => isDatabaseConfiguredMock(), + getOpsPruneToken: () => getOpsPruneTokenMock(), +})); + +vi.mock("../../lib/server/db", () => ({ + prisma: { tag: "prisma" }, +})); + +vi.mock("../../lib/server/pruneExpiredRecords", () => ({ + pruneExpiredRecords: (...args: unknown[]) => + pruneExpiredRecordsMock(...args), +})); + +vi.mock("../../lib/server/workshopSignup", () => ({ + retryPendingWorkshopInquiryMail: () => + retryPendingWorkshopInquiryMailMock(), +})); + +import { POST } from "../../app/api/ops/prune/route"; + +function post(auth?: string) { + return new NextRequest("https://x.test/api/ops/prune", { + method: "POST", + headers: auth ? { authorization: auth } : undefined, + }); +} + +beforeEach(() => { + isDatabaseConfiguredMock.mockReset(); + getOpsPruneTokenMock.mockReset(); + pruneExpiredRecordsMock.mockReset(); + retryPendingWorkshopInquiryMailMock.mockReset(); + isDatabaseConfiguredMock.mockReturnValue(true); + pruneExpiredRecordsMock.mockResolvedValue({ + sessions: 1, + magicLinkTokens: 2, + emailChangeTokens: 0, + unusedInviteTokens: 3, + workshopInquiries: 4, + }); + retryPendingWorkshopInquiryMailMock.mockResolvedValue(1); +}); + +afterEach(() => { + vi.unstubAllEnvs(); +}); + +describe("POST /api/ops/prune", () => { + it("returns 404 when the token is unset", async () => { + getOpsPruneTokenMock.mockReturnValue(null); + const res = await POST(post("Bearer secret"), undefined); + expect(res.status).toBe(404); + expect(pruneExpiredRecordsMock).not.toHaveBeenCalled(); + }); + + it("returns 401 when the bearer token does not match", async () => { + getOpsPruneTokenMock.mockReturnValue("secret"); + const res = await POST(post("Bearer other"), undefined); + expect(res.status).toBe(401); + expect(pruneExpiredRecordsMock).not.toHaveBeenCalled(); + }); + + it("prunes expired rows when authorized", async () => { + getOpsPruneTokenMock.mockReturnValue("secret"); + const res = await POST(post("Bearer secret"), undefined); + expect(res.status).toBe(200); + expect(await res.json()).toEqual({ + ok: true, + retried: 1, + sessions: 1, + magicLinkTokens: 2, + emailChangeTokens: 0, + unusedInviteTokens: 3, + workshopInquiries: 4, + }); + expect(pruneExpiredRecordsMock).toHaveBeenCalledWith( + expect.objectContaining({ tag: "prisma" }), + ); + }); +}); diff --git a/tests/unit/pathOnlyUrl.test.ts b/tests/unit/pathOnlyUrl.test.ts new file mode 100644 index 0000000..9d05453 --- /dev/null +++ b/tests/unit/pathOnlyUrl.test.ts @@ -0,0 +1,35 @@ +import { describe, expect, it } from "vitest"; +import { pathOnlyUrl } from "../../lib/server/pathOnlyUrl"; + +describe("pathOnlyUrl", () => { + it("drops the query string from an absolute URL", () => { + expect( + pathOnlyUrl( + "https://communityrule.info/api/auth/magic-link/verify?token=secret", + ), + ).toBe("https://communityrule.info/api/auth/magic-link/verify"); + }); + + it("drops invite tokens and hashes", () => { + expect( + pathOnlyUrl( + "https://communityrule.info/api/invites/rule-stakeholder/verify?token=abc#frag", + ), + ).toBe( + "https://communityrule.info/api/invites/rule-stakeholder/verify", + ); + }); + + it("keeps a share path with no query string", () => { + expect(pathOnlyUrl("https://communityrule.info/r/share-slug")).toBe( + "https://communityrule.info/r/share-slug", + ); + }); + + it("strips a relative URL at the first ? or #", () => { + expect(pathOnlyUrl("/create/community-name?facet.size=small")).toBe( + "/create/community-name", + ); + expect(pathOnlyUrl("/login#sent")).toBe("/login"); + }); +}); diff --git a/tests/unit/pruneExpiredRecords.test.ts b/tests/unit/pruneExpiredRecords.test.ts new file mode 100644 index 0000000..9ddaad6 --- /dev/null +++ b/tests/unit/pruneExpiredRecords.test.ts @@ -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/); + }); +}); diff --git a/tests/unit/webVitalsPostRoute.test.ts b/tests/unit/webVitalsPostRoute.test.ts new file mode 100644 index 0000000..d26182f --- /dev/null +++ b/tests/unit/webVitalsPostRoute.test.ts @@ -0,0 +1,83 @@ +import { NextRequest } from "next/server"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +const loggerInfoMock = vi.fn(); +const getWebVitalsStorageModeMock = vi.fn(); +const appendLocalWebVitalMock = vi.fn(); + +vi.mock("../../lib/logger", () => ({ + logger: { + info: (...args: unknown[]) => loggerInfoMock(...args), + warn: vi.fn(), + error: vi.fn(), + debug: vi.fn(), + }, +})); + +vi.mock("../../lib/server/webVitals/mode", () => ({ + getWebVitalsStorageMode: () => getWebVitalsStorageModeMock(), +})); + +vi.mock("../../lib/server/webVitals/localFileStore", () => ({ + appendLocalWebVital: (...args: unknown[]) => appendLocalWebVitalMock(...args), + readLocalAggregatedMetrics: () => ({}), +})); + +import { POST } from "../../app/api/web-vitals/route"; + +function post(url: string) { + return new NextRequest("https://x.test/api/web-vitals", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ + metric: "lcp", + data: { value: 1200, rating: "good" }, + url, + userAgent: "test", + timestamp: "2026-11-10T12:00:00.000Z", + }), + }); +} + +beforeEach(() => { + loggerInfoMock.mockReset(); + getWebVitalsStorageModeMock.mockReset(); + appendLocalWebVitalMock.mockReset(); + getWebVitalsStorageModeMock.mockReturnValue("external"); +}); + +afterEach(() => { + vi.unstubAllEnvs(); +}); + +describe("POST /api/web-vitals", () => { + it("logs origin and path without the query string", async () => { + const res = await POST( + post( + "https://communityrule.info/api/invites/rule-stakeholder/verify?token=secret", + ), + undefined, + ); + expect(res.status).toBe(200); + const line = String(loggerInfoMock.mock.calls[0]?.[0]); + expect(line).toContain( + "https://communityrule.info/api/invites/rule-stakeholder/verify", + ); + expect(line).not.toContain("token="); + expect(line).not.toContain("secret"); + }); + + it("stores a path-only URL in local mode", async () => { + getWebVitalsStorageModeMock.mockReturnValue("local"); + const res = await POST( + post("https://example.test/create/name?facet.size=small"), + undefined, + ); + expect(res.status).toBe(200); + expect(appendLocalWebVitalMock).toHaveBeenCalledWith( + expect.objectContaining({ + url: "https://example.test/create/name", + }), + ); + }); +}); From b4d0d085d3de6b6de3f4887df52e80204386dc8f Mon Sep 17 00:00:00 2001 From: adilallo <39313955+adilallo@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:34:35 -0700 Subject: [PATCH 2/3] Record the retention and hosting facts the privacy policy still has in brackets. Login links work for 60 minutes, unused rows are gone within a day, logs last 14 days, and backups are 30 days on MEDLab Cloudron in the United States. Live /privacy stays on the current page until counsel reads this text. --- docs/README.md | 3 ++ docs/privacy-policy-facts.md | 56 +++++++++++++++++++++++++++++ messages/en/pages/login.json | 2 +- messages/en/pages/privacy.json | 2 +- tests/unit/privacyRetention.test.ts | 11 ++++++ 5 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 docs/privacy-policy-facts.md create mode 100644 tests/unit/privacyRetention.test.ts diff --git a/docs/README.md b/docs/README.md index 2de86de..7f8fc5a 100644 --- a/docs/README.md +++ b/docs/README.md @@ -8,6 +8,9 @@ User-facing docs. Implementation conventions live in `.cursor/rules/`. URLs, persistence. Source of truth for product/eng alignment. - [testing-guide.md](./testing-guide.md) — Testing philosophy, layer coverage, and Prisma migration smoke before merge. +- [privacy-policy-facts.md](./privacy-policy-facts.md) — Periods and + hosting facts for the privacy policy counsel will read. Live `/privacy` + stays on the current page until that review. - [figma-component-registry.md](./figma-component-registry.md) — Figma Community Rule System file ↔ `app/components/` (and route-colocated admin UI) map. diff --git a/docs/privacy-policy-facts.md b/docs/privacy-policy-facts.md new file mode 100644 index 0000000..e470cca --- /dev/null +++ b/docs/privacy-policy-facts.md @@ -0,0 +1,56 @@ +# Privacy policy facts + +Numbers and hosting details the new privacy policy leaves in brackets. +Live `/privacy` stays on the current page until university counsel has +read this text. Do not switch the route on from this file. + +## How long things are kept + +| What | Period | Where it is built | +| --- | --- | --- | +| 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 | +| 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. | — | +| Server logs (app logs and Cloudron request logs) | **14 days.** Cloudron removes logs older than that. That is the shortest period the platform allows. | Cloudron log viewer | +| Backups | **30 days** (`30 daily` on the Cloudron backup site). Cloudron also keeps a pre-update snapshot for up to three extra weeks. | Operator setting, see the runbook | +| Reply to a data request | **30 days** | Process, not an app timer | + +Invitation **lifetime while it still works** is a product decision, not +set here. At launch the invite token uses the same 60-minute window as +login. + +## Where it runs + +Servers are in the **United States**. MEDLab operates them with +**Cloudron**: the app, the PostgreSQL database, mail, and file storage +on that host. Sign-in mail relays through Amazon SES. + +## Who reads workshop inquiries + +Only the people who answer them: MEDLab staff with the workshop inbox +and the operator page. Inquiries live in the `WorkshopInquiry` table +until the 12-month delete. + +## Analytics + +**Umami**, self-hosted beside the app, with no cookies and aggregate +counts only. Name it in the policy. The counter itself ships with the +counts-by-group work, not this pass. + +## Product copy the build already matches + +- **Login sent.** A login link works for **60 minutes**. The sent state + and the login email both say so. +- **Offline.** Answers stay in this browser until the person reconnects. + The offline notice already says that. + +## After counsel, and after launch + +- `/privacy` can show the new policy only after university counsel has + read it, including the Colorado Open Records Act line, Terms, Cookies, + the legal cards, and CC0 for cards and templates. +- An email to every account holder before a change to who can read Rules + is after launch. Nothing about who can read Rules changes at launch. diff --git a/messages/en/pages/login.json b/messages/en/pages/login.json index eff0895..85d176d 100644 --- a/messages/en/pages/login.json +++ b/messages/en/pages/login.json @@ -13,7 +13,7 @@ "emailPlaceholder": "you@example.com", "sendMagicLink": "Send me a magic link", "successTitle": "Check your email", - "successBody": "We sent a login link. Open it on this device to continue. If you don't see it, check spam or promotions.", + "successBody": "We sent a login link that works for 60 minutes. Open it on this device to continue. If you don't see it, check spam or promotions.", "legalPrefix": "By continuing, you agree to our ", "legalAnd": " and ", "legalSuffix": ".", diff --git a/messages/en/pages/privacy.json b/messages/en/pages/privacy.json index 6dd10c6..6533400 100644 --- a/messages/en/pages/privacy.json +++ b/messages/en/pages/privacy.json @@ -1,5 +1,5 @@ { - "_comment": "Privacy policy. Owns personal data, hosting, and how ranking works. API usage and licenses live on Terms. Grounded in MEDLab Cloudron + SES mail relay + facet-matrix recommendations. Not a substitute for university counsel review.", + "_comment": "Privacy policy currently on /privacy. Owns personal data, hosting, and how ranking works. API usage and licenses live on Terms. Grounded in MEDLab Cloudron + SES mail relay + facet-matrix recommendations. Periods and hosting facts for the new policy live in docs/privacy-policy-facts.md. Do not switch /privacy to that text until university counsel has read it.", "banner": { "title": "Privacy Policy", "description": "What we collect, where it is stored, and how template recommendations work.", diff --git a/tests/unit/privacyRetention.test.ts b/tests/unit/privacyRetention.test.ts new file mode 100644 index 0000000..133a75d --- /dev/null +++ b/tests/unit/privacyRetention.test.ts @@ -0,0 +1,11 @@ +import { describe, expect, it } from "vitest"; +import login from "../../messages/en/pages/login.json"; +import { MAGIC_LINK_TTL_MINUTES } from "../../lib/privacyRetention"; + +describe("privacy retention copy", () => { + it("states the login link lifetime in the sent state", () => { + expect(login.successBody).toContain( + `works for ${MAGIC_LINK_TTL_MINUTES} minutes`, + ); + }); +}); 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 3/3] 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/); });