fix(supabase): V1-2.1 close the second verifier and security findings

Save and submit lock the sheet before the version; change_note length and a
blank FR name answer closed codes; pgTAP for a capability refused to a
factorless account and the five remaining historical-sheet RPC refusals.
This commit is contained in:
bchanot committed 2026-10-28 18:44:00 +01:00
1 parent 4a829416c6
commit 961995804a
4 files changed
+47 -10

No files matched your search

@@ -469,7 +469,8 @@ declare
v_sheet uuid;
begin
perform private.check_document_structure(document);
v_name := left(coalesce(document -> 'name' ->> 'fr', document -> 'name' ->> 'en', 'Sans nom'), 120);
v_name := left(coalesce(nullif(btrim(document -> 'name' ->> 'fr'), ''), nullif(btrim(document -> 'name' ->> 'en'), ''),
'Sans nom'), 120);
v_description := left(coalesce(document -> 'description' ->> 'fr', ''), 2000);
-- An official sheet accepts an active or inactive species, never `other` (§7.2).
if v_species is null or v_species = 'other'
@@ -523,6 +524,9 @@ begin
where v.sheet_id = create_sheet_draft.sheet_id and v.status = 'draft') then
raise exception 'draft.exists' using errcode = 'P0001';
end if;
if char_length(create_sheet_draft.change_note) > 1000 then
raise exception 'invalid_input' using errcode = 'P0001';
end if;
perform private.consume_catalog_write_budget(v_actor);
-- Base only from a version still `published` that was published through a
@@ -554,11 +558,15 @@ declare
v_actor uuid := private.require_catalog_actor();
v_row public.anatomical_sheet_versions%rowtype;
begin
select v.* into v_row from public.anatomical_sheet_versions v where v.id = save_sheet_draft.version_id for update;
if v_row.id is null then
select v.sheet_id into v_row.sheet_id from public.anatomical_sheet_versions v where v.id = save_sheet_draft.version_id;
if v_row.sheet_id is null then
raise exception 'not_found' using errcode = '42501';
end if;
perform private.require_sheet_author(v_row.sheet_id);
-- Lock order sheet, then version (§6.1): an archive waits for this write or
-- is seen by it, so nothing lands on a sheet archived in the meantime.
perform 1 from public.anatomical_sheets s where s.id = v_row.sheet_id for share;
select v.* into v_row from public.anatomical_sheet_versions v where v.id = save_sheet_draft.version_id for update;
if v_row.status <> 'draft' then
raise exception 'draft.not_a_draft' using errcode = '55000';
end if;
@@ -574,6 +582,9 @@ begin
raise exception 'draft_conflict' using errcode = 'P0001';
end if;
perform private.check_document_structure(document);
if char_length(save_sheet_draft.change_note) > 1000 then
raise exception 'invalid_input' using errcode = 'P0001';
end if;
if (document ->> 'species_id') is distinct from (select species_id from public.anatomical_sheets where id = v_row.sheet_id) then
raise exception 'document.species' using errcode = '23514';
end if;
@@ -615,11 +626,15 @@ declare
v_round integer;
v_id uuid;
begin
select v.* into v_row from public.anatomical_sheet_versions v where v.id = submit_sheet_review.version_id for update;
if v_row.id is null then
select v.sheet_id into v_row.sheet_id from public.anatomical_sheet_versions v where v.id = submit_sheet_review.version_id;
if v_row.sheet_id is null then
raise exception 'not_found' using errcode = '42501';
end if;
perform private.require_sheet_author(v_row.sheet_id);
-- Lock order sheet, then version (§6.1): an archive waits for this write or
-- is seen by it, so nothing lands on a sheet archived in the meantime.
perform 1 from public.anatomical_sheets s where s.id = v_row.sheet_id for share;
select v.* into v_row from public.anatomical_sheet_versions v where v.id = submit_sheet_review.version_id for update;
if v_row.status <> 'draft' then
raise exception 'draft.not_a_draft' using errcode = '55000';
end if;
@@ -1086,7 +1101,7 @@ begin
if v_fingerprint is null or v_fingerprint !~ '^[0-9a-f]{64}$' then
raise exception 'verdict.fingerprint' using errcode = 'P0001';
end if;
v_name := left(coalesce(v_version.content -> 'name' ->> 'fr', v_sheet.name), 120);
v_name := left(coalesce(nullif(btrim(v_version.content -> 'name' ->> 'fr'), ''), v_sheet.name), 120);
v_description := left(coalesce(v_version.content -> 'description' ->> 'fr', ''), 2000);
perform private.consume_catalog_write_budget(v_actor);
@@ -1368,7 +1383,7 @@ begin
end if;
perform private.consume_catalog_write_budget(v_actor);
select private.insert_official_sheet(s.species_id, left(coalesce(v_source.content -> 'name' ->> 'fr', s.name), 120),
select private.insert_official_sheet(s.species_id, left(coalesce(nullif(btrim(v_source.content -> 'name' ->> 'fr'), ''), s.name), 120),
create_sheet_working_copy.slug, s.description, v_source.id)
into v_sheet
from public.anatomical_sheets s where s.id = v_source.sheet_id;