Files
errors.bitreport.pt/docs/permission-matrix.md
T
David Santos 7b2be679cd 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
2026-04-02 10:52:40 +00:00

147 lines
6.6 KiB
Markdown

# Permission Matrix — BitReport Error Reporting Admin
> **Source of truth:** `api/src/types/auth.ts` → `ROLE_PERMISSIONS`.
> This document is generated manually from that table. If they diverge, the code wins.
## Phase 1 Roles
| Role | Description |
|------|-------------|
| `superadmin` | Full access. Can manage other admin users and all system settings. |
| `admin` | Project and error management. Cannot manage other admin users. |
| `viewer` | Read-only across all resources. Cannot mutate anything. |
## Permission Matrix
| Permission | superadmin | admin | viewer |
|------------|:----------:|:-----:|:------:|
| **Projects** | | | |
| `projects:read` | ✅ | ✅ | ✅ |
| `projects:create` | ✅ | ✅ | ❌ |
| `projects:update` | ✅ | ✅ | ❌ |
| `projects:delete` | ✅ | ❌ | ❌ |
| **Errors** | | | |
| `errors:read` | ✅ | ✅ | ✅ |
| `errors:triage` | ✅ | ✅ | ❌ |
| `errors:delete` | ✅ | ✅ | ❌ |
| **Ingest Tokens** | | | |
| `ingest_tokens:read` | ✅ | ✅ | ✅ |
| `ingest_tokens:create` | ✅ | ✅ | ❌ |
| `ingest_tokens:revoke` | ✅ | ✅ | ❌ |
| **Sources (Active Collectors)** | | | |
| `sources:read` | ✅ | ✅ | ✅ |
| `sources:create` | ✅ | ✅ | ❌ |
| `sources:update` | ✅ | ✅ | ❌ |
| `sources:delete` | ✅ | ✅ | ❌ |
| **Admin Users** | | | |
| `users:read` | ✅ | ❌ | ❌ |
| `users:create` | ✅ | ❌ | ❌ |
| `users:update` | ✅ | ❌ | ❌ |
| `users:delete` | ✅ | ❌ | ❌ |
| **Settings** | | | |
| `settings:read` | ✅ | ✅ | ✅ |
| `settings:update` | ✅ | ❌ | ❌ |
| **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
Phase 1 keeps user management exclusive to `superadmin` to prevent privilege escalation:
an `admin` cannot create another `admin` or grant themselves `superadmin`.
### Why viewers can read audit logs
Transparency: all authenticated staff should be able to see what actions were taken,
even if they cannot perform those actions. Audit logs contain no secrets.
### Why `projects:delete` requires superadmin
Deleting a project is destructive and irreversible — it cascades to errors, tokens,
and sources. Only the highest privilege level can trigger this.
### Future: per-project scoping (phase 2)
In phase 2, a `project_members` table will add per-project roles:
```sql
CREATE TABLE project_members (
user_id UUID REFERENCES admin_users(id),
project_id UUID REFERENCES projects(id),
role VARCHAR(20) CHECK (role IN ('admin', 'viewer')),
PRIMARY KEY (user_id, project_id)
);
```
`superadmin` will retain global access; `admin` and `viewer` will be scoped per project.
## Audit Events
The following operations MUST emit audit log entries (see `api/src/plugins/audit.ts`):
| Action | Trigger | Key metadata |
|--------|---------|--------------|
| `auth.login` | Successful login | `role` |
| `auth.login_failed` | Failed login attempt | `reason` (user_not_found / bad_password / inactive) |
| `auth.logout` | Explicit logout | — |
| `users.create` | New admin created | `new_role`, `new_email` |
| `users.update` | Role/email changed | `old_role`, `new_role` |
| `users.delete` | Admin deleted | `deleted_email`, `deleted_role` |
| `projects.create` | Project registered | `project_id`, `name` |
| `projects.update` | Project settings changed | `fields_changed` |
| `projects.delete` | Project deleted | `project_id`, `name` |
| `ingest_tokens.create` | Token issued | `project_id`, `token_id` |
| `ingest_tokens.revoke` | Token revoked | `project_id`, `token_id` |
| `sources.create` | Collector source added | `project_id`, `source_type` |
| `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
| Endpoint | Limit | Window | Key |
|----------|-------|--------|-----|
| `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
| Endpoint group | Max body size |
|---------------|---------------|
| `/api/ingest/*` | 512 KB |
| Admin API (`/admin/*`) | 64 KB |