Authorization

This page describes how the server decides what an authenticated session may do: roles and membership, table and column privileges, column masks, row-level security (RLS), and the wire roles that gate non-SQL wires. For the operator summary see Security, section 2.

Sessions

After login, a wire builds an AuthenticatedSession with resolve_from_catalog (source: src/auth/session.rs, AuthenticatedSession::resolve_from_catalog).

  • A user found in the catalog gets its oid, superuser flag and bypassrls flag (source: src/auth/session.rs).
  • Effective roles (the transitive closure of memberships) are resolved at login, and only when the role has rolinherit. Membership changes therefore apply to new sessions, not to open ones (source: src/auth/session.rs; src/catalog/repository.rs, get_effective_roles).
  • get_effective_roles itself ignores INHERIT on intermediate roles; the INHERIT check is only on the login role (source: src/catalog/repository.rs, get_effective_roles).
  • A user name not in the catalog becomes a superuser session. The code comment calls this historical behaviour. It affects any login that was accepted without a catalog role, such as Redis requirepass, Memcached file users, or MongoDB env-seeded users (source: src/auth/session.rs, resolve_from_catalog).
  • can_bypass_rls is true for a superuser or a bypassrls role; acting_role is the role set by SET ROLE, else the login role (source: src/auth/session.rs).

Not verified: no test was found that asserts the superuser fallback for a name outside the catalog.

A query engine with no session attached runs with no privilege checks at all (see "The privilege check" below). The SQLite Hrana wire gives an anonymous loopback stream an engine with no session (source: src/protocol/sqlite/hrana.rs).

Roles and membership

Statement Rule Source
CREATE ROLE, ALTER ROLE, DROP ROLE Superuser roles may be changed only by a superuser. A user may change its own password and nothing else about itself. Otherwise CREATEROLE is needed. Changing SUPERUSER, BYPASSRLS or REPLICATION needs superuser; changing CREATEDB needs CREATEDB. A user cannot drop itself src/query/engine.rs, check_role_ddl_privilege; test role_ddl_requires_privileges
GRANT role TO role, REVOKE role FROM role A non-superuser needs CREATEROLE and the target must not be a superuser. Membership cycles are refused src/query/engine.rs, execute_role_membership; test granted_role_membership_carries_table_privileges
SET ROLE, RESET ROLE The session must be a member of the target. SET LOCAL ROLE is undone at transaction end. session_user stays the login role src/query/engine.rs, handle_set_role, reset_role; test test_set_local_role_applies_rls_and_privileges
ALTER ROLE ... SET, RESET Accepted test alter_role_set_and_reset_are_accepted
ALTER ROLE ... REPLICATION, BYPASSRLS Accepted test alter_role_accepts_replication_and_bypassrls_options

Bootstrap creates pg_read_all_data, pg_write_all_data, pg_database_owner, pg_signal_backend and pg_monitor (source: src/catalog/bootstrap.rs, run). Not verified: no code was found that gives these roles special privileges; they behave as ordinary roles unless granted something.

Privilege model

Privileges are PostgreSQL-style ACL bitmaps (source: src/auth/acl.rs, AclMode).

Privilege ACL character
INSERT a
SELECT r
UPDATE w
DELETE d
TRUNCATE D
REFERENCES x
TRIGGER t
EXECUTE X
USAGE U
CREATE C
CONNECT c
TEMPORARY T

(source: src/auth/acl.rs, AclMode; test acl_mode_chars in tests/security_auth.rs)

ALL on a table means TABLE_PRIVILEGES: SELECT, INSERT, UPDATE, DELETE, TRUNCATE, REFERENCES and TRIGGER (source: src/auth/acl.rs, TABLE_PRIVILEGES).

A relation's security record is RelationSecurity { owner, acl }. owned_by creates one for a new owner; unmanaged grants everything to PUBLIC (source: src/auth/acl.rs, RelationSecurity).

Where the records live:

Key Content
__catalog__:relacl:{name} Owner and ACL of one relation (relation_security_key)
__catalog__:colacl:{name} Column ACLs of one relation
__catalog__:dbacl Database-wide ACL (DATABASE_ACL_KEY)
__catalog__:globalacl Cluster-wide ACL in global storage (GLOBAL_ACL_KEY)
__catalog__:masked:{table}:{col} Column mask flag

(source: src/query/engine.rs)

GRANT and REVOKE

execute_grant_revoke handles privilege statements (source: src/query/engine.rs, execute_grant_revoke).

