सामग्री पर जाएँ
ऑराबेस लोगो
aurabase

Roles, keys and row level security bypass

Map each key and each tool to the PostgreSQL role it runs as, understand why two of those roles bypass row level security, and tell a privilege error from a policy that hides rows.

व्याख्या8 मि॰ पढ़ेंस्तर मध्यवर्ती

यह पेज अभी तक आपकी भाषा में उपलब्ध नहीं है. इसे अंग्रेजी में दिखाया गया है.

Mental model#

A key says who calls, a PostgreSQL role says what the query may touch. The gateway turns each key, and each signed-in user, into one role; PostgreSQL then applies that role's privileges (GRANT) and the row level security policies written for it. Two roles skip the policies on purpose: they belong to code you run on a server.

anon key, no user          ─▶ aura_anon            privileges + RLS
anon key + user token      ─▶ aura_authenticated   privileges + RLS, auth.uid() = the user
service_role key (SDK)     ─▶ aura_service_role    bypasses RLS
service_role key (/raw)    ─▶ tenant_<id>_admin    bypasses RLS, owns the tables
direct connection          ─▶ proj_xxxxxxxx        privileges + RLS

Keys#

KeyPrefixWhere it may liveBecomes
anonaura_anon_Browser, mobile app, anywhereaura_anon; aura_authenticated when a user token is sent with it
service_roleaura_sk_Server, CI, your terminalaura_service_role (SDK, REST), tenant_<id>_admin (/raw, schema API)
authenticatedaura_auth_; none by default, aura db api-keys create <name> --scope authenticatedServeraura_authenticated, without a user (auth.uid() is NULL); see API keys

Two initial keys, anon and service_role, are created with the project and shown once. Create, list and revoke keys with aura db api-keys or in the Studio under Settings › API keys. The gateway refuses a service_role key sent from a browser (401 SECRET_KEY_IN_BROWSER) or in a URL query string.

PostgreSQL roles of a project#

RoleLoginBypasses RLSUsed by
aura_anonnonoAnon key without a user; aura db raw --as anon
aura_authenticatednonoSigned-in users; aura db raw --as authenticated --user <uuid>
aura_service_rolenoyesservice_role key on the SDK and REST path
tenant_<id>nonoBase role of the project; the direct-connection role and the alias service_role inherit it
tenant_<id>_adminnoyesservice_role key on aura-db (/raw, the Studio SQL editor, schema API, migrations). Owns the tables it creates.
aura_authenticatoryesnoConnection role of aura-db and PostgREST, which switch to one of the roles above for each transaction
proj_xxxxxxxxyesnoDirect connection (psql, ORMs). Inherits tenant_<id> and aura_authenticated, may SET ROLE tenant_<id>_admin, and may only open its project's database.
anon, authenticated, service_rolenonoAliases kept for SQL written for other platforms: aura_anon, aura_authenticated and aura_service_role are members of them, so a grant or a policy written for anon also applies to aura_anon. The alias service_role itself does not have BYPASSRLS.

<id> is the project id with underscores instead of dashes. The three aura_* roles and aura_authenticator exist once per cluster; the tenant_ and proj_ roles exist per project.

Tables created through Aurabase are granted to aura_anon, aura_authenticated and aura_service_role at creation, and protected by row level security, which Aurabase enables on them. Functions are not: PostgreSQL's default EXECUTE for everyone is revoked, so a function is callable only by the roles you grant it to.

Roles that bypass row level security#

aura_service_role and tenant_<id>_admin have the PostgreSQL attribute BYPASSRLS. This is intended: they serve the service_role key, which is meant for code you control on a server (an admin back office, a webhook, a migration, the Studio). Such code needs to read and write every row, for every user, without a policy written for it.

What keeps this safe is where the key lives, not the role:

  • the key never goes to a browser: the gateway refuses it when the request has an Origin header;
  • it never travels in a URL: the gateway refuses it in a query string;
  • a request made with the key carries no user: auth.uid() is NULL, so a policy can never mistake it for a user.

If a service_role key leaks, revoke it (aura db api-keys revoke <id>) and create another.

