feat(api): harden error-monitoring mutations (audit, rate limit, 403)

Document the public CRM-shaped API matrix, emit audit rows for resolve,
bulk-resolve, and purge (including dry run), apply a dedicated per-IP
rate limit on mutation routes, and return 403 when a valid session lacks
RBAC for the action. Add Vitest coverage for authorizeMutation.

Paperclip-Agent: Tiago Neves (urlKey tiago-neves; id 5d084b36-f38f-42a5-a92a-520005afda4f)
Paperclip-Issue: BIT-731
Paperclip-Run: 7d0edfac-5676-41fa-85c2-86d01a808894
Made-with: Cursor
This commit is contained in:
David Santos committed 2026-04-02 10:52:40 +00:00
1 parent 8b0044be82
commit 7b2be679cd
6 files changed
+270 -83

No files matched your search

+16
View File
@@ -13,6 +13,8 @@
* RATE_LOGIN_WINDOW_MS window in ms (default: 300000 = 5 min)
* RATE_GLOBAL_MAX global limit per window (default: 500)
* RATE_GLOBAL_WINDOW_MS window in ms (default: 60000 = 1 min)
* RATE_MUTATION_MAX CRM-shaped POST /api/error-monitoring/* mutations (default: 120)
* RATE_MUTATION_WINDOW_MS (default: 60000)
*
* Payload limits:
* INGEST_MAX_BODY_BYTES max ingest request body (default: 512 KB)
@@ -31,6 +33,8 @@ const RATE_LOGIN_MAX = parseInt(process.env.RATE_LOGIN_MAX ?? '10'
const RATE_LOGIN_WINDOW_MS = parseInt(process.env.RATE_LOGIN_WINDOW_MS ?? '300000', 10);
const RATE_GLOBAL_MAX = parseInt(process.env.RATE_GLOBAL_MAX ?? '500', 10);
const RATE_GLOBAL_WINDOW_MS = parseInt(process.env.RATE_GLOBAL_WINDOW_MS ?? '60000', 10);
const RATE_MUTATION_MAX = parseInt(process.env.RATE_MUTATION_MAX ?? '120', 10);
const RATE_MUTATION_WINDOW_MS = parseInt(process.env.RATE_MUTATION_WINDOW_MS ?? '60000', 10);
export const INGEST_MAX_BODY_BYTES = parseInt(process.env.INGEST_MAX_BODY_BYTES ?? String(512 * 1024), 10);
export const ADMIN_MAX_BODY_BYTES = parseInt(process.env.ADMIN_MAX_BODY_BYTES ?? String(64 * 1024), 10);
@@ -75,6 +79,17 @@ export default fp(async function rateLimitPlugin(app: FastifyInstance) {
max: RATE_GLOBAL_MAX,
timeWindow: RATE_GLOBAL_WINDOW_MS,
},
mutation: {
max: RATE_MUTATION_MAX,
timeWindow: RATE_MUTATION_WINDOW_MS,
keyGenerator: (request: FastifyRequest) => `errmon_mut:ip:${request.ip}`,
errorResponseBuilder: (_request: FastifyRequest, context: any) => ({
success: false,
error: 'TooManyRequests',
message: 'Error-monitoring mutation rate limit exceeded. Slow down.',
retryAfter: Math.ceil(context.ttl / 1000),
}),
},
});
/**
@@ -110,6 +125,7 @@ declare module 'fastify' {
ingest: Record<string, unknown>;
login: Record<string, unknown>;
global: Record<string, unknown>;
mutation: Record<string, unknown>;
};
}
}