papertrail/db/migrations/0064_security-events.sql
caelum0x 514086bdda feat: Phase 3 enterprise hardening — gateway chain, XDR threat detection, compliance ops, plan tiers
- lib/security/gatewayChain.ts: composable ordered front-door (ip-allowlist -> authN -> authZ ->
  provenance -> rate/quota) as an additive primitive + docs/gateway-rollout.md (BUILD not fork Envoy)
- XDR: 0064 security_events + lib/security/threatDetection.ts (4 deterministic detectors) +
  securityScan + /console/admin/security dashboard
- Compliance ops: retention purge (with SQL-identifier guard), nightly chain-integrity verifyChain,
  access-review snapshot; folded into the daily /api/cron/tick (one Vercel-Hobby cron)
- Plan tiers: 0065 Researcher/Team/Pharma-Enterprise + plan_features entitlements + requireFeature +
  /console/billing/tier (Part-11 e-sign, audit export, SSO/SCIM gated to Enterprise)
- docs/compliance-controls.md (SOC2/HIPAA/21 CFR Part 11 controls matrix)
- Fixed: honest new-active-key threat description; removed duplicate /security page

migrations 0064/0065/0066 applied to Neon; tsc + build green.
2026-07-11 00:18:36 +03:00

38 lines
1.8 KiB
SQL

-- Tenant-scoped threat-detection ("XDR") events. The security scan (see
-- lib/security/securityScan.ts + app/api/cron/security-scan/route.ts) runs
-- deterministic detectors over telemetry the platform already owns
-- (api_requests, rate_limit_events, error_events) and persists a
-- security_events row for each new finding. High-severity findings are also
-- appended to the org's WORM audit chain for tamper-evident retention.
--
-- Multi-tenant: org_id on every row, cascading from orgs so a tenant can never
-- read or mutate another tenant's findings. `detail` is a free-form jsonb blob
-- carrying ONLY ids/counts/thresholds — never raw claim/patient text, secrets,
-- or PHI. `source_ip` is nullable because the current telemetry does not always
-- carry a client IP; when present it is a coarse network identifier, never a
-- user identity. Idempotent — safe to run repeatedly.
create table if not exists security_events (
id uuid primary key default gen_random_uuid(),
org_id uuid not null references orgs(id) on delete cascade,
kind text not null,
severity text not null default 'low'
check (severity in ('low', 'medium', 'high', 'critical')),
detail jsonb not null default '{}'::jsonb,
source_ip text,
detected_at timestamptz not null default now()
);
-- Primary access path: an org's findings, newest first (the event feed).
create index if not exists security_events_org_detected_idx
on security_events(org_id, detected_at desc);
-- Severity filtering / rollups on the feed.
create index if not exists security_events_severity_idx
on security_events(severity);
-- Dedup support: the scan looks up recent findings of the same (org, kind) to
-- avoid re-emitting the same standing condition on every sweep.
create index if not exists security_events_org_kind_detected_idx
on security_events(org_id, kind, detected_at desc);