diff --git a/supabase/migrations/20261028110000_sheet_review_workflow.sql b/supabase/migrations/20261028110000_sheet_review_workflow.sql index db2f46c7..84f06fd8 100644 --- a/supabase/migrations/20261028110000_sheet_review_workflow.sql +++ b/supabase/migrations/20261028110000_sheet_review_workflow.sql @@ -403,13 +403,20 @@ $$; -- working copy (contract §12.2): an object whose id-bearing collections are -- 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). +-- the caller gets the closed code, never a raw constraint error). A number +-- prints as plain digits (`1e131071` is 131 072 bytes of text), so the printed +-- numbers are summed one at a time first: the whole text is built only once it +-- stays within the input's size plus the cap, never a gigabyte from a small +-- payload (an out-of-memory error instead of the closed code). create or replace function private.check_document_structure(p_doc jsonb) returns void language plpgsql immutable set search_path = '' as $$ +declare + v_number jsonb; + v_number_bytes bigint := 0; begin if not private.sheet_document_has_shape(p_doc) then raise exception 'document.shape' using errcode = '23514'; @@ -417,6 +424,12 @@ begin if (p_doc -> 'schema_version') is distinct from '2'::jsonb then raise exception 'document.schema_version' using errcode = '23514'; end if; + for v_number in select jsonb_path_query(p_doc, 'strict $.** ? (@.type() == "number")') loop + v_number_bytes := v_number_bytes + octet_length(v_number::text); + if v_number_bytes > 2097152 then + raise exception 'document.too_large' using errcode = '23514'; + end if; + end loop; if octet_length(p_doc::text) > 2097152 then raise exception 'document.too_large' using errcode = '23514'; end if; diff --git a/supabase/tests/003_platform_roles.sql b/supabase/tests/003_platform_roles.sql index 515ee044..7d1967c0 100644 --- a/supabase/tests/003_platform_roles.sql +++ b/supabase/tests/003_platform_roles.sql @@ -1,7 +1,7 @@ -- Platform roles: bootstrap, AR-1 B (live aal2 session required), delegation -- rules, every denied path, visibility. Since V1-2.1 (M4) every role change -- needs an aal2 session that still exists in auth.sessions and, for a grant, --- a verified TOTP factor on the target. Full AR-1 matrix: 015. +-- a verified TOTP factor on the target. AR-1 cases: 015; full matrix: 020. begin; select plan(25); diff --git a/supabase/tests/017_sheet_review_rules.sql b/supabase/tests/017_sheet_review_rules.sql index c2da973c..61201690 100644 --- a/supabase/tests/017_sheet_review_rules.sql +++ b/supabase/tests/017_sheet_review_rules.sql @@ -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(104); +select plan(110); -- Accounts: O owner, E admin + editor + publisher, V reviewer, V2 admin + -- editor + reviewer (an author of a later round), V3 reviewer, R reader. @@ -604,6 +604,19 @@ select pg_temp.fixture_round('o2', 'o2.f', 'c', 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)'); +-- ============ Publication refused for the species `other` alone (§6.7) ============ +-- No RPC creates such a sheet (`sheet.species_invalid`); a row moved to `other` +-- by the table owner after an otherwise valid approval stands in for it. +select pg_temp.sheet('so', repeat('b6', 32)); +select pg_temp.fixture_round('so', 'so.f', 'd', jsonb_set(pg_temp.doc(pg_temp.id('so.asset'), 'reviewed'), '{description,fr}', '"so"'), 'E', 'V', 'V'); +reset role; +alter table public.anatomical_sheets disable trigger user; +update public.anatomical_sheets set species_id = 'other' where id = pg_temp.id('so'); +alter table public.anatomical_sheets enable trigger user; +select pg_temp.as_user('E'); +select throws_ok($$ select public.publish_sheet_version(pg_temp.id('so.f')) $$, + 'P0001', 'publish.species.other', 'publication refused for the species other, every other rule met'); + -- ============ 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')) $$, @@ -650,6 +663,26 @@ select ok((select octet_length(content::text) <= 2097152 and jsonb_array_length( 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'); +-- Numbers count as PostgreSQL prints them: plain digits, never an exponent. +select lives_ok($$ select pg_temp.save_now('sz', jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', '[1e-7, 1e21]')) $$, + 'size: numbers that JavaScript prints with an exponent are saved'); +reset role; +select is((select content -> 'pad' #>> '{}' from public.anatomical_sheet_versions where id = pg_temp.id('sz.v')), + '[0.0000001, 1000000000000000000000]', 'size: they are measured in their plain form'); +create table pg_temp.sz_exp as select jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', + (select jsonb_agg(1e21) from generate_series(1, 100000))) as d; +grant select on pg_temp.sz_exp to authenticated; +select ok((select octet_length(replace(replace(d::text, ', ', ','), ': ', ':')) - 17 * 100000 <= 2097152 + and octet_length(d::text) > 2097152 from pg_temp.sz_exp), + 'size: the exponent fixture is under the cap in compact JavaScript form and over it as measured'); +select pg_temp.as_user('E'); +select throws_ok($$ select pg_temp.save_now('sz', (select d from pg_temp.sz_exp)) $$, + '23514', 'document.too_large', 'size: a document over the cap only once its numbers print in full is refused'); +-- 9 000 numbers of 131 072 digits each: a 90 kB payload whose text would be +-- 1.2 GB. The closed code comes back, not an out-of-memory error. +select throws_ok($$ select pg_temp.save_now('sz', jsonb_set(pg_temp.doc(pg_temp.id('sz.asset'), 'reviewed'), '{pad}', + (select jsonb_agg(1e131071) from generate_series(1, 9000)))) $$, + '23514', 'document.too_large', 'size: numbers that print as gigabytes of digits get the closed code, not an out-of-memory error'); 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))', diff --git a/supabase/tests/020_role_rights_matrix.sql b/supabase/tests/020_role_rights_matrix.sql new file mode 100644 index 00000000..14a22070 --- /dev/null +++ b/supabase/tests/020_role_rights_matrix.sql @@ -0,0 +1,149 @@ +-- V1-2.1 M4: the AR-1 B matrix of contract §15 in full, and the catalogue +-- rights RPCs refused to every account that is not an owner at aal2 (§5.2, +-- §5.4; M6 keeps « no self-grant for a non-owner »). Table-driven: every +-- caller class (no role, owner, admin, moderator, support, deactivated owner) +-- at aal1 and aal2 grants and revokes each of the four roles, on itself and on +-- another account; expected: aal1 always `mfa_required`, self `self_target`, +-- aal2 allowed only where can_manage_platform_role allows (owner any role, +-- admin moderator and support), else `forbidden`. Every refused call leaves the +-- target's roles and the audit log unchanged. Fixtures are scoped to the ids +-- created here. +begin; +select plan(449); + +create table pg_temp.who (label text primary key, id uuid not null); +insert into pg_temp.who values + ('N', '20000000-0000-0000-0000-000000000001'), ('O', '20000000-0000-0000-0000-000000000002'), + ('A', '20000000-0000-0000-0000-000000000003'), ('M', '20000000-0000-0000-0000-000000000004'), + ('S', '20000000-0000-0000-0000-000000000005'), ('D', '20000000-0000-0000-0000-000000000006'), + ('E', '20000000-0000-0000-0000-000000000007'), ('V', '20000000-0000-0000-0000-000000000008'), + ('P', '20000000-0000-0000-0000-000000000009'), ('R', '20000000-0000-0000-0000-00000000000a'), + ('T', '20000000-0000-0000-0000-0000000000ff'); +insert into auth.users (id, email, email_confirmed_at) +select id, lower(label) || '@m20.local', now() from pg_temp.who; +insert into auth.mfa_factors (id, user_id, factor_type, status, created_at, updated_at) +select gen_random_uuid(), id, 'totp', 'verified', now(), now() from pg_temp.who; +-- Two live sessions per account: 2010… at aal2, 2020… at aal1. +insert into auth.sessions (id, user_id, aal) +select ('20100000' || substr(id::text, 9))::uuid, id, 'aal2'::auth.aal_level from pg_temp.who +union all +select ('20200000' || substr(id::text, 9))::uuid, id, 'aal1'::auth.aal_level from pg_temp.who; + +select set_config('request.jwt.claims', '', true); +select private.assign_platform_role(null, id, r::public.platform_role) +from (values ('O', 'owner'), ('A', 'admin'), ('M', 'moderator'), ('S', 'support'), ('D', 'owner'), + ('E', 'admin'), ('P', 'admin')) v(l, r) +join pg_temp.who w on w.label = v.l; +update public.profiles set active = false where id = '20000000-0000-0000-0000-000000000006'; +insert into public.anatomical_catalog_grants (user_id, grant_kind, reason, verification_note, granted_by) +select w.id, v.k::public.catalog_grant_kind, 'm20', case when v.k = 'catalog_reader' then null else 'c' end, + '20000000-0000-0000-0000-000000000002' +from (values ('E', 'sheet_editor'), ('V', 'sheet_reviewer'), ('P', 'sheet_publisher'), ('R', 'catalog_reader')) v(l, k) +join pg_temp.who w on w.label = v.l; + +create function pg_temp.uid(p text) returns uuid language sql stable as $$ select id from pg_temp.who where label = p $$; +-- The calling session of
at