@pgpm/supabase 0.5.1 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +2 -1
- package/Makefile +2 -3
- package/README.md +45 -59
- package/deploy/roles/anon.sql +15 -0
- package/deploy/schemas/supabase_integration/procedures/pre_request.sql +26 -0
- package/deploy/schemas/supabase_integration/schema.sql +9 -0
- package/package.json +27 -12
- package/pgpm-supabase.control +7 -0
- package/pgpm.plan +6 -3
- package/revert/roles/anon.sql +7 -0
- package/revert/schemas/supabase_integration/procedures/pre_request.sql +7 -0
- package/revert/schemas/supabase_integration/schema.sql +7 -0
- package/sql/pgpm-supabase--1.0.0.bundle.tar.gz +0 -0
- package/sql/pgpm-supabase--1.0.0.sql +32 -0
- package/verify/roles/anon.sql +12 -0
- package/verify/schemas/supabase_integration/procedures/pre_request.sql +19 -0
- package/verify/schemas/supabase_integration/schema.sql +18 -0
- package/__tests__/_realtime/_realtime-extensions.test.ts +0 -129
- package/__tests__/_realtime/_realtime-schema-migrations.test.ts +0 -160
- package/__tests__/_realtime/_realtime-tenants.test.ts +0 -163
- package/__tests__/auth/auth.audit-log-entries.test.ts +0 -148
- package/__tests__/auth/auth.flow-state.test.ts +0 -114
- package/__tests__/auth/auth.identities.test.ts +0 -149
- package/__tests__/auth/auth.instances.test.ts +0 -134
- package/__tests__/auth/auth.mfa-amr-claims.test.ts +0 -142
- package/__tests__/auth/auth.mfa-challenges.test.ts +0 -117
- package/__tests__/auth/auth.mfa-factors.test.ts +0 -118
- package/__tests__/auth/auth.oauth-authorizations.test.ts +0 -123
- package/__tests__/auth/auth.oauth-clients.test.ts +0 -120
- package/__tests__/auth/auth.oauth-consents.test.ts +0 -122
- package/__tests__/auth/auth.one-time-tokens.test.ts +0 -194
- package/__tests__/auth/auth.refresh-tokens.test.ts +0 -134
- package/__tests__/auth/auth.saml-providers.test.ts +0 -154
- package/__tests__/auth/auth.saml-relay-states.test.ts +0 -145
- package/__tests__/auth/auth.schema-migrations.test.ts +0 -131
- package/__tests__/auth/auth.sessions.test.ts +0 -140
- package/__tests__/auth/auth.sso-domains.test.ts +0 -137
- package/__tests__/auth/auth.sso-providers.test.ts +0 -136
- package/__tests__/auth/auth.users.test.ts +0 -314
- package/__tests__/conditional.test.ts +0 -229
- package/__tests__/functions.test.ts +0 -252
- package/__tests__/jsonb.test.ts +0 -237
- package/__tests__/net/net.http-request-queue.test.ts +0 -159
- package/__tests__/net/net.http-response.test.ts +0 -163
- package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +0 -168
- package/__tests__/realtime/realtime.messages.test.ts +0 -162
- package/__tests__/realtime/realtime.schema-migrations.test.ts +0 -139
- package/__tests__/realtime/realtime.subscription.test.ts +0 -159
- package/__tests__/storage/storage.buckets-analytics.test.ts +0 -134
- package/__tests__/storage/storage.buckets.test.ts +0 -146
- package/__tests__/storage/storage.iceberg-namespaces.test.ts +0 -132
- package/__tests__/storage/storage.iceberg-tables.test.ts +0 -142
- package/__tests__/storage/storage.migrations.test.ts +0 -133
- package/__tests__/storage/storage.objects.test.ts +0 -160
- package/__tests__/storage/storage.prefixes.test.ts +0 -138
- package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +0 -133
- package/__tests__/storage/storage.s3-multipart-uploads.test.ts +0 -128
- package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +0 -125
- package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +0 -213
- package/__tests__/temporal.test.ts +0 -225
- package/__tests__/vault-secrets.test.ts +0 -145
- package/deploy/LICENSE +0 -9
- package/deploy/supabase.sql +0 -2271
- package/jest.config.js +0 -15
- package/revert/supabase.sql +0 -3
- package/sql/supabase--0.0.1.sql +0 -1368
- package/src/index.ts +0 -3
- package/supabase.control +0 -7
- package/verify/supabase.sql +0 -3
package/LICENSE
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
The MIT License (MIT)
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2025
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive
|
|
4
5
|
|
|
5
6
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
7
|
of this software and associated documentation files (the "Software"), to deal
|
package/Makefile
CHANGED
package/README.md
CHANGED
|
@@ -1,64 +1,50 @@
|
|
|
1
1
|
# @pgpm/supabase
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
3
|
+
Bridge Supabase PostgREST requests to Constructive's transaction-local RLS claims.
|
|
4
|
+
|
|
5
|
+
## Supabase setup
|
|
6
|
+
|
|
7
|
+
1. Run `pgpm admin-users bootstrap --yes` against the Supabase database to create
|
|
8
|
+
Constructive's `anonymous` role (and the other base roles).
|
|
9
|
+
2. Ensure Supabase's `anon` and `authenticated` roles already exist. Outside
|
|
10
|
+
Supabase, create `anon` with `CREATE ROLE anon NOINHERIT NOLOGIN;` before deployment.
|
|
11
|
+
3. Install `@pgpm/supabase` and deploy `pgpm-supabase` with `pgpm deploy`.
|
|
12
|
+
4. As the database administrator, configure PostgREST:
|
|
13
|
+
|
|
14
|
+
```sql
|
|
15
|
+
ALTER ROLE authenticator SET pgrst.db_pre_request = 'supabase_integration.pre_request';
|
|
16
|
+
NOTIFY pgrst, 'reload config';
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Schema USAGE and hook EXECUTE are available to PUBLIC so PostgREST can also
|
|
20
|
+
invoke the hook as `service_role` without requiring that role at deploy time.
|
|
21
|
+
The hook is a no-op for roles other than `anon` and `authenticated`.
|
|
22
|
+
|
|
23
|
+
The security-invoker hook switches `anon` to `anonymous`. It does not grant
|
|
24
|
+
automatic inheritance: `anon` cannot use `anonymous` grants without switching.
|
|
25
|
+
On PostgreSQL 16+ the membership explicitly disables inheritance and allows SET;
|
|
26
|
+
on PostgreSQL 15 it relies on Supabase's `anon` being NOINHERIT.
|
|
27
|
+
|
|
28
|
+
For `authenticated`, the hook copies the verified `request.jwt.claims` JSON `sub`
|
|
29
|
+
to both `jwt.claims.user_id` and `jwt.claims.principal_id`, transaction-locally.
|
|
30
|
+
The subject must correspond to Constructive's `users.id`. Missing/empty claims
|
|
31
|
+
or subjects leave the claims unset; malformed JSON raises an error. Other roles
|
|
32
|
+
are unchanged. Only use this hook behind PostgREST's verified JWT boundary;
|
|
33
|
+
it does not verify JWTs or provision users. Each request must use a fresh transaction.
|
|
34
|
+
|
|
35
|
+
RLS policies must treat empty claim strings as absent, for example
|
|
36
|
+
`nullif(current_setting('jwt.claims.user_id', true), '')::uuid`. PostgreSQL can
|
|
37
|
+
return an empty string for a custom GUC after its transaction-local value is
|
|
38
|
+
rolled back; a direct UUID cast would then raise on a later unauthenticated request.
|
|
39
|
+
|
|
40
|
+
## Development
|
|
41
|
+
|
|
42
|
+
```sh
|
|
43
|
+
eval "$(pgpm env)"
|
|
27
44
|
pnpm test
|
|
28
|
-
|
|
29
|
-
# Watch mode for development
|
|
30
|
-
pnpm test:watch
|
|
45
|
+
pgpm package
|
|
31
46
|
```
|
|
32
47
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
37
|
-
|
|
38
|
-
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
39
|
-
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
40
|
-
|
|
41
|
-
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
42
|
-
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
43
|
-
|
|
44
|
-
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
45
|
-
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
46
|
-
|
|
47
|
-
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
48
|
-
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
49
|
-
|
|
50
|
-
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
51
|
-
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
52
|
-
|
|
53
|
-
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
54
|
-
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
55
|
-
|
|
56
|
-
## Credits
|
|
57
|
-
|
|
58
|
-
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
59
|
-
|
|
60
|
-
## Disclaimer
|
|
61
|
-
|
|
62
|
-
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED “AS IS”, AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
63
|
-
|
|
64
|
-
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
|
48
|
+
The tests create a missing `anon` role before deploying the module. Reverting
|
|
49
|
+
revokes the membership and drops the hook and schema, but does not drop roles
|
|
50
|
+
or change PostgREST configuration. Remove the pre-request setting before reverting.
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
-- Deploy roles/anon to pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
DO $$
|
|
6
|
+
BEGIN
|
|
7
|
+
IF current_setting('server_version_num')::integer >= 160000 THEN
|
|
8
|
+
GRANT anonymous TO anon WITH INHERIT FALSE, SET TRUE;
|
|
9
|
+
ELSE
|
|
10
|
+
GRANT anonymous TO anon;
|
|
11
|
+
END IF;
|
|
12
|
+
END;
|
|
13
|
+
$$;
|
|
14
|
+
|
|
15
|
+
COMMIT;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
-- Deploy schemas/supabase_integration/procedures/pre_request to pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
CREATE FUNCTION supabase_integration.pre_request()
|
|
6
|
+
RETURNS void
|
|
7
|
+
LANGUAGE plpgsql
|
|
8
|
+
AS $$
|
|
9
|
+
DECLARE
|
|
10
|
+
subject text;
|
|
11
|
+
BEGIN
|
|
12
|
+
IF current_user = 'anon' THEN
|
|
13
|
+
PERFORM set_config('role', 'anonymous', true);
|
|
14
|
+
ELSIF current_user = 'authenticated' THEN
|
|
15
|
+
subject := nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'sub';
|
|
16
|
+
IF subject IS NOT NULL AND subject <> '' THEN
|
|
17
|
+
PERFORM set_config('jwt.claims.user_id', subject, true);
|
|
18
|
+
PERFORM set_config('jwt.claims.principal_id', subject, true);
|
|
19
|
+
END IF;
|
|
20
|
+
END IF;
|
|
21
|
+
END;
|
|
22
|
+
$$;
|
|
23
|
+
|
|
24
|
+
GRANT EXECUTE ON FUNCTION supabase_integration.pre_request() TO PUBLIC;
|
|
25
|
+
|
|
26
|
+
COMMIT;
|
package/package.json
CHANGED
|
@@ -1,23 +1,38 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pgpm/supabase",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Bridge Supabase PostgREST requests to Constructive RLS claims",
|
|
5
|
+
"author": "Dan Lynch <pyramation@gmail.com>",
|
|
6
|
+
"contributors": [
|
|
7
|
+
"Constructive <developers@constructive.io>"
|
|
8
|
+
],
|
|
9
|
+
"keywords": [
|
|
10
|
+
"postgresql",
|
|
11
|
+
"pgpm",
|
|
12
|
+
"supabase",
|
|
13
|
+
"authentication"
|
|
14
|
+
],
|
|
6
15
|
"publishConfig": {
|
|
7
16
|
"access": "public"
|
|
8
17
|
},
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"test": "
|
|
12
|
-
|
|
18
|
+
"devDependencies": {
|
|
19
|
+
"pgpm": "^5",
|
|
20
|
+
"pgsql-test": "^5.20.13"
|
|
21
|
+
},
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@pgpm/verify": "0.47.7"
|
|
13
24
|
},
|
|
14
25
|
"repository": {
|
|
15
26
|
"type": "git",
|
|
16
|
-
"url": "https://github.com/constructive-io/
|
|
27
|
+
"url": "https://github.com/constructive-io/pgpm-modules"
|
|
17
28
|
},
|
|
18
|
-
"homepage": "https://github.com/constructive-io/
|
|
29
|
+
"homepage": "https://github.com/constructive-io/pgpm-modules",
|
|
19
30
|
"bugs": {
|
|
20
|
-
"url": "https://github.com/constructive-io/
|
|
31
|
+
"url": "https://github.com/constructive-io/pgpm-modules/issues"
|
|
21
32
|
},
|
|
22
|
-
"
|
|
23
|
-
|
|
33
|
+
"scripts": {
|
|
34
|
+
"bundle": "pgpm package",
|
|
35
|
+
"test": "jest",
|
|
36
|
+
"test:watch": "jest --watch"
|
|
37
|
+
}
|
|
38
|
+
}
|
package/pgpm.plan
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
%syntax-version=1.0.0
|
|
2
|
-
%project=supabase
|
|
3
|
-
%uri=supabase
|
|
4
|
-
|
|
2
|
+
%project=pgpm-supabase
|
|
3
|
+
%uri=pgpm-supabase
|
|
4
|
+
|
|
5
|
+
schemas/supabase_integration/schema [pgpm-verify:procedures/assert_schema pgpm-verify:procedures/assert_function] 2026-08-27T00:00:00Z Constructive <developers@constructive.io> # add schemas/supabase_integration/schema
|
|
6
|
+
roles/anon [schemas/supabase_integration/schema] 2026-08-27T00:00:00Z Constructive <developers@constructive.io> # add roles/anon
|
|
7
|
+
schemas/supabase_integration/procedures/pre_request [schemas/supabase_integration/schema roles/anon] 2026-08-27T00:00:00Z Constructive <developers@constructive.io> # add schemas/supabase_integration/procedures/pre_request
|
|
Binary file
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
\echo Use "CREATE EXTENSION pgpm-supabase" to load this file. \quit
|
|
2
|
+
CREATE SCHEMA supabase_integration;
|
|
3
|
+
|
|
4
|
+
GRANT USAGE ON SCHEMA supabase_integration TO PUBLIC;
|
|
5
|
+
|
|
6
|
+
DO $EOFCODE$
|
|
7
|
+
BEGIN
|
|
8
|
+
IF current_setting('server_version_num')::integer >= 160000 THEN
|
|
9
|
+
GRANT anonymous TO anon WITH INHERIT FALSE, SET TRUE;
|
|
10
|
+
ELSE
|
|
11
|
+
GRANT anonymous TO anon;
|
|
12
|
+
END IF;
|
|
13
|
+
END;
|
|
14
|
+
$EOFCODE$;
|
|
15
|
+
|
|
16
|
+
CREATE FUNCTION supabase_integration.pre_request() RETURNS void LANGUAGE plpgsql AS $EOFCODE$
|
|
17
|
+
DECLARE
|
|
18
|
+
subject text;
|
|
19
|
+
BEGIN
|
|
20
|
+
IF current_user = 'anon' THEN
|
|
21
|
+
PERFORM set_config('role', 'anonymous', true);
|
|
22
|
+
ELSIF current_user = 'authenticated' THEN
|
|
23
|
+
subject := nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'sub';
|
|
24
|
+
IF subject IS NOT NULL AND subject <> '' THEN
|
|
25
|
+
PERFORM set_config('jwt.claims.user_id', subject, true);
|
|
26
|
+
PERFORM set_config('jwt.claims.principal_id', subject, true);
|
|
27
|
+
END IF;
|
|
28
|
+
END IF;
|
|
29
|
+
END;
|
|
30
|
+
$EOFCODE$;
|
|
31
|
+
|
|
32
|
+
GRANT EXECUTE ON FUNCTION supabase_integration.pre_request() TO PUBLIC;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
-- Verify schemas/supabase_integration/procedures/pre_request from pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
SELECT assert_function('supabase_integration.pre_request()'::regprocedure);
|
|
6
|
+
|
|
7
|
+
DO $$
|
|
8
|
+
BEGIN
|
|
9
|
+
ASSERT EXISTS (
|
|
10
|
+
SELECT FROM pg_proc,
|
|
11
|
+
LATERAL aclexplode(coalesce(proacl, acldefault('f', proowner))) AS privilege
|
|
12
|
+
WHERE oid = 'supabase_integration.pre_request()'::regprocedure
|
|
13
|
+
AND privilege.grantee = 0 AND privilege.privilege_type = 'EXECUTE'
|
|
14
|
+
);
|
|
15
|
+
ASSERT NOT (SELECT prosecdef FROM pg_proc WHERE oid = 'supabase_integration.pre_request()'::regprocedure);
|
|
16
|
+
END;
|
|
17
|
+
$$;
|
|
18
|
+
|
|
19
|
+
ROLLBACK;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
-- Verify schemas/supabase_integration/schema from pg
|
|
2
|
+
|
|
3
|
+
BEGIN;
|
|
4
|
+
|
|
5
|
+
SELECT assert_schema('supabase_integration'::regnamespace);
|
|
6
|
+
|
|
7
|
+
DO $$
|
|
8
|
+
BEGIN
|
|
9
|
+
ASSERT EXISTS (
|
|
10
|
+
SELECT FROM pg_namespace,
|
|
11
|
+
LATERAL aclexplode(nspacl) AS privilege
|
|
12
|
+
WHERE nspname = 'supabase_integration'
|
|
13
|
+
AND privilege.grantee = 0 AND privilege.privilege_type = 'USAGE'
|
|
14
|
+
);
|
|
15
|
+
END;
|
|
16
|
+
$$;
|
|
17
|
+
|
|
18
|
+
ROLLBACK;
|
|
@@ -1,129 +0,0 @@
|
|
|
1
|
-
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
-
|
|
3
|
-
let pg: PgTestClient;
|
|
4
|
-
let db: PgTestClient;
|
|
5
|
-
let teardown: () => Promise<void>;
|
|
6
|
-
|
|
7
|
-
let tableExists = false;
|
|
8
|
-
|
|
9
|
-
beforeAll(async () => {
|
|
10
|
-
({ pg, db, teardown } = await getConnections());
|
|
11
|
-
|
|
12
|
-
// verify _realtime schema exists (optional schema)
|
|
13
|
-
const realtimeSchemaExists = await pg.any(
|
|
14
|
-
`SELECT EXISTS (
|
|
15
|
-
SELECT FROM information_schema.schemata
|
|
16
|
-
WHERE schema_name = '_realtime'
|
|
17
|
-
) as exists`
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
if (realtimeSchemaExists[0]?.exists === true) {
|
|
21
|
-
// grant access to _realtime schema for testing
|
|
22
|
-
await pg.any(
|
|
23
|
-
`GRANT USAGE ON SCHEMA _realtime TO public;
|
|
24
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO service_role;
|
|
25
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO authenticated;
|
|
26
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO anon;
|
|
27
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO service_role;
|
|
28
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO authenticated;
|
|
29
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO anon;`,
|
|
30
|
-
[]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// check if _realtime.extensions table exists (using pg in beforeAll only)
|
|
34
|
-
const exists = await pg.any(
|
|
35
|
-
`SELECT EXISTS (
|
|
36
|
-
SELECT FROM information_schema.tables
|
|
37
|
-
WHERE table_schema = '_realtime' AND table_name = 'extensions'
|
|
38
|
-
) as exists`
|
|
39
|
-
);
|
|
40
|
-
tableExists = exists[0]?.exists === true;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
afterAll(async () => {
|
|
45
|
-
await teardown();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
beforeEach(async () => {
|
|
49
|
-
await db.beforeEach();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
afterEach(async () => {
|
|
53
|
-
await db.afterEach();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe('tutorial: _realtime extensions table access', () => {
|
|
57
|
-
|
|
58
|
-
it('should verify extensions table exists in _realtime schema', async () => {
|
|
59
|
-
db.setContext({ role: 'service_role' });
|
|
60
|
-
|
|
61
|
-
// verify table exists in information schema
|
|
62
|
-
const exists = await db.any(
|
|
63
|
-
`SELECT EXISTS (
|
|
64
|
-
SELECT FROM information_schema.tables
|
|
65
|
-
WHERE table_schema = '_realtime' AND table_name = 'extensions'
|
|
66
|
-
) as exists`
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
expect(Array.isArray(exists)).toBe(true);
|
|
70
|
-
if (exists[0]?.exists === false) {
|
|
71
|
-
expect(exists[0].exists).toBe(false);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
expect(exists[0].exists).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should verify service_role can query extensions table structure', async () => {
|
|
78
|
-
if (!tableExists) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
db.setContext({ role: 'service_role' });
|
|
83
|
-
|
|
84
|
-
// query table column structure
|
|
85
|
-
const columns = await db.any(
|
|
86
|
-
`SELECT column_name, data_type
|
|
87
|
-
FROM information_schema.columns
|
|
88
|
-
WHERE table_schema = '_realtime' AND table_name = 'extensions'
|
|
89
|
-
ORDER BY ordinal_position`
|
|
90
|
-
);
|
|
91
|
-
|
|
92
|
-
expect(Array.isArray(columns)).toBe(true);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('should prevent anon from accessing extensions table', async () => {
|
|
96
|
-
if (!tableExists) {
|
|
97
|
-
return;
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// clear context to anon role
|
|
101
|
-
db.clearContext();
|
|
102
|
-
|
|
103
|
-
// anon should not be able to access _realtime.extensions (rls blocks)
|
|
104
|
-
const result = await db.any(
|
|
105
|
-
`SELECT * FROM _realtime.extensions LIMIT 1`
|
|
106
|
-
);
|
|
107
|
-
|
|
108
|
-
// rls should block access, result should be empty
|
|
109
|
-
expect(result.length).toBe(0);
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('should verify table has proper grants via information_schema', async () => {
|
|
113
|
-
if (!tableExists) {
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
db.setContext({ role: 'service_role' });
|
|
118
|
-
|
|
119
|
-
// check table privileges/grants
|
|
120
|
-
const grants = await db.any(
|
|
121
|
-
`SELECT grantee, privilege_type
|
|
122
|
-
FROM information_schema.table_privileges
|
|
123
|
-
WHERE table_schema = '_realtime' AND table_name = 'extensions'`
|
|
124
|
-
);
|
|
125
|
-
|
|
126
|
-
expect(Array.isArray(grants)).toBe(true);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
@@ -1,160 +0,0 @@
|
|
|
1
|
-
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
-
|
|
3
|
-
let pg: PgTestClient;
|
|
4
|
-
let db: PgTestClient;
|
|
5
|
-
let teardown: () => Promise<void>;
|
|
6
|
-
|
|
7
|
-
let tableExists = false;
|
|
8
|
-
|
|
9
|
-
beforeAll(async () => {
|
|
10
|
-
({ pg, db, teardown } = await getConnections());
|
|
11
|
-
|
|
12
|
-
// verify _realtime schema exists (optional schema)
|
|
13
|
-
const realtimeSchemaExists = await pg.any(
|
|
14
|
-
`SELECT EXISTS (
|
|
15
|
-
SELECT FROM information_schema.schemata
|
|
16
|
-
WHERE schema_name = '_realtime'
|
|
17
|
-
) as exists`
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
if (realtimeSchemaExists[0]?.exists === true) {
|
|
21
|
-
// grant access to _realtime schema for testing
|
|
22
|
-
await pg.any(
|
|
23
|
-
`GRANT USAGE ON SCHEMA _realtime TO public;
|
|
24
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO service_role;
|
|
25
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO authenticated;
|
|
26
|
-
GRANT SELECT ON ALL TABLES IN SCHEMA _realtime TO anon;
|
|
27
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO service_role;
|
|
28
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO authenticated;
|
|
29
|
-
ALTER DEFAULT PRIVILEGES IN SCHEMA _realtime GRANT SELECT ON TABLES TO anon;`,
|
|
30
|
-
[]
|
|
31
|
-
);
|
|
32
|
-
|
|
33
|
-
// check if _realtime.schema_migrations table exists (using pg in beforeAll only)
|
|
34
|
-
const exists = await pg.any(
|
|
35
|
-
`SELECT EXISTS (
|
|
36
|
-
SELECT FROM information_schema.tables
|
|
37
|
-
WHERE table_schema = '_realtime' AND table_name = 'schema_migrations'
|
|
38
|
-
) as exists`
|
|
39
|
-
);
|
|
40
|
-
tableExists = exists[0]?.exists === true;
|
|
41
|
-
}
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
afterAll(async () => {
|
|
45
|
-
await teardown();
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
beforeEach(async () => {
|
|
49
|
-
await db.beforeEach();
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
afterEach(async () => {
|
|
53
|
-
await db.afterEach();
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
describe('tutorial: _realtime schema_migrations table access', () => {
|
|
57
|
-
|
|
58
|
-
it('should verify schema_migrations table exists', async () => {
|
|
59
|
-
db.setContext({ role: 'service_role' });
|
|
60
|
-
|
|
61
|
-
// verify table exists in information schema
|
|
62
|
-
const exists = await db.any(
|
|
63
|
-
`SELECT EXISTS (
|
|
64
|
-
SELECT FROM information_schema.tables
|
|
65
|
-
WHERE table_schema = '_realtime' AND table_name = 'schema_migrations'
|
|
66
|
-
) as exists`
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
expect(Array.isArray(exists)).toBe(true);
|
|
70
|
-
if (exists[0]?.exists === false) {
|
|
71
|
-
expect(exists[0].exists).toBe(false);
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
expect(exists[0].exists).toBe(true);
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
it('should verify service_role can read migration records', async () => {
|
|
78
|
-
if (!tableExists) {
|
|
79
|
-
return;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
db.setContext({ role: 'service_role' });
|
|
83
|
-
|
|
84
|
-
// service_role should be able to query schema_migrations
|
|
85
|
-
const migrations = await db.any(
|
|
86
|
-
`SELECT * FROM _realtime.schema_migrations`
|
|
87
|
-
);
|
|
88
|
-
|
|
89
|
-
expect(Array.isArray(migrations)).toBe(true);
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
it('should verify table has version column', async () => {
|
|
93
|
-
if (!tableExists) {
|
|
94
|
-
return;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
db.setContext({ role: 'service_role' });
|
|
98
|
-
|
|
99
|
-
// check for version column in schema_migrations table
|
|
100
|
-
const columns = await db.any(
|
|
101
|
-
`SELECT column_name, data_type
|
|
102
|
-
FROM information_schema.columns
|
|
103
|
-
WHERE table_schema = '_realtime' AND table_name = 'schema_migrations'
|
|
104
|
-
AND column_name = 'version'`
|
|
105
|
-
);
|
|
106
|
-
|
|
107
|
-
expect(Array.isArray(columns)).toBe(true);
|
|
108
|
-
if (columns.length > 0) {
|
|
109
|
-
expect(columns[0].column_name).toBe('version');
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
|
|
113
|
-
it('should prevent authenticated users from accessing schema_migrations without proper permissions', async () => {
|
|
114
|
-
if (!tableExists) {
|
|
115
|
-
return;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// create a test user as admin using db with service_role context
|
|
119
|
-
// using auth.users (real supabase table) instead of rls_test.user_profiles (fake test table)
|
|
120
|
-
db.setContext({ role: 'service_role' });
|
|
121
|
-
const user = await db.one(
|
|
122
|
-
`INSERT INTO auth.users (id, email)
|
|
123
|
-
VALUES (gen_random_uuid(), $1)
|
|
124
|
-
RETURNING id`,
|
|
125
|
-
['realtime-migrations-test@example.com']
|
|
126
|
-
);
|
|
127
|
-
|
|
128
|
-
// set context to simulate authenticated user
|
|
129
|
-
db.setContext({
|
|
130
|
-
role: 'authenticated',
|
|
131
|
-
'request.jwt.claim.sub': user.id
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
// authenticated users should not be able to access schema_migrations (rls blocks)
|
|
135
|
-
const result = await db.any(
|
|
136
|
-
`SELECT * FROM _realtime.schema_migrations LIMIT 1`
|
|
137
|
-
);
|
|
138
|
-
|
|
139
|
-
// rls should block access, result should be empty
|
|
140
|
-
expect(result.length).toBe(0);
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
it('should prevent anon from reading migrations', async () => {
|
|
144
|
-
if (!tableExists) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
|
|
148
|
-
// clear context to anon role
|
|
149
|
-
db.clearContext();
|
|
150
|
-
|
|
151
|
-
// anon should not be able to access schema_migrations (rls blocks)
|
|
152
|
-
const result = await db.any(
|
|
153
|
-
`SELECT * FROM _realtime.schema_migrations LIMIT 1`
|
|
154
|
-
);
|
|
155
|
-
|
|
156
|
-
// rls should block access, result should be empty
|
|
157
|
-
expect(result.length).toBe(0);
|
|
158
|
-
});
|
|
159
|
-
});
|
|
160
|
-
|