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

@@ -4,18 +4,63 @@
-- Bucket anatomical-sheet-assets, private. Objects are content-addressed:
-- key = <sha256>.svg, so two sheets with the same drawing share one object
-- and a copy of a sheet never copies bytes. Reading an object is authorised
-- THROUGH a sheet (an asset row the caller may use or edit), never through
-- the hash alone; drafts stay visible to editors only, as the sheet rows do.
-- No API role writes objects: uploads are an operator / server task
-- THROUGH a sheet: an asset row the caller may use or edit AND whose upload
-- was confirmed server-side (uploaded_at), never through a hash a user
-- merely registered; drafts stay visible to editors only, as the sheet rows
-- do. No API role writes objects: uploads are an operator / server task
-- (import tool with the service key) until the editor step adds a signed
-- upload path.
insert into storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
values ('anatomical-sheet-assets', 'anatomical-sheet-assets', false, 20971520, array['image/svg+xml'])
on conflict (id) do nothing;
on conflict (id) do update
set public = false, file_size_limit = excluded.file_size_limit,
allowed_mime_types = excluded.allowed_mime_types;
-- An object is readable when a usable sheet publishes it (a non-draft
-- version references the asset) or when the caller edits the sheet.
-- ---------------------------------------------------------------------------
-- Upload confirmation. An editor may register any sha256 as metadata, so a
-- hash alone must never open an object: the read predicate requires
-- uploaded_at, which only the operator / server path (service role) sets
-- after the bytes have landed in the bucket for that sheet.
-- ---------------------------------------------------------------------------
alter table public.anatomical_sheet_assets
add column uploaded_at timestamptz;
comment on column public.anatomical_sheet_assets.uploaded_at is
'Set by confirm_sheet_asset_upload (service role) once the object <sha256>.svg exists for this sheet. Null = metadata only, object not readable through this row.';
create index anatomical_sheet_assets_sha256_idx on public.anatomical_sheet_assets (sha256);
create or replace function public.confirm_sheet_asset_upload(asset_id uuid)
returns uuid
language plpgsql
security definer
set search_path = ''
as $$
begin
update public.anatomical_sheet_assets
set uploaded_at = now()
where id = asset_id and uploaded_at is null;
if not found and not exists (select 1 from public.anatomical_sheet_assets where id = asset_id) then
raise exception 'unknown asset %', asset_id using errcode = 'P0002';
end if;
perform private.log_event(
auth.uid(), 'anatomical_sheet_asset.uploaded', 'anatomical_sheet_asset', asset_id
);
return asset_id;
end;
$$;
comment on function public.confirm_sheet_asset_upload(uuid) is
'Operator / server only (service role): marks the asset bytes as present in Storage. Idempotent.';
revoke all on function public.confirm_sheet_asset_upload(uuid) from public, anon, authenticated;
grant execute on function public.confirm_sheet_asset_upload(uuid) to service_role;
-- ---------------------------------------------------------------------------
-- Read predicate: a confirmed asset row of a sheet the caller edits, or of a
-- usable sheet whose non-draft version references it. Sargable on sha256.
-- ---------------------------------------------------------------------------
create or replace function private.can_read_sheet_asset_object(p_user_id uuid, p_object_name text)
returns boolean
language sql
@@ -26,7 +71,9 @@ as $$
select exists (
select 1
from public.anatomical_sheet_assets a
where a.sha256 || '.svg' = p_object_name
where a.sha256 = left(p_object_name, 64)
and p_object_name = a.sha256 || '.svg'
and a.uploaded_at is not null
and (
private.can_edit_sheet(p_user_id, a.sheet_id)
or (
@@ -43,7 +90,7 @@ as $$
$$;
comment on function private.can_read_sheet_asset_object(uuid, text) is
'Storage read predicate for bucket anatomical-sheet-assets: object <sha256>.svg referenced by a published version of a usable sheet, or by any version of a sheet the caller edits.';
'Storage read predicate for bucket anatomical-sheet-assets: object <sha256>.svg whose upload is confirmed, referenced by a published version of a usable sheet, or by any version of a sheet the caller edits.';
revoke all on function private.can_read_sheet_asset_object(uuid, text) from public;
grant execute on function private.can_read_sheet_asset_object(uuid, text) to authenticated;