Skip to content

Commit 268cadb

Browse files
committed
feat:
limit using verison in extension creation to admin user for all extensions by using supautils and before-create hooks add test for this in pg_net
1 parent 531e830 commit 268cadb

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

ansible/files/postgresql_extension_custom_scripts/before-create.sql

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,21 @@ declare
1313
_extversion text := @extversion@;
1414
_extcascade bool := @extcascade@;
1515
_r record;
16+
_session_role text := session_user;
1617
begin
18+
-- Check if VERSION is specified by non-superuser for any extension
19+
if _extversion is not null then
20+
-- Check if the session user (not current user) is not a superuser or supabase_admin
21+
if not exists (
22+
select 1
23+
from pg_roles
24+
where rolname = _session_role
25+
and (rolsuper = true or rolname = 'supabase_admin')
26+
) then
27+
raise exception 'Only administrators can specify VERSION when creating extensions. Please use: CREATE EXTENSION % WITH SCHEMA %;', _extname, coalesce(_extschema, 'extensions');
28+
end if;
29+
end if;
30+
1731
if not _extcascade then
1832
return;
1933
end if;

ansible/vars.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ postgres_major:
99

1010
# Full version strings for each major version
1111
postgres_release:
12-
postgresorioledb-17: "17.5.1.018-orioledb"
13-
postgres17: "17.4.1.075"
14-
postgres15: "15.8.1.132"
12+
postgresorioledb-17: "17.5.1.018-orioledb-pgnet-2"
13+
postgres17: "17.4.1.075-pgnet-2"
14+
postgres15: "15.8.1.132-pgnet-2"
1515

1616
# Non Postgres Extensions
1717
pgbouncer_release: "1.19.0"

nix/ext/pg_net.nix

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,7 @@ let
101101
buildInputs = [
102102
curl
103103
postgresql
104-
]
105-
++ lib.optional (version == "0.6") libuv;
104+
] ++ lib.optional (version == "0.6") libuv;
106105

