@marktiderman/genesis-supabase 0.6.2 → 0.8.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.
Files changed (35) hide show
  1. package/README.md +128 -7
  2. package/dist/blocks.d.ts +18 -1
  3. package/dist/blocks.js +1 -1
  4. package/dist/chunk-7SCMHKCR.js +33 -0
  5. package/dist/chunk-7SCMHKCR.js.map +1 -0
  6. package/dist/{chunk-ONBOLMXH.js → chunk-DYHH2G4A.js} +37 -3
  7. package/dist/chunk-DYHH2G4A.js.map +1 -0
  8. package/dist/chunk-XGAXPUFY.js +124 -0
  9. package/dist/chunk-XGAXPUFY.js.map +1 -0
  10. package/dist/{index-Ds0tO9WP.d.ts → index-D73YuwEY.d.ts} +4 -4
  11. package/dist/index.d.ts +2 -2
  12. package/dist/index.js +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/dist/jobs.d.ts +3 -1
  15. package/dist/jobs.js +48 -34
  16. package/dist/jobs.js.map +1 -1
  17. package/dist/native.d.ts +2 -2
  18. package/dist/runner.d.ts +179 -0
  19. package/dist/runner.js +3 -0
  20. package/dist/runner.js.map +1 -0
  21. package/dist/web-handoff-path-C_EEl_BC.d.ts +16 -0
  22. package/dist/web-handoff-server.d.ts +225 -0
  23. package/dist/web-handoff-server.js +262 -0
  24. package/dist/web-handoff-server.js.map +1 -0
  25. package/dist/web-handoff.d.ts +133 -0
  26. package/dist/web-handoff.js +104 -0
  27. package/dist/web-handoff.js.map +1 -0
  28. package/migrations/0009_genesis_owner_access.sql +89 -0
  29. package/migrations/0014_genesis_feature_registry_hardening.sql +124 -0
  30. package/migrations/0015_genesis_web_handoff.sql +270 -0
  31. package/migrations/tests/0009_genesis_owner_access.test.sql +70 -0
  32. package/migrations/tests/0014_genesis_feature_registry_hardening.test.sql +54 -0
  33. package/migrations/tests/0015_genesis_web_handoff.test.sql +203 -0
  34. package/package.json +88 -3
  35. package/dist/chunk-ONBOLMXH.js.map +0 -1