See it from your project#

aura db raw runs as the administrator role, unless --as asks for a public role. The query below returns the same answer for each of the three:

aura db raw "select current_user like 'tenant\_%\_admin' as admin_role, (select count(*) from todos) as todos"
aura db raw --as authenticated --user 00000000-0000-4000-8000-0000000000a1 "select current_user, (select count(*) from todos) as todos"
aura db raw --as anon "select current_user, (select count(*) from todos) as todos"
  admin_role │ todos
  ───────────┼──────
  true       │ 4

  ℹ 1 row(s).
  ℹ Executed as authenticated (00000000-0000-4000-8000-0000000000a1).
  current_user       │ todos
  ───────────────────┼──────
  aura_authenticated │ 3

  ℹ 1 row(s).
  ℹ Executed as anon.
  current_user │ todos
  ─────────────┼──────
  aura_anon    │ 0

  ℹ 1 row(s).

The administrator role counts every todo of the example schema; the demo user alice sees her three; anon sees none.

Privilege or policy#

PostgreSQL treats a missing privilege and a missing policy differently, and the SDK shows the difference:

What you seeCauseFix
data: [], no error, and a meta.hintRLS is on and no policy lets the role read these rows. PostgreSQL hides rows instead of failing.Add a select policy for the role.
403 INSUFFICIENT_PRIVILEGE, new row violates row-level security policy for table "todos"An insert or update that the with check of the policy rejects.Write rows that the policy accepts (for an owner policy: your own user_id).
403 INSUFFICIENT_PRIVILEGE, permission denied for function todo_countThe role has no EXECUTE on the function.grant execute on function todo_count(uuid) to aura_authenticated;
403 INSUFFICIENT_PRIVILEGE, permission denied for table …The role has no privilege on the table: typically a table created over the direct connection.Enable RLS, then grant, then add a policy (see Connect to your database).
403 FORBIDDEN, Forbidden: `/raw` query is reserved to the service_role roleSQL sent with the anon key or a user token.Run SQL from a server with the service_role key.

The three INSUFFICIENT_PRIVILEGE cases share the PostgreSQL code 42501, so read the message:

import { createClient } from '@aurabase/aurabase-js'

const user = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)
const email = `roles-${Date.now()}@example.com`
await user.auth.signUp({ email, password: 'correct-horse-battery-9' })
await user.auth.signInWithPassword({ email, password: 'correct-horse-battery-9' })

const notMine = await user.from('todos').insert({ user_id: '00000000-0000-4000-8000-0000000000a1', title: 'Not mine' })
console.log(notMine.error?.status, notMine.error?.code, notMine.error?.message)

const anon = createClient(process.env.AURABASE_URL!, process.env.AURABASE_ANON_KEY!)
const count = await anon.rpc('todo_count', { p_user: '00000000-0000-4000-8000-0000000000a1' })
console.log(count.error?.status, count.error?.code, count.error?.message)

const hidden = await anon.from('profiles').select('username')
console.log(hidden.data, hidden.error)
403 INSUFFICIENT_PRIVILEGE new row violates row-level security policy for table "todos"
403 INSUFFICIENT_PRIVILEGE permission denied for function todo_count
[] null

The script signs a test user up: on a project that requires email confirmation (the default), turn it off first, as in the Quickstart.

Trade-offs#

  • auth.role() differs by path. Through the SDK it returns aura_anon, aura_authenticated or aura_service_role; through aura db raw --as it returns anon or authenticated. Target roles with to aura_authenticated in a policy instead of testing auth.role().
  • The direct-connection role can become the administrator. proj_xxxxxxxx may SET ROLE tenant_<id>_admin, which bypasses RLS. Treat its password like the service_role key.
  • A table created by the administrator role over the direct connection is open to the public roles until you enable row level security on it, because the hook that enables RLS only runs for SQL sent through Aurabase.
  • No custom login role. A project cannot CREATE ROLE (permission denied to create role); use policies on aura_authenticated and claims such as auth.tenant() or auth.has_role() to model your own roles.