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

+37
View File
@@ -44,6 +44,38 @@
| **Audit Logs** | | | |
| `audit_logs:read` | ✅ | ✅ | ✅ |
## Public CRM-shaped API (`/api/error-monitoring/*`)
These routes mirror the CRM error-monitoring module for Paperclip and automation. They use **query/body secrets** and optionally the **same admin JWT** as `/admin/*`.
### Read: `GET /api/error-monitoring/recent-errors`
| Credential | Notes |
|------------|--------|
| `api_key` query | Must match `ERROR_RESOLVE_API_KEY` or `ERROR_REPORTING_API_KEY` |
| `view_errors` query | Must match `DEBUG_VIEW_ERRORS_KEY` (read-oriented) |
| Admin session | Not used on this path; query credentials only |
At least one of the server secrets above must be configured or the endpoint returns **503**.
### Mutations: resolve, bulk-resolve, purge-resolved
| Route | Required RBAC permission (session) | Automation (`ERROR_RESOLVE_API_KEY` or `ERROR_REPORTING_API_KEY`) |
|-------|-----------------------------------|---------------------------------------------------------------------|
| `POST …/resolve` | `errors:triage` | Body `api_key` or header `X-Error-Resolve-Key` |
| `POST …/bulk-resolve` | `errors:triage` | Same |
| `POST …/purge-resolved` | `errors:delete` | Same key (destructive; protect key like a root password) |
| Role | `resolve` / `bulk-resolve` | `purge-resolved` |
|------|---------------------------|------------------|
| `viewer` | Forbidden (**403** if session present) | Forbidden |
| `admin` | Allowed (session or key) | Allowed (session or key) |
| `superadmin` | Allowed | Allowed |
**401 vs 403:** Missing or wrong automation key → **401**. Valid JWT cookie but role without permission → **403**. Expired or invalid JWT (and no valid key) → **401**.
If **no** resolve key is set in the environment, only **session + permission** can mutate (automation via shared key is disabled).
## Design Notes
### Why admins cannot manage users
@@ -91,6 +123,10 @@ The following operations MUST emit audit log entries (see `api/src/plugins/audit
| `sources.delete` | Source removed | `project_id`, `source_id` |
| `errors.triage` | Error state changed | `from_status`, `to_status`, `error_id` |
| `errors.delete` | Error deleted | `error_id`, `project_id` |
| `error_monitoring.resolve` | Hub `POST …/resolve` succeeded | `unique_id`, `project_id`, `from_state`, `via` |
| `error_monitoring.bulk_resolve` | Hub `POST …/bulk-resolve` succeeded | `by`, `resolved`, `via` |
| `error_monitoring.purge_resolved_dry_run` | Hub purge dry run | `would_delete`, `via` |
| `error_monitoring.purge_resolved` | Hub `POST …/purge-resolved` deleted rows | `deleted`, `via` |
| `settings.update` | System settings changed | `keys_changed` |
## Rate Limits
@@ -99,6 +135,7 @@ The following operations MUST emit audit log entries (see `api/src/plugins/audit
|----------|-------|--------|-----|
| `POST /admin/auth/login` | 10 requests | 5 minutes | per IP |
| `POST /api/ingest/*` | 1000 requests | 1 minute | per token (fallback: IP) |
| `POST /api/error-monitoring/resolve`, `…/bulk-resolve`, `…/purge-resolved` | 120 requests (env `RATE_MUTATION_MAX`) | 1 minute (`RATE_MUTATION_WINDOW_MS`) | per client IP (`errmon_mut:ip:*`) |
| All others | 500 requests | 1 minute | per IP |
## Payload Limits