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.
This commit is contained in:
bchanot committed 2026-10-28 15:52:49 +01:00
1 parent 9c13bae4bc
commit 77e5fa4cf5
33 files changed
+4613 -1617

No files matched your search

+227
View File
@@ -0,0 +1,227 @@
import type { FaunosteoClient } from './client'
import type { Json } from './database.types'
/**
* Access layer for the official anatomical-sheet workflow (V1-2.1, M4/M5):
* typed wrappers over the server RPCs and a closed set of failure kinds. The
* server is the only authority: every RPC answers a closed code (SQLSTATE plus
* a lowercase dotted message), never a value or free text. This module folds
* those answers, the session and the network into one kind the UI phrases
* (`sheet.failure.<kind>`), exactly as `records.ts` does for V1-1. No document
* validation happens here: the Rust core (in the page) and the worker verdict
* decide; the former client publication gate is gone (publication takes a
* review id only).
*
* The screens that call these wrappers arrive in V1-2.5 onward; V1-2.1 ships
* the access layer and its types only.
*/
/** Every closed failure the sheet RPCs raise (contract §6.5, §6.7, §5.2, §5.7). */
export const SHEET_FAILURE_KINDS = [
// session and rights
'authentication_required',
'mfa_required',
'forbidden',
'self_target',
'rights.account',
'rights.capability',
'rights.mfa',
'rights.owner',
'rights.catalog',
'not_found',
// platform roles (AR-1 B)
'role_active',
'role_inactive',
'last_owner',
'target_invalid',
'target_factor_missing',
'operator_path_only',
// catalogue rights
'grant.active',
'grant.ineligible',
'grant.revoked',
'grant.self_review',
'grant.target_invalid',
// drafts and saving
'draft_conflict',
'draft.unchanged',
'draft.frozen',
'draft.not_a_draft',
'document.shape',
'document.species',
'document.too_large',
// sheet and species state
'sheet.inactive',
'sheet.state',
'sheet.no_source',
'sheet.published_history',
'sheet.historical_read_only',
'sheet.species_invalid',
'species.exists',
'species.not_active',
'species.not_inactive',
// review rounds
'round.open',
'round.terminal',
'round.not_open',
'review.digest',
'review.uncoverable',
'review.separation',
'review.groups.uncovered',
'version.not_published',
'version.has_rounds',
// verdict and publication
'verdict.missing',
'verdict.stale',
'verdict.fingerprint',
'publish.review.missing',
'publish.inherited.present',
'publish.species.inactive',
'publish.species.other',
'publish.drawing.missing',
'publish.drawing.changed',
// uploads and working copy
'upload_missing',
'size_mismatch',
'working_copy.source_invalid',
'working_copy.exists',
'working_copy.assets_missing',
// shared
'invalid_input',
'rate_limited',
'session_expired',
'network',
'unavailable',
] as const
export type SheetFailureKind = (typeof SHEET_FAILURE_KINDS)[number]
const KIND_SET: ReadonlySet<string> = new Set(SHEET_FAILURE_KINDS)
export class SheetFailure extends Error {
readonly kind: SheetFailureKind
constructor(kind: SheetFailureKind, cause?: unknown) {
super(`sheet: ${kind}`, { cause })
this.name = 'SheetFailure'
this.kind = kind
}
}
interface PostgrestLikeError {
code?: string | null
message?: string
}
/**
* Folds a PostgREST answer into a closed kind. The RPCs raise the closed code
* as the message (SQLSTATE 42501, P0001, 55000, 23514 or P0002); a message
* outside the closed set, a session error (401 / PGRST30x) and a dropped
* request each map to their own kind. Fails closed on anything unknown.
*/
export function classifySheetError(error: unknown, status?: number): SheetFailureKind {
if (error instanceof SheetFailure) return error.kind
if (error instanceof TypeError) return 'network'
const e = (error ?? {}) as PostgrestLikeError
if (e.message && KIND_SET.has(e.message)) return e.message as SheetFailureKind
if (status === 401 || e.code?.startsWith('PGRST30')) return 'session_expired'
if (status === 0) return 'network'
return 'unavailable'
}
export function failSheet(error: unknown, status?: number): never {
throw new SheetFailure(classifySheetError(error, status), error)
}
// --- Rights (owner, aal2) --------------------------------------------------
export async function grantCatalogRight(
client: FaunosteoClient,
args: {
user_id: string
kind: 'catalog_reader' | 'sheet_editor' | 'sheet_reviewer' | 'sheet_publisher'
reason: string
verification_note?: string
expires_at?: string
},
): Promise<string> {
const { data, error, status } = await client.rpc('grant_catalog_right', args)
if (error) failSheet(error, status)
return data
}
export async function revokeCatalogRight(
client: FaunosteoClient,
grantId: string,
reason: string,
): Promise<string> {
const { data, error, status } = await client.rpc('revoke_catalog_right', {
grant_id: grantId,
reason,
})
if (error) failSheet(error, status)
return data
}
export async function listCatalogGrants(client: FaunosteoClient): Promise<Json> {
const { data, error, status } = await client.rpc('list_catalog_grants')
if (error) failSheet(error, status)
return data
}
// --- Sheets, drafts, review, publication -----------------------------------
export async function createOfficialSheet(
client: FaunosteoClient,
slug: string,
document: Json,
): Promise<string> {
const { data, error, status } = await client.rpc('create_official_sheet', { slug, document })
if (error) failSheet(error, status)
return data
}
export async function saveSheetDraft(
client: FaunosteoClient,
args: { version_id: string; expected_revision: number; document: Json; change_note?: string },
): Promise<Json> {
const { data, error, status } = await client.rpc('save_sheet_draft', args)
if (error) failSheet(error, status)
return data
}
export async function submitSheetReview(
client: FaunosteoClient,
args: { version_id: string; expected_revision: number; note?: string },
): Promise<string> {
const { data, error, status } = await client.rpc('submit_sheet_review', args)
if (error) failSheet(error, status)
return data
}
export async function decideSheetReview(
client: FaunosteoClient,
args: { review_id: string; approve: boolean; note: string },
): Promise<string> {
const { data, error, status } = await client.rpc('decide_sheet_review', args)
if (error) failSheet(error, status)
return data
}
/** Publishes an approved round. The gate is the server's: no client validation. */
export async function publishSheetVersion(
client: FaunosteoClient,
reviewId: string,
): Promise<string> {
const { data, error, status } = await client.rpc('publish_sheet_version', { review_id: reviewId })
if (error) failSheet(error, status)
return data
}
export async function getSheetVersion(client: FaunosteoClient, versionId: string): Promise<Json> {
const { data, error, status } = await client.rpc('get_sheet_version', { version_id: versionId })
if (error) failSheet(error, status)
return data
}
export async function listOfficialCatalog(client: FaunosteoClient): Promise<Json> {
const { data, error, status } = await client.rpc('list_official_catalog')
if (error) failSheet(error, status)
return data
}