Security model
Supabase serves your tables over HTTP to anyone holding the project's anon key, and that key ships inside every browser bundle. Row-Level Security is the only thing between that key and your data. AccessNexus exists to make the generated RLS correct by construction, and to flag the schema choices that would make it wrong.
This page lists what the generator guarantees, the rules you (or your agent) must not break, what validate checks, and what is outside AccessNexus' reach.
What the generator guarantees
These hold for every schema, with no option to turn them off (except enableRLS: false, below).
RLS is enabled and forced on every table
ALTER TABLE notes ENABLE ROW LEVEL SECURITY;
ALTER TABLE notes FORCE ROW LEVEL SECURITY;FORCE applies the policies to the table's owner as well, so only roles with BYPASSRLS (on Supabase: postgres, the service role) skip them.
No policy means no access
AccessNexus fails closed:
- A table without
policiesis deny-all for the API:anonandauthenticatedget nothing. Only the service role andSECURITY DEFINERcode reach it.validatewarnsNO_POLICIES. - Once a table has
policies, an operation without a rule is denied.{ select: policy.isOwner('user_id') }allows reads of your rows and no writes at all. - The only way to open a table is an explicit
enableRLS: false, whichvalidatereports asRLS_DISABLED.
Auth calls are evaluated once per query
Every builder emits (SELECT auth.uid()) and (SELECT auth.jwt()), never the bare call. Postgres turns the subquery into an initPlan and evaluates it once per statement instead of once per row, which on large tables is the difference between milliseconds and seconds.
USING ((SELECT auth.uid()) = user_id)Raw SQL you write yourself (policy.custom, RPC bodies, lockColumns({ unless })) is emitted as written. validate warns UNWRAPPED_AUTH_CALL when it finds a bare call there.
Reads and writes are checked separately
| Operation | Generated clause | Meaning |
|---|---|---|
select | USING (rule) | which rows are visible |
delete | USING (rule) | which rows can be deleted |
insert | WITH CHECK (rule) | what may be written |
update | USING (rule) and WITH CHECK (rule) | which rows can be targeted, and what they may become |
UPDATE always carries an explicit WITH CHECK, so an update can never move a row out of the rule (for example, change user_id to someone else's id). policy.withCheck(using, check) writes WITH CHECK (using AND check): the written row must pass both. Only { mode: 'replace' } drops using from WITH CHECK, for rows that may legitimately leave it.
Policies target the narrowest role
A select policy is granted TO public only when some branch can be true for an anonymous visitor (policy.public(), policy.custom(...), a column-to-column matchesField); otherwise TO authenticated. Write policies are TO public only when an explicit policy.public() decides them. You never pick the role: the predicate does.
Membership checks cannot recurse or be called by strangers
policy.belongsTo(..., { table, foreignKey, localKey }) and definePermissions tenants compile to a helper function:
CREATE OR REPLACE FUNCTION accessnexus_org_members_belongs_to_1_fn(p_value uuid)
RETURNS boolean
LANGUAGE sql
SECURITY DEFINER
SET search_path = pg_catalog, public
AS $$
SELECT EXISTS (SELECT 1 FROM public.org_members WHERE org_id = p_value AND user_id = (SELECT auth.uid()));
$$;
REVOKE EXECUTE ON FUNCTION accessnexus_org_members_belongs_to_1_fn(uuid) FROM PUBLIC, anon;
GRANT EXECUTE ON FUNCTION accessnexus_org_members_belongs_to_1_fn(uuid) TO authenticated;SECURITY DEFINERlets a policy onorg_membersconsultorg_memberswithout re-entering its own RLS (no infinite recursion).SET search_path = pg_catalog, publicand the schema-qualifiedpublic.org_membersstop a caller from shadowing objects through their search path.EXECUTEis revoked fromPUBLICandanon. A helper used by a policy that also admits anonymous visitors (TO public) is granted toanontoo, which is safe because it returns false whenauth.uid()is null.
Writes are pinned in definePermissions
By default:
createalso requiresownerColumn = (SELECT auth.uid()): nobody creates rows attributed to someone else (pinOwnerOnCreate).update, andcreatewhen its level is not already tenant-scoped, also require membership of the row's (new) tenant: an owner cannot move a row into another org (pinTenant).
Both stay on unless you set pinOwnerOnCreate: false or pinTenant: false. See Permissions.
Trigger and RPC functions are hardened
trigger.auditLogandtrigger.softDeletefunctions areSECURITY DEFINERwithSET search_path = pg_catalog, publicand schema-qualified targets. The audit table keepsinsert: policy.none(), so users cannot forge entries.- A soft-delete guard rejects a plain
UPDATEby an API user that sets or clears the soft-delete column. OnlyDELETE, judged by the delete policy, can soft-delete a row. security: 'definer'RPCs getREVOKE EXECUTE ... FROM PUBLIC, anonand a grant toauthenticatedandservice_role, unlessallowAnon: true.- Every generated function pins
search_path.
Literals are quoted and identifiers are validated
- Every string value that ends up in SQL (
hasRole('admin'),belongsTowherevalues, the column lists ofauditLogandlockColumns) goes through a quoting function that doubles single quotes. - Table, column and RPC names are emitted unquoted, so
validateand the builders only accept lowercase snake_case that is not a reserved SQL keyword (INVALID_IDENTIFIER,RESERVED_IDENTIFIER).
Global roles come from a claim users cannot set
policy.hasRole('admin') and role arrays in definePermissions read app_metadata.role from the JWT. Only the server can set app_metadata (supabase.auth.admin.updateUserById(id, { app_metadata: { role: 'admin' } }) with the service role, or a custom access token hook). The top-level JWT role claim, which is the Postgres role authenticated, is never used.
push will not create helpers that break silently
The definer helpers rely on their owner bypassing RLS. push checks that the connected role has BYPASSRLS (or is a superuser) whenever the SQL contains SECURITY DEFINER, and refuses with PUSH_ROLE_NO_BYPASSRLS before applying anything. Without that check, every membership-checked query would fail with stack depth limit exceeded.
Rules you must not break
These come straight from the agent reference that ships in the package (node_modules/accessnexus/llms.txt). Each one closes a hole that the generator cannot close for you, because it depends on what your data means.
Keep every table closed by default
- Every table the client reads or writes has
policies. Usepolicy.none()for operations that must be blocked. - Never "fix" a permission error by removing policies, adding
enableRLS: false, or usingpolicy.public()for writes. - Never use
policy.public()(or'public'indefinePermissions) for insert, update or delete unless anonymous writes were asked for (PUBLIC_WRITE). - The
selectrule must admit rows the user just inserted or updated. The SDK'screateandupdatereturn the row (INSERT/UPDATE ... RETURNING), which fails with an RLS error otherwise. On a membership table, addpolicy.isOwner('user_id')toselect.
Do not leak private columns
- Never put private columns (email, phone, address) in a table whose
selectispolicy.isAuthenticated()orpolicy.public(). On Supabase anyone can sign up, so "signed in" means "anyone". - RLS cannot restrict columns. Use a separate table for private data, readable with
policy.isOwner(...).
Pin who owns a row
- A table keyed by the user id (
profiles.idreferencingauth.users.id) inserts withpolicy.isOwner('id'), neverpolicy.isAuthenticated(), which lets anyone create another user's row (USER_ROW_INSERT_OPEN). policy.isOwnercolumns areuuidand referenceauth.users.idorprofiles.id(OWNER_COLUMN_NOT_UUID).- Keep the write pins of
definePermissions. Disable one only for a concrete reason, and then pin the right column yourself. - Freeze owner, creator and tenant columns with
trigger.lockColumns. AnUPDATErule never does it, because RLS is row-level.
Get tenants and roles right
- Org or team roles are tenant roles (
{ scope: 'tenant', roles }, orbelongsTowithwhere: { role: [...] }), notpolicy.hasRole, which is platform-wide (GLOBAL_ROLE_IN_TENANT_TABLE). - Membership checks go through
belongsTowith a membership table, ordefinePermissionstenants. Do not re-implement them withpolicy.customsubqueries on the same table: that recurses. Trigger conditions such aslockColumns({ unless })are the exception; they run as the caller, outside any policy. - Never use
belongsTo(col)without a membership table while users can writeprofiles.<col>(BELONGS_TO_PROFILE_WRITABLE): a user would set their ownorg_idand join any tenant. - A tenant-scoped row that references another tenant-scoped row (tasks pointing at projects) needs a same-tenant guard trigger (
TENANT_PARENT_UNCHECKED). Foreign keys are checked without RLS. See the recipe. - Never let a member grant a role equal to or above their own. Restrict inserts and updates of the membership table, for example with
policy.custom("role = 'member'")for editors. - Never add a "bootstrap" INSERT rule on the membership table ("the creator may add themselves as owner"). It stays valid forever, so a removed creator can rejoin. Add the first member with a
security: 'definer'AFTER INSERT trigger on the tenant table.
Membership needs consent when it reveals people
Visibility derived from sharing a tenant
"Org mates can see my profile" is only private if joining an org needs the person's consent. If admins may insert any user_id into the membership table, anyone can create an org, add a stranger, and read that stranger's data as a "co-member".
When co-members can read each other's personal data, add people only through an accepted invitation: a security: 'definer' RPC that checks the invited, confirmed email, with the membership table's insert set to policy.none(). Otherwise, keep personal columns out of what co-members can read. See Invitation-based membership.
Raw SQL is your responsibility
- Never write a bare
auth.uid()/auth.jwt()inpolicy.customor RPC bodies. Always(SELECT auth.uid()). security: 'definer'RPCs and triggers, and the service role, bypass RLS: check the caller inside (see RPCs). In a definer body, schema-qualify tables (public.team_members).- Use
withCheck(using, check)to add conditions on the written row. Only use{ mode: 'replace' }when the row may legitimately leaveusing. - Do not edit generated SQL to change behavior. Change the schema and regenerate. Applied migrations are immutable;
pushrefuses drift.
What validate warns about
validate (also run by generate, diff and push) checks the schema statically. Errors stop the command; warnings are printed and must be treated as bugs unless the choice is deliberate.
| Code | Severity | What it catches |
|---|---|---|
NO_POLICIES | warning | Table is deny-all for the API. Fine for server-only tables; a bug if the client uses it. |
RLS_DISABLED | warning | enableRLS: false: the table is open to the anon key. |
RLS_DISABLED_WITH_POLICIES | error | enableRLS: false plus policies that would never apply. |
PUBLIC_WRITE | warning | Insert, update or delete open to anonymous visitors through policy.public(). |
USER_ROW_INSERT_OPEN | warning | A user-keyed table anyone signed in can insert into. Use policy.isOwner('id'). |
TENANT_PARENT_UNCHECKED | warning | A child row can point at another tenant's parent. Add the same-tenant guard. |
BELONGS_TO_PROFILE_WRITABLE | error | Tenant lookup through a profile column users can write. |
GLOBAL_ROLE_IN_TENANT_TABLE | warning | hasRole mixed with tenant checks on one table; usually meant as a tenant role. |
UNWRAPPED_AUTH_CALL | warning | Bare auth.uid() / auth.jwt() / auth.role() in custom SQL, an RPC body or a lockColumns condition. |
AUDIT_INSERT_OPEN | warning | Users can insert into the audit table, so they can forge entries. |
AUDIT_TABLE_MISMATCH | error | The audit table lacks the columns the audit trigger writes. |
OWNER_COLUMN_NOT_UUID | error | isOwner on a column that is not uuid. |
UNKNOWN_COLUMN, UNKNOWN_TABLE | error | A policy, trigger, reference or index names something that does not exist. TypeScript does not catch these in manual mode. |
INVALID_IDENTIFIER, RESERVED_IDENTIFIER | error | A name that cannot be emitted unquoted. |
INVALID_ENUM_VALUE | error | A belongsTo where value (for example a misspelled role) that is not in the membership column's enum. |
The complete list, with fixes, is in Troubleshooting.
validate cannot prove that a policy expresses what you meant. It checks structure, not intent: an isOwner on the wrong column, or a definer RPC that forgets to check the caller, passes.
Realtime follows the same policies
A table with realtime: true delivers a change only to subscribers whose SELECT policy shows the row. Channel rules become RLS policies on realtime.messages; with table, joining also requires that the user can SELECT the row the topic names, checked as that user. A malformed topic is denied, never an error. push only drops channel policies with its own <prefix>_rt_ names. See Realtime.
Edge Functions check the caller first
Each declared function runs behind withSupabase (@supabase/server): a request without the credential its auth requires gets 401 before your code runs, and verify_jwt stays on unless the function accepts 'none' (validate warns EDGE_FUNCTION_PUBLIC). The JSON body is validated against input before the handler sees it; ctx.supabase runs as the caller, so RLS still decides. Unexpected exceptions answer a generic 500. See Edge Functions.
What AccessNexus does not protect
Outside the generator's reach
- Column-level reads. RLS decides which rows a user sees, never which columns. If a user can read a row, they can read every column of it. Split private data into its own table. (
trigger.lockColumnsrestricts which columns can be written, not read.) - The service role. The
service_rolekey and thepostgresconnection bypass RLS completely. Keep them on the server; never in a browser bundle, a mobile app, or a public repository. - Definer code you write.
security: 'definer'RPCs and triggers run with the owner's rights. Their safety is exactly as good as the checks in their body. - Raw SQL you write.
policy.custom(),lockColumns({ unless })and RPC bodies are emitted verbatim. - Objects outside the schema. Policies created in the dashboard or by other migrations, functions you wrote by hand, storage buckets, and other schemas are not managed. Postgres ORs permissive policies, so an old permissive policy would keep granting access next to the generated ones: on the tables the schema manages,
pushrefuses withFOREIGN_POLICIESuntil you review them and pass--replace-foreign-policies. Policies on tables outside the schema are left alone. - Rules removed from the schema. In v0.4.4-next,
pushre-creates the policies the schema declares but does not drop a generated policy whose operation you removed (for example deletingdelete:from a table'spolicies), and does not drop removed triggers or RPCs. To revoke access, set the operation topolicy.none()instead of deleting the key, or drop the old object by hand. - Supabase's default grants. Tables and invoker RPCs keep Supabase's default grants (
anonandauthenticatedcan call them); RLS is what protects the rows inside. - Soft-deleted rows. They stay visible through RLS; only the SDK filters them. Add
deleted_at IS NULLtoselectif raw API reads must not see them. - Realtime deletes. Supabase does not apply RLS to DELETE events: every subscriber of a published table receives them, with the primary key only. Do not encode secrets in primary keys of published tables.
- Edge Function code you write.
ctx.supabaseAdminbypasses RLS: when a handler uses it, filtering byctx.userClaimsis your job. Functions withauth: 'none'must authenticate the request themselves (for example a webhook signature). - Public Realtime channels. Policies on
realtime.messagesonly apply to private channels (the SDK'schannels.*are private). Turn off public access in the project's Realtime settings so clients cannot join a topic as public.
Related
- Policies and Permissions
- RPCs: checking the caller in definer code
- Realtime: publication and channel authorization
- Edge Functions: typed, validated handlers
- Examples: the rules above, applied
- Existing projects: how imported policies are kept fail-closed
- Troubleshooting
- Limits & roadmap