postgresai 0.18.0-dev.0 → 0.18.0-dev.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,14 +7,10 @@ grant connect on database {{DB_IDENT}} to {{ROLE_IDENT}};
7
7
  grant pg_monitor to {{ROLE_IDENT}};
8
8
  grant select on pg_catalog.pg_index to {{ROLE_IDENT}};
9
9
 
10
- -- Required for cluster-wide pg_stat_statements collection.
11
- -- pg_stat_statements is backed by shared memory and holds entries for every database
12
- -- in the cluster, but a role without pg_read_all_stats sees other users' rows with
13
- -- queryid and query text redacted to NULL -- which collapses the per-queryid grouping
14
- -- in the pg_stat_statements metric. pg_monitor already implies pg_read_all_stats;
15
- -- granting it explicitly documents the dependency and keeps the metric working if the
16
- -- pg_monitor grant above is ever narrowed. Idempotent.
17
- grant pg_read_all_stats to {{ROLE_IDENT}};
10
+ -- Cluster-wide pg_stat_statements collection needs pg_read_all_stats (without it, other
11
+ -- users' rows have queryid and query text redacted to NULL). pg_monitor includes it, so it
12
+ -- is not granted directly: on PostgreSQL 16+ that needs ADMIN OPTION on pg_read_all_stats,
13
+ -- which Supabase's `postgres` role does not have (it has ADMIN OPTION on pg_monitor only).
18
14
 
19
15
  -- Create postgres_ai schema for our objects
20
16
  -- Using IF NOT EXISTS for idempotency - prepare-db can be run multiple times
@@ -59,27 +55,43 @@ begin
59
55
  end if;
60
56
  end $$;
61
57
 
62
- -- [SEARCH_PATH_BLOCK_START] Keep search_path predictable; postgres_ai first so our objects are found.
63
- -- Dynamically include the pg_stat_statements extension schema if it's in a non-standard location.
58
+ -- [SEARCH_PATH_BLOCK_START] Resolve built-ins from pg_catalog first; keep pg_temp last.
59
+ -- Do not include the role-named schema: it may be writable by that role.
60
+ -- public stays for pg_stat_statements. Residual risk: a function in public with
61
+ -- an exact-type match (e.g. public.quote_ident(name)) still beats pg_catalog.
62
+ -- The path for this database includes its pg_stat_statements schema if it is in a non-standard location.
64
63
  do $$
65
64
  declare
66
65
  ext_schema text;
67
- sp text;
66
+ role_path constant text := 'pg_catalog, postgres_ai, public, pg_temp';
67
+ db_path text := role_path;
68
68
  begin
69
+ -- This block runs as the admin: resolve its operators and functions from pg_catalog only
70
+ -- (CVE-2018-1058). The pin is inside the block: a DO block always runs in a transaction,
71
+ -- also in psql autocommit.
72
+ set local search_path = pg_catalog, pg_temp;
69
73
  -- Detect pg_stat_statements extension schema
70
74
  select n.nspname into ext_schema
71
75
  from pg_extension e
72
76
  join pg_namespace n on e.extnamespace = n.oid
73
77
  where e.extname = 'pg_stat_statements';
74
78
 
75
- -- Build search_path: include extension schema if in non-standard location
76
79
  if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
77
- sp := 'postgres_ai, ' || quote_ident(ext_schema) || ', "$user", public, pg_catalog';
78
- else
79
- sp := 'postgres_ai, "$user", public, pg_catalog';
80
+ db_path := 'pg_catalog, postgres_ai, ' || quote_ident(ext_schema) || ', public, pg_temp';
80
81
  end if;
81
82
 
82
- execute format('alter user {{ROLE_IDENT}} set search_path = %s', sp);
83
+ -- Role-wide value: for databases that prepare-db did not run in. All databases share
84
+ -- this one row, so write it only when it holds another value.
85
+ if not exists (
86
+ select from pg_catalog.pg_db_role_setting
87
+ where setrole = (select oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}})
88
+ and setdatabase = 0
89
+ and ('search_path=' || role_path) = any (setconfig)
90
+ ) then
91
+ execute format('alter user {{ROLE_IDENT}} set search_path = %s', role_path);
92
+ end if;
93
+ -- This database's value: other databases keep their own pg_stat_statements schema.
94
+ execute format('alter user {{ROLE_IDENT}} in database %I set search_path = %s', pg_catalog.current_database(), db_path);
83
95
  end $$;
84
96
  -- [SEARCH_PATH_BLOCK_END]
85
97
 