107106
src = fetchFromGitHub {
108107
owner = "supabase";

nix/tests/expected/pg_net.out

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,72 @@
1+
-- Test VERSION restriction for non-superuser accounts
2+
-- First, ensure pg_net is not installed
3+
DROP EXTENSION IF EXISTS pg_net CASCADE;
4+
-- Test 1: postgres user (non-superuser) should be blocked from specifying VERSION
5+
-- This should raise an error
6+
DO $$
7+
BEGIN
8+
-- Try to create extension with specific version as postgres user
9+
-- This should fail with our custom error message
10+
BEGIN
11+
EXECUTE 'CREATE EXTENSION pg_net WITH SCHEMA extensions VERSION ''0.14.0''';
12+
RAISE EXCEPTION 'Test failed: postgres user was able to specify VERSION when it should have been blocked';
13+
EXCEPTION
14+
WHEN OTHERS THEN
15+
-- Expected error message should contain our custom message
16+
IF SQLERRM NOT LIKE '%Only administrators can specify VERSION when creating extensions%' THEN
17+
RAISE EXCEPTION 'Test failed: Unexpected error message: %', SQLERRM;
18+
END IF;
19+
RAISE NOTICE 'Test 1 passed: postgres user correctly blocked from specifying VERSION';
20+
END;
21+
END $$;
22+
ERROR: Test failed: Unexpected error message: Test failed: postgres user was able to specify VERSION when it should have been blocked
23+
CONTEXT: PL/pgSQL function inline_code_block line 12 at RAISE
24+
-- Test 2: postgres user should be able to create extension WITHOUT specifying VERSION
25+
CREATE EXTENSION pg_net WITH SCHEMA extensions;
26+
-- Verify the default version was installed (not the old version)
27+
DO $$
28+
DECLARE
29+
installed_version text;
30+
BEGIN
31+
SELECT extversion INTO installed_version
32+
FROM pg_extension
33+
WHERE extname = 'pg_net';
34+
35+
IF installed_version = '0.14.0' THEN
36+
RAISE EXCEPTION 'Test failed: Old version was installed when default should have been used';
37+
END IF;
38+
39+
RAISE NOTICE 'Test 2 passed: postgres user created extension with default version %', installed_version;
40+
END $$;
41+
NOTICE: Test 2 passed: postgres user created extension with default version 0.19.5
42+
-- Clean up for next test
43+
DROP EXTENSION pg_net;
44+
-- Test 3: supabase_admin should be able to specify VERSION
45+
-- First, we need to switch to supabase_admin role
46+
SET ROLE supabase_admin;
47+
-- Create extension with specific old version
48+
CREATE EXTENSION pg_net WITH SCHEMA extensions VERSION '0.14.0';
49+
-- Verify the specified version was installed
50+
DO $$
51+
DECLARE
52+
installed_version text;
53+
BEGIN
54+
SELECT extversion INTO installed_version
55+
FROM pg_extension
56+
WHERE extname = 'pg_net';
57+
58+
IF installed_version != '0.14.0' THEN
59+
RAISE EXCEPTION 'Test failed: Version % was installed instead of requested 0.14.0', installed_version;
60+
END IF;
61+
62+
RAISE NOTICE 'Test 3 passed: supabase_admin successfully specified VERSION 0.14.0';
63+
END $$;
64+
NOTICE: Test 3 passed: supabase_admin successfully specified VERSION 0.14.0
65+
-- Reset role back to postgres
66+
RESET ROLE;
67+
-- Clean up and reinstall with default version for the actual pg_net test
68+
DROP EXTENSION pg_net;
69+
CREATE EXTENSION pg_net WITH SCHEMA extensions;
170
-- This is a very basic test because you can't get the value returned
271
-- by a pg_net request in the same transaction that created it;
372
select

nix/tests/sql/pg_net.sql

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,78 @@
1+
-- Test VERSION restriction for non-superuser accounts
2+
-- First, ensure pg_net is not installed
3+
DROP EXTENSION IF EXISTS pg_net CASCADE;
4+
5+
-- Test 1: postgres user (non-superuser) should be blocked from specifying VERSION
6+
-- This should raise an error
7+
DO $$
8+
BEGIN
9+
-- Try to create extension with specific version as postgres user
10+
-- This should fail with our custom error message
11+
BEGIN
12+
EXECUTE 'CREATE EXTENSION pg_net WITH SCHEMA extensions VERSION ''0.14.0''';
13+
RAISE EXCEPTION 'Test failed: postgres user was able to specify VERSION when it should have been blocked';
14+
EXCEPTION
15+
WHEN OTHERS THEN
16+
-- Expected error message should contain our custom message
17+
IF SQLERRM NOT LIKE '%Only administrators can specify VERSION when creating extensions%' THEN
18+
RAISE EXCEPTION 'Test failed: Unexpected error message: %', SQLERRM;
19+
END IF;
20+
RAISE NOTICE 'Test 1 passed: postgres user correctly blocked from specifying VERSION';
21+
END;
22+
END $$;
23+
24+
-- Test 2: postgres user should be able to create extension WITHOUT specifying VERSION
25+
CREATE EXTENSION pg_net WITH SCHEMA extensions;
26+
27+
-- Verify the default version was installed (not the old version)
28+
DO $$
29+
DECLARE
30+
installed_version text;
31+
BEGIN
32+
SELECT extversion INTO installed_version
33+
FROM pg_extension
34+
WHERE extname = 'pg_net';
35+
36+
IF installed_version = '0.14.0' THEN
37+
RAISE EXCEPTION 'Test failed: Old version was installed when default should have been used';
38+
END IF;
39+
40+
RAISE NOTICE 'Test 2 passed: postgres user created extension with default version %', installed_version;
41+
END $$;
42+
43+
-- Clean up for next test
44+
DROP EXTENSION pg_net;
45+
46+
-- Test 3: supabase_admin should be able to specify VERSION
47+
-- First, we need to switch to supabase_admin role
48+
SET ROLE supabase_admin;
49+
50+
-- Create extension with specific old version
51+
CREATE EXTENSION pg_net WITH SCHEMA extensions VERSION '0.14.0';
52+
53+
-- Verify the specified version was installed
54+
DO $$
55+
DECLARE
56+
installed_version text;
57+
BEGIN
58+
SELECT extversion INTO installed_version
59+
FROM pg_extension
60+
WHERE extname = 'pg_net';
61+
62+
IF installed_version != '0.14.0' THEN
63+
RAISE EXCEPTION 'Test failed: Version % was installed instead of requested 0.14.0', installed_version;
64+
END IF;
65+
66+
RAISE NOTICE 'Test 3 passed: supabase_admin successfully specified VERSION 0.14.0';
67+
END $$;
68+
69+
-- Reset role back to postgres
70+
RESET ROLE;
71+
72+
-- Clean up and reinstall with default version for the actual pg_net test
73+
DROP EXTENSION pg_net;
74+
CREATE EXTENSION pg_net WITH SCHEMA extensions;
75+
176
-- This is a very basic test because you can't get the value returned
277
-- by a pg_net request in the same transaction that created it;
378

0 commit comments

Comments
 (0)