-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add lint for securing queues over data apis
- Loading branch information
Showing
6 changed files
with
246 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
Level: ERROR | ||
|
||
### Rationale | ||
|
||
Queues exposed over Data APIs must be secured by Postgres permissions or row level security (RLS). Without this protection, anyone with a project's URL can manipulate queue data. That is a critically unsafe configuration. | ||
|
||
### How to Resolve | ||
|
||
To secure a queue, enable RLS on the queue's underlying table `pgmq.q_<queue_name>`: | ||
|
||
```sql | ||
alter table pgmq.q_<queue_name> enable row level security; | ||
``` | ||
|
||
Note that after enabling RLS you will not be able to access data in the queue over APIs until you create [row level security policies](https://supabase.com/docs/guides/auth/row-level-security) to control access. | ||
|
||
### Example | ||
|
||
Given a queue named `foo` and underlying table `pgmq.q_foo`: | ||
|
||
```sql | ||
create table pgmq.q_foo( | ||
msg_id bigint generated always as identity, | ||
read_ct int default 0 not null, | ||
enqueued_at timestamp with timezone default now() not null, | ||
vt timestamp with time zone not null, | ||
message jsonb | ||
); | ||
``` | ||
|
||
If Data APIs are enabled, and `anon` or `authenticated` have permissions on the table, any user with access to the project's URL and public API key will be able to manipulate messages in that Queue. To restrict access to users specified in row level security policies, enable RLS with: | ||
|
||
```sql | ||
alter table pgmq.q_foo enable row level security; | ||
``` | ||
|
||
If queuesa are not being accessed through data APIs, an alternative is to remove the `pgmq_public` schema from the [Exposed schemas in API settings](https://supabase.com/dashboard/project/_/settings/api). That change secures your project by making all queues inaccessible over APIs. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
create view lint."0019_insecure_queue_exposed_in_api" as | ||
|
||
select | ||
'insecure_queue_exposed_in_api' as name, | ||
'Insecure Queue Exposed in API' as title, | ||
'ERROR' as level, | ||
'EXTERNAL' as facing, | ||
array['SECURITY'] as categories, | ||
'Detects cases where an insecure Queue is exposed over Data APIs' as description, | ||
format( | ||
'Table \`%s.%s\` is public, but RLS has not been enabled.', | ||
n.nspname, | ||
c.relname | ||
) as detail, | ||
'https://supabase.com/docs/guides/database/database-linter?lint=0019_insecure_queue_exposed_in_api' as remediation, | ||
jsonb_build_object( | ||
'schema', n.nspname, | ||
'name', c.relname, | ||
'type', 'table' | ||
) as metadata, | ||
format( | ||
'rls_disabled_in_public_%s_%s', | ||
n.nspname, | ||
c.relname | ||
) as cache_key | ||
from | ||
pg_catalog.pg_class c | ||
join pg_catalog.pg_namespace n | ||
on c.relnamespace = n.oid | ||
where | ||
c.relkind in ('r', 'I') -- regular or partitioned tables | ||
and not c.relrowsecurity -- RLS is disabled | ||
and ( | ||
pg_catalog.has_table_privilege('anon', c.oid, 'SELECT') | ||
or pg_catalog.has_table_privilege('authenticated', c.oid, 'SELECT') | ||
) | ||
and n.nspname = 'pgmq' -- tables in the pgmq schema | ||
and c.relname like 'q_%' -- only queue tables | ||
-- Constant requirements | ||
and 'pgmq_public' = any(array(select trim(unnest(string_to_array(current_setting('pgrst.db_schemas', 't'), ','))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.