@@ -1,5 +1,8 @@
1
1
  -- Drop helper functions created by prepare-db (template-filled by cli/lib/init.ts)
2
2
  -- Run before dropping the postgres_ai schema.
3
3
 
4
+ -- Objects in other schemas (for example, a planted public.format) must not run as the admin.
5
+ set local search_path = pg_catalog, pg_temp;
6
+
4
7
  drop function if exists postgres_ai.explain_generic(text, text, text);
5
8
  drop function if exists postgres_ai.table_describe(text);
@@ -1,29 +1,85 @@
1
1
  -- Revoke permissions and drop objects created by prepare-db (template-filled by cli/lib/init.ts)
2
2
 
3
+ -- Objects in other schemas (for example, a planted public.=) must not run as the admin.
4
+ set local search_path = pg_catalog, pg_temp;
5
+ -- The CLI reads the "unprepare-db:" WARNINGs. A higher setting would hide them.
6
+ set local client_min_messages = warning;
7
+
3
8
  -- Drop the postgres_ai.pg_statistic view
4
9
  drop view if exists postgres_ai.pg_statistic;
5
10
 
6
- -- Drop the postgres_ai schema (CASCADE to handle any remaining objects)
7
- drop schema if exists postgres_ai cascade;
8
-
9
- -- Revoke permissions from the monitoring role
10
- -- Use a DO block to handle the case where the role doesn't exist
11
- do $$ begin
12
- revoke pg_monitor from {{ROLE_IDENT}};
13
- exception when undefined_object then
14
- null; -- Role doesn't exist, nothing to revoke
15
- end $$;
16
-
17
- do $$ begin
18
- revoke select on pg_catalog.pg_index from {{ROLE_IDENT}};
19
- exception when undefined_object then
20
- null; -- Role doesn't exist
21
- end $$;
11
+ -- Revoke only the grants that the role holds. The postgres_ai schema stays
12
+ -- because it may contain customer objects. The pg_read_all_stats membership
13
+ -- comes from older prepare-db versions. The functions come from optional
14
+ -- permissions (04, 05). A non-superuser admin cannot revoke a grant that a
15
+ -- superuser made. Pass 1 revokes. Pass 2 sends one "grant left" WARNING for
16
+ -- each grant that stays. The CLI shows these WARNINGs and exits 1.
17
+ do $$
18
+ declare
19
+ role_oid oid;
20
+ stmt text;
21
+ begin
22
+ select oid into role_oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}};
23
+ if role_oid is null then
24
+ return; -- The monitoring role was already removed.
25
+ end if;
22
26
 
23
- do $$ begin
24
- revoke connect on database {{DB_IDENT}} from {{ROLE_IDENT}};
25
- exception when undefined_object then
26
- null; -- Role doesn't exist
27
+ for pass in 1..2 loop
28
+ for stmt in
29
+ -- PG 16+ keeps one row for each grantor. A revoke without GRANTED BY
30
+ -- removes only the row of the current user. PG < 16 can keep the oid of
31
+ -- a dropped grantor; then the statement has no GRANTED BY.
32
+ select pg_catalog.format('revoke %s from %I', m.roleid::regrole, {{ROLE_LITERAL}})
33
+ || coalesce(' granted by ' || pg_catalog.quote_ident(g.rolname), '')
34
+ from pg_catalog.pg_auth_members m
35
+ left join pg_catalog.pg_roles g on g.oid = m.grantor
36
+ where m.member = role_oid
37
+ and m.roleid in ('pg_monitor'::regrole, 'pg_read_all_stats'::regrole)
38
+ union
39
+ select pg_catalog.format('revoke select on pg_catalog.pg_index from %I', {{ROLE_LITERAL}})
40
+ from pg_catalog.pg_class c
41
+ where c.oid operator(pg_catalog.=) pg_catalog.to_regclass('pg_catalog.pg_index')::oid
42
+ and exists (
43
+ select from pg_catalog.aclexplode(c.relacl) a
44
+ where a.grantee = role_oid and a.privilege_type = 'SELECT')
45
+ union
46
+ select pg_catalog.format('revoke connect on database %I from %I', d.datname, {{ROLE_LITERAL}})
47
+ from pg_catalog.pg_database d
48
+ where d.datname = pg_catalog.current_database()
49
+ and exists (
50
+ select from pg_catalog.aclexplode(d.datacl) a
51
+ where a.grantee = role_oid and a.privilege_type = 'CONNECT')
52
+ union
53
+ select pg_catalog.format('revoke usage on schema postgres_ai from %I', {{ROLE_LITERAL}})
54
+ from pg_catalog.pg_namespace n
55
+ where n.nspname = 'postgres_ai'
56
+ and exists (
57
+ select from pg_catalog.aclexplode(n.nspacl) a
58
+ where a.grantee = role_oid and a.privilege_type = 'USAGE')
59
+ union
60
+ select pg_catalog.format('revoke execute on function %s from %I', p.oid::regprocedure, {{ROLE_LITERAL}})
61
+ from pg_catalog.pg_proc p
62
+ where p.oid in (
63
+ pg_catalog.to_regprocedure('rds_tools.pg_ls_multixactdir()'),
64
+ pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text)'),
65
+ pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text, boolean)'),
66
+ pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text)'),
67
+ pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text, boolean, boolean)'))
68
+ and exists (
69
+ select from pg_catalog.aclexplode(p.proacl) a
70
+ where a.grantee = role_oid and a.privilege_type = 'EXECUTE')
71
+ loop
72
+ if pass = 2 then
73
+ raise warning 'unprepare-db: grant left; a superuser can run: %', stmt;
74
+ else
75
+ begin
76
+ execute stmt;
77
+ exception when insufficient_privilege then
78
+ raise warning 'unprepare-db: % failed: %', stmt, sqlerrm;
79
+ end;
80
+ end if;
81
+ end loop;
82
+ end loop;
27
83
  end $$;
