security
What it does
Security-admin console API: aggregates abuse signals into a "flagged accounts" report, bans/unbans people across three enforcement layers (the security_blocklist table, Supabase auth ban, and the Netlify edge firewall), and manages blocklist entries directly.
Route and methods
/.netlify/functions/security, reachable as /api/security (/api/* → /.netlify/functions/:splat). No method check — the action comes from ?action= or body.action:
action | Purpose |
|---|---|
flagged_accounts | 30-day rollup of security signals per person/account |
ban_user | Blocklist + Supabase ban + edge IP deny |
unban_user | Revoke blocklist rows + lift Supabase ban + remove edge rule |
blocklist | List security_blocklist rows (latest 500) |
blocklist_add | Upsert a blocklist entry |
blocklist_revoke | Stamp revoked_at on entries |
Unknown action → Unknown security action: <action> (HTTP 400).
Authentication
Standard createHandler plus requireSystemContextWithAudit — only system_admin principals (human system_admin role, *:* machine key, or internal secret) get in; anonymous → 401. All DB access uses adminDb.
Request contract
flagged_accounts — no parameters. Scans logs for span_name in security.rate_limit_exceeded, security.injection_detected, security.registration_blocked over the last 30 days (limit 500), buckets by person_id (falling back to account), and joins people, accounts, and active blocklist rows.
ban_user (body): at least one of person_id, email, ip required. Optional: reason, block_ip (default true), block_email (default true).
unban_user (body): person_id required.
blocklist — no parameters.
blocklist_add (body): type ∈ ip | email | email_domain | person_id | account_id and value required; optional severity (default 'hard'), reason, expires_at. Email and email-domain values are lowercased.
blocklist_revoke (body): id or (type, value).
Response contract
Standard { data, error, meta } envelope.
flagged_accounts→{ rows: [...] }, each row{ person_id, email, account_id, account_name, last_ip, last_country, person_status, signals, kinds, patterns, last_seen_at, banned }sorted by signal count descending.bannedis true when any matching person/email/IP/account blocklist entry is active.ban_user→{ ok: true, person_id, email, ip, layers }wherelayersreportsblocklist/auth/edgeasapplied,skipped, orerror: <msg>per layer.unban_user→{ ok: true, person_id, layers }.blocklist→{ rows: [...] }.blocklist_add→{ ok: true };blocklist_revoke→{ ok: true, revoked: <n> }.
Side effects
security_blocklistupserts keyed on(type, value)withbanned_by,severity,reason,expires_at; revoke stampsrevoked_at(rows are never deleted).ban_user/unban_usercallauth.admin.updateUserByIdwithban_duration: '876600h'(~100 years) /'none'when the person has anauth_uid.- Netlify edge sync: when
NETLIFY_ACCESS_TOKEN+NETLIFY_SITE_IDare set, IP entries are added to/removed from a managedspine-security:blocklistdeny rule on the site's firewall ruleset. Without those env vars the edge layer reportsskipped. Failures are reported inlayers.edge, not thrown. emitAuditwritessecurity.user_banned,security.user_unbanned,security.blocklist_add,security.blocklist_revokedaudit rows.- The blocklist is enforced elsewhere:
isBlockedRequest/isBlockedIpin the request middleware andcheckRegistrationBlocklistduring registration readsecurity_blocklist— this endpoint only writes it.
Errors
| Condition | HTTP | Message |
|---|---|---|
| Unknown action | 400 | Unknown security action: <action> |
| Missing ban target | 500 | person_id, email, or ip is required |
| Person not found | 500 | Person not found: <id> |
| Missing blocklist fields | 500 | type and value are required / id or (type, value) is required |
Invalid type | 500 | invalid type: <type> |
| Blocklist write failures | 500 | security_blocklist upsert failed: … etc. |
The system-admin denial is a real HTTP 403 — requireSystemContextWithAudit throws { statusCode: 401 | 403 } and createHandler maps it to the HTTP status (F-051 fix). See "Agent notes".
Agent notes
- Banning by bare
emailworks even when nopeoplerow exists — the email/domain still lands on the blocklist and blocks future registrations viacheckRegistrationBlocklist. ban_useralso blocklists the person's wholeaccount_id— banning a person can lock out their entire account. Checklayersin the response to confirm what was applied.unban_userrevokes only the rows matchingperson_id, the person's current email, and their current IP — theaccount_idrow written byban_useris not revoked, and neither are manually addedemail_domainentries. Revoke those separately viablocklist_revoke.- Per-layer results mean "success" is conditional:
layers.edge: 'skipped'just means Netlify env vars are absent, whilelayers.auth: 'skipped'means the person has noauth_uid(e.g. an unregistered invite stub). - Non-system-admin callers get a real HTTP 403 (
Authentication requiredfor anonymous,System context requiredfor authenticated non-admins) — the denial throws, it is not wrapped under 200.