Delete unused login and invite tokens on a daily job, and keep their URLs out of logs.
Expired sign-in, save-progress, email-change, and invitation tokens (and any draft they carry) are cleared by a Cloudron scheduler task. Page timing logs record the path without the query string, and mail failures log a request id instead of an address.
This commit is contained in:
1 parent
73d0856004
commit
46c34a6323
28 files changed
+636
-45
No files matched your search
@@ -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).
|
# Bearer token for POST /api/workshop-inquiries/purge (retry unsent mail, delete inquiries older than 12 months).
|
||||||
WORKSHOP_INQUIRY_PURGE_TOKEN=
|
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.
|
# 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.
|
# Server draft sync (default on). Set to `false` to disable PUT/GET /api/drafts/me.
|
||||||
NEXT_PUBLIC_ENABLE_BACKEND_SYNC=
|
NEXT_PUBLIC_ENABLE_BACKEND_SYNC=
|
||||||
|
|||||||
+2
-1
@@ -117,7 +117,8 @@ Facet semantics and the recommendation matrix:
|
|||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
| POST | `/api/organizer-inquiry` | "Ask an organizer" form submission. |
|
| POST | `/api/organizer-inquiry` | "Ask an organizer" form submission. |
|
||||||
| POST | `/api/use-cases/[slug]/duplicate` | Duplicate a use-case demo rule. |
|
| 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
|
## Testing
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,13 @@
|
|||||||
"addons": {
|
"addons": {
|
||||||
"postgresql": {},
|
"postgresql": {},
|
||||||
"sendmail": {},
|
"sendmail": {},
|
||||||
"localstorage": {}
|
"localstorage": {},
|
||||||
|
"scheduler": {
|
||||||
|
"prune-expired": {
|
||||||
|
"schedule": "20 7 * * *",
|
||||||
|
"command": "gosu node:node node /app/prune-expired.bundle.cjs"
|
||||||
|
}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"website": "https://communityrule.info",
|
"website": "https://communityrule.info",
|
||||||
"contactEmail": "hello@communityrule.info"
|
"contactEmail": "hello@communityrule.info"
|
||||||
|
|||||||
@@ -34,6 +34,10 @@ RUN ./node_modules/.bin/esbuild prisma/seed.ts \
|
|||||||
--bundle --platform=node --format=cjs \
|
--bundle --platform=node --format=cjs \
|
||||||
--outfile=prisma/seed.bundle.cjs \
|
--outfile=prisma/seed.bundle.cjs \
|
||||||
--external:@prisma/client
|
--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
|
FROM base AS runner
|
||||||
# openssl: Prisma engines. gosu: privilege drop in start.sh after chown.
|
# 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/standalone ./
|
||||||
COPY --from=builder --chown=node:node /app/.next/static ./.next/static
|
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/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).
|
# Facet/template seed JSON — NOT under /app/data (localstorage mount overlays that).
|
||||||
COPY --from=builder --chown=node:node /app/data ./seed-data
|
COPY --from=builder --chown=node:node /app/data ./seed-data
|
||||||
ENV SEED_DATA_DIR=/app/seed-data
|
ENV SEED_DATA_DIR=/app/seed-data
|
||||||
|
|||||||
@@ -23,8 +23,8 @@ import { safeInternalPath } from "../../../../../lib/safeInternalPath";
|
|||||||
import { getPublicOrigin } from "../../../../../lib/server/publicOrigin";
|
import { getPublicOrigin } from "../../../../../lib/server/publicOrigin";
|
||||||
import { magicLinkRequestBodySchema } from "../../../../../lib/server/validation/createFlowSchemas";
|
import { magicLinkRequestBodySchema } from "../../../../../lib/server/validation/createFlowSchemas";
|
||||||
import { jsonFromZodError } from "../../../../../lib/server/validation/zodHttp";
|
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 EMAIL_MIN_INTERVAL_MS = 60 * 1000;
|
||||||
const IP_MIN_INTERVAL_MS = 20 * 1000;
|
const IP_MIN_INTERVAL_MS = 20 * 1000;
|
||||||
const SCOPE = "auth.magicLink.request";
|
const SCOPE = "auth.magicLink.request";
|
||||||
@@ -106,7 +106,7 @@ export const POST = apiRoute(SCOPE, async (request: NextRequest, _ctx, { request
|
|||||||
try {
|
try {
|
||||||
await sendMagicLinkEmail(email, verifyUrl);
|
await sendMagicLinkEmail(email, verifyUrl);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logRouteError(SCOPE, requestId, err, { phase: "sendMagicLinkEmail", email });
|
logRouteError(SCOPE, requestId, err, { phase: "sendMagicLinkEmail" });
|
||||||
await prisma.magicLinkToken.deleteMany({ where: { email } });
|
await prisma.magicLinkToken.deleteMany({ where: { email } });
|
||||||
return errorJson("mail_failed", "Could not send email", 502);
|
return errorJson("mail_failed", "Could not send email", 502);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 });
|
||||||
|
});
|
||||||
@@ -234,7 +234,6 @@ export const POST = apiRoute(
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
logRouteError("rules.publish", requestId, err, {
|
logRouteError("rules.publish", requestId, err, {
|
||||||
phase: "sendRuleStakeholderInviteEmail",
|
phase: "sendRuleStakeholderInviteEmail",
|
||||||
email: inv.email,
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,8 @@ import { getPublicOrigin } from "../../../../../lib/server/publicOrigin";
|
|||||||
import { readLimitedJson } from "../../../../../lib/server/validation/requestBody";
|
import { readLimitedJson } from "../../../../../lib/server/validation/requestBody";
|
||||||
import { emailChangeRequestBodySchema } from "../../../../../lib/server/validation/userEmailChangeSchemas";
|
import { emailChangeRequestBodySchema } from "../../../../../lib/server/validation/userEmailChangeSchemas";
|
||||||
import { jsonFromZodError } from "../../../../../lib/server/validation/zodHttp";
|
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 EMAIL_MIN_INTERVAL_MS = 60 * 1000;
|
||||||
const IP_MIN_INTERVAL_MS = 20 * 1000;
|
const IP_MIN_INTERVAL_MS = 20 * 1000;
|
||||||
const SCOPE = "user.emailChange.request";
|
const SCOPE = "user.emailChange.request";
|
||||||
@@ -122,10 +122,7 @@ export const POST = apiRoute(SCOPE, async (request: NextRequest, _ctx, { request
|
|||||||
try {
|
try {
|
||||||
await sendEmailChangeEmail(newEmail, verifyUrl);
|
await sendEmailChangeEmail(newEmail, verifyUrl);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logRouteError(SCOPE, requestId, err, {
|
logRouteError(SCOPE, requestId, err, { phase: "sendEmailChangeEmail" });
|
||||||
phase: "sendEmailChangeEmail",
|
|
||||||
newEmail,
|
|
||||||
});
|
|
||||||
await prisma.emailChangeToken.deleteMany({ where: { userId: user.id } });
|
await prisma.emailChangeToken.deleteMany({ where: { userId: user.id } });
|
||||||
return errorJson("mail_failed", "Could not send email", 502);
|
return errorJson("mail_failed", "Could not send email", 502);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
import { readLimitedJson } from "../../../lib/server/validation/requestBody";
|
import { readLimitedJson } from "../../../lib/server/validation/requestBody";
|
||||||
import { webVitalIngestSchema } from "../../../lib/server/validation/webVitalsSchema";
|
import { webVitalIngestSchema } from "../../../lib/server/validation/webVitalsSchema";
|
||||||
import { jsonFromZodError } from "../../../lib/server/validation/zodHttp";
|
import { jsonFromZodError } from "../../../lib/server/validation/zodHttp";
|
||||||
|
import { pathOnlyUrl } from "../../../lib/server/pathOnlyUrl";
|
||||||
|
|
||||||
function normalizeTimestamp(raw: string | number): string {
|
function normalizeTimestamp(raw: string | number): string {
|
||||||
if (typeof raw === "number" && Number.isFinite(raw)) {
|
if (typeof raw === "number" && Number.isFinite(raw)) {
|
||||||
@@ -47,7 +48,7 @@ export const POST = apiRoute("webVitals.post", async (request: NextRequest) => {
|
|||||||
value: body.data.value,
|
value: body.data.value,
|
||||||
rating: body.data.rating,
|
rating: body.data.rating,
|
||||||
},
|
},
|
||||||
url: body.url,
|
url: pathOnlyUrl(body.url),
|
||||||
userAgent: body.userAgent,
|
userAgent: body.userAgent,
|
||||||
timestamp: normalizeTimestamp(body.timestamp),
|
timestamp: normalizeTimestamp(body.timestamp),
|
||||||
receivedAt: new Date().toISOString(),
|
receivedAt: new Date().toISOString(),
|
||||||
|
|||||||
@@ -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.
|
**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.
|
**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.
|
- **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.
|
- **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.
|
- Do **not** treat “switch to NextAuth/Lucia” as required for v1; document the custom lifecycle above instead.
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|||||||
@@ -98,9 +98,9 @@ per-app in the manifest and provisioned at install time.
|
|||||||
[`CloudronManifest.json`](../../CloudronManifest.json) (`memoryLimit:
|
[`CloudronManifest.json`](../../CloudronManifest.json) (`memoryLimit:
|
||||||
805306368`). The legacy LAMP app ran at 512 MiB; raise further only if
|
805306368`). The legacy LAMP app ran at 512 MiB; raise further only if
|
||||||
Next.js standalone OOMs under load.
|
Next.js standalone OOMs under load.
|
||||||
- Backups: Cloudron's automatic backups are already on for the host
|
- Backups: Cloudron's automatic backups are already on for the host.
|
||||||
(legacy app shows weekly snapshots ~451 MB each). Same default
|
Set the backup site retention to **`30 daily`** so backups match the
|
||||||
applies to new apps.
|
privacy policy (see [`ops-runbook.md`](ops-runbook.md) §9.3).
|
||||||
- TLS for Cloudron app hostnames: handled by Cloudron (Let's Encrypt).
|
- TLS for Cloudron app hostnames: handled by Cloudron (Let's Encrypt).
|
||||||
- **Mail DNS (SPF/DKIM):** *not* automatic for this domain. Cloudron's
|
- **Mail DNS (SPF/DKIM):** *not* automatic for this domain. Cloudron's
|
||||||
DNS provider for `communityrule.info` is Namecheap, but outbound mail
|
DNS provider for `communityrule.info` is Namecheap, but outbound mail
|
||||||
|
|||||||
@@ -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>`). |
|
| `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. |
|
| `NEXT_PUBLIC_ENABLE_BACKEND_SYNC` | `true` in staging/production — Postgres draft persistence. |
|
||||||
| `UPLOAD_ROOT` | `/app/data/uploads` on Cloudron — required for file uploads. |
|
| `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).
|
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.
|
`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
|
- [`ops-backend-deploy.md`](ops-backend-deploy.md) — first install, cutover
|
||||||
plan, legacy rules archive, build/push deep dive.
|
plan, legacy rules archive, build/push deep dive.
|
||||||
- [`backend-roadmap.md`](backend-roadmap.md) — migrations policy (§8),
|
- [`backend-roadmap.md`](backend-roadmap.md) — migrations policy (§8),
|
||||||
|
|||||||
@@ -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;
|
||||||
@@ -30,6 +30,12 @@ export function isDatabaseConfigured(): boolean {
|
|||||||
return Boolean(getDatabaseUrl());
|
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. */
|
/** Named admins who may open a private Rule. Comma-separated emails. */
|
||||||
export function getRuleAdminEmails(): string[] {
|
export function getRuleAdminEmails(): string[] {
|
||||||
const raw = process.env.RULE_ADMIN_EMAILS?.trim();
|
const raw = process.env.RULE_ADMIN_EMAILS?.trim();
|
||||||
|
|||||||
+3
-2
@@ -5,6 +5,7 @@ import {
|
|||||||
buildWorkshopInboxMail,
|
buildWorkshopInboxMail,
|
||||||
type WorkshopInquiryMailInput,
|
type WorkshopInquiryMailInput,
|
||||||
} from "../workshopSignupMail";
|
} from "../workshopSignupMail";
|
||||||
|
import { MAGIC_LINK_TTL_MINUTES } from "../privacyRetention";
|
||||||
import { getSmtpUrl } from "./env";
|
import { getSmtpUrl } from "./env";
|
||||||
import {
|
import {
|
||||||
buildRuleStakeholderInviteMail,
|
buildRuleStakeholderInviteMail,
|
||||||
@@ -85,7 +86,7 @@ export async function sendMagicLinkEmail(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { text, html } = buildVerifyLinkParts(
|
const { text, html } = buildVerifyLinkParts(
|
||||||
verifyUrl,
|
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",
|
"If you did not request this, you can ignore this email.\n\nSent because you saved a draft at communityrule.info",
|
||||||
"Log in",
|
"Log in",
|
||||||
);
|
);
|
||||||
@@ -174,7 +175,7 @@ export async function sendEmailChangeEmail(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const { text, html } = buildVerifyLinkParts(
|
const { text, html } = buildVerifyLinkParts(
|
||||||
verifyUrl,
|
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.",
|
"If you did not request this change, you can ignore this email. Your current login is unchanged until you confirm.",
|
||||||
"Confirm email",
|
"Confirm email",
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
import { WORKSHOP_INQUIRY_RETENTION_MS } from "../privacyRetention";
|
||||||
|
|
||||||
|
export interface PruneStore {
|
||||||
|
session: {
|
||||||
|
deleteMany: (args: {
|
||||||
|
where: { expiresAt: { lt: Date }; userId?: string };
|
||||||
|
}) => Promise<{ count: number }>;
|
||||||
|
};
|
||||||
|
magicLinkToken: {
|
||||||
|
deleteMany: (args: {
|
||||||
|
where: { expiresAt: { lt: Date } };
|
||||||
|
}) => Promise<{ count: number }>;
|
||||||
|
};
|
||||||
|
emailChangeToken: {
|
||||||
|
deleteMany: (args: {
|
||||||
|
where: { expiresAt: { lt: Date } };
|
||||||
|
}) => Promise<{ count: number }>;
|
||||||
|
};
|
||||||
|
ruleStakeholder: {
|
||||||
|
updateMany: (args: {
|
||||||
|
where: {
|
||||||
|
inviteTokenHash: { not: null };
|
||||||
|
inviteExpiresAt: { lt: Date };
|
||||||
|
};
|
||||||
|
data: { inviteTokenHash: null; inviteExpiresAt: null };
|
||||||
|
}) => Promise<{ count: number }>;
|
||||||
|
};
|
||||||
|
workshopInquiry: {
|
||||||
|
deleteMany: (args: {
|
||||||
|
where: { createdAt: { lt: Date } };
|
||||||
|
}) => Promise<{ count: number }>;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface PruneExpiredResult {
|
||||||
|
sessions: number;
|
||||||
|
magicLinkTokens: number;
|
||||||
|
emailChangeTokens: number;
|
||||||
|
unusedInviteTokens: number;
|
||||||
|
workshopInquiries: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteExpiredWorkshopInquiries(
|
||||||
|
db: Pick<PruneStore, "workshopInquiry">,
|
||||||
|
now = new Date(),
|
||||||
|
): Promise<number> {
|
||||||
|
const cutoff = new Date(now.getTime() - WORKSHOP_INQUIRY_RETENTION_MS);
|
||||||
|
const result = await db.workshopInquiry.deleteMany({
|
||||||
|
where: { createdAt: { lt: cutoff } },
|
||||||
|
});
|
||||||
|
return result.count;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete expired sessions, login/save-progress tokens (and the draft they
|
||||||
|
* carry), email-change tokens, unused invitation tokens, and old workshop
|
||||||
|
* inquiries. Does not delete stakeholder rows or accounts.
|
||||||
|
*/
|
||||||
|
export async function pruneExpiredRecords(
|
||||||
|
db: PruneStore,
|
||||||
|
now = new Date(),
|
||||||
|
): Promise<PruneExpiredResult> {
|
||||||
|
const sessions = await db.session.deleteMany({
|
||||||
|
where: { expiresAt: { lt: now } },
|
||||||
|
});
|
||||||
|
const magicLinkTokens = await db.magicLinkToken.deleteMany({
|
||||||
|
where: { expiresAt: { lt: now } },
|
||||||
|
});
|
||||||
|
const emailChangeTokens = await db.emailChangeToken.deleteMany({
|
||||||
|
where: { expiresAt: { lt: now } },
|
||||||
|
});
|
||||||
|
const unusedInviteTokens = await db.ruleStakeholder.updateMany({
|
||||||
|
where: {
|
||||||
|
inviteTokenHash: { not: null },
|
||||||
|
inviteExpiresAt: { lt: now },
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
inviteTokenHash: null,
|
||||||
|
inviteExpiresAt: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
const workshopInquiries = await deleteExpiredWorkshopInquiries(db, now);
|
||||||
|
|
||||||
|
return {
|
||||||
|
sessions: sessions.count,
|
||||||
|
magicLinkTokens: magicLinkTokens.count,
|
||||||
|
emailChangeTokens: emailChangeTokens.count,
|
||||||
|
unusedInviteTokens: unusedInviteTokens.count,
|
||||||
|
workshopInquiries,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -54,7 +54,6 @@ export async function createRuleStakeholderInviteAndSendMail(opts: {
|
|||||||
} catch (err) {
|
} catch (err) {
|
||||||
logRouteError(opts.scope, opts.requestId, err, {
|
logRouteError(opts.scope, opts.requestId, err, {
|
||||||
phase: "sendRuleStakeholderInviteEmail",
|
phase: "sendRuleStakeholderInviteEmail",
|
||||||
email: opts.email,
|
|
||||||
});
|
});
|
||||||
try {
|
try {
|
||||||
await prisma.ruleStakeholder.delete({ where: { id: row.id } });
|
await prisma.ruleStakeholder.delete({ where: { id: row.id } });
|
||||||
|
|||||||
@@ -21,11 +21,13 @@ import { hashSessionToken, newSessionToken } from "./hash";
|
|||||||
* 2. **Rotation: deferred.** No token rotation on privilege-sensitive
|
* 2. **Rotation: deferred.** No token rotation on privilege-sensitive
|
||||||
* actions in v1. Revisit if/when product requires it (ticket calls
|
* actions in v1. Revisit if/when product requires it (ticket calls
|
||||||
* this v1.1).
|
* 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])`).
|
* signing user's own expired rows (cheap — uses `@@index([userId])`).
|
||||||
* A small fraction of sign-ins (`SESSION_GLOBAL_PRUNE_PROB`) also runs
|
* 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
|
* 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.
|
* sign-in itself.
|
||||||
* 4. **Email change (CR-103).** After a verified email update, revoke every
|
* 4. **Email change (CR-103).** After a verified email update, revoke every
|
||||||
* `Session` for that `userId` **except** the current browser's session when
|
* `Session` for that `userId` **except** the current browser's session when
|
||||||
|
|||||||
@@ -7,11 +7,11 @@ import {
|
|||||||
import { rateLimitKey } from "./rateLimit";
|
import { rateLimitKey } from "./rateLimit";
|
||||||
import { logger } from "../logger";
|
import { logger } from "../logger";
|
||||||
import {
|
import {
|
||||||
WORKSHOP_INQUIRY_RETENTION_MS,
|
|
||||||
WORKSHOP_MIN_FILL_MS,
|
WORKSHOP_MIN_FILL_MS,
|
||||||
suggestWorkshopRoute,
|
suggestWorkshopRoute,
|
||||||
type WorkshopFormValues,
|
type WorkshopFormValues,
|
||||||
} from "../workshopSignup";
|
} from "../workshopSignup";
|
||||||
|
import { deleteExpiredWorkshopInquiries } from "./pruneExpiredRecords";
|
||||||
import {
|
import {
|
||||||
parseWorkshopSignupPayload,
|
parseWorkshopSignupPayload,
|
||||||
type ParsedWorkshopSignup,
|
type ParsedWorkshopSignup,
|
||||||
@@ -223,9 +223,5 @@ export async function retryPendingWorkshopInquiryMail(): Promise<number> {
|
|||||||
/** Delete inquiries older than the retention period. */
|
/** Delete inquiries older than the retention period. */
|
||||||
export async function purgeExpiredWorkshopInquiries(now = new Date()): Promise<number> {
|
export async function purgeExpiredWorkshopInquiries(now = new Date()): Promise<number> {
|
||||||
if (!isDatabaseConfigured()) return 0;
|
if (!isDatabaseConfigured()) return 0;
|
||||||
const cutoff = new Date(now.getTime() - WORKSHOP_INQUIRY_RETENTION_MS);
|
return deleteExpiredWorkshopInquiries(prisma, now);
|
||||||
const result = await prisma.workshopInquiry.deleteMany({
|
|
||||||
where: { createdAt: { lt: cutoff } },
|
|
||||||
});
|
|
||||||
return result.count;
|
|
||||||
}
|
}
|
||||||
@@ -12,9 +12,6 @@ export const WORKSHOP_STARTED_AT_FIELD = "startedAt";
|
|||||||
/** Ignore submits that arrive faster than a person can fill the form. */
|
/** Ignore submits that arrive faster than a person can fill the form. */
|
||||||
export const WORKSHOP_MIN_FILL_MS = 4_000;
|
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 = [
|
export const WORKSHOP_CHALLENGE_IDS = [
|
||||||
"members-rarely-vote",
|
"members-rarely-vote",
|
||||||
"preparing-the-board",
|
"preparing-the-board",
|
||||||
|
|||||||
+34
-14
@@ -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):
|
* 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 # 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
|
* Intentionally does NOT import `lib/server/session` — that module pulls in
|
||||||
* `next/headers` which requires a Next request context. For ad-hoc DB
|
* `next/headers` which requires a Next request context. For ad-hoc DB
|
||||||
* surgery we talk to Prisma directly.
|
* surgery we talk to Prisma directly.
|
||||||
*/
|
*/
|
||||||
import { PrismaClient } from "@prisma/client";
|
import { PrismaClient } from "@prisma/client";
|
||||||
|
import { pruneExpiredRecords } from "../lib/server/pruneExpiredRecords";
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
const userId = process.argv[2];
|
const userId = process.argv[2];
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
const before = await prisma.session.count();
|
|
||||||
const { count } = await prisma.session.deleteMany({
|
if (userId) {
|
||||||
where: {
|
const before = await prisma.session.count({ where: { userId } });
|
||||||
expiresAt: { lt: now },
|
const { count } = await prisma.session.deleteMany({
|
||||||
...(userId ? { userId } : {}),
|
where: {
|
||||||
},
|
expiresAt: { lt: now },
|
||||||
});
|
userId,
|
||||||
const after = await prisma.session.count();
|
},
|
||||||
|
});
|
||||||
|
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(
|
console.log(
|
||||||
JSON.stringify(
|
JSON.stringify(
|
||||||
{
|
{
|
||||||
scope: userId ?? "global",
|
scope: "global",
|
||||||
now: now.toISOString(),
|
now: now.toISOString(),
|
||||||
sessionsBefore: before,
|
...result,
|
||||||
pruned: count,
|
|
||||||
sessionsAfter: after,
|
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
2,
|
2,
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ vi.mock("../../lib/server/rateLimit", () => ({
|
|||||||
rateLimitKey: () => ({ ok: true }),
|
rateLimitKey: () => ({ ok: true }),
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
import { logger } from "../../lib/logger";
|
||||||
import { POST } from "../../app/api/auth/magic-link/request/route";
|
import { POST } from "../../app/api/auth/magic-link/request/route";
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
@@ -185,4 +186,29 @@ describe("POST /api/auth/magic-link/request", () => {
|
|||||||
expect(body.error.code).toBe("mail_failed");
|
expect(body.error.code).toBe("mail_failed");
|
||||||
expect(deleteManyMock).toHaveBeenCalledTimes(2);
|
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();
|
||||||
|
}
|
||||||
|
});
|
||||||
});
|
});
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
import {
|
import {
|
||||||
getDatabaseUrl,
|
getDatabaseUrl,
|
||||||
|
getOpsPruneToken,
|
||||||
getSmtpUrl,
|
getSmtpUrl,
|
||||||
isDatabaseConfigured,
|
isDatabaseConfigured,
|
||||||
} from "../../lib/server/env";
|
} from "../../lib/server/env";
|
||||||
@@ -11,6 +12,7 @@ const ENV_KEYS = [
|
|||||||
"CLOUDRON_MAIL_SMTP_PORT",
|
"CLOUDRON_MAIL_SMTP_PORT",
|
||||||
"CLOUDRON_MAIL_SMTP_USERNAME",
|
"CLOUDRON_MAIL_SMTP_USERNAME",
|
||||||
"CLOUDRON_MAIL_SMTP_PASSWORD",
|
"CLOUDRON_MAIL_SMTP_PASSWORD",
|
||||||
|
"OPS_PRUNE_TOKEN",
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
const ORIGINAL_ENV = Object.fromEntries(
|
const ORIGINAL_ENV = Object.fromEntries(
|
||||||
@@ -97,3 +99,16 @@ describe("getSmtpUrl", () => {
|
|||||||
expect(getSmtpUrl()).toBe("smtp://apikey:@smtp.example.com:25");
|
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");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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" }),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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");
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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/);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -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",
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user