fix(supabase): V1-2.1 harden M5 after the security and verifier gates

Fresh security audit PASS; verifier ECARTS(10) addressed:
- publish re-checks the approver's sheet_reviewer grant (publish.approval.invalid)
- scope disclaimer enforced at approve and publish (publish.disclaimer.missing, O6)
- to_confirm split from inherited (publish.to_confirm.open distinct from publish.inherited.present)
- save_sheet_draft requires an active sheet; working copy adds the 2 MiB octet_length check
- delete_sheet_asset EXECUTE revoked from service_role (§5.4 surface)
- species table comment updated (§7.2); lease columns dropped from the reviews grant
- lock order sheet->version->round in decide/publish; conflict_retry kind (40001/40P01) in sheets.ts
Tests: pgTAP 016 (omitted status, separation, revoked-reviewer + renewal, ack carry,
inactive-species publish refusal, deprecated-delete, schema_version 1, content-write
without actor, working-copy positive), 001 census (no other writer of profile_roles).
871 real-stack pgTAP / 122 PGlite / 4 sandbox green; typecheck, lint, types drift clean.
This commit is contained in:
bchanot committed 2026-10-28 16:20:51 +01:00
1 parent 77e5fa4cf5
commit 3914eaa866
5 files changed
+280 -14

No files matched your search

