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

@@ -13,6 +13,29 @@ import {
type MutationAuth,
} from '../../lib/errorMonitoringAuth.js';
async function auditMutation(
app: FastifyInstance,
request: FastifyRequest,
auth: MutationAuth,
action: string,
target: string,
metadata: Record<string, unknown>,
): Promise<void> {
const actor_id = auth.via === 'session' ? auth.session.userId : null;
const meta: Record<string, unknown> = {
...metadata,
via: auth.via,
...(auth.via === 'session' ? { email: auth.session.email } : { principal: 'resolve_api_key' }),
};
await app.auditLog({
action,
actor_id,
actor_ip: request.ip,
target,
metadata: meta,
});
}
function parseJsonBody(raw: unknown): Record<string, unknown> {
if (raw === null || typeof raw !== 'object' || Array.isArray(raw)) {
return {};
@@ -137,6 +160,12 @@ async function handleResolve(request: FastifyRequest, reply: FastifyReply): Prom
});
}
await auditMutation(request.server, request, auth, 'error_monitoring.resolve', row.incidentId, {
unique_id: uniqueId,
project_id: row.projectId,
from_state: row.currentState,
});
return void reply.send({
success: true,
message: 'Erro marcado como resolvido',
@@ -217,6 +246,12 @@ async function handleBulkResolve(request: FastifyRequest, reply: FastifyReply):
conn.release();
}
await auditMutation(request.server, request, auth, 'error_monitoring.bulk_resolve', 'batch', {
by: 'unique_ids',
resolved,
input_count: capped.length,
});
return void reply.send({
success: true,
message: 'Bulk resolve by unique_ids completed',
@@ -294,6 +329,12 @@ async function handleBulkResolve(request: FastifyRequest, reply: FastifyReply):
conn.release();
}
await auditMutation(request.server, request, auth, 'error_monitoring.bulk_resolve', 'batch', {
by: 'message_pattern',
resolved,
pattern_count: patterns.length,
});
return void reply.send({
success: true,
message: 'Bulk resolve by message pattern completed',
@@ -317,6 +358,14 @@ async function handlePurgeResolved(request: FastifyRequest, reply: FastifyReply)
const count = Number((countRows[0] as { c?: number }).c ?? 0);
if (dryRun) {
await auditMutation(
request.server,
request,
auth,
'error_monitoring.purge_resolved_dry_run',
'er_incidents',
{ would_delete: count },
);
return void reply.send({
success: true,
message: 'Dry run',
@@ -336,6 +385,10 @@ async function handlePurgeResolved(request: FastifyRequest, reply: FastifyReply)
`DELETE FROM er_incidents WHERE current_state = 'resolved'`,
);
await auditMutation(request.server, request, auth, 'error_monitoring.purge_resolved', 'er_incidents', {
deleted: del.affectedRows,
});
return void reply.send({
success: true,
message: `Apagados ${del.affectedRows} incidentes resolvidos`,
@@ -344,7 +397,8 @@ async function handlePurgeResolved(request: FastifyRequest, reply: FastifyReply)
}
export default async function errorMonitoringMutationsRoutes(app: FastifyInstance): Promise<void> {
app.post('/api/error-monitoring/resolve', handleResolve);
app.post('/api/error-monitoring/bulk-resolve', handleBulkResolve);
app.post('/api/error-monitoring/purge-resolved', handlePurgeResolved);
const rl = { config: { rateLimit: app.rateLimits.mutation } };
app.post('/api/error-monitoring/resolve', rl, handleResolve);
app.post('/api/error-monitoring/bulk-resolve', rl, handleBulkResolve);
app.post('/api/error-monitoring/purge-resolved', rl, handlePurgeResolved);
}