Skip to content

Commit 0be7768

Browse files
reshkereshke
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 cec67ee commit 0be7768

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

1092+
/* MDB addons. Never trust extension's controlfile trusted and/or superuser field.
1093+
* XXX: rethink this if we start support SQL-level ext API */
1094+
1095+
switch_to_superuser = false;
1096+
10861097
/*
10871098
* If installing a trusted extension on behalf of a non-superuser, become
10881099
* 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 \
@@ -15,6 +15,7 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
1515
test_ext4--1.0.sql test_ext5--1.0.sql test_ext6--1.0.sql \
1616
test_ext7--1.0.sql test_ext7--1.0--2.0.sql test_ext8--1.0.sql \
1717
test_ext9--1.0.sql \
18+
test_ext_mdb--1.0.sql \
1819
test_ext_cine--1.0.sql test_ext_cine--1.0--1.1.sql \
1920
test_ext_cor--1.0.sql \
2021
test_ext_cyclic1--1.0.sql test_ext_cyclic2--1.0.sql \
@@ -25,7 +26,7 @@ DATA = test_ext1--1.0.sql test_ext2--1.0.sql test_ext3--1.0.sql \
2526
test_ext_req_schema2--1.0.sql \
2627
test_ext_req_schema3--1.0.sql
2728

28-
REGRESS = test_extensions test_extdepend
29+
REGRESS = test_extensions test_extdepend test_mdb
2930

3031
# force C locale for output stability
3132
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
@@ -42,6 +42,8 @@ test_install_data += files(
4242
'test_ext_req_schema3.control',
4343
'test_ext_set_schema--1.0.sql',
4444
'test_ext_set_schema.control',
45+
'test_ext_mdb--1.0.sql',
46+
'test_ext_mdb.control',
4547
)
4648

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