fix(supabase): V1-2.1 owner self-review grant and the text size measure
M6 option A: grant_catalog_right no longer refuses an owner granting itself sheet_reviewer; authors stay refused at acknowledgment, approval and publication. The document cap is the UTF-8 bytes of the JSON text: check_document_structure drops pg_column_size and M5 replaces the base table CHECK by octet_length(content::text). pgTAP 015 and 017 cover the owner-reviewer journey and cap - 1, cap, cap + 1, multi-byte, dense.
This commit is contained in:
1 parent
2af61e72cc
commit
b1de87b5d0
6 files changed
+131
-34
No files matched your search
@@ -456,12 +456,9 @@ begin
|
||||
or grant_catalog_right.expires_at <= now() then
|
||||
raise exception 'invalid_input' using errcode = 'P0001';
|
||||
end if;
|
||||
-- An owner may hold the editor and publisher capabilities it needs (journey
|
||||
-- A0) but never reviews on its own grant: a reviewer is habilitated by
|
||||
-- another owner, so the separation of duties keeps two people.
|
||||
if grant_catalog_right.user_id = v_actor and grant_catalog_right.kind = 'sheet_reviewer' then
|
||||
raise exception 'grant.self_review' using errcode = 'P0001';
|
||||
end if;
|
||||
-- An owner may grant itself any capability, `sheet_reviewer` included (M6,
|
||||
-- owner decision 2026-10-28): the separation of duties is a per-round rule,
|
||||
-- so no author of a round acknowledges, approves or counts at publication.
|
||||
|
||||
-- Serialises the grants of one account: one active row per kind.
|
||||
perform 1 from public.profiles p where p.id = grant_catalog_right.user_id for no key update;
|
||||
|
||||
@@ -43,6 +43,15 @@ alter table public.anatomical_sheet_versions
|
||||
add constraint anatomical_sheet_versions_published_fingerprint
|
||||
check (status <> 'published' or document_fingerprint is not null);
|
||||
|
||||
-- The document cap is the contract's measure (§12.2): the UTF-8 bytes of the
|
||||
-- document's JSON text, `octet_length(content::text)`. The base CHECK measured
|
||||
-- the binary jsonb (`pg_column_size`), larger than the text for a document of
|
||||
-- many small values, so a conforming draft could hit a raw constraint error.
|
||||
alter table public.anatomical_sheet_versions
|
||||
drop constraint anatomical_sheet_versions_content_size,
|
||||
add constraint anatomical_sheet_versions_content_size
|
||||
check (octet_length(content::text) <= 2097152);
|
||||
|
||||
alter table public.anatomical_sheets
|
||||
add column working_copy_source_version_id uuid references public.anatomical_sheet_versions (id) on delete set null;
|
||||
|
||||
@@ -392,11 +401,9 @@ $$;
|
||||
|
||||
-- The structural guards a stored document passes at creation, save and
|
||||
-- working copy (contract §12.2): an object whose id-bearing collections are
|
||||
-- arrays, schema version 2, at most 2 MiB serialised. The table's own size
|
||||
-- CHECK (`pg_column_size(content)`, base migration) measures the binary jsonb,
|
||||
-- which a document of many small objects makes larger than its text: both
|
||||
-- measures are checked here, so either limit answers the closed code rather
|
||||
-- than a raw constraint error.
|
||||
-- arrays, schema version 2, at most 2 MiB as the UTF-8 bytes of its JSON text
|
||||
-- (the table CHECK of section 1 uses the same measure; checked here first so
|
||||
-- the caller gets the closed code, never a raw constraint error).
|
||||
create or replace function private.check_document_structure(p_doc jsonb)
|
||||
returns void
|
||||
language plpgsql
|
||||
@@ -410,7 +417,7 @@ begin
|
||||
if (p_doc -> 'schema_version') is distinct from '2'::jsonb then
|
||||
raise exception 'document.schema_version' using errcode = '23514';
|
||||
end if;
|
||||
if octet_length(p_doc::text) > 2097152 or pg_column_size(p_doc) > 2097152 then
|
||||
if octet_length(p_doc::text) > 2097152 then
|
||||
raise exception 'document.too_large' using errcode = '23514';
|
||||
end if;
|
||||
end;
|
||||
|
||||
@@ -162,10 +162,11 @@ select lives_ok(
|
||||
select throws_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-00000000000c','sheet_reviewer','partner','DVM verified') $$,
|
||||
'P0001', 'grant.ineligible', 'a capability is refused to an account without a verified factor');
|
||||
-- the owner cannot grant itself sheet_reviewer
|
||||
select throws_ok(
|
||||
-- the owner (aal2, verified factor) may grant itself sheet_reviewer (M6 option A);
|
||||
-- the separation of duties is per round (017)
|
||||
select lives_ok(
|
||||
$$ select public.grant_catalog_right('d0000000-0000-0000-0000-000000000001','sheet_reviewer','self','x') $$,
|
||||
'P0001', 'grant.self_review', 'the owner cannot review its own drafts');
|
||||
'the owner grants itself sheet_reviewer');
|
||||
|
||||
-- grant_active reflects eligibility, expiry and revocation (checked as the
|
||||
-- table owner: grant_active is internal, not granted to any API role).
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
-- ids created here, so the file also runs on a stack that already holds
|
||||
-- sheets, rounds or a working copy.
|
||||
begin;
|
||||
select plan(85);
|
||||
select plan(104);
|
||||
|
||||
-- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin +
|
||||
-- editor + reviewer (an author of a later round), V3 reviewer, R reader.
|
||||
@@ -144,6 +144,18 @@ begin
|
||||
end if;
|
||||
perform pg_temp.as_user('E');
|
||||
end $$;
|
||||
-- A document whose JSON text (`::text`, the contract's measure, §12.2) is
|
||||
-- exactly p_bytes UTF-8 bytes: the base plus a `pad` string of p_unit
|
||||
-- characters, completed with ASCII.
|
||||
create function pg_temp.sized(p_base jsonb, p_bytes int, p_unit text) returns jsonb language sql as $$
|
||||
select p_base || jsonb_build_object('pad', repeat(p_unit, f / octet_length(p_unit)) || repeat('x', f % octet_length(p_unit)))
|
||||
from (select p_bytes - octet_length((p_base || '{"pad": ""}'::jsonb)::text) as f) s;
|
||||
$$;
|
||||
-- Saves <doc> on the draft <p> at its current revision.
|
||||
create function pg_temp.save_now(p text, p_doc jsonb) returns void language sql as $$
|
||||
select public.save_sheet_draft(pg_temp.id(p || '.v'),
|
||||
(select revision from public.anatomical_sheet_versions where id = pg_temp.id(p || '.v')), p_doc, 'size');
|
||||
$$;
|
||||
grant all on pg_temp.k to authenticated;
|
||||
grant execute on all functions in schema pg_temp to authenticated;
|
||||
|
||||
@@ -517,13 +529,6 @@ select throws_ok($$ select public.create_official_sheet('r17-s13', pg_temp.doc(n
|
||||
'P0001', 'sheet.slug_taken', 'a slug already used by an official sheet');
|
||||
select throws_ok($$ select public.create_official_sheet('Bad Slug', pg_temp.doc(null, 'reviewed')) $$,
|
||||
'P0001', 'invalid_input', 'a slug outside the format');
|
||||
select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1,
|
||||
jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{pad}', to_jsonb(repeat('x', 2100000))), 'x') $$,
|
||||
'23514', 'document.too_large', 'save refuses a document over 2 MiB serialised');
|
||||
select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1,
|
||||
jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{pad}',
|
||||
(select jsonb_agg(jsonb_build_object('a', 1)) from generate_series(1, 100000))), 'x') $$,
|
||||
'23514', 'document.too_large', 'a document under 2 MiB as text but over it as stored jsonb gets the closed code');
|
||||
select throws_ok($$ select public.save_sheet_draft(pg_temp.id('s13.v'), 1,
|
||||
jsonb_set(pg_temp.doc(pg_temp.id('s13.asset'), 'reviewed'), '{species_id}', '"canine"'), 'x') $$,
|
||||
'23514', 'document.species', 'save refuses a document of another species');
|
||||
@@ -562,6 +567,94 @@ select lives_ok($$ select public.delete_anatomical_sheet(pg_temp.id('s13')) $$,
|
||||
select is((select count(*) from public.anatomical_sheets where id = pg_temp.id('s13')), 0::bigint,
|
||||
'a deleted sheet is invisible, capability holders included');
|
||||
|
||||
-- ============ M6: an owner may review, never a round it authored (§5.2, §5.5) ============
|
||||
select pg_temp.as_user('O');
|
||||
select lives_ok($$ select public.grant_catalog_right(pg_temp.u('O'), 'sheet_reviewer', 'owner reviews', 'DVM, owner') $$,
|
||||
'M6: the owner at aal2 grants itself sheet_reviewer');
|
||||
select public.grant_catalog_right(pg_temp.u('O'), 'sheet_editor', 'owner edits', 'owner');
|
||||
-- o1: written by E only; the owner acknowledges, approves, E publishes.
|
||||
select pg_temp.sheet('o1', repeat('c1', 32));
|
||||
select pg_temp.round('o1', 'o1.r', 1, repeat('c1', 32), pg_temp.groups3());
|
||||
select pg_temp.as_user('O');
|
||||
select lives_ok($$ select public.acknowledge_sheet_review_group(pg_temp.id('o1.r'), g ->> 'key', g ->> 'digest')
|
||||
from jsonb_array_elements(pg_temp.groups3()) g $$,
|
||||
'M6: the owner-reviewer acknowledges the groups of a draft written by another author');
|
||||
select lives_ok($$ select public.decide_sheet_review(pg_temp.id('o1.r'), true, 'ok') $$,
|
||||
'M6: the owner-reviewer approves a version written by another author');
|
||||
select pg_temp.as_user('E');
|
||||
select is(public.publish_sheet_version(pg_temp.id('o1.r')), pg_temp.id('o1.v'),
|
||||
'M6: the owner''s approval publishes when every other rule is met');
|
||||
-- o2: the owner saves a variant, so it is an author of the round.
|
||||
select pg_temp.sheet('o2', repeat('b6', 32));
|
||||
select pg_temp.as_user('O');
|
||||
select public.save_sheet_draft(pg_temp.id('o2.v'), 1,
|
||||
jsonb_set(pg_temp.doc(pg_temp.id('o2.asset'), 'reviewed'), '{description,fr}', '"o"'), 'owner edit');
|
||||
select pg_temp.round('o2', 'o2.r', 2, repeat('b6', 32), pg_temp.groups3());
|
||||
select pg_temp.as_user('O');
|
||||
select throws_ok($$ select public.acknowledge_sheet_review_group(pg_temp.id('o2.r'), 'general', repeat('1', 64)) $$,
|
||||
'42501', 'review.separation', 'M6: the owner-reviewer cannot acknowledge a round it authored');
|
||||
select throws_ok($$ select public.decide_sheet_review(pg_temp.id('o2.r'), true, 'ok') $$,
|
||||
'42501', 'review.separation', 'M6: the owner-reviewer cannot approve its own version');
|
||||
-- An approval by an author (written as the table owner, the RPC refuses it) is
|
||||
-- refused again at publication.
|
||||
select pg_temp.as_user('E');
|
||||
select public.withdraw_sheet_review(pg_temp.id('o2.r'), 'next');
|
||||
select pg_temp.fixture_round('o2', 'o2.f', 'c',
|
||||
jsonb_set(pg_temp.doc(pg_temp.id('o2.asset'), 'reviewed'), '{description,fr}', '"o2"'), 'O', 'O', 'V');
|
||||
select throws_ok($$ select public.publish_sheet_version(pg_temp.id('o2.f')) $$,
|
||||
'P0001', 'publish.approval.invalid', 'M6: publication refused after an approval by an author (the owner)');
|
||||
|
||||
-- ============ Document size: UTF-8 bytes of the JSON text (§6.2, §12.2) ============
|
||||
select pg_temp.sheet('sz', repeat('c3', 32));
|
||||
select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097151, 'x')) $$,
|
||||
'size: a document of cap − 1 bytes is saved');
|
||||
reset role;
|
||||
select is((select octet_length(content::text) from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 2097151,
|
||||
'size: the stored document measures cap − 1 bytes');
|
||||
select pg_temp.as_user('E');
|
||||
select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097152, 'x')) $$,
|
||||
'size: a document of exactly 2 MiB is saved');
|
||||
reset role;
|
||||
select is((select octet_length(content::text) from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), 2097152,
|
||||
'size: the stored document measures exactly the cap');
|
||||
select pg_temp.as_user('E');
|
||||
select throws_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097153, 'x')) $$,
|
||||
'23514', 'document.too_large', 'size: a document of cap + 1 bytes gets the closed code');
|
||||
-- Multi-byte text: the cap counts bytes, not characters.
|
||||
select lives_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097152, '€')) $$,
|
||||
'size: a document of exactly 2 MiB written in 3-byte characters is saved');
|
||||
reset role;
|
||||
select ok((select octet_length(content::text) = 2097152 and char_length(content::text) < 800000
|
||||
from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')),
|
||||
'size: that document is 2 MiB in bytes and far fewer characters');
|
||||
select ok(char_length(pg_temp.sized(pg_temp.doc(null, 'reviewed'), 2097153, '𝄞')::text) < 600000,
|
||||
'size: the 4-byte document below is far under the cap in characters');
|
||||
select pg_temp.as_user('E');
|
||||
select throws_ok($$ select pg_temp.save_now('sz', pg_temp.sized(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), 2097153, '𝄞')) $$,
|
||||
'23514', 'document.too_large', 'size: one byte over the cap in 4-byte characters gets the closed code');
|
||||
-- A dense document: within the cap as text, over it as binary jsonb (`|| '{}'`
|
||||
-- measures a fresh, uncompressed value, as the RPC receives it).
|
||||
reset role;
|
||||
create table pg_temp.sz_dense as select jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}',
|
||||
(select jsonb_agg(jsonb_build_object('a', 1)) from generate_series(1, 100000))) as d;
|
||||
grant select on pg_temp.sz_dense to authenticated;
|
||||
select ok((select octet_length(d::text) <= 2097152 and pg_column_size(d || '{}'::jsonb) > 2097152 from pg_temp.sz_dense),
|
||||
'size: the dense fixture fits the cap as text and exceeds it as binary jsonb');
|
||||
select pg_temp.as_user('E');
|
||||
select lives_ok($$ select pg_temp.save_now('sz', (select d from pg_temp.sz_dense)) $$,
|
||||
'size: a dense document within the cap as text is saved (the binary jsonb size is not the measure)');
|
||||
reset role;
|
||||
select ok((select octet_length(content::text) <= 2097152 and jsonb_array_length(content -> 'pad') = 100000
|
||||
from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')),
|
||||
'size: the dense document is stored whole');
|
||||
select pg_temp.as_user('E');
|
||||
select throws_ok($$ select public.create_official_sheet('r17-szbig', pg_temp.sized(pg_temp.doc(null, 'reviewed'), 2097153, 'x')) $$,
|
||||
'23514', 'document.too_large', 'size: creation refuses a document of cap + 1 bytes with the closed code');
|
||||
reset role;
|
||||
select is((select pg_get_constraintdef(oid) from pg_constraint where conname = 'anatomical_sheet_versions_content_size'),
|
||||
'CHECK ((octet_length((content)::text) <= 2097152))',
|
||||
'size: the table CHECK uses the same measure as the RPCs');
|
||||
|
||||
-- ============ Audit (§13): every lifecycle step of a sheet is recorded ============
|
||||
reset role;
|
||||
select results_eq(
|
||||
|
||||
Reference in new issue
Block a user