28
84
 
29
85
  -- Note: USAGE on public is typically granted by default; we don't revoke it
@@ -1,6 +1,9 @@
1
1
  -- Drop the monitoring role created by prepare-db (template-filled by cli/lib/init.ts)
2
2
  -- This must run after revoking all permissions from the role.
3
3
 
4
+ -- Objects in other schemas (for example, a planted public.format) must not run as the admin.
5
+ set local search_path = pg_catalog, pg_temp;
6
+
4
7
  -- Use a DO block to handle the case where the role doesn't exist
5
8
  do $$ begin
6
9
  -- Reassign owned objects to current user before dropping
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "postgresai",
3
- "version": "0.18.0-dev.0",
3
+ "version": "0.18.0-dev.10",
4
4
  "description": "postgres_ai CLI",
5
5
  "license": "Apache-2.0",
6
6
  "private": false,
@@ -7,14 +7,10 @@ grant connect on database {{DB_IDENT}} to {{ROLE_IDENT}};
7
7
  grant pg_monitor to {{ROLE_IDENT}};
8
8
  grant select on pg_catalog.pg_index to {{ROLE_IDENT}};
9
9
 
10
- -- Required for cluster-wide pg_stat_statements collection.
11
- -- pg_stat_statements is backed by shared memory and holds entries for every database
12
- -- in the cluster, but a role without pg_read_all_stats sees other users' rows with
13
- -- queryid and query text redacted to NULL -- which collapses the per-queryid grouping
14
- -- in the pg_stat_statements metric. pg_monitor already implies pg_read_all_stats;
15
- -- granting it explicitly documents the dependency and keeps the metric working if the
16
- -- pg_monitor grant above is ever narrowed. Idempotent.
17
- grant pg_read_all_stats to {{ROLE_IDENT}};
10
+ -- Cluster-wide pg_stat_statements collection needs pg_read_all_stats (without it, other
11
+ -- users' rows have queryid and query text redacted to NULL). pg_monitor includes it, so it
12
+ -- is not granted directly: on PostgreSQL 16+ that needs ADMIN OPTION on pg_read_all_stats,
13
+ -- which Supabase's `postgres` role does not have (it has ADMIN OPTION on pg_monitor only).
18
14
 
19
15
  -- Create postgres_ai schema for our objects
20
16
  -- Using IF NOT EXISTS for idempotency - prepare-db can be run multiple times
@@ -59,27 +55,43 @@ begin
59
55
  end if;
60
56
  end $$;
61
57
 
62
- -- [SEARCH_PATH_BLOCK_START] Keep search_path predictable; postgres_ai first so our objects are found.
63
- -- Dynamically include the pg_stat_statements extension schema if it's in a non-standard location.
58
+ -- [SEARCH_PATH_BLOCK_START] Resolve built-ins from pg_catalog first; keep pg_temp last.
59
+ -- Do not include the role-named schema: it may be writable by that role.
60
+ -- public stays for pg_stat_statements. Residual risk: a function in public with
61
+ -- an exact-type match (e.g. public.quote_ident(name)) still beats pg_catalog.
62
+ -- The path for this database includes its pg_stat_statements schema if it is in a non-standard location.
64
63
  do $$
65
64
  declare
66
65
  ext_schema text;
67
- sp text;
66
+ role_path constant text := 'pg_catalog, postgres_ai, public, pg_temp';
67
+ db_path text := role_path;
68
68
  begin
69
+ -- This block runs as the admin: resolve its operators and functions from pg_catalog only
70
+ -- (CVE-2018-1058). The pin is inside the block: a DO block always runs in a transaction,
71
+ -- also in psql autocommit.
72
+ set local search_path = pg_catalog, pg_temp;
69
73
  -- Detect pg_stat_statements extension schema
70
74
  select n.nspname into ext_schema
71
75
  from pg_extension e
72
76
  join pg_namespace n on e.extnamespace = n.oid
73
77
  where e.extname = 'pg_stat_statements';
74
78
 
75
- -- Build search_path: include extension schema if in non-standard location
76
79
  if ext_schema is not null and ext_schema not in ('pg_catalog', 'public') then
77
- sp := 'postgres_ai, ' || quote_ident(ext_schema) || ', "$user", public, pg_catalog';
78
- else
79
- sp := 'postgres_ai, "$user", public, pg_catalog';
80
+ db_path := 'pg_catalog, postgres_ai, ' || quote_ident(ext_schema) || ', public, pg_temp';
80
81
  end if;
81
82
 
82
- execute format('alter user {{ROLE_IDENT}} set search_path = %s', sp);
83
+ -- Role-wide value: for databases that prepare-db did not run in. All databases share
84
+ -- this one row, so write it only when it holds another value.
85
+ if not exists (
86
+ select from pg_catalog.pg_db_role_setting
87
+ where setrole = (select oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}})
88
+ and setdatabase = 0
89
+ and ('search_path=' || role_path) = any (setconfig)
90
+ ) then
91
+ execute format('alter user {{ROLE_IDENT}} set search_path = %s', role_path);
92
+ end if;
93
+ -- This database's value: other databases keep their own pg_stat_statements schema.
94
+ execute format('alter user {{ROLE_IDENT}} in database %I set search_path = %s', pg_catalog.current_database(), db_path);
83
95
  end $$;
