From 351bbe13cdaee80ffca8cdb053fd3e9fb5d15530 Mon Sep 17 00:00:00 2001 From: diubi Date: Thu, 22 Oct 2026 01:37:35 +0200 Subject: [PATCH 01/12] feat(audit): record every change in a central audit log Every mutation goes through a tRPC audit middleware that stores who made it, the input, the outcome (success, failure or denied) and when in common_audit_events. Handlers add the target and the state before and after through ctx.audit. Secrets and file contents are never stored. Telegram moderation reported by the bot is recorded as done by the moderator, and the migration copies the existing tg_audit_log. Dashboard users with audit:read list the log with audit.list; the legacy anonymous path reads it only in development, for the dashboard's agent mode. Co-Authored-By: Claude Opus 5.5 --- README.md | 17 + drizzle/0026_audit_events.sql | 43 + drizzle/meta/0026_snapshot.json | 3615 ++++++++++++++++++ drizzle/meta/_journal.json | 7 + src/audit/index.ts | 201 + src/audit/names.ts | 53 + src/audit/store.ts | 7 + src/db/schema/common/audit-events.ts | 59 + src/db/schema/common/index.ts | 2 + src/esign/service.ts | 5 + src/routers/audit.ts | 82 + src/routers/auth.ts | 4 +- src/routers/azure/groups.ts | 6 +- src/routers/azure/members.ts | 9 +- src/routers/esign.ts | 81 +- src/routers/groups/labels.ts | 16 +- src/routers/index.ts | 2 + src/routers/tg/audit-log.ts | 47 +- src/routers/tg/grants.ts | 11 +- src/routers/tg/groups.ts | 54 +- src/routers/tg/permissions.ts | 20 +- src/routers/wa/groups.ts | 22 +- src/routers/web/associations.ts | 16 +- src/routers/web/faqs.ts | 16 +- src/routers/web/guides_matricole.ts | 4 +- src/routers/web/projects.ts | 20 +- src/routers/web/reports.ts | 32 +- src/trpc.ts | 30 +- tests/audit-json.test.ts | 34 + tests/azure-router.test.ts | 11 + tests/group-label-platform-isolation.test.ts | 5 +- tests/idp-router.test.ts | 104 + 32 files changed, 4577 insertions(+), 58 deletions(-) create mode 100644 drizzle/0026_audit_events.sql create mode 100644 drizzle/meta/0026_snapshot.json create mode 100644 src/audit/index.ts create mode 100644 src/audit/names.ts create mode 100644 src/audit/store.ts create mode 100644 src/db/schema/common/audit-events.ts create mode 100644 src/routers/audit.ts create mode 100644 tests/audit-json.test.ts diff --git a/README.md b/README.md index 8a3ab0b..2af61fd 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,23 @@ The bot's socket.io client authenticates with its service token in `auth.token` The IdP notifies `POST /internal/events` when access changes; the backend also polls the snapshot every 30 s. The last good signing keys and snapshot are stored in `common_idp_state`, so a restart during an IdP outage resumes from them. Permission checks deny once the snapshot is more than an hour old. Configuration is in `.env.example`. +## Audit log + +Every mutation is recorded in `common_audit_events` by the tRPC audit middleware (`src/audit`): the action (the procedure +path), the caller from the token (or `legacy` with the Telegram ID the legacy caller sent), the input, the outcome +(`success`, `failure` when an error is thrown or returned, `denied` when the policy refuses a token caller) and when. +Handlers add the target and the state before and after with `ctx.audit.set(...)`, or `ctx.audit.skip()` when a call +changed nothing. Inputs and states are stored without secrets (`password`, `token`, `ticket`, `code`, `*key` fields) or +file contents. Calls refused before the caller is known (no token on a closed legacy path, an invalid token) and the +bot's ingestion (`tg.messages.add`, `tg.users.add`) are not recorded. + +Telegram moderation the bot reports through `tg.auditLog` is recorded as `tg.moderation.`, done by the moderator; +the migration copies the existing `tg_audit_log`. Dashboard users with the IdP permission `audit:read` read the log with +`audit.list`, newest first, paged by `cursor` and filtered by area, outcome, target or text. + +Changes made outside the backend are not in the log: directly in Telegram or WhatsApp unless the bot reports them, the +cron jobs, the e-signing agent endpoints (each request keeps its own history) and roles and permissions in the IdP. + ## E-signing with the CIE The `esign` router and `/esign/agent/*` endpoints let dashboard users sign PDFs with the Italian identity card diff --git a/drizzle/0026_audit_events.sql b/drizzle/0026_audit_events.sql new file mode 100644 index 0000000..98941ab --- /dev/null +++ b/drizzle/0026_audit_events.sql @@ -0,0 +1,43 @@ +CREATE TABLE "common_audit_events" ( + "id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "common_audit_events_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1), + "action" text NOT NULL, + "actor_kind" varchar(16) NOT NULL, + "actor_sub" text, + "actor_tg_id" bigint, + "actor_client" text, + "target_type" text, + "target_id" text, + "target_label" text, + "reason" text, + "outcome" varchar(16) NOT NULL, + "error" text, + "input" jsonb, + "before" jsonb, + "after" jsonb, + "created_at" timestamp (3) with time zone DEFAULT now() NOT NULL +); +--> statement-breakpoint +CREATE INDEX "common_audit_events_action_idx" ON "common_audit_events" USING btree ("action");--> statement-breakpoint +CREATE INDEX "common_audit_events_actor_sub_idx" ON "common_audit_events" USING btree ("actor_sub");--> statement-breakpoint +CREATE INDEX "common_audit_events_actor_tg_id_idx" ON "common_audit_events" USING btree ("actor_tg_id");--> statement-breakpoint +CREATE INDEX "common_audit_events_target_idx" ON "common_audit_events" USING btree ("target_type","target_id");--> statement-breakpoint +-- Telegram moderation recorded before this table existed, oldest first so that ids follow time. +INSERT INTO "common_audit_events" ("action", "actor_kind", "actor_sub", "actor_tg_id", "actor_client", "target_type", "target_id", "reason", "outcome", "after", "created_at") +SELECT + 'tg.moderation.' || "type", + CASE + WHEN "client" IS NULL THEN 'legacy' + WHEN coalesce("actor_tg_id", "admin_id") IS NOT NULL THEN 'telegram' + ELSE 'service' + END, + "actor_sub", + coalesce("actor_tg_id", "admin_id"), + "client", + 'tg.user', + "target_id"::text, + "reason", + 'success', + jsonb_build_object('groupId', "group_id", 'until', "until", 'basis', "basis", 'review', "review"), + "created_at" +FROM "tg_audit_log" +ORDER BY "created_at", "id"; diff --git a/drizzle/meta/0026_snapshot.json b/drizzle/meta/0026_snapshot.json new file mode 100644 index 0000000..6a983b4 --- /dev/null +++ b/drizzle/meta/0026_snapshot.json @@ -0,0 +1,3615 @@ +{ + "id": "2538705f-3293-46b3-a123-4da1a2ddb48a", + "prevId": "33542854-3513-411e-af76-254bf84afbaa", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.auth_accounts": { + "name": "auth_accounts", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "account_id": { + "name": "account_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "provider_id": { + "name": "provider_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "access_token": { + "name": "access_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "refresh_token": { + "name": "refresh_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "id_token": { + "name": "id_token", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "access_token_expires_at": { + "name": "access_token_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "refresh_token_expires_at": { + "name": "refresh_token_expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "scope": { + "name": "scope", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "password": { + "name": "password", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "account_userId_idx": { + "name": "account_userId_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "auth_accounts_user_id_auth_users_id_fk": { + "name": "auth_accounts_user_id_auth_users_id_fk", + "tableFrom": "auth_accounts", + "tableTo": "auth_users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_passkey": { + "name": "auth_passkey", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "public_key": { + "name": "public_key", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "credential_id": { + "name": "credential_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "counter": { + "name": "counter", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "device_type": { + "name": "device_type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "backed_up": { + "name": "backed_up", + "type": "boolean", + "primaryKey": false, + "notNull": true + }, + "transports": { + "name": "transports", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": false + }, + "aaguid": { + "name": "aaguid", + "type": "text", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "passkey_userId_idx": { + "name": "passkey_userId_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "passkey_credentialID_idx": { + "name": "passkey_credentialID_idx", + "columns": [ + { + "expression": "credential_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "auth_passkey_user_id_auth_users_id_fk": { + "name": "auth_passkey_user_id_auth_users_id_fk", + "tableFrom": "auth_passkey", + "tableTo": "auth_users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_sessions": { + "name": "auth_sessions", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "token": { + "name": "token", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "ip_address": { + "name": "ip_address", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_agent": { + "name": "user_agent", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "session_userId_idx": { + "name": "session_userId_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "auth_sessions_user_id_auth_users_id_fk": { + "name": "auth_sessions_user_id_auth_users_id_fk", + "tableFrom": "auth_sessions", + "tableTo": "auth_users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "auth_sessions_token_unique": { + "name": "auth_sessions_token_unique", + "nullsNotDistinct": false, + "columns": [ + "token" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_users": { + "name": "auth_users", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email_verified": { + "name": "email_verified", + "type": "boolean", + "primaryKey": false, + "notNull": true + }, + "image": { + "name": "image", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "tg_id": { + "name": "tg_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "tg_username": { + "name": "tg_username", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "auth_users_email_unique": { + "name": "auth_users_email_unique", + "nullsNotDistinct": false, + "columns": [ + "email" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.auth_verifications": { + "name": "auth_verifications", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "identifier": { + "name": "identifier", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": { + "verification_identifier_idx": { + "name": "verification_identifier_idx", + "columns": [ + { + "expression": "identifier", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.common_audit_events": { + "name": "common_audit_events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "bigint", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "common_audit_events_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "9223372036854775807", + "cache": "1", + "cycle": false + } + }, + "action": { + "name": "action", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "actor_kind": { + "name": "actor_kind", + "type": "varchar(16)", + "primaryKey": false, + "notNull": true + }, + "actor_sub": { + "name": "actor_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "actor_tg_id": { + "name": "actor_tg_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "actor_client": { + "name": "actor_client", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "target_type": { + "name": "target_type", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "target_id": { + "name": "target_id", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "target_label": { + "name": "target_label", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "outcome": { + "name": "outcome", + "type": "varchar(16)", + "primaryKey": false, + "notNull": true + }, + "error": { + "name": "error", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "input": { + "name": "input", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "before": { + "name": "before", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "after": { + "name": "after", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "common_audit_events_action_idx": { + "name": "common_audit_events_action_idx", + "columns": [ + { + "expression": "action", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "common_audit_events_actor_sub_idx": { + "name": "common_audit_events_actor_sub_idx", + "columns": [ + { + "expression": "actor_sub", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "common_audit_events_actor_tg_id_idx": { + "name": "common_audit_events_actor_tg_id_idx", + "columns": [ + { + "expression": "actor_tg_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "common_audit_events_target_idx": { + "name": "common_audit_events_target_idx", + "columns": [ + { + "expression": "target_type", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "target_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.common_group_labels": { + "name": "common_group_labels", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "common_group_labels_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "label": { + "name": "label", + "type": "varchar(128)", + "primaryKey": false, + "notNull": true + }, + "description": { + "name": "description", + "type": "varchar", + "primaryKey": false, + "notNull": false + }, + "color": { + "name": "color", + "type": "varchar(7)", + "primaryKey": false, + "notNull": true, + "default": "'#ffffff'" + }, + "granted_by_id": { + "name": "granted_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "modified_by_id": { + "name": "modified_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "modified_by_sub": { + "name": "modified_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "common_group_labels_granted_by_id_tg_permissions_user_id_fk": { + "name": "common_group_labels_granted_by_id_tg_permissions_user_id_fk", + "tableFrom": "common_group_labels", + "tableTo": "tg_permissions", + "columnsFrom": [ + "granted_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "common_group_labels_modified_by_id_tg_permissions_user_id_fk": { + "name": "common_group_labels_modified_by_id_tg_permissions_user_id_fk", + "tableFrom": "common_group_labels", + "tableTo": "tg_permissions", + "columnsFrom": [ + "modified_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "common_group_labels_label_unique": { + "name": "common_group_labels_label_unique", + "nullsNotDistinct": false, + "columns": [ + "label" + ] + } + }, + "policies": {}, + "checkConstraints": { + "common_group_labels_creator_check": { + "name": "common_group_labels_creator_check", + "value": "\"common_group_labels\".\"granted_by_id\" IS NOT NULL OR \"common_group_labels\".\"created_by_sub\" IS NOT NULL" + } + }, + "isRLSEnabled": false + }, + "public.common_idp_state": { + "name": "common_idp_state", + "schema": "", + "columns": { + "key": { + "name": "key", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "value": { + "name": "value", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.esign_documents": { + "name": "esign_documents", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "request_id": { + "name": "request_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, + "version": { + "name": "version", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "file_name": { + "name": "file_name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "content": { + "name": "content", + "type": "bytea", + "primaryKey": false, + "notNull": true + }, + "sha256": { + "name": "sha256", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "size": { + "name": "size", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "pages": { + "name": "pages", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "source": { + "name": "source", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "esign_documents_request_version_idx": { + "name": "esign_documents_request_version_idx", + "columns": [ + { + "expression": "request_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "version", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "where": "\"esign_documents\".\"request_id\" IS NOT NULL", + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "esign_documents_request_id_esign_requests_id_fk": { + "name": "esign_documents_request_id_esign_requests_id_fk", + "tableFrom": "esign_documents", + "tableTo": "esign_requests", + "columnsFrom": [ + "request_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.esign_drive_access": { + "name": "esign_drive_access", + "schema": "", + "columns": { + "drive_id": { + "name": "drive_id", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "request_roles": { + "name": "request_roles", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "request_permissions": { + "name": "request_permissions", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "sign_roles": { + "name": "sign_roles", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "sign_permissions": { + "name": "sign_permissions", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "updated_by_sub": { + "name": "updated_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.esign_events": { + "name": "esign_events", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "serial", + "primaryKey": true, + "notNull": true + }, + "request_id": { + "name": "request_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "at": { + "name": "at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + }, + "actor_sub": { + "name": "actor_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "data": { + "name": "data", + "type": "jsonb", + "primaryKey": false, + "notNull": false + } + }, + "indexes": { + "esign_events_request_idx": { + "name": "esign_events_request_idx", + "columns": [ + { + "expression": "request_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "esign_events_request_id_esign_requests_id_fk": { + "name": "esign_events_request_id_esign_requests_id_fk", + "tableFrom": "esign_events", + "tableTo": "esign_requests", + "columnsFrom": [ + "request_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.esign_jobs": { + "name": "esign_jobs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "ticket_hash": { + "name": "ticket_hash", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "request_id": { + "name": "request_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "signer_id": { + "name": "signer_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "sub": { + "name": "sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "base_version": { + "name": "base_version", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "places": { + "name": "places", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "step": { + "name": "step", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "expires_at": { + "name": "expires_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": true + }, + "prepared": { + "name": "prepared", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "prepared_pdf": { + "name": "prepared_pdf", + "type": "bytea", + "primaryKey": false, + "notNull": false + }, + "error_code": { + "name": "error_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "error_message": { + "name": "error_message", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "seen_at": { + "name": "seen_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": false + }, + "completed_at": { + "name": "completed_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "esign_jobs_request_idx": { + "name": "esign_jobs_request_idx", + "columns": [ + { + "expression": "request_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "esign_jobs_request_id_esign_requests_id_fk": { + "name": "esign_jobs_request_id_esign_requests_id_fk", + "tableFrom": "esign_jobs", + "tableTo": "esign_requests", + "columnsFrom": [ + "request_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "esign_jobs_signer_id_esign_signers_id_fk": { + "name": "esign_jobs_signer_id_esign_signers_id_fk", + "tableFrom": "esign_jobs", + "tableTo": "esign_signers", + "columnsFrom": [ + "signer_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "esign_jobs_ticket_hash_unique": { + "name": "esign_jobs_ticket_hash_unique", + "nullsNotDistinct": false, + "columns": [ + "ticket_hash" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.esign_profiles": { + "name": "esign_profiles", + "schema": "", + "columns": { + "sub": { + "name": "sub", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "fiscal_code": { + "name": "fiscal_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "certificate_subject": { + "name": "certificate_subject", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.esign_requests": { + "name": "esign_requests", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'in_progress'" + }, + "ordered": { + "name": "ordered", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": true + }, + "output": { + "name": "output", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "delivery": { + "name": "delivery", + "type": "jsonb", + "primaryKey": false, + "notNull": false + }, + "drives": { + "name": "drives", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{}'::text[]" + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "completed_at": { + "name": "completed_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": false + }, + "cancelled_at": { + "name": "cancelled_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "esign_requests_status_idx": { + "name": "esign_requests_status_idx", + "columns": [ + { + "expression": "status", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "esign_requests_created_by_idx": { + "name": "esign_requests_created_by_idx", + "columns": [ + { + "expression": "created_by_sub", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.esign_signers": { + "name": "esign_signers", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "uuid", + "primaryKey": true, + "notNull": true, + "default": "gen_random_uuid()" + }, + "request_id": { + "name": "request_id", + "type": "uuid", + "primaryKey": false, + "notNull": true + }, + "position": { + "name": "position", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "sub": { + "name": "sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "role": { + "name": "role", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "places": { + "name": "places", + "type": "jsonb", + "primaryKey": false, + "notNull": true + }, + "signatures_done": { + "name": "signatures_done", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "status": { + "name": "status", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "signed_at": { + "name": "signed_at", + "type": "timestamp (3) with time zone", + "primaryKey": false, + "notNull": false + }, + "version": { + "name": "version", + "type": "integer", + "primaryKey": false, + "notNull": false + }, + "fiscal_code": { + "name": "fiscal_code", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "certificate_subject": { + "name": "certificate_subject", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "certificate_issuer": { + "name": "certificate_issuer", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "decline_reason": { + "name": "decline_reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "esign_signers_request_idx": { + "name": "esign_signers_request_idx", + "columns": [ + { + "expression": "request_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "esign_signers_sub_idx": { + "name": "esign_signers_sub_idx", + "columns": [ + { + "expression": "sub", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "esign_signers_request_position_idx": { + "name": "esign_signers_request_position_idx", + "columns": [ + { + "expression": "request_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "position", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "esign_signers_request_id_esign_requests_id_fk": { + "name": "esign_signers_request_id_esign_requests_id_fk", + "tableFrom": "esign_signers", + "tableTo": "esign_requests", + "columnsFrom": [ + "request_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_audit_log": { + "name": "tg_audit_log", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "tg_audit_log_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "admin_id": { + "name": "admin_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "target_id": { + "name": "target_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "group_id": { + "name": "group_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "varchar(32)", + "primaryKey": false, + "notNull": true + }, + "until": { + "name": "until", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "reason": { + "name": "reason", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "actor_sub": { + "name": "actor_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "actor_tg_id": { + "name": "actor_tg_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "client": { + "name": "client", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "basis": { + "name": "basis", + "type": "varchar(32)", + "primaryKey": false, + "notNull": true, + "default": "'legacy'" + }, + "review": { + "name": "review", + "type": "varchar(32)", + "primaryKey": false, + "notNull": false + }, + "idempotency_key": { + "name": "idempotency_key", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "auditlog_adminid_idx": { + "name": "auditlog_adminid_idx", + "columns": [ + { + "expression": "admin_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "auditlog_actor_tg_id_idx": { + "name": "auditlog_actor_tg_id_idx", + "columns": [ + { + "expression": "actor_tg_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "auditlog_idempotency_key_idx": { + "name": "auditlog_idempotency_key_idx", + "columns": [ + { + "expression": "idempotency_key", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": true, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_grants": { + "name": "tg_grants", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "tg_grants_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "user_id": { + "name": "user_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "granted_by_id": { + "name": "granted_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "valid_since": { + "name": "valid_since", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true + }, + "valid_until": { + "name": "valid_until", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true + }, + "interrupted_by_id": { + "name": "interrupted_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "interrupted_at": { + "name": "interrupted_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tg_grants_user_id_idx": { + "name": "tg_grants_user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": { + "tg_grants_granted_by_id_tg_permissions_user_id_fk": { + "name": "tg_grants_granted_by_id_tg_permissions_user_id_fk", + "tableFrom": "tg_grants", + "tableTo": "tg_permissions", + "columnsFrom": [ + "granted_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "tg_grants_interrupted_by_id_tg_permissions_user_id_fk": { + "name": "tg_grants_interrupted_by_id_tg_permissions_user_id_fk", + "tableFrom": "tg_grants", + "tableTo": "tg_permissions", + "columnsFrom": [ + "interrupted_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_grants_v2": { + "name": "tg_grants_v2", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "tg_grants_v2_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "telegram_user_id": { + "name": "telegram_user_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "valid_since": { + "name": "valid_since", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true + }, + "valid_until": { + "name": "valid_until", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true + }, + "reason": { + "name": "reason", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "granted_by_sub": { + "name": "granted_by_sub", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "granted_via_client": { + "name": "granted_via_client", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "interrupted_at": { + "name": "interrupted_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "interrupted_by_sub": { + "name": "interrupted_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "interrupted_by_tg_id": { + "name": "interrupted_by_tg_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tg_grants_v2_telegram_user_id_idx": { + "name": "tg_grants_v2_telegram_user_id_idx", + "columns": [ + { + "expression": "telegram_user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "tg_grants_v2_validity_check": { + "name": "tg_grants_v2_validity_check", + "value": "\"tg_grants_v2\".\"valid_since\" < \"tg_grants_v2\".\"valid_until\"" + } + }, + "isRLSEnabled": false + }, + "public.tg_groups": { + "name": "tg_groups", + "schema": "", + "columns": { + "telegram_id": { + "name": "telegram_id", + "type": "bigint", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "tag": { + "name": "tag", + "type": "varchar", + "primaryKey": false, + "notNull": false + }, + "link": { + "name": "link", + "type": "varchar(128)", + "primaryKey": false, + "notNull": false + }, + "hide": { + "name": "hide", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "tg_groups_link_unique": { + "name": "tg_groups_link_unique", + "nullsNotDistinct": false, + "columns": [ + "link" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_group_label_relations": { + "name": "tg_group_label_relations", + "schema": "", + "columns": { + "group_id": { + "name": "group_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "label_id": { + "name": "label_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "tg_group_label_relations_group_id_tg_groups_telegram_id_fk": { + "name": "tg_group_label_relations_group_id_tg_groups_telegram_id_fk", + "tableFrom": "tg_group_label_relations", + "tableTo": "tg_groups", + "columnsFrom": [ + "group_id" + ], + "columnsTo": [ + "telegram_id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "tg_group_label_relations_label_id_common_group_labels_id_fk": { + "name": "tg_group_label_relations_label_id_common_group_labels_id_fk", + "tableFrom": "tg_group_label_relations", + "tableTo": "common_group_labels", + "columnsFrom": [ + "label_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "tg_group_label_relations_group_id_label_id_pk": { + "name": "tg_group_label_relations_group_id_label_id_pk", + "columns": [ + "group_id", + "label_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_link": { + "name": "tg_link", + "schema": "", + "columns": { + "code": { + "name": "code", + "type": "text", + "primaryKey": true, + "notNull": true + }, + "ttl": { + "name": "ttl", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "user_id": { + "name": "user_id", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "tg_username": { + "name": "tg_username", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "tg_id": { + "name": "tg_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "tg_link_user_id_auth_users_id_fk": { + "name": "tg_link_user_id_auth_users_id_fk", + "tableFrom": "tg_link", + "tableTo": "auth_users", + "columnsFrom": [ + "user_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "tg_link_tg_id_unique": { + "name": "tg_link_tg_id_unique", + "nullsNotDistinct": false, + "columns": [ + "tg_id" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_messages": { + "name": "tg_messages", + "schema": "", + "columns": { + "chat_id": { + "name": "chat_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "message_id": { + "name": "message_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "author_id": { + "name": "author_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "timestamp": { + "name": "timestamp", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true + }, + "message": { + "name": "message", + "type": "varchar(8704)", + "primaryKey": false, + "notNull": true + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "tg_messages_chat_author_timestamp_idx": { + "name": "tg_messages_chat_author_timestamp_idx", + "columns": [ + { + "expression": "chat_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "author_id", + "isExpression": false, + "asc": true, + "nulls": "last" + }, + { + "expression": "timestamp", + "isExpression": false, + "asc": false, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": { + "tg_messages_chat_id_message_id_pk": { + "name": "tg_messages_chat_id_message_id_pk", + "columns": [ + "chat_id", + "message_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_group_admins": { + "name": "tg_group_admins", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "group_id": { + "name": "group_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "added_by_id": { + "name": "added_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": { + "user_id_idx": { + "name": "user_id_idx", + "columns": [ + { + "expression": "user_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + } + }, + "foreignKeys": {}, + "compositePrimaryKeys": { + "tg_group_admins_user_id_group_id_pk": { + "name": "tg_group_admins_user_id_group_id_pk", + "columns": [ + "user_id", + "group_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_permissions": { + "name": "tg_permissions", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "bigint", + "primaryKey": true, + "notNull": true + }, + "roles": { + "name": "roles", + "type": "text[]", + "primaryKey": false, + "notNull": true, + "default": "'{\"admin\"}'" + }, + "added_by_id": { + "name": "added_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "modified_by_id": { + "name": "modified_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_test": { + "name": "tg_test", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "tg_test_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "text": { + "name": "text", + "type": "varchar", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.tg_users": { + "name": "tg_users", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "bigint", + "primaryKey": true, + "notNull": true + }, + "first_name": { + "name": "first_name", + "type": "varchar(512)", + "primaryKey": false, + "notNull": true + }, + "last_name": { + "name": "last_name", + "type": "varchar(512)", + "primaryKey": false, + "notNull": false + }, + "username": { + "name": "username", + "type": "varchar(128)", + "primaryKey": false, + "notNull": false + }, + "is_bot": { + "name": "is_bot", + "type": "boolean", + "primaryKey": false, + "notNull": true + }, + "lang_code": { + "name": "lang_code", + "type": "varchar(35)", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.wa_group_label_relations": { + "name": "wa_group_label_relations", + "schema": "", + "columns": { + "group_id": { + "name": "group_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "label_id": { + "name": "label_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "wa_group_label_relations_group_id_wa_groups_group_id_fk": { + "name": "wa_group_label_relations_group_id_wa_groups_group_id_fk", + "tableFrom": "wa_group_label_relations", + "tableTo": "wa_groups", + "columnsFrom": [ + "group_id" + ], + "columnsTo": [ + "group_id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "wa_group_label_relations_label_id_common_group_labels_id_fk": { + "name": "wa_group_label_relations_label_id_common_group_labels_id_fk", + "tableFrom": "wa_group_label_relations", + "tableTo": "common_group_labels", + "columnsFrom": [ + "label_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": { + "wa_group_label_relations_group_id_label_id_pk": { + "name": "wa_group_label_relations_group_id_label_id_pk", + "columns": [ + "group_id", + "label_id" + ] + } + }, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.wa_groups": { + "name": "wa_groups", + "schema": "", + "columns": { + "group_id": { + "name": "group_id", + "type": "bigint", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "wa_groups_group_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "9223372036854775807", + "cache": "1", + "cycle": false + } + }, + "title": { + "name": "title", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "link": { + "name": "link", + "type": "varchar(128)", + "primaryKey": false, + "notNull": false + }, + "hide": { + "name": "hide", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "wa_groups_link_unique": { + "name": "wa_groups_link_unique", + "nullsNotDistinct": false, + "columns": [ + "link" + ] + } + }, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.web_associations": { + "name": "web_associations", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "web_associations_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "name": { + "name": "name", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description_it": { + "name": "description_it", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description_en": { + "name": "description_en", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "logo": { + "name": "logo", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "email": { + "name": "email", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "website": { + "name": "website", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "facebook": { + "name": "facebook", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "instagram": { + "name": "instagram", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "tiktok": { + "name": "tiktok", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "x": { + "name": "x", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "youtube": { + "name": "youtube", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "telegram": { + "name": "telegram", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "linkedin": { + "name": "linkedin", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "spotify": { + "name": "spotify", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_id": { + "name": "created_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "modified_by_id": { + "name": "modified_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "modified_by_sub": { + "name": "modified_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "web_associations_created_by_id_tg_permissions_user_id_fk": { + "name": "web_associations_created_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_associations", + "tableTo": "tg_permissions", + "columnsFrom": [ + "created_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "web_associations_modified_by_id_tg_permissions_user_id_fk": { + "name": "web_associations_modified_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_associations", + "tableTo": "tg_permissions", + "columnsFrom": [ + "modified_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "web_associations_creator_check": { + "name": "web_associations_creator_check", + "value": "\"web_associations\".\"created_by_id\" IS NOT NULL OR \"web_associations\".\"created_by_sub\" IS NOT NULL" + } + }, + "isRLSEnabled": false + }, + "public.web_faq_categories": { + "name": "web_faq_categories", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "web_faq_categories_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "title_it": { + "name": "title_it", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "title_en": { + "name": "title_en", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "icon": { + "name": "icon", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "created_by_id": { + "name": "created_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "modified_by_id": { + "name": "modified_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "modified_by_sub": { + "name": "modified_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "web_faq_categories_created_by_id_tg_permissions_user_id_fk": { + "name": "web_faq_categories_created_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_faq_categories", + "tableTo": "tg_permissions", + "columnsFrom": [ + "created_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "web_faq_categories_modified_by_id_tg_permissions_user_id_fk": { + "name": "web_faq_categories_modified_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_faq_categories", + "tableTo": "tg_permissions", + "columnsFrom": [ + "modified_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "web_faq_categories_creator_check": { + "name": "web_faq_categories_creator_check", + "value": "\"web_faq_categories\".\"created_by_id\" IS NOT NULL OR \"web_faq_categories\".\"created_by_sub\" IS NOT NULL" + } + }, + "isRLSEnabled": false + }, + "public.web_faqs": { + "name": "web_faqs", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "web_faqs_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "title_it": { + "name": "title_it", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "title_en": { + "name": "title_en", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description_it": { + "name": "description_it", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description_en": { + "name": "description_en", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "category_id": { + "name": "category_id", + "type": "integer", + "primaryKey": false, + "notNull": true + }, + "created_by_id": { + "name": "created_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "modified_by_id": { + "name": "modified_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "modified_by_sub": { + "name": "modified_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "web_faqs_category_id_web_faq_categories_id_fk": { + "name": "web_faqs_category_id_web_faq_categories_id_fk", + "tableFrom": "web_faqs", + "tableTo": "web_faq_categories", + "columnsFrom": [ + "category_id" + ], + "columnsTo": [ + "id" + ], + "onDelete": "cascade", + "onUpdate": "no action" + }, + "web_faqs_created_by_id_tg_permissions_user_id_fk": { + "name": "web_faqs_created_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_faqs", + "tableTo": "tg_permissions", + "columnsFrom": [ + "created_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "web_faqs_modified_by_id_tg_permissions_user_id_fk": { + "name": "web_faqs_modified_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_faqs", + "tableTo": "tg_permissions", + "columnsFrom": [ + "modified_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "web_faqs_creator_check": { + "name": "web_faqs_creator_check", + "value": "\"web_faqs\".\"created_by_id\" IS NOT NULL OR \"web_faqs\".\"created_by_sub\" IS NOT NULL" + } + }, + "isRLSEnabled": false + }, + "public.web_group_link_reports": { + "name": "web_group_link_reports", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "web_group_link_reports_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "group_id": { + "name": "group_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "type": { + "name": "type", + "type": "varchar(2)", + "primaryKey": false, + "notNull": false + }, + "report_type": { + "name": "report_type", + "type": "varchar(32)", + "primaryKey": false, + "notNull": true + }, + "reported_link": { + "name": "reported_link", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "label": { + "name": "label", + "type": "varchar(256)", + "primaryKey": false, + "notNull": false + }, + "details": { + "name": "details", + "type": "varchar(500)", + "primaryKey": false, + "notNull": false + }, + "status": { + "name": "status", + "type": "varchar(16)", + "primaryKey": false, + "notNull": true, + "default": "'pending'" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + }, + "public.web_guides_matricole": { + "name": "web_guides_matricole", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "web_guides_matricole_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "version": { + "name": "version", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "date": { + "name": "date", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "file": { + "name": "file", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "created_by_id": { + "name": "created_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "modified_by_id": { + "name": "modified_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "modified_by_sub": { + "name": "modified_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "web_guides_matricole_created_by_id_tg_permissions_user_id_fk": { + "name": "web_guides_matricole_created_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_guides_matricole", + "tableTo": "tg_permissions", + "columnsFrom": [ + "created_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "web_guides_matricole_modified_by_id_tg_permissions_user_id_fk": { + "name": "web_guides_matricole_modified_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_guides_matricole", + "tableTo": "tg_permissions", + "columnsFrom": [ + "modified_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": { + "web_guides_matricole_version_unique": { + "name": "web_guides_matricole_version_unique", + "nullsNotDistinct": false, + "columns": [ + "version" + ] + } + }, + "policies": {}, + "checkConstraints": { + "web_guides_matricole_creator_check": { + "name": "web_guides_matricole_creator_check", + "value": "\"web_guides_matricole\".\"created_by_id\" IS NOT NULL OR \"web_guides_matricole\".\"created_by_sub\" IS NOT NULL" + } + }, + "isRLSEnabled": false + }, + "public.web_projects": { + "name": "web_projects", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "web_projects_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "title": { + "name": "title", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description_it": { + "name": "description_it", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "description_en": { + "name": "description_en", + "type": "text", + "primaryKey": false, + "notNull": true + }, + "logo": { + "name": "logo", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "link": { + "name": "link", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "category": { + "name": "category", + "type": "text", + "primaryKey": false, + "notNull": true, + "default": "'general'" + }, + "order": { + "name": "order", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 0 + }, + "created_by_id": { + "name": "created_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "modified_by_id": { + "name": "modified_by_id", + "type": "bigint", + "primaryKey": false, + "notNull": false + }, + "created_by_sub": { + "name": "created_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "modified_by_sub": { + "name": "modified_by_sub", + "type": "text", + "primaryKey": false, + "notNull": false + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": false + }, + "created_at": { + "name": "created_at", + "type": "timestamp (0) with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": { + "web_projects_created_by_id_tg_permissions_user_id_fk": { + "name": "web_projects_created_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_projects", + "tableTo": "tg_permissions", + "columnsFrom": [ + "created_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + }, + "web_projects_modified_by_id_tg_permissions_user_id_fk": { + "name": "web_projects_modified_by_id_tg_permissions_user_id_fk", + "tableFrom": "web_projects", + "tableTo": "tg_permissions", + "columnsFrom": [ + "modified_by_id" + ], + "columnsTo": [ + "user_id" + ], + "onDelete": "no action", + "onUpdate": "no action" + } + }, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": { + "web_projects_creator_check": { + "name": "web_projects_creator_check", + "value": "\"web_projects\".\"created_by_id\" IS NOT NULL OR \"web_projects\".\"created_by_sub\" IS NOT NULL" + } + }, + "isRLSEnabled": false + }, + "public.web_test": { + "name": "web_test", + "schema": "", + "columns": { + "id": { + "name": "id", + "type": "integer", + "primaryKey": true, + "notNull": true, + "identity": { + "type": "always", + "name": "web_test_id_seq", + "schema": "public", + "increment": "1", + "startWith": "1", + "minValue": "1", + "maxValue": "2147483647", + "cache": "1", + "cycle": false + } + }, + "text": { + "name": "text", + "type": "varchar", + "primaryKey": false, + "notNull": true + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": { + "public.groups_view": { + "columns": { + "telegram_id": { + "name": "telegram_id", + "type": "bigint", + "primaryKey": true, + "notNull": true + }, + "title": { + "name": "title", + "type": "varchar", + "primaryKey": false, + "notNull": true + }, + "link": { + "name": "link", + "type": "varchar(128)", + "primaryKey": false, + "notNull": false + }, + "hide": { + "name": "hide", + "type": "boolean", + "primaryKey": false, + "notNull": true, + "default": false + } + }, + "definition": "(select \"telegram_id\", \"title\", \"link\", \"hide\", 'tg' as \"type\" from \"tg_groups\") union all (select \"group_id\", \"title\", \"link\", \"hide\", 'wa' as \"type\" from \"wa_groups\")", + "name": "groups_view", + "schema": "public", + "isExisting": false, + "materialized": false + }, + "public.group_label_relations_view": { + "columns": { + "group_id": { + "name": "group_id", + "type": "bigint", + "primaryKey": false, + "notNull": true + }, + "label_id": { + "name": "label_id", + "type": "integer", + "primaryKey": false, + "notNull": true + } + }, + "definition": "(select \"group_id\", \"label_id\", 'tg' as \"type\" from \"tg_group_label_relations\") union all (select \"group_id\", \"label_id\", 'wa' as \"type\" from \"wa_group_label_relations\")", + "name": "group_label_relations_view", + "schema": "public", + "isExisting": false, + "materialized": false + } + }, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} \ No newline at end of file diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index 3f0d13e..1db3206 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -183,6 +183,13 @@ "when": 1792237528329, "tag": "0025_esign_drive_access", "breakpoints": true + }, + { + "idx": 26, + "version": "7", + "when": 1792616705733, + "tag": "0026_audit_events", + "breakpoints": true } ] } \ No newline at end of file diff --git a/src/audit/index.ts b/src/audit/index.ts new file mode 100644 index 0000000..af10536 --- /dev/null +++ b/src/audit/index.ts @@ -0,0 +1,201 @@ +import type { TRPCError } from "@trpc/server" +import { + AUDIT_ACTOR_KIND, + AUDIT_OUTCOME, + type AuditJson, + type auditEvents, + type TAuditActorKind, + type TAuditOutcome, +} from "@/db/schema/common/audit-events" +import type { RequestAuth } from "@/idp/auth" + +export type AuditEvent = typeof auditEvents.$inferInsert + +/** What a change was made to; `label` is a readable name, kept as it was at the time. */ +export type AuditTarget = { type: string; id: string | number; label?: string | null } + +export type AuditActor = { kind: TAuditActorKind; sub?: string | null; tgId?: number | null; client?: string | null } + +/** What only the handler knows about its change. Each call adds to or replaces the previous fields. */ +export type AuditNote = { + /** Replaces the procedure path, e.g. `tg.moderation.ban` for moderation the bot reports. */ + action?: string + /** Replaces the caller, for changes the caller reports on someone else's behalf. */ + actor?: AuditActor + target?: AuditTarget + before?: unknown + after?: unknown + reason?: string | null +} + +export type AuditRecorder = { + set: (note: AuditNote) => void + /** Records nothing for this call, e.g. a retry that changed nothing. */ + skip: () => void +} + +/** Mutations that are not changes made by someone: the bot's high-volume ingestion and per-visit syncs. */ +export const UNAUDITED = new Set(["tg.messages.add", "tg.users.add", "esign.profile.sync"]) + +/** Handed to queries, which change nothing. */ +export const noAudit: AuditRecorder = { set: () => {}, skip: () => {} } + +export function auditRecorder() { + const note: AuditNote = {} + let skipped = false + const recorder: AuditRecorder = { + set: (next) => Object.assign(note, next), + skip: () => { + skipped = true + }, + } + return { recorder, note, skipped: () => skipped } +} + +const SECRET_KEY = /pass(word)?|secret|token|ticket|otp|code|key$/i +const MAX_STRING = 2000 +const MAX_ITEMS = 100 +const MAX_DEPTH = 8 + +/** + * A JSON copy of an input or a row, safe to keep: secrets are replaced, files and bytes are described instead of copied, + * and long strings and arrays are cut. + */ +export function auditJson(value: unknown, depth = 0): AuditJson { + if (value === null || value === undefined) return null + if (typeof value === "string") + return value.length > MAX_STRING ? `${value.slice(0, MAX_STRING)}… (${value.length} characters)` : value + if (typeof value === "number") return Number.isFinite(value) ? value : String(value) + if (typeof value === "boolean") return value + if (typeof value === "bigint") return value.toString() + if (value instanceof Date) return Number.isNaN(value.getTime()) ? null : value.toISOString() + if (value instanceof File) return { file: value.name, type: value.type, size: value.size } + if (value instanceof Blob) return { file: null, type: value.type, size: value.size } + if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return `(${value.byteLength} bytes)` + if (depth >= MAX_DEPTH) return "(nested too deep)" + if (value instanceof FormData) return auditJson(Object.fromEntries(value.entries()), depth) + if (value instanceof Map) return auditJson(Object.fromEntries(value), depth) + if (value instanceof Set) return auditJson([...value], depth) + if (Array.isArray(value)) { + const items = value.slice(0, MAX_ITEMS).map((item) => auditJson(item, depth + 1)) + return value.length > MAX_ITEMS ? [...items, `(${value.length - MAX_ITEMS} more)`] : items + } + if (typeof value === "object") { + const copy: { [key: string]: AuditJson } = {} + for (const [key, item] of Object.entries(value)) { + if (item === undefined || typeof item === "function") continue + copy[key] = SECRET_KEY.test(key) ? "(hidden)" : auditJson(item, depth + 1) + } + return copy + } + return null +} + +/** Input fields in which legacy callers name themselves. */ +const LEGACY_ACTOR_FIELDS = [ + "createdBy", + "modifiedBy", + "updatedBy", + "adderId", + "removerId", + "interruptedById", + "performerId", + "adminId", +] + +function legacyTelegramId(input: AuditJson): number | null { + if (input === null || typeof input !== "object" || Array.isArray(input)) return null + for (const field of LEGACY_ACTOR_FIELDS) { + const id = Number(input[field]) + if (input[field] !== undefined && input[field] !== null && Number.isSafeInteger(id)) return id + } + return null +} + +function actorOf(auth: RequestAuth | undefined, input: AuditJson): AuditActor | null { + if (auth?.kind === "token") { + const { actor } = auth + if (actor.kind === "service") return { kind: AUDIT_ACTOR_KIND.SERVICE, client: actor.client } + return { + kind: actor.kind, + sub: actor.sub, + tgId: actor.telegramId === null ? null : Number(actor.telegramId), + client: actor.client, + } + } + if (auth?.kind === "rejected") return null + return { kind: AUDIT_ACTOR_KIND.LEGACY, tgId: legacyTelegramId(input) } +} + +/** An error a handler returned instead of throwing, e.g. `{ error: "NOT_FOUND" }`, `{ success: false }` or `false`. */ +function returnedError(data: unknown): string | null { + if (data === false) return "Nothing changed" + if (data === null || typeof data !== "object" || Array.isArray(data)) return null + if ("error" in data && typeof data.error === "string") return data.error + if ("success" in data && data.success === false) return "Not successful" + return null +} + +const DENIED_CODES = new Set(["UNAUTHORIZED", "FORBIDDEN"]) + +type Result = { ok: true; data: unknown } | { ok: false; error: TRPCError } + +const MAX_ERROR = 500 + +/** + * The root cause's message, on one line: a database error then reads "violates foreign key constraint …" rather than + * the failed query with its parameters. + */ +function errorMessage(error: Error) { + let cause = error + while (cause.cause instanceof Error) cause = cause.cause + return (cause.message.split("\n")[0] ?? "").slice(0, MAX_ERROR) +} + +function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | null } { + if (!result.ok) + return { + outcome: DENIED_CODES.has(result.error.code) ? AUDIT_OUTCOME.DENIED : AUDIT_OUTCOME.FAILURE, + error: errorMessage(result.error), + } + const error = returnedError(result.data) + if (error === null) return { outcome: AUDIT_OUTCOME.SUCCESS, error: null } + return { outcome: error.startsWith("UNAUTHORIZED") ? AUDIT_OUTCOME.DENIED : AUDIT_OUTCOME.FAILURE, error } +} + +/** + * The event for one mutation, or null when there is nothing worth keeping: calls refused before an actor was known + * (no token on a closed legacy path, an invalid token) would let anyone fill the log. + */ +export function auditEvent(options: { + path: string + auth: RequestAuth | undefined + rawInput: unknown + note: AuditNote + result: Result +}): AuditEvent | null { + const { path, auth, note, result } = options + const input = auditJson(options.rawInput) + const caller = actorOf(auth, input) + if (!caller) return null + const { outcome, error } = outcomeOf(result) + if (outcome === AUDIT_OUTCOME.DENIED && auth?.kind !== "token" && !result.ok) return null + + const actor = note.actor ?? caller + return { + action: note.action ?? path, + actorKind: actor.kind, + actorSub: actor.sub ?? null, + actorTgId: actor.tgId ?? null, + actorClient: actor.client ?? null, + targetType: note.target?.type ?? null, + targetId: note.target === undefined ? null : String(note.target.id), + targetLabel: note.target?.label ?? null, + reason: note.reason ?? null, + outcome, + error, + input, + before: note.before === undefined ? null : auditJson(note.before), + after: note.after === undefined ? null : auditJson(note.after), + } +} diff --git a/src/audit/names.ts b/src/audit/names.ts new file mode 100644 index 0000000..18c5205 --- /dev/null +++ b/src/audit/names.ts @@ -0,0 +1,53 @@ +import { inArray } from "drizzle-orm" +import { DB, SCHEMA } from "@/db" +import { idp } from "@/idp" +import { logger } from "@/logger" +import { decryptUser } from "@/utils/users" + +const DIRECTORY_TTL_MS = 5 * 60_000 +let directory: { at: number; names: Map } | null = null + +async function directoryNames(): Promise> { + if (!idp?.directory) return new Map() + if (directory && Date.now() - directory.at < DIRECTORY_TTL_MS) return directory.names + try { + const people = await idp.directory() + directory = { at: Date.now(), names: new Map(people.map((person) => [person.sub, person.name])) } + } catch (error) { + logger.warn({ err: error }, "[AUDIT] cannot read the IdP directory") + } + return directory?.names ?? new Map() +} + +/** Display names of IdP subjects: from the IdP's directory, else from the profiles Signatures keeps. */ +export async function userNames(subs: string[]): Promise> { + const names = new Map() + if (subs.length === 0) return names + const profiles = SCHEMA.ESIGN.profiles + for (const profile of await DB.select({ sub: profiles.sub, name: profiles.name }) + .from(profiles) + .where(inArray(profiles.sub, subs))) + names.set(profile.sub, profile.name) + const known = await directoryNames() + for (const sub of subs) { + const name = known.get(sub) + if (name) names.set(sub, name) + } + return names +} + +/** Display names of Telegram users the backend has seen, e.g. "Ada Lovelace (@ada)". */ +export async function telegramNames(ids: number[]): Promise> { + const names = new Map() + if (ids.length === 0) return names + const rows = await DB.select().from(SCHEMA.TG.users).where(inArray(SCHEMA.TG.users.userId, ids)) + await Promise.all( + rows.map(async (row) => { + const user = await decryptUser(row).catch(() => null) + if (!user) return + const name = [user.firstName, user.lastName].filter(Boolean).join(" ") + names.set(row.userId, user.username ? `${name} (@${user.username})` : name) + }) + ) + return names +} diff --git a/src/audit/store.ts b/src/audit/store.ts new file mode 100644 index 0000000..d3a71bd --- /dev/null +++ b/src/audit/store.ts @@ -0,0 +1,7 @@ +import { DB, SCHEMA } from "@/db" +import type { AuditEvent } from "." + +/** Kept apart from `src/audit` so that importing the tRPC setup does not open the database. */ +export async function writeAuditEvent(event: AuditEvent) { + await DB.insert(SCHEMA.COMMON.auditEvents).values(event) +} diff --git a/src/db/schema/common/audit-events.ts b/src/db/schema/common/audit-events.ts new file mode 100644 index 0000000..1852589 --- /dev/null +++ b/src/db/schema/common/audit-events.ts @@ -0,0 +1,59 @@ +import { sql } from "drizzle-orm" +import { bigint, index, jsonb, text, timestamp, varchar } from "drizzle-orm/pg-core" +import { createTable } from "../create-table" + +/** Who made a change: the kinds of RFC v3 actors, and `legacy` for anonymous legacy callers. */ +export const AUDIT_ACTOR_KIND = { + USER: "user", + TELEGRAM: "telegram", + SERVICE: "service", + LEGACY: "legacy", +} as const +export type TAuditActorKind = (typeof AUDIT_ACTOR_KIND)[keyof typeof AUDIT_ACTOR_KIND] + +export const AUDIT_OUTCOME = { + SUCCESS: "success", + /** The call ran but changed nothing: an error was thrown or returned. */ + FAILURE: "failure", + /** The caller lacked the scope or permission. */ + DENIED: "denied", +} as const +export type TAuditOutcome = (typeof AUDIT_OUTCOME)[keyof typeof AUDIT_OUTCOME] + +/** JSON as stored in the event: inputs and states are copied without secrets or file contents. */ +export type AuditJson = null | boolean | number | string | AuditJson[] | { [key: string]: AuditJson } + +/** + * Every change made through the backend, whoever made it: one row per mutation, written by the tRPC audit middleware + * (`src/audit`). Telegram moderation, recorded by the bot through `tg.auditLog`, is copied here too. + */ +export const auditEvents = createTable.common( + "audit_events", + { + id: bigint("id", { mode: "number" }).primaryKey().generatedAlwaysAsIdentity(), + /** The procedure path, e.g. `web.faqs.editFaqs`, or `tg.moderation.` for Telegram moderation. */ + action: text("action").notNull(), + actorKind: varchar("actor_kind", { length: 16 }).$type().notNull(), + actorSub: text("actor_sub"), + actorTgId: bigint("actor_tg_id", { mode: "number" }), + /** The OAuth client that made the call, e.g. `admin-dashboard`; null for legacy callers. */ + actorClient: text("actor_client"), + targetType: text("target_type"), + targetId: text("target_id"), + /** A readable name for the target when the event was written, e.g. a FAQ's title. */ + targetLabel: text("target_label"), + reason: text("reason"), + outcome: varchar("outcome", { length: 16 }).$type().notNull(), + error: text("error"), + input: jsonb("input").$type(), + before: jsonb("before").$type(), + after: jsonb("after").$type(), + createdAt: timestamp("created_at", { precision: 3, withTimezone: true }).default(sql`now()`).notNull(), + }, + (t) => [ + index("common_audit_events_action_idx").on(t.action), + index("common_audit_events_actor_sub_idx").on(t.actorSub), + index("common_audit_events_actor_tg_id_idx").on(t.actorTgId), + index("common_audit_events_target_idx").on(t.targetType, t.targetId), + ] +) diff --git a/src/db/schema/common/index.ts b/src/db/schema/common/index.ts index 64d1ca9..3ba091b 100644 --- a/src/db/schema/common/index.ts +++ b/src/db/schema/common/index.ts @@ -1,7 +1,9 @@ +import * as auditEvents from "./audit-events" import * as groupLabels from "./group-labels" import * as idpState from "./idp-state" export const schema = { + ...auditEvents, ...groupLabels, ...idpState, } diff --git a/src/esign/service.ts b/src/esign/service.ts index 6b13fc8..9335c20 100644 --- a/src/esign/service.ts +++ b/src/esign/service.ts @@ -625,6 +625,11 @@ export function createEsignService(options: ServiceOptions) { }) }, + /** For the audit log: the request's title, whoever asks. */ + async requestTitle(id: string) { + return (await store.getRequest(id))?.title ?? null + }, + async resetIdentity(sub: string) { await store.setProfileIdentity(sub, null) }, diff --git a/src/routers/audit.ts b/src/routers/audit.ts new file mode 100644 index 0000000..6701a8c --- /dev/null +++ b/src/routers/audit.ts @@ -0,0 +1,82 @@ +import { TRPCError } from "@trpc/server" +import { and, desc, eq, ilike, lt, or, type SQL } from "drizzle-orm" +import { z } from "zod" +import { telegramNames, userNames } from "@/audit/names" +import { DB, SCHEMA } from "@/db" +import { AUDIT_OUTCOME } from "@/db/schema/common/audit-events" +import { env } from "@/env" +import { dashboard } from "@/idp/policies" +import { createTRPCRouter, policy } from "@/trpc" + +const EVENTS = SCHEMA.COMMON.auditEvents + +/** The first segment of an action: where in the ecosystem the change was made. */ +export const AUDIT_AREAS = ["web", "tg", "wa", "groups", "azure", "esign", "auth"] as const + +const escapeLike = (value: string) => value.replace(/[\\%_]/g, (char) => `\\${char}`) + +export const auditRouter = createTRPCRouter({ + /** + * Every change made through the backend, newest first: who made it, on what, the state before and after, and the + * outcome. Pages with `cursor`, the `nextCursor` of the previous page. + */ + list: policy(dashboard("audit:read")) + .input( + z.object({ + cursor: z.number().int().positive().optional(), + limit: z.number().int().min(1).max(100).default(50), + area: z.enum(AUDIT_AREAS).optional(), + outcome: z.enum(Object.values(AUDIT_OUTCOME)).optional(), + /** Matched against the action, the target, the reason and the error. */ + search: z.string().trim().max(100).optional(), + target: z.object({ type: z.string().min(1), id: z.string().min(1) }).optional(), + }) + ) + .query(async ({ input, ctx }) => { + // The legacy path only serves the dashboard's agent mode, in development. + if (ctx.actor === null && env.NODE_ENV !== "development") throw new TRPCError({ code: "UNAUTHORIZED" }) + const conditions: SQL[] = [] + if (input.cursor) conditions.push(lt(EVENTS.id, input.cursor)) + if (input.area) conditions.push(ilike(EVENTS.action, `${input.area}.%`)) + if (input.outcome) conditions.push(eq(EVENTS.outcome, input.outcome)) + if (input.target) conditions.push(eq(EVENTS.targetType, input.target.type), eq(EVENTS.targetId, input.target.id)) + if (input.search) { + const pattern = `%${escapeLike(input.search)}%` + const matches = or( + ilike(EVENTS.action, pattern), + ilike(EVENTS.targetLabel, pattern), + ilike(EVENTS.targetId, pattern), + ilike(EVENTS.reason, pattern), + ilike(EVENTS.error, pattern) + ) + if (matches) conditions.push(matches) + } + + const rows = await DB.select() + .from(EVENTS) + .where(and(...conditions)) + .orderBy(desc(EVENTS.id)) + .limit(input.limit + 1) + const page = rows.slice(0, input.limit) + + const subs = [...new Set(page.flatMap((row) => (row.actorSub ? [row.actorSub] : [])))] + const telegramIds = new Set(page.flatMap((row) => (row.actorTgId === null ? [] : [row.actorTgId]))) + for (const row of page) + if (row.targetType === "tg.user" && row.targetId && Number.isSafeInteger(Number(row.targetId))) + telegramIds.add(Number(row.targetId)) + const [people, telegram] = await Promise.all([userNames(subs), telegramNames([...telegramIds])]) + + return { + events: page.map((row) => ({ + ...row, + actorName: + (row.actorSub ? people.get(row.actorSub) : undefined) ?? + (row.actorTgId === null ? undefined : telegram.get(row.actorTgId)) ?? + null, + targetLabel: + row.targetLabel ?? (row.targetType === "tg.user" ? (telegram.get(Number(row.targetId)) ?? null) : null), + })), + nextCursor: rows.length > input.limit ? (page.at(-1)?.id ?? null) : null, + } + }), +}) diff --git a/src/routers/auth.ts b/src/routers/auth.ts index 81197a4..f5621e0 100644 --- a/src/routers/auth.ts +++ b/src/routers/auth.ts @@ -22,11 +22,13 @@ export const authRouter = createTRPCRouter({ }) ) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "auth.user", id: input.userId } }) try { const buffer = Buffer.from(await input.image.arrayBuffer()) const file = await uploadBlob(buffer, input.image.type.includes("png") ? "png" : "jpeg", input.image.type) await DB.update(SCHEMA.AUTH.user).set({ image: file.url }).where(eq(SCHEMA.AUTH.user.id, input.userId)) + ctx.audit.set({ after: { image: file.url } }) return { success: true } } catch (err) { logger.error({ err }, "authRouter updateProfilePic error") diff --git a/src/routers/azure/groups.ts b/src/routers/azure/groups.ts index 8250921..8e0647a 100644 --- a/src/routers/azure/groups.ts +++ b/src/routers/azure/groups.ts @@ -9,12 +9,14 @@ export default createTRPCRouter({ }), addMember: policy(dashboard("azure:groups:write")) .input(z.object({ groupId: z.string(), userId: z.string() })) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "azure.group", id: input.groupId }, after: { member: input.userId } }) return await azureDirectory.addGroupMember(input.groupId, input.userId) }), removeMember: policy(dashboard("azure:groups:write")) .input(z.object({ groupId: z.string(), userId: z.string() })) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "azure.group", id: input.groupId }, before: { member: input.userId } }) return await azureDirectory.removeGroupMember(input.groupId, input.userId) }), }) diff --git a/src/routers/azure/members.ts b/src/routers/azure/members.ts index 027a9c8..cc9755e 100644 --- a/src/routers/azure/members.ts +++ b/src/routers/azure/members.ts @@ -25,7 +25,8 @@ export default createTRPCRouter({ }) ) .output(z.object({ error: z.nullable(z.string()) })) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "azure.member", id: input.userId }, after: { assocNumber: input.assocNumber } }) const { error } = await azureDirectory.setMemberNumber(input.userId, input.assocNumber) return { error } }), @@ -55,6 +56,8 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const by = ctx.actor?.kind === "user" ? { sub: ctx.actor.sub, client: ctx.actor.client } : { legacy: true } + const name = `${input.firstName} ${input.lastName}` + ctx.audit.set({ target: { type: "azure.member", id: input.assocNumber, label: name } }) try { const member = await azureDirectory.createMember({ firstName: input.firstName, @@ -62,6 +65,10 @@ export default createTRPCRouter({ assocNumber: input.assocNumber, }) logger.info({ by, memberId: member.id, assocNumber: input.assocNumber }, "[AZURE] member created") + ctx.audit.set({ + target: { type: "azure.member", id: member.id, label: name }, + after: { id: member.id, email: member.mail, assocNumber: input.assocNumber }, + }) const mailOk = await sendWelcomeEmail( input.sendEmailTo, diff --git a/src/routers/esign.ts b/src/routers/esign.ts index 0446425..a27d33d 100644 --- a/src/routers/esign.ts +++ b/src/routers/esign.ts @@ -1,6 +1,7 @@ import type { Actor } from "@polinetwork/auth-kit" import { TRPCError } from "@trpc/server" import { z } from "zod" +import type { AuditRecorder } from "@/audit" import { env } from "@/env" import { esign } from "@/esign" import { MAX_PDF_BYTES } from "@/esign/drive/types" @@ -75,6 +76,11 @@ function publicPdf(pdf: PdfInfo) { const accessKey = z.string().regex(/^[A-Za-z0-9:._-]{1,100}$/) const driveRule = z.object({ roles: z.array(accessKey).max(50), permissions: z.array(accessKey).max(50) }) +/** Names the request a mutation acts on in its audit event. */ +async function auditRequest(ctx: { audit: AuditRecorder }, id: string) { + ctx.audit.set({ target: { type: "esign.request", id, label: await esign.requestTitle(id) } }) +} + function requireManage(caller: Caller) { if (!caller.canManage) throw new TRPCError({ code: "FORBIDDEN", message: "You cannot manage signature requests" }) } @@ -100,6 +106,7 @@ export const esignRouter = createTRPCRouter({ }), /** Forgets the CIE bound to a person, e.g. after they renewed their card. */ resetIdentity: user.input(z.object({ sub: z.string().min(1) })).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "esign.signer", id: input.sub } }) requireManage(callerOf(ctx)) return run(() => esign.resetIdentity(input.sub)) }), @@ -116,9 +123,16 @@ export const esignRouter = createTRPCRouter({ access: user.query(({ ctx }) => run(() => esign.driveAccess(callerOf(ctx)))), setAccess: user .input(z.object({ driveId: z.string().min(1), request: driveRule, sign: driveRule })) - .mutation(({ ctx, input }) => - run(() => esign.setDriveAccess(callerOf(ctx), input.driveId, { request: input.request, sign: input.sign })) - ), + .mutation(async ({ ctx, input }) => { + const caller = callerOf(ctx) + const drive = (await run(() => esign.driveAccess(caller))).drives.find((d) => d.id === input.driveId) + ctx.audit.set({ + target: { type: "esign.drive", id: input.driveId, label: drive?.label }, + before: drive && { request: drive.request, sign: drive.sign }, + after: { request: input.request, sign: input.sign }, + }) + return run(() => esign.setDriveAccess(caller, input.driveId, { request: input.request, sign: input.sign })) + }), }), documents: createTRPCRouter({ @@ -135,6 +149,10 @@ export const esignRouter = createTRPCRouter({ const { document, pdf } = await run(async () => esign.stageUpload(caller, input.name, new Uint8Array(await input.arrayBuffer())) ) + ctx.audit.set({ + target: { type: "esign.document", id: document.id, label: document.fileName }, + after: document, + }) return { document, pdf: publicPdf(pdf) } }), fromDrive: user @@ -143,6 +161,10 @@ export const esignRouter = createTRPCRouter({ const caller = callerOf(ctx) requireManage(caller) const { document, pdf } = await run(() => esign.stageDriveItem(caller, input.driveId, input.itemId)) + ctx.audit.set({ + target: { type: "esign.document", id: document.id, label: document.fileName }, + after: document, + }) return { document, pdf: publicPdf(pdf) } }), /** The document's metadata, pages and existing signatures. */ @@ -187,22 +209,33 @@ export const esignRouter = createTRPCRouter({ email: z.object({ to: z.array(z.email()).max(20), message: z.string().max(2000).nullable() }).nullable(), }) ) - .mutation(({ ctx, input }) => run(() => esign.createRequest(callerOf(ctx), input))), + .mutation(async ({ ctx, input }) => { + ctx.audit.set({ target: { type: "esign.document", id: input.documentId, label: input.title } }) + const request = await run(() => esign.createRequest(callerOf(ctx), input)) + ctx.audit.set({ target: { type: "esign.request", id: request.id, label: request.title }, after: request }) + return request + }), list: user .input(z.object({ scope: z.enum(["inbox", "mine", "all"]) })) .query(({ ctx, input }) => run(() => esign.listRequests(callerOf(ctx), input.scope))), get: user .input(z.object({ id: z.uuid() })) .query(({ ctx, input }) => run(() => esign.getRequest(callerOf(ctx), input.id))), - cancel: user - .input(z.object({ id: z.uuid() })) - .mutation(({ ctx, input }) => run(() => esign.cancelRequest(callerOf(ctx), input.id))), - delete: user - .input(z.object({ id: z.uuid() })) - .mutation(({ ctx, input }) => run(() => esign.deleteRequest(callerOf(ctx), input.id))), + cancel: user.input(z.object({ id: z.uuid() })).mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.id) + return run(() => esign.cancelRequest(callerOf(ctx), input.id)) + }), + delete: user.input(z.object({ id: z.uuid() })).mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.id) + return run(() => esign.deleteRequest(callerOf(ctx), input.id)) + }), decline: user .input(z.object({ id: z.uuid(), reason: z.string().max(500).nullable() })) - .mutation(({ ctx, input }) => run(() => esign.declineRequest(callerOf(ctx), input.id, input.reason))), + .mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.id) + ctx.audit.set({ reason: input.reason }) + return run(() => esign.declineRequest(callerOf(ctx), input.id, input.reason)) + }), /** The fallback: a PDF signed with the official CIE app, uploaded by its signer. */ uploadSigned: user .input( @@ -216,14 +249,16 @@ export const esignRouter = createTRPCRouter({ }) ) ) - .mutation(({ ctx, input }) => - run(async () => + .mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.requestId) + return run(async () => esign.uploadSigned(callerOf(ctx), input.requestId, new Uint8Array(await input.file.arrayBuffer())) ) - ), - retryDelivery: user - .input(z.object({ id: z.uuid() })) - .mutation(({ ctx, input }) => run(() => esign.retryDelivery(callerOf(ctx), input.id))), + }), + retryDelivery: user.input(z.object({ id: z.uuid() })).mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.id) + return run(() => esign.retryDelivery(callerOf(ctx), input.id)) + }), }), jobs: createTRPCRouter({ @@ -240,7 +275,17 @@ export const esignRouter = createTRPCRouter({ .max(10), }) ) - .mutation(({ ctx, input }) => run(() => esign.createJobs(callerOf(ctx), input.items))), + .mutation(({ ctx, input }) => { + const ids = input.items.map((item) => item.requestId) + ctx.audit.set({ + target: { + type: "esign.request", + id: ids.join(","), + label: ids.length === 1 ? null : `${ids.length} requests`, + }, + }) + return run(() => esign.createJobs(callerOf(ctx), input.items)) + }), statuses: user .input(z.object({ jobIds: z.array(z.uuid()).min(1).max(10) })) .query(({ ctx, input }) => run(() => esign.jobStatuses(callerOf(ctx), input.jobIds))), diff --git a/src/routers/groups/labels.ts b/src/routers/groups/labels.ts index 643468d..0810e89 100644 --- a/src/routers/groups/labels.ts +++ b/src/routers/groups/labels.ts @@ -56,6 +56,7 @@ export default createTRPCRouter({ }) .returning() + ctx.audit.set({ target: { type: "groups.label", id: result[0].id, label }, after: result[0] }) return result }), @@ -65,7 +66,7 @@ export default createTRPCRouter({ label, }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { label } = input const labelRow = await DB.select({ id: GROUP_LABELS.id }) @@ -87,6 +88,7 @@ export default createTRPCRouter({ } const result = await DB.delete(GROUP_LABELS).where(eq(GROUP_LABELS.label, label)).returning() + ctx.audit.set({ target: { type: "groups.label", id: labelId, label }, before: result[0] }) return result }), @@ -108,6 +110,7 @@ export default createTRPCRouter({ const { label, newLabel, description, color, updatedBy } = input const author = authorOf(ctx.actor, updatedBy) + const [before] = await DB.select().from(GROUP_LABELS).where(eq(GROUP_LABELS.label, label)).limit(1) const result = await DB.update(GROUP_LABELS) .set({ label: newLabel, @@ -120,6 +123,11 @@ export default createTRPCRouter({ .where(eq(GROUP_LABELS.label, label)) .returning() + ctx.audit.set({ + target: { type: "groups.label", id: before?.id ?? label, label: newLabel ?? label }, + before, + after: result[0], + }) return result }), @@ -131,8 +139,9 @@ export default createTRPCRouter({ label, }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { groupId, type, label } = input + ctx.audit.set({ target: { type: `${type}.group`, id: groupId }, after: { label } }) await assertGroupExists(groupId, type) const labelIdResult = await DB.select({ id: GROUP_LABELS.id }) @@ -168,8 +177,9 @@ export default createTRPCRouter({ label, }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { groupId, type, label } = input + ctx.audit.set({ target: { type: `${type}.group`, id: groupId }, before: { label } }) await assertGroupExists(groupId, type) diff --git a/src/routers/index.ts b/src/routers/index.ts index b84c782..5ba1010 100644 --- a/src/routers/index.ts +++ b/src/routers/index.ts @@ -1,4 +1,5 @@ import { createTRPCRouter } from "@/trpc" +import { auditRouter } from "./audit" import { authRouter } from "./auth" import { azureRouter } from "./azure" import { esignRouter } from "./esign" @@ -15,6 +16,7 @@ import { webRouter } from "./web" * All routers added in /api/routers should be manually added here. */ export const appRouter = createTRPCRouter({ + audit: auditRouter, auth: authRouter, azure: azureRouter, esign: esignRouter, diff --git a/src/routers/tg/audit-log.ts b/src/routers/tg/audit-log.ts index baecc98..a904409 100644 --- a/src/routers/tg/audit-log.ts +++ b/src/routers/tg/audit-log.ts @@ -1,7 +1,9 @@ +import type { Actor } from "@polinetwork/auth-kit" import { TRPCError } from "@trpc/server" import { desc, eq, sql } from "drizzle-orm" import { alias } from "drizzle-orm/pg-core" import { z } from "zod" +import type { AuditNote } from "@/audit" import { DB, SCHEMA } from "@/db" import { ARRAY_AUDIT_TYPE, @@ -35,6 +37,37 @@ const RECORDED_BASES = [ AUDIT_BASIS.TELEGRAM_UI, ] as const +/** + * A moderation action as an audit event. When the bot reports it, it was done by the Telegram user the bot names, not + * by the bot; automatic actions and changes made in Telegram's UI have no such user and stay the bot's. + */ +function moderationNote( + action: { + type: TAuditType + targetId: number + groupId: number | null + until: Date | null + reason?: string | null + actorTgId?: number | null + actorSub?: string | null + basis?: string + review?: TAuditReview | null + }, + bot?: Actor +): AuditNote { + const actor: AuditNote["actor"] = + bot && action.actorTgId != null + ? { kind: "telegram", tgId: action.actorTgId, sub: action.actorSub ?? null, client: bot.client } + : undefined + return { + action: `tg.moderation.${action.type}`, + actor, + target: { type: "tg.user", id: action.targetId }, + after: { groupId: action.groupId, until: action.until, basis: action.basis, review: action.review ?? null }, + reason: action.reason ?? null, + } +} + export default createTRPCRouter({ /** Legacy path; replaced by `record`. */ create: legacyProcedure @@ -48,10 +81,12 @@ export default createTRPCRouter({ reason: z.string().optional(), }) ) - .mutation(async ({ input }) => { - await DB.insert(AUDIT_LOG) + .mutation(async ({ input, ctx }) => { + const [row] = await DB.insert(AUDIT_LOG) .values({ ...input, actorTgId: input.adminId, basis: AUDIT_BASIS.LEGACY }) .onConflictDoNothing() + .returning() + ctx.audit.set(moderationNote(row ?? input)) }), /** @@ -117,7 +152,13 @@ export default createTRPCRouter({ }) .onConflictDoNothing({ target: AUDIT_LOG.idempotencyKey }) .returning({ id: AUDIT_LOG.id }) - if (inserted) return { id: inserted.id, duplicate: false } + if (inserted) { + ctx.audit.set( + moderationNote({ ...input, actorTgId: input.actorTelegramId, actorSub: subject?.sub ?? null, review }, actor) + ) + return { id: inserted.id, duplicate: false } + } + ctx.audit.skip() const [existing] = await DB.select({ id: AUDIT_LOG.id }) .from(AUDIT_LOG) diff --git a/src/routers/tg/grants.ts b/src/routers/tg/grants.ts index cf5c366..8fa7188 100644 --- a/src/routers/tg/grants.ts +++ b/src/routers/tg/grants.ts @@ -87,6 +87,11 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const { actor } = ctx + ctx.audit.set({ + target: { type: "tg.user", id: input.userId }, + after: { validSince: input.since, validUntil: input.until }, + reason: input.reason, + }) if (actor && actor.kind !== "user") throw new TRPCError({ code: "FORBIDDEN" }) if (!actor && input.adderId === undefined) throw new TRPCError({ code: "BAD_REQUEST", message: "Missing adderId" }) @@ -164,6 +169,7 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const { actor } = ctx + ctx.audit.set({ target: { type: "tg.user", id: input.userId } }) if (actor?.kind === "service") throw new TRPCError({ code: "FORBIDDEN" }) if (!actor && input.interruptedById === undefined) throw new TRPCError({ code: "BAD_REQUEST", message: "Missing interruptedById" }) @@ -188,7 +194,7 @@ export default createTRPCRouter({ isNull(s.grants.interruptedAt) ) ) - .returning({ id: s.grants.id }), + .returning(), tx .update(s.grantsV2) .set({ @@ -203,10 +209,11 @@ export default createTRPCRouter({ isNull(s.grantsV2.interruptedAt) ) ) - .returning({ id: s.grantsV2.id }), + .returning(), ]) ) + ctx.audit.set({ after: [...legacy, ...current] }) if (legacy.length + current.length === 0) return { success: false, diff --git a/src/routers/tg/groups.ts b/src/routers/tg/groups.ts index c69f787..87a3c20 100644 --- a/src/routers/tg/groups.ts +++ b/src/routers/tg/groups.ts @@ -1,5 +1,5 @@ import { TRPCError } from "@trpc/server" -import { and, eq, exists, ilike, ne, not, notExists, or, type SQL, sql } from "drizzle-orm" +import { and, eq, exists, ilike, inArray, ne, not, notExists, or, type SQL, sql } from "drizzle-orm" import { z } from "zod" import { DB, SCHEMA } from "@/db" import { describeActor } from "@/idp/actor-info" @@ -166,10 +166,23 @@ export default createTRPCRouter({ ) ) .output(z.array(z.number())) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + const replaced = [] for (const group of input) { - await DB.delete(GROUPS).where(and(eq(GROUPS.link, group.link), ne(GROUPS.telegramId, group.telegramId))) + replaced.push( + ...(await DB.delete(GROUPS) + .where(and(eq(GROUPS.link, group.link), ne(GROUPS.telegramId, group.telegramId))) + .returning()) + ) } + const before = await DB.select() + .from(GROUPS) + .where( + inArray( + GROUPS.telegramId, + input.map((group) => group.telegramId) + ) + ) const rows = await DB.insert(GROUPS) .values(input) @@ -183,6 +196,25 @@ export default createTRPCRouter({ }, }) .returning() + // The bot syncs groups often; only what it changes is recorded. + const known = new Map(before.map((group) => [group.telegramId, group])) + const changed = rows.filter((row) => { + const old = known.get(row.telegramId) + return !old || old.title !== row.title || old.tag !== row.tag || old.link !== row.link + }) + if (replaced.length === 0 && changed.length === 0) ctx.audit.skip() + ctx.audit.set({ + target: + input.length === 1 + ? { type: "tg.group", id: input[0].telegramId, label: input[0].title } + : { + type: "tg.group", + id: input.map((group) => group.telegramId).join(","), + label: `${input.length} groups`, + }, + before: [...replaced, ...changed.flatMap((row) => known.get(row.telegramId) ?? [])], + after: changed, + }) return rows.map((r) => r.telegramId) }), @@ -193,8 +225,9 @@ export default createTRPCRouter({ }) ) .output(z.boolean()) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const rows = await DB.delete(GROUPS).where(eq(GROUPS.telegramId, input.telegramId)).returning() + ctx.audit.set({ target: { type: "tg.group", id: input.telegramId, label: rows[0]?.title }, before: rows[0] }) return rows.length === 1 }), @@ -205,11 +238,20 @@ export default createTRPCRouter({ hide: z.boolean(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + const [before] = await DB.select({ hide: GROUPS.hide }) + .from(GROUPS) + .where(eq(GROUPS.telegramId, input.telegramId)) + .limit(1) const rows = await DB.update(GROUPS) .set({ hide: input.hide }) .where(eq(GROUPS.telegramId, input.telegramId)) .returning() + ctx.audit.set({ + target: { type: "tg.group", id: input.telegramId, label: rows[0]?.title }, + before, + after: rows[0] && { hide: rows[0].hide }, + }) return rows.length === 1 }), @@ -226,6 +268,8 @@ export default createTRPCRouter({ if (!ctx.actor && input.performerId === undefined) throw new TRPCError({ code: "BAD_REQUEST", message: "Missing performerId" }) const performer = await describeActor(ctx.actor, input.performerId) + const [group] = await DB.select().from(GROUPS).where(eq(GROUPS.telegramId, input.chatId)).limit(1) + ctx.audit.set({ target: { type: "tg.group", id: input.chatId, label: group?.title }, before: group }) const left = await WSS.leaveChat({ chatId: input.chatId, performerId: performer.telegramId ? Number(performer.telegramId) : null, diff --git a/src/routers/tg/permissions.ts b/src/routers/tg/permissions.ts index 2a055f5..bd09ec9 100644 --- a/src/routers/tg/permissions.ts +++ b/src/routers/tg/permissions.ts @@ -126,7 +126,8 @@ export default createTRPCRouter({ }), ]) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "tg.user", id: input.userId } }) try { // get current permissions of adder and target const q = await DB.select() @@ -151,6 +152,7 @@ export default createTRPCRouter({ return { error: "UNAUTHORIZED" } const existingRoles = q.find((e) => e.userId === input.userId)?.roles ?? [] + ctx.audit.set({ before: { roles: existingRoles } }) // concat is the superpowered push const roles = Array.from( @@ -193,6 +195,7 @@ export default createTRPCRouter({ }, }) + ctx.audit.set({ after: { roles } }) return { roles, error: null } } catch (error) { logger.error({ error }, "Error while executing addRole in tg.permissions router") @@ -223,7 +226,8 @@ export default createTRPCRouter({ }), ]) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "tg.user", id: input.userId } }) try { // get current permissions of remover and target const q = await DB.select().from(s.permissions).where(eq(s.permissions.userId, input.removerId)) @@ -246,6 +250,11 @@ export default createTRPCRouter({ ) return { error: "UNAUTHORIZED" } + const [target] = await DB.select({ roles: s.permissions.roles }) + .from(s.permissions) + .where(eq(s.permissions.userId, input.userId)) + .limit(1) + ctx.audit.set({ before: target }) const affected = await DB.update(s.permissions) .set({ roles: sql`array_remove(${s.permissions.roles}, ${input.role})` }) .where(eq(s.permissions.userId, input.userId)) @@ -263,6 +272,7 @@ export default createTRPCRouter({ if (presAffected.length !== 0) roles = presAffected[0].roles } + ctx.audit.set({ after: { roles } }) return { roles, error: null } } catch (error) { logger.error({ error }, "Error while executing removeRole in tg.permissions router") @@ -287,7 +297,8 @@ export default createTRPCRouter({ groupId: z.number(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "tg.user", id: input.userId }, after: { groupAdminOf: input.groupId } }) await DB.insert(s.groupAdmins) .values({ userId: input.userId, @@ -313,7 +324,8 @@ export default createTRPCRouter({ ]), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + ctx.audit.set({ target: { type: "tg.user", id: input.userId }, before: { groupAdminOf: input.groupId } }) try { // get current permissions of remover and target const q = await DB.select().from(s.permissions).where(eq(s.permissions.userId, input.removerId)) diff --git a/src/routers/wa/groups.ts b/src/routers/wa/groups.ts index 2ad9f1c..da33513 100644 --- a/src/routers/wa/groups.ts +++ b/src/routers/wa/groups.ts @@ -52,8 +52,9 @@ export default createTRPCRouter({ hide: z.boolean().default(false), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const [created] = await DB.insert(GROUPS).values(input).returning() + ctx.audit.set({ target: { type: "wa.group", id: created.id, label: created.title }, after: created }) return created }), @@ -65,9 +66,15 @@ export default createTRPCRouter({ link: z.url({ hostname: /^chat\.whatsapp\.com$/ }), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { id, ...values } = input + const [before] = await DB.select().from(GROUPS).where(eq(GROUPS.id, id)).limit(1) const [updated] = await DB.update(GROUPS).set(values).where(eq(GROUPS.id, id)).returning() + ctx.audit.set({ + target: { type: "wa.group", id, label: updated?.title ?? before?.title }, + before, + after: updated, + }) return updated }), @@ -78,8 +85,9 @@ export default createTRPCRouter({ }) ) .output(z.boolean()) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const rows = await DB.delete(GROUPS).where(eq(GROUPS.id, input.id)).returning() + ctx.audit.set({ target: { type: "wa.group", id: input.id, label: rows[0]?.title }, before: rows[0] }) return rows.length === 1 }), @@ -90,8 +98,14 @@ export default createTRPCRouter({ hide: z.boolean(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + const [before] = await DB.select({ hide: GROUPS.hide }).from(GROUPS).where(eq(GROUPS.id, input.id)).limit(1) const rows = await DB.update(GROUPS).set({ hide: input.hide }).where(eq(GROUPS.id, input.id)).returning() + ctx.audit.set({ + target: { type: "wa.group", id: input.id, label: rows[0]?.title }, + before, + after: rows[0] && { hide: rows[0].hide }, + }) return rows.length === 1 }), diff --git a/src/routers/web/associations.ts b/src/routers/web/associations.ts index 5a18822..ace4d79 100644 --- a/src/routers/web/associations.ts +++ b/src/routers/web/associations.ts @@ -103,6 +103,7 @@ export default createTRPCRouter({ }) .returning() + ctx.audit.set({ target: { type: "web.association", id: res.id, label: res.name }, after: res }) return formatAssociation(res) }), @@ -125,6 +126,7 @@ export default createTRPCRouter({ ? await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type) : undefined + const [before] = await DB.select().from(ASSOCIATIONS).where(eq(ASSOCIATIONS.id, id)).limit(1) const [res] = await DB.update(ASSOCIATIONS) .set({ name, @@ -136,6 +138,11 @@ export default createTRPCRouter({ .where(eq(ASSOCIATIONS.id, id)) .returning() + ctx.audit.set({ + target: { type: "web.association", id, label: res?.name ?? before?.name }, + before, + after: res, + }) if (!res) return { error: "NOT_FOUND" } return formatAssociation(res) }), @@ -152,6 +159,7 @@ export default createTRPCRouter({ const { id, links, modifiedBy } = input const { email, website, facebook, instagram, tiktok, x, youtube, telegram, linkedin, spotify } = links + const [before] = await DB.select().from(ASSOCIATIONS).where(eq(ASSOCIATIONS.id, id)).limit(1) const [res] = await DB.update(ASSOCIATIONS) .set({ email, @@ -169,6 +177,11 @@ export default createTRPCRouter({ .where(eq(ASSOCIATIONS.id, id)) .returning() + ctx.audit.set({ + target: { type: "web.association", id, label: res?.name ?? before?.name }, + before, + after: res, + }) if (!res) return { error: "NOT_FOUND" } return formatAssociation(res) }), @@ -179,9 +192,10 @@ export default createTRPCRouter({ id: z.number(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { id } = input const deleted = await DB.delete(ASSOCIATIONS).where(eq(ASSOCIATIONS.id, id)).returning() + ctx.audit.set({ target: { type: "web.association", id, label: deleted[0]?.name }, before: deleted[0] }) if (deleted.length === 0) return { error: "NOT_FOUND" } return { error: null } diff --git a/src/routers/web/faqs.ts b/src/routers/web/faqs.ts index fb12b80..1149c18 100644 --- a/src/routers/web/faqs.ts +++ b/src/routers/web/faqs.ts @@ -78,6 +78,7 @@ export default createTRPCRouter({ }) .returning() + ctx.audit.set({ target: { type: "web.faq", id: res.id, label: res.titleIt }, after: res }) return res }), @@ -102,6 +103,7 @@ export default createTRPCRouter({ }) .returning() + ctx.audit.set({ target: { type: "web.faqCategory", id: res.id, label: res.titleIt }, after: res }) return res }), @@ -120,6 +122,7 @@ export default createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { id, titleIt, titleEn, descriptionIt, descriptionEn, categoryId, modifiedBy } = input + const [before] = await DB.select().from(FAQS).where(eq(FAQS.id, id)).limit(1) const [res] = await DB.update(FAQS) .set({ titleIt, @@ -132,6 +135,7 @@ export default createTRPCRouter({ .where(eq(FAQS.id, id)) .returning() + ctx.audit.set({ target: { type: "web.faq", id, label: res?.titleIt ?? before?.titleIt }, before, after: res }) if (!res) return { error: "NOT_FOUND" } return res }), @@ -149,6 +153,7 @@ export default createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { id, titleIt, titleEn, icon, modifiedBy } = input + const [before] = await DB.select().from(FAQ_CATEGORIES).where(eq(FAQ_CATEGORIES.id, id)).limit(1) const [res] = await DB.update(FAQ_CATEGORIES) .set({ titleIt, @@ -159,6 +164,11 @@ export default createTRPCRouter({ .where(eq(FAQ_CATEGORIES.id, id)) .returning() + ctx.audit.set({ + target: { type: "web.faqCategory", id, label: res?.titleIt ?? before?.titleIt }, + before, + after: res, + }) if (!res) return { error: "NOT_FOUND" } return res }), @@ -169,9 +179,10 @@ export default createTRPCRouter({ id: z.number(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { id } = input const deleted = await DB.delete(FAQS).where(eq(FAQS.id, id)).returning() + ctx.audit.set({ target: { type: "web.faq", id, label: deleted[0]?.titleIt }, before: deleted[0] }) if (deleted.length === 0) return { error: "NOT_FOUND" } return { error: null } @@ -183,9 +194,10 @@ export default createTRPCRouter({ id: z.number(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { id } = input const deleted = await DB.delete(FAQ_CATEGORIES).where(eq(FAQ_CATEGORIES.id, id)).returning() + ctx.audit.set({ target: { type: "web.faqCategory", id, label: deleted[0]?.titleIt }, before: deleted[0] }) if (deleted.length === 0) return { error: "NOT_FOUND" } return { error: null } diff --git a/src/routers/web/guides_matricole.ts b/src/routers/web/guides_matricole.ts index 067b2bd..0db6cd6 100644 --- a/src/routers/web/guides_matricole.ts +++ b/src/routers/web/guides_matricole.ts @@ -72,6 +72,7 @@ export default createTRPCRouter({ }) .returning() + ctx.audit.set({ target: { type: "web.guide", id: res.id, label: res.version }, after: res }) return res }), @@ -81,9 +82,10 @@ export default createTRPCRouter({ id: z.number(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { id } = input const deleted = await DB.delete(GUIDES_MATRICOLE).where(eq(GUIDES_MATRICOLE.id, id)).returning() + ctx.audit.set({ target: { type: "web.guide", id, label: deleted[0]?.version }, before: deleted[0] }) if (deleted.length === 0) return { error: "NOT_FOUND" } diff --git a/src/routers/web/projects.ts b/src/routers/web/projects.ts index 2e24dea..c2e302b 100644 --- a/src/routers/web/projects.ts +++ b/src/routers/web/projects.ts @@ -72,6 +72,7 @@ export default createTRPCRouter({ }) .returning() + ctx.audit.set({ target: { type: "web.project", id: res.id, label: res.title }, after: res }) return res }), @@ -93,6 +94,7 @@ export default createTRPCRouter({ ? await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type) : undefined + const [before] = await DB.select().from(PROJECTS).where(eq(PROJECTS.id, input.id)).limit(1) const [res] = await DB.update(PROJECTS) .set({ title, @@ -106,6 +108,11 @@ export default createTRPCRouter({ .where(eq(PROJECTS.id, input.id)) .returning() + ctx.audit.set({ + target: { type: "web.project", id: input.id, label: res?.title ?? before?.title }, + before, + after: res, + }) if (!res) return { error: "NOT_FOUND" } return res }), @@ -119,13 +126,21 @@ export default createTRPCRouter({ .refine((projectIds) => new Set(projectIds).size === projectIds.length), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { + const order = () => + DB.select({ id: PROJECTS.id, title: PROJECTS.title }).from(PROJECTS).orderBy(asc(PROJECTS.order)) + const before = await order() await DB.transaction(async (tx) => { await Promise.all( input.projectIds.map((id, index) => tx.update(PROJECTS).set({ order: index }).where(eq(PROJECTS.id, id))) ) }) + ctx.audit.set({ + target: { type: "web.projects", id: "order", label: "Projects order" }, + before, + after: await order(), + }) return { error: null } }), @@ -135,9 +150,10 @@ export default createTRPCRouter({ id: z.number(), }) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { const { id } = input const deleted = await DB.delete(PROJECTS).where(eq(PROJECTS.id, id)).returning() + ctx.audit.set({ target: { type: "web.project", id, label: deleted[0]?.title }, before: deleted[0] }) if (deleted.length === 0) return { error: "NOT_FOUND" } return { error: null } diff --git a/src/routers/web/reports.ts b/src/routers/web/reports.ts index 580d673..7aa0f55 100644 --- a/src/routers/web/reports.ts +++ b/src/routers/web/reports.ts @@ -20,6 +20,10 @@ async function assertGroupExists(groupId: number, type: "tg" | "wa") { if (!groups.length) throw new Error("Group not found") } +function reportsTarget(ids: number[]) { + return { type: "web.report", id: ids.join(","), label: ids.length === 1 ? null : `${ids.length} reports` } +} + export const reports = createTRPCRouter({ create: policy({ service: { scope: SCOPE.publicRead } }) .input( @@ -37,12 +41,16 @@ export const reports = createTRPCRouter({ }), ]) ) - .mutation(async ({ input }) => { + .mutation(async ({ input, ctx }) => { if (input.reportType === "broken_link") { await assertGroupExists(input.groupId, input.type) } - await DB.insert(REPORTS).values(input) + const [report] = await DB.insert(REPORTS).values(input).returning() + ctx.audit.set({ + target: { type: "web.report", id: report.id, label: report.label ?? report.reportedLink }, + after: report, + }) return { ok: true as const } }), @@ -74,13 +82,25 @@ export const reports = createTRPCRouter({ resolve: policy(dashboard("web:reports:manage")) .input(z.object({ ids: z.array(z.number().int()).min(1) })) - .mutation(async ({ input }) => { - return await DB.update(REPORTS).set({ status: "resolved" }).where(inArray(REPORTS.id, input.ids)).returning() + .mutation(async ({ input, ctx }) => { + const before = await DB.select().from(REPORTS).where(inArray(REPORTS.id, input.ids)) + const after = await DB.update(REPORTS) + .set({ status: "resolved" }) + .where(inArray(REPORTS.id, input.ids)) + .returning() + ctx.audit.set({ target: reportsTarget(input.ids), before, after }) + return after }), dismiss: policy(dashboard("web:reports:manage")) .input(z.object({ ids: z.array(z.number().int()).min(1) })) - .mutation(async ({ input }) => { - return await DB.update(REPORTS).set({ status: "dismissed" }).where(inArray(REPORTS.id, input.ids)).returning() + .mutation(async ({ input, ctx }) => { + const before = await DB.select().from(REPORTS).where(inArray(REPORTS.id, input.ids)) + const after = await DB.update(REPORTS) + .set({ status: "dismissed" }) + .where(inArray(REPORTS.id, input.ids)) + .returning() + ctx.audit.set({ target: reportsTarget(input.ids), before, after }) + return after }), }) diff --git a/src/trpc.ts b/src/trpc.ts index 97baca1..a189031 100644 --- a/src/trpc.ts +++ b/src/trpc.ts @@ -2,6 +2,7 @@ import type { AccessSnapshotClient, Actor } from "@polinetwork/auth-kit" import { initTRPC, TRPCError } from "@trpc/server" import superjson from "superjson" import { ZodError, z } from "zod/v4" +import { auditEvent, auditRecorder, noAudit, UNAUDITED } from "./audit" import type { RequestAuth } from "./idp/auth" import { logger } from "./logger" @@ -71,6 +72,33 @@ const timingMiddleware = t.middleware(async ({ next, path }) => { return result }) +/** + * Records every mutation in `common_audit_events` (see `src/audit`): who called it, with what input, and whether it + * succeeded, failed or was denied. It runs before the policy so that denied calls are kept too. Handlers add the + * target and the state before and after through `ctx.audit`. A failed write is logged and does not fail the call, + * whose change has already happened. + */ +const auditMiddleware = t.middleware(async ({ ctx, path, type, getRawInput, next }) => { + if (type !== "mutation" || UNAUDITED.has(path)) return next({ ctx: { audit: noAudit } }) + + const { recorder, note, skipped } = auditRecorder() + const result = await next({ ctx: { audit: recorder } }) + if (skipped()) return result + + try { + const rawInput = await getRawInput().catch(() => undefined) + const event = auditEvent({ path, auth: ctx.auth, rawInput, note, result }) + if (event) { + // Imported here so that loading the router does not open the database. + const { writeAuditEvent } = await import("./audit/store") + await writeAuditEvent(event) + } + } catch (error) { + logger.error({ err: error, procedure: path }, "[AUDIT] cannot record the event") + } + return result +}) + /** Mounts that RFC v3 §8 removes; their procedures stay reachable only on the legacy path. */ const LEGACY_ONLY_MOUNTS = ["tg.groupLabels."] @@ -124,7 +152,7 @@ const enforcePolicy = t.middleware(async ({ ctx, meta, path, next }) => { return next({ ctx: enforced }) }) -const baseProcedure = t.procedure.use(timingMiddleware) +const baseProcedure = t.procedure.use(timingMiddleware).use(auditMiddleware) /** A procedure with its RFC v3 §8 policy. Every procedure must be built from this or `legacyProcedure`. */ export function policy(rules: Policy, options: { tokenOnly?: boolean } = {}) { diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts new file mode 100644 index 0000000..f00a62f --- /dev/null +++ b/tests/audit-json.test.ts @@ -0,0 +1,34 @@ +import { describe, expect, it } from "vitest" +import { auditJson } from "@/audit" + +describe("auditJson", () => { + it("hides secrets and describes files instead of copying them", () => { + const form = new FormData() + form.set("name", "PoliNetwork") + form.set("logo", new File([new Uint8Array(3)], "logo.png", { type: "image/png" })) + expect( + auditJson({ + form, + password: "hunter2", + ticket: "abc", + idempotencyKey: "k", + bytes: new Uint8Array(4), + at: new Date(0), + }) + ).toEqual({ + form: { name: "PoliNetwork", logo: { file: "logo.png", type: "image/png", size: 3 } }, + password: "(hidden)", + ticket: "(hidden)", + idempotencyKey: "(hidden)", + bytes: "(4 bytes)", + at: "1970-01-01T00:00:00.000Z", + }) + }) + + it("cuts long strings and arrays", () => { + const copy = auditJson({ text: "x".repeat(2500), list: Array.from({ length: 103 }, (_, i) => i) }) + expect(copy).toMatchObject({ text: `${"x".repeat(2000)}… (2500 characters)` }) + expect((copy as { list: unknown[] }).list).toHaveLength(101) + expect((copy as { list: unknown[] }).list.at(-1)).toBe("(3 more)") + }) +}) diff --git a/tests/azure-router.test.ts b/tests/azure-router.test.ts index 70071a4..da401f7 100644 --- a/tests/azure-router.test.ts +++ b/tests/azure-router.test.ts @@ -5,6 +5,8 @@ import type { Context } from "@/trpc" vi.mock("@/emails/mailer", () => ({ sendWelcomeEmail: vi.fn().mockResolvedValue(true), })) +const audited = vi.hoisted(() => [] as Array>) +vi.mock("@/audit/store", () => ({ writeAuditEvent: async (event: Record) => audited.push(event) })) const REQUIRED_ENV: Record = { NODE_ENV: "test", @@ -79,6 +81,15 @@ describe("Azure tRPC routes without credentials", () => { employeeId: "2043", isMember: true, }) + expect(audited.at(-1)).toMatchObject({ + action: "members.create", + actorKind: "legacy", + outcome: "success", + targetType: "azure.member", + targetId: result.id, + targetLabel: "Katherine Johnson", + after: { id: result.id, email: "katherine.johnson@polinetwork.org", assocNumber: 2043 }, + }) }) it("adds and removes group memberships", async () => { diff --git a/tests/group-label-platform-isolation.test.ts b/tests/group-label-platform-isolation.test.ts index 82ffa4a..b3f3ce8 100644 --- a/tests/group-label-platform-isolation.test.ts +++ b/tests/group-label-platform-isolation.test.ts @@ -65,8 +65,9 @@ vi.mock("@/db", () => ({ vi.mock("@/trpc", () => { const publicProcedure = { input: (schema: { parse: (input: unknown) => unknown }) => ({ - mutation: (handler: (args: { input: unknown }) => unknown) => ({ - execute: (input: unknown) => handler({ input: schema.parse(input) }), + mutation: (handler: (args: { input: unknown; ctx: unknown }) => unknown) => ({ + execute: (input: unknown) => + handler({ input: schema.parse(input), ctx: { audit: { set: () => {}, skip: () => {} } } }), }), }), query: (handler: () => unknown) => ({ handler }), diff --git a/tests/idp-router.test.ts b/tests/idp-router.test.ts index 52d18a1..add0dd1 100644 --- a/tests/idp-router.test.ts +++ b/tests/idp-router.test.ts @@ -54,6 +54,8 @@ vi.mock("@/db", async () => { } }) vi.mock("@/server", () => ({ WSS: {} })) +const audited = vi.hoisted(() => [] as Array>) +vi.mock("@/audit/store", () => ({ writeAuditEvent: async (event: Record) => audited.push(event) })) let appRouter: typeof import("@/routers").appRouter @@ -246,6 +248,7 @@ describe("tg.auditLog.record", () => { db.inserted = [] db.conflict = false db.rows = {} + audited.length = 0 }) it("stores the actor's subject and the executing client", async () => { @@ -273,9 +276,110 @@ describe("tg.auditLog.record", () => { db.conflict = true db.rows = { tg_audit_log: [{ id: 41 }] } expect(await record(access(true, ["usr_10"]), {})).toEqual({ id: 41, duplicate: true }) + expect(audited).toEqual([]) + }) + + it("adds the action to the audit log as done by the moderator, through the bot", async () => { + await record(access(true, ["usr_10"]), { reason: "spam" }) + expect(audited).toEqual([ + expect.objectContaining({ + action: "tg.moderation.ban", + actorKind: "telegram", + actorTgId: 10, + actorSub: "usr_10", + actorClient: "telegram-bot", + targetType: "tg.user", + targetId: "777", + reason: "spam", + outcome: "success", + after: { groupId: -100, until: null, basis: "idp_permission", review: null }, + }), + ]) + expect(audited[0]?.input).toMatchObject({ idempotencyKey: "(hidden)", targetId: 777 }) + }) + + it("leaves automatic actions to the bot", async () => { + await record(access(true, []), { basis: "automatic", actorTelegramId: null }) + expect(audited[0]).toMatchObject({ actorKind: "service", actorTgId: null, actorClient: "telegram-bot" }) }) it("needs an actor unless the action was automatic or made in Telegram's UI", async () => { await expect(record(access(true, []), { actorTelegramId: null })).rejects.toMatchObject({ code: "BAD_REQUEST" }) }) }) + +describe("audit log", () => { + const admin: Actor = { kind: "user", client: "admin-dashboard", sub: "usr_admin", telegramId: "5" } + const caller = (permissions: string[], legacyAnonymous: "allow" | "deny" = "allow", token = true) => + appRouter.createCaller({ + auth: token ? { kind: "token", actor: admin, scopes: new Set(["backend:admin"]) } : undefined, + legacyAnonymous, + access: { + has: (_actor, permission) => permissions.includes(permission), + current: () => null, + subjectBySub: () => undefined, + subjectByTelegramId: () => undefined, + status: () => ({ lastSyncAt: 1, fresh: true, generation: 1, subjects: 1 }), + }, + }) + + beforeEach(() => { + db.inserted = [] + db.rows = {} + audited.length = 0 + }) + + it("is readable only with audit:read", () => { + const meta = procedures().find(([name]) => name === "audit.list")?.[1]._def.meta + expect(meta?.policy).toEqual({ user: { scope: "backend:admin", permission: "audit:read" } }) + }) + + it("refuses anonymous readers outside development", async () => { + await expect(appRouter.createCaller({}).audit.list({})).rejects.toMatchObject({ code: "UNAUTHORIZED" }) + }) + + it("records a denied mutation with the caller and the input", async () => { + await expect(caller([]).web.faqs.deleteFaqs({ id: 3 })).rejects.toMatchObject({ code: "FORBIDDEN" }) + expect(audited).toEqual([ + expect.objectContaining({ + action: "web.faqs.deleteFaqs", + actorKind: "user", + actorSub: "usr_admin", + actorTgId: 5, + actorClient: "admin-dashboard", + outcome: "denied", + error: "Missing permission", + input: { id: 3 }, + targetType: null, + }), + ]) + }) + + it("records a returned error as a failure, with what the handler adds", async () => { + db.rows = { tg_grants: [{ userId: 456, validUntil: new Date(Date.now() + 3_600_000) }], tg_grants_v2: [] } + const since = new Date() + const until = new Date(Date.now() + 7_200_000) + expect( + await caller(["tg:grants:manage"]).tg.grants.create({ userId: 456, since, until, reason: "exam week" }) + ).toEqual({ success: false, error: "ALREADY_EXISTING" }) + expect(audited).toEqual([ + expect.objectContaining({ + action: "tg.grants.create", + outcome: "failure", + error: "ALREADY_EXISTING", + targetType: "tg.user", + targetId: "456", + reason: "exam week", + after: { validSince: since.toISOString(), validUntil: until.toISOString() }, + }), + ]) + }) + + it("does not record calls refused before the caller is known, nor queries", async () => { + await expect(caller([], "deny", false).web.faqs.deleteFaqs({ id: 3 })).rejects.toMatchObject({ + code: "UNAUTHORIZED", + }) + await caller(["admin:access"]).me.access() + expect(audited).toEqual([]) + }) +}) From b89e297d77a8b13dda8feb890fda34542dacd16b Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Sun, 8 Nov 2026 11:00:22 +0100 Subject: [PATCH 02/12] fix(audit): keep the audit log to accountability, without moderation copies or People details The central log now records who used which permission to change what. Areas with their own history keep the details there: - Telegram moderation stays in tg_audit_log, whose retention follows the privacy policy: the bot's reports are no longer copied, and the migration no longer backfills them. - People mutations keep the actor, the action, the person and the outcome, without the input or the states, so HR notes and contacts stay behind People's own permissions. Also: - Denied calls keep no input, and copies cap the number of fields and their total size, so a caller with any valid token cannot fill the log. - Events are deleted 24 months after they were written. - audit.list filters by actor and by date; the areas follow the routers (people and me added, auth removed), checked by a test. - Events carry the request id, shared by the calls of one HTTP request. - Calls on several records leave the target id empty instead of joining the ids. - Secret fields are matched by whole name or camelCase suffix, so courseCode is kept. - The legacy actor kind and its input fields are gone, with main's anonymous path. Migration 0033 is regenerated: drop common_audit_events from any database that ran the earlier version before migrating. --- README.md | 35 ++++--- drizzle/0033_audit_events.sql | 23 +---- drizzle/meta/0033_snapshot.json | 38 ++++++- drizzle/meta/_journal.json | 2 +- src/audit/index.ts | 146 +++++++++++++++------------ src/constants.ts | 5 + src/cron.ts | 19 +++- src/db/schema/common/audit-events.ts | 16 +-- src/routers/audit.ts | 23 ++++- src/routers/azure/members.ts | 3 +- src/routers/esign.ts | 10 +- src/routers/tg/audit-log.ts | 41 +------- src/routers/tg/groups.ts | 7 +- src/routers/web/reports.ts | 5 +- src/server.ts | 1 + src/trpc.ts | 10 +- tests/audit-json.test.ts | 104 ++++++++++++++++++- tests/cron-retention.test.ts | 18 +++- tests/idp-router.test.ts | 36 +++---- 19 files changed, 349 insertions(+), 193 deletions(-) diff --git a/README.md b/README.md index 0a65bdd..d562575 100644 --- a/README.md +++ b/README.md @@ -59,20 +59,27 @@ The IdP notifies `POST /internal/events` when access changes; the backend also p ## Audit log -Every mutation is recorded in `common_audit_events` by the tRPC audit middleware (`src/audit`): the action (the procedure -path), the caller from the token (or `legacy` with the Telegram ID the legacy caller sent), the input, the outcome -(`success`, `failure` when an error is thrown or returned, `denied` when the policy refuses a token caller) and when. -Handlers add the target and the state before and after with `ctx.audit.set(...)`, or `ctx.audit.skip()` when a call -changed nothing. Inputs and states are stored without secrets (`password`, `token`, `ticket`, `code`, `*key` fields) or -file contents. Calls refused before the caller is known (no token on a closed legacy path, an invalid token) and the -bot's ingestion (`tg.messages.add`, `tg.users.add`) are not recorded. - -Telegram moderation the bot reports through `tg.auditLog` is recorded as `tg.moderation.`, done by the moderator; -the migration copies the existing `tg_audit_log`. Dashboard users with the IdP permission `audit:read` read the log with -`audit.list`, newest first, paged by `cursor` and filtered by area, outcome, target or text. - -Changes made outside the backend are not in the log: directly in Telegram or WhatsApp unless the bot reports them, the -cron jobs, the e-signing agent endpoints (each request keeps its own history) and roles and permissions in the IdP. +`common_audit_events` records who used which permission to change what through the backend. The tRPC audit middleware +(`src/audit`) writes one event per mutation: the action (the procedure path), the caller from the token, the target, +the outcome (`success`, `failure` when an error is thrown or returned, `denied` when the policy refuses the caller), the +request id shared by the calls of one HTTP request, and when. Handlers add the target and the state before and after +with `ctx.audit.set(...)`, or `ctx.audit.skip()` when a call changed nothing. Inputs and states are stored without +secrets (`password`, `token`, `ticket`, `code`, `*Key`, `*Secret` fields) or file contents, and are cut when too large. +Denied calls keep no input. Calls without a valid token are not recorded. + +Areas with their own history keep the details there, readable with their own permissions: + +- People: events name the actor, the action, the person and the outcome, without the input or the states; what changed + is in the person's activity history. +- Telegram moderation: kept in `tg_audit_log` under its own retention rules and not copied here, like the bot's + ingestion (`tg.messages.add`, `tg.users.add`). + +Events are deleted 24 months after they were written (`AUDIT_EVENTS_RETENTION_MONTHS`). Dashboard users with the IdP +permission `audit:read` read the log with `audit.list`, newest first, paged by `cursor` and filtered by area, outcome, +target, actor, date or text. + +Changes made outside the backend's tRPC procedures are not in the log: directly in Telegram or WhatsApp, the cron jobs, +the e-signing agent endpoints (each request keeps its own history) and roles and permissions in the IdP. ## People diff --git a/drizzle/0033_audit_events.sql b/drizzle/0033_audit_events.sql index eeaa09c..e02b998 100644 --- a/drizzle/0033_audit_events.sql +++ b/drizzle/0033_audit_events.sql @@ -1,6 +1,7 @@ CREATE TABLE "common_audit_events" ( "id" bigint PRIMARY KEY GENERATED ALWAYS AS IDENTITY (sequence name "common_audit_events_id_seq" INCREMENT BY 1 MINVALUE 1 MAXVALUE 9223372036854775807 START WITH 1 CACHE 1), "action" text NOT NULL, + "request_id" uuid, "actor_kind" varchar(16) NOT NULL, "actor_sub" text, "actor_tg_id" bigint, @@ -21,23 +22,5 @@ CREATE INDEX "common_audit_events_action_idx" ON "common_audit_events" USING btr CREATE INDEX "common_audit_events_actor_sub_idx" ON "common_audit_events" USING btree ("actor_sub");--> statement-breakpoint CREATE INDEX "common_audit_events_actor_tg_id_idx" ON "common_audit_events" USING btree ("actor_tg_id");--> statement-breakpoint CREATE INDEX "common_audit_events_target_idx" ON "common_audit_events" USING btree ("target_type","target_id");--> statement-breakpoint --- Telegram moderation recorded before this table existed, oldest first so that ids follow time. -INSERT INTO "common_audit_events" ("action", "actor_kind", "actor_sub", "actor_tg_id", "actor_client", "target_type", "target_id", "reason", "outcome", "after", "created_at") -SELECT - 'tg.moderation.' || "type", - CASE - WHEN "client" IS NULL THEN 'legacy' - WHEN "actor_tg_id" IS NOT NULL THEN 'telegram' - ELSE 'service' - END, - "actor_sub", - "actor_tg_id", - "client", - 'tg.user', - "target_id"::text, - "reason", - 'success', - jsonb_build_object('groupId', "group_id", 'until', "until", 'basis', "basis", 'review', "review"), - "created_at" -FROM "tg_audit_log" -ORDER BY "created_at", "id"; +CREATE INDEX "common_audit_events_request_id_idx" ON "common_audit_events" USING btree ("request_id");--> statement-breakpoint +CREATE INDEX "common_audit_events_created_at_idx" ON "common_audit_events" USING btree ("created_at"); \ No newline at end of file diff --git a/drizzle/meta/0033_snapshot.json b/drizzle/meta/0033_snapshot.json index 79f48ef..bb02758 100644 --- a/drizzle/meta/0033_snapshot.json +++ b/drizzle/meta/0033_snapshot.json @@ -1,5 +1,5 @@ { - "id": "5528cb2f-2677-44b0-9bdb-46e65a78f0c6", + "id": "c566cb6d-c956-4ec6-b9e2-238b1b334db7", "prevId": "e5e83f23-209c-4eb1-a909-bf427b65e42d", "version": "7", "dialect": "postgresql", @@ -31,6 +31,12 @@ "primaryKey": false, "notNull": true }, + "request_id": { + "name": "request_id", + "type": "uuid", + "primaryKey": false, + "notNull": false + }, "actor_kind": { "name": "actor_kind", "type": "varchar(16)", @@ -183,6 +189,36 @@ "concurrently": false, "method": "btree", "with": {} + }, + "common_audit_events_request_id_idx": { + "name": "common_audit_events_request_id_idx", + "columns": [ + { + "expression": "request_id", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} + }, + "common_audit_events_created_at_idx": { + "name": "common_audit_events_created_at_idx", + "columns": [ + { + "expression": "created_at", + "isExpression": false, + "asc": true, + "nulls": "last" + } + ], + "isUnique": false, + "concurrently": false, + "method": "btree", + "with": {} } }, "foreignKeys": {}, diff --git a/drizzle/meta/_journal.json b/drizzle/meta/_journal.json index e2993ed..bb8b0d2 100644 --- a/drizzle/meta/_journal.json +++ b/drizzle/meta/_journal.json @@ -236,7 +236,7 @@ { "idx": 33, "version": "7", - "when": 1793634394495, + "when": 1794131956902, "tag": "0033_audit_events", "breakpoints": true } diff --git a/src/audit/index.ts b/src/audit/index.ts index af10536..7d75bff 100644 --- a/src/audit/index.ts +++ b/src/audit/index.ts @@ -11,17 +11,18 @@ import type { RequestAuth } from "@/idp/auth" export type AuditEvent = typeof auditEvents.$inferInsert -/** What a change was made to; `label` is a readable name, kept as it was at the time. */ -export type AuditTarget = { type: string; id: string | number; label?: string | null } +/** + * What a change was made to; `label` is a readable name, kept as it was at the time. `id` is null when the call acts + * on several records, which are then named in the input. + */ +export type AuditTarget = { type: string; id: string | number | null; label?: string | null } export type AuditActor = { kind: TAuditActorKind; sub?: string | null; tgId?: number | null; client?: string | null } /** What only the handler knows about its change. Each call adds to or replaces the previous fields. */ export type AuditNote = { - /** Replaces the procedure path, e.g. `tg.moderation.ban` for moderation the bot reports. */ + /** Replaces the procedure path. */ action?: string - /** Replaces the caller, for changes the caller reports on someone else's behalf. */ - actor?: AuditActor target?: AuditTarget before?: unknown after?: unknown @@ -34,8 +35,19 @@ export type AuditRecorder = { skip: () => void } -/** Mutations that are not changes made by someone: the bot's high-volume ingestion and per-visit syncs. */ -export const UNAUDITED = new Set(["tg.messages.add", "tg.users.add", "esign.profile.sync"]) +/** + * Mutations that are not changes made by someone: the bot's high-volume ingestion and per-visit syncs. Moderation the + * bot reports is kept in `tg_audit_log`, under its own retention rules, and not copied here. + */ +export const UNAUDITED = new Set(["tg.messages.add", "tg.users.add", "tg.auditLog.record", "esign.profile.sync"]) + +/** + * Areas that keep their own history, readable with their own permissions: their events name the actor, the action, + * the target and the outcome, without the input or the state before and after. + * + * Temporary: a per-procedure `audit` option in the procedure meta replaces this list. + */ +const MINIMAL_AREAS = [{ prefix: "people.", targetType: "people.person" }] /** Handed to queries, which change nothing. */ export const noAudit: AuditRecorder = { set: () => {}, skip: () => {} } @@ -52,16 +64,15 @@ export function auditRecorder() { return { recorder, note, skipped: () => skipped } } -const SECRET_KEY = /pass(word)?|secret|token|ticket|otp|code|key$/i +/** Field names that hold secrets: whole names, or the last word of a camelCase name (`idempotencyKey`). */ +const SECRET_KEY = /^(pass(word)?|passwd|secret|token|ticket|otp|code|key)$|(Pass(word)?|Secret|Token|Ticket|Otp|Key)$/ const MAX_STRING = 2000 const MAX_ITEMS = 100 const MAX_DEPTH = 8 +/** A copy longer than this, as JSON, is replaced by a note of its size. */ +const MAX_JSON = 64 * 1024 -/** - * A JSON copy of an input or a row, safe to keep: secrets are replaced, files and bytes are described instead of copied, - * and long strings and arrays are cut. - */ -export function auditJson(value: unknown, depth = 0): AuditJson { +function copyOf(value: unknown, depth: number): AuditJson { if (value === null || value === undefined) return null if (typeof value === "string") return value.length > MAX_STRING ? `${value.slice(0, MAX_STRING)}… (${value.length} characters)` : value @@ -73,58 +84,44 @@ export function auditJson(value: unknown, depth = 0): AuditJson { if (value instanceof Blob) return { file: null, type: value.type, size: value.size } if (value instanceof ArrayBuffer || ArrayBuffer.isView(value)) return `(${value.byteLength} bytes)` if (depth >= MAX_DEPTH) return "(nested too deep)" - if (value instanceof FormData) return auditJson(Object.fromEntries(value.entries()), depth) - if (value instanceof Map) return auditJson(Object.fromEntries(value), depth) - if (value instanceof Set) return auditJson([...value], depth) + if (value instanceof FormData) return copyOf(Object.fromEntries(value.entries()), depth) + if (value instanceof Map) return copyOf(Object.fromEntries(value), depth) + if (value instanceof Set) return copyOf([...value], depth) if (Array.isArray(value)) { - const items = value.slice(0, MAX_ITEMS).map((item) => auditJson(item, depth + 1)) + const items = value.slice(0, MAX_ITEMS).map((item) => copyOf(item, depth + 1)) return value.length > MAX_ITEMS ? [...items, `(${value.length - MAX_ITEMS} more)`] : items } if (typeof value === "object") { + const entries = Object.entries(value).filter(([, item]) => item !== undefined && typeof item !== "function") const copy: { [key: string]: AuditJson } = {} - for (const [key, item] of Object.entries(value)) { - if (item === undefined || typeof item === "function") continue - copy[key] = SECRET_KEY.test(key) ? "(hidden)" : auditJson(item, depth + 1) - } + for (const [key, item] of entries.slice(0, MAX_ITEMS)) + copy[key] = SECRET_KEY.test(key) ? "(hidden)" : copyOf(item, depth + 1) + if (entries.length > MAX_ITEMS) copy["(more)"] = `${entries.length - MAX_ITEMS} more fields` return copy } return null } -/** Input fields in which legacy callers name themselves. */ -const LEGACY_ACTOR_FIELDS = [ - "createdBy", - "modifiedBy", - "updatedBy", - "adderId", - "removerId", - "interruptedById", - "performerId", - "adminId", -] - -function legacyTelegramId(input: AuditJson): number | null { - if (input === null || typeof input !== "object" || Array.isArray(input)) return null - for (const field of LEGACY_ACTOR_FIELDS) { - const id = Number(input[field]) - if (input[field] !== undefined && input[field] !== null && Number.isSafeInteger(id)) return id - } - return null +/** + * A JSON copy of an input or a row, safe to keep: secrets are replaced, files and bytes are described instead of copied, + * and long strings, arrays, objects and copies are cut. + */ +export function auditJson(value: unknown): AuditJson { + const copy = copyOf(value, 0) + const size = JSON.stringify(copy)?.length ?? 0 + return size > MAX_JSON ? `(${size} characters, too large to keep)` : copy } -function actorOf(auth: RequestAuth | undefined, input: AuditJson): AuditActor | null { - if (auth?.kind === "token") { - const { actor } = auth - if (actor.kind === "service") return { kind: AUDIT_ACTOR_KIND.SERVICE, client: actor.client } - return { - kind: actor.kind, - sub: actor.sub, - tgId: actor.telegramId === null ? null : Number(actor.telegramId), - client: actor.client, - } +function actorOf(auth: RequestAuth | undefined): AuditActor | null { + if (auth?.kind !== "token") return null + const { actor } = auth + if (actor.kind === "service") return { kind: AUDIT_ACTOR_KIND.SERVICE, client: actor.client } + return { + kind: actor.kind, + sub: actor.sub, + tgId: actor.telegramId === null ? null : Number(actor.telegramId), + client: actor.client, } - if (auth?.kind === "rejected") return null - return { kind: AUDIT_ACTOR_KIND.LEGACY, tgId: legacyTelegramId(input) } } /** An error a handler returned instead of throwing, e.g. `{ error: "NOT_FOUND" }`, `{ success: false }` or `false`. */ @@ -159,43 +156,66 @@ function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | nu error: errorMessage(result.error), } const error = returnedError(result.data) - if (error === null) return { outcome: AUDIT_OUTCOME.SUCCESS, error: null } - return { outcome: error.startsWith("UNAUTHORIZED") ? AUDIT_OUTCOME.DENIED : AUDIT_OUTCOME.FAILURE, error } + return error === null ? { outcome: AUDIT_OUTCOME.SUCCESS, error: null } : { outcome: AUDIT_OUTCOME.FAILURE, error } +} + +/** The first of `keys` that `value` holds as a string or a number. */ +function idIn(value: unknown, keys: string[]): string | null { + if (value === null || typeof value !== "object") return null + for (const key of keys) { + const id = (value as Record)[key] + if (typeof id === "string" || typeof id === "number") return String(id) + } + return null } /** * The event for one mutation, or null when there is nothing worth keeping: calls refused before an actor was known - * (no token on a closed legacy path, an invalid token) would let anyone fill the log. + * (no token, an invalid token) would let anyone fill the log. */ export function auditEvent(options: { path: string auth: RequestAuth | undefined + requestId?: string | null rawInput: unknown note: AuditNote result: Result }): AuditEvent | null { const { path, auth, note, result } = options - const input = auditJson(options.rawInput) - const caller = actorOf(auth, input) - if (!caller) return null + const actor = actorOf(auth) + if (!actor) return null const { outcome, error } = outcomeOf(result) - if (outcome === AUDIT_OUTCOME.DENIED && auth?.kind !== "token" && !result.ok) return null - const actor = note.actor ?? caller - return { + const event: AuditEvent = { action: note.action ?? path, + requestId: options.requestId ?? null, actorKind: actor.kind, actorSub: actor.sub ?? null, actorTgId: actor.tgId ?? null, actorClient: actor.client ?? null, targetType: note.target?.type ?? null, - targetId: note.target === undefined ? null : String(note.target.id), + targetId: note.target?.id == null ? null : String(note.target.id), targetLabel: note.target?.label ?? null, reason: note.reason ?? null, outcome, error, - input, + // A refused call changed nothing, and its input is whatever the caller chose to send. + input: outcome === AUDIT_OUTCOME.DENIED ? null : auditJson(options.rawInput), before: note.before === undefined ? null : auditJson(note.before), after: note.after === undefined ? null : auditJson(note.after), } + + const minimal = MINIMAL_AREAS.find((area) => path.startsWith(area.prefix)) + if (!minimal) return event + const id = idIn(options.rawInput, ["personId", "id"]) ?? (result.ok ? idIn(result.data, ["personId", "id"]) : null) + return { + ...event, + targetType: minimal.targetType, + targetId: id, + targetLabel: null, + reason: null, + input: null, + before: null, + after: null, + } } diff --git a/src/constants.ts b/src/constants.ts index 26d0949..8e0e82e 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -6,3 +6,8 @@ export const MESSAGES_RETENTION_DAYS = 7 export const MODERATION_RETENTION_MONTHS = 24 /** How long revocations of permanent measures stay in the moderation audit log; see `endedMeasures`. */ export const ENDED_MODERATION_RETENTION_DAYS = 1 +/** + * Events in the backend audit log (`common_audit_events`) are deleted this many months after they were written. It + * matches the moderation records' retention; the privacy policy should state it. + */ +export const AUDIT_EVENTS_RETENTION_MONTHS = 24 diff --git a/src/cron.ts b/src/cron.ts index 9d01bb6..8aa131a 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -1,6 +1,6 @@ import { Cron } from "croner" import { lt } from "drizzle-orm" -import { MESSAGES_RETENTION_DAYS } from "./constants" +import { AUDIT_EVENTS_RETENTION_MONTHS, MESSAGES_RETENTION_DAYS } from "./constants" import { DB, SCHEMA } from "./db" import { env } from "./env" import { logger } from "./logger" @@ -11,6 +11,7 @@ export function cron() { logger.info("[CRON] start") await cleanMessages() await cleanAuditLog() + await cleanAuditEvents() logger.info("[CRON] end") }) @@ -63,3 +64,19 @@ export async function cleanAuditLog() { logger.error(err, "[CRON] cleanAuditLog") } } + +/** Deletes the backend audit events past their retention. */ +export async function cleanAuditEvents(now = new Date()) { + logger.info(`[CRON] START(cleanAuditEvents) retention months = ${AUDIT_EVENTS_RETENTION_MONTHS}`) + + const cutoff = new Date(now) + cutoff.setMonth(cutoff.getMonth() - AUDIT_EVENTS_RETENTION_MONTHS) + try { + const deleted = await DB.delete(SCHEMA.COMMON.auditEvents) + .where(lt(SCHEMA.COMMON.auditEvents.createdAt, cutoff)) + .returning({ id: SCHEMA.COMMON.auditEvents.id }) + logger.info(`[CRON] END(cleanAuditEvents) deleted ${deleted.length} audit event(s).`) + } catch (err) { + logger.error(err, "[CRON] cleanAuditEvents") + } +} diff --git a/src/db/schema/common/audit-events.ts b/src/db/schema/common/audit-events.ts index 1852589..aa49856 100644 --- a/src/db/schema/common/audit-events.ts +++ b/src/db/schema/common/audit-events.ts @@ -1,13 +1,12 @@ import { sql } from "drizzle-orm" -import { bigint, index, jsonb, text, timestamp, varchar } from "drizzle-orm/pg-core" +import { bigint, index, jsonb, text, timestamp, uuid, varchar } from "drizzle-orm/pg-core" import { createTable } from "../create-table" -/** Who made a change: the kinds of RFC v3 actors, and `legacy` for anonymous legacy callers. */ +/** Who made a change: the kinds of RFC v3 actors. */ export const AUDIT_ACTOR_KIND = { USER: "user", TELEGRAM: "telegram", SERVICE: "service", - LEGACY: "legacy", } as const export type TAuditActorKind = (typeof AUDIT_ACTOR_KIND)[keyof typeof AUDIT_ACTOR_KIND] @@ -24,15 +23,17 @@ export type TAuditOutcome = (typeof AUDIT_OUTCOME)[keyof typeof AUDIT_OUTCOME] export type AuditJson = null | boolean | number | string | AuditJson[] | { [key: string]: AuditJson } /** - * Every change made through the backend, whoever made it: one row per mutation, written by the tRPC audit middleware - * (`src/audit`). Telegram moderation, recorded by the bot through `tg.auditLog`, is copied here too. + * Who used which permission to change what through the backend: one row per mutation, written by the tRPC audit + * middleware (`src/audit`). Areas with their own history (People, Telegram moderation) keep the details there. */ export const auditEvents = createTable.common( "audit_events", { id: bigint("id", { mode: "number" }).primaryKey().generatedAlwaysAsIdentity(), - /** The procedure path, e.g. `web.faqs.editFaqs`, or `tg.moderation.` for Telegram moderation. */ + /** The procedure path, e.g. `web.faqs.editFaqs`. */ action: text("action").notNull(), + /** Shared by the events of one HTTP request, to match them with what the request asked of other systems. */ + requestId: uuid("request_id"), actorKind: varchar("actor_kind", { length: 16 }).$type().notNull(), actorSub: text("actor_sub"), actorTgId: bigint("actor_tg_id", { mode: "number" }), @@ -55,5 +56,8 @@ export const auditEvents = createTable.common( index("common_audit_events_actor_sub_idx").on(t.actorSub), index("common_audit_events_actor_tg_id_idx").on(t.actorTgId), index("common_audit_events_target_idx").on(t.targetType, t.targetId), + index("common_audit_events_request_id_idx").on(t.requestId), + // The retention cron deletes by age. + index("common_audit_events_created_at_idx").on(t.createdAt), ] ) diff --git a/src/routers/audit.ts b/src/routers/audit.ts index 7d98c31..8b70582 100644 --- a/src/routers/audit.ts +++ b/src/routers/audit.ts @@ -1,4 +1,4 @@ -import { and, desc, eq, ilike, lt, or, type SQL } from "drizzle-orm" +import { and, desc, eq, gte, ilike, lt, or, type SQL } from "drizzle-orm" import { z } from "zod" import { telegramNames, userNames } from "@/audit/names" import { DB, SCHEMA } from "@/db" @@ -8,15 +8,15 @@ import { createTRPCRouter, policy } from "@/trpc" const EVENTS = SCHEMA.COMMON.auditEvents -/** The first segment of an action: where in the ecosystem the change was made. */ -export const AUDIT_AREAS = ["web", "tg", "wa", "groups", "azure", "esign", "auth"] as const +/** The first segment of an action: where in the ecosystem the change was made. A test keeps it in step with the routers. */ +export const AUDIT_AREAS = ["azure", "esign", "groups", "me", "people", "tg", "wa", "web"] as const const escapeLike = (value: string) => value.replace(/[\\%_]/g, (char) => `\\${char}`) export const auditRouter = createTRPCRouter({ /** * Every change made through the backend, newest first: who made it, on what, the state before and after, and the - * outcome. Pages with `cursor`, the `nextCursor` of the previous page. + * outcome. Pages with `cursor`, the `nextCursor` of the previous page; `from` is inclusive, `to` exclusive. */ list: policy(dashboard("audit:read")) .input( @@ -28,6 +28,15 @@ export const auditRouter = createTRPCRouter({ /** Matched against the action, the target, the reason and the error. */ search: z.string().trim().max(100).optional(), target: z.object({ type: z.string().min(1), id: z.string().min(1) }).optional(), + /** Who made the changes: an IdP subject, or a Telegram user. */ + actor: z + .union([ + z.object({ sub: z.string().min(1) }), + z.object({ telegramId: z.number().int().positive().max(Number.MAX_SAFE_INTEGER) }), + ]) + .optional(), + from: z.date().optional(), + to: z.date().optional(), }) ) .query(async ({ input }) => { @@ -36,6 +45,12 @@ export const auditRouter = createTRPCRouter({ if (input.area) conditions.push(ilike(EVENTS.action, `${input.area}.%`)) if (input.outcome) conditions.push(eq(EVENTS.outcome, input.outcome)) if (input.target) conditions.push(eq(EVENTS.targetType, input.target.type), eq(EVENTS.targetId, input.target.id)) + if (input.actor) + conditions.push( + "sub" in input.actor ? eq(EVENTS.actorSub, input.actor.sub) : eq(EVENTS.actorTgId, input.actor.telegramId) + ) + if (input.from) conditions.push(gte(EVENTS.createdAt, input.from)) + if (input.to) conditions.push(lt(EVENTS.createdAt, input.to)) if (input.search) { const pattern = `%${escapeLike(input.search)}%` const matches = or( diff --git a/src/routers/azure/members.ts b/src/routers/azure/members.ts index c806df1..83225a3 100644 --- a/src/routers/azure/members.ts +++ b/src/routers/azure/members.ts @@ -143,7 +143,8 @@ export default createTRPCRouter({ .mutation(async ({ input, ctx }) => { const by = auditActor(ctx.actor) const name = `${input.firstName} ${input.lastName}` - ctx.audit.set({ target: { type: "azure.member", id: input.assocNumber, label: name } }) + // The member's directory id once it is created, like the other member changes. + ctx.audit.set({ target: { type: "azure.member", id: null, label: name } }) const localPart = mailboxLocalPart(input.firstName, input.lastName, input.mailbox) if (localPart === null) return { error: "This name cannot form a standard address: choose a custom one." } const mail = `${localPart}@${MAIL_DOMAIN}` diff --git a/src/routers/esign.ts b/src/routers/esign.ts index ec026f0..ce9bda6 100644 --- a/src/routers/esign.ts +++ b/src/routers/esign.ts @@ -279,12 +279,12 @@ export const esignRouter = createTRPCRouter({ ) .mutation(({ ctx, input }) => { const ids = input.items.map((item) => item.requestId) + // Several requests are named by their number, with the ids in the input. ctx.audit.set({ - target: { - type: "esign.request", - id: ids.join(","), - label: ids.length === 1 ? null : `${ids.length} requests`, - }, + target: + ids.length === 1 + ? { type: "esign.request", id: ids[0] ?? null } + : { type: "esign.request", id: null, label: `${ids.length} requests` }, }) return run(() => esign.createJobs(callerOf(ctx), input.items)) }), diff --git a/src/routers/tg/audit-log.ts b/src/routers/tg/audit-log.ts index 3abf1be..7251a08 100644 --- a/src/routers/tg/audit-log.ts +++ b/src/routers/tg/audit-log.ts @@ -1,9 +1,7 @@ -import type { Actor } from "@polinetwork/auth-kit" import { TRPCError } from "@trpc/server" import { desc, eq } from "drizzle-orm" import { alias } from "drizzle-orm/pg-core" import { z } from "zod" -import type { AuditNote } from "@/audit" import { DB, SCHEMA } from "@/db" import { ARRAY_AUDIT_TYPE, @@ -38,37 +36,6 @@ const RECORDED_BASES = [ AUDIT_BASIS.TELEGRAM_UI, ] as const -/** - * A moderation action as an audit event. When the bot reports it, it was done by the Telegram user the bot names, not - * by the bot; automatic actions and changes made in Telegram's UI have no such user and stay the bot's. - */ -function moderationNote( - action: { - type: TAuditType - targetId: number - groupId: number | null - until: Date | null - reason?: string | null - actorTgId?: number | null - actorSub?: string | null - basis?: string - review?: TAuditReview | null - }, - bot?: Actor -): AuditNote { - const actor: AuditNote["actor"] = - bot && action.actorTgId != null - ? { kind: "telegram", tgId: action.actorTgId, sub: action.actorSub ?? null, client: bot.client } - : undefined - return { - action: `tg.moderation.${action.type}`, - actor, - target: { type: "tg.user", id: action.targetId }, - after: { groupId: action.groupId, until: action.until, basis: action.basis, review: action.review ?? null }, - reason: action.reason ?? null, - } -} - export default createTRPCRouter({ /** * Records a moderation action that already happened on Telegram (RFC v3 §9.2). It is never @@ -139,13 +106,7 @@ export default createTRPCRouter({ if (revoked) await tx.delete(AUDIT_LOG).where(revoked) return row }) - if (inserted) { - ctx.audit.set( - moderationNote({ ...input, actorTgId: input.actorTelegramId, actorSub: subject?.sub ?? null, review }, actor) - ) - return { id: inserted.id, duplicate: false } - } - ctx.audit.skip() + if (inserted) return { id: inserted.id, duplicate: false } const [existing] = await DB.select({ id: AUDIT_LOG.id }) .from(AUDIT_LOG) diff --git a/src/routers/tg/groups.ts b/src/routers/tg/groups.ts index ca6cd47..4b19f82 100644 --- a/src/routers/tg/groups.ts +++ b/src/routers/tg/groups.ts @@ -205,11 +205,8 @@ export default createTRPCRouter({ target: input.length === 1 ? { type: "tg.group", id: input[0].telegramId, label: input[0].title } - : { - type: "tg.group", - id: input.map((group) => group.telegramId).join(","), - label: `${input.length} groups`, - }, + : // Several groups are named by their number, with the ids in the input. + { type: "tg.group", id: null, label: `${input.length} groups` }, before: [...replaced, ...changed.flatMap((row) => known.get(row.telegramId) ?? [])], after: changed, }) diff --git a/src/routers/web/reports.ts b/src/routers/web/reports.ts index 7aa0f55..9aef763 100644 --- a/src/routers/web/reports.ts +++ b/src/routers/web/reports.ts @@ -20,8 +20,11 @@ async function assertGroupExists(groupId: number, type: "tg" | "wa") { if (!groups.length) throw new Error("Group not found") } +/** One report by its id; several by their number, with the ids in the input. */ function reportsTarget(ids: number[]) { - return { type: "web.report", id: ids.join(","), label: ids.length === 1 ? null : `${ids.length} reports` } + return ids.length === 1 + ? { type: "web.report", id: ids[0] ?? null } + : { type: "web.report", id: null, label: `${ids.length} reports` } } export const reports = createTRPCRouter({ diff --git a/src/server.ts b/src/server.ts index e8f5846..9e87680 100644 --- a/src/server.ts +++ b/src/server.ts @@ -46,6 +46,7 @@ app.use( actor: c.req.header(ACTOR_HEADER), }), access: idp?.snapshot ?? null, + requestId: crypto.randomUUID(), }), }) ) diff --git a/src/trpc.ts b/src/trpc.ts index 2e832da..345d01d 100644 --- a/src/trpc.ts +++ b/src/trpc.ts @@ -12,6 +12,8 @@ export type Context = { auth?: RequestAuth /** Permission source for token callers; null denies every permission. */ access?: Pick | null + /** Set by the HTTP adapter, one per request: the audit events of a batched request share it. */ + requestId?: string } /** @@ -69,9 +71,9 @@ const timingMiddleware = t.middleware(async ({ next, path }) => { /** * Records every mutation in `common_audit_events` (see `src/audit`): who called it, with what input, and whether it - * succeeded, failed or was denied. It runs before the policy so that denied calls are kept too. Handlers add the - * target and the state before and after through `ctx.audit`. A failed write is logged and does not fail the call, - * whose change has already happened. + * succeeded, failed or was denied. It runs before the policy so that denied calls are kept too, without their input. + * Handlers add the target and the state before and after through `ctx.audit`. A failed write is logged and does not + * fail the call, whose change has already happened. */ const auditMiddleware = t.middleware(async ({ ctx, path, type, getRawInput, next }) => { if (type !== "mutation" || UNAUDITED.has(path)) return next({ ctx: { audit: noAudit } }) @@ -82,7 +84,7 @@ const auditMiddleware = t.middleware(async ({ ctx, path, type, getRawInput, next try { const rawInput = await getRawInput().catch(() => undefined) - const event = auditEvent({ path, auth: ctx.auth, rawInput, note, result }) + const event = auditEvent({ path, auth: ctx.auth, requestId: ctx.requestId, rawInput, note, result }) if (event) { // Imported here so that loading the router does not open the database. const { writeAuditEvent } = await import("./audit/store") diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts index f00a62f..83e8a0a 100644 --- a/tests/audit-json.test.ts +++ b/tests/audit-json.test.ts @@ -1,5 +1,7 @@ +import type { Actor } from "@polinetwork/auth-kit" +import { TRPCError } from "@trpc/server" import { describe, expect, it } from "vitest" -import { auditJson } from "@/audit" +import { auditEvent, auditJson } from "@/audit" describe("auditJson", () => { it("hides secrets and describes files instead of copying them", () => { @@ -31,4 +33,104 @@ describe("auditJson", () => { expect((copy as { list: unknown[] }).list).toHaveLength(101) expect((copy as { list: unknown[] }).list.at(-1)).toBe("(3 more)") }) + + it("cuts objects with many fields, and copies too large to keep", () => { + const fields = Object.fromEntries(Array.from({ length: 103 }, (_, i) => [`k${i}`, i])) + const copy = auditJson(fields) as Record + expect(Object.keys(copy)).toHaveLength(101) + expect(copy["(more)"]).toBe("3 more fields") + + const large = Object.fromEntries(Array.from({ length: 50 }, (_, i) => [`k${i}`, "x".repeat(2000)])) + expect(auditJson(large)).toMatch(/^\(\d+ characters, too large to keep\)$/) + }) + + it("hides whole secret names and camelCase secret suffixes only", () => { + expect( + auditJson({ code: "1", apiKey: "2", clientSecret: "3", courseCode: "054321", countryCode: "IT", monkey: 1 }) + ).toEqual({ + code: "(hidden)", + apiKey: "(hidden)", + clientSecret: "(hidden)", + courseCode: "054321", + countryCode: "IT", + monkey: 1, + }) + }) +}) + +describe("auditEvent", () => { + const admin: Actor = { kind: "user", client: "admin-dashboard", sub: "usr_admin", telegramId: null } + const auth = { kind: "token", actor: admin, scopes: new Set(["backend:admin"]) } as const + const ok = (data: unknown = undefined) => ({ ok: true as const, data }) + + it("records nothing for calls without a valid token", () => { + const rejected = { kind: "rejected", status: 401, reason: "Missing bearer token" } as const + for (const caller of [undefined, rejected]) + expect(auditEvent({ path: "web.faqs.deleteFaqs", auth: caller, rawInput: {}, note: {}, result: ok() })).toBeNull() + }) + + it("keeps the request id, and no input for denied calls", () => { + const event = auditEvent({ + path: "web.faqs.deleteFaqs", + auth, + requestId: "0b0c8f0e-6a49-4a43-9d53-6a1f9b2f6f0e", + rawInput: { id: 3, padding: "x".repeat(1000) }, + note: {}, + result: { ok: false, error: new TRPCError({ code: "FORBIDDEN", message: "Missing permission" }) }, + }) + expect(event).toMatchObject({ + requestId: "0b0c8f0e-6a49-4a43-9d53-6a1f9b2f6f0e", + outcome: "denied", + input: null, + }) + }) + + it("names several targets by their number, without an id", () => { + const event = auditEvent({ + path: "web.reports.resolve", + auth, + rawInput: { ids: [1, 2] }, + note: { target: { type: "web.report", id: null, label: "2 reports" } }, + result: ok([]), + }) + expect(event).toMatchObject({ targetType: "web.report", targetId: null, targetLabel: "2 reports" }) + }) + + it("keeps only the actor, the action, the person and the outcome for People", () => { + const note = auditEvent({ + path: "people.persons.setNote", + auth, + rawInput: { id: "P-0001", note: "private HR note" }, + note: {}, + result: ok(), + }) + expect(note).toMatchObject({ + action: "people.persons.setNote", + actorSub: "usr_admin", + targetType: "people.person", + targetId: "P-0001", + outcome: "success", + input: null, + before: null, + after: null, + }) + + const created = auditEvent({ + path: "people.persons.create", + auth, + rawInput: { name: "Ada", contactEmail: "ada@example.org", phone: "+39 000" }, + note: {}, + result: ok({ id: "P-0002" }), + }) + expect(created).toMatchObject({ targetId: "P-0002", input: null }) + + const ended = auditEvent({ + path: "people.admins.end", + auth, + rawInput: { personId: "P-0003", reason: "stepped down" }, + note: {}, + result: ok(), + }) + expect(ended).toMatchObject({ targetId: "P-0003", reason: null, input: null }) + }) }) diff --git a/tests/cron-retention.test.ts b/tests/cron-retention.test.ts index 35826ab..b94fdd1 100644 --- a/tests/cron-retention.test.ts +++ b/tests/cron-retention.test.ts @@ -2,25 +2,28 @@ import { beforeEach, describe, expect, it, vi } from "vitest" const state = vi.hoisted(() => { const auditLog = { id: Symbol("tg_audit_log.id") } + const auditEvents = { id: Symbol("common_audit_events.id"), createdAt: Symbol("common_audit_events.created_at") } const ended = Symbol("ended-measures") const returning = vi.fn(async () => [{ id: 1 }]) const where = vi.fn(() => ({ returning })) const deleteFrom = vi.fn(() => ({ where })) const endedMeasures = vi.fn(() => ended) - return { auditLog, deleteFrom, ended, endedMeasures, returning, where } + return { auditEvents, auditLog, deleteFrom, ended, endedMeasures, returning, where } }) vi.mock("croner", () => ({ Cron: vi.fn() })) vi.mock("@/db", () => ({ DB: { delete: state.deleteFrom }, - SCHEMA: { TG: { auditLog: state.auditLog } }, + SCHEMA: { TG: { auditLog: state.auditLog }, COMMON: { auditEvents: state.auditEvents } }, })) vi.mock("@/env", () => ({ env: { NODE_ENV: "test" } })) vi.mock("@/logger", () => ({ logger: { error: vi.fn(), info: vi.fn() } })) vi.mock("@/utils/audit-log", () => ({ endedMeasures: state.endedMeasures })) -const { cleanAuditLog } = await import("@/cron") +vi.mock("drizzle-orm", () => ({ lt: (column: unknown, value: unknown) => ({ lt: [column, value] }) })) + +const { cleanAuditEvents, cleanAuditLog } = await import("@/cron") beforeEach(() => { vi.clearAllMocks() @@ -39,3 +42,12 @@ describe("cleanAuditLog", () => { expect(state.where).toHaveBeenCalledWith(state.ended) }) }) + +describe("cleanAuditEvents", () => { + it("deletes the audit events written more than 24 months ago", async () => { + await cleanAuditEvents(new Date("2026-11-08T04:23:00Z")) + + expect(state.deleteFrom).toHaveBeenCalledWith(state.auditEvents) + expect(state.where).toHaveBeenCalledWith({ lt: [state.auditEvents.createdAt, new Date("2024-11-08T04:23:00Z")] }) + }) +}) diff --git a/tests/idp-router.test.ts b/tests/idp-router.test.ts index 2c87fff..d376b50 100644 --- a/tests/idp-router.test.ts +++ b/tests/idp-router.test.ts @@ -386,28 +386,11 @@ describe("tg.auditLog.record", () => { expect(audited).toEqual([]) }) - it("adds the action to the audit log as done by the moderator, through the bot", async () => { + it("keeps moderation out of the backend audit log, which has no moderation retention", async () => { await record(access(true, ["usr_10"]), { reason: "spam" }) - expect(audited).toEqual([ - expect.objectContaining({ - action: "tg.moderation.ban", - actorKind: "telegram", - actorTgId: 10, - actorSub: "usr_10", - actorClient: "telegram-bot", - targetType: "tg.user", - targetId: "777", - reason: "spam", - outcome: "success", - after: { groupId: -100, until: null, basis: "idp_permission", review: null }, - }), - ]) - expect(audited[0]?.input).toMatchObject({ idempotencyKey: "(hidden)", targetId: 777 }) - }) - - it("leaves automatic actions to the bot", async () => { - await record(access(true, []), { basis: "automatic", actorTelegramId: null }) - expect(audited[0]).toMatchObject({ actorKind: "service", actorTgId: null, actorClient: "telegram-bot" }) + await record(access(true, []), { basis: "automatic", actorTelegramId: null, idempotencyKey: "key-2" }) + expect(db.inserted).toHaveLength(2) + expect(audited).toEqual([]) }) it("deletes the records of the measures a revocation lifts", async () => { @@ -450,6 +433,13 @@ describe("audit log", () => { audited.length = 0 }) + it("filters by every area of the router", async () => { + const { AUDIT_AREAS } = await import("@/routers/audit") + const areas = new Set(procedures().map(([path]) => path.split(".")[0])) + areas.delete("audit") + expect([...AUDIT_AREAS].sort()).toEqual([...areas].sort()) + }) + it("is readable only with audit:read", () => { const meta = procedures().find(([name]) => name === "audit.list")?.[1]._def.meta expect(meta?.policy).toEqual({ user: { scope: "backend:admin", permission: "audit:read" } }) @@ -459,7 +449,7 @@ describe("audit log", () => { await expect(appRouter.createCaller({}).audit.list({})).rejects.toMatchObject({ code: "UNAUTHORIZED" }) }) - it("records a denied mutation with the caller and the input", async () => { + it("records a denied mutation with the caller, without its input", async () => { await expect(caller([]).web.faqs.deleteFaqs({ id: 3 })).rejects.toMatchObject({ code: "FORBIDDEN" }) expect(audited).toEqual([ expect.objectContaining({ @@ -470,7 +460,7 @@ describe("audit log", () => { actorClient: "admin-dashboard", outcome: "denied", error: "Missing permission", - input: { id: 3 }, + input: null, targetType: null, }), ]) From aabaa7f22780a2c51a5983a59a8c50ef086328ed Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Sun, 8 Nov 2026 11:22:12 +0100 Subject: [PATCH 03/12] fix(audit): record states only once they are saved, and summarize invalid inputs From CodeRabbit's review of #90: - An invalid input's error lists its issues instead of the first line of the ZodError's JSON, which was only "[". - A failed read of the IdP directory keeps the last names and waits a full TTL before asking again. - Azure: the new member number is the state after only once Microsoft 365 accepted it, and a member left partly set up is named by its directory id. - E-signing: drive access records the rules as saved, cleaned and sorted, and only once saved. - Group labels: deleting a missing label records nothing, and a modified label's target is its id, never its name. - The Telegram group sync deletes, reads and upserts in one transaction, and the projects reorder reads both orders in its own, so each state before is the one the change replaced. --- src/audit/index.ts | 11 +++++- src/audit/names.ts | 5 ++- src/esign/service.ts | 9 ++--- src/routers/azure/members.ts | 8 ++++- src/routers/esign.ts | 6 ++-- src/routers/groups/labels.ts | 8 +++-- src/routers/tg/groups.ts | 66 +++++++++++++++++++++--------------- src/routers/web/projects.ts | 16 ++++----- tests/audit-json.test.ts | 14 ++++++++ 9 files changed, 93 insertions(+), 50 deletions(-) diff --git a/src/audit/index.ts b/src/audit/index.ts index 7d75bff..83b869f 100644 --- a/src/audit/index.ts +++ b/src/audit/index.ts @@ -1,4 +1,5 @@ import type { TRPCError } from "@trpc/server" +import { ZodError } from "zod/v4" import { AUDIT_ACTOR_KIND, AUDIT_OUTCOME, @@ -52,6 +53,7 @@ const MINIMAL_AREAS = [{ prefix: "people.", targetType: "people.person" }] /** Handed to queries, which change nothing. */ export const noAudit: AuditRecorder = { set: () => {}, skip: () => {} } +/** A fresh recorder for one mutation, with the note it collects and whether the handler skipped the event. */ export function auditRecorder() { const note: AuditNote = {} let skipped = false @@ -112,6 +114,7 @@ export function auditJson(value: unknown): AuditJson { return size > MAX_JSON ? `(${size} characters, too large to keep)` : copy } +/** The caller from the verified token; null without one, so such calls are not recorded. */ function actorOf(auth: RequestAuth | undefined): AuditActor | null { if (auth?.kind !== "token") return null const { actor } = auth @@ -141,14 +144,20 @@ const MAX_ERROR = 500 /** * The root cause's message, on one line: a database error then reads "violates foreign key constraint …" rather than - * the failed query with its parameters. + * the failed query with its parameters, and an invalid input lists its issues rather than their JSON. */ function errorMessage(error: Error) { let cause = error while (cause.cause instanceof Error) cause = cause.cause + if (cause instanceof ZodError) + return cause.issues + .map((issue) => (issue.path.length ? `${issue.path.join(".")}: ${issue.message}` : issue.message)) + .join("; ") + .slice(0, MAX_ERROR) return (cause.message.split("\n")[0] ?? "").slice(0, MAX_ERROR) } +/** Success, failure or denial, from a thrown error or an error the handler returned. */ function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | null } { if (!result.ok) return { diff --git a/src/audit/names.ts b/src/audit/names.ts index 18c5205..18e2a79 100644 --- a/src/audit/names.ts +++ b/src/audit/names.ts @@ -7,6 +7,7 @@ import { decryptUser } from "@/utils/users" const DIRECTORY_TTL_MS = 5 * 60_000 let directory: { at: number; names: Map } | null = null +/** The IdP directory's names by subject, cached for `DIRECTORY_TTL_MS`; empty without a directory. */ async function directoryNames(): Promise> { if (!idp?.directory) return new Map() if (directory && Date.now() - directory.at < DIRECTORY_TTL_MS) return directory.names @@ -15,8 +16,10 @@ async function directoryNames(): Promise> { directory = { at: Date.now(), names: new Map(people.map((person) => [person.sub, person.name])) } } catch (error) { logger.warn({ err: error }, "[AUDIT] cannot read the IdP directory") + // Keeps the last names and waits a full TTL before asking again, rather than on every read. + directory = { at: Date.now(), names: directory?.names ?? new Map() } } - return directory?.names ?? new Map() + return directory.names } /** Display names of IdP subjects: from the IdP's directory, else from the profiles Signatures keeps. */ diff --git a/src/esign/service.ts b/src/esign/service.ts index 189fda0..7d9ce35 100644 --- a/src/esign/service.ts +++ b/src/esign/service.ts @@ -889,13 +889,10 @@ export function createEsignService(options: ServiceOptions) { roles: [...new Set(r.roles.map((k) => k.trim()).filter(Boolean))].sort(), permissions: [...new Set(r.permissions.map((k) => k.trim()).filter(Boolean))].sort(), }) - await store.setDriveAccess({ - driveId, - request: clean(rule.request), - sign: clean(rule.sign), - updatedBySub: caller.sub, - }) + const saved = { request: clean(rule.request), sign: clean(rule.sign) } + await store.setDriveAccess({ driveId, ...saved, updatedBySub: caller.sub }) logger.info({ driveId, by: caller.sub }, "[ESIGN] drive access changed") + return saved }, /** diff --git a/src/routers/azure/members.ts b/src/routers/azure/members.ts index 83225a3..33b7b21 100644 --- a/src/routers/azure/members.ts +++ b/src/routers/azure/members.ts @@ -50,7 +50,7 @@ export default createTRPCRouter({ .output(z.object({ error: z.nullable(z.string()), notificationSent: z.nullable(z.boolean()) })) .mutation(async ({ input, ctx }) => { const by = auditActor(ctx.actor) - ctx.audit.set({ target: { type: "azure.member", id: input.userId }, after: { assocNumber: input.assocNumber } }) + ctx.audit.set({ target: { type: "azure.member", id: input.userId } }) try { const changed = await serializeMemberWrite(async () => { const member = await currentMember(input.userId) @@ -71,6 +71,7 @@ export default createTRPCRouter({ return { error: "Microsoft 365 refused the change, see the backend logs." } } rememberNumber(member, input.assocNumber) + ctx.audit.set({ after: { assocNumber: input.assocNumber } }) return { error: null, member } }) if (changed.error !== null) return { error: changed.error, notificationSent: null } @@ -208,6 +209,11 @@ export default createTRPCRouter({ } } catch (error) { if (error instanceof IncompleteMemberError) { + // The account exists, only partly set up: name it like a created member. + ctx.audit.set({ + target: { type: "azure.member", id: error.member.id, label: name }, + after: { id: error.member.id, assocNumber: input.assocNumber }, + }) logger.error( { error: safeErrorDetail(error.cause), by, memberId: error.member.id }, "[AZURE] member setup incomplete" diff --git a/src/routers/esign.ts b/src/routers/esign.ts index ce9bda6..2b145c5 100644 --- a/src/routers/esign.ts +++ b/src/routers/esign.ts @@ -128,9 +128,11 @@ export const esignRouter = createTRPCRouter({ ctx.audit.set({ target: { type: "esign.drive", id: input.driveId, label: drive?.label }, before: drive && { request: drive.request, sign: drive.sign }, - after: { request: input.request, sign: input.sign }, }) - return run(() => esign.setDriveAccess(caller, input.driveId, { request: input.request, sign: input.sign })) + const saved = await run(() => + esign.setDriveAccess(caller, input.driveId, { request: input.request, sign: input.sign }) + ) + ctx.audit.set({ after: saved }) }), }), diff --git a/src/routers/groups/labels.ts b/src/routers/groups/labels.ts index 7a3e591..713bdb3 100644 --- a/src/routers/groups/labels.ts +++ b/src/routers/groups/labels.ts @@ -69,7 +69,11 @@ export default createTRPCRouter({ .from(GROUP_LABELS) .where(eq(GROUP_LABELS.label, label)) .limit(1) - if (!labelRow.length) return [] + if (!labelRow.length) { + // Nothing to delete, so nothing changed. + ctx.audit.skip() + return [] + } const labelId = labelRow[0].id const [[tgCount], [waCount]] = await Promise.all([ @@ -118,7 +122,7 @@ export default createTRPCRouter({ .returning() ctx.audit.set({ - target: { type: "groups.label", id: before?.id ?? label, label: newLabel ?? label }, + target: { type: "groups.label", id: result[0]?.id ?? before?.id ?? null, label: newLabel ?? label }, before, after: result[0], }) diff --git a/src/routers/tg/groups.ts b/src/routers/tg/groups.ts index 4b19f82..f65fe29 100644 --- a/src/routers/tg/groups.ts +++ b/src/routers/tg/groups.ts @@ -165,42 +165,52 @@ export default createTRPCRouter({ ) .output(z.array(z.number())) .mutation(async ({ input, ctx }) => { - const replaced = [] - for (const group of input) { - replaced.push( - ...(await DB.delete(GROUPS) - .where(and(eq(GROUPS.link, group.link), ne(GROUPS.telegramId, group.telegramId))) - .returning()) - ) - } - const before = await DB.select() - .from(GROUPS) - .where( - inArray( - GROUPS.telegramId, - input.map((group) => group.telegramId) + // One transaction, so the recorded state before is the one the upsert replaced. + const { replaced, before, rows } = await DB.transaction(async (tx) => { + const replaced = [] + for (const group of input) { + replaced.push( + ...(await tx + .delete(GROUPS) + .where(and(eq(GROUPS.link, group.link), ne(GROUPS.telegramId, group.telegramId))) + .returning()) + ) + } + const before = await tx + .select() + .from(GROUPS) + .where( + inArray( + GROUPS.telegramId, + input.map((group) => group.telegramId) + ) ) - ) - const rows = await DB.insert(GROUPS) - .values(input) - .onConflictDoUpdate({ - target: GROUPS.telegramId, - set: { - // this means: use the new value - title: sql.raw(`excluded.${GROUPS.title.name}`), - tag: sql.raw(`excluded.${GROUPS.tag.name}`), - link: sql.raw(`excluded.${GROUPS.link.name}`), - }, - }) - .returning() + const rows = await tx + .insert(GROUPS) + .values(input) + .onConflictDoUpdate({ + target: GROUPS.telegramId, + set: { + // this means: use the new value + title: sql.raw(`excluded.${GROUPS.title.name}`), + tag: sql.raw(`excluded.${GROUPS.tag.name}`), + link: sql.raw(`excluded.${GROUPS.link.name}`), + }, + }) + .returning() + return { replaced, before, rows } + }) // The bot syncs groups often; only what it changes is recorded. const known = new Map(before.map((group) => [group.telegramId, group])) const changed = rows.filter((row) => { const old = known.get(row.telegramId) return !old || old.title !== row.title || old.tag !== row.tag || old.link !== row.link }) - if (replaced.length === 0 && changed.length === 0) ctx.audit.skip() + if (replaced.length === 0 && changed.length === 0) { + ctx.audit.skip() + return rows.map((r) => r.telegramId) + } ctx.audit.set({ target: input.length === 1 diff --git a/src/routers/web/projects.ts b/src/routers/web/projects.ts index c1773cd..0c8e9dc 100644 --- a/src/routers/web/projects.ts +++ b/src/routers/web/projects.ts @@ -122,20 +122,18 @@ export default createTRPCRouter({ }) ) .mutation(async ({ input, ctx }) => { - const order = () => - DB.select({ id: PROJECTS.id, title: PROJECTS.title }).from(PROJECTS).orderBy(asc(PROJECTS.order)) - const before = await order() - await DB.transaction(async (tx) => { + // Both orders are read in the reorder's transaction, so they are the ones it changed. + const { before, after } = await DB.transaction(async (tx) => { + const order = () => + tx.select({ id: PROJECTS.id, title: PROJECTS.title }).from(PROJECTS).orderBy(asc(PROJECTS.order)) + const before = await order() await Promise.all( input.projectIds.map((id, index) => tx.update(PROJECTS).set({ order: index }).where(eq(PROJECTS.id, id))) ) + return { before, after: await order() } }) - ctx.audit.set({ - target: { type: "web.projects", id: "order", label: "Projects order" }, - before, - after: await order(), - }) + ctx.audit.set({ target: { type: "web.projects", id: "order", label: "Projects order" }, before, after }) return { error: null } }), diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts index 83e8a0a..df91bf9 100644 --- a/tests/audit-json.test.ts +++ b/tests/audit-json.test.ts @@ -1,6 +1,7 @@ import type { Actor } from "@polinetwork/auth-kit" import { TRPCError } from "@trpc/server" import { describe, expect, it } from "vitest" +import { z } from "zod/v4" import { auditEvent, auditJson } from "@/audit" describe("auditJson", () => { @@ -85,6 +86,19 @@ describe("auditEvent", () => { }) }) + it("lists the issues of an invalid input rather than their JSON", () => { + const invalid = z.object({ id: z.number(), title: z.string() }).safeParse({ id: "x" }) + const event = auditEvent({ + path: "web.faqs.editFaqs", + auth, + rawInput: { id: "x" }, + note: {}, + result: { ok: false, error: new TRPCError({ code: "BAD_REQUEST", cause: invalid.error }) }, + }) + expect(event?.outcome).toBe("failure") + expect(event?.error).toMatch(/^id: .+; title: .+$/) + }) + it("names several targets by their number, without an id", () => { const event = auditEvent({ path: "web.reports.resolve", From b0f81d588f0ad157b5b1206b73d667ed2afbad32 Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Sun, 8 Nov 2026 11:30:25 +0100 Subject: [PATCH 04/12] fix(audit): read no body for unrecorded calls, and keep caller-chosen values bounded From a review of #90: - The middleware no longer reads the body of calls without a token or of denied calls: tRPC reads bodies lazily, and the policy used to refuse such calls without parsing up to 31 MB of input. - Target ids, labels and reasons are cut like other strings, and a People target is only a valid People id, since a denied or invalid call's input is unchecked. - esign.signers.resetIdentity names its target only after the permission check. - States are recorded only once saved: a grant's period after the insert, a tag after the relation exists, an untag only when the group had the label. - Modifying an unknown label or WhatsApp group records nothing, as nothing changed. --- src/audit/index.ts | 25 ++++++++++++------ src/routers/esign.ts | 2 +- src/routers/groups/labels.ts | 47 ++++++++++++++++----------------- src/routers/tg/grants.ts | 7 ++--- src/routers/wa/groups.ts | 2 ++ src/trpc.ts | 8 +++--- tests/audit-json.test.ts | 25 ++++++++++++++++++ tests/idp-router.test.ts | 50 ++++++++++++++++++++++++++++++++++-- 8 files changed, 122 insertions(+), 44 deletions(-) diff --git a/src/audit/index.ts b/src/audit/index.ts index 83b869f..300f024 100644 --- a/src/audit/index.ts +++ b/src/audit/index.ts @@ -9,6 +9,7 @@ import { type TAuditOutcome, } from "@/db/schema/common/audit-events" import type { RequestAuth } from "@/idp/auth" +import { personIdSchema } from "@/people/contract" export type AuditEvent = typeof auditEvents.$inferInsert @@ -140,6 +141,12 @@ const DENIED_CODES = new Set(["UNAUTHORIZED", "FORBIDDEN"]) type Result = { ok: true; data: unknown } | { ok: false; error: TRPCError } +/** Whether the caller lacked the scope or the permission. */ +export const isDenied = (result: Result) => !result.ok && DENIED_CODES.has(result.error.code) + +/** Target ids, labels and reasons can come from the caller, so they are cut like strings in copies. */ +const cut = (value: string | null) => (value === null ? null : (copyOf(value, 0) as string)) + const MAX_ERROR = 500 /** @@ -168,12 +175,12 @@ function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | nu return error === null ? { outcome: AUDIT_OUTCOME.SUCCESS, error: null } : { outcome: AUDIT_OUTCOME.FAILURE, error } } -/** The first of `keys` that `value` holds as a string or a number. */ -function idIn(value: unknown, keys: string[]): string | null { +/** The first of `keys` that `value` holds as a valid People id: raw inputs are not validated yet. */ +function personIdIn(value: unknown, keys: string[]): string | null { if (value === null || typeof value !== "object") return null for (const key of keys) { - const id = (value as Record)[key] - if (typeof id === "string" || typeof id === "number") return String(id) + const id = personIdSchema.safeParse((value as Record)[key]) + if (id.success) return id.data } return null } @@ -194,6 +201,7 @@ export function auditEvent(options: { const actor = actorOf(auth) if (!actor) return null const { outcome, error } = outcomeOf(result) + const targetId = note.target?.id == null ? null : String(note.target.id) const event: AuditEvent = { action: note.action ?? path, @@ -203,9 +211,9 @@ export function auditEvent(options: { actorTgId: actor.tgId ?? null, actorClient: actor.client ?? null, targetType: note.target?.type ?? null, - targetId: note.target?.id == null ? null : String(note.target.id), - targetLabel: note.target?.label ?? null, - reason: note.reason ?? null, + targetId: cut(targetId), + targetLabel: cut(note.target?.label ?? null), + reason: cut(note.reason ?? null), outcome, error, // A refused call changed nothing, and its input is whatever the caller chose to send. @@ -216,7 +224,8 @@ export function auditEvent(options: { const minimal = MINIMAL_AREAS.find((area) => path.startsWith(area.prefix)) if (!minimal) return event - const id = idIn(options.rawInput, ["personId", "id"]) ?? (result.ok ? idIn(result.data, ["personId", "id"]) : null) + const id = + personIdIn(options.rawInput, ["personId", "id"]) ?? (result.ok ? personIdIn(result.data, ["personId", "id"]) : null) return { ...event, targetType: minimal.targetType, diff --git a/src/routers/esign.ts b/src/routers/esign.ts index 2b145c5..95cc562 100644 --- a/src/routers/esign.ts +++ b/src/routers/esign.ts @@ -101,8 +101,8 @@ export const esignRouter = createTRPCRouter({ }), /** Forgets the CIE bound to a person, e.g. after they renewed their card. */ resetIdentity: user.input(z.object({ sub: z.string().min(1) })).mutation(({ ctx, input }) => { - ctx.audit.set({ target: { type: "esign.signer", id: input.sub } }) requireManage(callerOf(ctx)) + ctx.audit.set({ target: { type: "esign.signer", id: input.sub } }) return run(() => esign.resetIdentity(input.sub)) }), }), diff --git a/src/routers/groups/labels.ts b/src/routers/groups/labels.ts index 713bdb3..0308792 100644 --- a/src/routers/groups/labels.ts +++ b/src/routers/groups/labels.ts @@ -121,6 +121,8 @@ export default createTRPCRouter({ .where(eq(GROUP_LABELS.label, label)) .returning() + // An unknown label changed nothing. + if (result.length === 0) ctx.audit.skip() ctx.audit.set({ target: { type: "groups.label", id: result[0]?.id ?? before?.id ?? null, label: newLabel ?? label }, before, @@ -139,7 +141,7 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const { groupId, type, label } = input - ctx.audit.set({ target: { type: `${type}.group`, id: groupId }, after: { label } }) + ctx.audit.set({ target: { type: `${type}.group`, id: groupId } }) await assertGroupExists(groupId, type) const labelIdResult = await DB.select({ id: GROUP_LABELS.id }) @@ -149,22 +151,12 @@ export default createTRPCRouter({ if (!labelIdResult.length) throw new Error("Label not found") const labelId = labelIdResult[0].id - if (type === "tg") { - const result = await DB.insert(TG_RELATIONS) - .values({ - groupId, - labelId, - }) - .returning() - return result - } - - return DB.insert(WA_RELATIONS) - .values({ - groupId, - labelId, - }) - .returning() + const result = + type === "tg" + ? await DB.insert(TG_RELATIONS).values({ groupId, labelId }).returning() + : await DB.insert(WA_RELATIONS).values({ groupId, labelId }).returning() + ctx.audit.set({ after: { label } }) + return result }), untagGroup: policy(dashboard("groups:labels:write")) @@ -177,7 +169,7 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const { groupId, type, label } = input - ctx.audit.set({ target: { type: `${type}.group`, id: groupId }, before: { label } }) + ctx.audit.set({ target: { type: `${type}.group`, id: groupId } }) await assertGroupExists(groupId, type) @@ -188,12 +180,17 @@ export default createTRPCRouter({ if (!labelIdResult.length) throw new Error("Label not found") const labelId = labelIdResult[0].id - return type === "tg" - ? DB.delete(TG_RELATIONS) - .where(and(eq(TG_RELATIONS.groupId, groupId), eq(TG_RELATIONS.labelId, labelId))) - .returning() - : DB.delete(WA_RELATIONS) - .where(and(eq(WA_RELATIONS.groupId, groupId), eq(WA_RELATIONS.labelId, labelId))) - .returning() + const result = + type === "tg" + ? await DB.delete(TG_RELATIONS) + .where(and(eq(TG_RELATIONS.groupId, groupId), eq(TG_RELATIONS.labelId, labelId))) + .returning() + : await DB.delete(WA_RELATIONS) + .where(and(eq(WA_RELATIONS.groupId, groupId), eq(WA_RELATIONS.labelId, labelId))) + .returning() + // The group may not have had the label: then nothing changed. + if (result.length === 0) ctx.audit.skip() + else ctx.audit.set({ before: { label } }) + return result }), }) diff --git a/src/routers/tg/grants.ts b/src/routers/tg/grants.ts index 5505fc6..5316b27 100644 --- a/src/routers/tg/grants.ts +++ b/src/routers/tg/grants.ts @@ -76,11 +76,7 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const { actor } = ctx - ctx.audit.set({ - target: { type: "tg.user", id: input.userId }, - after: { validSince: input.since, validUntil: input.until }, - reason: input.reason, - }) + ctx.audit.set({ target: { type: "tg.user", id: input.userId }, reason: input.reason }) if (actor.kind !== "user") throw new TRPCError({ code: "FORBIDDEN" }) try { @@ -97,6 +93,7 @@ export default createTRPCRouter({ grantedBySub: actor.sub, grantedViaClient: actor.client, }) + ctx.audit.set({ after: { validSince: input.since, validUntil: input.until } }) if (input.sendTgLog) { const author = await describeActor(actor) diff --git a/src/routers/wa/groups.ts b/src/routers/wa/groups.ts index da33513..a93b53f 100644 --- a/src/routers/wa/groups.ts +++ b/src/routers/wa/groups.ts @@ -70,6 +70,8 @@ export default createTRPCRouter({ const { id, ...values } = input const [before] = await DB.select().from(GROUPS).where(eq(GROUPS.id, id)).limit(1) const [updated] = await DB.update(GROUPS).set(values).where(eq(GROUPS.id, id)).returning() + // An unknown group changed nothing. + if (!updated) ctx.audit.skip() ctx.audit.set({ target: { type: "wa.group", id, label: updated?.title ?? before?.title }, before, diff --git a/src/trpc.ts b/src/trpc.ts index 345d01d..10736ca 100644 --- a/src/trpc.ts +++ b/src/trpc.ts @@ -2,7 +2,7 @@ import type { AccessSnapshotClient, Actor } from "@polinetwork/auth-kit" import { initTRPC, TRPCError } from "@trpc/server" import superjson from "superjson" import { ZodError, z } from "zod/v4" -import { auditEvent, auditRecorder, noAudit, UNAUDITED } from "./audit" +import { auditEvent, auditRecorder, isDenied, noAudit, UNAUDITED } from "./audit" import { env } from "./env" import type { RequestAuth } from "./idp/auth" import { logger } from "./logger" @@ -76,14 +76,16 @@ const timingMiddleware = t.middleware(async ({ next, path }) => { * fail the call, whose change has already happened. */ const auditMiddleware = t.middleware(async ({ ctx, path, type, getRawInput, next }) => { - if (type !== "mutation" || UNAUDITED.has(path)) return next({ ctx: { audit: noAudit } }) + // Calls without a valid token are not recorded, so their bodies are never read for the log. + if (type !== "mutation" || UNAUDITED.has(path) || ctx.auth?.kind !== "token") return next({ ctx: { audit: noAudit } }) const { recorder, note, skipped } = auditRecorder() const result = await next({ ctx: { audit: recorder } }) if (skipped()) return result try { - const rawInput = await getRawInput().catch(() => undefined) + // A denied call's body may never have been read, and the event keeps none of it. + const rawInput = isDenied(result) ? undefined : await getRawInput().catch(() => undefined) const event = auditEvent({ path, auth: ctx.auth, requestId: ctx.requestId, rawInput, note, result }) if (event) { // Imported here so that loading the router does not open the database. diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts index df91bf9..f91a83d 100644 --- a/tests/audit-json.test.ts +++ b/tests/audit-json.test.ts @@ -86,6 +86,31 @@ describe("auditEvent", () => { }) }) + it("cuts target ids, labels and reasons the caller chose", () => { + const event = auditEvent({ + path: "esign.signers.resetIdentity", + auth, + rawInput: {}, + note: { + target: { type: "esign.signer", id: "s".repeat(5000), label: "l".repeat(5000) }, + reason: "r".repeat(5000), + }, + result: ok(), + }) + for (const value of [event?.targetId, event?.targetLabel, event?.reason]) expect(value?.length).toBeLessThan(2100) + }) + + it("names a person only by a valid People id", () => { + const event = auditEvent({ + path: "people.persons.setNote", + auth, + rawInput: { id: "ada@example.org", note: "" }, + note: {}, + result: { ok: false, error: new TRPCError({ code: "BAD_REQUEST", message: "Invalid input" }) }, + }) + expect(event).toMatchObject({ targetType: "people.person", targetId: null, input: null }) + }) + it("lists the issues of an invalid input rather than their JSON", () => { const invalid = z.object({ id: z.number(), title: z.string() }).safeParse({ id: "x" }) const event = auditEvent({ diff --git a/tests/idp-router.test.ts b/tests/idp-router.test.ts index d376b50..3399dab 100644 --- a/tests/idp-router.test.ts +++ b/tests/idp-router.test.ts @@ -433,7 +433,7 @@ describe("audit log", () => { audited.length = 0 }) - it("filters by every area of the router", async () => { + it("offers every area of the router as a filter", async () => { const { AUDIT_AREAS } = await import("@/routers/audit") const areas = new Set(procedures().map(([path]) => path.split(".")[0])) areas.delete("audit") @@ -481,11 +481,57 @@ describe("audit log", () => { targetType: "tg.user", targetId: "456", reason: "exam week", - after: { validSince: since.toISOString(), validUntil: until.toISOString() }, + // Nothing was saved, so there is no state after. + after: null, }), ]) }) + it("does not read the body of a call it will not record", async () => { + const { fetchRequestHandler } = await import("@trpc/server/adapters/fetch") + const send = async (auth: Context["auth"]) => { + let read = false + // No buffering, so `pull` runs only when someone reads the body. + const body = new ReadableStream( + { + pull(controller) { + read = true + controller.enqueue(new TextEncoder().encode(JSON.stringify({ json: { id: 3 } }))) + controller.close() + }, + }, + { highWaterMark: 0 } + ) + const res = await fetchRequestHandler({ + endpoint: "/trpc", + router: appRouter, + req: new Request("http://backend/trpc/web.faqs.deleteFaqs", { + method: "POST", + headers: { "content-type": "application/json" }, + body, + duplex: "half", + } as RequestInit), + createContext: () => ({ + auth, + access: { + has: () => false, + current: () => null, + subjectBySub: () => undefined, + subjectByTelegramId: () => undefined, + status: () => ({ lastSyncAt: 1, fresh: true, generation: 1, subjects: 1 }), + }, + }), + }) + return { status: res.status, read } + } + expect(await send(undefined)).toEqual({ status: 401, read: false }) + expect(await send({ kind: "token", actor: admin, scopes: new Set(["backend:admin"]) })).toEqual({ + status: 403, + read: false, + }) + expect(audited).toEqual([expect.objectContaining({ outcome: "denied", input: null })]) + }) + it("does not record calls refused before the caller is known, nor queries", async () => { await expect(caller([], false).web.faqs.deleteFaqs({ id: 3 })).rejects.toMatchObject({ code: "UNAUTHORIZED", From 675164902873a67e4aa8e14468cceced0d14aabb Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Thu, 12 Nov 2026 00:19:59 +0100 Subject: [PATCH 05/12] feat(audit): declare how each mutation is recorded in its procedure meta (#121) Every mutation now says how the audit log records it, next to its policy, and a router test fails when one does not, so a new area cannot store full inputs without anyone deciding it: - audit.full({ fields?, target? }): the input (only `fields`, when given), the reason and the states the handler adds. - audit.minimal({ target? }): the actor, the action, the target and the outcome. - audit.none(why): not recorded, with the reason in the code. The option replaces the UNAUDITED set and the temporary rule for People: - People's base procedure is minimal, with the person taken from a valid People id in the input or the created record; the router test checks both. - The bot's ingestion, its moderation reports and the e-signing profile sync are none. - E-signing keeps the request's title, document, signers and output, not the email recipients or message, and its state after names the signers by subject and position, without their fiscal codes or certificates. A decline keeps its id and reason. - Azure keeps the member's directory id and number, not names, the notification address or the new mailbox address. The helpers live in src/audit/option.ts, which imports nothing, so declaring an option does not load the database schema. A mutation without one is recorded as minimal and logged. --- README.md | 27 ++++--- src/audit/index.ts | 80 +++++++----------- src/audit/option.ts | 42 ++++++++++ src/people/router.ts | 48 +++++++---- src/routers/azure/members.ts | 5 +- src/routers/esign.ts | 85 ++++++++++++++------ src/routers/groups/labels.ts | 6 ++ src/routers/tg/audit-log.ts | 2 + src/routers/tg/grants.ts | 3 + src/routers/tg/groups.ts | 5 ++ src/routers/tg/messages.ts | 2 + src/routers/tg/users.ts | 2 + src/routers/wa/groups.ts | 5 ++ src/routers/web/associations.ts | 5 ++ src/routers/web/faqs.ts | 7 ++ src/routers/web/freshman.ts | 4 + src/routers/web/guides_matricole.ts | 3 + src/routers/web/projects.ts | 5 ++ src/routers/web/reports.ts | 4 + src/trpc.ts | 26 +++--- tests/audit-json.test.ts | 85 ++++++++++++++------ tests/azure-router.test.ts | 2 +- tests/group-label-platform-isolation.test.ts | 1 + tests/guides-latest.test.ts | 1 + tests/idp-router.test.ts | 34 +++++++- tests/tg-users-creation-history.test.ts | 1 + 26 files changed, 355 insertions(+), 135 deletions(-) create mode 100644 src/audit/option.ts diff --git a/README.md b/README.md index d562575..74227b2 100644 --- a/README.md +++ b/README.md @@ -62,17 +62,22 @@ The IdP notifies `POST /internal/events` when access changes; the backend also p `common_audit_events` records who used which permission to change what through the backend. The tRPC audit middleware (`src/audit`) writes one event per mutation: the action (the procedure path), the caller from the token, the target, the outcome (`success`, `failure` when an error is thrown or returned, `denied` when the policy refuses the caller), the -request id shared by the calls of one HTTP request, and when. Handlers add the target and the state before and after -with `ctx.audit.set(...)`, or `ctx.audit.skip()` when a call changed nothing. Inputs and states are stored without -secrets (`password`, `token`, `ticket`, `code`, `*Key`, `*Secret` fields) or file contents, and are cut when too large. -Denied calls keep no input. Calls without a valid token are not recorded. - -Areas with their own history keep the details there, readable with their own permissions: - -- People: events name the actor, the action, the person and the outcome, without the input or the states; what changed - is in the person's activity history. -- Telegram moderation: kept in `tg_audit_log` under its own retention rules and not copied here, like the bot's - ingestion (`tg.messages.add`, `tg.users.add`). +request id shared by the calls of one HTTP request, and when. Calls without a valid token are not recorded. + +Every mutation declares how it is recorded in its procedure meta, and a test walks the router so that none ships +without one, like its policy: + +- `.meta(audit.full())`: also the input, the reason and the states before and after that the handler adds with + `ctx.audit.set(...)`. `fields` keeps only those fields of the input, for inputs that carry personal data or free + text not worth keeping. +- `.meta(audit.minimal({ target }))`: only the actor, the action, the target and the outcome, for areas that keep their + own history behind their own permissions. People records each change in the person's activity history. +- `.meta(audit.none(why))`: not recorded, e.g. the bot's ingestion, or Telegram moderation, which `tg_audit_log` keeps + under its own retention rules. + +Handlers call `ctx.audit.skip()` when a call changed nothing. Inputs and states are stored without secrets +(`password`, `token`, `ticket`, `code`, `*Key`, `*Secret` fields) or file contents, and are cut when too large. Denied +calls keep no input. Events are deleted 24 months after they were written (`AUDIT_EVENTS_RETENTION_MONTHS`). Dashboard users with the IdP permission `audit:read` read the log with `audit.list`, newest first, paged by `cursor` and filtered by area, outcome, diff --git a/src/audit/index.ts b/src/audit/index.ts index 300f024..4b8bc75 100644 --- a/src/audit/index.ts +++ b/src/audit/index.ts @@ -9,15 +9,10 @@ import { type TAuditOutcome, } from "@/db/schema/common/audit-events" import type { RequestAuth } from "@/idp/auth" -import { personIdSchema } from "@/people/contract" +import type { AuditOption, AuditTarget } from "./option" export type AuditEvent = typeof auditEvents.$inferInsert - -/** - * What a change was made to; `label` is a readable name, kept as it was at the time. `id` is null when the call acts - * on several records, which are then named in the input. - */ -export type AuditTarget = { type: string; id: string | number | null; label?: string | null } +export { type AuditOption, type AuditTarget, type AuditTargetOf, audit } from "./option" export type AuditActor = { kind: TAuditActorKind; sub?: string | null; tgId?: number | null; client?: string | null } @@ -37,19 +32,8 @@ export type AuditRecorder = { skip: () => void } -/** - * Mutations that are not changes made by someone: the bot's high-volume ingestion and per-visit syncs. Moderation the - * bot reports is kept in `tg_audit_log`, under its own retention rules, and not copied here. - */ -export const UNAUDITED = new Set(["tg.messages.add", "tg.users.add", "tg.auditLog.record", "esign.profile.sync"]) - -/** - * Areas that keep their own history, readable with their own permissions: their events name the actor, the action, - * the target and the outcome, without the input or the state before and after. - * - * Temporary: a per-procedure `audit` option in the procedure meta replaces this list. - */ -const MINIMAL_AREAS = [{ prefix: "people.", targetType: "people.person" }] +/** For a mutation without an option, which the router test keeps from shipping: the option that stores the least. */ +export const UNDECLARED: AuditOption = { capture: "minimal" } /** Handed to queries, which change nothing. */ export const noAudit: AuditRecorder = { set: () => {}, skip: () => {} } @@ -175,14 +159,10 @@ function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | nu return error === null ? { outcome: AUDIT_OUTCOME.SUCCESS, error: null } : { outcome: AUDIT_OUTCOME.FAILURE, error } } -/** The first of `keys` that `value` holds as a valid People id: raw inputs are not validated yet. */ -function personIdIn(value: unknown, keys: string[]): string | null { - if (value === null || typeof value !== "object") return null - for (const key of keys) { - const id = personIdSchema.safeParse((value as Record)[key]) - if (id.success) return id.data - } - return null +/** Only `fields` of a copied input, when it is an object. */ +function onlyFields(copy: AuditJson, fields: readonly string[] | undefined): AuditJson { + if (!fields || copy === null || typeof copy !== "object" || Array.isArray(copy)) return copy + return Object.fromEntries(fields.flatMap((field) => (field in copy ? [[field, copy[field] ?? null]] : []))) } /** @@ -191,49 +171,49 @@ function personIdIn(value: unknown, keys: string[]): string | null { */ export function auditEvent(options: { path: string + option?: AuditOption auth: RequestAuth | undefined requestId?: string | null rawInput: unknown note: AuditNote result: Result }): AuditEvent | null { - const { path, auth, note, result } = options - const actor = actorOf(auth) + const { path, note, result } = options + const option = options.option ?? UNDECLARED + if (option.capture === "none") return null + const actor = actorOf(options.auth) if (!actor) return null const { outcome, error } = outcomeOf(result) - const targetId = note.target?.id == null ? null : String(note.target.id) + // What the handler names wins over the option's target, which only sees the raw input and the result. + const target = + note.target ?? option.target?.({ input: options.rawInput, data: result.ok ? result.data : undefined }) ?? undefined + const targetId = target?.id == null ? null : String(target.id) const event: AuditEvent = { - action: note.action ?? path, + action: note.action ?? option.action ?? path, requestId: options.requestId ?? null, actorKind: actor.kind, actorSub: actor.sub ?? null, actorTgId: actor.tgId ?? null, actorClient: actor.client ?? null, - targetType: note.target?.type ?? null, + targetType: target?.type ?? null, targetId: cut(targetId), - targetLabel: cut(note.target?.label ?? null), - reason: cut(note.reason ?? null), + targetLabel: cut(target?.label ?? null), + reason: null, outcome, error, - // A refused call changed nothing, and its input is whatever the caller chose to send. - input: outcome === AUDIT_OUTCOME.DENIED ? null : auditJson(options.rawInput), - before: note.before === undefined ? null : auditJson(note.before), - after: note.after === undefined ? null : auditJson(note.after), + input: null, + before: null, + after: null, } + if (option.capture === "minimal") return event - const minimal = MINIMAL_AREAS.find((area) => path.startsWith(area.prefix)) - if (!minimal) return event - const id = - personIdIn(options.rawInput, ["personId", "id"]) ?? (result.ok ? personIdIn(result.data, ["personId", "id"]) : null) return { ...event, - targetType: minimal.targetType, - targetId: id, - targetLabel: null, - reason: null, - input: null, - before: null, - after: null, + reason: cut(note.reason ?? null), + // A refused call changed nothing, and its input is whatever the caller chose to send. + input: outcome === AUDIT_OUTCOME.DENIED ? null : onlyFields(auditJson(options.rawInput), option.fields), + before: note.before === undefined ? null : auditJson(note.before), + after: note.after === undefined ? null : auditJson(note.after), } } diff --git a/src/audit/option.ts b/src/audit/option.ts new file mode 100644 index 0000000..1d996f8 --- /dev/null +++ b/src/audit/option.ts @@ -0,0 +1,42 @@ +// No imports: routers declare their audit options with this module without loading the database schema. + +/** + * What a change was made to; `label` is a readable name, kept as it was at the time. `id` is null when the call acts + * on several records, which are then named in the input. + */ +export type AuditTarget = { type: string; id: string | number | null; label?: string | null } + +/** + * The target of a call, from its input and, once it succeeded, its result. The input is the raw one, not yet validated + * (and undefined for denied calls, whose body is not read), so a target function checks what it takes from it. + */ +export type AuditTargetOf = (call: { input: unknown; data: unknown }) => AuditTarget | null + +/** + * How a mutation is recorded, declared in its procedure meta with `.meta(audit.full())` and the like. A test walks the + * router so that every mutation declares one. + */ +export type AuditOption = + /** Not recorded; `why` says why, e.g. the bot's ingestion or an area that keeps its own log. */ + | { capture: "none"; why: string } + /** + * The actor, the action, the target and the outcome, without the input, the states or the reason: for areas that + * keep their own history, readable with their own permissions. + */ + | { capture: "minimal"; target?: AuditTargetOf; action?: string } + /** + * Also the input, the states the handler adds and the reason. `fields` keeps only those fields of the input, for + * inputs that carry personal data or free text not worth keeping. + */ + | { capture: "full"; fields?: readonly string[]; target?: AuditTargetOf; action?: string } + +/** Procedure meta for each way of recording a mutation. */ +export const audit = { + full: (options: Omit, "capture"> = {}) => ({ + audit: { capture: "full", ...options } satisfies AuditOption, + }), + minimal: (options: Omit, "capture"> = {}) => ({ + audit: { capture: "minimal", ...options } satisfies AuditOption, + }), + none: (why: string) => ({ audit: { capture: "none", why } satisfies AuditOption }), +} diff --git a/src/people/router.ts b/src/people/router.ts index 8f09c70..31864df 100644 --- a/src/people/router.ts +++ b/src/people/router.ts @@ -1,5 +1,6 @@ import { TRPCError } from "@trpc/server" import { z } from "zod" +import { type AuditTargetOf, audit } from "@/audit/option" import { SCOPE } from "@/idp/policies" import { createTRPCRouter, policy } from "@/trpc" import * as C from "./contract" @@ -7,6 +8,21 @@ import { requireGeneral } from "./model" import type { PeopleService } from "./service" import type { Viewer } from "./types" +/** + * The person a People call is about: the input's `personId` or `id`, else the created record's. The raw input is not + * validated yet, so only a valid People id is taken from it. + */ +const personTarget: AuditTargetOf = ({ input, data }) => { + for (const value of [input, data]) { + if (value === null || typeof value !== "object") continue + for (const key of ["personId", "id"]) { + const id = C.personIdSchema.safeParse((value as Record)[key]) + if (id.success) return { type: "people.person", id: id.data } + } + } + return { type: "people.person", id: null } +} + /** * Factory keeps policy and shape tests independent of databases and external services. * @@ -14,20 +30,24 @@ import type { Viewer } from "./types" * are open to every dashboard user here, since a team's Lead may add and remove its members. */ export function createPeopleRouter(people: PeopleService) { - const user = policy({ user: { scope: SCOPE.admin } }).use(async ({ ctx, next }) => { - if (ctx.actor.kind !== "user") throw new TRPCError({ code: "FORBIDDEN" }) - const sub = ctx.actor.sub - const viewer: Viewer = { - sub, - telegramId: ctx.actor.telegramId, - can: ctx.can, - holdsRole: (role) => { - const index = ctx.access?.current() - return !!index && index.hasRole(index.bySub(sub), role) - }, - } - return next({ ctx: { viewer } }) - }) + // People keeps each record's history in `people_activity`, behind its own permissions: the audit log only says who + // changed which record, and whether it worked. + const user = policy({ user: { scope: SCOPE.admin } }) + .meta(audit.minimal({ target: personTarget })) + .use(async ({ ctx, next }) => { + if (ctx.actor.kind !== "user") throw new TRPCError({ code: "FORBIDDEN" }) + const sub = ctx.actor.sub + const viewer: Viewer = { + sub, + telegramId: ctx.actor.telegramId, + can: ctx.can, + holdsRole: (role) => { + const index = ctx.access?.current() + return !!index && index.hasRole(index.bySub(sub), role) + }, + } + return next({ ctx: { viewer } }) + }) const admin = (permission: string) => user.use(({ ctx, next }) => { requireGeneral(ctx.viewer, permission) diff --git a/src/routers/azure/members.ts b/src/routers/azure/members.ts index 33b7b21..c12874a 100644 --- a/src/routers/azure/members.ts +++ b/src/routers/azure/members.ts @@ -1,5 +1,6 @@ import type { Actor } from "@polinetwork/auth-kit" import z from "zod" +import { audit } from "@/audit/option" import { azureDirectory } from "@/azure/directory" import { IncompleteMemberError, safeErrorDetail } from "@/azure/errors" import { MAIL_DOMAIN, mailboxLocalPart, mailboxSchema } from "@/azure/mailbox" @@ -40,6 +41,7 @@ export default createTRPCRouter({ * optionally email the new number. Token callers only: it can send mail to a caller-supplied address. */ setAssocNumber: policy(dashboard("azure:members:write")) + .meta(audit.full({ fields: ["userId", "assocNumber"] })) .input( z.object({ userId: z.string().min(1), @@ -121,6 +123,7 @@ export default createTRPCRouter({ * caller-supplied group or existing user. The address is `firstname.lastname` unless a custom one is chosen. */ create: policy(dashboard("azure:members:create")) + .meta(audit.full({ fields: ["assocNumber", "mailbox"] })) .input( z.object({ firstName: personName, @@ -187,7 +190,7 @@ export default createTRPCRouter({ logger.info({ by, memberId: member.id, assocNumber: input.assocNumber }, "[AZURE] member created") ctx.audit.set({ target: { type: "azure.member", id: member.id, label: name }, - after: { id: member.id, email: member.mail, assocNumber: input.assocNumber }, + after: { id: member.id, assocNumber: input.assocNumber }, }) let mailOk = false diff --git a/src/routers/esign.ts b/src/routers/esign.ts index 95cc562..0fb0276 100644 --- a/src/routers/esign.ts +++ b/src/routers/esign.ts @@ -2,6 +2,7 @@ import type { Actor } from "@polinetwork/auth-kit" import { TRPCError } from "@trpc/server" import { z } from "zod" import type { AuditRecorder } from "@/audit" +import { audit } from "@/audit/option" import { esign } from "@/esign" import { MAX_PDF_BYTES } from "@/esign/drive/types" import { EsignError, type EsignErrorCode } from "@/esign/errors" @@ -90,6 +91,7 @@ export const esignRouter = createTRPCRouter({ profile: createTRPCRouter({ /** The BFF passes the signed-in user's name and email from the ID token. */ sync: user + .meta(audit.none("Copies the signed-in user's name and email on each visit, not a change made by someone")) .input(z.object({ name: z.string().min(1).max(200), email: z.email().nullable() })) .mutation(({ ctx, input }) => run(() => esign.syncProfile(callerOf(ctx), input))), }), @@ -100,11 +102,14 @@ export const esignRouter = createTRPCRouter({ return run(() => esign.listProfiles()) }), /** Forgets the CIE bound to a person, e.g. after they renewed their card. */ - resetIdentity: user.input(z.object({ sub: z.string().min(1) })).mutation(({ ctx, input }) => { - requireManage(callerOf(ctx)) - ctx.audit.set({ target: { type: "esign.signer", id: input.sub } }) - return run(() => esign.resetIdentity(input.sub)) - }), + resetIdentity: user + .meta(audit.full()) + .input(z.object({ sub: z.string().min(1) })) + .mutation(({ ctx, input }) => { + requireManage(callerOf(ctx)) + ctx.audit.set({ target: { type: "esign.signer", id: input.sub } }) + return run(() => esign.resetIdentity(input.sub)) + }), }), drive: createTRPCRouter({ @@ -121,6 +126,7 @@ export const esignRouter = createTRPCRouter({ .input(z.object({ driveId: z.string().min(1), sign: driveRule })) .query(({ ctx, input }) => run(() => esign.driveAccessImpact(callerOf(ctx), input.driveId, input.sign))), setAccess: user + .meta(audit.full()) .input(z.object({ driveId: z.string().min(1), request: driveRule, sign: driveRule })) .mutation(async ({ ctx, input }) => { const caller = callerOf(ctx) @@ -138,6 +144,7 @@ export const esignRouter = createTRPCRouter({ documents: createTRPCRouter({ upload: user + .meta(audit.full()) .input( z .instanceof(FormData) @@ -157,6 +164,7 @@ export const esignRouter = createTRPCRouter({ return { document, pdf: publicPdf(pdf) } }), fromDrive: user + .meta(audit.full()) .input(z.object({ driveId: z.string().min(1), itemId: z.string().min(1) })) .mutation(async ({ ctx, input }) => { const caller = callerOf(ctx) @@ -182,6 +190,7 @@ export const esignRouter = createTRPCRouter({ requests: createTRPCRouter({ create: user + .meta(audit.full({ fields: ["title", "documentId", "ordered", "signers", "output"] })) .input( z.object({ title: z.string().trim().min(1).max(200), @@ -213,7 +222,22 @@ export const esignRouter = createTRPCRouter({ .mutation(async ({ ctx, input }) => { ctx.audit.set({ target: { type: "esign.document", id: input.documentId, label: input.title } }) const request = await run(() => esign.createRequest(callerOf(ctx), input)) - ctx.audit.set({ target: { type: "esign.request", id: request.id, label: request.title }, after: request }) + // Who signs, in which order: not their fiscal codes, certificates or the email recipients. + ctx.audit.set({ + target: { type: "esign.request", id: request.id, label: request.title }, + after: { + id: request.id, + title: request.title, + status: request.status, + ordered: request.ordered, + drives: request.drives, + signers: request.signers.map((signer) => ({ + sub: signer.sub, + role: signer.role, + position: signer.position, + })), + }, + }) return request }), list: user @@ -222,15 +246,22 @@ export const esignRouter = createTRPCRouter({ get: user .input(z.object({ id: z.uuid() })) .query(({ ctx, input }) => run(() => esign.getRequest(callerOf(ctx), input.id))), - cancel: user.input(z.object({ id: z.uuid() })).mutation(async ({ ctx, input }) => { - await auditRequest(ctx, input.id) - return run(() => esign.cancelRequest(callerOf(ctx), input.id)) - }), - delete: user.input(z.object({ id: z.uuid() })).mutation(async ({ ctx, input }) => { - await auditRequest(ctx, input.id) - return run(() => esign.deleteRequest(callerOf(ctx), input.id)) - }), + cancel: user + .meta(audit.full()) + .input(z.object({ id: z.uuid() })) + .mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.id) + return run(() => esign.cancelRequest(callerOf(ctx), input.id)) + }), + delete: user + .meta(audit.full()) + .input(z.object({ id: z.uuid() })) + .mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.id) + return run(() => esign.deleteRequest(callerOf(ctx), input.id)) + }), decline: user + .meta(audit.full({ fields: ["id"] })) .input(z.object({ id: z.uuid(), reason: z.string().max(500).nullable() })) .mutation(async ({ ctx, input }) => { await auditRequest(ctx, input.id) @@ -242,6 +273,7 @@ export const esignRouter = createTRPCRouter({ * once no deployed dashboard calls it, this and the service's `uploadSigned` can be removed. */ uploadSigned: user + .meta(audit.full()) .input( z .instanceof(FormData) @@ -259,10 +291,13 @@ export const esignRouter = createTRPCRouter({ esign.uploadSigned(callerOf(ctx), input.requestId, new Uint8Array(await input.file.arrayBuffer())) ) }), - retryDelivery: user.input(z.object({ id: z.uuid() })).mutation(async ({ ctx, input }) => { - await auditRequest(ctx, input.id) - return run(() => esign.retryDelivery(callerOf(ctx), input.id)) - }), + retryDelivery: user + .meta(audit.full()) + .input(z.object({ id: z.uuid() })) + .mutation(async ({ ctx, input }) => { + await auditRequest(ctx, input.id) + return run(() => esign.retryDelivery(callerOf(ctx), input.id)) + }), }), jobs: createTRPCRouter({ @@ -271,6 +306,7 @@ export const esignRouter = createTRPCRouter({ * ticket, so one PIN signs them all. `places` moves the caller's boxes before signing, one per place, in order. */ create: user + .meta(audit.full()) .input( z.object({ items: z @@ -293,10 +329,13 @@ export const esignRouter = createTRPCRouter({ statuses: user .input(z.object({ jobIds: z.array(z.uuid()).min(1).max(10) })) .query(({ ctx, input }) => run(() => esign.jobStatuses(callerOf(ctx), input.jobIds))), - cancel: user.input(z.object({ jobIds: z.array(z.uuid()).min(1).max(10) })).mutation(({ ctx, input }) => - run(async () => { - for (const jobId of input.jobIds) await esign.cancelJob(callerOf(ctx), jobId) - }) - ), + cancel: user + .meta(audit.full()) + .input(z.object({ jobIds: z.array(z.uuid()).min(1).max(10) })) + .mutation(({ ctx, input }) => + run(async () => { + for (const jobId of input.jobIds) await esign.cancelJob(callerOf(ctx), jobId) + }) + ), }), }) diff --git a/src/routers/groups/labels.ts b/src/routers/groups/labels.ts index 0308792..b5260ff 100644 --- a/src/routers/groups/labels.ts +++ b/src/routers/groups/labels.ts @@ -1,5 +1,6 @@ import { and, eq, sql } from "drizzle-orm" import { z } from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { authorOf } from "@/idp/author" import { dashboard } from "@/idp/policies" @@ -32,6 +33,7 @@ export default createTRPCRouter({ }), create: policy(dashboard("groups:labels:write")) + .meta(audit.full()) .input( z.object({ label, @@ -57,6 +59,7 @@ export default createTRPCRouter({ }), delete: policy(dashboard("groups:labels:write")) + .meta(audit.full()) .input( z.object({ label, @@ -93,6 +96,7 @@ export default createTRPCRouter({ }), modify: policy(dashboard("groups:labels:write")) + .meta(audit.full()) .input( z.object({ label, @@ -132,6 +136,7 @@ export default createTRPCRouter({ }), tagGroup: policy(dashboard("groups:labels:write")) + .meta(audit.full()) .input( z.object({ groupId: z.number(), @@ -160,6 +165,7 @@ export default createTRPCRouter({ }), untagGroup: policy(dashboard("groups:labels:write")) + .meta(audit.full()) .input( z.object({ groupId: z.number(), diff --git a/src/routers/tg/audit-log.ts b/src/routers/tg/audit-log.ts index 7251a08..558a268 100644 --- a/src/routers/tg/audit-log.ts +++ b/src/routers/tg/audit-log.ts @@ -2,6 +2,7 @@ import { TRPCError } from "@trpc/server" import { desc, eq } from "drizzle-orm" import { alias } from "drizzle-orm/pg-core" import { z } from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { ARRAY_AUDIT_TYPE, @@ -44,6 +45,7 @@ export default createTRPCRouter({ * `idempotencyKey` records nothing and returns the first record. */ record: policy({ service: { scope: SCOPE.tgAudit } }) + .meta(audit.none("Moderation is kept in tg_audit_log, under its own retention rules")) .input( z .object({ diff --git a/src/routers/tg/grants.ts b/src/routers/tg/grants.ts index 5316b27..d11f149 100644 --- a/src/routers/tg/grants.ts +++ b/src/routers/tg/grants.ts @@ -1,6 +1,7 @@ import { TRPCError } from "@trpc/server" import { and, eq, gt, inArray, isNull } from "drizzle-orm" import { z } from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { describeActor } from "@/idp/actor-info" import { ACT_AS_SCOPE } from "@/idp/auth" @@ -53,6 +54,7 @@ export default createTRPCRouter({ * grantor is the token's user. */ create: policy(dashboard("tg:grants:manage")) + .meta(audit.full()) .input( z.object({ userId: z.number(), @@ -121,6 +123,7 @@ export default createTRPCRouter({ telegram: { scope: ACT_AS_SCOPE, permission: "tg:grants:manage" }, ...dashboard("tg:grants:manage"), }) + .meta(audit.full()) .input( z.object({ userId: z.number(), diff --git a/src/routers/tg/groups.ts b/src/routers/tg/groups.ts index f65fe29..7ee1ec9 100644 --- a/src/routers/tg/groups.ts +++ b/src/routers/tg/groups.ts @@ -1,5 +1,6 @@ import { and, eq, exists, ilike, inArray, ne, not, notExists, or, type SQL, sql } from "drizzle-orm" import { z } from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { describeActor } from "@/idp/actor-info" import { botReadOrDashboard, dashboard, SCOPE, seesHiddenGroups } from "@/idp/policies" @@ -153,6 +154,7 @@ export default createTRPCRouter({ }), create: policy({ service: { scope: SCOPE.tgGroupsSync } }) + .meta(audit.full()) .input( z.array( z.object({ @@ -224,6 +226,7 @@ export default createTRPCRouter({ }), delete: policy({ service: { scope: SCOPE.tgGroupsSync } }) + .meta(audit.full()) .input( z.object({ telegramId: z.number(), @@ -237,6 +240,7 @@ export default createTRPCRouter({ }), setHide: policy(dashboard("tg:groups:manage")) + .meta(audit.full()) .input( z.object({ telegramId: z.number(), @@ -263,6 +267,7 @@ export default createTRPCRouter({ /** The performer is the token's user. */ leaveChat: policy(dashboard("tg:groups:manage")) + .meta(audit.full()) .input( z.object({ chatId: z.number(), diff --git a/src/routers/tg/messages.ts b/src/routers/tg/messages.ts index 13fb7e7..7198c27 100644 --- a/src/routers/tg/messages.ts +++ b/src/routers/tg/messages.ts @@ -1,5 +1,6 @@ import { and, desc, eq } from "drizzle-orm" import { z } from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { botReadOrDashboard, SCOPE } from "@/idp/policies" import { logger } from "@/logger" @@ -78,6 +79,7 @@ export default createTRPCRouter({ }), add: policy({ service: { scope: SCOPE.tgIngest } }) + .meta(audit.none("The bot's ingestion of group messages, not changes made by someone")) .input(z.object({ messages: z.array(message) })) .output(z.object({ error: z.union([z.null(), z.enum(["ENCRYPT_ERROR"])]) })) .mutation(async ({ input }) => { diff --git a/src/routers/tg/users.ts b/src/routers/tg/users.ts index a458289..8fbbb15 100644 --- a/src/routers/tg/users.ts +++ b/src/routers/tg/users.ts @@ -1,5 +1,6 @@ import { count, eq, sql } from "drizzle-orm" import { z } from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { botReadOrDashboard, dashboard, SCOPE } from "@/idp/policies" import { logger } from "@/logger" @@ -138,6 +139,7 @@ export default createTRPCRouter({ }), add: policy({ service: { scope: SCOPE.tgIngest } }) + .meta(audit.none("The bot's ingestion of the users it sees, not changes made by someone")) .input(z.object({ users: z.array(TgUserSchema) })) .output( z.union([ diff --git a/src/routers/wa/groups.ts b/src/routers/wa/groups.ts index a93b53f..9aa14d6 100644 --- a/src/routers/wa/groups.ts +++ b/src/routers/wa/groups.ts @@ -1,5 +1,6 @@ import { eq } from "drizzle-orm" import { z } from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { dashboard } from "@/idp/policies" import { createTRPCRouter, policy } from "@/trpc" @@ -45,6 +46,7 @@ export default createTRPCRouter({ }), add: policy(dashboard("wa:groups:manage")) + .meta(audit.full()) .input( z.object({ title: z.string(), @@ -59,6 +61,7 @@ export default createTRPCRouter({ }), modify: policy(dashboard("wa:groups:manage")) + .meta(audit.full()) .input( z.object({ id: z.number(), @@ -81,6 +84,7 @@ export default createTRPCRouter({ }), delete: policy(dashboard("wa:groups:manage")) + .meta(audit.full()) .input( z.object({ id: z.number(), @@ -94,6 +98,7 @@ export default createTRPCRouter({ }), setHide: policy(dashboard("wa:groups:manage")) + .meta(audit.full()) .input( z.object({ id: z.number(), diff --git a/src/routers/web/associations.ts b/src/routers/web/associations.ts index 992ffd8..8726f53 100644 --- a/src/routers/web/associations.ts +++ b/src/routers/web/associations.ts @@ -1,5 +1,6 @@ import { asc, eq } from "drizzle-orm" import z from "zod" +import { audit } from "@/audit/option" import { uploadBlob } from "@/azure/blob" import { DB, SCHEMA } from "@/db" import { authorOf, createdByColumns, modifiedByColumns } from "@/idp/author" @@ -76,6 +77,7 @@ export default createTRPCRouter({ }), addAssociation: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z .instanceof(FormData) @@ -104,6 +106,7 @@ export default createTRPCRouter({ }), editAssociation: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z .instanceof(FormData) @@ -143,6 +146,7 @@ export default createTRPCRouter({ }), editAssociationLinks: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), @@ -181,6 +185,7 @@ export default createTRPCRouter({ }), deleteAssociation: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), diff --git a/src/routers/web/faqs.ts b/src/routers/web/faqs.ts index ca44be9..efc0dd6 100644 --- a/src/routers/web/faqs.ts +++ b/src/routers/web/faqs.ts @@ -1,5 +1,6 @@ import { asc, eq, sql } from "drizzle-orm" import z from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { authorOf, createdByColumns, modifiedByColumns } from "@/idp/author" import { dashboard, publicData } from "@/idp/policies" @@ -55,6 +56,7 @@ export default createTRPCRouter({ }), addFaqs: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ titleIt: z.string(), @@ -83,6 +85,7 @@ export default createTRPCRouter({ }), addFaqsCategory: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ titleIt: z.string(), @@ -107,6 +110,7 @@ export default createTRPCRouter({ }), editFaqs: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), @@ -139,6 +143,7 @@ export default createTRPCRouter({ }), editFaqsCategory: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), @@ -171,6 +176,7 @@ export default createTRPCRouter({ }), deleteFaqs: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), @@ -196,6 +202,7 @@ export default createTRPCRouter({ }), deleteFaqsCategory: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), diff --git a/src/routers/web/freshman.ts b/src/routers/web/freshman.ts index d611e20..b69e5a5 100644 --- a/src/routers/web/freshman.ts +++ b/src/routers/web/freshman.ts @@ -1,5 +1,6 @@ import { TRPCError } from "@trpc/server" import { asc, eq, inArray, sql } from "drizzle-orm" +import { audit } from "@/audit/option" import { DB, SCHEMA } from "@/db" import { authorOf, modifiedByColumns } from "@/idp/author" import { dashboard, publicData } from "@/idp/policies" @@ -58,6 +59,7 @@ export default createTRPCRouter({ }), updateCard: policy(dashboard("web:content:write")) + .meta(audit.full()) .input(updateFreshmanCardInput) .mutation(async ({ input, ctx }) => { const { id, ...fields } = input @@ -70,6 +72,7 @@ export default createTRPCRouter({ }), moveCard: policy(dashboard("web:content:write")) + .meta(audit.full()) .input(moveFreshmanCardInput) .mutation(async ({ input, ctx }) => { const index = freshmanSlots.findIndex((slot) => slot.id === input.id) @@ -103,6 +106,7 @@ export default createTRPCRouter({ }), selectFaq: policy(dashboard("web:content:write")) + .meta(audit.full()) .input(selectFreshmanFaqInput) .mutation(async ({ input, ctx }) => { const author = modifiedByColumns(authorOf(ctx.actor)) diff --git a/src/routers/web/guides_matricole.ts b/src/routers/web/guides_matricole.ts index d909b45..95820f6 100644 --- a/src/routers/web/guides_matricole.ts +++ b/src/routers/web/guides_matricole.ts @@ -1,5 +1,6 @@ import { desc, eq } from "drizzle-orm" import z from "zod" +import { audit } from "@/audit/option" import { deleteBlob, uploadBlob } from "@/azure/blob" import { DB, SCHEMA } from "@/db" import { authorOf, createdByColumns } from "@/idp/author" @@ -42,6 +43,7 @@ export default createTRPCRouter({ }), addGuide: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z .instanceof(FormData) @@ -76,6 +78,7 @@ export default createTRPCRouter({ }), deleteGuide: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), diff --git a/src/routers/web/projects.ts b/src/routers/web/projects.ts index 0c8e9dc..e77db91 100644 --- a/src/routers/web/projects.ts +++ b/src/routers/web/projects.ts @@ -1,5 +1,6 @@ import { asc, eq } from "drizzle-orm" import z from "zod" +import { audit } from "@/audit/option" import { uploadBlob } from "@/azure/blob" import { DB, SCHEMA } from "@/db" import { projectsCategories } from "@/db/schema/web/projects" @@ -43,6 +44,7 @@ export default createTRPCRouter({ }), addProject: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z .instanceof(FormData) @@ -73,6 +75,7 @@ export default createTRPCRouter({ }), editProject: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z .instanceof(FormData) @@ -113,6 +116,7 @@ export default createTRPCRouter({ }), reorderProjects: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ projectIds: z @@ -138,6 +142,7 @@ export default createTRPCRouter({ }), deleteProject: policy(dashboard("web:content:write")) + .meta(audit.full()) .input( z.object({ id: z.number(), diff --git a/src/routers/web/reports.ts b/src/routers/web/reports.ts index 9aef763..1f0e228 100644 --- a/src/routers/web/reports.ts +++ b/src/routers/web/reports.ts @@ -1,5 +1,6 @@ import { and, eq, inArray, sql } from "drizzle-orm" import z from "zod" +import { audit } from "@/audit/option" import { DB, SCHEMA, VIEWS } from "@/db" import { dashboard, SCOPE } from "@/idp/policies" import { createTRPCRouter, policy } from "@/trpc" @@ -29,6 +30,7 @@ function reportsTarget(ids: number[]) { export const reports = createTRPCRouter({ create: policy({ service: { scope: SCOPE.publicRead } }) + .meta(audit.full()) .input( z.discriminatedUnion("reportType", [ z.object({ @@ -84,6 +86,7 @@ export const reports = createTRPCRouter({ }), resolve: policy(dashboard("web:reports:manage")) + .meta(audit.full()) .input(z.object({ ids: z.array(z.number().int()).min(1) })) .mutation(async ({ input, ctx }) => { const before = await DB.select().from(REPORTS).where(inArray(REPORTS.id, input.ids)) @@ -96,6 +99,7 @@ export const reports = createTRPCRouter({ }), dismiss: policy(dashboard("web:reports:manage")) + .meta(audit.full()) .input(z.object({ ids: z.array(z.number().int()).min(1) })) .mutation(async ({ input, ctx }) => { const before = await DB.select().from(REPORTS).where(inArray(REPORTS.id, input.ids)) diff --git a/src/trpc.ts b/src/trpc.ts index 10736ca..2ee5d9d 100644 --- a/src/trpc.ts +++ b/src/trpc.ts @@ -2,7 +2,8 @@ import type { AccessSnapshotClient, Actor } from "@polinetwork/auth-kit" import { initTRPC, TRPCError } from "@trpc/server" import superjson from "superjson" import { ZodError, z } from "zod/v4" -import { auditEvent, auditRecorder, isDenied, noAudit, UNAUDITED } from "./audit" +import { auditEvent, auditRecorder, isDenied, noAudit } from "./audit" +import type { AuditOption } from "./audit/option" import { env } from "./env" import type { RequestAuth } from "./idp/auth" import { logger } from "./logger" @@ -25,7 +26,11 @@ export type PolicyRule = { scope: string | readonly string[]; permission?: strin /** Actor kinds a procedure accepts. A kind that is not listed is denied. */ export type Policy = { service?: PolicyRule; telegram?: PolicyRule; user?: PolicyRule } -type Meta = { policy?: Policy } +type Meta = { + policy?: Policy + /** How a mutation is recorded in the audit log; see `audit` in `src/audit`. */ + audit?: AuditOption +} const t = initTRPC .context() @@ -70,14 +75,17 @@ const timingMiddleware = t.middleware(async ({ next, path }) => { }) /** - * Records every mutation in `common_audit_events` (see `src/audit`): who called it, with what input, and whether it - * succeeded, failed or was denied. It runs before the policy so that denied calls are kept too, without their input. - * Handlers add the target and the state before and after through `ctx.audit`. A failed write is logged and does not - * fail the call, whose change has already happened. + * Records every mutation in `common_audit_events` (see `src/audit`) as its `audit` meta option says: who called it, + * on what, and whether it succeeded, failed or was denied, with the input and the states for `full`. It runs before + * the policy so that denied calls are kept too, without their input. Handlers add the target and the state before and + * after through `ctx.audit`. A failed write is logged and does not fail the call, whose change has already happened. */ -const auditMiddleware = t.middleware(async ({ ctx, path, type, getRawInput, next }) => { +const auditMiddleware = t.middleware(async ({ ctx, meta, path, type, getRawInput, next }) => { + const option = meta?.audit + if (type === "mutation" && !option) logger.error({ procedure: path }, "[AUDIT] mutation without an audit option") // Calls without a valid token are not recorded, so their bodies are never read for the log. - if (type !== "mutation" || UNAUDITED.has(path) || ctx.auth?.kind !== "token") return next({ ctx: { audit: noAudit } }) + if (type !== "mutation" || option?.capture === "none" || ctx.auth?.kind !== "token") + return next({ ctx: { audit: noAudit } }) const { recorder, note, skipped } = auditRecorder() const result = await next({ ctx: { audit: recorder } }) @@ -86,7 +94,7 @@ const auditMiddleware = t.middleware(async ({ ctx, path, type, getRawInput, next try { // A denied call's body may never have been read, and the event keeps none of it. const rawInput = isDenied(result) ? undefined : await getRawInput().catch(() => undefined) - const event = auditEvent({ path, auth: ctx.auth, requestId: ctx.requestId, rawInput, note, result }) + const event = auditEvent({ path, option, auth: ctx.auth, requestId: ctx.requestId, rawInput, note, result }) if (event) { // Imported here so that loading the router does not open the database. const { writeAuditEvent } = await import("./audit/store") diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts index f91a83d..27011e7 100644 --- a/tests/audit-json.test.ts +++ b/tests/audit-json.test.ts @@ -64,6 +64,8 @@ describe("auditEvent", () => { const auth = { kind: "token", actor: admin, scopes: new Set(["backend:admin"]) } as const const ok = (data: unknown = undefined) => ({ ok: true as const, data }) + const full = { capture: "full" } as const + it("records nothing for calls without a valid token", () => { const rejected = { kind: "rejected", status: 401, reason: "Missing bearer token" } as const for (const caller of [undefined, rejected]) @@ -91,6 +93,7 @@ describe("auditEvent", () => { path: "esign.signers.resetIdentity", auth, rawInput: {}, + option: full, note: { target: { type: "esign.signer", id: "s".repeat(5000), label: "l".repeat(5000) }, reason: "r".repeat(5000), @@ -100,17 +103,6 @@ describe("auditEvent", () => { for (const value of [event?.targetId, event?.targetLabel, event?.reason]) expect(value?.length).toBeLessThan(2100) }) - it("names a person only by a valid People id", () => { - const event = auditEvent({ - path: "people.persons.setNote", - auth, - rawInput: { id: "ada@example.org", note: "" }, - note: {}, - result: { ok: false, error: new TRPCError({ code: "BAD_REQUEST", message: "Invalid input" }) }, - }) - expect(event).toMatchObject({ targetType: "people.person", targetId: null, input: null }) - }) - it("lists the issues of an invalid input rather than their JSON", () => { const invalid = z.object({ id: z.number(), title: z.string() }).safeParse({ id: "x" }) const event = auditEvent({ @@ -135,41 +127,84 @@ describe("auditEvent", () => { expect(event).toMatchObject({ targetType: "web.report", targetId: null, targetLabel: "2 reports" }) }) - it("keeps only the actor, the action, the person and the outcome for People", () => { - const note = auditEvent({ + it("records nothing for a mutation declared not to be", () => { + const option = { capture: "none", why: "ingestion" } as const + expect(auditEvent({ path: "tg.messages.add", option, auth, rawInput: {}, note: {}, result: ok() })).toBeNull() + }) + + it("keeps only the actor, the action, the target and the outcome when minimal", () => { + const event = auditEvent({ path: "people.persons.setNote", + option: { + capture: "minimal", + target: ({ input }) => ({ type: "people.person", id: (input as { id: string }).id }), + }, auth, rawInput: { id: "P-0001", note: "private HR note" }, - note: {}, + note: { before: { note: "old" }, after: { note: "new" }, reason: "why" }, result: ok(), }) - expect(note).toMatchObject({ + expect(event).toMatchObject({ action: "people.persons.setNote", actorSub: "usr_admin", targetType: "people.person", targetId: "P-0001", outcome: "success", + reason: null, input: null, before: null, after: null, }) + }) - const created = auditEvent({ - path: "people.persons.create", + it("stores as little as minimal for a mutation without an option", () => { + const event = auditEvent({ + path: "web.faqs.editFaqs", + auth, + rawInput: { id: 1 }, + note: { after: {} }, + result: ok(), + }) + expect(event).toMatchObject({ input: null, after: null }) + }) + + it("keeps only the declared fields of the input", () => { + const event = auditEvent({ + path: "azure.members.setAssocNumber", + option: { capture: "full", fields: ["userId", "assocNumber"] }, auth, - rawInput: { name: "Ada", contactEmail: "ada@example.org", phone: "+39 000" }, + rawInput: { userId: "u1", assocNumber: 7, notifyEmail: "ada@example.org" }, note: {}, - result: ok({ id: "P-0002" }), + result: ok({ error: null }), }) - expect(created).toMatchObject({ targetId: "P-0002", input: null }) + expect(event?.input).toEqual({ userId: "u1", assocNumber: 7 }) + }) - const ended = auditEvent({ - path: "people.admins.end", + it("prefers the target the handler names, and gives the option's the result", () => { + const option = { + capture: "full", + target: ({ data }: { data: unknown }) => ({ + type: "web.faq", + id: (data as { id: number } | undefined)?.id ?? null, + }), + } as const + const named = auditEvent({ + path: "web.faqs.addFaqs", + option, auth, - rawInput: { personId: "P-0003", reason: "stepped down" }, + rawInput: {}, + note: { target: { type: "web.faq", id: 9, label: "Exams" } }, + result: ok({ id: 1 }), + }) + expect(named).toMatchObject({ targetId: "9", targetLabel: "Exams" }) + const derived = auditEvent({ + path: "web.faqs.addFaqs", + option, + auth, + rawInput: {}, note: {}, - result: ok(), + result: ok({ id: 1 }), }) - expect(ended).toMatchObject({ targetId: "P-0003", reason: null, input: null }) + expect(derived).toMatchObject({ targetType: "web.faq", targetId: "1" }) }) }) diff --git a/tests/azure-router.test.ts b/tests/azure-router.test.ts index 384a97a..2062d10 100644 --- a/tests/azure-router.test.ts +++ b/tests/azure-router.test.ts @@ -133,7 +133,7 @@ describe("Azure tRPC routes without credentials", () => { targetType: "azure.member", targetId: result.id, targetLabel: "Katherine Johnson", - after: { id: result.id, email: "katherine.johnson@polinetwork.org", assocNumber: 2043 }, + after: { id: result.id, assocNumber: 2043 }, }) }) diff --git a/tests/group-label-platform-isolation.test.ts b/tests/group-label-platform-isolation.test.ts index 21654fd..730b7c0 100644 --- a/tests/group-label-platform-isolation.test.ts +++ b/tests/group-label-platform-isolation.test.ts @@ -64,6 +64,7 @@ vi.mock("@/db", () => ({ vi.mock("@/trpc", () => { const publicProcedure = { + meta: () => publicProcedure, input: (schema: { parse: (input: unknown) => unknown }) => ({ mutation: (handler: (args: { input: unknown; ctx: unknown }) => unknown) => ({ execute: (input: unknown) => diff --git a/tests/guides-latest.test.ts b/tests/guides-latest.test.ts index 13a4932..634e4f6 100644 --- a/tests/guides-latest.test.ts +++ b/tests/guides-latest.test.ts @@ -16,6 +16,7 @@ vi.mock("@/azure/blob", () => ({ deleteBlob: vi.fn(), uploadBlob: vi.fn() })) vi.mock("@/db", () => ({ DB: { select: state.select }, SCHEMA: { WEB: { guidesMatricole: state.guides } } })) vi.mock("@/trpc", () => { const procedure = { + meta: () => procedure, input: () => ({ mutation: (handler: unknown) => ({ handler }) }), output: () => ({ query: (handler: () => unknown) => ({ handler }) }), } diff --git a/tests/idp-router.test.ts b/tests/idp-router.test.ts index 3399dab..d7cb922 100644 --- a/tests/idp-router.test.ts +++ b/tests/idp-router.test.ts @@ -84,7 +84,9 @@ beforeAll(async () => { ;({ appRouter } = await import("@/routers")) }) -type ProcedureDef = { _def: { meta?: { policy?: unknown } } } +type ProcedureDef = { + _def: { type: string; meta?: { policy?: unknown; audit?: { capture: string; why?: string } } } +} function procedures(): Array<[string, ProcedureDef]> { return Object.entries( @@ -93,6 +95,36 @@ function procedures(): Array<[string, ProcedureDef]> { } describe("app router", () => { + it("records People changes without their details, naming a person only by a valid People id", () => { + const people = procedures().filter( + ([path, procedure]) => path.startsWith("people.") && procedure._def.type === "mutation" + ) + expect(people.length).toBeGreaterThan(10) + for (const [, procedure] of people) expect(procedure._def.meta?.audit?.capture).toBe("minimal") + const target = ( + procedures().find(([path]) => path === "people.persons.setNote")?.[1]._def.meta?.audit as unknown as { + target: (call: { input: unknown; data: unknown }) => unknown + } + ).target + expect(target({ input: { id: "P-0001", note: "" }, data: undefined })).toEqual({ + type: "people.person", + id: "P-0001", + }) + expect(target({ input: { personId: "P-0002" }, data: undefined })).toEqual({ type: "people.person", id: "P-0002" }) + expect(target({ input: { name: "Ada" }, data: { id: "P-0003" } })).toEqual({ type: "people.person", id: "P-0003" }) + expect(target({ input: { id: "ada@example.org" }, data: undefined })).toEqual({ type: "people.person", id: null }) + }) + + it("gives every mutation an audit option, and says why one is not recorded", () => { + const mutations = procedures().filter(([, procedure]) => procedure._def.type === "mutation") + expect(mutations.length).toBeGreaterThan(60) + expect(mutations.filter(([, procedure]) => !procedure._def.meta?.audit).map(([path]) => path)).toEqual([]) + const unexplained = mutations.filter( + ([, procedure]) => procedure._def.meta?.audit?.capture === "none" && !procedure._def.meta.audit.why?.trim() + ) + expect(unexplained.map(([path]) => path)).toEqual([]) + }) + it("gives every procedure a policy", () => { const missing = procedures() .filter(([, procedure]) => !procedure._def.meta?.policy) diff --git a/tests/tg-users-creation-history.test.ts b/tests/tg-users-creation-history.test.ts index a09db02..1b5628d 100644 --- a/tests/tg-users-creation-history.test.ts +++ b/tests/tg-users-creation-history.test.ts @@ -32,6 +32,7 @@ vi.mock("@/idp/policies", () => ({ })) vi.mock("@/trpc", () => { const procedure = { + meta: () => procedure, input: () => procedure, output: () => procedure, query: (handler: unknown) => ({ handler }), From 09767875462d8313460a91ea440fe4a47efd1980 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Wed, 11 Nov 2026 23:24:27 +0000 Subject: [PATCH 06/12] fix(audit): resolve member targets and capture project reorder values only when rows match --- src/routers/azure/members.ts | 5 +- src/routers/web/projects.ts | 18 ++++--- tests/azure-router.test.ts | 24 +++++++++ tests/projects-reorder.test.ts | 95 ++++++++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 7 deletions(-) create mode 100644 tests/projects-reorder.test.ts diff --git a/src/routers/azure/members.ts b/src/routers/azure/members.ts index c12874a..ad8c4f4 100644 --- a/src/routers/azure/members.ts +++ b/src/routers/azure/members.ts @@ -57,7 +57,10 @@ export default createTRPCRouter({ const changed = await serializeMemberWrite(async () => { const member = await currentMember(input.userId) if (!member) return { error: "This member no longer exists in the directory." } - ctx.audit.set({ before: { assocNumber: member.employeeId } }) + ctx.audit.set({ + target: { type: "azure.member", id: member.id }, + before: { assocNumber: member.employeeId }, + }) if (member.employeeId === input.assocNumber.toString()) return { error: `The member already has number ${input.assocNumber}.` } diff --git a/src/routers/web/projects.ts b/src/routers/web/projects.ts index e77db91..4811038 100644 --- a/src/routers/web/projects.ts +++ b/src/routers/web/projects.ts @@ -127,17 +127,23 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { // Both orders are read in the reorder's transaction, so they are the ones it changed. - const { before, after } = await DB.transaction(async (tx) => { + const { before, after, updatedCount } = await DB.transaction(async (tx) => { const order = () => - tx.select({ id: PROJECTS.id, title: PROJECTS.title }).from(PROJECTS).orderBy(asc(PROJECTS.order)) + tx + .select({ id: PROJECTS.id, title: PROJECTS.title, order: PROJECTS.order }) + .from(PROJECTS) + .orderBy(asc(PROJECTS.order)) const before = await order() - await Promise.all( - input.projectIds.map((id, index) => tx.update(PROJECTS).set({ order: index }).where(eq(PROJECTS.id, id))) + const updated = await Promise.all( + input.projectIds.map((id, index) => + tx.update(PROJECTS).set({ order: index }).where(eq(PROJECTS.id, id)).returning({ id: PROJECTS.id }) + ) ) - return { before, after: await order() } + return { before, after: await order(), updatedCount: updated.reduce((total, rows) => total + rows.length, 0) } }) - ctx.audit.set({ target: { type: "web.projects", id: "order", label: "Projects order" }, before, after }) + if (updatedCount === 0) ctx.audit.skip() + else ctx.audit.set({ target: { type: "web.projects", id: "order", label: "Projects order" }, before, after }) return { error: null } }), diff --git a/tests/azure-router.test.ts b/tests/azure-router.test.ts index 2062d10..4334715 100644 --- a/tests/azure-router.test.ts +++ b/tests/azure-router.test.ts @@ -387,6 +387,30 @@ describe("Azure member number changes", () => { expect(write).not.toHaveBeenCalled() }) + it("audits the resolved directory id when the lookup uses an alias", async () => { + const caller = await writer() + const { azureDirectory } = await import("@/azure/directory") + const member = await azureDirectory.getMember("mock-member-alan") + vi.spyOn(azureDirectory, "getMember").mockResolvedValueOnce(member) + + await expect(caller.members.setAssocNumber({ userId: "alan-alias", assocNumber: 2042 })).resolves.toEqual({ + error: null, + notificationSent: null, + }) + expect(audited.at(-1)).toMatchObject({ targetType: "azure.member", targetId: "mock-member-alan" }) + }) + + it("keeps the input audit target when the member lookup fails", async () => { + const caller = await writer() + const { azureDirectory } = await import("@/azure/directory") + vi.spyOn(azureDirectory, "getMember").mockResolvedValueOnce(null) + + await expect(caller.members.setAssocNumber({ userId: "missing-alias", assocNumber: 2042 })).resolves.toMatchObject({ + error: "This member no longer exists in the directory.", + }) + expect(audited.at(-1)).toMatchObject({ targetType: "azure.member", targetId: "missing-alias" }) + }) + it("changes the number and emails the member", async () => { const caller = await writer() diff --git a/tests/projects-reorder.test.ts b/tests/projects-reorder.test.ts new file mode 100644 index 0000000..e8187d8 --- /dev/null +++ b/tests/projects-reorder.test.ts @@ -0,0 +1,95 @@ +import { beforeEach, expect, it, vi } from "vitest" +import { auditRecorder } from "@/audit" + +const state = vi.hoisted(() => ({ + projects: { id: Symbol("id"), title: Symbol("title"), order: Symbol("order") }, + rows: [] as { id: number; title: string; order: number }[], +})) +vi.mock("drizzle-orm", async (importOriginal) => ({ + ...(await importOriginal()), + asc: (column: unknown) => column, + eq: (_column: unknown, id: number) => id, +})) +vi.mock("@/azure/blob", () => ({ uploadBlob: vi.fn() })) +vi.mock("@/utils/web", () => ({ getImageExtension: vi.fn() })) +vi.mock("@/db", () => { + const tx = { + select: (fields: Record) => ({ + from: () => ({ + orderBy: async () => + [...state.rows] + .sort((a, b) => a.order - b.order) + .map((row) => Object.fromEntries(Object.keys(fields).map((key) => [key, row[key as keyof typeof row]]))), + }), + }), + update: () => ({ + set: ({ order }: { order: number }) => ({ + where: (id: number) => ({ + returning: async () => { + const row = state.rows.find((row) => row.id === id) + if (!row) return [] + row.order = order + return [{ id }] + }, + }), + }), + }), + } + return { + DB: { transaction: (run: (tx: unknown) => unknown) => run(tx) }, + SCHEMA: { WEB: { projects: state.projects } }, + } +}) +vi.mock("@/trpc", () => { + const procedure = { + meta: () => procedure, + input: () => procedure, + output: () => procedure, + mutation: (handler: unknown) => ({ handler }), + query: (handler: unknown) => ({ handler }), + } + return { createTRPCRouter: (router: T) => router, policy: () => procedure } +}) +const { default: router } = await import("@/routers/web/projects") +const reorder = router.reorderProjects as unknown as { + handler: (args: { + input: { projectIds: number[] } + ctx: { audit: ReturnType["recorder"] } + }) => Promise +} +beforeEach(() => { + state.rows = [ + { id: 1, title: "One", order: 5 }, + { id: 2, title: "Two", order: 5 }, + ] +}) +it("records order values even when tied projects retain their snapshot positions", async () => { + const audit = auditRecorder() + await expect(reorder.handler({ input: { projectIds: [1, 99] }, ctx: { audit: audit.recorder } })).resolves.toEqual({ + error: null, + }) + expect(audit.skipped()).toBe(false) + expect(audit.note).toMatchObject({ + target: { type: "web.projects", id: "order" }, + before: [ + { id: 1, title: "One", order: 5 }, + { id: 2, title: "Two", order: 5 }, + ], + after: [ + { id: 1, title: "One", order: 0 }, + { id: 2, title: "Two", order: 5 }, + ], + }) +}) +it("skips the audit when all requested projects are missing", async () => { + const audit = auditRecorder() + await reorder.handler({ input: { projectIds: [98, 99] }, ctx: { audit: audit.recorder } }) + expect(audit.skipped()).toBe(true) +}) +it("retains the audit when a matching row already has the requested order", async () => { + state.rows[0].order = 0 + const audit = auditRecorder() + await reorder.handler({ input: { projectIds: [1] }, ctx: { audit: audit.recorder } }) + expect(audit.skipped()).toBe(false) + expect(audit.note.before).toEqual(audit.note.after) +}) From b2569ac60ca21f0520e5fce5c7f49f1b4e2a6023 Mon Sep 17 00:00:00 2001 From: "coderabbitai[bot]" <136622811+coderabbitai[bot]@users.noreply.github.com> Date: Sun, 15 Nov 2026 22:17:40 +0000 Subject: [PATCH 07/12] fix(audit): Validate mutation targets, enforce input allowlists, and capture consistent before/after snapshots --- src/audit/index.ts | 13 ++-- src/people/router.ts | 117 +++++++++++++++++++------------- src/people/service.ts | 1 + src/routers/esign.ts | 2 +- src/routers/web/associations.ts | 68 +++++++++++-------- src/routers/web/faqs.ts | 56 +++++++++------ src/routers/web/freshman.ts | 45 ++++++++---- src/routers/web/projects.ts | 32 +++++---- src/routers/web/reports.ts | 50 +++++++++++--- tests/audit-json.test.ts | 26 +++++++ tests/freshman.test.ts | 73 ++++++++++++++++++++ tests/people-router.test.ts | 27 +++++++- 12 files changed, 366 insertions(+), 144 deletions(-) diff --git a/src/audit/index.ts b/src/audit/index.ts index 4b8bc75..51f9eeb 100644 --- a/src/audit/index.ts +++ b/src/audit/index.ts @@ -159,10 +159,13 @@ function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | nu return error === null ? { outcome: AUDIT_OUTCOME.SUCCESS, error: null } : { outcome: AUDIT_OUTCOME.FAILURE, error } } -/** Only `fields` of a copied input, when it is an object. */ -function onlyFields(copy: AuditJson, fields: readonly string[] | undefined): AuditJson { - if (!fields || copy === null || typeof copy !== "object" || Array.isArray(copy)) return copy - return Object.fromEntries(fields.flatMap((field) => (field in copy ? [[field, copy[field] ?? null]] : []))) +/** Select declared input fields before redaction and size limits are applied. */ +function onlyFields(input: unknown, fields: readonly string[] | undefined): unknown { + if (!fields) return input + if (input instanceof FormData || input instanceof Map) input = Object.fromEntries(input.entries()) + if (input === null || typeof input !== "object" || Array.isArray(input)) return {} + const record = input as Record + return Object.fromEntries(fields.flatMap((field) => (Object.hasOwn(record, field) ? [[field, record[field]]] : []))) } /** @@ -212,7 +215,7 @@ export function auditEvent(options: { ...event, reason: cut(note.reason ?? null), // A refused call changed nothing, and its input is whatever the caller chose to send. - input: outcome === AUDIT_OUTCOME.DENIED ? null : onlyFields(auditJson(options.rawInput), option.fields), + input: outcome === AUDIT_OUTCOME.DENIED ? null : auditJson(onlyFields(options.rawInput, option.fields)), before: note.before === undefined ? null : auditJson(note.before), after: note.after === undefined ? null : auditJson(note.after), } diff --git a/src/people/router.ts b/src/people/router.ts index 31864df..b096c64 100644 --- a/src/people/router.ts +++ b/src/people/router.ts @@ -8,19 +8,10 @@ import { requireGeneral } from "./model" import type { PeopleService } from "./service" import type { Viewer } from "./types" -/** - * The person a People call is about: the input's `personId` or `id`, else the created record's. The raw input is not - * validated yet, so only a valid People id is taken from it. - */ -const personTarget: AuditTargetOf = ({ input, data }) => { - for (const value of [input, data]) { - if (value === null || typeof value !== "object") continue - for (const key of ["personId", "id"]) { - const id = C.personIdSchema.safeParse((value as Record)[key]) - if (id.success) return { type: "people.person", id: id.data } - } - } - return { type: "people.person", id: null } +/** Handler targets use validated fields; creation can resolve its id from the result. */ +const personTarget: AuditTargetOf = ({ data }) => { + const id = C.personIdSchema.safeParse(data && typeof data === "object" ? (data as { id?: unknown }).id : undefined) + return { type: "people.person", id: id.success ? id.data : null } } /** @@ -73,28 +64,44 @@ export function createPeopleRouter(people: PeopleService) { .input(C.createPersonInputSchema) .output(C.createdSchema) .mutation(({ ctx, input }) => people.createPerson(ctx.viewer, input)), - update: write - .input(C.updatePersonInputSchema) - .mutation(({ ctx, input }) => people.updatePerson(ctx.viewer, input)), - setNote: write.input(C.setNoteInputSchema).mutation(({ ctx, input }) => people.setNote(ctx.viewer, input)), + update: write.input(C.updatePersonInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.id } }) + return people.updatePerson(ctx.viewer, input) + }), + setNote: write.input(C.setNoteInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.id } }) + return people.setNote(ctx.viewer, input) + }), }), admins: createTRPCRouter({ list: read.output(C.adminSummarySchema.array()).query(({ ctx }) => people.listAdmins(ctx.viewer)), - assign: write.input(C.assignAdminInputSchema).mutation(({ ctx, input }) => people.assignAdmin(ctx.viewer, input)), - end: write.input(C.endAssignmentInputSchema).mutation(({ ctx, input }) => people.endAdmin(ctx.viewer, input)), - setCapoOfReference: write - .input(C.capoRefInputSchema) - .mutation(({ ctx, input }) => people.setCapoOfReference(ctx.viewer, input)), + assign: write.input(C.assignAdminInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.assignAdmin(ctx.viewer, input) + }), + end: write.input(C.endAssignmentInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.endAdmin(ctx.viewer, input) + }), + setCapoOfReference: write.input(C.capoRefInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.setCapoOfReference(ctx.viewer, input) + }), }), capos: createTRPCRouter({ list: read.output(C.capoSummarySchema.array()).query(({ ctx }) => people.listCapos(ctx.viewer)), - appoint: appoint - .input(C.appointCapoInputSchema) - .mutation(({ ctx, input }) => people.appointCapo(ctx.viewer, input)), - setScope: appoint - .input(C.capoScopeInputSchema) - .mutation(({ ctx, input }) => people.setCapoScope(ctx.viewer, input)), - end: appoint.input(C.endAssignmentInputSchema).mutation(({ ctx, input }) => people.endCapo(ctx.viewer, input)), + appoint: appoint.input(C.appointCapoInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.appointCapo(ctx.viewer, input) + }), + setScope: appoint.input(C.capoScopeInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.setCapoScope(ctx.viewer, input) + }), + end: appoint.input(C.endAssignmentInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.endCapo(ctx.viewer, input) + }), }), teams: createTRPCRouter({ list: user.output(C.teamSummarySchema.array()).query(({ ctx }) => people.listTeams(ctx.viewer)), @@ -102,32 +109,43 @@ export function createPeopleRouter(people: PeopleService) { .input(C.teamCandidatesInputSchema) .output(C.teamCandidateSchema.array()) .query(({ ctx, input }) => people.listTeamCandidates(ctx.viewer, input)), - addMember: user - .input(C.addTeamMemberInputSchema) - .mutation(({ ctx, input }) => people.addTeamMember(ctx.viewer, input)), - updateFields: user - .input(C.teamFieldsInputSchema) - .mutation(({ ctx, input }) => people.updateTeamFields(ctx.viewer, input)), - endMembership: user - .input(C.endTeamInputSchema) - .mutation(({ ctx, input }) => people.endTeamMembership(ctx.viewer, input)), + addMember: user.input(C.addTeamMemberInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.addTeamMember(ctx.viewer, input) + }), + updateFields: user.input(C.teamFieldsInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.updateTeamFields(ctx.viewer, input) + }), + endMembership: user.input(C.endTeamInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.endTeamMembership(ctx.viewer, input) + }), }), linking: createTRPCRouter({ invites: read.output(C.inviteSchema.array()).query(({ ctx }) => people.listInvites(ctx.viewer)), recent: read.output(C.recentLinkSchema.array()).query(({ ctx }) => people.listRecentLinks(ctx.viewer)), // People writers, or the Lead of one of the person's teams: the service decides. - sendInvite: user.input(C.personRefInputSchema).mutation(({ ctx, input }) => people.sendInvite(ctx.viewer, input)), - revokeInvite: user - .input(C.personRefInputSchema) - .mutation(({ ctx, input }) => people.revokeInvite(ctx.viewer, input)), + sendInvite: user.input(C.personRefInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.sendInvite(ctx.viewer, input) + }), + revokeInvite: user.input(C.personRefInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.revokeInvite(ctx.viewer, input) + }), searchAccounts: link .input(C.searchAccountsInputSchema) .output(C.accountMatchSchema.array()) .query(({ ctx, input }) => people.searchAccounts(ctx.viewer, input)), - link: link.input(C.linkAccountInputSchema).mutation(({ ctx, input }) => people.linkAccount(ctx.viewer, input)), - unlink: link - .input(C.unlinkAccountInputSchema) - .mutation(({ ctx, input }) => people.unlinkAccount(ctx.viewer, input)), + link: link.input(C.linkAccountInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.linkAccount(ctx.viewer, input) + }), + unlink: link.input(C.unlinkAccountInputSchema).mutation(({ ctx, input }) => { + ctx.audit.set({ target: { type: "people.person", id: input.personId } }) + return people.unlinkAccount(ctx.viewer, input) + }), }), capo: createTRPCRouter({ get: self.output(C.capoAreaSchema).query(({ ctx }) => people.getCapoArea(ctx.viewer)), @@ -140,9 +158,10 @@ export function createPeopleRouter(people: PeopleService) { .input(C.inviteTokenInputSchema) .output(C.invitePreviewSchema) .query(({ ctx, input }) => people.previewInvite(ctx.viewer, input)), - acceptInvite: user - .input(C.inviteTokenInputSchema) - .mutation(({ ctx, input }) => people.acceptInvite(ctx.viewer, input)), + acceptInvite: user.input(C.inviteTokenInputSchema).mutation(async ({ ctx, input }) => { + const personId = await people.acceptInvite(ctx.viewer, input) + ctx.audit.set({ target: { type: "people.person", id: personId } }) + }), }), }) } diff --git a/src/people/service.ts b/src/people/service.ts index ae6a2d9..cf3969f 100644 --- a/src/people/service.ts +++ b/src/people/service.ts @@ -877,6 +877,7 @@ export function createPeopleService(options: PeopleOptions) { const p = link(u, find(u.state, invite.personId), v.sub, v.sub, "invite", "Accepted the email invite") Object.assign(invite, { revokedAt: null, acceptedAt: now().toISOString(), acceptedBy: v.sub }) sendPending(u, ctx, p, (a) => a.assignedBy, "Linked by accepting an invite in People") + return p.id }) }, diff --git a/src/routers/esign.ts b/src/routers/esign.ts index 0fb0276..fa6cb05 100644 --- a/src/routers/esign.ts +++ b/src/routers/esign.ts @@ -190,7 +190,7 @@ export const esignRouter = createTRPCRouter({ requests: createTRPCRouter({ create: user - .meta(audit.full({ fields: ["title", "documentId", "ordered", "signers", "output"] })) + .meta(audit.full({ fields: ["title", "documentId", "ordered", "output"] })) .input( z.object({ title: z.string().trim().min(1).max(200), diff --git a/src/routers/web/associations.ts b/src/routers/web/associations.ts index 8726f53..2915743 100644 --- a/src/routers/web/associations.ts +++ b/src/routers/web/associations.ts @@ -124,17 +124,23 @@ export default createTRPCRouter({ ? await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type) : undefined - const [before] = await DB.select().from(ASSOCIATIONS).where(eq(ASSOCIATIONS.id, id)).limit(1) - const [res] = await DB.update(ASSOCIATIONS) - .set({ - name, - descriptionIt, - descriptionEn, - ...(uploadedLogo ? { logo: uploadedLogo.url } : {}), - ...modifiedByColumns(authorOf(ctx.actor)), - }) - .where(eq(ASSOCIATIONS.id, id)) - .returning() + const { before, res } = await DB.transaction(async (tx) => { + const [before] = await tx.select().from(ASSOCIATIONS).where(eq(ASSOCIATIONS.id, id)).limit(1).for("update") + if (!before) return { before, res: undefined } + const [res] = await tx + .update(ASSOCIATIONS) + .set({ + name, + descriptionIt, + descriptionEn, + ...(uploadedLogo ? { logo: uploadedLogo.url } : {}), + ...modifiedByColumns(authorOf(ctx.actor)), + }) + .where(eq(ASSOCIATIONS.id, id)) + .returning() + + return { before, res } + }) ctx.audit.set({ target: { type: "web.association", id, label: res?.name ?? before?.name }, @@ -157,23 +163,29 @@ export default createTRPCRouter({ const { id, links } = input const { email, website, facebook, instagram, tiktok, x, youtube, telegram, linkedin, spotify } = links - const [before] = await DB.select().from(ASSOCIATIONS).where(eq(ASSOCIATIONS.id, id)).limit(1) - const [res] = await DB.update(ASSOCIATIONS) - .set({ - email, - website, - facebook, - instagram, - tiktok, - x, - youtube, - telegram, - linkedin, - spotify, - ...modifiedByColumns(authorOf(ctx.actor)), - }) - .where(eq(ASSOCIATIONS.id, id)) - .returning() + const { before, res } = await DB.transaction(async (tx) => { + const [before] = await tx.select().from(ASSOCIATIONS).where(eq(ASSOCIATIONS.id, id)).limit(1).for("update") + if (!before) return { before, res: undefined } + const [res] = await tx + .update(ASSOCIATIONS) + .set({ + email, + website, + facebook, + instagram, + tiktok, + x, + youtube, + telegram, + linkedin, + spotify, + ...modifiedByColumns(authorOf(ctx.actor)), + }) + .where(eq(ASSOCIATIONS.id, id)) + .returning() + + return { before, res } + }) ctx.audit.set({ target: { type: "web.association", id, label: res?.name ?? before?.name }, diff --git a/src/routers/web/faqs.ts b/src/routers/web/faqs.ts index efc0dd6..b35df21 100644 --- a/src/routers/web/faqs.ts +++ b/src/routers/web/faqs.ts @@ -124,18 +124,24 @@ export default createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { id, titleIt, titleEn, descriptionIt, descriptionEn, categoryId } = input - const [before] = await DB.select().from(FAQS).where(eq(FAQS.id, id)).limit(1) - const [res] = await DB.update(FAQS) - .set({ - titleIt, - titleEn, - descriptionIt, - descriptionEn, - categoryId, - ...modifiedByColumns(authorOf(ctx.actor)), - }) - .where(eq(FAQS.id, id)) - .returning() + const { before, res } = await DB.transaction(async (tx) => { + const [before] = await tx.select().from(FAQS).where(eq(FAQS.id, id)).limit(1).for("update") + if (!before) return { before, res: undefined } + const [res] = await tx + .update(FAQS) + .set({ + titleIt, + titleEn, + descriptionIt, + descriptionEn, + categoryId, + ...modifiedByColumns(authorOf(ctx.actor)), + }) + .where(eq(FAQS.id, id)) + .returning() + + return { before, res } + }) ctx.audit.set({ target: { type: "web.faq", id, label: res?.titleIt ?? before?.titleIt }, before, after: res }) if (!res) return { error: "NOT_FOUND" } @@ -155,16 +161,22 @@ export default createTRPCRouter({ .mutation(async ({ input, ctx }) => { const { id, titleIt, titleEn, icon } = input - const [before] = await DB.select().from(FAQ_CATEGORIES).where(eq(FAQ_CATEGORIES.id, id)).limit(1) - const [res] = await DB.update(FAQ_CATEGORIES) - .set({ - titleIt, - titleEn, - icon, - ...modifiedByColumns(authorOf(ctx.actor)), - }) - .where(eq(FAQ_CATEGORIES.id, id)) - .returning() + const { before, res } = await DB.transaction(async (tx) => { + const [before] = await tx.select().from(FAQ_CATEGORIES).where(eq(FAQ_CATEGORIES.id, id)).limit(1).for("update") + if (!before) return { before, res: undefined } + const [res] = await tx + .update(FAQ_CATEGORIES) + .set({ + titleIt, + titleEn, + icon, + ...modifiedByColumns(authorOf(ctx.actor)), + }) + .where(eq(FAQ_CATEGORIES.id, id)) + .returning() + + return { before, res } + }) ctx.audit.set({ target: { type: "web.faqCategory", id, label: res?.titleIt ?? before?.titleIt }, diff --git a/src/routers/web/freshman.ts b/src/routers/web/freshman.ts index b69e5a5..a9a1a8e 100644 --- a/src/routers/web/freshman.ts +++ b/src/routers/web/freshman.ts @@ -63,11 +63,17 @@ export default createTRPCRouter({ .input(updateFreshmanCardInput) .mutation(async ({ input, ctx }) => { const { id, ...fields } = input - const [updated] = await DB.update(CARDS) - .set({ ...fields, ...modifiedByColumns(authorOf(ctx.actor)) }) - .where(eq(CARDS.id, id)) - .returning({ id: CARDS.id }) - if (!updated) throw new TRPCError({ code: "NOT_FOUND", message: "The website slot is not initialized." }) + const { before, after } = await DB.transaction(async (tx) => { + const [before] = await tx.select().from(CARDS).where(eq(CARDS.id, id)).for("update") + if (!before) throw new TRPCError({ code: "NOT_FOUND", message: "The website slot is not initialized." }) + const [after] = await tx + .update(CARDS) + .set({ ...fields, ...modifiedByColumns(authorOf(ctx.actor)) }) + .where(eq(CARDS.id, id)) + .returning() + return { before, after } + }) + ctx.audit.set({ target: { type: "web.freshmanCard", id }, before, after }) return { error: null } }), @@ -81,7 +87,7 @@ export default createTRPCRouter({ if (!next || next.group !== slot.group) throw new TRPCError({ code: "BAD_REQUEST", message: "Cards can only move within their website section." }) const author = modifiedByColumns(authorOf(ctx.actor)) - await DB.transaction(async (tx) => { + const { before, after } = await DB.transaction(async (tx) => { const rows = await tx .select() .from(CARDS) @@ -99,9 +105,11 @@ export default createTRPCRouter({ icon: row.icon, ...author, }) - await tx.update(CARDS).set(values(target)).where(eq(CARDS.id, current.id)) - await tx.update(CARDS).set(values(current)).where(eq(CARDS.id, target.id)) + const first = await tx.update(CARDS).set(values(target)).where(eq(CARDS.id, current.id)).returning() + const second = await tx.update(CARDS).set(values(current)).where(eq(CARDS.id, target.id)).returning() + return { before: rows, after: [...first, ...second] } }) + ctx.audit.set({ target: { type: "web.freshmanCard", id: input.id }, before, after }) return { error: null } }), @@ -110,7 +118,7 @@ export default createTRPCRouter({ .input(selectFreshmanFaqInput) .mutation(async ({ input, ctx }) => { const author = modifiedByColumns(authorOf(ctx.actor)) - await DB.transaction(async (tx) => { + const { before, after } = await DB.transaction(async (tx) => { // Serialize writes to the five slots before reading; public snapshot reads remain unblocked. // Catalog deletions take the same lock before checking whether a FAQ is selected. await tx.execute(sql`LOCK TABLE ${FEATURED} IN SHARE ROW EXCLUSIVE MODE`) @@ -121,24 +129,31 @@ export default createTRPCRouter({ if (!faq) throw new TRPCError({ code: "NOT_FOUND", message: "This FAQ no longer exists." }) const current = rows.find((row) => row.position === input.position) if (!current) throw new TRPCError({ code: "NOT_FOUND" }) - if (current.faqId === input.faqId) return + if (current.faqId === input.faqId) return { before: [current], after: [current] } const existing = rows.find((row) => row.faqId === input.faqId) if (existing) { // Reinsert the pair atomically so the declared immediate unique constraint also permits swaps. // Preserve each slot's creation metadata and record this edit on both positions. await tx.delete(FEATURED).where(inArray(FEATURED.position, [current.position, existing.position])) const updatedAt = new Date() - await tx.insert(FEATURED).values([ - { ...current, faqId: existing.faqId, ...author, updatedAt }, - { ...existing, faqId: current.faqId, ...author, updatedAt }, - ]) + const after = await tx + .insert(FEATURED) + .values([ + { ...current, faqId: existing.faqId, ...author, updatedAt }, + { ...existing, faqId: current.faqId, ...author, updatedAt }, + ]) + .returning() + return { before: [current, existing], after } } else { - await tx + const after = await tx .update(FEATURED) .set({ faqId: input.faqId, ...author }) .where(eq(FEATURED.position, input.position)) + .returning() + return { before: [current], after } } }) + ctx.audit.set({ target: { type: "web.freshmanFaq", id: input.position }, before, after }) return { error: null } }), }) diff --git a/src/routers/web/projects.ts b/src/routers/web/projects.ts index 4811038..c0293af 100644 --- a/src/routers/web/projects.ts +++ b/src/routers/web/projects.ts @@ -92,19 +92,25 @@ export default createTRPCRouter({ ? await uploadBlob(Buffer.from(await logo.arrayBuffer()), getImageExtension(logo), logo.type) : undefined - const [before] = await DB.select().from(PROJECTS).where(eq(PROJECTS.id, input.id)).limit(1) - const [res] = await DB.update(PROJECTS) - .set({ - title, - descriptionIt, - descriptionEn, - ...(uploadedLogo ? { logo: uploadedLogo.url } : {}), - link, - category, - ...modifiedByColumns(authorOf(ctx.actor)), - }) - .where(eq(PROJECTS.id, input.id)) - .returning() + const { before, res } = await DB.transaction(async (tx) => { + const [before] = await tx.select().from(PROJECTS).where(eq(PROJECTS.id, input.id)).limit(1).for("update") + if (!before) return { before, res: undefined } + const [res] = await tx + .update(PROJECTS) + .set({ + title, + descriptionIt, + descriptionEn, + ...(uploadedLogo ? { logo: uploadedLogo.url } : {}), + link, + category, + ...modifiedByColumns(authorOf(ctx.actor)), + }) + .where(eq(PROJECTS.id, input.id)) + .returning() + + return { before, res } + }) ctx.audit.set({ target: { type: "web.project", id: input.id, label: res?.title ?? before?.title }, diff --git a/src/routers/web/reports.ts b/src/routers/web/reports.ts index 1f0e228..d74b2c4 100644 --- a/src/routers/web/reports.ts +++ b/src/routers/web/reports.ts @@ -89,11 +89,26 @@ export const reports = createTRPCRouter({ .meta(audit.full()) .input(z.object({ ids: z.array(z.number().int()).min(1) })) .mutation(async ({ input, ctx }) => { - const before = await DB.select().from(REPORTS).where(inArray(REPORTS.id, input.ids)) - const after = await DB.update(REPORTS) - .set({ status: "resolved" }) - .where(inArray(REPORTS.id, input.ids)) - .returning() + const { before, after } = await DB.transaction(async (tx) => { + const before = await tx + .select() + .from(REPORTS) + .where(inArray(REPORTS.id, input.ids)) + .orderBy(REPORTS.id) + .for("update") + const after = await tx + .update(REPORTS) + .set({ status: "resolved" }) + .where( + inArray( + REPORTS.id, + before.map((row) => row.id) + ) + ) + .returning() + return { before, after } + }) + ctx.audit.set({ target: reportsTarget(input.ids), before, after }) return after }), @@ -102,11 +117,26 @@ export const reports = createTRPCRouter({ .meta(audit.full()) .input(z.object({ ids: z.array(z.number().int()).min(1) })) .mutation(async ({ input, ctx }) => { - const before = await DB.select().from(REPORTS).where(inArray(REPORTS.id, input.ids)) - const after = await DB.update(REPORTS) - .set({ status: "dismissed" }) - .where(inArray(REPORTS.id, input.ids)) - .returning() + const { before, after } = await DB.transaction(async (tx) => { + const before = await tx + .select() + .from(REPORTS) + .where(inArray(REPORTS.id, input.ids)) + .orderBy(REPORTS.id) + .for("update") + const after = await tx + .update(REPORTS) + .set({ status: "dismissed" }) + .where( + inArray( + REPORTS.id, + before.map((row) => row.id) + ) + ) + .returning() + return { before, after } + }) + ctx.audit.set({ target: reportsTarget(input.ids), before, after }) return after }), diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts index 27011e7..c6bc502 100644 --- a/tests/audit-json.test.ts +++ b/tests/audit-json.test.ts @@ -180,6 +180,32 @@ describe("auditEvent", () => { expect(event?.input).toEqual({ userId: "u1", assocNumber: 7 }) }) + it("selects fields before object and total size limits, while still redacting", () => { + const padding = Object.fromEntries(Array.from({ length: 105 }, (_, i) => [`excluded${i}`, "x".repeat(2000)])) + const event = auditEvent({ + path: "test", + auth, + option: { capture: "full", fields: ["id", "token"] }, + rawInput: { ...padding, id: 7, token: "secret" }, + note: {}, + result: ok(), + }) + expect(event?.input).toEqual({ id: 7, token: "(hidden)" }) + }) + + it.each(["scalar", 42, true, null, ["secret"]])("enforces the allowlist for unsupported input %j", (rawInput) => { + const options = { path: "test", auth, rawInput, note: {}, result: ok() } + expect(auditEvent({ ...options, option: { capture: "full", fields: ["id"] } })?.input).toEqual({}) + expect(auditEvent({ ...options, option: full })?.input).toEqual(auditJson(rawInput)) + expect( + auditEvent({ + ...options, + option: { capture: "full", fields: ["id"] }, + result: { ok: false, error: new TRPCError({ code: "FORBIDDEN" }) }, + })?.input + ).toBeNull() + }) + it("prefers the target the handler names, and gives the option's the result", () => { const option = { capture: "full", diff --git a/tests/freshman.test.ts b/tests/freshman.test.ts index 0be1a83..81083a9 100644 --- a/tests/freshman.test.ts +++ b/tests/freshman.test.ts @@ -18,6 +18,9 @@ vi.hoisted(() => { }) }) +const events = vi.hoisted(() => ({ write: vi.fn() })) +vi.mock("@/audit/store", () => ({ writeAuditEvent: events.write })) + const db = vi.hoisted(() => ({ transaction: vi.fn(), set: vi.fn(), update: vi.fn() })) vi.mock("@/db", async () => ({ DB: { transaction: db.transaction, update: db.update }, @@ -48,6 +51,12 @@ beforeEach(() => { const chain = { where: () => ({ returning: async () => [{ id: "featured-1" }] }) } db.set.mockReturnValue(chain) db.update.mockReturnValue({ set: db.set }) + db.transaction.mockImplementation(async (run) => + run({ + select: () => ({ from: () => ({ where: () => ({ for: async () => [{ id: "featured-1", title: "Before" }] }) }) }), + update: db.update, + }) + ) }) describe("Freshman validation", () => { @@ -126,6 +135,70 @@ describe("Freshman authorization", () => { it("takes authorship from the token and clears a legacy modifier", async () => { await freshman.createCaller(context(editor, ["web:content:write"])).updateCard({ id: "featured-1", ...fields }) expect(db.set).toHaveBeenCalledWith({ ...fields, legacyModifiedByTgId: null, modifiedBySub: "editor" }) + expect(events.write).toHaveBeenCalledWith( + expect.objectContaining({ + targetType: "web.freshmanCard", + targetId: "featured-1", + before: { id: "featured-1", title: "Before" }, + after: { id: "featured-1" }, + }) + ) + }) + + it("records both rows of a card swap from the transaction results", async () => { + const before = [ + { id: "featured-1", ...fields }, + { id: "featured-2", ...fields, title: "Second" }, + ] + const after = [ + { ...before[0], title: "Second" }, + { ...before[1], title: "Title" }, + ] + const returning = vi.fn().mockResolvedValueOnce([after[0]]).mockResolvedValueOnce([after[1]]) + db.transaction.mockImplementation(async (run) => + run({ + select: () => ({ from: () => ({ where: () => ({ orderBy: () => ({ for: async () => before }) }) }) }), + update: () => ({ set: () => ({ where: () => ({ returning }) }) }), + }) + ) + await freshman.createCaller(context(editor, ["web:content:write"])).moveCard({ id: "featured-1", direction: 1 }) + expect(returning).toHaveBeenCalledTimes(2) + expect(events.write).toHaveBeenCalledWith( + expect.objectContaining({ + targetId: "featured-1", + before, + after, + }) + ) + }) + + it("records both FAQ slots when selecting an already featured FAQ", async () => { + const rows = Array.from({ length: 5 }, (_, position) => ({ position, faqId: position + 1 })) + const after = [ + { position: 0, faqId: 2 }, + { position: 1, faqId: 1 }, + ] + let reads = 0 + db.transaction.mockImplementation(async (run) => + run({ + execute: async () => {}, + select: () => ({ + from: () => + ++reads === 1 ? { orderBy: async () => rows } : { where: () => ({ for: async () => [{ id: 2 }] }) }, + }), + delete: () => ({ where: async () => {} }), + insert: () => ({ values: () => ({ returning: async () => after }) }), + }) + ) + await freshman.createCaller(context(editor, ["web:content:write"])).selectFaq({ position: 0, faqId: 2 }) + expect(events.write).toHaveBeenCalledWith( + expect.objectContaining({ + targetType: "web.freshmanFaq", + targetId: "0", + before: rows.slice(0, 2), + after, + }) + ) }) it("rejects a client-supplied author", async () => { diff --git a/tests/people-router.test.ts b/tests/people-router.test.ts index 7439a9c..6af3415 100644 --- a/tests/people-router.test.ts +++ b/tests/people-router.test.ts @@ -1,8 +1,11 @@ import type { Actor } from "@polinetwork/auth-kit" -import { beforeAll, describe, expect, it } from "vitest" +import { beforeAll, describe, expect, it, vi } from "vitest" import type { Context } from "@/trpc" import { harness, master, permissions, profile } from "./people-fixtures" +const events = vi.hoisted(() => ({ write: vi.fn() })) +vi.mock("@/audit/store", () => ({ writeAuditEvent: events.write })) + Object.assign(process.env, { NODE_ENV: "test", BETTER_AUTH_SECRET: "test-secret-with-at-least-thirty-two-characters", @@ -49,6 +52,20 @@ describe("People tRPC contract", () => { r.createCaller(context(permissions, undefined, ["backend:public:read"])).me.get() ).rejects.toMatchObject({ code: "FORBIDDEN" }) }) + it("uses the validated mutation target when extra ids are stripped", async () => { + const h = harness() + await h.seed() + const person = await h.service.createPerson(master, profile()) + events.write.mockClear() + await makeRouter(h.service) + .createCaller(context()) + .persons.setNote({ + id: person.id, + personId: "P-9999", + note: "Updated", + } as never) + expect(events.write).toHaveBeenCalledWith(expect.objectContaining({ targetId: person.id })) + }) it("validates profiles before reaching People", async () => { const h = harness() await h.seed() @@ -67,7 +84,15 @@ describe("People tRPC contract", () => { const caller = makeRouter(h.service).createCaller(context([], actor)) await expect(caller.me.get()).rejects.toMatchObject({ code: "FORBIDDEN" }) expect(await caller.me.invite({ token })).toMatchObject({ name: "Luca Verdi" }) + events.write.mockClear() await caller.me.acceptInvite({ token }) + expect(events.write).toHaveBeenCalledWith( + expect.objectContaining({ + targetType: "people.person", + targetId: "P-0003", + input: null, + }) + ) expect(h.state().persons["P-0003"].accountSub).toBe("newcomer") }) it("mounts every People procedure with a dashboard policy", () => { From b200a45db2e6cd9c3b8e10734aaaa31a88114736 Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Tue, 17 Nov 2026 16:45:27 +0100 Subject: [PATCH 08/12] fix(audit): keep only the declared fields of an e-sign request's output requests.create kept `output` whole from the raw input, so extra keys a caller sent under it were stored. An allowlist field can now name a nested field (`output.driveId`), and the request keeps only driveId, folderId and fileName. --- README.md | 2 +- src/audit/index.ts | 24 ++++++++++++++++++++++-- src/audit/option.ts | 2 +- src/routers/esign.ts | 7 ++++++- tests/audit-json.test.ts | 15 +++++++++++++++ 5 files changed, 45 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 8bbab50..bc7ab2a 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ without one, like its policy: - `.meta(audit.full())`: also the input, the reason and the states before and after that the handler adds with `ctx.audit.set(...)`. `fields` keeps only those fields of the input, for inputs that carry personal data or free - text not worth keeping. + text not worth keeping; a dotted field (`output.driveId`) keeps one field of a nested object. - `.meta(audit.minimal({ target }))`: only the actor, the action, the target and the outcome, for areas that keep their own history behind their own permissions. People records each change in the person's activity history, and the Libro Soci in `soci_activity`. diff --git a/src/audit/index.ts b/src/audit/index.ts index 51f9eeb..8df05fa 100644 --- a/src/audit/index.ts +++ b/src/audit/index.ts @@ -159,13 +159,33 @@ function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | nu return error === null ? { outcome: AUDIT_OUTCOME.SUCCESS, error: null } : { outcome: AUDIT_OUTCOME.FAILURE, error } } -/** Select declared input fields before redaction and size limits are applied. */ +/** + * Select declared input fields before redaction and size limits are applied. A dotted field (`output.driveId`) keeps + * only that field of a nested object, and a null object stays null. + */ function onlyFields(input: unknown, fields: readonly string[] | undefined): unknown { if (!fields) return input if (input instanceof FormData || input instanceof Map) input = Object.fromEntries(input.entries()) if (input === null || typeof input !== "object" || Array.isArray(input)) return {} const record = input as Record - return Object.fromEntries(fields.flatMap((field) => (Object.hasOwn(record, field) ? [[field, record[field]]] : []))) + const nested = new Map() + const picked: Record = {} + for (const field of fields) { + const dot = field.indexOf(".") + if (dot === -1) { + if (Object.hasOwn(record, field)) picked[field] = record[field] + continue + } + const head = field.slice(0, dot) + nested.set(head, [...(nested.get(head) ?? []), field.slice(dot + 1)]) + } + for (const [head, rest] of nested) { + if (!Object.hasOwn(record, head)) continue + const value = record[head] + if (value === null) picked[head] = null + else if (typeof value === "object" && !Array.isArray(value)) picked[head] = onlyFields(value, rest) + } + return picked } /** diff --git a/src/audit/option.ts b/src/audit/option.ts index 1d996f8..c8a32d5 100644 --- a/src/audit/option.ts +++ b/src/audit/option.ts @@ -26,7 +26,7 @@ export type AuditOption = | { capture: "minimal"; target?: AuditTargetOf; action?: string } /** * Also the input, the states the handler adds and the reason. `fields` keeps only those fields of the input, for - * inputs that carry personal data or free text not worth keeping. + * inputs that carry personal data or free text not worth keeping; `"output.driveId"` keeps one field of a nested object. */ | { capture: "full"; fields?: readonly string[]; target?: AuditTargetOf; action?: string } diff --git a/src/routers/esign.ts b/src/routers/esign.ts index fa6cb05..8f0926c 100644 --- a/src/routers/esign.ts +++ b/src/routers/esign.ts @@ -190,7 +190,12 @@ export const esignRouter = createTRPCRouter({ requests: createTRPCRouter({ create: user - .meta(audit.full({ fields: ["title", "documentId", "ordered", "output"] })) + .meta( + // Only the declared parts of `output`: the raw input is recorded, so extra keys a caller sends would be kept. + audit.full({ + fields: ["title", "documentId", "ordered", "output.driveId", "output.folderId", "output.fileName"], + }) + ) .input( z.object({ title: z.string().trim().min(1).max(200), diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts index c6bc502..494c6dd 100644 --- a/tests/audit-json.test.ts +++ b/tests/audit-json.test.ts @@ -180,6 +180,21 @@ describe("auditEvent", () => { expect(event?.input).toEqual({ userId: "u1", assocNumber: 7 }) }) + it("keeps only the declared fields of a nested object, and keeps it null", () => { + const option = { capture: "full", fields: ["title", "output.driveId", "output.fileName"] } as const + const options = { path: "esign.requests.create", option, auth, note: {}, result: ok() } + const event = auditEvent({ + ...options, + rawInput: { title: "T", output: { driveId: "d", fileName: "f.pdf", extra: "x".repeat(500) }, email: {} }, + }) + expect(event?.input).toEqual({ title: "T", output: { driveId: "d", fileName: "f.pdf" } }) + expect(auditEvent({ ...options, rawInput: { title: "T", output: null } })?.input).toEqual({ + title: "T", + output: null, + }) + expect(auditEvent({ ...options, rawInput: { output: "not an object" } })?.input).toEqual({}) + }) + it("selects fields before object and total size limits, while still redacting", () => { const padding = Object.fromEntries(Array.from({ length: 105 }, (_, i) => [`excluded${i}`, "x".repeat(2000)])) const event = auditEvent({ From dc16bbff119050ad6346ee66f42c3697b245bd63 Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Tue, 17 Nov 2026 16:45:27 +0100 Subject: [PATCH 09/12] fix(web): serialize project reorders before reading the order they replace Two concurrent reorders could read the same order as their audit 'before'. Lock the projects table in SHARE ROW EXCLUSIVE mode first, as the freshman FAQ slots do, so each reads the order the previous one committed. --- src/routers/web/projects.ts | 4 +++- tests/projects-reorder.test.ts | 11 +++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/routers/web/projects.ts b/src/routers/web/projects.ts index c0293af..28c34a8 100644 --- a/src/routers/web/projects.ts +++ b/src/routers/web/projects.ts @@ -1,4 +1,4 @@ -import { asc, eq } from "drizzle-orm" +import { asc, eq, sql } from "drizzle-orm" import z from "zod" import { audit } from "@/audit/option" import { uploadBlob } from "@/azure/blob" @@ -139,6 +139,8 @@ export default createTRPCRouter({ .select({ id: PROJECTS.id, title: PROJECTS.title, order: PROJECTS.order }) .from(PROJECTS) .orderBy(asc(PROJECTS.order)) + // Serialize reorders before reading, so each one's `before` is the order the previous one committed. + await tx.execute(sql`LOCK TABLE ${PROJECTS} IN SHARE ROW EXCLUSIVE MODE`) const before = await order() const updated = await Promise.all( input.projectIds.map((id, index) => diff --git a/tests/projects-reorder.test.ts b/tests/projects-reorder.test.ts index e8187d8..273a5d4 100644 --- a/tests/projects-reorder.test.ts +++ b/tests/projects-reorder.test.ts @@ -4,6 +4,7 @@ import { auditRecorder } from "@/audit" const state = vi.hoisted(() => ({ projects: { id: Symbol("id"), title: Symbol("title"), order: Symbol("order") }, rows: [] as { id: number; title: string; order: number }[], + calls: [] as string[], })) vi.mock("drizzle-orm", async (importOriginal) => ({ ...(await importOriginal()), @@ -14,9 +15,13 @@ vi.mock("@/azure/blob", () => ({ uploadBlob: vi.fn() })) vi.mock("@/utils/web", () => ({ getImageExtension: vi.fn() })) vi.mock("@/db", () => { const tx = { + execute: async () => { + state.calls.push("lock") + }, select: (fields: Record) => ({ from: () => ({ orderBy: async () => + state.calls.push("select") && [...state.rows] .sort((a, b) => a.order - b.order) .map((row) => Object.fromEntries(Object.keys(fields).map((key) => [key, row[key as keyof typeof row]]))), @@ -58,6 +63,7 @@ const reorder = router.reorderProjects as unknown as { }) => Promise } beforeEach(() => { + state.calls = [] state.rows = [ { id: 1, title: "One", order: 5 }, { id: 2, title: "Two", order: 5 }, @@ -93,3 +99,8 @@ it("retains the audit when a matching row already has the requested order", asyn expect(audit.skipped()).toBe(false) expect(audit.note.before).toEqual(audit.note.after) }) +it("locks the projects before reading the order it replaces", async () => { + const audit = auditRecorder() + await reorder.handler({ input: { projectIds: [2, 1] }, ctx: { audit: audit.recorder } }) + expect(state.calls).toEqual(["lock", "select", "select"]) +}) From 7428d9d483560e15971eff042336ea4ff410cc16 Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Tue, 17 Nov 2026 17:20:20 +0100 Subject: [PATCH 10/12] fix(audit): bound copies in UTF-8 bytes, keep only scalar allowlisted fields, and keep documentKey - The 64 KB limit on a copy counted characters, so text in multibyte characters could store up to three times as much. It now counts the UTF-8 bytes of the JSON. - An allowlisted field keeps its value only when it is a string, number, boolean or null: an object or array from the raw input could carry anything. Regulations name the parts of `from` and `source` they keep (`from.kind`, `from.id`), the only allowlist that relied on objects. - `documentKey` matched the `*Key` secret suffix and was hidden, losing which regulation a delega or draft is about. It is kept as an identifier. --- src/audit/index.ts | 21 ++++++++++++++------- src/regulations/router.ts | 6 ++++-- tests/audit-json.test.ts | 25 ++++++++++++++++++++++++- 3 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/audit/index.ts b/src/audit/index.ts index 8df05fa..7ecbc18 100644 --- a/src/audit/index.ts +++ b/src/audit/index.ts @@ -53,10 +53,13 @@ export function auditRecorder() { /** Field names that hold secrets: whole names, or the last word of a camelCase name (`idempotencyKey`). */ const SECRET_KEY = /^(pass(word)?|passwd|secret|token|ticket|otp|code|key)$|(Pass(word)?|Secret|Token|Ticket|Otp|Key)$/ +/** Names that end like a secret but hold a public identifier, such as a regulation's `documentKey`. */ +const NOT_SECRET = new Set(["documentKey"]) +const isSecret = (key: string) => SECRET_KEY.test(key) && !NOT_SECRET.has(key) const MAX_STRING = 2000 const MAX_ITEMS = 100 const MAX_DEPTH = 8 -/** A copy longer than this, as JSON, is replaced by a note of its size. */ +/** A copy larger than this many bytes, as UTF-8 JSON, is replaced by a note of its size. */ const MAX_JSON = 64 * 1024 function copyOf(value: unknown, depth: number): AuditJson { @@ -82,7 +85,7 @@ function copyOf(value: unknown, depth: number): AuditJson { const entries = Object.entries(value).filter(([, item]) => item !== undefined && typeof item !== "function") const copy: { [key: string]: AuditJson } = {} for (const [key, item] of entries.slice(0, MAX_ITEMS)) - copy[key] = SECRET_KEY.test(key) ? "(hidden)" : copyOf(item, depth + 1) + copy[key] = isSecret(key) ? "(hidden)" : copyOf(item, depth + 1) if (entries.length > MAX_ITEMS) copy["(more)"] = `${entries.length - MAX_ITEMS} more fields` return copy } @@ -95,8 +98,8 @@ function copyOf(value: unknown, depth: number): AuditJson { */ export function auditJson(value: unknown): AuditJson { const copy = copyOf(value, 0) - const size = JSON.stringify(copy)?.length ?? 0 - return size > MAX_JSON ? `(${size} characters, too large to keep)` : copy + const size = Buffer.byteLength(JSON.stringify(copy) ?? "", "utf8") + return size > MAX_JSON ? `(${size} bytes, too large to keep)` : copy } /** The caller from the verified token; null without one, so such calls are not recorded. */ @@ -159,9 +162,13 @@ function outcomeOf(result: Result): { outcome: TAuditOutcome; error: string | nu return error === null ? { outcome: AUDIT_OUTCOME.SUCCESS, error: null } : { outcome: AUDIT_OUTCOME.FAILURE, error } } +/** Whether an allowlisted field holds a value that is kept as it is: objects and arrays could carry anything. */ +const isScalar = (value: unknown) => + value === null || typeof value === "string" || typeof value === "number" || typeof value === "boolean" + /** - * Select declared input fields before redaction and size limits are applied. A dotted field (`output.driveId`) keeps - * only that field of a nested object, and a null object stays null. + * Select declared input fields before redaction and size limits are applied. Only scalar values are kept; a dotted + * field (`output.driveId`) reaches into a nested object, and a null object stays null. */ function onlyFields(input: unknown, fields: readonly string[] | undefined): unknown { if (!fields) return input @@ -173,7 +180,7 @@ function onlyFields(input: unknown, fields: readonly string[] | undefined): unkn for (const field of fields) { const dot = field.indexOf(".") if (dot === -1) { - if (Object.hasOwn(record, field)) picked[field] = record[field] + if (Object.hasOwn(record, field) && isScalar(record[field])) picked[field] = record[field] continue } const head = field.slice(0, dot) diff --git a/src/regulations/router.ts b/src/regulations/router.ts index 5683198..3858bd0 100644 --- a/src/regulations/router.ts +++ b/src/regulations/router.ts @@ -20,8 +20,10 @@ const AUDITED_FIELDS = [ "userId", "role", "to", - "from", - "source", + "from.kind", + "from.id", + "source.kind", + "source.id", "kind", "title", "name", diff --git a/tests/audit-json.test.ts b/tests/audit-json.test.ts index 494c6dd..54bdff8 100644 --- a/tests/audit-json.test.ts +++ b/tests/audit-json.test.ts @@ -42,7 +42,18 @@ describe("auditJson", () => { expect(copy["(more)"]).toBe("3 more fields") const large = Object.fromEntries(Array.from({ length: 50 }, (_, i) => [`k${i}`, "x".repeat(2000)])) - expect(auditJson(large)).toMatch(/^\(\d+ characters, too large to keep\)$/) + expect(auditJson(large)).toMatch(/^\(\d+ bytes, too large to keep\)$/) + }) + + it("measures the size of a copy in UTF-8 bytes, not characters", () => { + // 20 strings of 2,000 characters: about 40 KB of ASCII, about 120 KB in three-byte characters. + const of = (char: string) => Object.fromEntries(Array.from({ length: 20 }, (_, i) => [`k${i}`, char.repeat(2000)])) + expect(auditJson(of("x"))).toEqual(of("x")) + expect(auditJson(of("€"))).toMatch(/^\(\d+ bytes, too large to keep\)$/) + }) + + it("keeps a regulation's documentKey, which is an identifier and not a secret", () => { + expect(auditJson({ documentKey: "statuto", apiKey: "2" })).toEqual({ documentKey: "statuto", apiKey: "(hidden)" }) }) it("hides whole secret names and camelCase secret suffixes only", () => { @@ -195,6 +206,18 @@ describe("auditEvent", () => { expect(auditEvent({ ...options, rawInput: { output: "not an object" } })?.input).toEqual({}) }) + it("keeps only scalar values of the declared fields, dropping objects and arrays", () => { + const event = auditEvent({ + path: "test", + auth, + option: { capture: "full", fields: ["id", "from", "tags", "from.kind", "from.id", "gone"] }, + rawInput: { id: 1, from: { kind: "draft", id: "d1", extra: "x" }, tags: ["a"], gone: null }, + note: {}, + result: ok(), + }) + expect(event?.input).toEqual({ id: 1, from: { kind: "draft", id: "d1" }, gone: null }) + }) + it("selects fields before object and total size limits, while still redacting", () => { const padding = Object.fromEntries(Array.from({ length: 105 }, (_, i) => [`excluded${i}`, "x".repeat(2000)])) const event = auditEvent({ From 7846704c0c2a9c13968ec6c251717d30c8c352e8 Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Tue, 17 Nov 2026 17:20:21 +0100 Subject: [PATCH 11/12] fix(web): record the documents catalog's targets and states in the audit log Each web.documents mutation names its category or document and keeps the state before and after, like the other web content routers: updates read the row they change in their transaction, deletions keep what they removed (a document with its versions), reorders keep both orders. States are set once the transaction has committed, and only for calls that changed something. updateCategory now locks the category it reads. The PostgreSQL test checks the recorded events. --- src/routers/web/documents.ts | 140 +++++++++++++++++++--------- tests/documents-postgres.fixture.ts | 58 ++++++++++++ tests/documents-postgres.test.ts | 2 +- 3 files changed, 155 insertions(+), 45 deletions(-) diff --git a/src/routers/web/documents.ts b/src/routers/web/documents.ts index 845a29e..2e8574a 100644 --- a/src/routers/web/documents.ts +++ b/src/routers/web/documents.ts @@ -198,6 +198,14 @@ async function nextDocumentPosition(tx: Tx, categoryId: number) { return last === null ? 0 : last + 1 } +const categoryTarget = (row: { id: number; title: string }) => ({ + type: "web.documentCategory", + id: row.id, + label: row.title, +}) +const documentTarget = (row: { id: number; title: string }) => ({ type: "web.document", id: row.id, label: row.title }) + +// Audit states are set once a transaction has committed, and only for calls that changed something. export default createTRPCRouter({ getCatalog: policy(publicData) .output( @@ -273,7 +281,7 @@ export default createTRPCRouter({ .meta(audit.full()) .input(z.object({ slug: slugSchema, title: titleSchema, description: descriptionSchema })) .mutation(async ({ input, ctx }) => { - return DB.transaction(async (tx) => { + const result = await DB.transaction(async (tx) => { await lockCategoryList(tx) const [{ last }] = await tx.select({ last: max(CATEGORIES.position) }).from(CATEGORIES) const [row] = await tx @@ -284,6 +292,8 @@ export default createTRPCRouter({ if (!row) return { error: "DUPLICATE_SLUG" as const } return row }) + if (!("error" in result)) ctx.audit.set({ target: categoryTarget(result), after: result }) + return result }), updateCategory: policy(dashboard("web:content:write")) @@ -298,7 +308,9 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const { id, ...fields } = input - return DB.transaction(async (tx) => { + const result = await DB.transaction(async (tx) => { + const [before] = await tx.select(categoryColumns).from(CATEGORIES).where(eq(CATEGORIES.id, id)).for("update") + if (!before) return { error: "NOT_FOUND" as const } if (fields.slug !== undefined && (await slugTaken(tx, CATEGORIES, fields.slug, id))) return { error: "DUPLICATE_SLUG" as const } const [row] = await tx @@ -307,15 +319,18 @@ export default createTRPCRouter({ .where(eq(CATEGORIES.id, id)) .returning(categoryColumns) if (!row) return { error: "NOT_FOUND" as const } - return row + return { before, row } }).catch(duplicateSlug("web_document_categories")) + if (result.error) return { error: result.error } + ctx.audit.set({ target: categoryTarget(result.row), before: result.before, after: result.row }) + return result.row }), deleteCategory: policy(dashboard("web:content:write")) .meta(audit.full()) .input(z.object({ id: idSchema })) - .mutation(async ({ input }) => { - return DB.transaction(async (tx) => { + .mutation(async ({ input, ctx }) => { + const result = await DB.transaction(async (tx) => { if (!(await lockCategories(tx, [input.id])).has(input.id)) return { error: "NOT_FOUND" as const } const [document] = await tx .select({ id: DOCUMENTS.id }) @@ -323,16 +338,22 @@ export default createTRPCRouter({ .where(eq(DOCUMENTS.categoryId, input.id)) .limit(1) if (document) return { error: "CATEGORY_NOT_EMPTY" as const } - await tx.delete(CATEGORIES).where(eq(CATEGORIES.id, input.id)) - return { error: null } + const [deleted] = await tx.delete(CATEGORIES).where(eq(CATEGORIES.id, input.id)).returning(categoryColumns) + return { deleted } }) + if (result.error) return { error: result.error } + ctx.audit.set({ + target: { type: "web.documentCategory", id: input.id, label: result.deleted?.title }, + before: result.deleted, + }) + return { error: null } }), reorderCategories: policy(dashboard("web:content:write")) .meta(audit.full()) .input(z.object({ ids: orderSchema })) - .mutation(async ({ input }) => { - return DB.transaction(async (tx) => { + .mutation(async ({ input, ctx }) => { + const result = await DB.transaction(async (tx) => { await lockCategoryList(tx) const current = await tx .select({ id: CATEGORIES.id }) @@ -346,10 +367,23 @@ export default createTRPCRouter({ ) ) return { error: "INVALID_ORDER" as const } + const order = () => + tx + .select({ id: CATEGORIES.id, title: CATEGORIES.title, position: CATEGORIES.position }) + .from(CATEGORIES) + .orderBy(asc(CATEGORIES.position), asc(CATEGORIES.id)) + const before = await order() for (const [position, id] of input.ids.entries()) await tx.update(CATEGORIES).set({ position }).where(eq(CATEGORIES.id, id)) - return { error: null } + return { before, after: await order() } + }) + if (result.error) return { error: result.error } + ctx.audit.set({ + target: { type: "web.documentCategories", id: "order", label: "Document categories order" }, + before: result.before, + after: result.after, }) + return { error: null } }), createDocument: policy(dashboard("web:content:write")) @@ -363,7 +397,7 @@ export default createTRPCRouter({ }) ) .mutation(async ({ input, ctx }) => { - return DB.transaction(async (tx) => { + const result = await DB.transaction(async (tx) => { if (!(await lockCategories(tx, [input.categoryId])).has(input.categoryId)) return { error: "CATEGORY_NOT_FOUND" as const } const [row] = await tx @@ -379,6 +413,8 @@ export default createTRPCRouter({ if (!row) return { error: "DUPLICATE_SLUG" as const } return row }) + if (!("error" in result)) ctx.audit.set({ target: documentTarget(result), after: result }) + return result }), updateDocument: policy(dashboard("web:content:write")) @@ -394,16 +430,13 @@ export default createTRPCRouter({ ) .mutation(async ({ input, ctx }) => { const { id, categoryId, description, ...fields } = input - return DB.transaction(async (tx) => { + const result = await DB.transaction(async (tx) => { await lockDocument(tx, id) - const [current] = await tx - .select({ categoryId: DOCUMENTS.categoryId }) - .from(DOCUMENTS) - .where(eq(DOCUMENTS.id, id)) - if (!current) return { error: "NOT_FOUND" as const } - const locked = await lockCategories(tx, [current.categoryId, categoryId ?? current.categoryId]) + const [before] = await tx.select(documentColumns).from(DOCUMENTS).where(eq(DOCUMENTS.id, id)) + if (!before) return { error: "NOT_FOUND" as const } + const locked = await lockCategories(tx, [before.categoryId, categoryId ?? before.categoryId]) if (categoryId !== undefined && !locked.has(categoryId)) return { error: "CATEGORY_NOT_FOUND" as const } - const moving = categoryId !== undefined && categoryId !== current.categoryId + const moving = categoryId !== undefined && categoryId !== before.categoryId if (fields.slug !== undefined && (await slugTaken(tx, DOCUMENTS, fields.slug, id))) return { error: "DUPLICATE_SLUG" as const } const [row] = await tx @@ -417,58 +450,70 @@ export default createTRPCRouter({ }) .where(eq(DOCUMENTS.id, id)) .returning(documentColumns) - return row + if (!row) return { error: "NOT_FOUND" as const } + return { before, row } }).catch(duplicateSlug("web_documents")) + if (result.error) return { error: result.error } + ctx.audit.set({ target: documentTarget(result.row), before: result.before, after: result.row }) + return result.row }), deleteDocument: policy(dashboard("web:content:write")) .meta(audit.full()) .input(z.object({ id: idSchema })) - .mutation(async ({ input }) => { - const files = await DB.transaction(async (tx) => { + .mutation(async ({ input, ctx }) => { + const deleted = await DB.transaction(async (tx) => { await lockDocument(tx, input.id) - const [document] = await tx - .select({ categoryId: DOCUMENTS.categoryId }) - .from(DOCUMENTS) - .where(eq(DOCUMENTS.id, input.id)) + const [document] = await tx.select(documentColumns).from(DOCUMENTS).where(eq(DOCUMENTS.id, input.id)) if (!document) return null await lockCategories(tx, [document.categoryId]) // Wait for in-flight version uploads before removing their document. await tx.select({ id: DOCUMENTS.id }).from(DOCUMENTS).where(eq(DOCUMENTS.id, input.id)).for("update") - const versions = await tx - .delete(VERSIONS) - .where(eq(VERSIONS.documentId, input.id)) - .returning({ file: VERSIONS.file }) + const versions = await tx.delete(VERSIONS).where(eq(VERSIONS.documentId, input.id)).returning(versionColumns) await tx.delete(DOCUMENTS).where(eq(DOCUMENTS.id, input.id)) - return versions.map((version) => version.file) + return { document, versions } }) - if (!files) return { error: "NOT_FOUND" as const } - await deleteBlobs(files) + if (!deleted) return { error: "NOT_FOUND" as const } + ctx.audit.set({ + target: documentTarget(deleted.document), + before: { ...deleted.document, versions: deleted.versions }, + }) + await deleteBlobs(deleted.versions.map((version) => version.file)) return { error: null } }), reorderDocuments: policy(dashboard("web:content:write")) .meta(audit.full()) .input(z.object({ categoryId: idSchema, ids: orderSchema })) - .mutation(async ({ input }) => { - return DB.transaction(async (tx) => { + .mutation(async ({ input, ctx }) => { + const result = await DB.transaction(async (tx) => { if (!(await lockCategories(tx, [input.categoryId])).has(input.categoryId)) return { error: "INVALID_ORDER" as const } - const current = await tx - .select({ id: DOCUMENTS.id }) - .from(DOCUMENTS) - .where(eq(DOCUMENTS.categoryId, input.categoryId)) + const order = () => + tx + .select({ id: DOCUMENTS.id, title: DOCUMENTS.title, position: DOCUMENTS.position }) + .from(DOCUMENTS) + .where(eq(DOCUMENTS.categoryId, input.categoryId)) + .orderBy(asc(DOCUMENTS.position), asc(DOCUMENTS.id)) + const before = await order() if ( !sameIds( - current.map((row) => row.id), + before.map((row) => row.id), input.ids ) ) return { error: "INVALID_ORDER" as const } for (const [position, id] of input.ids.entries()) await tx.update(DOCUMENTS).set({ position }).where(eq(DOCUMENTS.id, id)) - return { error: null } + return { before, after: await order() } }) + if (result.error) return { error: result.error } + ctx.audit.set({ + target: { type: "web.documentCategory", id: input.categoryId, label: "Documents order" }, + before: result.before, + after: result.after, + }) + return { error: null } }), addVersion: policy(dashboard("web:content:write")) @@ -484,8 +529,11 @@ export default createTRPCRouter({ const author = createdByColumns(authorOf(ctx.actor)) const bytes = await readPdf(file) - const [document] = await DB.select({ id: DOCUMENTS.id }).from(DOCUMENTS).where(eq(DOCUMENTS.id, documentId)) + const [document] = await DB.select({ id: DOCUMENTS.id, title: DOCUMENTS.title }) + .from(DOCUMENTS) + .where(eq(DOCUMENTS.id, documentId)) if (!document) return { error: "NOT_FOUND" as const } + ctx.audit.set({ target: documentTarget(document) }) const [existing] = await DB.select({ id: VERSIONS.id }) .from(VERSIONS) .where(and(eq(VERSIONS.documentId, documentId), eq(VERSIONS.version, version))) @@ -512,15 +560,19 @@ export default createTRPCRouter({ throw error }) if ("error" in result) await deleteBlobs([uploadedFile.url]) + else ctx.audit.set({ after: result }) return result }), deleteVersion: policy(dashboard("web:content:write")) .meta(audit.full()) .input(z.object({ id: idSchema })) - .mutation(async ({ input }) => { - const [deleted] = await DB.delete(VERSIONS).where(eq(VERSIONS.id, input.id)).returning({ file: VERSIONS.file }) + .mutation(async ({ input, ctx }) => { + const [deleted] = await DB.delete(VERSIONS) + .where(eq(VERSIONS.id, input.id)) + .returning({ ...versionColumns, documentId: VERSIONS.documentId }) if (!deleted) return { error: "NOT_FOUND" as const } + ctx.audit.set({ target: { type: "web.document", id: deleted.documentId }, before: deleted }) await deleteBlobs([deleted.file]) return { error: null } }), diff --git a/tests/documents-postgres.fixture.ts b/tests/documents-postgres.fixture.ts index 1157240..a00fc01 100644 --- a/tests/documents-postgres.fixture.ts +++ b/tests/documents-postgres.fixture.ts @@ -277,6 +277,64 @@ try { assert.equal(await website.getDocument({ slug: "statuto" }), null) checked.push("deleting a version or a document deletes its files") + // The router is called on its own here, so actions are its procedure names without the `web.documents.` prefix. + const events = async (action: string) => + // `#>> '{}'` reads a jsonb object or a jsonb string holding one (as drizzle's Bun driver stores them) as text. + ( + await connection`SELECT outcome, target_type, target_id, before #>> '{}' AS before, after #>> '{}' AS after + FROM common_audit_events WHERE action = ${action} ORDER BY id` + ).map( + (event: { + outcome: string + target_type: string + target_id: string + before: string | null + after: string | null + }) => ({ + ...event, + before: event.before === null ? null : JSON.parse(event.before), + after: event.after === null ? null : JSON.parse(event.after), + }) + ) + // Not found, a duplicate slug, then the rename. + const [missingCategory, duplicateSlugEvent, renamed] = await events("updateCategory") + assert.deepEqual( + [renamed.outcome, renamed.target_type, renamed.target_id, renamed.before.title, renamed.after.title], + ["success", "web.documentCategory", String(bilanci.id), "Bilanci", "Bilanci annuali"] + ) + for (const failed of [missingCategory, duplicateSlugEvent]) + assert.deepEqual([failed.outcome, failed.before, failed.after], ["failure", null, null]) + const deletedCategories = await events("deleteCategory") + assert.deepEqual( + deletedCategories.map((event: { outcome: string; before: { title: string } | null }) => [ + event.outcome, + event.before?.title ?? null, + ]), + [ + ["failure", null], + ["success", "Vuota"], + ["failure", null], + ] + ) + const deletedVersion = (await events("deleteVersion")).find( + (event: { outcome: string }) => event.outcome === "success" + ) + assert.deepEqual( + [deletedVersion?.target_type, deletedVersion?.target_id, deletedVersion?.before.version], + ["web.document", String(statutoDoc.id), v3.version] + ) + const deletedDocument = (await events("deleteDocument")).find( + (event: { outcome: string }) => event.outcome === "success" + ) + assert.deepEqual( + [deletedDocument?.target_id, deletedDocument?.before.title, deletedDocument?.before.versions.length], + [String(statutoDoc.id), "Statuto", 2] + ) + const [{ count }] = await connection`SELECT count(*)::int AS count FROM common_audit_events + WHERE outcome <> 'success' AND after IS NOT NULL` + assert.equal(count, 0) + checked.push("audit events name the target and keep the states before and after, only for changes that happened") + for (const slug of ["Invalid", "a b", "", "a".repeat(101)]) await assert.rejects(website.getDocument({ slug }), { code: "BAD_REQUEST" }) checked.push("document lookups validate the public slug") diff --git a/tests/documents-postgres.test.ts b/tests/documents-postgres.test.ts index 1af5937..64b48e8 100644 --- a/tests/documents-postgres.test.ts +++ b/tests/documents-postgres.test.ts @@ -10,7 +10,7 @@ it.skipIf(!process.env.SEED_TEST_DB_PORT)( timeout: 60000, }) const result = JSON.parse(stdout.trim().split("\n").at(-1) ?? "{}") - expect(result.checked).toHaveLength(14) + expect(result.checked).toHaveLength(15) }, 65000 ) From 7793f1d9216cb9df2dae2327cc986062c5881839 Mon Sep 17 00:00:00 2001 From: Tommaso Morganti Date: Tue, 17 Nov 2026 17:20:21 +0100 Subject: [PATCH 12/12] fix(web): lock projects in EXCLUSIVE mode before reordering them SHARE ROW EXCLUSIVE does not conflict with the row locks editProject takes with SELECT FOR UPDATE: an edit holding one, then updating, would deadlock with a reorder updating the same row. EXCLUSIVE waits for such edits and still lets plain reads go on. --- src/routers/web/projects.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/routers/web/projects.ts b/src/routers/web/projects.ts index 28c34a8..4d38acb 100644 --- a/src/routers/web/projects.ts +++ b/src/routers/web/projects.ts @@ -139,8 +139,9 @@ export default createTRPCRouter({ .select({ id: PROJECTS.id, title: PROJECTS.title, order: PROJECTS.order }) .from(PROJECTS) .orderBy(asc(PROJECTS.order)) - // Serialize reorders before reading, so each one's `before` is the order the previous one committed. - await tx.execute(sql`LOCK TABLE ${PROJECTS} IN SHARE ROW EXCLUSIVE MODE`) + // Serialize reorders before reading, so each one's `before` is the order the previous one committed. EXCLUSIVE + // also waits for edits holding row locks (which would otherwise deadlock with this update); plain reads go on. + await tx.execute(sql`LOCK TABLE ${PROJECTS} IN EXCLUSIVE MODE`) const before = await order() const updated = await Promise.all( input.projectIds.map((id, index) =>