Skip to content

Commit dcac9df

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 d48e0f1 commit dcac9df

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
@@ -1055,11 +1055,17 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
10551055
* here so that the control flags are correctly associated with the right
10561056
* script(s) if they happen to be set in secondary control files.
10571057
*/
1058-
if (control->superuser && !superuser())
1058+
/* MDB does not trust control->superuser */
1059+
if (!superuser())
10591060
{
1061+
1062+
/* MDB addon */
1063+
#if 0
10601064
if (extension_is_trusted(control))
10611065
switch_to_superuser = true;
1062-
else if (from_version == NULL)
1066+
else
1067+
#endif
1068+
if (from_version == NULL)
10631069
ereport(ERROR,
10641070
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
10651071
errmsg("permission denied to create extension \"%s\"",
@@ -1084,6 +1090,11 @@ execute_extension_script(Oid extensionOid, ExtensionControlFile *control,
10841090
else
10851091
elog(DEBUG1, "executing extension script for \"%s\" update from version '%s' to '%s'", control->name, from_version, version);
10861092

1093+
/* MDB addons. Never trust extension's controlfile trusted and/or superuser field.
1094+
* XXX: rethink this if we start support SQL-level ext API */
1095+
1096+
switch_to_superuser = false;
1097+
10871098
/*
10881099
* If installing a trusted extension on behalf of a non-superuser, become
10891100
* 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_ext_cine test_ext_cor \
7+
test_ext7 test_ext8 test_ext_mdb test_ext_cine test_ext_cor \
88
test_ext_cyclic1 test_ext_cyclic2 \
99
test_ext_extschema \
1010
test_ext_evttrig \
@@ -13,6 +13,7 @@ EXTENSION = test_ext1 test_ext2 test_ext3 test_ext4 test_ext5 test_ext6 \
1313
DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
1414
test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \
1515
test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \
16+
test_ext_mdb--1.0.sql \
1617
test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \
1718
test_ext_cor--1.0.sql \
1819
test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \
@@ -22,7 +23,7 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
2223
test_ext_req_schema2--1.0.sql \
2324
test_ext_req_schema3--1.0.sql
2425

25-
REGRESS = test_extensions test_extdepend
26+
REGRESS = test_extensions test_extdepend test_mdb
2627

2728
# force C locale for output stability
2829
NO_LOCALE = 1
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
@@ -38,6 +38,8 @@ test_install_data += files(
3838
'test_ext_req_schema2.control',
3939
'test_ext_req_schema3--1.0.sql',
4040
'test_ext_req_schema3.control',
41+
'test_ext_mdb--1.0.sql',
42+
'test_ext_mdb.control',
4143
)
4244

4345
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)