Skip to content

Commit b7f1c22

Browse files
reshkerobot-cloud-aw
authored andcommitted
Never trust trusted and superuser fields from extension controlfile (#86)
For all in-core (contrib) extension, we already switch trusted options to false. For third-party extensions, simply never trust nothing. This makes API management of extensions consistent.
1 parent 65108c7 commit b7f1c22

7 files changed

Lines changed: 76 additions & 4 deletions

File tree

src/backend/commands/extension.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,11 +1213,17 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
12131213
* here so that the control flags are correctly associated with the right
12141214
* script(s) if they happen to be set in secondary control files.
12151215
*/
1216-
if (control->superuser && !superuser())
1216+
/* MDB does not trust control->superuser */
1217+
if (!superuser())
12171218
{
1219+
1220+
/* MDB addon */
1221+
#if 0
12181222
if (extension_is_trusted(control))
12191223
switch_to_superuser = true;
1220-
else if (from_version == NULL)
1224+
else
1225+
#endif
1226+
if (from_version == NULL)
12211227
ereport(ERROR,
12221228
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
12231229
errmsg("permission denied to create extension \"%s\"",
@@ -1242,6 +1248,11 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
12421248
else
12431249
elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version);
12441250

1251+
/* MDB addons. Never trust extension's controlfile trusted and/or superuser field.
1252+
* XXX: rethink this if we start support SQL-level ext API */
1253+
1254+
switch_to_superuser = false;
1255+
12451256
/*
12461257
* If installing a trusted extension on behalf of a non-superuser, become
12471258
* the bootstrap superuser. (This switch will be cleaned up automatically

src/test/modules/test_extensions/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ MODULE = test_extensions
44
PGFILEDESC = "test_extensions - regression testing for EXTENSION support"
55

66
EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \
7-
test_ext7 test_ext8 test_ext9 test_ext_cine test_ext_cor \
7+
test_ext7 test_ext8 test_ext9 test_ext_mdb test_ext_cine test_ext_cor \
88
test_ext_cyclic1 test_ext_cyclic2 \
99
test_ext_extschema \
1010
test_ext_evttrig \
@@ -17,6 +17,7 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
1717
test_ext7--2.0--2.1bad.sql test_ext7--2.0--2.2bad.sql \
1818
test_ext8--1.0.sql \
1919
test_ext9--1.0.sql \
20+
test_ext_mdb--1.0.sql \
2021
test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \
2122
test_ext_cor--1.0.sql \
2223
test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \
@@ -27,7 +28,7 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
2728
test_ext_req_schema2--1.0.sql \
2829
test_ext_req_schema3--1.0.sql
2930

30-
REGRESS = test_extensions test_extdepend
31+
REGRESS = test_extensions test_extdepend test_mdb
3132
TAP_TESTS = 1
3233

3334
# force C locale for output stability
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
CREATE ROLE regress_mdb_admin_user1;
2+
CREATE ROLE mdb_superuser;
3+
CREATE ROLE mdb_admin;
4+
GRANT mdb_admin TO regress_mdb_admin_user1;
5+
GRANT mdb_superuser TO regress_mdb_admin_user1;
6+
GRANT CREATE ON DATABASE contrib_regression TO regress_mdb_admin_user1;
7+
SET ROLE regress_mdb_admin_user1;
8+
CREATE EXTENSION test_ext_mdb;
9+
ERROR: permission denied to create extension "test_ext_mdb"
10+
HINT: Must have CREATE privilege on current database to create this extension.
11+
RESET ROLE;
12+
DROP ROLE mdb_superuser;
13+
DROP ROLE mdb_admin;
14+
DROP OWNED BY regress_mdb_admin_user1;
15+
DROP ROLE regress_mdb_admin_user1;

src/test/modules/test_extensions/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ test_install_data += files(
4444
'test_ext_req_schema3.control',
4545
'test_ext_set_schema--1.0.sql',
4646
'test_ext_set_schema.control',
47+
'test_ext_mdb--1.0.sql',
48+
'test_ext_mdb.control',
4749
)
4850

4951
tests += {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
CREATE ROLE regress_mdb_admin_user1;
2+
3+
CREATE ROLE mdb_superuser;
4+
CREATE ROLE mdb_admin;
5+
6+
GRANT mdb_admin TO regress_mdb_admin_user1;
7+
GRANT mdb_superuser TO regress_mdb_admin_user1;
8+
9+
GRANT CREATE ON DATABASE contrib_regression TO regress_mdb_admin_user1;
10+
11+
SET ROLE regress_mdb_admin_user1;
12+
13+
CREATE EXTENSION test_ext_mdb;
14+
15+
RESET ROLE;
16+
17+
DROP ROLE mdb_superuser;
18+
DROP ROLE mdb_admin;
19+
20+
DROP OWNED BY regress_mdb_admin_user1;
21+
22+
DROP ROLE regress_mdb_admin_user1;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* src/test/modules/test_extensions/test_ext9--1.0.sql */
2+
3+
-- complain if script is sourced in psql, rather than via CREATE EXTENSION
4+
\echo Use "CREATE EXTENSION test_ext9" to load this file. \quit
5+
6+
-- create some random data type
7+
create domain posint as int check (value > 0);
8+
9+
-- use it in regular and temporary tables and functions
10+
11+
create table ext9_table1 (f1 posint);
12+
13+
create function ext9_even (posint) returns bool as
14+
'select ($1 % 2) = 0' language sql;
15+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
comment = 'Test extension mdb'
2+
default_version = '1.0'
3+
schema = 'public'
4+
relocatable = false
5+
trusted = true
6+
superuser = false

0 commit comments

Comments
 (0)