Merge pull request 'Delete unused login links each day, keep those URLs out of logs, and write the privacy-policy numbers' (#103) from adilallo/fix/CR-251-link-hygiene into main

Reviewed-on: #103
This commit was merged in pull request #103.
This commit is contained in:
an.di committed 2026-11-11 01:02:25 +00:00
commit 0efc907633
33 files changed
+675 -47

No files matched your search

+3
View File
@@ -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=
+2 -1
View File
@@ -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
+7 -1
View File
@@ -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"
+5
View File
@@ -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
+2 -2
View File
@@ -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);
}
+21
View File
@@ -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 });
});
-1
View File
@@ -234,7 +234,6 @@ export const POST = apiRoute(
} catch (err) {
logRouteError("rules.publish", requestId, err, {
phase: "sendRuleStakeholderInviteEmail",
email: inv.email,
});
}
}
+2 -5
View File
@@ -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);
}
+2 -1
View File
@@ -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(),
+3
View File
@@ -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.
+2 -2
View File
@@ -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.
---
+3 -3
View File
@@ -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
+66 -1
View File
@@ -246,6 +246,8 @@ persist across image updates unless changed:
| `SMTP_FROM` | Visible From on sign-in emails (e.g. `Community Rule <hello@communityrule.info>`). |
| `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,71 @@ 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, 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:
```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),
+56
View File
@@ -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 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. | — |
| 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.
+16
View File
@@ -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;
+6
View File
@@ -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();
+3 -2
View File
@@ -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<void> {
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<void> {
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",
);
+15
View File
@@ -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);
}
}
+74
View File
@@ -0,0 +1,74 @@
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 }>;
};
workshopInquiry: {
deleteMany: (args: {
where: { createdAt: { lt: Date } };
}) => Promise<{ count: number }>;
};
}
export interface PruneExpiredResult {
sessions: number;
magicLinkTokens: number;
emailChangeTokens: 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, 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,
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 workshopInquiries = await deleteExpiredWorkshopInquiries(db, now);
return {
sessions: sessions.count,
magicLinkTokens: magicLinkTokens.count,
emailChangeTokens: emailChangeTokens.count,
workshopInquiries,
};
}
-1
View File
@@ -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 } });
+4 -2
View File
@@ -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
+2 -6
View File
@@ -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<number> {
/** Delete inquiries older than the retention period. */
export async function purgeExpiredWorkshopInquiries(now = new Date()): Promise<number> {
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);
}
-3
View File
@@ -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",
+1 -1
View File
@@ -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": ".",
+1 -1
View File
@@ -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.",
+34 -14
View File
@@ -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), 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
* node --env-file=.env --import tsx scripts/prune-sessions.ts <userId> # per-user
* node --env-file=.env --import tsx scripts/prune-sessions.ts <userId> # 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,
@@ -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<string, unknown>;
expect(payload.requestId).toBe("req-no-email");
expect(JSON.stringify(payload)).not.toContain("member@example.com");
} finally {
errorSpy.mockRestore();
}
});
});
+15
View File
@@ -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");
});
});
+87
View File
@@ -0,0 +1,87 @@
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,
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,
workshopInquiries: 4,
});
expect(pruneExpiredRecordsMock).toHaveBeenCalledWith(
expect.objectContaining({ tag: "prisma" }),
);
});
});
+35
View File
@@ -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");
});
});
+11
View File
@@ -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`,
);
});
});
+88
View File
@@ -0,0 +1,88 @@
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;
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: 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 without clearing invite hashes", async () => {
const now = new Date("2026-11-10T12:00:00.000Z");
const mocks = store({
sessions: 2,
magicLinkTokens: 3,
emailChangeTokens: 1,
workshopInquiries: 5,
});
const result = await pruneExpiredRecords(mocks.db, now);
expect(result).toEqual({
sessions: 2,
magicLinkTokens: 3,
emailChangeTokens: 1,
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).not.toHaveBeenCalled();
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,
]);
expect(payload).not.toMatch(/tokenHash|email/);
});
});
+83
View File
@@ -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",
}),
);
});
});