Db as source of truth for exposed schemas
Problem
- Schemas in
db-schemasare validated and if one drops postgREST errs: https://github.com/PostgREST/postgrest/issues/4364#issuecomment-4761900952 db-schemasanddb-extra-search-pathare another layer on top of postgresql.
The main features both config options gives us are:
Default schema
- first of
db-schemas
Exposed schemas
db-schemas
Implicit schemas
These are schemas that the API role has USAGE but are not exposed
db-extra-search-path
Solution
Option 1: Direct USAGE privilege
Default schema
- first of authenticator
search_path
Exposed schemas
- schemas where the API role has direct USAGE privilege
- Excludes USAGE granted through PUBLIC and memberships
- Can exclude
extensionsschema if USAGE is granted to PUBLIC or through a membership - Excludes pg_catalog, information_schema
Implicit schemas
- authenticator
search_path
Cons
Requirement to grant USAGE on schemas to all users separately is too much of an administrative burden and contrary to the goals of role system itself. https://github.com/PostgREST/postgrest/issues/4364#issuecomment-4796099405
Option 2: Direct and member USAGE privilege
Default schema
- first of authenticator
search_path
Exposed schemas
- schemas where the API role has direct USAGE privilege or USAGE through membership
- Excludes USAGE granted through PUBLIC
- Can exclude
extensionsschema if USAGE is granted to PUBLIC - Excludes pg_catalog, information_schema
Implicit schemas
- authenticator
search_path
Cons
Not flexible enough to easily restrict the set of exposed schemas. If extensions USAGE is not PUBLIC, then it will always be exposed. Another idea was mentioned:
Maybe grantor might be used for this purpose? Ie. expose schemas as above but take into account only USAGE grants granted by a specific grantor (configurable): GRANT USAGE ON SCHEMA api_schema TO api_role GRANTED BY api_manager; GRANT USAGE ON SCHEMA impl_schema TO api_role; https://github.com/PostgREST/postgrest/issues/4364#issuecomment-4802917338
But that would also defeat the purpose of this issue, since we'd have yet another config.
Option 3: USAGE privilege on the authenticator
From https://github.com/PostgREST/postgrest/issues/4364#issuecomment-4761900952
Default schema
- first of authenticator
search_path
Exposed schemas
- schemas where the
authenticatorhas USAGE privilege, direct or indirect- Excludes PUBLIC,
extensionsis not a problem sinceauthenticatordoesn't need to have USAGE privilege on it. - Excludes pg_catalog, information_schema
- Excludes PUBLIC,
Implicit schemas
- authenticator
search_path
Pros
- Leaves the API roles alone (existing or new) and they can have USAGE privileges in whichever way.
Cons
Historically we've never required authenticator to have any privilege, so we'd have to deal with the consequences of USAGE privilege:
If function privileges are not revoked from PUBLIC (default), then authenticator will be able to execute all functions. So /rpc is accessible for all.
- We have a guide to solve this, so maybe not so critical.
The danger of authenticator credentials being stolen and using it to access the db. I remember there was a discussion about this a long time ago. The main idea was that if authenticator creds were leaked, the damage caused would be much less compared to a traditional backend with usual postgres/superuser access. A db dump would not reveal any info. While you could impersonate the roles, if tables were protected with RLS filtered with application users' UUID, the attacker would have to guess UUIDs to extract some rows. So much more time consuming and during this an intrusion system could give a timely alert. Would this layer of defense weaken now that authenticator has USAGE on schemas? I don't think so, after all impersonating a role is easy and then the impersonated role has USAGE on the schemas. Also maybe this consequence doesn't matter at all and we shouldn't worry about it.
Any more come to mind?
Source: PostgREST/postgrest