Files
faunosteo/packages/supabase/tests/db/supabase-shim.sql
T
bchanot 77e5fa4cf5 feat(supabase): V1-2.1 M4/M5 secure sheet access and review workflow
M4 20261028100000_sheet_access_hardening + M5 20261028110000_sheet_review_workflow
(applied together; no unprotected window). Implements contract §5-§14.

- Catalogue rights table + owner RPCs; has_mfa / has_aal2_session (live
  auth.sessions check); grant_active vs caller_has split; catalogue reader
  predicate; can_edit_sheet on the sheet_editor capability (F14).
- AR-1 B: aal2 required on every platform role change, target verified factor,
  race-free last-owner guard (F18/F19); service_role loses table privileges on
  the catalogue + profiles/profile_roles and EXECUTE on the role RPCs (F12/F17).
- Reviewed lifecycle: version provenance + review columns, content/freeze/
  author triggers, rounds/comments/acknowledgments, save/submit/withdraw/
  comment/acknowledge/decide, atomic publish_sheet_version(review_id) on the
  stored verdict, deprecate/archive/restore, species RPCs, equine working copy,
  uploads with the storage check (F7) + editor INSERT policy + 2 MiB bucket.
  F1-F3,F5,F6,F8,F9,F13,F15,F16 closed. Verdict is a fixture until M6 (V1-2.3).
- O22: SQL business mirror removed, declared status check kept; pgTAP 011 and
  the Rust generator's SQL parts deleted, ledger retargeted.
- Access layer sheets.ts (closed SheetFailure kinds); old client publish gate
  deleted; types regenerated; upload tool reads via get_sheet_asset_state.

Tested on the throwaway sandbox only; DEV untouched. pgTAP 850 (real stack) /
122 (PGlite) green; real-GoTrue AR-1 B + F18 race green; fresh install and
M3->M5 upgrade green; V1-1 regression green; rust:check, typecheck, lint,
build, types drift clean. BDR-084, LRN-095.
2026-10-28 15:52:49 +01:00

136 lines
4.7 KiB
PL/PgSQL

-- Minimal stand-in for the Supabase base schema, applied to a bare PGlite
-- database before the project migrations. Mirrors what the hosted/local stack
-- provides: API roles, auth schema with users table + JWT claim helpers, and
-- the permissive default privileges on public (ALL to the API roles), so a
-- migration that forgets to revoke is caught by the tests.
-- Test-only file: never applied to a real Supabase database.
create role anon nologin noinherit;
create role authenticated nologin noinherit;
create role service_role nologin noinherit bypassrls;
create schema auth;
create schema extensions;
create table auth.users (
instance_id uuid,
id uuid primary key default gen_random_uuid(),
aud varchar(255),
role varchar(255),
email varchar(255),
encrypted_password varchar(255),
email_confirmed_at timestamptz,
raw_app_meta_data jsonb,
raw_user_meta_data jsonb,
created_at timestamptz default now(),
updated_at timestamptz default now()
);
-- MFA state read by the V1-2 assurance checks (private.has_aal2_session,
-- private.has_verified_totp). Types, names and NOT NULL columns as in GoTrue
-- v2.197.0 (checked on the pinned image by tests/sandbox/mfa.sandbox.ts);
-- the columns the migrations never read are left out.
create type auth.aal_level as enum ('aal1', 'aal2', 'aal3');
create type auth.factor_type as enum ('totp', 'webauthn', 'phone', 'recovery_code');
create type auth.factor_status as enum ('unverified', 'verified');
create table auth.sessions (
id uuid primary key,
user_id uuid not null references auth.users (id) on delete cascade,
created_at timestamptz,
updated_at timestamptz,
factor_id uuid,
aal auth.aal_level,
not_after timestamptz
);
create table auth.mfa_factors (
id uuid primary key,
user_id uuid not null references auth.users (id) on delete cascade,
friendly_name text,
factor_type auth.factor_type not null,
status auth.factor_status not null,
created_at timestamptz not null,
updated_at timestamptz not null
);
create or replace function auth.uid()
returns uuid
language sql
stable
as $$
select nullif(
coalesce(
current_setting('request.jwt.claim.sub', true),
(nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'sub')
),
''
)::uuid
$$;
create or replace function auth.role()
returns text
language sql
stable
as $$
select nullif(
coalesce(
current_setting('request.jwt.claim.role', true),
(nullif(current_setting('request.jwt.claims', true), '')::jsonb ->> 'role')
),
''
)::text
$$;
create or replace function auth.jwt()
returns jsonb
language sql
stable
as $$
select coalesce(
nullif(current_setting('request.jwt.claim', true), ''),
nullif(current_setting('request.jwt.claims', true), '')
)::jsonb
$$;
grant usage on schema auth to anon, authenticated, service_role;
grant execute on function auth.uid(), auth.role(), auth.jwt() to anon, authenticated, service_role;
grant usage on schema public to anon, authenticated, service_role;
grant usage on schema extensions to anon, authenticated, service_role;
alter default privileges in schema public grant all on tables to anon, authenticated, service_role;
alter default privileges in schema public grant all on functions to anon, authenticated, service_role;
alter default privileges in schema public grant all on sequences to anon, authenticated, service_role;
-- Storage schema stand-in (buckets + objects only, the columns the
-- migrations and tests touch). The real stack ships the full schema owned by
-- supabase_storage_admin; policies declared on storage.objects apply there.
create schema storage;
grant usage on schema storage to anon, authenticated, service_role;
create table storage.buckets (
id text primary key,
name text not null,
owner uuid,
public boolean default false,
file_size_limit bigint,
allowed_mime_types text[],
created_at timestamptz default now(),
updated_at timestamptz default now()
);
create table storage.objects (
id uuid primary key default gen_random_uuid(),
bucket_id text references storage.buckets (id),
name text,
owner uuid,
metadata jsonb,
created_at timestamptz default now(),
updated_at timestamptz default now(),
last_accessed_at timestamptz default now()
);
alter table storage.objects enable row level security;
grant select on storage.buckets to anon, authenticated, service_role;
grant select, insert, update, delete on storage.objects to anon, authenticated, service_role;