@@ -168,6 +168,43 @@ $$;
comment on function private.sheet_all_reviewed(jsonb) is
'Declared defensive status check (contract §12.2): true when every annotation, finding category and finding node is reviewed (an absent review_status counts as inherited, so it is refused).';
-- True when any element is explicitly `to_confirm` (contract §6.7 keeps
-- `publish.to_confirm.open` apart from `publish.inherited.present`, which
-- covers an inherited or omitted status).
create or replace function private.sheet_has_to_confirm(p_doc jsonb)
returns boolean
language sql
immutable
set search_path = ''
as $$
select exists (
select 1 from jsonb_array_elements(coalesce(p_doc -> 'annotations', '[]'::jsonb)) a
where a ->> 'review_status' = 'to_confirm'
) or exists (
select 1
from jsonb_array_elements(coalesce(p_doc -> 'finding_zones', '[]'::jsonb)) z,
jsonb_array_elements(coalesce(z -> 'categories', '[]'::jsonb)) c
where c ->> 'review_status' = 'to_confirm'
) or exists (
select 1
from jsonb_array_elements(coalesce(p_doc -> 'finding_zones', '[]'::jsonb)) z,
jsonb_array_elements(coalesce(z -> 'categories', '[]'::jsonb)) c,
jsonb_path_query(c, '$.children[*].**{0 to 64}') n
where jsonb_typeof(n) = 'object' and n ? 'id' and n ->> 'review_status' = 'to_confirm'
);
$$;
-- True when the scope disclaimer is present and non-blank in FR and EN (O6).
create or replace function private.sheet_disclaimer_ok(p_doc jsonb)
returns boolean
language sql
immutable
set search_path = ''
as $$
select length(btrim(coalesce(p_doc -> 'scope_disclaimer' ->> 'fr', ''))) > 0
and length(btrim(coalesce(p_doc -> 'scope_disclaimer' ->> 'en', ''))) > 0;
$$;
-- ===========================================================================
-- 3. Triggers: revision, author set, freeze, extended publication guard
-- ===========================================================================
@@ -426,6 +463,10 @@ begin
if v_row.status <> 'draft' then
raise exception 'draft.not_a_draft' using errcode = '55000';
end if;
-- A draft of an active official sheet only (§6.2): an archived sheet is frozen.
if not exists (select 1 from public.anatomical_sheets s where s.id = v_row.sheet_id and s.status = 'active') then
raise exception 'sheet.inactive' using errcode = '55000';
end if;
if exists (select 1 from public.anatomical_sheet_reviews r
where r.version_id = v_row.id and r.status in ('verifying', 'open', 'approved')) then
raise exception 'draft.frozen' using errcode = '55000';
@@ -755,17 +796,20 @@ begin
if char_length(note) not between 1 and 4000 then
raise exception 'invalid_input' using errcode = 'P0001';
end if;
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = decide_sheet_review.review_id for update;
if v_review.id is null then
-- Lock order version then round (contract §6.1), matching publish, so the
-- two never deadlock.
select version_id into v_version.id from public.anatomical_sheet_reviews where id = decide_sheet_review.review_id;
if v_version.id is null then
raise exception 'not_found' using errcode = '42501';
end if;
select v.* into v_version from public.anatomical_sheet_versions v where v.id = v_version.id for update;
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = decide_sheet_review.review_id for update;
if v_review.status <> 'open' then
raise exception 'round.not_open' using errcode = '55000';
end if;
if v_review.authors @> array[v_actor] then
raise exception 'review.separation' using errcode = '42501';
end if;
select v.* into v_version from public.anatomical_sheet_versions v where v.id = v_review.version_id for update;
perform private.consume_catalog_write_budget(v_actor);
if approve then
@@ -774,9 +818,15 @@ begin
if v_review.verdict is null or v_review.revision <> v_version.revision then
raise exception 'verdict.stale' using errcode = 'P0001';
end if;
if private.sheet_has_to_confirm(v_version.content) then
raise exception 'publish.to_confirm.open' using errcode = 'P0001';
end if;
if not private.sheet_all_reviewed(v_version.content) then
raise exception 'publish.inherited.present' using errcode = 'P0001';
end if;
if not private.sheet_disclaimer_ok(v_version.content) then
raise exception 'publish.disclaimer.missing' using errcode = 'P0001';
end if;
v_uncovered := private.uncovered_review_groups(v_review.id);
if coalesce(array_length(v_uncovered, 1), 0) > 0 then
raise exception 'review.groups.uncovered' using errcode = 'P0001';
@@ -821,15 +871,18 @@ declare
v_name text;
v_description text;
begin
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = publish_sheet_version.review_id for update;
if v_review.id is null then
-- Lock order sheet, then version, then round (contract §6.1).
select v.sheet_id, v.id into v_sheet.id, v_version.id
from public.anatomical_sheet_versions v
join public.anatomical_sheet_reviews r on r.version_id = v.id
where r.id = publish_sheet_version.review_id;
if v_version.id is null then
raise exception 'not_found' using errcode = '42501';
end if;
select s.* into v_sheet from public.anatomical_sheets s
join public.anatomical_sheet_versions v on v.sheet_id = s.id
where v.id = v_review.version_id for update;
select s.* into v_sheet from public.anatomical_sheets s where s.id = v_sheet.id for update;
v_actor := private.require_sheet_publisher(v_sheet.id);
select v.* into v_version from public.anatomical_sheet_versions v where v.id = v_review.version_id for update;
select v.* into v_version from public.anatomical_sheet_versions v where v.id = v_version.id for update;
select r.* into v_review from public.anatomical_sheet_reviews r where r.id = publish_sheet_version.review_id for update;
if v_sheet.status <> 'active' then
raise exception 'sheet.inactive' using errcode = '55000';
@@ -846,6 +899,11 @@ begin
if v_review.verdict is null then
raise exception 'verdict.missing' using errcode = 'P0001';
end if;
-- The approving reviewer's grant must still be active at publication (§5.2,
-- §6.7): a revoked reviewer's approval does not carry a publication.
if not private.grant_active(v_review.decided_by, 'sheet_reviewer') then
raise exception 'publish.approval.invalid' using errcode = 'P0001';
end if;
-- Species active and not `other` (§6.7).
if not exists (select 1 from public.species s where s.id = v_sheet.species_id and s.active) then
raise exception 'publish.species.inactive' using errcode = 'P0001';
@@ -853,10 +911,16 @@ begin
if v_sheet.species_id = 'other' then
raise exception 'publish.species.other' using errcode = 'P0001';
end if;
-- Status check kept in SQL (§12.2): no to_confirm / inherited element.
-- Status check kept in SQL (§12.2): no to_confirm, no inherited (or omitted).
if private.sheet_has_to_confirm(v_version.content) then
raise exception 'publish.to_confirm.open' using errcode = 'P0001';
end if;
if not private.sheet_all_reviewed(v_version.content) then
raise exception 'publish.inherited.present' using errcode = 'P0001';
end if;
if not private.sheet_disclaimer_ok(v_version.content) then
raise exception 'publish.disclaimer.missing' using errcode = 'P0001';
end if;
-- Groups still covered at publication.
if coalesce(array_length(private.uncovered_review_groups(v_review.id), 1), 0) > 0 then
raise exception 'review.groups.uncovered' using errcode = 'P0001';
@@ -1141,6 +1205,10 @@ begin
v_map := v_map || jsonb_build_object(r.id::text, v_new::text);
end loop;
-- Size checked as in §6.2: the remapped document must fit the 2 MiB cap.
if octet_length(private.remap_document_assets(v_source.content, v_map)::text) > 2097152 then
raise exception 'document.too_large' using errcode = '23514';
end if;
v_draft := private.insert_sheet_draft(
v_actor, v_sheet, private.remap_document_assets(v_source.content, v_map),
'Copie de travail de la fiche historique (version ' || v_source.version_number || ', révision ' || v_source.revision || ')'
@@ -1531,7 +1599,14 @@ create trigger anatomical_sheet_versions_guard_update
revoke all on public.anatomical_sheet_reviews from public, anon, authenticated, service_role;
revoke all on public.anatomical_sheet_review_comments from public, anon, authenticated, service_role;
revoke all on public.anatomical_sheet_review_acknowledgments from public, anon, authenticated, service_role;
grant select on public.anatomical_sheet_reviews to authenticated;
-- The lease columns (attempts, lease_token, locked_at) are the worker's, read
-- only by the M6 service-role RPCs; the admin screens never see them, as the
-- avatar lease pattern hides its own (20261013100000_files_foundation.sql).
grant select (id, version_id, round, revision, authors, submitted_by, submitted_at,
submission_note, status, verdict, verified_at, verified_with,
failure_outcome, failure_code, decided_by, decided_at, decision_note,
withdrawn_by, withdrawn_at, withdrawal_note, created_at)
on public.anatomical_sheet_reviews to authenticated;
grant select on public.anatomical_sheet_review_comments to authenticated;
grant select on public.anatomical_sheet_review_acknowledgments to authenticated;
-- The editor needs the draft revision for the save-conflict check (§6.2); the
@@ -1609,6 +1684,8 @@ $$;
-- Private helpers: EXECUTE for nobody except the predicates the review
-- policies evaluate (caller_can_see_official_sheet, granted in M4).
revoke all on function private.sheet_all_reviewed(jsonb) from public, anon, authenticated, service_role;
revoke all on function private.sheet_has_to_confirm(jsonb) from public, anon, authenticated, service_role;
revoke all on function private.sheet_disclaimer_ok(jsonb) from public, anon, authenticated, service_role;
revoke all on function private.on_sheet_version_content() from public, anon, authenticated, service_role;
revoke all on function private.guard_sheet_version_update() from public, anon, authenticated, service_role;
revoke all on function private.require_sheet_author(uuid) from public, anon, authenticated, service_role;
@@ -1622,3 +1699,13 @@ revoke all on function private.set_sheet_status(uuid, uuid, public.sheet_status,
-- longer called). Drop them so no stale path survives.
drop function if exists private.require_sheet_editor(uuid, uuid);
drop function if exists private.require_version_editor(uuid, uuid);
-- delete_sheet_asset survives from the base migration (editor, aal2, via
-- can_edit_sheet); the service role never needs it (§5.4 limits its surface to
-- the worker RPCs, the upload confirmation and the operator read).
revoke all on function public.delete_sheet_asset(uuid) from service_role;
-- §7.2: the species table is no longer write-only through migrations; editors
-- create inactive species and the owner activates them, by audited RPC.
comment on table public.species is
'Species of animals and of anatomical sheets. Written only by the audited RPCs create_species (editor, inactive), activate_species / rename_species (owner), update_inactive_species (editor); no direct API write path.';