Object Behaviour
Table, view Stored and enforced
Columns of a table Stored and enforced
ALL TABLES IN SCHEMA Expanded to each table
MySQL db.* grants Superuser only
Database, schema, function, sequence, other objects Accepted and ignored. A REVOKE on them logs a warning that it is not enforced

(source: src/query/engine.rs, execute_grant_revoke)

  • A grant to PUBLIC is stored with grantee None (source: src/query/engine.rs).
  • A non-owner may grant only privileges it holds WITH GRANT OPTION (source: src/query/engine.rs).
  • A table-level REVOKE also removes the column grants for that privilege (source: src/query/engine.rs).
  • Database ownership and CREATE DATABASE, DROP DATABASE, ALTER DATABASE ... OWNER are checked by require_database_owner (source: src/query/engine.rs; tests database_create_drop_and_owner_need_privileges, database_owner_is_recorded_and_reported).

The privilege check

flowchart TD A["execute_plan"] --> B{"Session attached and not superuser?"} B -- no --> X["No checks"] B -- yes --> C["required_access(plan)"] C --> D{"Each relation: security record exists?"} D -- no --> E{"Wire-internal prefix?"} E -- no --> OK["Allowed"] E -- yes --> DENY["PermissionDenied 42501"] D -- yes --> F{"Owner, ACL, database ACL or global ACL allows?"} F -- no --> DENY F -- yes --> G{"column_grants_allow?"} G -- no --> DENY G -- yes --> OK

(source: src/query/engine.rs, authorize_plan, privilege_roles, column_grants_allow)

  • authorize_plan runs at the entry of every execute_plan, before ownership changes and before execution (source: src/query/engine.rs, authorize_plan).
  • privilege_roles returns no roles for a missing session or a superuser, and no roles means no checks (source: src/query/engine.rs, privilege_roles).
  • required_access walks the whole plan, including subqueries, CTEs, views and set operations, and returns pairs of relation and access kind: Select, Insert, Update, Delete, References or Owner (source: src/plan/privileges.rs, required_access, RelationAccess; test table_privileges_cover_subqueries_ctes_views_and_set_ops).
  • A relation with no security record is open to everyone. The exception is a relation whose name starts with a wire-internal prefix (_couchdb_, _couch_, _es_, _s3_, _sr_, _dynamo_, _memcached_, _gremlin_, _prometheus_, _kafka_, _redis_, _influx_, _influxdb_, _mongo_, _mongodb_): those are closed to ordinary roles unless SYNERGYDB_UNOWNED_RELATIONS=open (source: src/query/engine.rs, is_wire_internal_relation; test wire_internal_tables_are_closed_to_ordinary_roles).
  • The owner passes. Otherwise the ACL, the database ACL or the global ACL must grant the access to one of the session's roles. Owner-only operations need a wide CREATE grant (source: src/query/engine.rs, authorize_plan; test table_privileges_follow_ownership_and_grants).
  • A refusal is PermissionDenied, SQLSTATE 42501 (source: src/query/engine.rs).
  • The creator of a relation becomes its owner; ALTER ... OWNER TO transfers it; DROP and RENAME update the record (source: src/query/engine.rs, ownership_effect, apply_ownership_effect; test owner_to_transfers_relation_ownership).
  • Column grants are checked against the columns the plan uses (source: src/plan/privileges.rs, column_usage; test column_level_select_grants_are_enforced).

Column masks

A column can be marked masked. For a non-superuser, apply_column_masks wraps the column in mask_full, which returns *** for a non-NULL value and keeps NULL (source: src/query/engine.rs, apply_column_masks, mask_full).

Query shape Masked?
Bare column, or alias of a column, from one table through Filter, Sort, Limit, Distinct Yes
SELECT * Yes; the star is expanded first (test test_column_mask_applies_to_select_star)
An expression over the column, such as upper(col) No
Joins, aggregates, subqueries No: find_underlying_table follows only a Scan through Filter, Sort, Limit and Distinct

(source: src/query/engine.rs, apply_column_masks, find_underlying_table)

Not verified: no test covers the unmasked shapes in the last two rows; the conclusion is from reading find_underlying_table.

Row-level security

RLS is applied by rewriting the plan in src/plan/rls_injector.rs (source: src/plan/rls_injector.rs).

Which policies apply

get_policies_for_table returns, per table and command (source: src/plan/rls_injector.rs, get_policies_for_table):

