feat(supabase): files foundation — assets table, private buckets, avatar policies through assets, purge queue, browser and worker RPCs
Migration 20261013100000: asset_status enum, public.assets (server-built keys, lease token, closed rejection reasons), seven private buckets (avatars open with cap and MIME list, six closed), storage.objects SELECT/INSERT policies for avatars evaluated through assets, private.object_purge_queue fed by status and delete triggers (48 h / 1 h grace), storage ceiling, RPCs begin_avatar_upload / confirm_asset_upload / remove_avatar (authenticated) and claim_avatar_jobs / complete_avatar_job / reject_avatar_job / mark_quarantine_purged / claim_purges / ack_purge / claim_quarantine_leftovers / expire_pending_uploads (service_role), explicit revokes and column grants. pgTAP 010 (125 checks: allowed and denied twins, policy census, stale lease, supersede, removal, leftovers, cascade, ceiling), 001 census extended, database.types.ts regenerated from the real stack, schema-types test counts input arguments only.
This commit is contained in:
1 parent
3f1105bf39
commit
614887470c
5 files changed
+1346
-6
No files matched your search
@@ -239,6 +239,75 @@ export type Database = {
|
||||
},
|
||||
]
|
||||
}
|
||||
assets: {
|
||||
Row: {
|
||||
attempts: number
|
||||
bucket_id: string
|
||||
byte_size: number | null
|
||||
created_at: string
|
||||
declared_mime: string
|
||||
declared_size: number
|
||||
id: string
|
||||
kind: string
|
||||
lock_token: string | null
|
||||
locked_at: string | null
|
||||
object_key: string
|
||||
owner_id: string
|
||||
quarantine_key: string | null
|
||||
rejection_reason: string | null
|
||||
sha256: string | null
|
||||
status: Database['public']['Enums']['asset_status']
|
||||
updated_at: string
|
||||
}
|
||||
ComputedFields: never
|
||||
Insert: {
|
||||
attempts?: number
|
||||
bucket_id: string
|
||||
byte_size?: number | null
|
||||
created_at?: string
|
||||
declared_mime: string
|
||||
declared_size: number
|
||||
id?: string
|
||||
kind: string
|
||||
lock_token?: string | null
|
||||
locked_at?: string | null
|
||||
object_key: string
|
||||
owner_id: string
|
||||
quarantine_key?: string | null
|
||||
rejection_reason?: string | null
|
||||
sha256?: string | null
|
||||
status?: Database['public']['Enums']['asset_status']
|
||||
updated_at?: string
|
||||
}
|
||||
Update: {
|
||||
attempts?: number
|
||||
bucket_id?: string
|
||||
byte_size?: number | null
|
||||
created_at?: string
|
||||
declared_mime?: string
|
||||
declared_size?: number
|
||||
id?: string
|
||||
kind?: string
|
||||
lock_token?: string | null
|
||||
locked_at?: string | null
|
||||
object_key?: string
|
||||
owner_id?: string
|
||||
quarantine_key?: string | null
|
||||
rejection_reason?: string | null
|
||||
sha256?: string | null
|
||||
status?: Database['public']['Enums']['asset_status']
|
||||
updated_at?: string
|
||||
}
|
||||
Relationships: [
|
||||
{
|
||||
foreignKeyName: 'assets_owner_id_fkey'
|
||||
columns: ['owner_id']
|
||||
isOneToOne: false
|
||||
referencedRelation: 'profiles'
|
||||
referencedColumns: ['id']
|
||||
},
|
||||
]
|
||||
}
|
||||
profile_roles: {
|
||||
Row: {
|
||||
granted_at: string
|
||||
@@ -380,7 +449,50 @@ export type Database = {
|
||||
[_ in never]: never
|
||||
}
|
||||
Functions: {
|
||||
ack_purge: { Args: { purge_id: string }; Returns: undefined }
|
||||
archive_anatomical_sheet: { Args: { sheet_id: string }; Returns: string }
|
||||
begin_avatar_upload: {
|
||||
Args: { declared_mime: string; declared_size: number }
|
||||
Returns: Json
|
||||
}
|
||||
claim_avatar_jobs: {
|
||||
Args: { max_jobs: number }
|
||||
Returns: {
|
||||
asset_id: string
|
||||
attempts: number
|
||||
bucket_id: string
|
||||
lock_token: string
|
||||
object_key: string
|
||||
owner_id: string
|
||||
quarantine_key: string
|
||||
}[]
|
||||
}
|
||||
claim_purges: {
|
||||
Args: { max_items: number }
|
||||
Returns: {
|
||||
bucket_id: string
|
||||
object_name: string
|
||||
purge_id: string
|
||||
}[]
|
||||
}
|
||||
claim_quarantine_leftovers: {
|
||||
Args: { max_items: number; older_than_seconds: number }
|
||||
Returns: {
|
||||
asset_id: string
|
||||
bucket_id: string
|
||||
object_name: string
|
||||
}[]
|
||||
}
|
||||
complete_avatar_job: {
|
||||
Args: {
|
||||
asset_id: string
|
||||
byte_size: number
|
||||
lock_token: string
|
||||
sha256: string
|
||||
}
|
||||
Returns: undefined
|
||||
}
|
||||
confirm_asset_upload: { Args: { asset_id: string }; Returns: undefined }
|
||||
confirm_sheet_asset_upload: {
|
||||
Args: { asset_id: string }
|
||||
Returns: string
|
||||
@@ -412,6 +524,10 @@ export type Database = {
|
||||
Args: { reason?: string; version_id: string }
|
||||
Returns: string
|
||||
}
|
||||
expire_pending_uploads: {
|
||||
Args: { max_age_seconds: number }
|
||||
Returns: number
|
||||
}
|
||||
fork_anatomical_sheet: {
|
||||
Args: {
|
||||
name: string
|
||||
@@ -428,6 +544,7 @@ export type Database = {
|
||||
}
|
||||
Returns: string
|
||||
}
|
||||
mark_quarantine_purged: { Args: { asset_id: string }; Returns: undefined }
|
||||
publish_sheet_version: { Args: { version_id: string }; Returns: string }
|
||||
register_sheet_asset: {
|
||||
Args: {
|
||||
@@ -438,6 +555,11 @@ export type Database = {
|
||||
}
|
||||
Returns: string
|
||||
}
|
||||
reject_avatar_job: {
|
||||
Args: { asset_id: string; lock_token: string; reason: string }
|
||||
Returns: undefined
|
||||
}
|
||||
remove_avatar: { Args: Record<PropertyKey, never>; Returns: number }
|
||||
restore_anatomical_sheet: { Args: { sheet_id: string }; Returns: string }
|
||||
revoke_platform_role: {
|
||||
Args: {
|
||||
@@ -456,6 +578,12 @@ export type Database = {
|
||||
}
|
||||
}
|
||||
Enums: {
|
||||
asset_status:
|
||||
| 'pending_upload'
|
||||
| 'uploaded'
|
||||
| 'clean'
|
||||
| 'rejected'
|
||||
| 'deleted'
|
||||
platform_role: 'owner' | 'admin' | 'moderator' | 'support'
|
||||
sheet_status: 'active' | 'archived' | 'deleted'
|
||||
sheet_version_status: 'draft' | 'published' | 'deprecated'
|
||||
@@ -587,6 +715,13 @@ export type CompositeTypes<
|
||||
export const Constants = {
|
||||
public: {
|
||||
Enums: {
|
||||
asset_status: [
|
||||
'pending_upload',
|
||||
'uploaded',
|
||||
'clean',
|
||||
'rejected',
|
||||
'deleted',
|
||||
],
|
||||
platform_role: ['owner', 'admin', 'moderator', 'support'],
|
||||
sheet_status: ['active', 'archived', 'deleted'],
|
||||
sheet_version_status: ['draft', 'published', 'deprecated'],
|
||||
|
||||
@@ -66,6 +66,25 @@ const expectedColumns = {
|
||||
'updated_at',
|
||||
'visibility',
|
||||
],
|
||||
assets: [
|
||||
'attempts',
|
||||
'bucket_id',
|
||||
'byte_size',
|
||||
'created_at',
|
||||
'declared_mime',
|
||||
'declared_size',
|
||||
'id',
|
||||
'kind',
|
||||
'lock_token',
|
||||
'locked_at',
|
||||
'object_key',
|
||||
'owner_id',
|
||||
'quarantine_key',
|
||||
'rejection_reason',
|
||||
'sha256',
|
||||
'status',
|
||||
'updated_at',
|
||||
],
|
||||
profile_roles: [
|
||||
'granted_at',
|
||||
'granted_by',
|
||||
@@ -109,7 +128,14 @@ type FunctionArgs = {
|
||||
string)[]
|
||||
}
|
||||
const expectedFunctionArgs = {
|
||||
ack_purge: ['purge_id'],
|
||||
archive_anatomical_sheet: ['sheet_id'],
|
||||
begin_avatar_upload: ['declared_mime', 'declared_size'],
|
||||
claim_avatar_jobs: ['max_jobs'],
|
||||
claim_purges: ['max_items'],
|
||||
claim_quarantine_leftovers: ['max_items', 'older_than_seconds'],
|
||||
complete_avatar_job: ['asset_id', 'byte_size', 'lock_token', 'sha256'],
|
||||
confirm_asset_upload: ['asset_id'],
|
||||
confirm_sheet_asset_upload: ['asset_id'],
|
||||
create_anatomical_sheet: [
|
||||
'description',
|
||||
@@ -125,20 +151,28 @@ const expectedFunctionArgs = {
|
||||
delete_sheet_asset: ['asset_id'],
|
||||
delete_sheet_version: ['version_id'],
|
||||
deprecate_sheet_version: ['reason', 'version_id'],
|
||||
expire_pending_uploads: ['max_age_seconds'],
|
||||
fork_anatomical_sheet: ['name', 'slug', 'source_version_id', 'visibility'],
|
||||
grant_platform_role: ['role', 'target_user_id'],
|
||||
mark_quarantine_purged: ['asset_id'],
|
||||
publish_sheet_version: ['version_id'],
|
||||
register_sheet_asset: ['byte_size', 'file_name', 'sha256', 'sheet_id'],
|
||||
reject_avatar_job: ['asset_id', 'lock_token', 'reason'],
|
||||
remove_avatar: [],
|
||||
restore_anatomical_sheet: ['sheet_id'],
|
||||
revoke_platform_role: ['reason', 'role', 'target_user_id'],
|
||||
set_sheet_visibility: ['sheet_id', 'visibility'],
|
||||
} as const satisfies FunctionArgs
|
||||
|
||||
// A function without arguments is generated as `Record<PropertyKey, never>`
|
||||
// (every key, no value): its key set is open, so only named keys are checked.
|
||||
type MissingArgs = {
|
||||
[F in keyof Functions]: Exclude<
|
||||
keyof Functions[F]['Args'],
|
||||
(typeof expectedFunctionArgs)[F][number]
|
||||
>
|
||||
[F in keyof Functions]: string extends keyof Functions[F]['Args']
|
||||
? never
|
||||
: Exclude<
|
||||
keyof Functions[F]['Args'],
|
||||
(typeof expectedFunctionArgs)[F][number]
|
||||
>
|
||||
}[keyof Functions]
|
||||
const noMissingArg: MissingArgs extends never ? true : never = true
|
||||
|
||||
@@ -216,9 +250,15 @@ describe('database.types.ts matches the migrated schema', () => {
|
||||
expect(actual).toEqual(expected)
|
||||
})
|
||||
|
||||
// Input arguments only: a `returns table` function also names its output columns in proargnames.
|
||||
it('lists exactly the public functions and their argument names', async () => {
|
||||
const { rows } = await db.query<FunctionRow>(
|
||||
`select p.proname as name, coalesce(p.proargnames, '{}') as args
|
||||
`select p.proname as name,
|
||||
case when p.proargmodes is null then coalesce(p.proargnames, '{}')
|
||||
else coalesce((select array_agg(u.n order by u.i)
|
||||
from unnest(p.proargnames, p.proargmodes) with ordinality as u(n, m, i)
|
||||
where u.m in ('i', 'b', 'v')), '{}')
|
||||
end as args
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname = 'public' and p.prokind = 'f'
|
||||
and not exists (
|
||||
|
||||
@@ -0,0 +1,674 @@
|
||||
-- Files foundation (plan Step 8, docs/development/STORAGE.md): the `assets`
|
||||
-- table as the authority over every user file, the private buckets, the
|
||||
-- avatar read / upload policies evaluated through `assets`, the purge queue
|
||||
-- and the RPCs of the browser and of the Rust worker.
|
||||
--
|
||||
-- First consumer: the profile photo (kind profile_avatar, bucket avatars).
|
||||
-- Every other bucket exists and stays closed (no policy names it) until its
|
||||
-- own step writes its rules. The sheet bucket of 20261009100000 is untouched.
|
||||
--
|
||||
-- Invariants pinned by supabase/tests/010_files_foundation.sql:
|
||||
-- * no API role reads a quarantine object, ever; a final object is readable
|
||||
-- only through a `clean` row of the caller whose object_key is the name;
|
||||
-- * the browser can insert only at the quarantine key of its own
|
||||
-- pending_upload row; no UPDATE / DELETE policy exists on the bucket;
|
||||
-- * every status change is an RPC; the worker's RPCs carry a lease token so
|
||||
-- an old task never overwrites a newer avatar;
|
||||
-- * a terminal row (deleted, rejected) enqueues its objects for a deferred
|
||||
-- purge; a clean row's object_key never enters the queue.
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Buckets: all private. Upsert so the migration converges everywhere.
|
||||
-- ---------------------------------------------------------------------------
|
||||
insert into storage.buckets (id, name, public, file_size_limit, allowed_mime_types)
|
||||
values ('avatars', 'avatars', false, 5242880, array['image/jpeg', 'image/png', 'image/webp'])
|
||||
on conflict (id) do update
|
||||
set public = false, file_size_limit = excluded.file_size_limit,
|
||||
allowed_mime_types = excluded.allowed_mime_types;
|
||||
|
||||
insert into storage.buckets (id, name, public)
|
||||
values
|
||||
('logos', 'logos', false),
|
||||
('professional-documents', 'professional-documents', false),
|
||||
('client-files', 'client-files', false),
|
||||
('animal-files', 'animal-files', false),
|
||||
('report-files', 'report-files', false),
|
||||
('imports', 'imports', false)
|
||||
on conflict (id) do update set public = false;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- assets
|
||||
-- ---------------------------------------------------------------------------
|
||||
create type public.asset_status as enum
|
||||
('pending_upload', 'uploaded', 'clean', 'rejected', 'deleted');
|
||||
|
||||
create table public.assets (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
owner_id uuid not null references public.profiles (id) on delete cascade,
|
||||
kind text not null
|
||||
constraint assets_kind_known check (kind in ('profile_avatar')),
|
||||
bucket_id text not null
|
||||
constraint assets_bucket_per_kind check (
|
||||
(kind = 'profile_avatar' and bucket_id = 'avatars')
|
||||
),
|
||||
-- Final key, server-built from uuids; one canonical file name per kind.
|
||||
object_key text not null unique
|
||||
constraint assets_object_key_shape check (
|
||||
object_key ~ '^profiles/[0-9a-f-]{36}/[0-9a-f-]{36}/avatar\.webp$'
|
||||
),
|
||||
-- Upload key; null once the worker confirmed the quarantine object is gone.
|
||||
quarantine_key text unique
|
||||
constraint assets_quarantine_key_shape check (
|
||||
quarantine_key ~ '^quarantine/[0-9a-f-]{36}/[0-9a-f-]{36}/upload$'
|
||||
),
|
||||
status public.asset_status not null default 'pending_upload',
|
||||
declared_mime text not null
|
||||
constraint assets_declared_mime_known check (
|
||||
declared_mime in ('image/jpeg', 'image/png', 'image/webp')
|
||||
),
|
||||
declared_size integer not null
|
||||
constraint assets_declared_size_range check (declared_size between 1 and 5242880),
|
||||
-- The clean output, set by complete_avatar_job.
|
||||
byte_size integer
|
||||
constraint assets_byte_size_range check (byte_size between 1 and 524288),
|
||||
sha256 text
|
||||
constraint assets_sha256_hex check (sha256 ~ '^[0-9a-f]{64}$'),
|
||||
rejection_reason text
|
||||
constraint assets_rejection_reason_known check (
|
||||
rejection_reason in ('unsupported_type', 'decode_failed', 'dimensions', 'processing_error')
|
||||
),
|
||||
constraint assets_rejection_reason_iff_rejected check (
|
||||
(status = 'rejected') = (rejection_reason is not null)
|
||||
),
|
||||
-- Lease: claim_avatar_jobs sets both; complete / reject require the token.
|
||||
attempts smallint not null default 0,
|
||||
locked_at timestamptz,
|
||||
lock_token uuid,
|
||||
created_at timestamptz not null default now(),
|
||||
updated_at timestamptz not null default now()
|
||||
);
|
||||
|
||||
comment on table public.assets is
|
||||
'One row per user file, created before the bytes exist and kept after they are gone (deleted / rejected) until the purge queue has been served. Writes only through RPCs (docs/development/STORAGE.md).';
|
||||
comment on column public.assets.object_key is
|
||||
'Final key in the bucket (profiles/<owner>/<id>/avatar.webp). Readable by the owner while the row is clean.';
|
||||
comment on column public.assets.quarantine_key is
|
||||
'Upload key (quarantine/<owner>/<id>/upload). Never readable by an API role; null once purged.';
|
||||
comment on column public.assets.lock_token is
|
||||
'Lease token of the worker that claimed the row; complete_avatar_job and reject_avatar_job refuse any other token.';
|
||||
|
||||
-- "The" avatar of an account = its single clean row (no profile column).
|
||||
create unique index assets_one_clean_avatar
|
||||
on public.assets (owner_id) where kind = 'profile_avatar' and status = 'clean';
|
||||
create index assets_claimable on public.assets (created_at) where status = 'uploaded';
|
||||
create index assets_owner_idx on public.assets (owner_id, created_at desc);
|
||||
|
||||
create trigger assets_set_updated_at
|
||||
before update on public.assets
|
||||
for each row execute function private.set_updated_at();
|
||||
|
||||
-- Privileges + RLS: the owner reads the public columns of their rows; nobody
|
||||
-- writes through the API.
|
||||
revoke all on public.assets from public, anon, authenticated;
|
||||
grant select (
|
||||
id, owner_id, kind, bucket_id, object_key, status, declared_mime, declared_size,
|
||||
byte_size, sha256, rejection_reason, created_at, updated_at
|
||||
) on public.assets to authenticated;
|
||||
|
||||
alter table public.assets enable row level security;
|
||||
|
||||
create policy assets_select_own
|
||||
on public.assets
|
||||
for select
|
||||
to authenticated
|
||||
using (owner_id = (select auth.uid()));
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Purge queue (private): every object deletion the worker makes comes from
|
||||
-- here or from the quarantine leftover query; nothing else is ever deleted.
|
||||
-- ---------------------------------------------------------------------------
|
||||
create table private.object_purge_queue (
|
||||
id uuid primary key default gen_random_uuid(),
|
||||
bucket_id text not null,
|
||||
object_name text not null,
|
||||
not_before timestamptz not null,
|
||||
enqueued_at timestamptz not null default now(),
|
||||
locked_at timestamptz,
|
||||
attempts smallint not null default 0,
|
||||
unique (bucket_id, object_name)
|
||||
);
|
||||
|
||||
comment on table private.object_purge_queue is
|
||||
'Objects to delete after a grace period (48 h for the final key of a deleted row: longer than a backup run; 1 h for quarantine keys and rejected uploads). Served by claim_purges / ack_purge.';
|
||||
|
||||
revoke all on private.object_purge_queue from public, anon, authenticated, service_role;
|
||||
|
||||
create or replace function private.enqueue_object_purge(
|
||||
p_bucket_id text,
|
||||
p_object_name text,
|
||||
p_grace interval
|
||||
)
|
||||
returns void
|
||||
language sql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
insert into private.object_purge_queue (bucket_id, object_name, not_before)
|
||||
values (p_bucket_id, p_object_name, now() + p_grace)
|
||||
on conflict (bucket_id, object_name) do update
|
||||
set not_before = least(private.object_purge_queue.not_before, excluded.not_before);
|
||||
$$;
|
||||
|
||||
revoke all on function private.enqueue_object_purge(text, text, interval) from public;
|
||||
|
||||
-- A row reaching a terminal status, or disappearing (account cascade),
|
||||
-- enqueues both of its keys. A clean row's object_key is never enqueued.
|
||||
create or replace function private.enqueue_asset_objects()
|
||||
returns trigger
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_row public.assets;
|
||||
v_final_grace interval;
|
||||
begin
|
||||
if tg_op = 'DELETE' then
|
||||
v_row := old;
|
||||
v_final_grace := interval '48 hours';
|
||||
else
|
||||
if new.status not in ('deleted', 'rejected') or old.status in ('deleted', 'rejected') then
|
||||
return new;
|
||||
end if;
|
||||
v_row := new;
|
||||
v_final_grace := case when new.status = 'deleted' then interval '48 hours' else interval '1 hour' end;
|
||||
end if;
|
||||
perform private.enqueue_object_purge(v_row.bucket_id, v_row.object_key, v_final_grace);
|
||||
if v_row.quarantine_key is not null then
|
||||
perform private.enqueue_object_purge(v_row.bucket_id, v_row.quarantine_key, interval '1 hour');
|
||||
end if;
|
||||
return coalesce(new, old);
|
||||
end;
|
||||
$$;
|
||||
|
||||
revoke all on function private.enqueue_asset_objects() from public;
|
||||
|
||||
create trigger assets_enqueue_purge_on_status
|
||||
after update of status on public.assets
|
||||
for each row execute function private.enqueue_asset_objects();
|
||||
|
||||
create trigger assets_enqueue_purge_on_delete
|
||||
after delete on public.assets
|
||||
for each row execute function private.enqueue_asset_objects();
|
||||
|
||||
-- Global ceiling on declared sizes of non-deleted rows (10 GiB to start).
|
||||
create or replace function private.storage_ceiling()
|
||||
returns bigint
|
||||
language sql
|
||||
immutable
|
||||
set search_path = ''
|
||||
as $$ select 10737418240::bigint; $$;
|
||||
|
||||
revoke all on function private.storage_ceiling() from public;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Storage policies for bucket avatars, evaluated through assets.
|
||||
-- ---------------------------------------------------------------------------
|
||||
create or replace function private.can_read_avatar_object(p_user_id uuid, p_object_name text)
|
||||
returns boolean
|
||||
language sql
|
||||
stable
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
select exists (
|
||||
select 1 from public.assets a
|
||||
where a.owner_id = p_user_id
|
||||
and a.kind = 'profile_avatar'
|
||||
and a.status = 'clean'
|
||||
and a.object_key = p_object_name
|
||||
);
|
||||
$$;
|
||||
|
||||
comment on function private.can_read_avatar_object(uuid, text) is
|
||||
'Storage read predicate for bucket avatars: the object_key of a clean row of the caller. Quarantine keys never match.';
|
||||
|
||||
create or replace function private.can_upload_avatar_object(p_user_id uuid, p_object_name text)
|
||||
returns boolean
|
||||
language sql
|
||||
stable
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
select exists (
|
||||
select 1 from public.assets a
|
||||
where a.owner_id = p_user_id
|
||||
and a.kind = 'profile_avatar'
|
||||
and a.status = 'pending_upload'
|
||||
and a.quarantine_key = p_object_name
|
||||
);
|
||||
$$;
|
||||
|
||||
comment on function private.can_upload_avatar_object(uuid, text) is
|
||||
'Storage insert predicate for bucket avatars: the quarantine_key of a pending_upload row of the caller. Final keys never match.';
|
||||
|
||||
revoke all on function private.can_read_avatar_object(uuid, text) from public;
|
||||
revoke all on function private.can_upload_avatar_object(uuid, text) from public;
|
||||
grant execute on function private.can_read_avatar_object(uuid, text) to authenticated;
|
||||
grant execute on function private.can_upload_avatar_object(uuid, text) to authenticated;
|
||||
|
||||
create policy avatars_read
|
||||
on storage.objects
|
||||
for select
|
||||
to authenticated
|
||||
using (
|
||||
bucket_id = 'avatars'
|
||||
and private.can_read_avatar_object((select auth.uid()), name)
|
||||
);
|
||||
|
||||
create policy avatars_upload
|
||||
on storage.objects
|
||||
for insert
|
||||
to authenticated
|
||||
with check (
|
||||
bucket_id = 'avatars'
|
||||
and private.can_upload_avatar_object((select auth.uid()), name)
|
||||
);
|
||||
|
||||
-- No UPDATE / DELETE policy for API roles on this bucket; no policy at all on
|
||||
-- the six closed buckets.
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Browser RPCs (authenticated). Business refusals: SQLSTATE P0001 with the
|
||||
-- closed code as the message; authorisation refusals: 42501.
|
||||
-- ---------------------------------------------------------------------------
|
||||
create or replace function public.begin_avatar_upload(declared_mime text, declared_size integer)
|
||||
returns jsonb
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_actor uuid := auth.uid();
|
||||
v_active boolean;
|
||||
v_id uuid := gen_random_uuid();
|
||||
v_recent integer;
|
||||
v_used bigint;
|
||||
v_object_key text;
|
||||
v_quarantine_key text;
|
||||
begin
|
||||
if v_actor is null then
|
||||
raise exception 'authentication required' using errcode = '42501';
|
||||
end if;
|
||||
-- Serialises a double click and refuses a deactivated account.
|
||||
select p.active into v_active from public.profiles p where p.id = v_actor for update;
|
||||
if v_active is distinct from true then
|
||||
raise exception 'account inactive or unknown' using errcode = '42501';
|
||||
end if;
|
||||
if declared_mime is null or declared_mime not in ('image/jpeg', 'image/png', 'image/webp') then
|
||||
raise exception using message = 'unsupported_type', errcode = 'P0001';
|
||||
end if;
|
||||
if declared_size is null or declared_size < 1 or declared_size > 5242880 then
|
||||
raise exception using message = 'too_large', errcode = 'P0001';
|
||||
end if;
|
||||
select count(*) into v_recent from public.assets a
|
||||
where a.owner_id = v_actor and a.created_at > now() - interval '1 hour';
|
||||
if v_recent >= 10 then
|
||||
raise exception using message = 'rate_limited', errcode = 'P0001';
|
||||
end if;
|
||||
select coalesce(sum(a.declared_size), 0) into v_used from public.assets a
|
||||
where a.status <> 'deleted';
|
||||
if v_used + declared_size > private.storage_ceiling() then
|
||||
raise exception using message = 'storage_full', errcode = 'P0001';
|
||||
end if;
|
||||
|
||||
-- One in-flight avatar per account: a new pick supersedes the previous one.
|
||||
update public.assets a set status = 'deleted'
|
||||
where a.owner_id = v_actor and a.kind = 'profile_avatar'
|
||||
and a.status in ('pending_upload', 'uploaded');
|
||||
|
||||
v_object_key := 'profiles/' || v_actor || '/' || v_id || '/avatar.webp';
|
||||
v_quarantine_key := 'quarantine/' || v_actor || '/' || v_id || '/upload';
|
||||
insert into public.assets (id, owner_id, kind, bucket_id, object_key, quarantine_key, declared_mime, declared_size)
|
||||
values (v_id, v_actor, 'profile_avatar', 'avatars', v_object_key, v_quarantine_key, declared_mime, declared_size);
|
||||
|
||||
return jsonb_build_object('asset_id', v_id, 'bucket_id', 'avatars', 'quarantine_key', v_quarantine_key);
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.begin_avatar_upload(text, integer) is
|
||||
'Reserves an avatar upload: rate (10 per hour), global ceiling, type and size caps, supersedes any in-flight row of the caller, returns {asset_id, bucket_id, quarantine_key}.';
|
||||
|
||||
create or replace function public.confirm_asset_upload(asset_id uuid)
|
||||
returns void
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_actor uuid := auth.uid();
|
||||
v_row public.assets;
|
||||
v_size bigint;
|
||||
v_found boolean;
|
||||
begin
|
||||
if v_actor is null then
|
||||
raise exception 'authentication required' using errcode = '42501';
|
||||
end if;
|
||||
select a.* into v_row from public.assets a
|
||||
where a.id = asset_id and a.owner_id = v_actor for update;
|
||||
if v_row.id is null then
|
||||
raise exception using message = 'not_found', errcode = 'P0001';
|
||||
end if;
|
||||
if v_row.status <> 'pending_upload' then
|
||||
raise exception using message = 'invalid_state', errcode = 'P0001';
|
||||
end if;
|
||||
select true, coalesce((o.metadata ->> 'size')::bigint, 0) into v_found, v_size
|
||||
from storage.objects o
|
||||
where o.bucket_id = v_row.bucket_id and o.name = v_row.quarantine_key;
|
||||
if v_found is distinct from true then
|
||||
raise exception using message = 'upload_missing', errcode = 'P0001';
|
||||
end if;
|
||||
if v_size > 5242880 then
|
||||
raise exception using message = 'too_large', errcode = 'P0001';
|
||||
end if;
|
||||
update public.assets a set status = 'uploaded' where a.id = v_row.id;
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.confirm_asset_upload(uuid) is
|
||||
'Explicit client signal: the quarantine object of the caller''s pending_upload row exists → uploaded. The worker never guesses from Storage internals whether a PUT finished.';
|
||||
|
||||
create or replace function public.remove_avatar()
|
||||
returns integer
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
declare
|
||||
v_actor uuid := auth.uid();
|
||||
v_count integer;
|
||||
begin
|
||||
if v_actor is null then
|
||||
raise exception 'authentication required' using errcode = '42501';
|
||||
end if;
|
||||
update public.assets a set status = 'deleted'
|
||||
where a.owner_id = v_actor and a.kind = 'profile_avatar'
|
||||
and a.status in ('pending_upload', 'uploaded', 'clean');
|
||||
get diagnostics v_count = row_count;
|
||||
if v_count > 0 then
|
||||
perform private.log_event(
|
||||
v_actor, 'asset.avatar_removed', 'asset', null, v_actor, null,
|
||||
jsonb_build_object('count', v_count)
|
||||
);
|
||||
end if;
|
||||
return v_count;
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.remove_avatar() is
|
||||
'Every avatar row of the caller still alive (clean or in flight) → deleted; the objects are purged after the grace period. Audited.';
|
||||
|
||||
revoke all on function public.begin_avatar_upload(text, integer) from public, anon;
|
||||
revoke all on function public.confirm_asset_upload(uuid) from public, anon;
|
||||
revoke all on function public.remove_avatar() from public, anon;
|
||||
grant execute on function public.begin_avatar_upload(text, integer) to authenticated, service_role;
|
||||
grant execute on function public.confirm_asset_upload(uuid) to authenticated, service_role;
|
||||
grant execute on function public.remove_avatar() to authenticated, service_role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Worker RPCs (service_role only; EXECUTE is the guard, as for
|
||||
-- confirm_sheet_asset_upload).
|
||||
-- ---------------------------------------------------------------------------
|
||||
create or replace function public.claim_avatar_jobs(max_jobs integer)
|
||||
returns table (
|
||||
asset_id uuid, owner_id uuid, bucket_id text, object_key text,
|
||||
quarantine_key text, lock_token uuid, attempts smallint
|
||||
)
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_limit integer := least(greatest(coalesce(max_jobs, 1), 1), 10);
|
||||
v_expired record;
|
||||
begin
|
||||
-- A third expired lease is a refusal: the content or the environment
|
||||
-- defeated the worker three times.
|
||||
for v_expired in
|
||||
update public.assets a
|
||||
set status = 'rejected', rejection_reason = 'processing_error',
|
||||
locked_at = null, lock_token = null
|
||||
where a.status = 'uploaded' and a.attempts >= 3
|
||||
and a.locked_at is not null and a.locked_at < now() - interval '10 minutes'
|
||||
returning a.id, a.owner_id
|
||||
loop
|
||||
perform private.log_event(
|
||||
null, 'asset.rejected', 'asset', v_expired.id, v_expired.owner_id, null,
|
||||
jsonb_build_object('reason', 'processing_error', 'cause', 'lease_expired')
|
||||
);
|
||||
end loop;
|
||||
|
||||
return query
|
||||
with claimed as (
|
||||
select a.id
|
||||
from public.assets a
|
||||
where a.status = 'uploaded' and a.attempts < 3
|
||||
and (a.locked_at is null or a.locked_at < now() - interval '10 minutes')
|
||||
order by a.created_at
|
||||
limit v_limit
|
||||
for update skip locked
|
||||
)
|
||||
update public.assets a
|
||||
set locked_at = now(), attempts = a.attempts + 1, lock_token = gen_random_uuid()
|
||||
from claimed
|
||||
where a.id = claimed.id
|
||||
returning a.id, a.owner_id, a.bucket_id, a.object_key, a.quarantine_key, a.lock_token, a.attempts;
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.claim_avatar_jobs(integer) is
|
||||
'Worker: leases uploaded rows oldest first (skip locked, 10 min lease, 3 attempts); rows past their 3rd expired lease become rejected (processing_error). VOLATILE on purpose.';
|
||||
|
||||
create or replace function public.complete_avatar_job(
|
||||
asset_id uuid, lock_token uuid, byte_size integer, sha256 text
|
||||
)
|
||||
returns void
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_row public.assets;
|
||||
begin
|
||||
select a.* into v_row from public.assets a where a.id = asset_id for update;
|
||||
if v_row.id is null then
|
||||
raise exception using message = 'not_found', errcode = 'P0001';
|
||||
end if;
|
||||
-- A superseded or removed row is no longer uploaded; an expired lease
|
||||
-- carries a stale token: both refuse, the purge queue owns any object.
|
||||
if v_row.status <> 'uploaded' or lock_token is null or v_row.lock_token is distinct from lock_token then
|
||||
raise exception using message = 'invalid_state', errcode = 'P0001';
|
||||
end if;
|
||||
if byte_size is null or byte_size < 1 or byte_size > 524288 or sha256 !~ '^[0-9a-f]{64}$' then
|
||||
raise exception using message = 'invalid_output', errcode = 'P0001';
|
||||
end if;
|
||||
|
||||
-- Replacement: the previous clean avatar is enqueued (48 h) by the trigger.
|
||||
update public.assets a set status = 'deleted'
|
||||
where a.owner_id = v_row.owner_id and a.kind = v_row.kind
|
||||
and a.status = 'clean' and a.id <> v_row.id;
|
||||
update public.assets a
|
||||
set status = 'clean', byte_size = byte_size, sha256 = sha256,
|
||||
locked_at = null, lock_token = null
|
||||
where a.id = v_row.id;
|
||||
|
||||
perform private.log_event(
|
||||
null, 'asset.avatar_set', 'asset', v_row.id, v_row.owner_id, null,
|
||||
jsonb_build_object('byte_size', byte_size)
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.complete_avatar_job(uuid, uuid, integer, text) is
|
||||
'Worker: the clean object is in place → the row becomes clean and the previous clean avatar of the owner is deleted, in one transaction. Refused on any status but uploaded or on a stale lease token.';
|
||||
|
||||
create or replace function public.reject_avatar_job(asset_id uuid, lock_token uuid, reason text)
|
||||
returns void
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_row public.assets;
|
||||
begin
|
||||
select a.* into v_row from public.assets a where a.id = asset_id for update;
|
||||
if v_row.id is null then
|
||||
raise exception using message = 'not_found', errcode = 'P0001';
|
||||
end if;
|
||||
if v_row.status <> 'uploaded' or lock_token is null or v_row.lock_token is distinct from lock_token then
|
||||
raise exception using message = 'invalid_state', errcode = 'P0001';
|
||||
end if;
|
||||
if reason is null or reason not in ('unsupported_type', 'decode_failed', 'dimensions', 'processing_error') then
|
||||
raise exception using message = 'invalid_reason', errcode = 'P0001';
|
||||
end if;
|
||||
update public.assets a
|
||||
set status = 'rejected', rejection_reason = reason, locked_at = null, lock_token = null
|
||||
where a.id = v_row.id;
|
||||
perform private.log_event(
|
||||
null, 'asset.rejected', 'asset', v_row.id, v_row.owner_id, null,
|
||||
jsonb_build_object('reason', reason)
|
||||
);
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.reject_avatar_job(uuid, uuid, text) is
|
||||
'Worker: the upload was refused with a closed code. Same guards as complete_avatar_job.';
|
||||
|
||||
create or replace function public.mark_quarantine_purged(asset_id uuid)
|
||||
returns void
|
||||
language sql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
update public.assets a set quarantine_key = null
|
||||
where a.id = asset_id and a.status = 'clean';
|
||||
$$;
|
||||
|
||||
comment on function public.mark_quarantine_purged(uuid) is
|
||||
'Worker: the quarantine object of a clean row was deleted. No-op on any other status.';
|
||||
|
||||
create or replace function public.claim_purges(max_items integer)
|
||||
returns table (purge_id uuid, bucket_id text, object_name text)
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_limit integer := least(greatest(coalesce(max_items, 1), 1), 100);
|
||||
begin
|
||||
return query
|
||||
with claimed as (
|
||||
select q.id
|
||||
from private.object_purge_queue q
|
||||
where q.not_before <= now()
|
||||
and (q.locked_at is null or q.locked_at < now() - interval '10 minutes')
|
||||
order by q.not_before
|
||||
limit v_limit
|
||||
for update skip locked
|
||||
)
|
||||
update private.object_purge_queue q
|
||||
set locked_at = now(), attempts = q.attempts + 1
|
||||
from claimed
|
||||
where q.id = claimed.id
|
||||
returning q.id, q.bucket_id, q.object_name;
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.claim_purges(integer) is
|
||||
'Worker: due purge entries (grace elapsed), leased for 10 minutes. Delete the object, then ack_purge.';
|
||||
|
||||
create or replace function public.ack_purge(purge_id uuid)
|
||||
returns void
|
||||
language sql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
delete from private.object_purge_queue q where q.id = purge_id;
|
||||
$$;
|
||||
|
||||
comment on function public.ack_purge(uuid) is
|
||||
'Worker: the object of a purge entry is gone (a missing object counts as gone).';
|
||||
|
||||
create or replace function public.claim_quarantine_leftovers(older_than_seconds integer, max_items integer)
|
||||
returns table (bucket_id text, object_name text, asset_id uuid)
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_limit integer := least(greatest(coalesce(max_items, 1), 1), 100);
|
||||
v_age interval := make_interval(secs => greatest(coalesce(older_than_seconds, 0), 0));
|
||||
begin
|
||||
-- Bytes under quarantine/ whose row is no longer waiting for them: a slow
|
||||
-- PUT or a signed upload URL used after the row moved on, or the
|
||||
-- quarantine object of a clean row the worker could not delete in time.
|
||||
return query
|
||||
select o.bucket_id, o.name, a.id
|
||||
from storage.objects o
|
||||
left join public.assets a on a.bucket_id = o.bucket_id and a.quarantine_key = o.name
|
||||
where o.bucket_id = 'avatars'
|
||||
and o.name like 'quarantine/%'
|
||||
and o.created_at <= now() - v_age
|
||||
and (a.id is null or a.status not in ('pending_upload', 'uploaded'))
|
||||
order by o.created_at
|
||||
limit v_limit;
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.claim_quarantine_leftovers(integer, integer) is
|
||||
'Worker sweep: quarantine objects older than the grace whose row is not pending_upload / uploaded. The worker deletes them and calls mark_quarantine_purged when asset_id is set.';
|
||||
|
||||
create or replace function public.expire_pending_uploads(max_age_seconds integer)
|
||||
returns integer
|
||||
language plpgsql
|
||||
security definer
|
||||
set search_path = ''
|
||||
as $$
|
||||
#variable_conflict use_variable
|
||||
declare
|
||||
v_count integer;
|
||||
begin
|
||||
update public.assets a set status = 'deleted'
|
||||
where a.status = 'pending_upload'
|
||||
and a.created_at < now() - make_interval(secs => greatest(coalesce(max_age_seconds, 0), 0));
|
||||
get diagnostics v_count = row_count;
|
||||
return v_count;
|
||||
end;
|
||||
$$;
|
||||
|
||||
comment on function public.expire_pending_uploads(integer) is
|
||||
'Worker sweep: pending_upload rows never confirmed within the age → deleted (their keys are enqueued by the trigger).';
|
||||
|
||||
revoke all on function public.claim_avatar_jobs(integer) from public, anon, authenticated;
|
||||
revoke all on function public.complete_avatar_job(uuid, uuid, integer, text) from public, anon, authenticated;
|
||||
revoke all on function public.reject_avatar_job(uuid, uuid, text) from public, anon, authenticated;
|
||||
revoke all on function public.mark_quarantine_purged(uuid) from public, anon, authenticated;
|
||||
revoke all on function public.claim_purges(integer) from public, anon, authenticated;
|
||||
revoke all on function public.ack_purge(uuid) from public, anon, authenticated;
|
||||
revoke all on function public.claim_quarantine_leftovers(integer, integer) from public, anon, authenticated;
|
||||
revoke all on function public.expire_pending_uploads(integer) from public, anon, authenticated;
|
||||
grant execute on function public.claim_avatar_jobs(integer) to service_role;
|
||||
grant execute on function public.complete_avatar_job(uuid, uuid, integer, text) to service_role;
|
||||
grant execute on function public.reject_avatar_job(uuid, uuid, text) to service_role;
|
||||
grant execute on function public.mark_quarantine_purged(uuid) to service_role;
|
||||
grant execute on function public.claim_purges(integer) to service_role;
|
||||
grant execute on function public.ack_purge(uuid) to service_role;
|
||||
grant execute on function public.claim_quarantine_leftovers(integer, integer) to service_role;
|
||||
grant execute on function public.expire_pending_uploads(integer) to service_role;
|
||||
@@ -62,7 +62,7 @@ select is(
|
||||
(select string_agg(p.proname::text, ',' order by p.proname::text collate "C")
|
||||
from pg_proc p join pg_namespace n on n.oid = p.pronamespace
|
||||
where n.nspname in ('private', 'audit') and has_function_privilege('authenticated', p.oid, 'execute')),
|
||||
'can_edit_sheet,can_read_sheet_asset_object,can_use_sheet,has_platform_role,is_platform_admin,is_platform_moderator,is_platform_owner,is_support',
|
||||
'can_edit_sheet,can_read_avatar_object,can_read_sheet_asset_object,can_upload_avatar_object,can_use_sheet,has_platform_role,is_platform_admin,is_platform_moderator,is_platform_owner,is_support',
|
||||
'authenticated executes only the policy predicates in private'
|
||||
);
|
||||
|
||||
|
||||
@@ -0,0 +1,491 @@
|
||||
-- Files foundation: buckets, assets, avatar policies through assets, the
|
||||
-- browser and worker RPCs, the purge queue. Every allowed path has its denial
|
||||
-- twin; the eight scenarios of the Step 8 brief are named in the descriptions.
|
||||
begin;
|
||||
select plan(125);
|
||||
|
||||
insert into auth.users (id, email) values
|
||||
('40000000-0000-0000-0000-00000000000a', 'alice-files@test.local'),
|
||||
('40000000-0000-0000-0000-00000000000b', 'bob-files@test.local'),
|
||||
('40000000-0000-0000-0000-00000000000c', 'carol-files@test.local');
|
||||
|
||||
create table pg_temp.ids (key text primary key, id uuid not null);
|
||||
create function pg_temp.id(p_key text) returns uuid language sql stable
|
||||
as $$ select id from pg_temp.ids where key = p_key $$;
|
||||
create function pg_temp.row(p_key text) returns public.assets language sql stable security definer
|
||||
as $$ select a from public.assets a where a.id = pg_temp.id(p_key) $$;
|
||||
grant all on pg_temp.ids to anon, authenticated, service_role;
|
||||
grant execute on function pg_temp.id(text) to anon, authenticated, service_role;
|
||||
grant execute on function pg_temp.row(text) to anon, authenticated, service_role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 1. Buckets and the policy census
|
||||
-- ---------------------------------------------------------------------------
|
||||
select is(
|
||||
(select count(*) from storage.buckets where id in ('avatars', 'logos', 'professional-documents',
|
||||
'client-files', 'animal-files', 'report-files', 'anatomical-sheet-assets', 'imports')),
|
||||
8::bigint, 'the eight buckets exist'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from storage.buckets where public and id in ('avatars', 'logos', 'professional-documents',
|
||||
'client-files', 'animal-files', 'report-files', 'anatomical-sheet-assets', 'imports')),
|
||||
0::bigint, 'every bucket is private'
|
||||
);
|
||||
select is((select file_size_limit from storage.buckets where id = 'avatars'), 5242880::bigint, 'avatars capped at 5 MiB');
|
||||
select is(
|
||||
(select allowed_mime_types from storage.buckets where id = 'avatars'),
|
||||
array['image/jpeg', 'image/png', 'image/webp'], 'avatars accepts jpeg, png, webp only (never SVG)'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
|
||||
and (select count(*) from regexp_matches(coalesce(qual, '') || coalesce(with_check, ''), 'bucket_id = ''[a-z-]+''', 'g')) <> 1),
|
||||
0::bigint, 'census: every storage.objects policy pins exactly one bucket literal'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
|
||||
and (coalesce(qual, '') || coalesce(with_check, '')) like '%bucket_id = ''avatars''%'),
|
||||
2::bigint, 'census: two policies name avatars'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
|
||||
and (coalesce(qual, '') || coalesce(with_check, '')) like '%bucket_id = ''anatomical-sheet-assets''%'),
|
||||
1::bigint, 'census: one policy names anatomical-sheet-assets'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'),
|
||||
3::bigint, 'census: three policies in total, so the six closed buckets have none'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from pg_policies where schemaname = 'storage' and tablename = 'objects'
|
||||
and cmd in ('UPDATE', 'DELETE')),
|
||||
0::bigint, 'no UPDATE / DELETE policy exists on storage.objects for any API role'
|
||||
);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 2. Schema and privileges of assets
|
||||
-- ---------------------------------------------------------------------------
|
||||
select has_table('public', 'assets', 'table public.assets exists');
|
||||
select ok((select relrowsecurity from pg_class where oid = 'public.assets'::regclass), 'RLS enabled on assets');
|
||||
select enum_has_labels('public', 'asset_status',
|
||||
array['pending_upload', 'uploaded', 'clean', 'rejected', 'deleted'], 'asset_status labels');
|
||||
select ok(not has_table_privilege('anon', 'public.assets', 'select'), 'anon cannot select assets');
|
||||
select ok(not has_table_privilege('authenticated', 'public.assets', 'insert'), 'authenticated cannot insert assets');
|
||||
select ok(not has_table_privilege('authenticated', 'public.assets', 'update'), 'authenticated cannot update assets');
|
||||
select ok(not has_table_privilege('authenticated', 'public.assets', 'delete'), 'authenticated cannot delete assets');
|
||||
select ok(has_column_privilege('authenticated', 'public.assets', 'status', 'select'), 'authenticated reads status');
|
||||
select ok(not has_column_privilege('authenticated', 'public.assets', 'lock_token', 'select'), 'lock_token stays server-side');
|
||||
select ok(not has_column_privilege('authenticated', 'public.assets', 'quarantine_key', 'select'), 'quarantine_key stays server-side');
|
||||
select ok(not has_function_privilege('authenticated', 'public.claim_avatar_jobs(integer)', 'execute'), 'authenticated cannot claim jobs');
|
||||
select ok(not has_function_privilege('authenticated', 'public.complete_avatar_job(uuid, uuid, integer, text)', 'execute'), 'authenticated cannot complete a job');
|
||||
select ok(not has_function_privilege('authenticated', 'public.claim_purges(integer)', 'execute'), 'authenticated cannot claim purges');
|
||||
select ok(not has_function_privilege('anon', 'public.begin_avatar_upload(text, integer)', 'execute'), 'anon cannot begin an upload');
|
||||
select is(private.storage_ceiling(), 10737418240::bigint, 'storage ceiling is 10 GiB');
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 3. Alice begins an upload
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select throws_ok($$ select public.begin_avatar_upload('image/gif', 1000) $$, 'P0001', 'unsupported_type', 'a GIF is refused at begin');
|
||||
select throws_ok($$ select public.begin_avatar_upload('image/svg+xml', 1000) $$, 'P0001', 'unsupported_type', 'an SVG is refused at begin');
|
||||
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 5242881) $$, 'P0001', 'too_large', 'over 5 MiB is refused at begin');
|
||||
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 0) $$, 'P0001', 'too_large', 'an empty file is refused at begin');
|
||||
insert into pg_temp.ids select 'a1', (public.begin_avatar_upload('image/jpeg', 1000) ->> 'asset_id')::uuid;
|
||||
select is((pg_temp.row('a1')).status, 'pending_upload', 'a1 is pending_upload');
|
||||
select is((pg_temp.row('a1')).object_key,
|
||||
'profiles/40000000-0000-0000-0000-00000000000a/' || pg_temp.id('a1') || '/avatar.webp', 'final key built from uuids');
|
||||
select is((pg_temp.row('a1')).quarantine_key,
|
||||
'quarantine/40000000-0000-0000-0000-00000000000a/' || pg_temp.id('a1') || '/upload', 'quarantine key built from uuids');
|
||||
select results_eq(
|
||||
$$ select id, status::text, object_key from public.assets order by created_at $$,
|
||||
$$ select pg_temp.id('a1'), 'pending_upload'::text, (pg_temp.row('a1')).object_key $$,
|
||||
'alice reads her own row through RLS'
|
||||
);
|
||||
select throws_ok($$ select lock_token from public.assets $$, '42501', null, 'alice cannot read the lease column');
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 4. Upload policy: only the own quarantine key of a pending row
|
||||
-- ---------------------------------------------------------------------------
|
||||
select throws_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).object_key, '{"size":1000}') $$,
|
||||
'42501', null, 'scenario: the final key cannot be written by the browser'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', 'quarantine/40000000-0000-0000-0000-00000000000a/00000000-0000-0000-0000-000000000000/upload', '{"size":1000}') $$,
|
||||
'42501', null, 'an unknown quarantine key is refused'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, metadata) values ('logos', 'anything', '{"size":1000}') $$,
|
||||
'42501', null, 'a closed bucket refuses every upload'
|
||||
);
|
||||
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'P0001', 'upload_missing', 'confirm before the bytes landed is refused');
|
||||
|
||||
-- Bob at Alice's key
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
||||
select throws_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).quarantine_key, '{"size":1000}') $$,
|
||||
'42501', null, 'scenario: Bob cannot upload at Alice''s quarantine key'
|
||||
);
|
||||
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'P0001', 'not_found', 'Bob cannot confirm Alice''s row');
|
||||
select is((select count(*) from public.assets), 0::bigint, 'Bob sees none of Alice''s rows');
|
||||
|
||||
-- Alice uploads and confirms
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select lives_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).quarantine_key, '{"size":1000}') $$,
|
||||
'Alice uploads at her quarantine key'
|
||||
);
|
||||
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint,
|
||||
'scenario: a quarantine object is never readable, not even by its uploader');
|
||||
select lives_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'Alice confirms');
|
||||
select is((pg_temp.row('a1')).status, 'uploaded', 'a1 is uploaded');
|
||||
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('a1')) $$, 'P0001', 'invalid_state', 'confirming twice is refused');
|
||||
select throws_ok(
|
||||
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).quarantine_key, '{"size":1000}') $$,
|
||||
'42501', null, 'once uploaded, the quarantine key accepts no second upload'
|
||||
);
|
||||
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'nothing readable while uploaded');
|
||||
select throws_ok($$ select * from public.claim_avatar_jobs(1) $$, '42501', null, 'the browser cannot claim jobs');
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 5. The worker claims, the parent writes the clean object, completes
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
insert into pg_temp.ids select 'tok1', lock_token from public.claim_avatar_jobs(1);
|
||||
select is((select count(*) from public.claim_avatar_jobs(1)), 0::bigint, 'a live lease is skipped by the next claim');
|
||||
select is((pg_temp.row('a1')).attempts, 1::smallint, 'claim counts one attempt');
|
||||
select throws_ok(
|
||||
$$ select public.complete_avatar_job(pg_temp.id('a1'), gen_random_uuid(), 30000, repeat('a', 64)) $$,
|
||||
'P0001', 'invalid_state', 'scenario: a complete with a foreign lease token is refused'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 30000, 'nope') $$,
|
||||
'P0001', 'invalid_output', 'a malformed sha256 is refused'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 600000, repeat('a', 64)) $$,
|
||||
'P0001', 'invalid_output', 'an output over 512 KiB is refused'
|
||||
);
|
||||
reset role;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a1')).object_key, '{"size":30000}');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select lives_ok($$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 30000, repeat('a', 64)) $$, 'worker completes a1');
|
||||
select is((pg_temp.row('a1')).status, 'clean', 'a1 is clean');
|
||||
select is((pg_temp.row('a1')).byte_size, 30000, 'byte_size recorded');
|
||||
select is((pg_temp.row('a1')).lock_token, null, 'lease cleared on complete');
|
||||
select throws_ok(
|
||||
$$ select public.complete_avatar_job(pg_temp.id('a1'), pg_temp.id('tok1'), 30000, repeat('a', 64)) $$,
|
||||
'P0001', 'invalid_state', 'a clean row cannot be completed again'
|
||||
);
|
||||
select lives_ok($$ select public.mark_quarantine_purged(pg_temp.id('a1')) $$, 'worker marks the quarantine purged');
|
||||
select is((pg_temp.row('a1')).quarantine_key, null, 'quarantine_key cleared');
|
||||
reset role;
|
||||
select is((select count(*) from private.object_purge_queue where object_name = (pg_temp.row('a1')).object_key), 0::bigint,
|
||||
'a clean row''s object_key never enters the purge queue');
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 6. Read policy: the owner reads exactly the clean object
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select results_eq(
|
||||
$$ select name from storage.objects where bucket_id = 'avatars' $$,
|
||||
$$ select (pg_temp.row('a1')).object_key $$,
|
||||
'Alice reads her clean avatar object and nothing else (the quarantine object still exists)'
|
||||
);
|
||||
select ok(
|
||||
not private.can_read_avatar_object('40000000-0000-0000-0000-00000000000a', (pg_temp.row('a1')).object_key || '/')
|
||||
and not private.can_read_avatar_object('40000000-0000-0000-0000-00000000000a', upper((pg_temp.row('a1')).object_key))
|
||||
and not private.can_read_avatar_object('40000000-0000-0000-0000-00000000000a', 'quarantine/40000000-0000-0000-0000-00000000000a/' || pg_temp.id('a1') || '/upload'),
|
||||
'name variants and the quarantine key never match the read predicate'
|
||||
);
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
||||
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'scenario: Bob reads none of Alice''s objects');
|
||||
select is((select count(*) from public.assets), 0::bigint, 'Bob reads none of Alice''s rows');
|
||||
select is(public.remove_avatar(), 0, 'scenario: Bob''s remove_avatar touches nothing of Alice');
|
||||
select is((pg_temp.row('a1')).status, 'clean', 'Alice''s avatar survives Bob''s removal call');
|
||||
set local role anon;
|
||||
select set_config('request.jwt.claims', '', true);
|
||||
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'scenario: anon reads no private object');
|
||||
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, '42501', null, 'anon cannot begin an upload');
|
||||
select throws_ok($$ select public.remove_avatar() $$, '42501', null, 'anon cannot remove an avatar');
|
||||
reset role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 7. Replacement: a2 clean → a1 deleted and enqueued with the 48 h grace
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
insert into pg_temp.ids select 'a2', (public.begin_avatar_upload('image/png', 2000) ->> 'asset_id')::uuid;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a2')).quarantine_key, '{"size":2000}');
|
||||
select public.confirm_asset_upload(pg_temp.id('a2'));
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
insert into pg_temp.ids select 'tok2', lock_token from public.claim_avatar_jobs(1);
|
||||
reset role;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a2')).object_key, '{"size":31000}');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select lives_ok($$ select public.complete_avatar_job(pg_temp.id('a2'), pg_temp.id('tok2'), 31000, repeat('b', 64)) $$, 'worker completes a2');
|
||||
select is((pg_temp.row('a2')).status, 'clean', 'a2 is clean');
|
||||
select is((pg_temp.row('a1')).status, 'deleted', 'the previous clean avatar is deleted in the same transaction');
|
||||
select is((select count(*) from public.claim_purges(100)), 0::bigint, 'nothing is due before its grace elapsed');
|
||||
reset role;
|
||||
select is(
|
||||
(select count(*) from private.object_purge_queue
|
||||
where object_name = (pg_temp.row('a1')).object_key and not_before > now() + interval '47 hours'),
|
||||
1::bigint, 'the replaced object is enqueued with the 48 h grace (backup invariant)'
|
||||
);
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select results_eq(
|
||||
$$ select name from storage.objects where bucket_id = 'avatars' $$,
|
||||
$$ select (pg_temp.row('a2')).object_key $$,
|
||||
'scenario: a replaced object is no longer readable although it still exists'
|
||||
);
|
||||
reset role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 8. Supersede race: a claimed job whose row was superseded cannot complete
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
insert into pg_temp.ids select 'a3', (public.begin_avatar_upload('image/jpeg', 3000) ->> 'asset_id')::uuid;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a3')).quarantine_key, '{"size":3000}');
|
||||
select public.confirm_asset_upload(pg_temp.id('a3'));
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
insert into pg_temp.ids select 'tok3', lock_token from public.claim_avatar_jobs(1);
|
||||
reset role;
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
insert into pg_temp.ids select 'a4', (public.begin_avatar_upload('image/jpeg', 4000) ->> 'asset_id')::uuid;
|
||||
select is((pg_temp.row('a3')).status, 'deleted', 'a new pick supersedes the in-flight row');
|
||||
reset role;
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select throws_ok(
|
||||
$$ select public.complete_avatar_job(pg_temp.id('a3'), pg_temp.id('tok3'), 30000, repeat('c', 64)) $$,
|
||||
'P0001', 'invalid_state', 'scenario: an old task cannot complete a superseded row'
|
||||
);
|
||||
select throws_ok(
|
||||
$$ select public.reject_avatar_job(pg_temp.id('a3'), pg_temp.id('tok3'), 'decode_failed') $$,
|
||||
'P0001', 'invalid_state', 'nor reject it'
|
||||
);
|
||||
select is((pg_temp.row('a2')).status, 'clean', 'the current avatar is untouched by the stale task');
|
||||
reset role;
|
||||
select is(
|
||||
(select count(*) from private.object_purge_queue where object_name = (pg_temp.row('a3')).quarantine_key and not_before < now() + interval '2 hours'),
|
||||
1::bigint, 'the superseded quarantine key is enqueued with the 1 h grace'
|
||||
);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 9. Stale lease: an expired lease is re-claimed, the old token is refused
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a4')).quarantine_key, '{"size":4000}');
|
||||
select public.confirm_asset_upload(pg_temp.id('a4'));
|
||||
reset role;
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
insert into pg_temp.ids select 'tok4a', lock_token from public.claim_avatar_jobs(1);
|
||||
reset role;
|
||||
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a4');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
insert into pg_temp.ids select 'tok4b', lock_token from public.claim_avatar_jobs(1);
|
||||
select is((pg_temp.row('a4')).attempts, 2::smallint, 'an expired lease is re-claimed (second attempt)');
|
||||
select throws_ok(
|
||||
$$ select public.complete_avatar_job(pg_temp.id('a4'), pg_temp.id('tok4a'), 30000, repeat('d', 64)) $$,
|
||||
'P0001', 'invalid_state', 'scenario: the worker whose lease expired cannot overwrite with its stale token'
|
||||
);
|
||||
reset role;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a4')).object_key, '{"size":32000}');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select lives_ok($$ select public.complete_avatar_job(pg_temp.id('a4'), pg_temp.id('tok4b'), 32000, repeat('d', 64)) $$, 'the current lease holder completes');
|
||||
select is((pg_temp.row('a4')).status, 'clean', 'a4 is clean');
|
||||
select is((pg_temp.row('a2')).status, 'deleted', 'a2 replaced');
|
||||
reset role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 10. Removal: clean and in-flight rows go, a later complete is refused
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
insert into pg_temp.ids select 'a5', (public.begin_avatar_upload('image/webp', 5000) ->> 'asset_id')::uuid;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a5')).quarantine_key, '{"size":5000}');
|
||||
select public.confirm_asset_upload(pg_temp.id('a5'));
|
||||
reset role;
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
insert into pg_temp.ids select 'tok5', lock_token from public.claim_avatar_jobs(1);
|
||||
reset role;
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select is(public.remove_avatar(), 2, 'remove_avatar deletes the clean row and the in-flight one');
|
||||
select is((pg_temp.row('a4')).status, 'deleted', 'a4 deleted');
|
||||
select is((pg_temp.row('a5')).status, 'deleted', 'a5 deleted');
|
||||
select is((select count(*) from storage.objects where bucket_id = 'avatars'), 0::bigint, 'nothing readable after removal');
|
||||
reset role;
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select throws_ok(
|
||||
$$ select public.complete_avatar_job(pg_temp.id('a5'), pg_temp.id('tok5'), 30000, repeat('e', 64)) $$,
|
||||
'P0001', 'invalid_state', 'scenario: a removed avatar does not reappear through a late complete'
|
||||
);
|
||||
reset role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 11. Rejections: closed codes, and the third expired lease
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
insert into pg_temp.ids select 'a6', (public.begin_avatar_upload('image/jpeg', 6000) ->> 'asset_id')::uuid;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a6')).quarantine_key, '{"size":6000}');
|
||||
select public.confirm_asset_upload(pg_temp.id('a6'));
|
||||
reset role;
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
insert into pg_temp.ids select 'tok6', lock_token from public.claim_avatar_jobs(1);
|
||||
select throws_ok(
|
||||
$$ select public.reject_avatar_job(pg_temp.id('a6'), pg_temp.id('tok6'), 'virus') $$,
|
||||
'P0001', 'invalid_reason', 'a reason outside the closed set is refused'
|
||||
);
|
||||
select lives_ok($$ select public.reject_avatar_job(pg_temp.id('a6'), pg_temp.id('tok6'), 'decode_failed') $$, 'worker rejects a6');
|
||||
select is((pg_temp.row('a6')).status, 'rejected', 'a6 rejected');
|
||||
select is((pg_temp.row('a6')).rejection_reason, 'decode_failed', 'reason stored');
|
||||
reset role;
|
||||
select is(
|
||||
(select count(*) from private.object_purge_queue where object_name in ((pg_temp.row('a6')).object_key, (pg_temp.row('a6')).quarantine_key)),
|
||||
2::bigint, 'both keys of a rejected row are enqueued'
|
||||
);
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
select is((select rejection_reason from public.assets where id = pg_temp.id('a6')), 'decode_failed', 'the owner reads the rejection code');
|
||||
insert into pg_temp.ids select 'a7', (public.begin_avatar_upload('image/jpeg', 7000) ->> 'asset_id')::uuid;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a7')).quarantine_key, '{"size":7000}');
|
||||
select public.confirm_asset_upload(pg_temp.id('a7'));
|
||||
reset role;
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select is((select count(*) from public.claim_avatar_jobs(1)), 1::bigint, 'lease 1 of a7');
|
||||
reset role;
|
||||
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a7');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select is((select count(*) from public.claim_avatar_jobs(1)), 1::bigint, 'lease 2 of a7');
|
||||
reset role;
|
||||
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a7');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select is((select count(*) from public.claim_avatar_jobs(1)), 1::bigint, 'lease 3 of a7');
|
||||
reset role;
|
||||
update public.assets set locked_at = now() - interval '11 minutes' where id = pg_temp.id('a7');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select is((select count(*) from public.claim_avatar_jobs(1)), 0::bigint, 'no fourth lease');
|
||||
select is((pg_temp.row('a7')).status, 'rejected', 'the third expired lease rejects the row');
|
||||
select is((pg_temp.row('a7')).rejection_reason, 'processing_error', 'with processing_error');
|
||||
reset role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 12. Purge queue: due entries are leased, acked entries disappear
|
||||
-- ---------------------------------------------------------------------------
|
||||
update private.object_purge_queue set not_before = now() - interval '1 second'
|
||||
where object_name in ((pg_temp.row('a6')).object_key, (pg_temp.row('a6')).quarantine_key);
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
create table pg_temp.purges as select * from public.claim_purges(100);
|
||||
select is((select count(*) from pg_temp.purges), 2::bigint, 'the two due entries are claimed');
|
||||
select is((select count(*) from public.claim_purges(100)), 0::bigint, 'a leased purge entry is not claimed again');
|
||||
select lives_ok($$ select public.ack_purge(purge_id) from pg_temp.purges $$, 'worker acks both');
|
||||
reset role;
|
||||
select is(
|
||||
(select count(*) from private.object_purge_queue where object_name in ((pg_temp.row('a6')).object_key, (pg_temp.row('a6')).quarantine_key)),
|
||||
0::bigint, 'acked entries are gone'
|
||||
);
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 13. Quarantine leftovers and expiry of never-confirmed uploads
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- Objects still present under quarantine/: a1 (its row no longer references
|
||||
-- it), a2, a3, a4, a5, a6, a7 (terminal rows). None pending or uploaded.
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select is((select count(*) from public.claim_quarantine_leftovers(0, 100)), 7::bigint, 'every quarantine object without a waiting row is a leftover');
|
||||
select is((select count(*) from public.claim_quarantine_leftovers(0, 100) l where l.asset_id is null), 1::bigint,
|
||||
'a leftover whose row forgot the key (purged earlier) has no asset_id');
|
||||
select is((select count(*) from public.claim_quarantine_leftovers(3600, 100)), 0::bigint, 'nothing younger than the grace is a leftover');
|
||||
reset role;
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
insert into pg_temp.ids select 'a8', (public.begin_avatar_upload('image/jpeg', 8000) ->> 'asset_id')::uuid;
|
||||
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('a8')).quarantine_key, '{"size":8000}');
|
||||
reset role;
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select is((select count(*) from public.claim_quarantine_leftovers(0, 100) l where l.object_name = (pg_temp.row('a8')).quarantine_key), 0::bigint,
|
||||
'scenario: bytes of a pending row are not a leftover');
|
||||
select is(public.expire_pending_uploads(86400), 0, 'a fresh pending row is not expired');
|
||||
reset role;
|
||||
update public.assets set created_at = now() - interval '25 hours' where id = pg_temp.id('a8');
|
||||
set local role service_role;
|
||||
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
|
||||
select is(public.expire_pending_uploads(86400), 1, 'a pending row older than 24 h is expired');
|
||||
select is((pg_temp.row('a8')).status, 'deleted', 'expired row deleted');
|
||||
select is((select count(*) from public.claim_quarantine_leftovers(0, 100) l where l.object_name = (pg_temp.row('a8')).quarantine_key), 1::bigint,
|
||||
'scenario: bytes landed for a row that moved on are swept as a leftover');
|
||||
reset role;
|
||||
|
||||
-- ---------------------------------------------------------------------------
|
||||
-- 14. Rate limit, audit, account cascade, ceiling
|
||||
-- ---------------------------------------------------------------------------
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000a","role":"authenticated"}', true);
|
||||
-- a1..a7 = 7 rows this hour (a8 was aged in §13); three more pass, the 11th is refused.
|
||||
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'begin 8 of the hour');
|
||||
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'begin 9 of the hour');
|
||||
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'begin 10 of the hour');
|
||||
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 100) $$, 'P0001', 'rate_limited', 'the 11th begin within the hour is refused');
|
||||
reset role;
|
||||
select is(
|
||||
(select count(*) from audit.audit_logs where target_user_id = '40000000-0000-0000-0000-00000000000a' and action = 'asset.avatar_set'),
|
||||
3::bigint, 'three avatar_set audit rows (a1, a2, a4)'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from audit.audit_logs where target_user_id = '40000000-0000-0000-0000-00000000000a' and action = 'asset.avatar_removed'),
|
||||
1::bigint, 'one avatar_removed audit row'
|
||||
);
|
||||
select is(
|
||||
(select count(*) from audit.audit_logs where target_user_id = '40000000-0000-0000-0000-00000000000a' and action = 'asset.rejected'),
|
||||
2::bigint, 'two rejected audit rows (worker refusal, third expired lease)'
|
||||
);
|
||||
-- Bob's account disappears: his keys are enqueued by the delete trigger.
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000b","role":"authenticated"}', true);
|
||||
insert into pg_temp.ids select 'b1', (public.begin_avatar_upload('image/jpeg', 1000) ->> 'asset_id')::uuid;
|
||||
reset role;
|
||||
delete from auth.users where id = '40000000-0000-0000-0000-00000000000b';
|
||||
select is((select count(*) from public.assets where owner_id = '40000000-0000-0000-0000-00000000000b'), 0::bigint, 'assets cascade with the profile');
|
||||
select is(
|
||||
(select count(*) from private.object_purge_queue where object_name like 'profiles/40000000-0000-0000-0000-00000000000b/%' or object_name like 'quarantine/40000000-0000-0000-0000-00000000000b/%'),
|
||||
2::bigint, 'the cascade enqueues both keys of the deleted row'
|
||||
);
|
||||
-- Ceiling: 2048 rejected rows of 5 MiB owned by Alice fill the 10 GiB.
|
||||
insert into public.assets (owner_id, kind, bucket_id, object_key, quarantine_key, status, declared_mime, declared_size, rejection_reason, created_at)
|
||||
select '40000000-0000-0000-0000-00000000000a', 'profile_avatar', 'avatars',
|
||||
'profiles/40000000-0000-0000-0000-00000000000a/' || g || '/avatar.webp',
|
||||
'quarantine/40000000-0000-0000-0000-00000000000a/' || g || '/upload',
|
||||
'rejected', 'image/jpeg', 5242880, 'decode_failed', now() - interval '2 hours'
|
||||
from (select gen_random_uuid() as g from generate_series(1, 2048)) s;
|
||||
set local role authenticated;
|
||||
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
|
||||
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'P0001', 'storage_full', 'the global ceiling refuses a new upload');
|
||||
reset role;
|
||||
|
||||
select * from finish();
|
||||
rollback;
|
||||
Reference in new issue
Block a user