84
96
  -- [SEARCH_PATH_BLOCK_END]
85
97
 
@@ -1,5 +1,8 @@
1
1
  -- Drop helper functions created by prepare-db (template-filled by cli/lib/init.ts)
2
2
  -- Run before dropping the postgres_ai schema.
3
3
 
4
+ -- Objects in other schemas (for example, a planted public.format) must not run as the admin.
5
+ set local search_path = pg_catalog, pg_temp;
6
+
4
7
  drop function if exists postgres_ai.explain_generic(text, text, text);
5
8
  drop function if exists postgres_ai.table_describe(text);
@@ -1,29 +1,85 @@
1
1
  -- Revoke permissions and drop objects created by prepare-db (template-filled by cli/lib/init.ts)
2
2
 
3
+ -- Objects in other schemas (for example, a planted public.=) must not run as the admin.
4
+ set local search_path = pg_catalog, pg_temp;
5
+ -- The CLI reads the "unprepare-db:" WARNINGs. A higher setting would hide them.
6
+ set local client_min_messages = warning;
7
+
3
8
  -- Drop the postgres_ai.pg_statistic view
4
9
  drop view if exists postgres_ai.pg_statistic;
5
10
 
6
- -- Drop the postgres_ai schema (CASCADE to handle any remaining objects)
7
- drop schema if exists postgres_ai cascade;
8
-
9
- -- Revoke permissions from the monitoring role
10
- -- Use a DO block to handle the case where the role doesn't exist
11
- do $$ begin
12
- revoke pg_monitor from {{ROLE_IDENT}};
13
- exception when undefined_object then
14
- null; -- Role doesn't exist, nothing to revoke
15
- end $$;
16
-
17
- do $$ begin
18
- revoke select on pg_catalog.pg_index from {{ROLE_IDENT}};
19
- exception when undefined_object then
20
- null; -- Role doesn't exist
21
- end $$;
11
+ -- Revoke only the grants that the role holds. The postgres_ai schema stays
12
+ -- because it may contain customer objects. The pg_read_all_stats membership
13
+ -- comes from older prepare-db versions. The functions come from optional
14
+ -- permissions (04, 05). A non-superuser admin cannot revoke a grant that a
15
+ -- superuser made. Pass 1 revokes. Pass 2 sends one "grant left" WARNING for
16
+ -- each grant that stays. The CLI shows these WARNINGs and exits 1.
17
+ do $$
18
+ declare
19
+ role_oid oid;
20
+ stmt text;
21
+ begin
22
+ select oid into role_oid from pg_catalog.pg_roles where rolname = {{ROLE_LITERAL}};
23
+ if role_oid is null then
24
+ return; -- The monitoring role was already removed.
25
+ end if;
22
26
 
