fix(supabase): confirmed uploads gate asset reads (hash claim), seeds-listed test, Dockerfile workspace manifests; importer declares every authored label, real in=out control

This commit is contained in:
bchanot committed 2026-10-09 00:50:36 +02:00
1 parent bbfb8d6203
commit dee4ce43d9
28 files changed
+511 -87

No files matched your search

+50 -8
View File
@@ -2,7 +2,7 @@
-- through a sheet (published reference for users, any version for editors),
-- never through its hash alone. No API writes.
begin;
select plan(14);
select plan(28);
insert into auth.users (id, email) values
('30000000-0000-0000-0000-00000000000a', 'alice-assets@test.local'),
@@ -11,8 +11,8 @@ insert into auth.users (id, email) values
create table pg_temp.ids (key text primary key, id uuid not null);
create function pg_temp.id(p_key text) returns uuid language sql stable
as $$ select id from pg_temp.ids where key = p_key $$;
grant all on pg_temp.ids to anon, authenticated;
grant execute on function pg_temp.id(text) to anon, authenticated;
grant all on pg_temp.ids to anon, authenticated, service_role;
grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
-- Minimal publishable document with one main view on a given asset.
create function pg_temp.doc(p_asset uuid) returns jsonb language sql as $$
@@ -27,7 +27,7 @@ create function pg_temp.doc(p_asset uuid) returns jsonb language sql as $$
'name', jsonb_build_object('fr', 'V', 'en', 'V'), 'svg_asset_id', p_asset, 'view_box', null)),
'annotations', '[]'::jsonb, 'finding_zones', '[]'::jsonb);
$$;
grant execute on function pg_temp.doc(uuid) to authenticated;
grant execute on function pg_temp.doc(uuid) to authenticated, service_role;
-- 1. Bucket contract
select is((select public from storage.buckets where id = 'anatomical-sheet-assets'), false, 'bucket is private');
@@ -59,26 +59,45 @@ select lives_ok($$ select public.publish_sheet_version(pg_temp.id('v1')) $$, 'v1
insert into pg_temp.ids select 'v2', public.create_sheet_draft(pg_temp.id('sheet'));
update public.anatomical_sheet_versions set content = pg_temp.doc(pg_temp.id('b')) where id = pg_temp.id('v2');
-- Objects uploaded by the operator (owner session)
-- Objects uploaded by the operator (owner session), then confirmed through the
-- service-role RPC for A and B only; C stays metadata without bytes.
reset role;
insert into storage.objects (bucket_id, name) values
('anatomical-sheet-assets', repeat('a', 64) || '.svg'),
('anatomical-sheet-assets', repeat('b', 64) || '.svg'),
('anatomical-sheet-assets', repeat('c', 64) || '.svg'),
('anatomical-sheet-assets', repeat('d', 64) || '.svg');
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'nothing is readable before the upload is confirmed');
select throws_ok(
$$ select public.confirm_sheet_asset_upload(pg_temp.id('a')) $$,
'42501', null, 'an editor cannot confirm an upload (service role only)'
);
set local role service_role;
select lives_ok($$ select public.confirm_sheet_asset_upload(pg_temp.id('a')) $$, 'service role confirms asset A');
select lives_ok($$ select public.confirm_sheet_asset_upload(pg_temp.id('b')) $$, 'service role confirms asset B');
select is(public.confirm_sheet_asset_upload(pg_temp.id('a')), pg_temp.id('a'), 'confirming twice is idempotent');
reset role;
-- 3. Editor reads every object of her sheet (published, draft, unreferenced), not foreign ones
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg'), (repeat('b', 64) || '.svg'), (repeat('c', 64) || '.svg') $$,
'editor reads the objects of her sheet; the object no sheet registered stays hidden'
$$ values (repeat('a', 64) || '.svg'), (repeat('b', 64) || '.svg') $$,
'editor reads the confirmed objects of her sheet; unconfirmed and foreign objects stay hidden'
);
-- 4. Bob: private sheet → nothing; public sheet → published reference only
-- 4. Bob: private sheet → nothing; claiming the hash on his own sheet opens nothing
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'other user reads nothing from a private sheet');
insert into pg_temp.ids select 'bob-sheet', public.create_anatomical_sheet('equine', 'Claim', 'claim-test', pg_temp.doc(null));
select lives_ok(
$$ select public.register_sheet_asset(pg_temp.id('bob-sheet'), repeat('a', 64), 100, 'A.svg') $$,
'bob registers the known hash on his own sheet'
);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'a self-registered hash without a confirmed upload opens no object');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.set_sheet_visibility(pg_temp.id('sheet'), 'public') $$, 'alice makes the sheet public');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
@@ -100,11 +119,34 @@ select is(
0::bigint, 'no API role has an insert/update/delete policy on the bucket objects'
);
-- Name variants never match the strict key
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select ok(
not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', repeat('a', 64))
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', upper(repeat('a', 64)) || '.svg')
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', 'x/' || repeat('a', 64) || '.svg')
and not private.can_read_sheet_asset_object('30000000-0000-0000-0000-00000000000a', repeat('a', 64) || '.svg/'),
'object name must be exactly <sha256>.svg'
);
-- A deprecated version still publishes its asset
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('v2')) $$, 'v2 (asset B) published');
select lives_ok($$ select public.deprecate_sheet_version(pg_temp.id('v1'), 'old') $$, 'v1 (asset A) deprecated');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select results_eq(
$$ select name from storage.objects where bucket_id = 'anatomical-sheet-assets' order by name $$,
$$ values (repeat('a', 64) || '.svg'), (repeat('b', 64) || '.svg') $$,
'published and deprecated references both open the object for users of a public sheet'
);
-- 5. Archived sheet hides objects from others; anon sees nothing
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.archive_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice archives');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'archived sheet: others read nothing');
select set_config('request.jwt.claims', '{"sub":"30000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select lives_ok($$ select public.restore_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice restores');
select lives_ok($$ select public.delete_anatomical_sheet(pg_temp.id('sheet')) $$, 'alice soft-deletes the sheet');
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'deleted sheet: even the creator reads nothing');
set local role anon;
select set_config('request.jwt.claims', '', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'anon reads nothing');
+23 -3
View File
@@ -2,7 +2,7 @@
-- present as a platform-owned DRAFT, never published by the seed, publishable
-- by platform admins only, content accepted by the SQL referential gate.
begin;
select plan(14);
select plan(18);
insert into auth.users (id, email) values
('40000000-0000-0000-0000-000000000001', 'root-seed@test.local'),
@@ -12,8 +12,8 @@ select private.assign_platform_role(null, '40000000-0000-0000-0000-000000000001'
create table pg_temp.ids (key text primary key, id uuid not null);
create function pg_temp.id(p_key text) returns uuid language sql stable
as $$ select id from pg_temp.ids where key = p_key $$;
grant all on pg_temp.ids to anon, authenticated;
grant execute on function pg_temp.id(text) to anon, authenticated;
grant all on pg_temp.ids to anon, authenticated, service_role;
grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
insert into pg_temp.ids select 'sheet', id from public.anatomical_sheets where slug = 'faunosteo-cheval' and is_official;
insert into pg_temp.ids select 'draft', id from public.anatomical_sheet_versions where sheet_id = pg_temp.id('sheet');
@@ -74,5 +74,25 @@ select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-0000000
select is((select count(*) from public.anatomical_sheet_versions where id = pg_temp.id('draft')), 1::bigint, 'a platform admin sees the official draft');
select lives_ok($$ select public.publish_sheet_version(pg_temp.id('draft')) $$, 'a platform admin could publish it (rolled back here)');
-- 3. Storage: the seed registers metadata only; after the operator upload + confirmation,
-- a platform admin reads both objects and a plain user none.
reset role;
insert into storage.objects (bucket_id, name)
select 'anatomical-sheet-assets', a.sha256 || '.svg' from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet');
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'seed objects are not readable before confirmation');
set local role service_role;
select is(
(select count(*) from public.anatomical_sheet_assets a where a.sheet_id = pg_temp.id('sheet')
and public.confirm_sheet_asset_upload(a.id) = a.id),
2::bigint, 'operator confirms the two uploads'
);
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-000000000001","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 2::bigint, 'a platform admin reads both official SVGs');
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
select is((select count(*) from storage.objects where bucket_id = 'anatomical-sheet-assets'), 0::bigint, 'a plain user reads no official draft SVG');
select * from finish();
rollback;