@@ -0,0 +1,270 @@
1
+ -- Genesis web handoff: continue on the web, already signed in
2
+ --
3
+ -- The database half of `@marktiderman/genesis-supabase/web-handoff/server`. A native app asks
4
+ -- the server for a link to a web page; the link opens that page signed in as the same person.
5
+ -- Each link is a row in private.genesis_web_handoffs, which keeps SHA-256 digests only, never a
6
+ -- token, so a copy of the table signs nobody in. A row moves through:
7
+ --
8
+ -- issued the app asked (genesis_web_handoff_issue): person, path, a 60-second expiry, the
9
+ -- app session it came from. At most `max` a person per window (SQLSTATE GS429),
10
+ -- counted under a per-person lock.
11
+ -- claimed the page opened it (genesis_web_handoff_claim), once and unexpired, and got a
12
+ -- continue nonce that lives 30 seconds. Nothing that signs anyone in exists yet.
13
+ -- continued the person chose Continue (genesis_web_handoff_continue), once, within the
14
+ -- nonce's life; only now does the server ask Supabase Auth for a sign-in token.
15
+ -- discarded the person chose Cancel (genesis_web_handoff_discard): the nonce is burnt.
16
+ -- bound the page reported the session it made (genesis_web_handoff_bind_session), so
17
+ -- signing out of the app can end exactly that web session
18
+ -- (genesis_web_handoff_revoke_sessions, which deletes those auth.sessions rows:
19
+ -- Supabase documents a session as signed out when its row is gone; its access
20
+ -- token still works until it expires).
21
+ --
22
+ -- Rows are also the log of handoffs (who, which path, when), kept 30 days, or longer while the
23
+ -- web session they made still exists.
24
+ --
25
+ -- Access: only service_role, through the functions below, granted by name. anon and
26
+ -- authenticated reach nothing: the table is in `private`, has row level security with no
27
+ -- policies, and every function also refuses any role but service_role
28
+ -- (`current_setting('role')`, which is what PostgREST set, not a JWT claim).
29
+ --
30
+ -- Account deletion: a person's handoffs go with their login (on delete cascade).
31
+ --
32
+ -- Use: copy this file into your app's `supabase/migrations/` under a new timestamped name.
33
+ -- Never edit it once applied; change it with a new migration. It stands alone.
34
+ --
35
+ -- Verify: the pgTAP test beside this file (`tests/0015_genesis_web_handoff.test.sql`).
36
+
37
+ create schema if not exists private;
38
+
39
+ create table private.genesis_web_handoffs (
40
+ id uuid primary key default gen_random_uuid(),
41
+ user_id uuid not null references auth.users (id) on delete cascade,
42
+ token_digest text not null unique check (token_digest ~ '^[0-9a-f]{64}$'),
43
+ path text not null check (char_length(path) between 1 and 2048 and left(path, 1) = '/' and left(path, 2) <> '//'),
44
+ created_at timestamptz not null default now(),
45
+ expires_at timestamptz not null,
46
+ used_at timestamptz,
47
+ source_session_id uuid,
48
+ nonce_digest text unique check (nonce_digest ~ '^[0-9a-f]{64}$'),
49
+ nonce_expires_at timestamptz,
50
+ continued_at timestamptz,
51
+ discarded_at timestamptz,
52
+ web_session_id uuid,
53
+ check (expires_at > created_at)
54
+ );
55
+
56
+ comment on table private.genesis_web_handoffs is
57
+ 'Native-to-web sign-in handoffs (genesis-supabase web-handoff): digests, never tokens. Service role only.';
58
+
59
+ create index genesis_web_handoffs_user_recent on private.genesis_web_handoffs (user_id, created_at desc);
60
+ create index genesis_web_handoffs_created on private.genesis_web_handoffs (created_at);
61
+ create index genesis_web_handoffs_source on private.genesis_web_handoffs (user_id, source_session_id) where web_session_id is not null;
62
+
63
+ alter table private.genesis_web_handoffs enable row level security;
64
+ revoke all on table private.genesis_web_handoffs from public, anon, authenticated;
65
+
66
+ -- Issue ------------------------------------------------------------------------------
67
+
68
+ create or replace function public.genesis_web_handoff_issue(
69
+ p_user_id uuid,
70
+ p_digest text,
71
+ p_path text,
72
+ p_ttl_seconds integer default 60,
73
+ p_max integer default 10,
74
+ p_window_seconds integer default 60,
75
+ p_source_session_id uuid default null
76
+ )
77
+ returns timestamptz
78
+ language plpgsql
79
+ volatile
80
+ security definer
81
+ set search_path = ''
82
+ as $$
83
+ declare
84
+ recent integer;
85
+ expires timestamptz;
86
+ begin
87
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
88
+ raise exception 'genesis_web_handoff_issue is for the server only' using errcode = '42501';
89
+ end if;
90
+ if p_ttl_seconds is null or p_ttl_seconds not between 10 and 300 then
91
+ raise exception 'p_ttl_seconds must be from 10 to 300' using errcode = '22023';
92
+ end if;
93
+ if p_max is null or p_max < 1 or p_window_seconds is null or p_window_seconds < 1 then
94
+ raise exception 'p_max and p_window_seconds must be positive' using errcode = '22023';
95
+ end if;
96
+
97
+ -- One person's issues one at a time, so the count below can't be raced.
98
+ perform pg_advisory_xact_lock(hashtextextended('genesis_web_handoff:' || p_user_id::text, 0));
99
+
100
+ select count(*) into recent
101
+ from private.genesis_web_handoffs h
102
+ where h.user_id = p_user_id and h.created_at > now() - make_interval(secs => p_window_seconds);
103
+ if recent >= p_max then
104
+ raise exception 'too many web handoffs; try again in a minute' using errcode = 'GS429';
105
+ end if;
106
+
107
+ expires := now() + make_interval(secs => p_ttl_seconds);
108
+ insert into private.genesis_web_handoffs (user_id, token_digest, path, expires_at, source_session_id)
109
+ values (p_user_id, p_digest, p_path, expires, p_source_session_id);
110
+
111
+ -- The log keeps 30 days, and a row whose web session still exists until that session goes.
112
+ delete from private.genesis_web_handoffs h
113
+ where h.created_at < now() - interval '30 days'
114
+ and (h.web_session_id is null or not exists (select 1 from auth.sessions s where s.id = h.web_session_id));
115
+ return expires;
116
+ end;
117
+ $$;
118
+
119
+ revoke execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) from public, anon, authenticated;
120
+ grant execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) to service_role;
121
+
122
+ -- Claim: the page opened the link. Once, unexpired; hands out a short-lived continue nonce.
123
+
124
+ create or replace function public.genesis_web_handoff_claim(
125
+ p_digest text,
126
+ p_nonce_digest text,
127
+ p_nonce_ttl_seconds integer default 30
128
+ )
129
+ returns table (user_id uuid, path text)
130
+ language plpgsql
131
+ volatile
132
+ security definer
133
+ set search_path = ''
134
+ as $$
135
+ begin
136
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
137
+ raise exception 'genesis_web_handoff_claim is for the server only' using errcode = '42501';
138
+ end if;
139
+ if p_nonce_ttl_seconds is null or p_nonce_ttl_seconds not between 5 and 120 then
140
+ raise exception 'p_nonce_ttl_seconds must be from 5 to 120' using errcode = '22023';
141
+ end if;
142
+ return query
143
+ update private.genesis_web_handoffs h
144
+ set used_at = now(), nonce_digest = p_nonce_digest, nonce_expires_at = now() + make_interval(secs => p_nonce_ttl_seconds)
145
+ where h.token_digest = p_digest and h.used_at is null and h.expires_at > now()
146
+ returning h.user_id, h.path;
147
+ end;
148
+ $$;
149
+
150
+ revoke execute on function public.genesis_web_handoff_claim(text, text, integer) from public, anon, authenticated;
151
+ grant execute on function public.genesis_web_handoff_claim(text, text, integer) to service_role;
152
+
153
+ -- Continue: the person chose Continue. Once, within the nonce's life, not after Cancel.
154
+
155
+ create or replace function public.genesis_web_handoff_continue(p_nonce_digest text)
156
+ returns table (user_id uuid, path text)
157
+ language plpgsql
158
+ volatile
159
+ security definer
160
+ set search_path = ''
161
+ as $$
162
+ begin
163
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
164
+ raise exception 'genesis_web_handoff_continue is for the server only' using errcode = '42501';
165
+ end if;
166
+ return query
167
+ update private.genesis_web_handoffs h
168
+ set continued_at = now()
169
+ where h.nonce_digest = p_nonce_digest and h.continued_at is null and h.discarded_at is null
170
+ and h.nonce_expires_at > now()
171
+ returning h.user_id, h.path;
172
+ end;
173
+ $$;
174
+
175
+ revoke execute on function public.genesis_web_handoff_continue(text) from public, anon, authenticated;
176
+ grant execute on function public.genesis_web_handoff_continue(text) to service_role;
177
+
178
+ -- Discard: the person chose Cancel. Burns the nonce, so Continue is refused afterwards.
179
+
180
+ create or replace function public.genesis_web_handoff_discard(p_nonce_digest text)
181
+ returns boolean
182
+ language plpgsql
183
+ volatile
184
+ security definer
185
+ set search_path = ''
186
+ as $$
187
+ begin
188
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
189
+ raise exception 'genesis_web_handoff_discard is for the server only' using errcode = '42501';
190
+ end if;
191
+ update private.genesis_web_handoffs h
192
+ set discarded_at = now()
193
+ where h.nonce_digest = p_nonce_digest and h.continued_at is null and h.discarded_at is null;
194
+ return found;
195
+ end;
196
+ $$;
197
+
198
+ revoke execute on function public.genesis_web_handoff_discard(text) from public, anon, authenticated;
199
+ grant execute on function public.genesis_web_handoff_discard(text) to service_role;
200
+
201
+ -- Bind: the page reports the session Continue made (its id from the verified access token),
202
+ -- once, within two minutes of Continue, for the handoff's own person.
203
+
204
+ create or replace function public.genesis_web_handoff_bind_session(p_nonce_digest text, p_user_id uuid, p_session_id uuid)
205
+ returns boolean
206
+ language plpgsql
207
+ volatile
208
+ security definer
209
+ set search_path = ''
210
+ as $$
211
+ begin
212
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
213
+ raise exception 'genesis_web_handoff_bind_session is for the server only' using errcode = '42501';
214
+ end if;
215
+ update private.genesis_web_handoffs h
216
+ set web_session_id = p_session_id
217
+ where h.nonce_digest = p_nonce_digest and h.user_id = p_user_id and h.web_session_id is null
218
+ and h.continued_at > now() - interval '2 minutes'
219
+ and h.source_session_id is distinct from p_session_id;
220
+ return found;
221
+ end;
222
+ $$;
223
+
224
+ revoke execute on function public.genesis_web_handoff_bind_session(text, uuid, uuid) from public, anon, authenticated;
225
+ grant execute on function public.genesis_web_handoff_bind_session(text, uuid, uuid) to service_role;
226
+
227
+ -- Revoke: the app signs out. Ends exactly the web sessions handed off from that app session
228
+ -- (never the person's other devices), by deleting their auth.sessions rows.
229
+
230
+ create or replace function public.genesis_web_handoff_revoke_sessions(p_user_id uuid, p_source_session_id uuid)
231
+ returns integer
232
+ language plpgsql
233
+ volatile
234
+ security definer
235
+ set search_path = ''
236
+ as $$
237
+ declare
238
+ ended integer;
239
+ begin
240
+ if coalesce(nullif(current_setting('role', true), ''), 'none') <> 'service_role' then
241
+ raise exception 'genesis_web_handoff_revoke_sessions is for the server only' using errcode = '42501';
242
+ end if;
243
+ if p_user_id is null or p_source_session_id is null then
244
+ return 0;
245
+ end if;
246
+ with targets as (
247
+ select h.id, h.web_session_id
248
+ from private.genesis_web_handoffs h
249
+ where h.user_id = p_user_id and h.source_session_id = p_source_session_id
250
+ and h.web_session_id is not null and h.web_session_id <> p_source_session_id
251
+ for update
252
+ ), gone as (
253
+ delete from auth.sessions s
254
+ using targets t
255
+ where s.id = t.web_session_id and s.user_id = p_user_id
256
+ returning s.id
257
+ ), cleared as (
258
+ update private.genesis_web_handoffs h
259
+ set web_session_id = null
260
+ from targets t
261
+ where h.id = t.id
262
+ returning h.id
263
+ )
264
+ select count(*) into ended from gone;
265
+ return ended;
266
+ end;
267
+ $$;
268
+
269
+ revoke execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) from public, anon, authenticated;
270
+ grant execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) to service_role;
@@ -0,0 +1,70 @@
1
+ -- @feat:owner-access
2
+ -- pgTAP test for 0009_genesis_owner_access.sql.
3
+ -- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
4
+ -- It writes only its own users and hooks and rolls everything back. If your app already has the
5
+ -- hooks, they are dropped and replaced inside this transaction only.
6
+ begin;
7
+ create extension if not exists pgtap with schema extensions;
8
+ select plan(11);
9
+
10
+ insert into auth.users (id, aud, role, email, instance_id)
11
+ select ('00000000-0000-4000-8000-0000000000' || n)::uuid, 'authenticated', 'authenticated', 'genesis-owner-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
12
+ from unnest(array['a1', 'b2']) n;
13
+
14
+ create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-0000000000' || n)::uuid $$;
15
+ create function pg_temp.tenant(n int) returns uuid language sql immutable as $$ select ('00000000-0000-4000-9000-00000000000' || n)::uuid $$;
16
+ create function pg_temp.team(n int) returns uuid language sql immutable as $$ select ('00000000-0000-4000-a000-00000000000' || n)::uuid $$;
17
+ -- The wrappers are called as the owner with a person's claims, the way a policy calls them for
18
+ -- that person (signed-in callers can't name the private schema themselves).
19
+ create function pg_temp.claims(n text) returns void language plpgsql as $$
20
+ begin
21
+ perform set_config('request.jwt.claims', json_build_object('sub', pg_temp.person(n), 'role', 'authenticated')::text, true);
22
+ perform set_config('request.jwt.claim.sub', pg_temp.person(n)::text, true);
23
+ end $$;
24
+ create function pg_temp.no_one() returns void language plpgsql as $$
25
+ begin
26
+ perform set_config('request.jwt.claims', '', true);
27
+ perform set_config('request.jwt.claim.sub', '', true);
28
+ end $$;
29
+
30
+ drop function if exists private.genesis_owner_can(uuid, uuid, uuid, uuid, uuid, text);
31
+ drop function if exists private.genesis_readable_owners(uuid);
32
+
33
+ -- Privileges ---------------------------------------------------------------------------------
34
+ select ok(not has_function_privilege('anon', 'private.genesis_owner_allows(uuid,uuid,uuid,uuid,text)', 'execute'), 'anon can''t call genesis_owner_allows');
35
+ select ok(not has_function_privilege('anon', 'private.genesis_owner_readable()', 'execute'), 'anon can''t call genesis_owner_readable');
36
+
37
+ -- No hooks: fail closed -----------------------------------------------------------------------
38
+ select pg_temp.claims('a1');
39
+ select is(private.genesis_owner_allows(pg_temp.tenant(1), null, pg_temp.team(1), null, 'write'), false, 'without the hook, no');
40
+ select is((select count(*)::int from private.genesis_owner_readable()), 0, 'without the hook, an empty set');
41
+
42
+ -- The app's hooks -----------------------------------------------------------------------------
43
+ create function private.genesis_readable_owners(viewer uuid)
44
+ returns table (tenant_id uuid, owner_id uuid) language sql stable set search_path = '' as $$
45
+ select '00000000-0000-4000-9000-000000000001'::uuid, '00000000-0000-4000-a000-000000000001'::uuid
46
+ where viewer = '00000000-0000-4000-8000-0000000000a1'::uuid
47
+ $$;
48
+ create function private.genesis_owner_can(tenant uuid, viewer uuid, person_id uuid, team_id uuid, organization_id uuid, action text)
49
+ returns boolean language sql stable set search_path = '' as $$
50
+ select viewer = '00000000-0000-4000-8000-0000000000a1'::uuid and team_id = '00000000-0000-4000-a000-000000000001'::uuid
51
+ $$;
52
+
53
+ select is(private.genesis_owner_allows(pg_temp.tenant(1), null, pg_temp.team(1), null, 'write'), true, 'the wrapper answers for the signed-in person');
54
+ select is((select count(*)::int from private.genesis_owner_readable()), 1, 'the readable set for the signed-in person');
55
+ select is(private.genesis_owner_allows(pg_temp.tenant(1), null, pg_temp.team(1), null, 'delete'), false, 'an action other than read or write is no');
56
+ select is(private.genesis_owner_allows(null, null, pg_temp.team(1), null, 'read'), false, 'a null tenant is no');
57
+ select pg_temp.claims('b2');
58
+ select is(private.genesis_owner_allows(pg_temp.tenant(1), null, pg_temp.team(1), null, 'read'), false, 'it answers only for the signed-in person');
59
+ select pg_temp.no_one();
60
+ select is(private.genesis_owner_allows(pg_temp.tenant(1), null, pg_temp.team(1), null, 'read'), false, 'nobody signed in is no');
61
+
62
+ -- A hook with the wrong return type fails closed -----------------------------------------------
63
+ drop function private.genesis_owner_can(uuid, uuid, uuid, uuid, uuid, text);
64
+ create function private.genesis_owner_can(tenant uuid, viewer uuid, person_id uuid, team_id uuid, organization_id uuid, action text)
65
+ returns text language sql stable set search_path = '' as $$ select 'maybe' $$;
66
+ select pg_temp.claims('a1');
67
+ select is(private.genesis_owner_allows(pg_temp.tenant(1), null, pg_temp.team(1), null, 'write'), false, 'a hook that errors means no');
68
+
69
+ select * from finish();
70
+ rollback;
@@ -0,0 +1,54 @@
1
+ -- @feat:feature-mirror
2
+ -- pgTAP test for 0014_genesis_feature_registry_hardening.sql (needs 0004 and 0008).
3
+ -- Copy into your app's `supabase/tests/database/` and run `supabase test db`. Rolls back.
4
+ begin;
5
+ create extension if not exists pgtap with schema extensions;
6
+ select plan(11);
7
+
8
+ insert into auth.users (id, aud, role, email, instance_id)
9
+ values ('00000000-0000-4000-8000-0000000014a1', 'authenticated', 'authenticated', 'hardening-a1@example.test', '00000000-0000-0000-0000-000000000000');
10
+ drop function if exists private.genesis_tenant_member(uuid, uuid);
11
+ create function private.genesis_tenant_member(tenant uuid, person uuid) returns boolean language sql stable set search_path = '' as $$
12
+ select tenant = '00000000-0000-4000-9000-000000001401'::uuid and person = '00000000-0000-4000-8000-0000000014a1'::uuid
13
+ $$;
14
+ revoke execute on function private.genesis_tenant_member(uuid, uuid) from public, anon, authenticated;
15
+
16
+ select public.sync_feature_registry(9100, 'hard-1',
17
+ '[{"id": "open", "name": "Open", "status": "live"},
18
+ {"id": "secret", "name": "Secret", "status": "live", "visibility": "internal"},
19
+ {"id": "a", "name": "A", "status": "live"}, {"id": "b", "name": "B", "status": "live"}]',
20
+ '[{"key": "open.release", "feature": "open", "kind": "release", "default": false}]');
21
+
22
+ select ok(exists (select 1 from pg_indexes where indexname = 'feature_entitlements_feature_active'), 'entitlements are indexed by feature');
23
+
24
+ -- A session that switched to authenticated without claims is a person with no tenant, not the server.
25
+ set local role authenticated;
26
+ select set_config('request.jwt.claims', '', true);
27
+ select is(public.feature_on('00000000-0000-4000-9000-000000001401', 'open'), false, 'SET ROLE authenticated with no claims gets no answer');
28
+ select throws_ok($$select public.feature_gate_state('00000000-0000-4000-9000-000000001401')$$, '42501', null, '... and no gate state');
29
+ select is(public.feature_shipped('secret'), false, 'a person can''t confirm an internal feature');
30
+ select is(public.feature_shipped('open'), true, '... but sees customer-facing ones');
31
+ -- Claims that say service_role don't make a person server code: the role decides.
32
+ select set_config('request.jwt.claims', '{"role": "service_role"}', true);
33
+ select is(public.feature_on('00000000-0000-4000-9000-000000001401', 'open'), false, 'claims claiming service_role are refused');
34
+ select is(public.feature_shipped('secret'), false, '... and see no internal feature');
35
+ -- A member with claims is still answered.
36
+ select set_config('request.jwt.claims', '{"sub": "00000000-0000-4000-8000-0000000014a1", "role": "authenticated"}', true);
37
+ select is(public.feature_on('00000000-0000-4000-9000-000000001401', 'open'), true, 'a member is answered');
38
+ reset role;
39
+ select set_config('request.jwt.claims', '', true);
40
+
41
+ set local role service_role;
42
+ select is(public.feature_shipped('secret'), true, 'server code sees internal features');
43
+ reset role;
44
+
45
+ -- A flag that leaves the registry loses its kill switch.
46
+ update public.feature_flags set platform_value = 'false' where key = 'open.release';
47
+ select public.sync_feature_registry(9200, 'hard-2',
48
+ '[{"id": "open", "name": "Open", "status": "live"}, {"id": "secret", "name": "Secret", "status": "live", "visibility": "internal"},
49
+ {"id": "a", "name": "A", "status": "live"}, {"id": "b", "name": "B", "status": "live"}]', '[]');
50
+ select is((select platform_value from public.feature_flags where key = 'open.release'), null, 'a flag leaving the registry drops its platform value');
51
+ select is((select in_registry from public.feature_flags where key = 'open.release'), false, '... and is marked gone');
52
+
53
+ select * from finish();
54
+ rollback;
@@ -0,0 +1,203 @@
1
+ -- @feat:web-handoff
2
+ -- pgTAP test for 0015_genesis_web_handoff.sql.
3
+ -- Copy into your app's `supabase/tests/database/` and run `supabase test db`.
4
+ -- It writes only its own users, sessions and handoffs and rolls everything back.
5
+ begin;
6
+ create extension if not exists pgtap with schema extensions;
7
+ select plan(38);
8
+
9
+ insert into auth.users (id, aud, role, email, instance_id)
10
+ select ('00000000-0000-4000-8000-0000000015' || n)::uuid, 'authenticated', 'authenticated', 'genesis-handoff-' || n || '@example.test', '00000000-0000-0000-0000-000000000000'
11
+ from unnest(array['a1', 'b2']) n;
12
+
13
+ create function pg_temp.person(n text) returns uuid language sql immutable as $$ select ('00000000-0000-4000-8000-0000000015' || n)::uuid $$;
14
+ create function pg_temp.session(n int) returns uuid language sql immutable as $$ select ('00000000-0000-4000-9000-0000000150' || lpad(n::text, 2, '0'))::uuid $$;
15
+ create function pg_temp.digest(n int) returns text language sql immutable as $$ select repeat(to_hex(n % 16), 64) $$;
16
+ create function pg_temp.nonce(n int) returns text language sql immutable as $$ select repeat(chr(96 + n), 64) $$;
17
+ grant execute on function pg_temp.person(text), pg_temp.session(int), pg_temp.digest(int), pg_temp.nonce(int) to anon, authenticated, service_role;
18
+
19
+ -- a1's app session (1), the web session a handoff makes (2), and a1's other device (3).
20
+ insert into auth.sessions (id, user_id, created_at, updated_at)
21
+ values (pg_temp.session(1), pg_temp.person('a1'), now(), now()),
22
+ (pg_temp.session(2), pg_temp.person('a1'), now(), now()),
23
+ (pg_temp.session(3), pg_temp.person('a1'), now(), now());
24
+
25
+ -- Shape and access --------------------------------------------------------------------
26
+
27
+ select has_table('private', 'genesis_web_handoffs', 'the handoff table is in private');
28
+ select ok(
29
+ (select relrowsecurity from pg_class where oid = 'private.genesis_web_handoffs'::regclass),
30
+ 'row level security is on'
31
+ );
32
+ select ok(
33
+ not has_table_privilege('authenticated', 'private.genesis_web_handoffs', 'select')
34
+ and not has_table_privilege('anon', 'private.genesis_web_handoffs', 'select'),
35
+ 'anon and authenticated cannot read the table'
36
+ );
37
+ select ok(
38
+ not exists (
39
+ select 1
40
+ from unnest(array[
41
+ 'public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid)',
42
+ 'public.genesis_web_handoff_claim(text, text, integer)',
43
+ 'public.genesis_web_handoff_continue(text)',
44
+ 'public.genesis_web_handoff_discard(text)',
45
+ 'public.genesis_web_handoff_bind_session(text, uuid, uuid)',
46
+ 'public.genesis_web_handoff_revoke_sessions(uuid, uuid)'
47
+ ]) f
48
+ cross join unnest(array['anon', 'authenticated']) r
49
+ where has_function_privilege(r, f, 'execute')
50
+ ),
51
+ 'anon and authenticated can execute none of the functions'
52
+ );
53
+ select ok(
54
+ has_function_privilege('service_role', 'public.genesis_web_handoff_continue(text)', 'execute'),
55
+ 'service_role can'
56
+ );
57
+
58
+ -- Even granted, the functions refuse any role but service_role, whatever the JWT claims.
59
+ grant execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) to authenticated;
60
+ grant execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) to authenticated;
61
+ set local role authenticated;
62
+ select set_config('request.jwt.claims', json_build_object('sub', pg_temp.person('a1'), 'role', 'service_role')::text, true);
63
+ select throws_ok(
64
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(1), '/x') $$,
65
+ '42501', 'genesis_web_handoff_issue is for the server only',
66
+ 'issue: authenticated is refused even with a service_role claim in its JWT'
67
+ );
68
+ select throws_ok(
69
+ $$ select public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)) $$,
70
+ '42501', 'genesis_web_handoff_revoke_sessions is for the server only',
71
+ 'revoke: the same'
72
+ );
73
+ reset role;
74
+ revoke execute on function public.genesis_web_handoff_issue(uuid, text, text, integer, integer, integer, uuid) from authenticated;
75
+ revoke execute on function public.genesis_web_handoff_revoke_sessions(uuid, uuid) from authenticated;
76
+
77
+ -- Issue, claim, continue ---------------------------------------------------------------
78
+
79
+ set local role service_role;
80
+ -- (Through a subquery: BETWEEN would call the function twice.)
81
+ select ok(
82
+ (select e between now() + interval '59 seconds' and now() + interval '61 seconds'
83
+ from (select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(1), '/clients/1/forms', 60, 10, 60, pg_temp.session(1)) as e) issued),
84
+ 'a handoff expires in 60 seconds by default'
85
+ );
86
+ select results_eq(
87
+ $$ select user_id, path from public.genesis_web_handoff_claim(pg_temp.digest(1), pg_temp.nonce(1)) $$,
88
+ $$ values (pg_temp.person('a1'), '/clients/1/forms') $$,
89
+ 'claiming returns the person and path'
90
+ );
91
+ select is_empty(
92
+ $$ select * from public.genesis_web_handoff_claim(pg_temp.digest(1), pg_temp.nonce(2)) $$,
93
+ 'a link is claimed once'
94
+ );
95
+ select is_empty(
96
+ $$ select * from public.genesis_web_handoff_claim(pg_temp.digest(9), pg_temp.nonce(3)) $$,
97
+ 'an unknown link claims nothing'
98
+ );
99
+ select results_eq(
100
+ $$ select user_id, path from public.genesis_web_handoff_continue(pg_temp.nonce(1)) $$,
101
+ $$ values (pg_temp.person('a1'), '/clients/1/forms') $$,
102
+ 'Continue with the nonce returns the person and path'
103
+ );
104
+ select is_empty(
105
+ $$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(1)) $$,
106
+ 'a nonce continues once'
107
+ );
108
+ select ok(
109
+ not public.genesis_web_handoff_discard(pg_temp.nonce(1)),
110
+ 'a continued handoff cannot be discarded'
111
+ );
112
+
113
+ -- Bind and revoke.
114
+ select ok(
115
+ not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('b2'), pg_temp.session(2)),
116
+ 'bind refuses another person'
117
+ );
118
+ select ok(
119
+ not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(1)),
120
+ 'bind refuses the app session itself'
121
+ );
122
+ select ok(
123
+ public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(2)),
124
+ 'bind keeps the web session'
125
+ );
126
+ select ok(
127
+ not public.genesis_web_handoff_bind_session(pg_temp.nonce(1), pg_temp.person('a1'), pg_temp.session(3)),
128
+ 'bind happens once'
129
+ );
130
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('b2'), pg_temp.session(1)), 0, 'another person ends nothing');
131
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(3)), 0, 'another app session ends nothing');
132
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)), 1, 'the app session that handed off ends one web session');
133
+ reset role;
134
+ select is(
135
+ (select array_agg(id order by id) from auth.sessions where user_id = pg_temp.person('a1')),
136
+ array[pg_temp.session(1), pg_temp.session(3)],
137
+ 'only the handed-off web session is gone; the app and the other device stay'
138
+ );
139
+ set local role service_role;
140
+ select is(public.genesis_web_handoff_revoke_sessions(pg_temp.person('a1'), pg_temp.session(1)), 0, 'ending again ends nothing');
141
+
142
+ -- Cancel, and the nonce's life.
143
+ select lives_ok(
144
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(2), '/a') $$,
145
+ 'a second handoff'
146
+ );
147
+ select lives_ok($$ select * from public.genesis_web_handoff_claim(pg_temp.digest(2), pg_temp.nonce(4)) $$, 'claimed');
148
+ select ok(public.genesis_web_handoff_discard(pg_temp.nonce(4)), 'Cancel discards it');
149
+ select is_empty($$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(4)) $$, 'Continue after Cancel is refused');
150
+
151
+ select lives_ok(
152
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('a1'), pg_temp.digest(3), '/a') $$,
153
+ 'a third handoff'
154
+ );
155
+ select lives_ok($$ select * from public.genesis_web_handoff_claim(pg_temp.digest(3), pg_temp.nonce(5), 5) $$, 'claimed with a 5-second nonce');
156
+ reset role;
157
+ update private.genesis_web_handoffs set nonce_expires_at = now() - interval '1 second' where nonce_digest = pg_temp.nonce(5);
158
+ set local role service_role;
159
+ select is_empty($$ select * from public.genesis_web_handoff_continue(pg_temp.nonce(5)) $$, 'Continue after the nonce expired is refused');
160
+
161
+ -- Expired links, refusals and the rate limit (b2).
162
+ select lives_ok(
163
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(4), '/a', 10) $$,
164
+ 'a 10-second handoff is issued'
165
+ );
166
+ reset role;
167
+ update private.genesis_web_handoffs set created_at = now() - interval '1 minute', expires_at = now() - interval '1 second' where token_digest = pg_temp.digest(4);
168
+ set local role service_role;
169
+ select is_empty(
170
+ $$ select * from public.genesis_web_handoff_claim(pg_temp.digest(4), pg_temp.nonce(6)) $$,
171
+ 'an expired link claims nothing'
172
+ );
173
+ select throws_ok(
174
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(5), '/a', 301) $$,
175
+ '22023', null, 'a lifetime over 300 seconds is refused'
176
+ );
177
+ select throws_ok(
178
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(5), '//evil.example') $$,
179
+ '23514', null, 'a protocol-relative path is refused'
180
+ );
181
+ select lives_ok(
182
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(6), '/a', 60, 2, 60) $$,
183
+ 'first in the window'
184
+ );
185
+ select lives_ok(
186
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(7), '/a', 60, 2, 60) $$,
187
+ 'second in the window, at the limit'
188
+ );
189
+ select throws_ok(
190
+ $$ select public.genesis_web_handoff_issue(pg_temp.person('b2'), pg_temp.digest(8), '/a', 60, 2, 60) $$,
191
+ 'GS429', null, 'the third in the window is refused'
192
+ );
193
+ reset role;
194
+
195
+ -- Deleting the login deletes its handoffs.
196
+ delete from auth.users where id = pg_temp.person('b2');
197
+ select is(
198
+ (select count(*)::int from private.genesis_web_handoffs where user_id = pg_temp.person('b2')),
199
+ 0, 'a deleted login leaves no handoffs'
200
+ );
201
+
202
+ select * from finish();
203
+ rollback;