23
- do $$ begin
24
- revoke connect on database {{DB_IDENT}} from {{ROLE_IDENT}};
25
- exception when undefined_object then
26
- null; -- Role doesn't exist
27
+ for pass in 1..2 loop
28
+ for stmt in
29
+ -- PG 16+ keeps one row for each grantor. A revoke without GRANTED BY
30
+ -- removes only the row of the current user. PG < 16 can keep the oid of
31
+ -- a dropped grantor; then the statement has no GRANTED BY.
32
+ select pg_catalog.format('revoke %s from %I', m.roleid::regrole, {{ROLE_LITERAL}})
33
+ || coalesce(' granted by ' || pg_catalog.quote_ident(g.rolname), '')
34
+ from pg_catalog.pg_auth_members m
35
+ left join pg_catalog.pg_roles g on g.oid = m.grantor
36
+ where m.member = role_oid
37
+ and m.roleid in ('pg_monitor'::regrole, 'pg_read_all_stats'::regrole)
38
+ union
39
+ select pg_catalog.format('revoke select on pg_catalog.pg_index from %I', {{ROLE_LITERAL}})
40
+ from pg_catalog.pg_class c
41
+ where c.oid operator(pg_catalog.=) pg_catalog.to_regclass('pg_catalog.pg_index')::oid
42
+ and exists (
43
+ select from pg_catalog.aclexplode(c.relacl) a
44
+ where a.grantee = role_oid and a.privilege_type = 'SELECT')
45
+ union
46
+ select pg_catalog.format('revoke connect on database %I from %I', d.datname, {{ROLE_LITERAL}})
47
+ from pg_catalog.pg_database d
48
+ where d.datname = pg_catalog.current_database()
49
+ and exists (
50
+ select from pg_catalog.aclexplode(d.datacl) a
51
+ where a.grantee = role_oid and a.privilege_type = 'CONNECT')
52
+ union
53
+ select pg_catalog.format('revoke usage on schema postgres_ai from %I', {{ROLE_LITERAL}})
54
+ from pg_catalog.pg_namespace n
55
+ where n.nspname = 'postgres_ai'
56
+ and exists (
57
+ select from pg_catalog.aclexplode(n.nspacl) a
58
+ where a.grantee = role_oid and a.privilege_type = 'USAGE')
59
+ union
60
+ select pg_catalog.format('revoke execute on function %s from %I', p.oid::regprocedure, {{ROLE_LITERAL}})
61
+ from pg_catalog.pg_proc p
62
+ where p.oid in (
63
+ pg_catalog.to_regprocedure('rds_tools.pg_ls_multixactdir()'),
64
+ pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text)'),
65
+ pg_catalog.to_regprocedure('pg_catalog.pg_stat_file(text, boolean)'),
66
+ pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text)'),
67
+ pg_catalog.to_regprocedure('pg_catalog.pg_ls_dir(text, boolean, boolean)'))
68
+ and exists (
69
+ select from pg_catalog.aclexplode(p.proacl) a
70
+ where a.grantee = role_oid and a.privilege_type = 'EXECUTE')
71
+ loop
72
+ if pass = 2 then
73
+ raise warning 'unprepare-db: grant left; a superuser can run: %', stmt;
74
+ else
75
+ begin
76
+ execute stmt;
77
+ exception when insufficient_privilege then
78
+ raise warning 'unprepare-db: % failed: %', stmt, sqlerrm;
79
+ end;
80
+ end if;
81
+ end loop;
82
+ end loop;
27
83
  end $$;
28
84
 
29
85
  -- Note: USAGE on public is typically granted by default; we don't revoke it
@@ -1,6 +1,9 @@
1
1
  -- Drop the monitoring role created by prepare-db (template-filled by cli/lib/init.ts)
2
2
  -- This must run after revoking all permissions from the role.
3
3
 
4
+ -- Objects in other schemas (for example, a planted public.format) must not run as the admin.
5
+ set local search_path = pg_catalog, pg_temp;
6
+
4
7
  -- Use a DO block to handle the case where the role doesn't exist
5
8
  do $$ begin
6
9
  -- Reassign owned objects to current user before dropping