Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Supabase 10 min read How-to hardMaster

Protecting reserved roles with PostgreSQL Hooks

Supabase grants customers the CREATEROLE privilege while preventing them from altering or dropping its internal service roles (like supabase_storage_admin) via a C extension, SupaUtils, that hooks PostgreSQL's ProcessUtility_hook. The hook chains any previously installed hook, reads a reloadable GUC parameter supautils.reserved_roles, and inspects AlterRoleStmt and DropRoleStmt nodes to raise an insufficient-privilege error when a reserved role is targeted. The walkthrough covers the PGXS Makefile, _PG_init registration, DefineCustomStringVariable, and testing via shared_preload_libraries, mirroring how Amazon RDS's rdsutils protects rds-prefixed roles.

Related posts

  • PostgreSQL Event Triggers without superuser access

    Supabase hardMaster

    Supabase's supautils extension lets non-superusers create Postgres Event Triggers by intercepting `create event trigger` from a "privileged role" via ProcessUtility_hook, temporarily elevating to superuser for creation, then assigning ownership back to the privileged role. Because event triggers fire for every role with the invoking role's privileges, this alone would allow privilege escalation, such as a trigger that runs `alter role ... superuser` when a superuser executes DDL. To prevent that, the extension uses fmgr_hook to replace user event trigger functions with a no-op (`version()`) whenever a superuser or reserved role is executing. The feature is live for new Supabase projects, and upstream patches proposing more restricted user event triggers in Postgres core are under discussion.

  • Postgres Roles and Privileges

    Supabase mediumAdept

    Postgres access control rests on objects, privileges, roles and ownership: an object's owner holds all privileges on it and is the one who can grant them, so even an admin-like role such as Supabase's non-superuser postgres gets permission denied when granting on a table it neither owns nor holds privileges on. A psql walkthrough with junior_dev and senior_dev roles shows granting CREATE on a schema, reading ACLs with \dn+, \dp and \ddp, delegating with WITH GRANT OPTION, and using ALTER DEFAULT PRIVILEGES so tables created later are automatically shared.

  • Supabase Auth: Identity Linking, Hooks, and HaveIBeenPwned integration

    Supabase mediumAdept

    Supabase Auth adds four features: manual Identity Linking via linkIdentity()/unlinkIdentity() for OAuth identities, session controls (time-boxed sessions, inactivity timeouts, single session per user on Pro and above), leaked password protection using the HaveIBeenPwned Pwned Passwords API with configurable password requirements, and Auth Hooks. Auth Hooks are Postgres functions that run synchronously at points such as access token generation, MFA verification and password verification, so they can add custom JWT claims or enforce custom security rules. Webhook-based hooks via Standard Webhooks are planned for Q1 2024. The GoTrue fork is also being renamed to Auth, with no breaking changes.

  • Postgres Roles: What to Know Before You Begin

    Neon mediumAdept

    Postgres has no separate users and groups, only roles: a role with LOGIN behaves like a user, and a role without it acts as a group whose privileges pass to its members through inheritance. Access depends on privileges at every level of the hierarchy. Reading a table requires CONNECT on the database, USAGE on the schema and SELECT on the table, so revoking schema USAGE silently blocks table-level grants. Common gotchas include the public schema defaults, SECURITY DEFINER functions, search_path hijacking, implicit owner privileges and inconsistent attribute inheritance. Migrations with tangled role and object dependencies are the biggest pain point, so the post recommends mapping dependencies, writing custom scripts beyond pg_dump/pg_restore, testing with a rollback plan, and following least privilege.

  • Higher-level CRUD Abstraction for Postgres RLS

    Neon mediumAdept

    Postgres Row-Level Security requires separate USING and WITH CHECK policies per operation, which gets repetitive for CRUD apps. Neon's Drizzle ORM helper `crudPolicy` takes a role plus `read` and `modify` expressions (true, false, a SQL expression, or null to skip) and generates the select, insert, update and delete policies. A companion `authUid` helper ties pg_session_jwt's `auth.user_id()` to a table column. The post shows the raw SQL for a todos table next to the equivalent concise Drizzle schema. The functionality has since moved into the Neon Data API.

The quest for software engineering knowledge.

Look:
Layout: