@pgpm/supabase 0.4.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 +21 -0
- package/Makefile +7 -0
- package/README.md +37 -0
- package/__tests__/_realtime/_realtime-extensions.test.ts +129 -0
- package/__tests__/_realtime/_realtime-schema-migrations.test.ts +160 -0
- package/__tests__/_realtime/_realtime-tenants.test.ts +163 -0
- package/__tests__/auth/auth.audit-log-entries.test.ts +148 -0
- package/__tests__/auth/auth.flow-state.test.ts +114 -0
- package/__tests__/auth/auth.identities.test.ts +149 -0
- package/__tests__/auth/auth.instances.test.ts +134 -0
- package/__tests__/auth/auth.mfa-amr-claims.test.ts +142 -0
- package/__tests__/auth/auth.mfa-challenges.test.ts +117 -0
- package/__tests__/auth/auth.mfa-factors.test.ts +118 -0
- package/__tests__/auth/auth.oauth-authorizations.test.ts +123 -0
- package/__tests__/auth/auth.oauth-clients.test.ts +120 -0
- package/__tests__/auth/auth.oauth-consents.test.ts +122 -0
- package/__tests__/auth/auth.one-time-tokens.test.ts +194 -0
- package/__tests__/auth/auth.refresh-tokens.test.ts +134 -0
- package/__tests__/auth/auth.saml-providers.test.ts +154 -0
- package/__tests__/auth/auth.saml-relay-states.test.ts +145 -0
- package/__tests__/auth/auth.schema-migrations.test.ts +131 -0
- package/__tests__/auth/auth.sessions.test.ts +140 -0
- package/__tests__/auth/auth.sso-domains.test.ts +137 -0
- package/__tests__/auth/auth.sso-providers.test.ts +136 -0
- package/__tests__/auth/auth.users.test.ts +314 -0
- package/__tests__/conditional.test.ts +229 -0
- package/__tests__/functions.test.ts +252 -0
- package/__tests__/jsonb.test.ts +237 -0
- package/__tests__/net/net.http-request-queue.test.ts +159 -0
- package/__tests__/net/net.http-response.test.ts +163 -0
- package/__tests__/realtime/realtime.messages-2025-10-31.test.ts +168 -0
- package/__tests__/realtime/realtime.messages.test.ts +162 -0
- package/__tests__/realtime/realtime.schema-migrations.test.ts +139 -0
- package/__tests__/realtime/realtime.subscription.test.ts +159 -0
- package/__tests__/storage/storage.buckets-analytics.test.ts +134 -0
- package/__tests__/storage/storage.buckets.test.ts +146 -0
- package/__tests__/storage/storage.iceberg-namespaces.test.ts +132 -0
- package/__tests__/storage/storage.iceberg-tables.test.ts +142 -0
- package/__tests__/storage/storage.migrations.test.ts +133 -0
- package/__tests__/storage/storage.objects.test.ts +160 -0
- package/__tests__/storage/storage.prefixes.test.ts +138 -0
- package/__tests__/storage/storage.s3-multipart-uploads-parts.test.ts +133 -0
- package/__tests__/storage/storage.s3-multipart-uploads.test.ts +128 -0
- package/__tests__/supabase-functions/supabase.functions-hooks.test.ts +125 -0
- package/__tests__/supabase-functions/supabase.functions-migrations.test.ts +213 -0
- package/__tests__/temporal.test.ts +225 -0
- package/__tests__/vault-secrets.test.ts +145 -0
- package/deploy/LICENSE +9 -0
- package/deploy/supabase.sql +2271 -0
- package/jest.config.js +15 -0
- package/launchql.plan +4 -0
- package/package.json +23 -0
- package/revert/supabase.sql +3 -0
- package/sql/supabase--0.0.1.sql +1368 -0
- package/src/index.ts +3 -0
- package/supabase.control +7 -0
- package/verify/supabase.sql +3 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let db: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
beforeAll(async () => {
|
|
8
|
+
({ pg, db, teardown } = await getConnections());
|
|
9
|
+
|
|
10
|
+
// verify auth schema exists
|
|
11
|
+
const authSchemaExists = await pg.any(
|
|
12
|
+
`SELECT EXISTS (
|
|
13
|
+
SELECT FROM information_schema.schemata
|
|
14
|
+
WHERE schema_name = 'auth'
|
|
15
|
+
) as exists`
|
|
16
|
+
);
|
|
17
|
+
expect(authSchemaExists[0].exists).toBe(true);
|
|
18
|
+
|
|
19
|
+
// grants for reads
|
|
20
|
+
await pg.any(
|
|
21
|
+
`GRANT USAGE ON SCHEMA auth TO public;
|
|
22
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
|
|
23
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
|
|
24
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
|
|
25
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
|
|
26
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
|
|
27
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
|
|
28
|
+
[]
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// assert mfa_factors exists (fail fast)
|
|
32
|
+
const exists = await pg.any(
|
|
33
|
+
`SELECT EXISTS (
|
|
34
|
+
SELECT FROM information_schema.tables
|
|
35
|
+
WHERE table_schema = 'auth' AND table_name = 'mfa_factors'
|
|
36
|
+
) as exists`
|
|
37
|
+
);
|
|
38
|
+
expect(exists[0].exists).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterAll(async () => {
|
|
42
|
+
await teardown();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
await db.beforeEach();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await db.afterEach();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('tutorial: auth mfa_factors table access', () => {
|
|
54
|
+
|
|
55
|
+
it('should verify mfa_factors table exists', async () => {
|
|
56
|
+
db.setContext({ role: 'service_role' });
|
|
57
|
+
|
|
58
|
+
// verify table exists in information schema
|
|
59
|
+
const exists = await db.any(
|
|
60
|
+
`SELECT EXISTS (
|
|
61
|
+
SELECT FROM information_schema.tables
|
|
62
|
+
WHERE table_schema = 'auth' AND table_name = 'mfa_factors'
|
|
63
|
+
) as exists`
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
67
|
+
expect(exists[0].exists).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should verify service_role can read mfa_factors', async () => {
|
|
71
|
+
db.setContext({ role: 'service_role' });
|
|
72
|
+
|
|
73
|
+
// service_role should be able to query mfa_factors
|
|
74
|
+
const factors = await db.any(
|
|
75
|
+
`SELECT id, user_id, friendly_name, factor_type
|
|
76
|
+
FROM auth.mfa_factors
|
|
77
|
+
LIMIT 10`
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(Array.isArray(factors)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should verify unique index on (friendly_name, user_id)', async () => {
|
|
84
|
+
db.setContext({ role: 'service_role' });
|
|
85
|
+
const indexes = await db.any(
|
|
86
|
+
`SELECT indexname
|
|
87
|
+
FROM pg_indexes
|
|
88
|
+
WHERE schemaname = 'auth' AND tablename = 'mfa_factors'`
|
|
89
|
+
);
|
|
90
|
+
expect(Array.isArray(indexes)).toBe(true);
|
|
91
|
+
if (indexes.length > 0) {
|
|
92
|
+
const hasUniqueIdx = indexes.some((r: any) => r.indexname === 'mfa_factors_user_friendly_name_unique');
|
|
93
|
+
expect(typeof hasUniqueIdx).toBe('boolean');
|
|
94
|
+
}
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('should verify anon access to mfa_factors based on rls', async () => {
|
|
98
|
+
// check rls status
|
|
99
|
+
db.setContext({ role: 'service_role' });
|
|
100
|
+
const rlsStatus = await db.any(
|
|
101
|
+
`SELECT c.relrowsecurity
|
|
102
|
+
FROM pg_class c
|
|
103
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
104
|
+
WHERE n.nspname = 'auth' AND c.relname = 'mfa_factors'`
|
|
105
|
+
);
|
|
106
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
107
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
108
|
+
|
|
109
|
+
// anon query
|
|
110
|
+
db.clearContext();
|
|
111
|
+
const result = await db.any(`SELECT * FROM auth.mfa_factors LIMIT 1`);
|
|
112
|
+
expect(Array.isArray(result)).toBe(true);
|
|
113
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
114
|
+
expect(result.length).toBe(0);
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
});
|
|
118
|
+
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let db: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
beforeAll(async () => {
|
|
8
|
+
({ pg, db, teardown } = await getConnections());
|
|
9
|
+
|
|
10
|
+
// verify auth schema exists
|
|
11
|
+
const authSchemaExists = await pg.any(
|
|
12
|
+
`SELECT EXISTS (
|
|
13
|
+
SELECT FROM information_schema.schemata
|
|
14
|
+
WHERE schema_name = 'auth'
|
|
15
|
+
) as exists`
|
|
16
|
+
);
|
|
17
|
+
expect(authSchemaExists[0].exists).toBe(true);
|
|
18
|
+
|
|
19
|
+
// grants
|
|
20
|
+
await pg.any(
|
|
21
|
+
`GRANT USAGE ON SCHEMA auth TO public;
|
|
22
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
|
|
23
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
|
|
24
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
|
|
25
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
|
|
26
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
|
|
27
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
|
|
28
|
+
[]
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// assert table exists
|
|
32
|
+
const exists = await pg.any(
|
|
33
|
+
`SELECT EXISTS (
|
|
34
|
+
SELECT FROM information_schema.tables
|
|
35
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_authorizations'
|
|
36
|
+
) as exists`
|
|
37
|
+
);
|
|
38
|
+
expect(exists[0].exists).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterAll(async () => {
|
|
42
|
+
await teardown();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
await db.beforeEach();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await db.afterEach();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('tutorial: auth oauth_authorizations table access', () => {
|
|
54
|
+
|
|
55
|
+
it('should verify oauth_authorizations table exists', async () => {
|
|
56
|
+
db.setContext({ role: 'service_role' });
|
|
57
|
+
|
|
58
|
+
// verify table exists in information schema
|
|
59
|
+
const exists = await db.any(
|
|
60
|
+
`SELECT EXISTS (
|
|
61
|
+
SELECT FROM information_schema.tables
|
|
62
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_authorizations'
|
|
63
|
+
) as exists`
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
67
|
+
expect(exists[0].exists).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should verify service_role can query oauth_authorizations structure', async () => {
|
|
71
|
+
db.setContext({ role: 'service_role' });
|
|
72
|
+
const columns = await db.any(
|
|
73
|
+
`SELECT column_name, data_type
|
|
74
|
+
FROM information_schema.columns
|
|
75
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_authorizations'
|
|
76
|
+
ORDER BY ordinal_position`
|
|
77
|
+
);
|
|
78
|
+
expect(Array.isArray(columns)).toBe(true);
|
|
79
|
+
expect(columns.length).toBeGreaterThan(0);
|
|
80
|
+
const names = columns.map((c: any) => c.column_name);
|
|
81
|
+
expect(names).toContain('id');
|
|
82
|
+
expect(names).toContain('client_id');
|
|
83
|
+
expect(names).toContain('authorization_id');
|
|
84
|
+
expect(names).toContain('status');
|
|
85
|
+
expect(names).toContain('expires_at');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('should verify unique constraints and indexes', async () => {
|
|
89
|
+
db.setContext({ role: 'service_role' });
|
|
90
|
+
const uniques = await db.any(
|
|
91
|
+
`SELECT constraint_name
|
|
92
|
+
FROM information_schema.table_constraints
|
|
93
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_authorizations' AND constraint_type = 'UNIQUE'`
|
|
94
|
+
);
|
|
95
|
+
expect(Array.isArray(uniques)).toBe(true);
|
|
96
|
+
const indexes = await db.any(
|
|
97
|
+
`SELECT indexname
|
|
98
|
+
FROM pg_indexes
|
|
99
|
+
WHERE schemaname = 'auth' AND tablename = 'oauth_authorizations'`
|
|
100
|
+
);
|
|
101
|
+
expect(Array.isArray(indexes)).toBe(true);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should verify anon access to oauth_authorizations based on rls', async () => {
|
|
105
|
+
db.setContext({ role: 'service_role' });
|
|
106
|
+
const rlsStatus = await db.any(
|
|
107
|
+
`SELECT c.relrowsecurity
|
|
108
|
+
FROM pg_class c
|
|
109
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
110
|
+
WHERE n.nspname = 'auth' AND c.relname = 'oauth_authorizations'`
|
|
111
|
+
);
|
|
112
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
113
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
114
|
+
|
|
115
|
+
db.clearContext();
|
|
116
|
+
const result = await db.any(`SELECT * FROM auth.oauth_authorizations LIMIT 1`);
|
|
117
|
+
expect(Array.isArray(result)).toBe(true);
|
|
118
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
119
|
+
expect(result.length).toBe(0);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let db: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
beforeAll(async () => {
|
|
8
|
+
({ pg, db, teardown } = await getConnections());
|
|
9
|
+
|
|
10
|
+
// verify auth schema exists
|
|
11
|
+
const authSchemaExists = await pg.any(
|
|
12
|
+
`SELECT EXISTS (
|
|
13
|
+
SELECT FROM information_schema.schemata
|
|
14
|
+
WHERE schema_name = 'auth'
|
|
15
|
+
) as exists`
|
|
16
|
+
);
|
|
17
|
+
expect(authSchemaExists[0].exists).toBe(true);
|
|
18
|
+
|
|
19
|
+
// grants for reads
|
|
20
|
+
await pg.any(
|
|
21
|
+
`GRANT USAGE ON SCHEMA auth TO public;
|
|
22
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
|
|
23
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
|
|
24
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
|
|
25
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
|
|
26
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
|
|
27
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
|
|
28
|
+
[]
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// assert table exists (fail fast)
|
|
32
|
+
const exists = await pg.any(
|
|
33
|
+
`SELECT EXISTS (
|
|
34
|
+
SELECT FROM information_schema.tables
|
|
35
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_clients'
|
|
36
|
+
) as exists`
|
|
37
|
+
);
|
|
38
|
+
expect(exists[0].exists).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterAll(async () => {
|
|
42
|
+
await teardown();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
await db.beforeEach();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await db.afterEach();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('tutorial: auth oauth_clients table access', () => {
|
|
54
|
+
|
|
55
|
+
it('should verify oauth_clients table exists', async () => {
|
|
56
|
+
db.setContext({ role: 'service_role' });
|
|
57
|
+
|
|
58
|
+
// verify table exists in information schema
|
|
59
|
+
const exists = await db.any(
|
|
60
|
+
`SELECT EXISTS (
|
|
61
|
+
SELECT FROM information_schema.tables
|
|
62
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_clients'
|
|
63
|
+
) as exists`
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
67
|
+
expect(exists[0].exists).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should verify service_role can read oauth_clients', async () => {
|
|
71
|
+
db.setContext({ role: 'service_role' });
|
|
72
|
+
|
|
73
|
+
// service_role should be able to query oauth_clients
|
|
74
|
+
const clients = await db.any(
|
|
75
|
+
`SELECT id, client_id, client_secret_hash, client_name, created_at, updated_at
|
|
76
|
+
FROM auth.oauth_clients
|
|
77
|
+
LIMIT 10`
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(Array.isArray(clients)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should verify table has unique constraint on client_id', async () => {
|
|
84
|
+
db.setContext({ role: 'service_role' });
|
|
85
|
+
|
|
86
|
+
// check for unique constraint on client_id via constraints
|
|
87
|
+
const constraints = await db.any(
|
|
88
|
+
`SELECT constraint_name
|
|
89
|
+
FROM information_schema.table_constraints
|
|
90
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_clients' AND constraint_type = 'UNIQUE'`
|
|
91
|
+
);
|
|
92
|
+
expect(Array.isArray(constraints)).toBe(true);
|
|
93
|
+
// and the index exists
|
|
94
|
+
const indexes = await db.any(
|
|
95
|
+
`SELECT indexname FROM pg_indexes WHERE schemaname = 'auth' AND tablename = 'oauth_clients'`
|
|
96
|
+
);
|
|
97
|
+
expect(Array.isArray(indexes)).toBe(true);
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('should verify anon access to oauth_clients based on rls', async () => {
|
|
101
|
+
// rls status
|
|
102
|
+
db.setContext({ role: 'service_role' });
|
|
103
|
+
const rlsStatus = await db.any(
|
|
104
|
+
`SELECT c.relrowsecurity
|
|
105
|
+
FROM pg_class c
|
|
106
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
107
|
+
WHERE n.nspname = 'auth' AND c.relname = 'oauth_clients'`
|
|
108
|
+
);
|
|
109
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
110
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
111
|
+
|
|
112
|
+
db.clearContext();
|
|
113
|
+
const result = await db.any(`SELECT * FROM auth.oauth_clients LIMIT 1`);
|
|
114
|
+
expect(Array.isArray(result)).toBe(true);
|
|
115
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
116
|
+
expect(result.length).toBe(0);
|
|
117
|
+
}
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let db: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
beforeAll(async () => {
|
|
8
|
+
({ pg, db, teardown } = await getConnections());
|
|
9
|
+
|
|
10
|
+
// verify auth schema exists
|
|
11
|
+
const authSchemaExists = await pg.any(
|
|
12
|
+
`SELECT EXISTS (
|
|
13
|
+
SELECT FROM information_schema.schemata
|
|
14
|
+
WHERE schema_name = 'auth'
|
|
15
|
+
) as exists`
|
|
16
|
+
);
|
|
17
|
+
expect(authSchemaExists[0].exists).toBe(true);
|
|
18
|
+
|
|
19
|
+
// grant access to auth schema for testing
|
|
20
|
+
await pg.any(
|
|
21
|
+
`GRANT USAGE ON SCHEMA auth TO public;
|
|
22
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
|
|
23
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
|
|
24
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
|
|
25
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
|
|
26
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
|
|
27
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
|
|
28
|
+
[]
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
// assert table exists
|
|
32
|
+
const exists = await pg.any(
|
|
33
|
+
`SELECT EXISTS (
|
|
34
|
+
SELECT FROM information_schema.tables
|
|
35
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_consents'
|
|
36
|
+
) as exists`
|
|
37
|
+
);
|
|
38
|
+
expect(exists[0].exists).toBe(true);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterAll(async () => {
|
|
42
|
+
await teardown();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
beforeEach(async () => {
|
|
46
|
+
await db.beforeEach();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
afterEach(async () => {
|
|
50
|
+
await db.afterEach();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('tutorial: auth oauth_consents table access', () => {
|
|
54
|
+
|
|
55
|
+
it('should verify oauth_consents table exists', async () => {
|
|
56
|
+
db.setContext({ role: 'service_role' });
|
|
57
|
+
|
|
58
|
+
// verify table exists in information schema
|
|
59
|
+
const exists = await db.any(
|
|
60
|
+
`SELECT EXISTS (
|
|
61
|
+
SELECT FROM information_schema.tables
|
|
62
|
+
WHERE table_schema = 'auth' AND table_name = 'oauth_consents'
|
|
63
|
+
) as exists`
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
67
|
+
expect(exists[0].exists).toBe(true);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('should verify service_role can query oauth_consents', async () => {
|
|
71
|
+
db.setContext({ role: 'service_role' });
|
|
72
|
+
|
|
73
|
+
// service_role should be able to query oauth_consents
|
|
74
|
+
const consents = await db.any(
|
|
75
|
+
`SELECT id, user_id, client_id, scopes
|
|
76
|
+
FROM auth.oauth_consents
|
|
77
|
+
LIMIT 10`
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
expect(Array.isArray(consents)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should verify foreign keys to users and oauth_clients', async () => {
|
|
84
|
+
db.setContext({ role: 'service_role' });
|
|
85
|
+
|
|
86
|
+
const fks = await db.any(
|
|
87
|
+
`SELECT tc.constraint_name, ccu.table_name AS foreign_table_name
|
|
88
|
+
FROM information_schema.table_constraints AS tc
|
|
89
|
+
JOIN information_schema.constraint_column_usage AS ccu
|
|
90
|
+
ON ccu.constraint_name = tc.constraint_name AND ccu.table_schema = tc.table_schema
|
|
91
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
|
92
|
+
AND tc.table_schema = 'auth'
|
|
93
|
+
AND tc.table_name = 'oauth_consents'`
|
|
94
|
+
);
|
|
95
|
+
expect(Array.isArray(fks)).toBe(true);
|
|
96
|
+
if (fks.length > 0) {
|
|
97
|
+
const tables = new Set(fks.map((r: any) => r.foreign_table_name));
|
|
98
|
+
expect(tables.has('users')).toBe(true);
|
|
99
|
+
expect(tables.has('oauth_clients')).toBe(true);
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
it('should verify anon access to oauth_consents based on rls', async () => {
|
|
104
|
+
db.setContext({ role: 'service_role' });
|
|
105
|
+
const rlsStatus = await db.any(
|
|
106
|
+
`SELECT c.relrowsecurity
|
|
107
|
+
FROM pg_class c
|
|
108
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
109
|
+
WHERE n.nspname = 'auth' AND c.relname = 'oauth_consents'`
|
|
110
|
+
);
|
|
111
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
112
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
113
|
+
|
|
114
|
+
db.clearContext();
|
|
115
|
+
const result = await db.any(`SELECT * FROM auth.oauth_consents LIMIT 1`);
|
|
116
|
+
expect(Array.isArray(result)).toBe(true);
|
|
117
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
118
|
+
expect(result.length).toBe(0);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import { getConnections, PgTestClient } from 'supabase-test';
|
|
2
|
+
|
|
3
|
+
let pg: PgTestClient;
|
|
4
|
+
let db: PgTestClient;
|
|
5
|
+
let teardown: () => Promise<void>;
|
|
6
|
+
|
|
7
|
+
beforeAll(async () => {
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
({ pg, db, teardown } = await getConnections());
|
|
11
|
+
|
|
12
|
+
// verify auth schema exists
|
|
13
|
+
const authSchemaExists = await pg.any(
|
|
14
|
+
`SELECT EXISTS (
|
|
15
|
+
SELECT FROM information_schema.schemata
|
|
16
|
+
WHERE schema_name = 'auth'
|
|
17
|
+
) as exists`
|
|
18
|
+
);
|
|
19
|
+
expect(authSchemaExists[0].exists).toBe(true);
|
|
20
|
+
|
|
21
|
+
// grant access to auth schema for testing
|
|
22
|
+
await pg.any(
|
|
23
|
+
`GRANT USAGE ON SCHEMA auth TO public;
|
|
24
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO service_role;
|
|
25
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO authenticated;
|
|
26
|
+
GRANT SELECT ON ALL TABLES IN SCHEMA auth TO anon;
|
|
27
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO service_role;
|
|
28
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO authenticated;
|
|
29
|
+
ALTER DEFAULT PRIVILEGES IN SCHEMA auth GRANT SELECT ON TABLES TO anon;`,
|
|
30
|
+
[]
|
|
31
|
+
);
|
|
32
|
+
|
|
33
|
+
// assert auth.one_time_tokens exists
|
|
34
|
+
const exists = await pg.any(
|
|
35
|
+
`SELECT EXISTS (
|
|
36
|
+
SELECT FROM information_schema.tables
|
|
37
|
+
WHERE table_schema = 'auth' AND table_name = 'one_time_tokens'
|
|
38
|
+
) as exists`
|
|
39
|
+
);
|
|
40
|
+
expect(exists[0].exists).toBe(true);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
afterAll(async () => {
|
|
44
|
+
await teardown();
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
beforeEach(async () => {
|
|
48
|
+
await db.beforeEach();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
afterEach(async () => {
|
|
52
|
+
await db.afterEach();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe('tutorial: auth one_time_tokens table access', () => {
|
|
56
|
+
|
|
57
|
+
it('should verify one_time_tokens table exists', async () => {
|
|
58
|
+
db.setContext({ role: 'service_role' });
|
|
59
|
+
|
|
60
|
+
// verify table exists in information schema
|
|
61
|
+
const exists = await db.any(
|
|
62
|
+
`SELECT EXISTS (
|
|
63
|
+
SELECT FROM information_schema.tables
|
|
64
|
+
WHERE table_schema = 'auth' AND table_name = 'one_time_tokens'
|
|
65
|
+
) as exists`
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
expect(Array.isArray(exists)).toBe(true);
|
|
69
|
+
expect(exists[0].exists).toBe(true);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('should verify service_role can read one_time_tokens', async () => {
|
|
73
|
+
db.setContext({ role: 'service_role' });
|
|
74
|
+
|
|
75
|
+
const rows = await db.any(
|
|
76
|
+
`SELECT id, user_id, token_type, token_hash, relates_to, created_at, updated_at
|
|
77
|
+
FROM auth.one_time_tokens
|
|
78
|
+
LIMIT 10`
|
|
79
|
+
);
|
|
80
|
+
expect(Array.isArray(rows)).toBe(true);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('should verify table has indexes on token_hash and relates_to', async () => {
|
|
84
|
+
db.setContext({ role: 'service_role' });
|
|
85
|
+
|
|
86
|
+
// check for indexes on the table
|
|
87
|
+
const indexes = await db.any(
|
|
88
|
+
`SELECT indexname, indexdef
|
|
89
|
+
FROM pg_indexes
|
|
90
|
+
WHERE schemaname = 'auth' AND tablename = 'one_time_tokens'`
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
expect(Array.isArray(indexes)).toBe(true);
|
|
94
|
+
if (indexes.length > 0) {
|
|
95
|
+
const defs = indexes.map((r: any) => r.indexdef.toLowerCase()).join(' ');
|
|
96
|
+
expect(defs.includes('token_hash')).toBe(true);
|
|
97
|
+
expect(defs.includes('relates_to')).toBe(true);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('should verify table has primary key on id', async () => {
|
|
102
|
+
db.setContext({ role: 'service_role' });
|
|
103
|
+
const pk = await db.any(
|
|
104
|
+
`SELECT constraint_name
|
|
105
|
+
FROM information_schema.table_constraints
|
|
106
|
+
WHERE table_schema = 'auth' AND table_name = 'one_time_tokens'
|
|
107
|
+
AND constraint_type = 'PRIMARY KEY'`
|
|
108
|
+
);
|
|
109
|
+
expect(Array.isArray(pk)).toBe(true);
|
|
110
|
+
if (pk.length > 0) {
|
|
111
|
+
expect(pk[0].constraint_name).toBeDefined();
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('should verify foreign key to auth.users on user_id', async () => {
|
|
116
|
+
db.setContext({ role: 'service_role' });
|
|
117
|
+
const fks = await db.any(
|
|
118
|
+
`SELECT tc.constraint_name, ccu.table_name AS foreign_table_name, ccu.column_name
|
|
119
|
+
FROM information_schema.table_constraints AS tc
|
|
120
|
+
JOIN information_schema.constraint_column_usage AS ccu
|
|
121
|
+
ON ccu.constraint_name = tc.constraint_name
|
|
122
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
|
123
|
+
AND tc.table_schema = 'auth'
|
|
124
|
+
AND tc.table_name = 'one_time_tokens'`
|
|
125
|
+
);
|
|
126
|
+
expect(Array.isArray(fks)).toBe(true);
|
|
127
|
+
if (fks.length > 0) {
|
|
128
|
+
const foreignTables = fks.map((r: any) => r.foreign_table_name);
|
|
129
|
+
expect(foreignTables.includes('users')).toBe(true);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it('should verify unique constraint on (user_id, token_type)', async () => {
|
|
134
|
+
db.setContext({ role: 'service_role' });
|
|
135
|
+
const uniques = await db.any(
|
|
136
|
+
`SELECT indexname, indexdef
|
|
137
|
+
FROM pg_indexes
|
|
138
|
+
WHERE schemaname = 'auth' AND tablename = 'one_time_tokens'`
|
|
139
|
+
);
|
|
140
|
+
expect(Array.isArray(uniques)).toBe(true);
|
|
141
|
+
if (uniques.length > 0) {
|
|
142
|
+
const defs = uniques.map((r: any) => r.indexdef.toLowerCase()).join(' ');
|
|
143
|
+
expect(defs.includes('(user_id, token_type)')).toBe(true);
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('should verify enum type one_time_token_type exists with values', async () => {
|
|
148
|
+
db.setContext({ role: 'service_role' });
|
|
149
|
+
const typeExists = await db.any(
|
|
150
|
+
`SELECT EXISTS (
|
|
151
|
+
SELECT 1 FROM pg_type WHERE typname = 'one_time_token_type'
|
|
152
|
+
) AS exists`
|
|
153
|
+
);
|
|
154
|
+
expect(typeExists[0].exists).toBe(true);
|
|
155
|
+
const labels = await db.any(
|
|
156
|
+
`SELECT e.enumlabel
|
|
157
|
+
FROM pg_type t
|
|
158
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
159
|
+
WHERE t.typname = 'one_time_token_type'`
|
|
160
|
+
);
|
|
161
|
+
const vals = labels.map((r: any) => r.enumlabel);
|
|
162
|
+
expect(vals).toEqual(expect.arrayContaining([
|
|
163
|
+
'confirmation_token',
|
|
164
|
+
'reauthentication_token',
|
|
165
|
+
'recovery_token',
|
|
166
|
+
'email_change_token_new',
|
|
167
|
+
'email_change_token_current',
|
|
168
|
+
'phone_change_token'
|
|
169
|
+
]));
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('should verify anon access to one_time_tokens based on rls', async () => {
|
|
173
|
+
// check rls status
|
|
174
|
+
db.setContext({ role: 'service_role' });
|
|
175
|
+
const rlsStatus = await db.any(
|
|
176
|
+
`SELECT c.relrowsecurity
|
|
177
|
+
FROM pg_class c
|
|
178
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
179
|
+
WHERE n.nspname = 'auth' AND c.relname = 'one_time_tokens'`
|
|
180
|
+
);
|
|
181
|
+
expect(Array.isArray(rlsStatus)).toBe(true);
|
|
182
|
+
expect(rlsStatus.length).toBeGreaterThan(0);
|
|
183
|
+
|
|
184
|
+
// clear context to anon role
|
|
185
|
+
db.clearContext();
|
|
186
|
+
|
|
187
|
+
const result = await db.any(`SELECT * FROM auth.one_time_tokens LIMIT 1`);
|
|
188
|
+
expect(Array.isArray(result)).toBe(true);
|
|
189
|
+
if (rlsStatus[0].relrowsecurity === true) {
|
|
190
|
+
expect(result.length).toBe(0);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
});
|
|
194
|
+
|