Skip to content

Commit 8c9a5d6

Browse files
committed
fix: remove pg_net grants
1 parent e88ce4b commit 8c9a5d6

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-- migrate:up
2+
DO $$
3+
DECLARE
4+
pg_net_installed boolean;
5+
BEGIN
6+
-- checks if pg_net is enabled
7+
pg_net_installed = (
8+
select count(*) = 1
9+
from pg_available_extensions
10+
where name = 'pg_net'
11+
and installed_version is not null
12+
13+
);
14+
15+
IF pg_net_installed
16+
THEN
17+
ALTER function net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY INVOKER;
18+
ALTER function net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) SECURITY INVOKER;
19+
20+
REVOKE EXECUTE ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) FROM supabase_functions_admin, postgres, anon, authenticated, service_role;
21+
REVOKE EXECUTE ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) FROM supabase_functions_admin, postgres, anon, authenticated, service_role;
22+
23+
GRANT ALL ON FUNCTION net.http_get(url text, params jsonb, headers jsonb, timeout_milliseconds integer) TO PUBLIC;
24+
GRANT ALL ON FUNCTION net.http_post(url text, body jsonb, params jsonb, headers jsonb, timeout_milliseconds integer) TO PUBLIC;
25+
END IF;
26+
END $$;
27+
28+
CREATE OR REPLACE FUNCTION extensions.grant_pg_net_access()
29+
RETURNS event_trigger
30+
LANGUAGE plpgsql
31+
AS $$
32+
BEGIN
33+
IF EXISTS (
34+
SELECT 1
35+
FROM pg_event_trigger_ddl_commands() AS ev
36+
JOIN pg_extension AS ext
37+
ON ev.objid = ext.oid
38+
WHERE ext.extname = 'pg_net'
39+
)
40+
THEN
41+
IF NOT EXISTS (
42+
SELECT 1
43+
FROM pg_roles
44+
WHERE rolname = 'supabase_functions_admin'
45+
)
46+
THEN
47+
CREATE USER supabase_functions_admin NOINHERIT CREATEROLE LOGIN NOREPLICATION;
48+
END IF;
49+
50+
GRANT USAGE ON SCHEMA net TO supabase_functions_admin, postgres, anon, authenticated, service_role;
51+
END IF;
52+
END;
53+
$$;
54+
COMMENT ON FUNCTION extensions.grant_pg_net_access IS 'Grants access to pg_net';
55+
56+
-- migrate:down

0 commit comments

Comments
 (0)