Result Meaning
None RLS is off for the table; no filter
Empty list RLS is on with no matching policy; filter is FALSE (default deny)
Policies Combined as below
  • Superusers and bypassrls roles skip RLS (source: src/plan/rls_injector.rs, inject).
  • The table owner is exempt unless the table has FORCE ROW LEVEL SECURITY (relforcerowsecurity) (source: src/plan/rls_injector.rs; test test_owner_is_exempt_unless_forced).
  • Permissive policies are OR'd together; restrictive policies are AND'd on top. If there is no permissive policy the result is FALSE (source: src/plan/rls_injector.rs, combine_policies_with).
  • WITH CHECK falls back to USING when absent (source: src/plan/rls_injector.rs; tests test_insert_with_check_rejects_and_allows, test_insert_default_deny_without_insert_policy, test_update_with_check_blocks_partition_escape).
  • A policy expression that cannot be parsed at query time becomes FALSE (source: src/plan/rls_injector.rs).
  • CREATE POLICY rejects expression shapes that policy_expr_to_sql cannot render. Supported: columns, literals, function calls, comparison, arithmetic, AND/OR, LIKE/ILIKE, concatenation, bitwise operators, unary operators, IN lists, BETWEEN, casts, CASE, EXISTS subqueries (source: src/query/engine.rs, policy_expr_to_sql).
  • Policies are scoped to their database (source: src/plan/rls_injector.rs; test test_policies_stay_in_their_own_database).

Session values

substitute_session_values replaces current_user, session_user and current_role with the session's user name, and current_setting(...) with the session setting or NULL, including inside EXISTS subqueries (source: src/plan/rls_injector.rs, substitute_session_values; tests test_and_current_user_policy_filters_rows, test_current_setting_cast_policy_filters_rows, test_set_config_with_bound_param_feeds_rls, test_exists_subquery_policy_scopes_rows_to_tenant).

current_role is replaced with the session user name, not the role set by SET ROLE (source: src/plan/rls_injector.rs, substitute_session_values).

Where filters are injected

inject rewrites these plan nodes: Scan, Filter, Project, Join, LateralJoin, Aggregate, Sort, Limit, LimitWithTies, LockRows, Distinct, Alias, RecursiveCTE, Union, Intersect, Except, InsertSelect (the SELECT source), Insert, Update, Delete. Any other plan node passes through unchanged (source: src/plan/rls_injector.rs, inject; tests test_lateral_join_respects_rls_policy, test_injector_recurses_into_lateral_join, test_subqueries_and_set_operations_respect_policies).

For UPDATE ... FROM and DELETE ... USING, the extra tables in from and using are not rewritten, so their policies do not apply (source: src/plan/rls_injector.rs, inject). Not verified: no test covers this gap.

Caching and change propagation

Policies are cached for 1 s (POLICY_CACHE_TTL) and invalidated by a global POLICY_EPOCH counter; the cache key includes the namespaces (source: src/plan/rls_injector.rs; test test_policy_change_reaches_other_connections).

Other RLS tests: test_in_list_policy_filters_rows, test_pg_dump_rls_statements_apply, test_prefix_rls_eval_placeholder_hid_all_rows (source: src/plan/rls_injector.rs).

Wire roles for non-SQL wires

Non-SQL wires have no per-table privileges. Instead, verify_wire_credential(wire, ...) checks that the login role may use that wire at all (source: src/auth/wire_auth.rs, wire_access_allowed).

Case Result
No storage Allowed
Role not in the catalog (a wire's own credential) Allowed
Catalog read error Refused
Superuser Allowed
Effective roles include synergy_wire_<wire> or synergy_wire_all Allowed
Otherwise Refused

(source: src/auth/wire_auth.rs, wire_access_allowed, WIRE_ROLE_PREFIX, WIRE_ROLE_ALL)

SYNERGYDB_WIRE_ACCESS set to open, off or any turns the check off (source: src/auth/wire_auth.rs, wire_access_enforced).

Wires that apply the wire-role check: Prometheus, CouchDB, Redis, Gremlin, InfluxDB, Kafka, SQLite, Memcached, Elasticsearch, and MongoDB for catalog credentials (source: callers of verify_wire_credential and wire_access_allowed under src/protocol/). Not verified: no test was found that logs in without a wire role and expects a refusal.

Native permission models

Some wires also have their own permission model on top of the login:

Wire Model Source
Redis Redis 8 ACL: per-user commands, key patterns, channels src/protocol/redis/commands/acl.rs; tests acl_user_command_key_and_channel_rules, acl_refusal_inside_multi_aborts_exec, acl_script_refusal_matches_redis_text
MongoDB Users and built-in roles per database; a user with no MongoDB record has full access src/protocol/mongodb/users.rs
Cassandra Roles and permissions under __cqlrole__:<name> src/protocol/cassandra/roles.rs

Per-wire details are in the wire pages.