Permission Model
Spine authorization is two independent layers, and the code says so verbatim: "RLS controls row access; design_schema controls what the principal can do. No permissions granted = none given." (.framework/functions/_shared/permissions.ts)
- Row visibility (RLS): Postgres policies scope every tenant table to the accounts the caller can reach — see Scope and Accounts.
- Action authorization (
record_permissions): per request, the permission engine evaluates the caller's role against the schema stamped on the record to decide create/read/update/delete and per-field read/write.
Principals
.framework/functions/_shared/principal.ts resolves two principal types:
- Human — Supabase JWT →
peoplerow →role_idjoin toroles; the principal carriesroles: [slug]and gets a JWT-scoped DB client (ctx.db), so RLS applies. - Machine —
x-api-keyheader →api_keysrow →scopesarray and the key'saccount_id. Machine principals getadminDb(service role), which bypasses RLS entirely; their only authorization is the scope check.
The three surfaces
detectSurface in .framework/functions/_shared/permissions.ts buckets the target by table name:
| Surface | Covers | Rule |
|---|---|---|
| First (default) | runtime records — items, threads, messages, people, accounts, … | record_permissions on the record's stamped design_schema |
| Second | config objects — apps, pipelines, triggers, ai_agents, embeddings, timers, integrations, roles, types, prompt_configs | system_admin full access; machine principals read-only; everyone else denied |
| Third | system metadata — logs, pipeline_executions, trigger_executions, link_types, links | system_admin/machine full; humans read only their own/account rows |
The second-surface row is the engine's policy — what detectSurface produces where the engine is consulted. Endpoint enforcement is uneven and is the contract that actually binds:
roleswrites are system-admin-only at RLS itself (migration045), so the table holds regardless of the endpoint.timersandtriggersmutations are enforced end-to-end (decision0026): every mutating handler callscanAccessConfigObject(triggerscreatevia the equivalent grant path), and migration081replaces their account-scopedFOR ALLpolicies with account-scoped read + system-admin write — direct PostgREST now matches the API.typesmutations areisSystemAdmin-gated at the handler.pipelines,ai_agents,prompt_configs, andembeddingsmutations still have no engine gate — an account member writes throughctx.db, bounded only by account-scoped RLS. Same shape the0026fix closed for timers/triggers, not yet adjudicated for these tables.integrationsdeliberately usesaccount_admin, notsystem_admin.- Machine principals skip all of this:
ctx.db= service role → RLS bypass (see the security checklist "Known ungated surfaces" for the full list).
First-surface evaluation
resolveFirstSurfacePermissions runs on every runtime-record access:
- Schema source is the record. The stamped
design_schemaon the record is authoritative; there is deliberately no fallback to the type row — a record with no schema or norecord_permissionsis denied. - Role resolution. The caller's role slug comes from
people.role_id → roles.slug(one role per person, cached ~60s). No role, no permissions. - Record grant.
record_permissions[roleSlug]is an array of actions (create,read,update,delete). The special key'all'is a wildcard evaluated for every authenticated principal — grants toallapply to anyone who passed RLS. - Field merge. For each declared field,
fields.<name>.permissions[role]— an array drawn fromread/writeonly — replaces the default. Fields without an entry default to{ read: canRead, write: canUpdate }from the record grant. That is whyrecord_permissionsis the base, most restrictive grant and field entries are overrides: an explicit["read"]on a field removes write access thatupdatewould otherwise give.
Role keys are matched verbatim.
record_permissionskeys must be realroles.slugvalues — nothing normalizes them. The seeded admin slug issystem_admin(underscore,.framework/migrations/003_core_system_data.sql); asystem-adminkey silently matches nobody and denies the admin.
Enforcement points
- Read (
sanitizeFirstSurfaceRecordData):canReadgates the record;datais filtered per field byfieldPermissions[name].read; values are formatted throughvalidation_schema(formatFieldData); legacymetadatais stripped from responses. Records with an emptyfieldsmap pass through whole at the record grant. - Update (
validateFirstSurfaceUpdatePermissions):canUpdategates the record, then every key insidedatamust havewrite— one denied field fails the whole update with a per-field error. Values are sanitized throughsanitizeFieldData(type coercion + constraint checks; violations are errors, not silent drops). Top-level non-datakeys pass through the gate. Writes therefore require both the record action and field-level write — undeclared fields have nofieldPermissionsentry and 403. - Create (
admin-datacreate): permission is checked against the type'sdesign_schema(which becomes the record's stamp) before insert;type_id/type_slugis required. A machine principal with*:*scope bypasses the create check.
system_admin bypass
isSystemAdmin (roles contains system_admin) short-circuits record access, sanitization, and update validation in permissions.ts, and gates the types.ts/roles.ts/apps.ts write APIs entirely. The same slug drives the RLS bypass inside get_accessible_accounts — system admins see all accounts.
Machine (API key) scopes
checkMachineScope grants an action when the key's scopes contain resource:action exactly, resource:*, or *:* (e.g. items:read, items:*, *:*). Scopes are the only thing standing between a leaked API key and adminDb, so issue the narrowest scope that works — *:* also bypasses the create permission check in admin-data.
Canonical example
The seeded pattern from the framework migrations — base grant first, field override only where needed:
{
"record_permissions": {
"system_admin": ["create", "read", "update", "delete"],
"member_admin": ["create", "read", "update"],
"member": ["create", "read", "update"]
},
"fields": {
"title": { "data_type": "text", "label": "Title", "required": true, "system": true },
"status": { "data_type": "select", "label": "Status", "required": true,
"options": ["open", "closed"],
"permissions": { "member": ["read"] } },
"internal_notes": { "data_type": "textarea", "label": "Internal Notes",
"required": false,
"permissions": { "member": [] } }
}
}Reading it: member can create and update records, so every declared field defaults to read+write — except status, whose ["read"] override strips member's write, and internal_notes, whose empty override hides it from member entirely. member_admin touches neither override, so it inherits record-level read+write on both. That is the whole discipline: add a fields.*.permissions entry only where the base grant is wrong for that field — usually to restrict a sensitive or system: true field, occasionally to loosen one the base grant closes.
See also
- Design Schema — where
record_permissionslives - Scope and Accounts — the RLS layer beneath this one
- ../roles.md — the role entity (
permissionson roles bound the subject side at the app/UI level)