fix(supabase): V1-2.1 bound printed numbers before the size measure

check_document_structure sums the numbers' printed digits one at a time
before building the whole text, so huge exponents answer
document.too_large instead of an out-of-memory error. pgTAP 020: the
full AR-1 B matrix and the rights RPCs refused to every non-owner; 017:
exponent vectors, the number bomb, publish.species.other alone.
This commit is contained in:
bchanot committed 2026-10-28 20:08:18 +01:00
1 parent 12816671e4
commit 3eba6d89b2
4 files changed
+198 -3

No files matched your search

@@ -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;