Merge feature/step-08-files-foundation into develop
ci / rust (push) Canceled after 0s
ci / check (push) Canceled after 0s
ci / docker (push) Canceled after 0s
ci / supabase (push) Canceled after 0s

This commit is contained in:
bchanot committed 2026-10-13 21:59:54 +02:00
commit f5865f073f
67 files changed
+7113 -118

No files matched your search

+6
View File
@@ -382,3 +382,9 @@ Status: closed (R0 delivered, awaiting owner validation; R1 not started).
Owner brief after R0 delivery: dev machine = self-hosted Ubuntu native (not WSL, not VPS), Docker rootless. Decision: daily Rust = native user-level rustup (channel from `rust-toolchain.toml`, wasm32 target, wasm-bindgen-cli 0.2.129, cargo-deny 0.20.2 via `cargo install --locked`, no sudo, no global package). Root scripts `rust:check` / `wasm:build` run `scripts/rust-check.sh` / `scripts/wasm-build.sh` natively (moved from docker/, PATH guard `scripts/rust-env.sh`); `rust:check:docker` / `wasm:build:docker` = same files through the builder image (kept, reference for a future runner, proof VPS never compiles). `pnpm check:all` (`scripts/check-all.sh`) = single pre-merge gate: rust check, types drift, wasm, `pnpm check`, app image, `worker:test`, `worker:image` read-only. Docker keeps Supabase, worker container (isolation untouched), sandbox + seccomp tests, images. Workflow files kept, headers "prepared, never executed"; no runner / secret / registry. Supersedes BDR-046 (1) "no rustup on host, container = arbiter". Names: existing script names stay = native (main path), `:docker` suffix = alternative; `check` stays the Node gate, `check:all` the full one.
Alt rejected: Docker as main path (owner: native loop; Docker for services only); scripts left under docker/ (misleading location); one script per environment (drift); `check:all` needing the Supabase stack (non-hermetic; `pnpm db:test` stays the complement after a supabase/ change).
Status: active; awaiting the owner's final validation before R1.
## BDR-048 | 2026-10-13 | Step 8 / R1 implementation choices: lease token, SQL-side sweeps, ureq worker, no signed URL
Built on STEP8 D1–D9 (D1 = Rust worker, D3 = top-bar avatar + name → Mon compte). Choices beyond the analysis: (1) `assets.lock_token` uuid set by `claim_avatar_jobs`, required by complete/reject → stale worker after lease expiry refused (`invalid_state`), not only superseded rows; (2) complete refused → worker deletes nothing, purge queue owns every object of a terminal row (the analysis' "delete the clean object it wrote" would race a legit re-claim); (3) key-driven quarantine sweep = SQL anti-join RPC `claim_quarantine_leftovers` on `storage.objects` (same DB) instead of paginated Storage API listing; `expire_pending_uploads` RPC for the 24 h expiry; (4) closed refusals = SQLSTATE P0001 with the code as message, authz = 42501; TS/Rust map message → closed kind only when it has the code shape; (5) worker HTTP = `ureq` 3 (rustls + ring, webpki-roots), hand JSON log lines, 25-line base64url decoder: deviation from review Part V (tokio/reqwest/tracing/base64) justified by sequential loop + smaller keyed-process tree (34 → 84 crates, no aws-lc-rs/cmake); (6) Storage API v1.79 answers a missing object HTTP 400 + statusCode "404" → normalised to 404 = purge done (found by the e2e, queue entries looped otherwise); (7) e2e = vitest file outside the default include, runner script starts the worker container (5 s sweep, 0 grace), fresh admin-created user per run (10/h rate limit), purge clock shifted via `docker exec psql`; (8) `run --check` exit 0 when startable OR wholly unconfigured (check:all envless stage), 2 on partial config; (9) `.env.worker*` not writable here (file rule) → documented in DEPLOY.md, user creates it; (10) R1 branch cut from the R0 tip because `gitflow finish` of R0 was refused by the harness classifier.
Alt rejected: profile column avatar pointer (D5 kept derived); signed read URL (bearer outlives row flip); Storage listing sweep (3-level prefix pagination); reqwest blocking (pulls tokio).
Status: active; awaiting owner validation (TEST FONCTIONNEL + TEST GRAPHIQUE), no merge.
+7
View File
@@ -143,3 +143,10 @@ Method: host audit first (rustup already user-level, 1.99.0 auto-installed by `r
Result: verifier pass 1 ECARTS(2) = floor STUB hits on doc prose "(not implemented)" → reworded; pass 2 CONFORME 11/11, floor clean. Security pass 1 PASS + 1 MEDIUM (no advisory check has ever run: nightly job never executed) + 6 LOW (PATH prepend, `rm -rf "$out"` via variable, prctl comment overstating the kernel contract + i32 into variadic unsigned long, dormant CI triggers, native build.rs exposure, drift gate blind to untracked files) → fixed except the trigger question (owner's CI/CD step); pass 2 PASS + 1 MEDIUM (my ci.yml drift step failed open on a git error: `test -z "$(…)"` drops the exit status) + `--untracked-files=all` + stale comment → fixed (496f549), verified by execution + shellcheck, no third dispatch (3-line change). Numbers: native rust-check 32 s warm / 1 s incremental, cold workspace build 31.5 s, wasm 224 777 B (+64 B vs builder, path strings), 14 proofs, `check:all` 1 min 59 s then 1 min 39 s, 777 Node tests, 16 sandbox tests, allowlist covers the trace, worker read-only `run --check` OK.
Anomalies: the R0 note "no cargo on the host" was stale, rustup had been installed 2026-10-12 23:27 (rtk) → always re-audit the host at session start; `rustup show` has a side effect (auto-install) → acceptable here (requested install) but say so; a fix I wrote for one security finding introduced the next MEDIUM (fail-open test) → the EVAL-017 lesson again: CI one-liners need the same scrutiny as code, prefer assign-then-test; the harness clock jumped 01:16 → 09:43 mid-session (timestamps in logs are not durations).
Action: owner's final validation of the R0 correction; R1 not started; CI/CD decision separate (trigger mode, runner label, action pinning, advisories cadence).
## EVAL-019 | 2026-10-13 | Step 8 / R1 files foundation — 2 forks, verifier ×2, security ×5, e2e ×7, browser smoke
Method: contract-level `STORAGE.md` written first; migration + pgTAP by me while fork A (Rust loop) and fork B (TS + UI) ran with full context; real-stack e2e (`pnpm e2e:avatar`, worker container, fresh admin user) + headless Chromium smoke (playwright-core in scratchpad, cached chromium, dev server :5174) for the browser canvas path; contract `2026-10-13-step-08-r1-verify.md` (15 criteria, oracles dry-run before dispatch) → fresh verifier; security-auditor on `3f1105b..HEAD` then focused passes on each fix commit; `pnpm check:all` ×4.
Result: verifier pass 1 ECARTS(4) = oracle (`for update` row-lock grep) + FILE SCOPE (tracking files, lockfile) → rev 2 → CONFORME 15/15, floor clean. Security pass 1 PASS (2 MEDIUM: ceiling ratchet by rejected rows, cleartext http any host; 9 LOW) → fixed; pass 2 PASS but my fixes introduced 2 MEDIUM (bypassable host check `127.*`/numeric, ceiling undercount vs contract) → fixed; pass 3 BLOCK(1) HIGH: my purge-queue join let begin spam pin the ceiling with claims → ceiling rewritten on bytes present + cap per pending claim; pass 4 PASS (MEDIUM residual: per-account holding) → 64 MiB per-account budget; pass 5 PASS (LOW hygiene) → comments/doc. e2e 5/5 ×7 (found the Storage API 400-as-404 making purge acks loop: added queue-drained assertion); smoke 18/18 desktop+mobile; final: pgTAP 140 (real stack 420 total), app 300, supabase 37, worker 21+11 native, sandbox 16, allowlist = 7 syscalls unchanged, image 11.6 MB.
Anomalies: EVAL-017 lesson again ×3 — each security fix needed its own adversarial pass and twice created a new finding (host check, ceiling join); a security control I added became the attack lever (BLOCK). Fork B's `database.types.ts` hand-write matched the generator byte-for-byte except ordering; fork A found the 400/404 only via e2e. `gitflow finish` of R0 and any `.env*` write refused by the harness classifier; `git config` reads refused too. Verifier sandbox intercepted the literal `pnpm lint` (rtk wrapper) → oracle uses the binary path.
Action: owner's manual validation (TEST FONCTIONNEL + TEST GRAPHIQUE), owner runs `gitflow finish` for R0 then R1 after validation; no merge, no deploy, release 0.2.0 untouched.
+4
View File
@@ -236,3 +236,7 @@ Owner go (BDR-045): chore/step-08-files-analysis finished into develop (Annex C)
## 2026-10-13 (R0 correction: native toolchain)
Owner brief: dev = Ubuntu native server, Docker for services/isolation/images, VPS separate, no CI active. Host audit: rustup 1.29.1 already user-level, `rustup show` auto-installed 1.99.0; added wasm32 target, wasm-bindgen-cli 0.2.129, cargo-deny 0.20.2 (`--locked`, 100 s, no sudo, no package missing). Scripts moved docker/ → scripts/ (`rust-check.sh`, `wasm-build.sh`, PATH guard `rust-env.sh`), root `rust:check`/`wasm:build` native, `:docker` variants, `pnpm check:all` single pre-merge gate (+ warn-only advisories, porcelain drift gate). Worker unit tests now in the native check; sandbox/seccomp stay Docker. SAFETY comments rewritten as invariants (no `[gated]`), stride checked_mul, prctl args as c_ulong. Workflows kept, marked prepared/never executed. Docs: RUST.md §1/§4/§5.8/§6, CLAUDE, README, DEPLOY, WORKSPACE, TEST_STRATEGY, CHANGELOG, review C.1. Gates: verifier ECARTS(2 prose) → CONFORME; security PASS ×2 (fixes landed). `check:all` green ×2. BDR-047, LRN-050, EVAL-018. 7 commits on feature/rust-r0-foundation, pushed. Stop: owner's final validation; R1 not started; no deploy, no release.
## 2026-10-13 (Step 8 = Rust R1 delivered)
Owner validated R0, go for R1 with D1–D9 (D1 Rust worker, D3 top-bar avatar). `gitflow finish` of R0 refused by the classifier → R1 branch feature/step-08-files-foundation cut from the R0 tip (owner finishes both). Delivered: migration 20261013100000 (assets, 7 private buckets, avatar policies through assets, purge queue, 11 RPCs, lease token, per-account byte budget + bytes-present ceiling), pgTAP 010 (140), worker R1 loop (ureq, config/log/keys/backend/http/job/sweep/watchdog, 11 loop tests, child untouched, allowlist unchanged), TS assets layer + avatar UI (Mon compte, sidebar, drawer, top bar), real-stack e2e + runner, compose worker profile, STORAGE.md + docs. Gates: verifier CONFORME (pass 2), security PASS after 5 passes (1 BLOCK from my own fix), check:all ×4 green, e2e ×7, Chromium smoke 18/18. BDR-048, LRN-051, EVAL-019. Stop: owner manual validation; no merge, no deploy, no release.
+11
View File
@@ -383,3 +383,14 @@ Apply: R1 (worker loop keeps the same child), R2 (browser loader: fresh-instance
- Same rustc + lock → wasm differs by 64 B and by hash between build roots (`/w` vs `/home/...`): panic `Location` paths embedded; bit-identity needs `--remap-path-prefix` (unset at R0). Builder image = reference when bytes must match.
- Worker sandbox tests are `#![cfg(feature = "test-hooks")]`: plain `cargo test --workspace` compiles them empty → the 9 worker unit tests run natively for free (R0's `--exclude faunosteo-worker` was unneeded); seccomp derivation stays Docker/musl only (glibc syscalls differ).
- Contract oracles need a dry run before dispatch (EVAL-013/017 lesson, applied): a grep for `width * 4` matched my own SAFETY prose; `pnpm -r test:run` prints per-package totals, never one root line.
## LRN-051 | 2026-10-13 | Supabase Storage + worker facts: 400-as-404, RETURNS TABLE arg names, service_role cannot read private tables, pg_temp definer helpers in pgTAP
- storage-api v1.79 answers a missing object with HTTP 400 and body `{"statusCode":"404","error":"not_found"}` (DELETE and GET alike): classify by body, not status, or purge acks never happen (sweep errors loop every interval; caught by the e2e queue-drained assertion, not by unit tests).
- `pg_proc.proargnames` lists OUT columns of a `returns table` function: a types-vs-schema test must filter `proargmodes in ('i','b','v')` or the generator's Args (IN only) never match.
- `service_role` has BYPASSRLS but no table grants on `private.*`: a pgTAP block running as service_role cannot `select` the purge queue → read private tables after `reset role`; the worker RPCs are definer anyway.
- pgTAP pattern: `pg_temp.row(key) returns public.assets security definer` + `pg_temp.ids` map lets authenticated/service sessions inspect rows across role switches; `is((row).status, 'literal')` resolves the enum from the first arg; rows inserted in the test share `now()` → an "older than 0 s" sweep needs `<=`, not `<`.
- PostgREST: `raise exception using message = '<code>', errcode = 'P0001'` → 400 `{code:"P0001", message:"<code>"}`; only accept messages with the closed-code shape (lowercase + `_`, ≤ 40) to keep server free text out of the kinds.
- Harness: writing any `.env*` file and `gitflow finish` (merge + push to develop) are refused by the auto-mode classifier; forks (full-context subagents) parallelise Rust vs TS tracks well when a contract-level doc (STORAGE.md) is written first.
- Real browser proof without a project dep: playwright-core in the scratchpad + cached `~/.cache/ms-playwright/chromium-*` via `executablePath`, Vite on a spare port with `VITE_SUPABASE_*`; `setInputFiles` exercises createImageBitmap + canvas for real (happy-dom cannot).
Apply: every Storage call in later steps (documents, logos), every `returns table` RPC, future pgTAP on service-role paths, R2+ gates.
+10 -8
View File
@@ -126,14 +126,16 @@ feat/findings-tree-v2 merged (FindingsTreePicker tracked in HEAD); all work belo
- [x] 7.8 tests: config, authStore (fake client), locale sync, forms, guards, shell, dashboard, account; i18n parity; pgTAP 009 (PGlite + real stack 280/280); root check + docker build green; headless e2e vs local stack 20/20 (signup via Mailpit, recovery, logout, mobile)
- [x] 7.9 docs (IDENTITY.md, SUPABASE.md, CLAUDE.md, CHANGELOG, plan note); memory BDR-041, LRN-047, EVAL-015, journal; gates: verifier ECARTS(2) → open-redirect fix + contract CHECK → CONFORME 16/16; security PASS (MEDIUM fixed, 3 LOW closed, TOTP enforcement = documented gap). User validated 2026-10-12; user action still open: `.env.example` += `APP_AUTH_PROVIDERS=` (file edit denied here)
- [ ] 7.10 follow-ups kept by the user (2026-10-12), each owned by a later step: MFA mandatory before any sensitive admin feature (has_mfa helper + enrolment UI, IDENTITY.md §4; first admin screen); professional verification distinct from the SIRET format check (Step 11); real tests of Google and Apple once the provider ids exist (config.toml env(), APP_AUTH_PROVIDERS); integration of the report tool into the signed-in /app space (Step 14 gating); mockup elements not built yet listed in docs/design/README.md (bell, avatar photo → Step 8, stat tiles, tabs, mobile tab bar)
- [ ] Step 8 — files foundation + Supabase Storage (analysis 2026-10-12 on chore/step-08-files-analysis: docs/development/STEP8_FILES_ANALYSIS.md; 3 plan-challengers (47 findings folded, rev 2); AWAITING USER DECISIONS D1–D9 + go; nothing implemented). User brief: 8 private buckets closed by default, quarantine + server validation, allowlist SVG, avatar in Mon compte (upload/preview/replace/delete/header), JPEG/PNG/WebP ≤ 5 MB resized + metadata stripped, no SVG avatar, service role never in the browser, portable self-hosted → managed, allowed + denied access tests
- [ ] Step 8 = Rust R1 — files foundation + Supabase Storage + avatar (GO 2026-10-13: R0 validated, D1–D9 validated with D1 = Rust worker, D3 = top-bar avatar + name linking to Mon compte; branch feature/step-08-files-foundation, cut from the R0 tip because `gitflow finish` of R0 was refused by the harness classifier: the owner runs it). Analysis: docs/development/STEP8_FILES_ANALYSIS.md; contract-level spec written first in docs/development/STORAGE.md. Out: SVG upload (Step 13), other buckets' flows, PDF/anatomy ports, webhooks, CI/CD, VPS deploy, release 0.2.0
- [x] 8.0 analysis: facts (existing bucket + policy pattern, sanitiser, edge runtime off, CSP, deploy), design (assets table authority, server-built keys, quarantine prefix, worker `faunosteo_worker` with sharp + pdf sanitiser, storage.objects policies through assets, Storage API signing under RLS, sweeper/reconcile, backups file volume + restic, managed migration by rows), 10 security answers, scope, tests, risks, D1–D8
- [ ] 8.1 migration 20261012100000_files_foundation (asset_status, assets, 7 buckets, avatar predicates, avatars policies, purge queue + triggers, storage ceiling, 9 RPCs, revokes) + pgTAP 010 allowed/denied + policy census (one bucket literal per policy) + 001 census + types regen
- [ ] 8.2 worker → SUPERSEDED by Rust R1 (RUST_ARCHITECTURE_REVIEW Part IV, D1 = native Rust worker): `crates/worker` keyed `run` loop + keyless `decode` child, lease claim, PUT→complete→DELETE order, sweeper (purge queue + quarantine by key), per-stage deadlines, Dockerfile (distroless/static) + compose service (read-only, cpus 1, internal network) + .env.worker.example, fixtures + sandbox tests on the production image, local-stack integration test
- [ ] 8.3 packages/supabase assets service (begin/upload/confirm/remove/fetchOwnAvatar/downloadAvatar, closed failure kinds; no signed URL)
- [ ] 8.4 app: avatar block in Mon compte (picker, canvas ≤1024 JPEG transport, preview, save, processing, supersede, delete), UserBadge photo, top bar avatar (D3), authStore blob URL, i18n, tests; design toolchain + impeccable detect
- [ ] 8.5 docs: STORAGE.md (model, keys, statuses, purge/grace rules, reconciliation query, backup ordering + invariant, managed migration runbook), DEPLOY.md (worker, env file, network, CSP connect-src), SUPABASE.md, CLAUDE.md, CHANGELOG, plan note (worker early, SVG processor → Step 13); CI jobs
- [ ] 8.6 verify: root check, PGlite + real stack pgTAP, types, docker builds (app + worker), real-stack e2e (sign allowed/denied), gates verifier + security; memory; stop for user validation
- [x] 8.1 migration 20261013100000_files_foundation: `asset_status`, `assets` (lease token added for stale-task refusal), 7 buckets (avatars open: 5 MiB, jpeg/png/webp; 6 closed; sheet bucket untouched), `private.can_read_avatar_object` / `can_upload_avatar_object`, storage.objects SELECT + INSERT policies, `private.object_purge_queue` + status/delete triggers (48 h final key of a deleted row, 1 h quarantine and rejected), `private.storage_ceiling()`, RPCs begin_avatar_upload / confirm_asset_upload / remove_avatar (authenticated), claim_avatar_jobs / complete_avatar_job / reject_avatar_job / mark_quarantine_purged / claim_purges / ack_purge / claim_quarantine_leftovers / expire_pending_uploads (service_role), closed error messages (P0001), explicit revokes, column grants
- [x] 8.2 pgTAP 010: allowed + denied twins for every RPC and policy, the 8 scenarios of the owner's brief (cross-user read, cross-user replace/delete, anon, quarantine never readable, final key never uploadable, superseded/removed row never completes, stale lease refused, late bytes inert), purge queue contents and grace, policy census (one bucket literal per policy, 2/1/0 per bucket, total pinned), audit rows; 001 census += the two predicates; shim adjustments if needed; real stack `db:reset` + `db:test`; `db:types` + schema-types map
- [x] 8.3 crates/worker R1 (fork A): `config` (env, no secret printed), `log` (JSON lines, field allowlist, child stderr escaped and truncated), `backend` trait + `http` (ureq 3, rustls + ring, PostgREST rpc + Storage object endpoints, 30 s timeouts, 5 MiB download cap, strict key validation before any URL), `decoder` trait over `Spawner`, `job` (claim → download → decode → PUT clean → complete → DELETE quarantine → mark purged; refusals → reject; `Broken` → alert + stop; complete refused → nothing deleted, the queue owns the object), `sweep` (claim_purges/ack, quarantine leftovers, expire pending), `watchdog` (per-stage deadlines, exit on overrun), `run` loop + `run --check`; unit tests on a fake backend + fake decoder; seccomp derivation re-run (the parent gained TLS/HTTP code, the child must stay at the traced set); JWT exp logged at start
- [x] 8.4 packages/supabase `assets.ts` (fork B): beginAvatarUpload, uploadToQuarantine, confirmAssetUpload, removeAvatar, fetchOwnAvatarState, downloadAvatar, closed `AssetFailure` kinds (P0001 messages → kinds; storage errors → network/unknown); tests on a stub client
- [x] 8.5 app (fork B): `account/avatarTransport.ts` (magic-byte sniff jpeg/png/webp, never SVG, createImageBitmap → canvas ≤ 1024 → JPEG 0.9, ≤ 5 MiB after transport), `AvatarSection` in Mon compte (circle photo/monogram, Modifier la photo, preview + Enregistrer/Annuler, processing with 1 s polling ≤ 30 s, rejection message per code, Supprimer with Dialog confirmation, in-flight row on mount), authStore `avatarUrl` blob (download under RLS, revoked on change/sign-out), `UserBadge` photo, top bar avatar + name → /app/account (D3), i18n FR/EN parity, component + store tests, `impeccable detect`
- [x] 8.6 integration: docker-compose `worker` service under profile `worker` (read-only, cap_drop ALL, no-new-privileges, mem 512m, cpus 1, pids 64, external Supabase network), `.env.worker` variables documented (file not editable here), real-stack e2e `pnpm --filter @faunosteo/supabase test:e2e` (worker container against the local stack: Alice flow clean + download 200, replace, remove, Bob 400, anon 400, quarantine 400, late signed-upload bytes swept, purge after grace via clock shift)
- [x] 8.7 docs: STORAGE.md final (model, keys, statuses, worker operations, purge/grace, reconciliation query, backup ordering + invariant, managed migration runbook), DEPLOY.md (worker service, env file, network, CSP connect-src), SUPABASE.md (worker in dev, buckets), RUST.md §3 (R1 loop), WORKSPACE.md, TEST_STRATEGY.md, IDENTITY.md §5 avatar steps, docs/design/README.md (avatar built), CLAUDE.md, CHANGELOG; CI yml untouched beyond a comment (no runner)
- [x] 8.8 verify: `pnpm check:all` ×4, PGlite + real stack pgTAP (140 / 420), types unchanged, worker image + sandbox tests, e2e ×7, Chromium smoke 18/18, verifier CONFORME (pass 2), security PASS after 5 passes (BLOCK on my ceiling join fixed: bytes-present ceiling + 64 MiB per-account budget); BDR-048, LRN-051, EVAL-019, journal. OPEN: owner manual validation; owner runs `gitflow finish` for feature/rust-r0-foundation then, after validation, for this branch; `.env.worker` to create by hand (DEPLOY.md); residual LOWs documented in STORAGE.md §2.1 (colluding-account ceiling fill, search-domain on native glibc runs, ceiling race)
- [ ] Rust by default (rule adopted 2026-10-12, BDR-043; orientation + 10 principles VALIDATED by the owner 2026-10-12, BDR-044; study `docs/development/RUST_ARCHITECTURE_REVIEW.md` rev 2 + Annex A decision table + Annex B R0 scope on chore/step-08-files-analysis; AWAITING the owner's validation of decisions D-R1..D-R14 (D-R6 withdrawn); R0 and Step 8 NOT started until then). Roadmap (each phase: oracles frozen with the old code, consumer-driven deletion, rollback row, gates):
- [ ] R0 foundation (GO 2026-10-12, BDR-045; branch feature/rust-r0-foundation; Annex B; owner constraints: builds reproducible in Docker, no Rust on the VPS, live deployment untouched, frontend never loads the module, no R1):
- [x] R0.1 toolchain: `docker/rust-builder.Dockerfile` (rust:1.99.0-bookworm + wasm target + wasm-bindgen-cli 0.2.129 + cargo-deny 0.20.2 + rustfmt/clippy), `docker/rust-run.sh` (named volumes faunosteo_cargo_registry / faunosteo_rust_target), `docker/rust-check.sh`, `docker/wasm-build.sh` (CLI/crate version lock), root scripts rust:builder / rust:check / wasm:build / worker:test / worker:image
@@ -158,7 +160,7 @@ feat/findings-tree-v2 merged (FindingsTreePicker tracked in HEAD); all work belo
- [x] R0.11.7 docs: RUST.md §1/§4/§6, CLAUDE.md, README verifying, DEPLOY.md, WORKSPACE.md, TEST_STRATEGY.md, CHANGELOG, review Annex C addendum
- [x] R0.11.8 gates: verifier ECARTS(2 doc prose) → CONFORME 11/11; security PASS ×2 (1 MEDIUM + 6 LOW then 1 MEDIUM + 2 LOW, fixed: advisories warn-only stage, fail-closed porcelain drift gate with --untracked-files=all, PATH guard append/absolute, literal rm, prctl c_ulong args); BDR-047, LRN-050, EVAL-018, journal
- [ ] R0.11 follow-ups for the CI/CD step (owner): ci.yml push/PR triggers stay live, so registering a runner starts PR builds at once (keep, or switch to workflow_dispatch until then); `actions/*@v4` mutable tags (Step 27); advisories cadence while no nightly runs (`cargo deny check advisories` is warn-only in check:all); `--remap-path-prefix` if native and builder wasm must match byte for byte
- [ ] R1 = Step 8 worker in Rust (replaces 8.2 below): decode child dispatched before any thread, empty env, RLIMIT_AS/CPU, traced seccomp allowlist, `image` + `moxcms` + `libwebp-sys` in the child, parent RIFF-header check only, 512 KiB output cap, per-stage deadlines, service key
- [x] R1 = Step 8 worker in Rust (delivered 2026-10-13 on feature/step-08-files-foundation, see Step 8 block; awaiting owner validation): decode child dispatched before any thread, empty env, RLIMIT_AS/CPU, traced seccomp allowlist, `image` + `moxcms` + `libwebp-sys` in the child, parent RIFF-header check only, 512 KiB output cap, per-stage deadlines, service key
- [ ] R2 domain core (before Step 9): sheet/report/archive contracts, `SheetHandle` + `SheetIndexData`, reducers, Finding builder, vertebra selection, publish gate; wasm facade (JSON strings, ts-rs types, poison + reinstantiate, lazy init); app switched; 3 envelopes frozen first; N-1 reader test; draft quarantine; conformance fixtures + generated pgTAP; `packages/domain` frozen for engine/pipeline
- [ ] R3 anatomy core: tree, one sanitiser, serialiser, layer renderer, `compose_view_svg`; app canvas switched; composed views frozen as trees first
- [ ] R4 pdf core: reader (Web Worker, BLK-009 browser measurements) + writer (`pdf-writer`, Std14 AFM, draw log; lines/rects/colours golden frozen with the TS engine first, D-R2) + `report-compare` tool; `packages/pdf` deleted; N-1 scanner reads R4 PDFs
@@ -0,0 +1,59 @@
# CONTRACT — step-08 / Rust R1: files foundation, worker job loop, avatar
- date: 2026-10-13 | flow: orchestrator + 2 forks, verifier gate | branch: feature/step-08-files-foundation (cut from the R0 tip 3f1105b)
- status: dispatched after the implementation commits
- spec: docs/development/STORAGE.md (contract), docs/development/STEP8_FILES_ANALYSIS.md (D1–D9, D1 = Rust worker), RUST_ARCHITECTURE_REVIEW.md Part IV, owner brief 2026-10-13 (sections 2–7)
- diff range: 3f1105b..HEAD (run every CHECK from the repository root)
## REQUEST (IMMUTABLE)
Step 8 = R1: `assets` table as the authority over user files, eight private buckets (only `avatars` open through policies evaluated on `assets`), quarantine upload path, deferred purge queue, browser and worker RPCs with closed error codes; the Rust worker's keyed job loop + sweeper on the R0 sandboxed decode child (unchanged); the avatar in Mon compte, the sidebar, the drawer and the top bar (link to Mon compte, no dropdown, no redesign); tests at every layer; real-stack e2e; docs. Out: other buckets' flows, SVG upload, PDF/anatomy ports, CI runner, deploy, release, merge into develop.
## ACCEPTANCE CRITERIA
1. The SQL foundation applies on PGlite and every pgTAP file passes, including the new 010 (125 checks) and the extended 001 census.
CHECK: pnpm --filter @faunosteo/supabase exec vitest run tests/db/pgtap.test.ts 2>&1 | grep -E "^ +Tests "
EXPECT: Tests 11 passed (11)
2. Exactly three policies are created by the migration (assets own-row SELECT, avatars read, avatars upload), the two Storage ones pin the bucket literal, and no UPDATE / DELETE policy exists on storage.objects.
CHECK: grep -c "^create policy" supabase/migrations/20261013100000_files_foundation.sql; grep -c "on storage.objects" supabase/migrations/20261013100000_files_foundation.sql; grep -cE "^ for (update|delete)$" supabase/migrations/20261013100000_files_foundation.sql
EXPECT: 3 / 2 / 0 (the policy command clause is a line of its own; `select … for update` row locks inside functions are not policies)
3. The owner's column grant on assets excludes every lease and quarantine column.
CHECK: sed -n '/^grant select ($/,/to authenticated;/p' supabase/migrations/20261013100000_files_foundation.sql | grep -c "lock_token\|quarantine_key\|locked_at\|attempts"
EXPECT: 0
4. Both worker verdict RPCs refuse a stale lease token (status uploaded AND token match).
CHECK: grep -c "lock_token is distinct from lock_token" supabase/migrations/20261013100000_files_foundation.sql
EXPECT: 2
5. The eight scenarios of the owner's brief are named as pgTAP descriptions (cross-user read, cross-user replace/delete, anon, quarantine never readable, final key never written by the browser, superseded / removed row never completes, stale lease refused, late bytes swept) — at least 12 descriptions carry the `scenario:` marker.
CHECK: grep -c "scenario:" supabase/tests/010_files_foundation.sql
EXPECT: 13
6. The R0 sandboxed child is untouched by R1 (decode, sandbox, spawn, webp_check unchanged in the range).
CHECK: git diff 3f1105b..HEAD --stat -- crates/worker/src/decode.rs crates/worker/src/sandbox.rs crates/worker/src/spawn.rs crates/worker/src/webp_check.rs | wc -l
EXPECT: 0
7. The worker links no async runtime and no aws-lc-rs (ureq on rustls + ring, review deviation recorded).
CHECK: . scripts/rust-env.sh && cargo tree -p faunosteo-worker --prefix none 2>/dev/null | grep -cE "^(tokio|reqwest|aws-lc-rs) "
EXPECT: 0
8. Keys from the database are validated before any URL (http, job, sweep all call the key validators) and no log or print site exposes the service key.
CHECK: grep -n "validate_any_key\|validate_object_key\|validate_quarantine_key" crates/worker/src/http.rs crates/worker/src/job.rs crates/worker/src/sweep.rs | wc -l; grep -rn "println!\|eprintln!\|format!" crates/worker/src/ | grep -c "expose()"
EXPECT: 5 / 0
9. The native Rust gate is green (fmt, clippy -D warnings, core + worker tests including the 11 job-loop tests, ts-rs export, cargo deny).
CHECK: bash scripts/rust-check.sh > /tmp/rust-check-verify.log 2>&1; echo "exit $?"; grep -cE "^test result: ok" /tmp/rust-check-verify.log
EXPECT: exit 0 / at least 6
10. The browser never selects `*` on assets and never holds a service role reference.
CHECK: grep -n "from('assets')" -A2 packages/supabase/src/assets.ts | grep -c "'\*'"; grep -rn "SERVICE_ROLE\|service_role" apps/app/src packages/supabase/src | grep -v database.types | wc -l
EXPECT: 0 / 0
11. The Node gate is green with the new tests (supabase package 37, app 300).
CHECK: pnpm --filter @faunosteo/supabase test:run 2>&1 | grep -E "^ +Tests "; pnpm --filter @faunosteo/app test:run 2>&1 | grep -E "^ +Tests "; ./node_modules/.bin/pnpm typecheck > /dev/null 2>&1 && ./node_modules/.bin/pnpm lint > /dev/null 2>&1 && echo GATE_OK
EXPECT: Tests 37 passed (37) / Tests 300 passed (300) / GATE_OK
12. The avatar transport refuses SVG by content and the UI section exists with the mockup's elements (change, save, cancel, remove, processing), strings in both dictionaries.
CHECK: grep -c "svg" apps/app/tests/unit/account.avatarTransport.test.ts; grep -c "'account.avatar\." apps/app/src/i18n/fr.ts; grep -c "'account.avatar\." apps/app/src/i18n/en.ts
EXPECT: 4 / 29 / 29
13. The compose worker service is hardened and opt-in (profile), reads its own env file, joins the external Supabase network.
CHECK: grep -c 'profiles: \["worker"\]\|read_only: true\|cap_drop: \[ALL\]' docker-compose.yml; grep -c "\.env\.worker" docker-compose.yml; docker compose --profile worker config --no-interpolate 2>&1 | grep -c "container_name: faunosteo_worker"
EXPECT: 3 / 1 / 1
14. The real-stack end-to-end suite and its runner exist, typecheck, and are documented; the e2e is not part of the default vitest include.
CHECK: test -f packages/supabase/tests/e2e/avatar-flow.e2e.ts && test -x scripts/avatar-e2e.sh && grep -c "e2e:avatar" package.json docs/development/STORAGE.md | tr '\n' ' '; grep -c "e2e" packages/supabase/vitest.config.ts
EXPECT: package.json:1 docs/development/STORAGE.md:1 / 0
15. Docs updated: STORAGE.md present; CLAUDE.md, DEPLOY.md, SUPABASE.md, RUST.md, TEST_STRATEGY.md, WORKSPACE.md, IDENTITY.md, docs/design/README.md and CHANGELOG.md reference the step.
CHECK: for f in CLAUDE.md DEPLOY.md docs/development/SUPABASE.md docs/development/RUST.md docs/development/TEST_STRATEGY.md docs/development/WORKSPACE.md docs/development/IDENTITY.md docs/design/README.md CHANGELOG.md; do printf '%s:%s ' "$f" "$(grep -cE 'STORAGE.md|Step 8|avatar' "$f")"; done
EXPECT: every count ≥ 1
## FILE SCOPE
supabase/migrations/20261013100000_files_foundation.sql, supabase/tests/{001_schema,010_files_foundation}.sql, packages/supabase/{src,tests,package.json,vitest.e2e.config.ts}, apps/app/src/{account,components/account,components/shell,state/authStore.ts,i18n}, apps/app/tests, crates/worker/**, Cargo.toml, Cargo.lock, deny.toml, docker-compose.yml, scripts/avatar-e2e.sh, package.json, pnpm-lock.yaml (zod importer entry), docs/**, CLAUDE.md, README.md, DEPLOY.md, CHANGELOG.md, .claude/tasks/** (TODO plan and this contract), .claude/memory/** (registries).
- revision 2 (2026-10-13, after verifier pass 1 ECARTS(4), all oracle/scope): criterion 2 third grep anchored to the policy clause; criterion 11 lint via the binary path (the literal `pnpm lint` is intercepted by a sandbox output wrapper); FILE SCOPE widened to the lockfile and the tracking files.
+4
View File
@@ -6,6 +6,10 @@ Versions follow [Semantic Versioning](https://semver.org/).
## [Unreleased]
### Added (SaaS migration, plan Step 8 = Rust R1)
- **Files foundation and profile photo (Step 8)** — `public.assets` as the authority over user files (status, owner, server-built keys, worker lease token), seven private Storage buckets (only `avatars` open, through SELECT / INSERT policies evaluated on `assets`; the six others closed until their step), a quarantine upload path (`begin_avatar_upload` → direct upload at the quarantine key → `confirm_asset_upload`), a deferred purge queue (48 h for a replaced or removed avatar, 1 h for quarantine and rejected uploads: the backup invariant) and the audited RPCs of the browser and of the worker (migration `20261013100000`, pgTAP 010 with 125 allowed and denied checks, policy census). **Rust worker job loop** (`crates/worker`, R1): the keyed parent claims leased jobs through PostgREST, downloads the quarantine bytes, hands them to the sandboxed `decode` child of R0 (empty environment, rlimits, seccomp allowlist unchanged), uploads the 512 × 512 lossy WebP, commits the row, deletes the quarantine object, rejects with closed codes (`unsupported_type`, `decode_failed`, `dimensions`, `processing_error`), sweeps expired uploads, the purge queue and quarantine leftovers; JSON line logs with a field allowlist; per-stage watchdog; Compose service `worker` (profile `worker`, read-only, no capability, external Supabase network, `.env.worker`). **Profile photo** in Mon compte (preview before any request, save, processing with polling, rejection messages, replacement, removal with confirmation) and in the sidebar, the mobile drawer and the top bar (name beside it on desktop, link to Mon compte); JPEG / PNG / WebP only, never SVG, downscaled to 1024 px in the browser as transport, judged again by the worker; the owner downloads the clean object under RLS (no signed URL). Real-stack end-to-end run `pnpm e2e:avatar`. Spec: `docs/development/STORAGE.md`.
### Added (Rust foundation, plan R0)
- **Rust by default** (rule adopted 2026-10-12, `docs/development/RUST_ARCHITECTURE_REVIEW.md`): Cargo workspace with three crates, `crates/core` (contracts and the JavaScript-compatible JSON formatter, no I/O by lint), `crates/wasm` (browser facade built into `packages/core-wasm/pkg`) and `crates/worker` (the future file-processing worker: sandboxed `decode` child with rlimits and a seccomp allowlist derived by `strace`, image decoding in pure Rust, lossy WebP through libwebp confined to the child, parent-side container check; `run --check` only, no job loop before R1). Reference toolchain in Docker (`docker/rust-builder.Dockerfile`, `pnpm rust:check`, `pnpm wasm:build`), worker image on `distroless/static`, CI job `rust`, nightly advisories, manual digest-tagged image workflow (no deploy). R0 proofs recorded in `docs/development/RUST.md`: `float_roundtrip` required for JSON parity, traps leave a wasm instance unusable (fresh instance per recovery), CSP needs `'wasm-unsafe-eval'`. No application behaviour, interface, archive or database change; the frontend does not load the module.
+39 -6
View File
@@ -27,7 +27,12 @@ confirmation, password recovery, OAuth buttons driven by config, the guarded
`/app` shell (dashboard, Mon compte with personal / professional / preferences /
security sections) on Supabase Auth + `profiles` + `profile_roles`, language
synced with `profiles.locale` (`docs/development/IDENTITY.md`). The local tool
routes stay unauthenticated.
routes stay unauthenticated. Step 8 (= Rust R1) added the files foundation: the
`assets` table as the authority over user files, seven private buckets (only
`avatars` open, through policies evaluated on `assets`), a quarantine upload path,
the Rust worker `crates/worker` (keyed job loop + keyless sandboxed decode child)
producing 512 × 512 WebP avatars, a deferred purge queue, and the profile photo in
Mon compte, the sidebar, the drawer and the top bar (`docs/development/STORAGE.md`).
MVP species: equine only. Open-source MIT.
---
@@ -112,6 +117,7 @@ pnpm rust:check:docker # the same rust-check.sh inside the builder image
pnpm wasm:build:docker # the same wasm-build.sh inside the builder image
pnpm worker:test # Docker only: crates/worker/Dockerfile stage test, sandbox tests + seccomp derivation on the musl binary
pnpm worker:image # Docker only: runtime image faunosteo_worker:dev (distroless/static, `run --check`)
pnpm e2e:avatar # scripts/avatar-e2e.sh: worker container against the local stack + packages/supabase/tests/e2e (real-stack avatar flow)
```
## Docker commands
@@ -213,7 +219,8 @@ faunosteo/
│ │ ├─ brand/{Brand,AppHeader,Footer,LanguageSwitcher,HomeSpaceLink}.tsx
│ │ ├─ auth/{AuthLayout,OAuthButtons,PasswordField,UnavailableNotice,AuthLink}.tsx
│ │ ├─ shell/{AppLayout,SidebarNav,MobileDrawer,UserBadge,SignOutButton}.tsx
│ │ └─ account/{SectionCard,SaveRow,PersonalInfoForm,ProfessionalInfoForm,PreferencesForm,SecurityPanel,RoleBadges}.tsx
│ │ └─ account/{SectionCard,SaveRow,AvatarSection,PersonalInfoForm,ProfessionalInfoForm,PreferencesForm,SecurityPanel,RoleBadges}.tsx
│ ├─ account/avatarTransport.ts ← magic-byte sniff (never SVG), createImageBitmap → canvas ≤ 1024 px → JPEG transport blob
│ ├─ pdf/ ← app side of @faunosteo/pdf (engine in packages/pdf)
│ │ ├─ export.ts + import.ts ← exportReportToPdf (sheet snapshot + branding + browser rasterizer → engine), importReportFromPdf (ArchiveReadOutcome, ImportError kinds)
│ │ └─ branding.ts + browserRasterizer.ts ← bundled src/assets/brand logos (?inline / ?raw) → Branding; Image/canvas Rasterizer adapter
@@ -245,11 +252,12 @@ faunosteo/
│ ├─ src/curation/{schema,targets,fields,ops,rename,apply,types,assets,labels,stats,changelog,review-report,overlay,seed,sources,pipeline}.ts ← changeset → draft
│ └─ tests/ ← counts preserved, special cases, determinism, curation ops, golden artifacts
├─ packages/supabase/ ← @faunosteo/supabase
│ ├─ src/{index,client,database.types,anatomical-sheets}.ts ← typed client, generated types, publish gate
│ ├─ src/{index,client,database.types,anatomical-sheets,assets}.ts ← typed client, generated types, publish gate, avatar flow (closed AssetFailure kinds)
│ ├─ tests/e2e/avatar-flow.e2e.ts + vitest.e2e.config.ts ← real-stack end-to-end (`pnpm e2e:avatar`), skipped without the stack env
│ └─ tests/{client,anatomical-sheets}.test.ts, tests/db/ ← PGlite harness, shim, pgTAP runner, schema↔types test
├─ supabase/
│ ├─ config.toml, seed.sql ← CLI config (public schema only), local dev accounts
│ ├─ migrations/*.sql ← forward-only; foundation, profiles, platform_roles, anatomical_sheets, storage, sheet_document_schema_v2
│ ├─ migrations/*.sql ← forward-only; foundation, profiles, platform_roles, anatomical_sheets, storage, sheet_document_schema_v2, profiles_professional, files_foundation (assets, buckets, avatar policies, purge queue, RPCs)
│ ├─ seed/anatomical-sheets/equine/
│ │ ├─ legacy-sources/ ← the historical Horse.svg, Horse_head.svg, pathologies.json, vertebrae.ts (importer input, never edited)
│ │ ├─ legacy-import/ ← GENERATED baseline: document, id map, IMPORT-REPORT, SVG copies (no SQL)
@@ -261,8 +269,9 @@ faunosteo/
├─ crates/ ← Rust workspace (Cargo.toml, rust-toolchain.toml, deny.toml, clippy.toml, .cargo/config.toml at the root)
│ ├─ core/ ← faunosteo-core: contracts (report types), js_number + JS pretty formatter, facade `call(op, json)`; no I/O, no clock (lint-enforced)
│ ├─ wasm/ ← faunosteo-wasm: wasm-bindgen facade (core_version, call, R0 trap proofs)
│ └─ worker/ ← faunosteo-worker: plain main; `decode` sandboxed child (rlimits, seccomp allowlist, image → libwebp), parent Spawner + RIFF check, `run --check`; Dockerfile, scripts/derive-seccomp.sh
├─ scripts/{rust-env,rust-check,wasm-build,check-all}.sh ← native Rust gate, wasm build, the pre-merge gate (`pnpm check:all`)
│ └─ worker/ ← faunosteo-worker: plain main; `decode` sandboxed child (rlimits, seccomp allowlist, image → libwebp), parent Spawner + RIFF check; R1 `run` loop: config, log (JSON lines, allowlist), keys, backend trait + http (ureq, PostgREST rpc + Storage objects), job, sweep, watchdog; Dockerfile, scripts/derive-seccomp.sh
├─ scripts/{rust-env,rust-check,wasm-build,check-all,avatar-e2e}.sh ← native Rust gate, wasm build, the pre-merge gate (`pnpm check:all`), the real-stack avatar e2e
├─ docker-compose.yml ← `app` service; `worker` service under the `worker` profile (read-only, cap_drop ALL, external Supabase network, `.env.worker`)
├─ docker/rust-builder.Dockerfile, rust-run.sh, csp-proof/ ← reproducible Rust builder (alternative) + the CSP proof
├─ .gitea/workflows/ci.yml ← PREPARED, never executed (no runner): rust job (builder image, scripts/rust-check.sh, types drift, wasm), install, typecheck, test, lint, build, docker (app + worker test stage + runtime)
├─ .gitea/workflows/{rust-advisories,release-images}.yml ← nightly RustSec advisories; manual digest-tagged image publication (no deploy)
@@ -319,6 +328,15 @@ Identity (Step 7, docs/development/IDENTITY.md)
→ RequireAuth guards /app/* (→ /login?next=), AppLayout (sidebar / top bar / drawer), Dashboard, Account (updateOwnProfile)
→ no Supabase configured: status unavailable, auth screens show a notice, local tool unchanged
Files and avatar (Step 8 / R1, docs/development/STORAGE.md)
→ AvatarSection: pick → avatarTransport (sniff jpeg/png/webp, bitmap → canvas ≤ 1024 → JPEG) → preview (no request)
→ save: rpc begin_avatar_upload (row pending_upload, server-built keys) → storage upload at the quarantine key (INSERT policy)
→ rpc confirm_asset_upload (uploaded) → poll own assets row 1 s ≤ 30 s → clean: download under the SELECT policy → blob URL in authStore
→ worker (container faunosteo_worker, service key): claim_avatar_jobs (lease token) → GET quarantine → decode child (sandbox, 512² WebP)
→ POST clean object (upsert) → complete_avatar_job (previous clean → deleted, enqueued 48 h) → DELETE quarantine → mark_quarantine_purged
→ refusals: reject_avatar_job(closed code); sweeper: expire_pending_uploads, claim_purges/ack_purge, claim_quarantine_leftovers
→ remove_avatar: rows → deleted, objects purged after the grace; a deleted / superseded row never completes (status + token guards)
Runtime config
→ Docker entrypoint writes env.js (window.__APP_CONFIG__)
→ index.html loads env.js before bundle
@@ -415,6 +433,20 @@ Database (Supabase)
operator scripts under `packages/*/scripts/` read it from the environment and are never bundled.
- pgTAP files run on PGlite and on the real image: scope counts to fixture ids, pin `collate "C"` on text ordering.
### Files and Storage (migration 20261013100000, docs/development/STORAGE.md)
- `public.assets` is the authority over every user file: status, owner, keys, lease. Every write is an RPC; the browser
reads its own rows through an explicit column list (`select=*` is refused: lease columns are not granted).
- Object keys are server-built from uuids (`profiles/<owner>/<id>/avatar.webp`, `quarantine/<owner>/<id>/upload`);
policies on `storage.objects` compare whole strings through `assets` and pin one bucket literal each (census test).
A quarantine object is never readable by an API role; a final key is never writable by the browser.
- New buckets stay closed (row only, no policy) until their step writes the rules; `anatomical-sheet-assets` keeps its own policy.
- The worker holds the service key; the decode child has an empty environment, no file, no network, and is the only
process that decodes untrusted bytes. The parent checks the container header only. Rejections are closed codes.
- Deletions are row-driven: terminal rows enqueue their keys (48 h / 1 h grace), the sweeper serves the queue and the
quarantine leftovers; a clean row's object is never deleted. Backups: dump first, files within 48 h.
- Avatar UI follows the validated mockup: photo in Mon compte (Informations personnelles), sidebar, drawer and top bar
link to Mon compte; JPEG / PNG / WebP only, never SVG; no signed URL for the owner-only avatar (download under RLS).
### Anatomical sheet model (packages/domain + migration 20261008110000)
- `docs/development/ANATOMICAL_SHEET_MODEL.md` is the source of truth: document shape, issue codes, tables, RPCs.
- Ids are stable slugs never derived from labels; labels are `{ fr, en }` objects. Annotation → findings is the
@@ -514,6 +546,7 @@ the owner's validation before any implementation; release 0.2.0 unaffected).
- After PDF roundtrip tests fail: check attachment bytes, then the layered reader (report first, envelope metadata, snapshot/branding/labels), then DecompressionStream inflation. Goldens under `apps/app/tests/fixtures/goldens/step6` are never regenerated without a user decision.
- Node 24 / pnpm 10.13.1: `corepack enable` or `corepack pnpm`; never a global install.
- Rust: native on the Ubuntu development machine (user-level rustup, never a global package, never on the VPS); `pnpm rust:check` and `pnpm wasm:build` run natively, the `:docker` variants reproduce them in the builder image. Before every Rust commit: `pnpm rust:check` green, `pnpm worker:test` green when `crates/worker` changed (Docker only: the sandbox tests and the seccomp derivation never run natively), the ts-rs types under `packages/core-wasm/types` committed, `Cargo.lock` committed. Before a merge: `pnpm check:all`. No CI runs on the forge: a workflow file is never cited as a passed check. The core takes ids, time and bytes as inputs (clippy `disallowed_methods`); the worker child is the only process that decodes untrusted bytes.
- Storage or worker change: `pnpm e2e:avatar` (worker container against the local stack, `packages/supabase/tests/e2e`) in addition to `pnpm worker:test`; `docker compose --profile worker` serves the worker from `.env.worker` (never the app `.env`).
- SQL change: `pnpm --filter @faunosteo/supabase test:run` (PGlite) before commit; `pnpm db:reset && pnpm db:test`
on the real stack when Docker is available; regenerate types (`pnpm db:types`).
Generated
+690 -2
View File
@@ -14,12 +14,27 @@ version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "base64"
version = "0.23.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5"
[[package]]
name = "bitflags"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
[[package]]
name = "block-buffer"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d2f6c7dbe95a6ed67ad9f18e57daf93a2f034c524b99fd2b76d18fdfeb6660aa"
dependencies = [
"hybrid-array",
]
[[package]]
name = "bumpalo"
version = "3.20.3"
@@ -38,6 +53,12 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f1fe948ff07f4bd06c30984e69f5b4899c516a3ef74f34df92a2df2ab535495"
[[package]]
name = "bytes"
version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc652a48c352aef3ea3aed32080501cf3ef6ed5da78602a020c991775b0aff04"
[[package]]
name = "cc"
version = "1.8.0"
@@ -56,6 +77,50 @@ version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
[[package]]
name = "const-oid"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a6ef517f0926dd24a1582492c791b6a4818a4d94e789a334894aa15b0d12f55c"
[[package]]
name = "cookie"
version = "0.18.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a373e3602691c3cdea496d2f0ee5935151e6168fe87739483c463db1b2f2f87"
dependencies = [
"percent-encoding",
"time",
"version_check",
]
[[package]]
name = "cookie_store"
version = "0.22.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15b2c103cf610ec6cae3da84a766285b42fd16aad564758459e6ecf128c75206"
dependencies = [
"cookie",
"document-features",
"idna",
"indexmap",
"log",
"serde",
"serde_derive",
"serde_json",
"time",
"url",
]
[[package]]
name = "cpufeatures"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca28b0ae3115b884660db4118d803791fd6756b6e88f39c0f3f7859060d7566"
dependencies = [
"libc",
]
[[package]]
name = "crc32fast"
version = "1.5.2"
@@ -65,6 +130,52 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "crypto-common"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ce6e4c961d6cd6c9a86db418387425e8bdeaf05b3c8bc1411e6dca4c252f1453"
dependencies = [
"hybrid-array",
]
[[package]]
name = "deranged"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e"
[[package]]
name = "digest"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1dd6dbb5841937940781866fa1281a1ff7bd3bf827091440879f9994983d5c2"
dependencies = [
"block-buffer",
"const-oid",
"crypto-common",
]
[[package]]
name = "displaydoc"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c6232dd377dcc64799954cbd3a9bb882e9cdc1308ccd87b1c098f1fb2eaf82a8"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
]
[[package]]
name = "document-features"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d4b8a88685455ed29a21542a33abd9cb6510b6b129abadabdcef0f4c55bc8f61"
dependencies = [
"litrs",
]
[[package]]
name = "equivalent"
version = "1.0.2"
@@ -99,6 +210,10 @@ dependencies = [
"libwebp-sys",
"rlimit",
"seccompiler",
"serde",
"serde_json",
"sha2",
"ureq",
]
[[package]]
@@ -133,6 +248,26 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
[[package]]
name = "form_urlencoded"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb4cb245038516f5f85277875cdaa4f7d2c9a0fa0468de06ed190163b1581fcf"
dependencies = [
"percent-encoding",
]
[[package]]
name = "getrandom"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff2abc00be7fca6ebc474524697ae276ad847ad0a6b3faa4bcb027e9a4614ad0"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "getrandom"
version = "0.4.3"
@@ -156,6 +291,135 @@ version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
[[package]]
name = "http"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0"
dependencies = [
"bytes",
"itoa",
]
[[package]]
name = "httparse"
version = "1.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87"
[[package]]
name = "hybrid-array"
version = "0.4.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27f864f10dfb56725ce5ce5472bc52252c8f93a4ab86327122cebf62c5f59a17"
dependencies = [
"typenum",
]
[[package]]
name = "icu_collections"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa68d21081c4a05d5a901a1c62add574c77048b6a1c67be3b50ce0b60d4ca513"
dependencies = [
"displaydoc",
"potential_utf",
"utf8_iter",
"yoke",
"zerofrom",
"zerovec",
]
[[package]]
name = "icu_locale_core"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d56e28588da92eee5c3201a6eff33fabdd49b62269c8938d4ff050ce4d900deb"
dependencies = [
"displaydoc",
"litemap",
"tinystr",
"writeable",
"zerovec",
]
[[package]]
name = "icu_normalizer"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "12f9cf5f235641ed274641dd81c3f28d870e276763d0797aeeab72317b1c646f"
dependencies = [
"icu_collections",
"icu_normalizer_data",
"icu_properties",
"icu_provider",
"smallvec",
"zerovec",
]
[[package]]
name = "icu_normalizer_data"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1563da1ed3e0b3bf3d74c9b85917ac9c56464d2f57242270c09c9e752f8021a0"
[[package]]
name = "icu_properties"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e7ca276ad3145661a65914e6daf131ca5120cd3dcee8f8f3214b8875184a148"
dependencies = [
"displaydoc",
"icu_collections",
"icu_locale_core",
"icu_properties_data",
"icu_provider",
"zerotrie",
"zerovec",
]
[[package]]
name = "icu_properties_data"
version = "2.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e590f038c1464a96894fd6d10127e90a8be4509f56ff7ecef851b15cee0b7caa"
[[package]]
name = "icu_provider"
version = "2.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d27bbb9d3abbefac45d55f647c9de1d44aafcd1186eb91879afef17c396c3e73"
dependencies = [
"displaydoc",
"icu_locale_core",
"writeable",
"yoke",
"zerofrom",
"zerotrie",
"zerovec",
]
[[package]]
name = "idna"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b0875f23caa03898994f6ddc501886a45c7d3d62d04d2d90788d47be1b1e4de"
dependencies = [
"idna_adapter",
"smallvec",
"utf8_iter",
]
[[package]]
name = "idna_adapter"
version = "1.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb68373c0d6620ef8105e855e7745e18b0d00d3bdb07fb532e434244cdb9a714"
dependencies = [
"icu_normalizer",
"icu_properties",
]
[[package]]
name = "image"
version = "0.25.10"
@@ -204,7 +468,7 @@ version = "0.1.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1c00acbd29eabad4a2392fa0e921c874934dbbf4194312ad20f04a0ed67a3cb3"
dependencies = [
"getrandom",
"getrandom 0.4.3",
"libc",
]
@@ -225,6 +489,24 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "litemap"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d9d19d1d6efa0109d2f65ff4c85cddd50bd572e5a00127ab10987290bcefae"
[[package]]
name = "litrs"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4744e383959f0db86ede514b809b1c53251889093803c05267acc7d4e7030d70"
[[package]]
name = "log"
version = "0.4.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f9f8bd3e56ce4dfc153cf470fffbfa98c7620958b312ca5c3a4b8d5181fd13c6"
[[package]]
name = "memchr"
version = "2.8.3"
@@ -261,6 +543,12 @@ dependencies = [
"pxfm",
]
[[package]]
name = "num-conv"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441"
[[package]]
name = "num-traits"
version = "0.2.19"
@@ -276,6 +564,12 @@ version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "percent-encoding"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220"
[[package]]
name = "pkg-config"
version = "0.3.34"
@@ -295,6 +589,21 @@ dependencies = [
"miniz_oxide 0.8.9",
]
[[package]]
name = "potential_utf"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d83eb9bc6d8e5cf568e7a1101d60ee05e81ed50ea106026f3d18deeb046d7661"
dependencies = [
"zerovec",
]
[[package]]
name = "powerfmt"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958"
[[package]]
name = "proc-macro2"
version = "1.0.107"
@@ -331,6 +640,20 @@ version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "ring"
version = "0.17.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7"
dependencies = [
"cc",
"cfg-if",
"getrandom 0.2.17",
"libc",
"untrusted",
"windows-sys 0.52.0",
]
[[package]]
name = "rlimit"
version = "0.11.0"
@@ -340,6 +663,41 @@ dependencies = [
"libc",
]
[[package]]
name = "rustls"
version = "0.23.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634"
dependencies = [
"log",
"once_cell",
"ring",
"rustls-pki-types",
"rustls-webpki",
"subtle",
"zeroize",
]
[[package]]
name = "rustls-pki-types"
version = "1.15.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f4925028c7eb5d1fcdaf196971378ed9d2c1c4efc7dc5d011256f76c99c0a96"
dependencies = [
"zeroize",
]
[[package]]
name = "rustls-webpki"
version = "0.103.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2"
dependencies = [
"ring",
"rustls-pki-types",
"untrusted",
]
[[package]]
name = "rustversion"
version = "1.0.23"
@@ -400,6 +758,17 @@ dependencies = [
"zmij",
]
[[package]]
name = "sha2"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "47d7069beb7d6ac7b9acd1039986e73443f24234f41074da099d6f994ac9ad19"
dependencies = [
"cfg-if",
"cpufeatures",
"digest",
]
[[package]]
name = "shlex"
version = "2.0.1"
@@ -412,6 +781,24 @@ version = "0.3.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3a219298ac11a56ea9a6d2120044824d6f01aeb034955e7af7bc16858527deea"
[[package]]
name = "smallvec"
version = "1.16.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b3dc8af474f516a851ff4bd12db780f948b9250ad37211e4eec0bccea54e01b"
[[package]]
name = "stable_deref_trait"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6ce2be8dc25455e1f91df71bfa12ad37d7af1092ae736f3a6cd0e37bc7810596"
[[package]]
name = "subtle"
version = "2.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292"
[[package]]
name = "syn"
version = "2.0.119"
@@ -434,6 +821,17 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "synstructure"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "901704edd0dfe137f1987838ee4f259e4e063c31371bdb423f7ae38ec6f77f02"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
]
[[package]]
name = "termcolor"
version = "1.4.1"
@@ -463,6 +861,46 @@ dependencies = [
"syn 3.0.9",
]
[[package]]
name = "time"
version = "0.3.55"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134"
dependencies = [
"deranged",
"num-conv",
"powerfmt",
"serde_core",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109"
[[package]]
name = "time-macros"
version = "0.2.32"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "tinystr"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1e27c91459209c2986af3dcf603a5a74a4368754ce37414f59acc971167f643"
dependencies = [
"displaydoc",
"zerovec",
]
[[package]]
name = "ts-rs"
version = "12.0.1"
@@ -485,12 +923,91 @@ dependencies = [
"termcolor",
]
[[package]]
name = "typenum"
version = "1.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20"
[[package]]
name = "unicode-ident"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
[[package]]
name = "untrusted"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1"
[[package]]
name = "ureq"
version = "3.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a7ac20be9b7726e0bbdbf974c059676d9acb1cd414961f570a4e8231cacd7fc"
dependencies = [
"base64",
"cookie_store",
"log",
"percent-encoding",
"rustls",
"rustls-pki-types",
"serde",
"serde_json",
"ureq-proto",
"utf8-zero",
"webpki-roots",
]
[[package]]
name = "ureq-proto"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f86fd172ccca569e458f61b6bdd6220965a9ef36e672a6852953b51a0e1583be"
dependencies = [
"base64",
"http",
"httparse",
"log",
]
[[package]]
name = "url"
version = "2.5.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff67a8a4397373c3ef660812acab3268222035010ab8680ec4215f38ba3d0eed"
dependencies = [
"form_urlencoded",
"idna",
"percent-encoding",
"serde",
]
[[package]]
name = "utf8-zero"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e"
[[package]]
name = "utf8_iter"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6c140620e7ffbb22c2dee59cafe6084a59b5ffc27a8859a5f0d494b5d52b6be"
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "wasm-bindgen"
version = "0.2.129"
@@ -536,13 +1053,22 @@ dependencies = [
"unicode-ident",
]
[[package]]
name = "webpki-roots"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a"
dependencies = [
"rustls-pki-types",
]
[[package]]
name = "winapi-util"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22"
dependencies = [
"windows-sys",
"windows-sys 0.61.2",
]
[[package]]
@@ -551,6 +1077,15 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-sys"
version = "0.61.2"
@@ -560,6 +1095,159 @@ dependencies = [
"windows-link",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "writeable"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc"
[[package]]
name = "yoke"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "709fe23a0424b6a435d82152b1bd3fdfb0833487d5fa90d05d42762a9891fef5"
dependencies = [
"stable_deref_trait",
"yoke-derive",
"zerofrom",
]
[[package]]
name = "yoke-derive"
version = "0.8.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec8ebde2db3681e8c9980cc27822030e68752690ddfa9473e739aeb4dbde6d71"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
]
[[package]]
name = "zerofrom"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ec05a11813ea801ff6d75110ad09cd0824ddba17dfe17128ea0d5f68e6c5272"
dependencies = [
"zerofrom-derive",
]
[[package]]
name = "zerofrom-derive"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f75b4683f6c7f45248d4d64056a24298c6281e0993356d7d1b4a1a962ef10d4a"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
"synstructure",
]
[[package]]
name = "zeroize"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e13084392c5e4bc371903e2935a5eaeed24905a7511356b883835e18a78f6879"
[[package]]
name = "zerotrie"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ea269c3bd32f0a32c321907a2ae912ba6f4649bb0fc764a15627e99a7095a3f"
dependencies = [
"displaydoc",
"yoke",
"zerofrom",
]
[[package]]
name = "zerovec"
version = "0.11.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bb0464e17806c1d976d5cba29399c7f08e516e279e2ba493f63123b5fca67dd8"
dependencies = [
"yoke",
"zerofrom",
"zerovec-derive",
]
[[package]]
name = "zerovec-derive"
version = "0.11.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "34df6fc39dbd26ddc9c10e6a2984476e13acce22e64e4487636ef494369225da"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.9",
]
[[package]]
name = "zlib-rs"
version = "0.6.8"
+4
View File
@@ -29,6 +29,10 @@ libwebp-sys = "0.14.4"
seccompiler = "0.5.0"
rlimit = "0.11"
libc = "0.2"
# Worker job loop (R1): blocking HTTP client (rustls + ring, bundled webpki roots for a managed
# Supabase over HTTPS) and the hash of the clean output. No async runtime: the loop is sequential.
ureq = { version = "3.4.2", default-features = false, features = ["rustls", "json"] }
sha2 = "0.11"
# The library crates deny unwrap/expect so that no panic path depends on content;
# the worker binary relaxes disallowed_methods (it owns I/O) in its own [lints].
+28 -3
View File
@@ -21,10 +21,13 @@ The host should publish only ports `22`, `80`, `443` to the internet. The
container port (`8080` by default) should stay loopback-only — the host
reverse proxy reads from it on `127.0.0.1:8080`.
Rust (plan R0, `docs/development/RUST.md`): the repository also builds a
Rust (plan R0/R1, `docs/development/RUST.md`): the repository also builds a
worker image (`crates/worker/Dockerfile`, static musl binary on
`distroless/static`, container `faunosteo_worker`). It is not deployed yet
(its job loop arrives at R1); the live deployment above is unchanged. The
`distroless/static`, container `faunosteo_worker`, service `worker` under the
Compose profile `worker`). Since Step 8 it carries the files job loop (avatar
processing, purge sweeps: `docs/development/STORAGE.md` §3). It is not deployed
yet: it needs the self-hosted Supabase stack beside it, which production does
not run today; the live deployment above is unchanged. The
production VPS never needs a Rust toolchain: Rust is compiled on the Ubuntu
development machine (native toolchain) or inside Docker builder images, and
production images will be pulled by digest ("Image deploy by digest",
@@ -101,6 +104,28 @@ the service_role key never leaves the Supabase host (see
The `.env` file is not sensitive but should still be `chmod 600` to keep
local identity overrides private.
### Worker environment (`.env.worker`, Step 8)
The files worker (`docker compose --profile worker up -d worker`) reads a
second env file, `.env.worker`, never the app `.env`: it holds the service
key. `chmod 600`, excluded from every backup copy of env files, readable
through `docker inspect` by the `docker` group (documented limit). The
assistant cannot write `.env*` files in this repository: create it by hand
from the table (STORAGE.md §3.2 lists the optional tuning variables).
| Variable | Required | Notes |
| --- | --- | --- |
| `SUPABASE_URL` | yes | Kong origin on the Supabase compose network (`http://supabase_kong_faunosteo:8000` on the local CLI stack; the self-hosted stack's own Kong name in production). Never the public URL when the stack is on the same host. |
| `SUPABASE_SERVICE_ROLE_KEY` | yes | the service key; on self-hosted a long-lived JWT (revoking it = rotating `JWT_SECRET`). Never in `.env`, env.js or the repository. |
| `WORKER_ID` | no | tag of the log lines (default `worker-<pid>`). |
The service joins the external network named by `SUPABASE_NETWORK`
(default `supabase_network_faunosteo`, the local CLI stack); it publishes no
port, runs read-only with every capability dropped, 512 MiB, 1 CPU, 64 pids.
Browser side, the production CSP `connect-src` must name the Supabase
origin the app talks to (already required since Step 7), and `img-src`
keeps `blob:` (the avatar is shown from a blob URL downloaded under RLS).
---
## Provision the VPS
+5 -1
View File
@@ -66,7 +66,11 @@ pnpm dev # http://localhost:5173
Rust (core, browser module, worker): a user-level `rustup` installs the
pinned toolchain from `rust-toolchain.toml` on first use; see
`docs/development/RUST.md` §1 for the three `cargo install` / `rustup
target add` lines and the Docker alternative.
target add` lines and the Docker alternative. The files worker (profile
photos, Step 8) runs as a container beside the local Supabase stack:
`pnpm worker:image`, then `pnpm e2e:avatar` for the real-stack end-to-end
run, or `docker compose --profile worker up worker` with a `.env.worker`
(`docs/development/STORAGE.md`).
Docker Compose (matches production topology):
+133
View File
@@ -0,0 +1,133 @@
import { AssetFailure } from '@faunosteo/supabase'
/**
* Browser-side preparation of a profile photo (docs/development/STORAGE.md
* §4): sniff the real type, decode, downscale to a transport size, re-encode
* as JPEG. Transport only: the worker re-decodes and judges the bytes again.
*/
export type SniffedImageType = 'image/jpeg' | 'image/png' | 'image/webp'
/** Largest file accepted at the picker (sanity bound before decoding). */
export const MAX_PICKED_BYTES = 20 * 1024 * 1024
/** Largest blob actually transferred (the bucket cap and the RPC CHECK). */
export const MAX_TRANSPORT_BYTES = 5 * 1024 * 1024
/** Longest side after the canvas downscale. */
export const TRANSPORT_MAX_SIDE = 1024
const JPEG_QUALITY = 0.9
function startsWith(bytes: Uint8Array, prefix: number[], at = 0): boolean {
if (bytes.length < at + prefix.length) return false
return prefix.every((b, i) => bytes[at + i] === b)
}
/** Magic bytes only: SVG, GIF, BMP or any text are null. */
export function sniffImageType(bytes: Uint8Array): SniffedImageType | null {
if (startsWith(bytes, [0xff, 0xd8, 0xff])) return 'image/jpeg'
if (startsWith(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])) {
return 'image/png'
}
if (
startsWith(bytes, [0x52, 0x49, 0x46, 0x46]) &&
startsWith(bytes, [0x57, 0x45, 0x42, 0x50], 8)
) {
return 'image/webp'
}
return null
}
/** Canvas size for a bitmap: longest side capped, never upscaled. */
export function transportSize(
width: number,
height: number,
maxSide = TRANSPORT_MAX_SIDE,
): { width: number; height: number } {
const longest = Math.max(width, height)
if (longest <= maxSide) return { width, height }
const scale = maxSide / longest
return {
width: Math.max(1, Math.round(width * scale)),
height: Math.max(1, Math.round(height * scale)),
}
}
export interface BitmapLike {
width: number
height: number
close?: () => void
}
export interface CanvasLike {
width: number
height: number
getContext: (kind: '2d') => { drawImage: (image: unknown, x: number, y: number, w: number, h: number) => void } | null
toBlob: (cb: (blob: Blob | null) => void, type: string, quality: number) => void
}
/** The browser APIs involved, injectable for tests under happy-dom. */
export interface TransportDeps {
createImageBitmap: (file: Blob) => Promise<BitmapLike>
createCanvas: () => CanvasLike
createObjectURL: (blob: Blob) => string
}
function defaultDeps(): TransportDeps {
return {
createImageBitmap: (file) => createImageBitmap(file),
createCanvas: () => document.createElement('canvas') as unknown as CanvasLike,
createObjectURL: (blob) => URL.createObjectURL(blob),
}
}
export interface PreparedAvatar {
blob: Blob
previewUrl: string
}
async function readHead(file: Blob): Promise<Uint8Array> {
return new Uint8Array(await file.slice(0, 16).arrayBuffer())
}
function encodeJpeg(canvas: CanvasLike): Promise<Blob> {
return new Promise((resolve, reject) => {
canvas.toBlob(
(blob) => (blob ? resolve(blob) : reject(new AssetFailure('unsupported_type'))),
'image/jpeg',
JPEG_QUALITY,
)
})
}
/**
* Sniff → decode → downscale → JPEG. Throws AssetFailure('too_large' |
* 'unsupported_type'); never uploads anything.
*/
export async function prepareAvatarUpload(
file: File,
deps: TransportDeps = defaultDeps(),
): Promise<PreparedAvatar> {
if (file.size > MAX_PICKED_BYTES) throw new AssetFailure('too_large')
if (sniffImageType(await readHead(file)) === null) {
throw new AssetFailure('unsupported_type')
}
let bitmap: BitmapLike
try {
bitmap = await deps.createImageBitmap(file)
} catch (cause) {
throw new AssetFailure('unsupported_type', cause)
}
try {
const size = transportSize(bitmap.width, bitmap.height)
const canvas = deps.createCanvas()
canvas.width = size.width
canvas.height = size.height
const context = canvas.getContext('2d')
if (!context) throw new AssetFailure('unsupported_type')
context.drawImage(bitmap, 0, 0, size.width, size.height)
const blob = await encodeJpeg(canvas)
if (blob.size > MAX_TRANSPORT_BYTES) throw new AssetFailure('too_large')
return { blob, previewUrl: deps.createObjectURL(blob) }
} finally {
bitmap.close?.()
}
}
@@ -0,0 +1,280 @@
import { useEffect, useRef, useState, type ChangeEvent } from 'react'
import {
beginAvatarUpload,
classifyAssetError,
confirmAssetUpload,
isAvatarRejectionCode,
removeAvatar,
uploadToQuarantine,
type AssetFailureKind,
type AvatarRejectionCode,
type AvatarState,
} from '@faunosteo/supabase'
import Button from '@/components/ui/Button'
import Dialog from '@/components/ui/Dialog'
import Notice from '@/components/ui/Notice'
import { monogram } from '@/components/shell/UserBadge'
import { prepareAvatarUpload, type PreparedAvatar } from '@/account/avatarTransport'
import { getSupabaseClient } from '@/lib/supabase'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
/** Polling of the own row after a save (docs/development/STORAGE.md §4 step 4). */
export const POLL_INTERVAL_MS = 1000
export const POLL_MAX_MS = 30_000
const ACCEPT = 'image/jpeg,image/png,image/webp'
type Phase =
| { kind: 'idle' }
| { kind: 'picked'; prepared: PreparedAvatar }
| { kind: 'saving' }
| { kind: 'processing'; long: boolean }
| { kind: 'saved' }
| { kind: 'removed' }
| { kind: 'rejected'; code: AvatarRejectionCode }
| { kind: 'failed'; failure: AssetFailureKind }
function revoke(url: string | undefined) {
if (url && typeof URL.revokeObjectURL === 'function') URL.revokeObjectURL(url)
}
/** Photo, preview or monogram in the circle; the busy state replaces it with a label. */
function Circle({
src,
label,
busy,
busyLabel,
}: {
src: string | null
label: string
busy: boolean
busyLabel: string
}) {
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const base =
'grid h-24 w-24 shrink-0 place-items-center overflow-hidden rounded-full bg-[var(--color-accent)] text-2xl font-semibold text-white'
if (busy) {
return (
<div className={`${base} bg-[var(--color-surface-strong)] text-xs font-medium text-[var(--color-ink-muted)]`} role="status">
<span className="px-2 text-center leading-tight">{busyLabel}</span>
</div>
)
}
if (src) {
return (
<div className={base}>
<img src={src} alt={label} className="h-full w-full object-cover" />
</div>
)
}
return (
<div className={base} aria-hidden="true">
{monogram(profile?.display_name, user?.email)}
</div>
)
}
/**
* Profile photo block of "Mon compte": pick → local preview → save (begin,
* quarantine upload, confirm) → poll until the worker's verdict → photo.
* Removal asks once. Every message is a closed i18n key.
*/
export default function AvatarSection() {
const t = useT()
const avatarUrl = useAuthStore((s) => s.avatarUrl)
const avatar = useAuthStore((s) => s.avatar)
const refreshAvatar = useAuthStore((s) => s.refreshAvatar)
const clearAvatar = useAuthStore((s) => s.clearAvatar)
const [phase, setPhase] = useState<Phase>(() =>
avatar?.inFlight ? { kind: 'processing', long: false } : { kind: 'idle' },
)
const [confirmOpen, setConfirmOpen] = useState(false)
const inputRef = useRef<HTMLInputElement>(null)
const pollRef = useRef<number | null>(null)
const stopPolling = () => {
if (pollRef.current !== null) {
window.clearInterval(pollRef.current)
pollRef.current = null
}
}
/** One tick per second until the row leaves `uploaded`, 30 s at most. */
const startPolling = () => {
stopPolling()
const startedAt = Date.now()
pollRef.current = window.setInterval(() => {
void refreshAvatar()
.then((state: AvatarState) => {
if (state.inFlight === null) {
stopPolling()
setPhase(settledPhase(state))
} else if (Date.now() - startedAt >= POLL_MAX_MS) {
stopPolling()
setPhase({ kind: 'processing', long: true })
}
})
.catch((error: unknown) => {
stopPolling()
setPhase({ kind: 'failed', failure: classifyAssetError(error) })
})
}, POLL_INTERVAL_MS)
}
// A row already in flight at mount resumes the bounded polling (refs: no stale closure, no deps).
const startPollingRef = useRef(startPolling)
startPollingRef.current = startPolling
const resumeOnMount = useRef(phase.kind === 'processing')
useEffect(() => {
if (resumeOnMount.current) startPollingRef.current()
return () => {
if (pollRef.current !== null) window.clearInterval(pollRef.current)
}
}, [])
useEffect(() => {
return () => {
if (phase.kind === 'picked') revoke(phase.prepared.previewUrl)
}
}, [phase])
async function onPick(e: ChangeEvent<HTMLInputElement>) {
const file = e.target.files?.[0]
e.target.value = ''
if (!file) return
try {
const prepared = await prepareAvatarUpload(file)
setPhase({ kind: 'picked', prepared })
} catch (error) {
setPhase({ kind: 'failed', failure: classifyAssetError(error) })
}
}
async function onSave() {
if (phase.kind !== 'picked') return
const client = getSupabaseClient()
if (!client) return
const { blob } = phase.prepared
setPhase({ kind: 'saving' })
try {
const ticket = await beginAvatarUpload(client, {
declaredMime: 'image/jpeg',
declaredSize: blob.size,
})
await uploadToQuarantine(client, { ...ticket, blob })
await confirmAssetUpload(client, ticket.assetId)
setPhase({ kind: 'processing', long: false })
startPolling()
} catch (error) {
setPhase({ kind: 'failed', failure: classifyAssetError(error) })
}
}
async function onRemove() {
const client = getSupabaseClient()
setConfirmOpen(false)
if (!client) return
try {
await removeAvatar(client)
clearAvatar()
setPhase({ kind: 'removed' })
} catch (error) {
setPhase({ kind: 'failed', failure: classifyAssetError(error) })
}
}
const busy = phase.kind === 'saving' || phase.kind === 'processing'
const picked = phase.kind === 'picked'
const shown = picked ? phase.prepared.previewUrl : avatarUrl
const rejected =
phase.kind === 'rejected'
? phase.code
: phase.kind === 'idle' && avatar?.lastRejected && isAvatarRejectionCode(avatar.lastRejected.rejection_reason)
? avatar.lastRejected.rejection_reason
: null
return (
<div className="space-y-3" data-testid="avatar-section">
<h3 className="text-sm font-medium text-[var(--color-ink)]">{t('account.avatar.title')}</h3>
<div className="flex flex-wrap items-center gap-4">
<Circle
src={shown}
label={t('account.avatar.alt')}
busy={busy}
busyLabel={phase.kind === 'saving' ? t('account.avatar.saving') : t('account.avatar.processing')}
/>
<div className="flex flex-wrap items-center gap-2">
<input
ref={inputRef}
type="file"
accept={ACCEPT}
onChange={onPick}
className="sr-only"
aria-label={t('account.avatar.change')}
disabled={busy}
/>
{picked ? (
<>
<Button type="button" onClick={onSave}>
{t('account.avatar.save')}
</Button>
<Button type="button" variant="ghost" onClick={() => setPhase({ kind: 'idle' })}>
{t('account.avatar.cancel')}
</Button>
</>
) : (
<>
<Button
type="button"
variant="secondary"
disabled={busy}
onClick={() => inputRef.current?.click()}
>
{t('account.avatar.change')}
</Button>
{avatarUrl && !busy && (
<Button type="button" variant="danger" onClick={() => setConfirmOpen(true)}>
{t('account.avatar.remove')}
</Button>
)}
</>
)}
</div>
</div>
<p className="text-xs text-[var(--color-ink-muted)]">
{phase.kind === 'processing' && !phase.long
? t('account.avatar.processing_hint')
: t('account.avatar.hint')}
</p>
{phase.kind === 'processing' && phase.long && (
<Notice tone="info">{t('account.avatar.processing_long')}</Notice>
)}
{phase.kind === 'saved' && <Notice tone="success">{t('account.avatar.saved')}</Notice>}
{phase.kind === 'removed' && <Notice tone="success">{t('account.avatar.removed')}</Notice>}
{phase.kind === 'failed' && (
<Notice tone="error">{t(`account.avatar.error.${phase.failure}`)}</Notice>
)}
{rejected && <Notice tone="error">{t(`account.avatar.rejected.${rejected}`)}</Notice>}
<Dialog open={confirmOpen} onClose={() => setConfirmOpen(false)} title={t('account.avatar.remove_title')} size="sm">
<p className="text-sm text-[var(--color-ink-muted)]">{t('account.avatar.remove_body')}</p>
<div className="mt-4 flex justify-end gap-2">
<Button type="button" variant="ghost" onClick={() => setConfirmOpen(false)}>
{t('common.cancel')}
</Button>
<Button type="button" variant="danger" onClick={onRemove}>
{t('account.avatar.remove_confirm')}
</Button>
</div>
</Dialog>
</div>
)
}
/** The verdict once no row is in flight: the clean photo, a rejection, or nothing new. */
function settledPhase(state: AvatarState): Phase {
if (state.lastRejected && isAvatarRejectionCode(state.lastRejected.rejection_reason)) {
return { kind: 'rejected', code: state.lastRejected.rejection_reason }
}
return state.clean ? { kind: 'saved' } : { kind: 'idle' }
}
@@ -1,6 +1,7 @@
import type { Profile } from '@faunosteo/supabase'
import FormField, { fieldDescribedBy } from '@/components/ui/FormField'
import Input from '@/components/ui/Input'
import AvatarSection from './AvatarSection'
import SaveRow from './SaveRow'
import SectionCard from './SectionCard'
import { personalPatch, personalSchema, translateFieldError, type PersonalValues } from '@/account/validation'
@@ -27,7 +28,8 @@ export default function PersonalInfoForm({ profile }: { profile: Profile }) {
title={t('account.personal.title')}
description={t('account.personal.description')}
>
<form onSubmit={form.submit} noValidate className="space-y-4">
<AvatarSection />
<form onSubmit={form.submit} noValidate className="mt-6 space-y-4 border-t border-[var(--color-border)] pt-6">
<FormField id="display_name" label={t('account.personal.display_name')} error={err('display_name')}>
<Input
id="display_name"
+15 -2
View File
@@ -6,7 +6,8 @@ import Icon from '@/components/ui/Icon'
import MobileDrawer from './MobileDrawer'
import SidebarNav, { NAV_ITEMS } from './SidebarNav'
import SignOutButton from './SignOutButton'
import UserBadge from './UserBadge'
import UserBadge, { AvatarCircle } from './UserBadge'
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
/** Title of the current /app page for the top bar, from the nav table. */
@@ -25,6 +26,9 @@ export default function AppLayout() {
const t = useT()
const location = useLocation()
const [drawerOpen, setDrawerOpen] = useState(false)
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const displayName = profile?.display_name ?? user?.email ?? ''
return (
<div className="flex min-h-dvh bg-[var(--color-bg)]">
@@ -60,8 +64,17 @@ export default function AppLayout() {
<h1 className="hidden lg:block text-sm font-medium text-[var(--color-ink-muted)]">
{t(pageTitleKey(location.pathname))}
</h1>
<div className="ml-auto flex items-center gap-2">
<div className="ml-auto flex items-center gap-2 sm:gap-3">
<LanguageSwitcher />
<Link
to="/app/account"
aria-label={t('shell.nav.account')}
data-testid="topbar-account"
className="flex min-h-11 items-center gap-2 rounded-full pr-1 pl-1 text-sm text-[var(--color-ink)] hover:bg-[var(--color-surface-strong)] focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-[var(--color-accent)] sm:pr-3"
>
<AvatarCircle size="sm" />
<span className="hidden max-w-[12rem] truncate sm:inline">{displayName}</span>
</Link>
</div>
</header>
<main className="flex-1 px-4 py-6 sm:px-8 sm:py-8">
+27 -7
View File
@@ -1,4 +1,5 @@
import { useAuthStore } from '@/state/authStore'
import { useT } from '@/i18n/useT'
/** Two-letter monogram from the display name, else the email. */
export function monogram(name: string | undefined, email: string | undefined) {
@@ -10,19 +11,38 @@ export function monogram(name: string | undefined, email: string | undefined) {
return letters.toUpperCase()
}
/** Avatar monogram + display name + email, as shown at the bottom of the sidebar. */
const CIRCLE =
'grid shrink-0 place-items-center overflow-hidden rounded-full bg-[var(--color-accent)] font-semibold text-white'
/** The photo when the store holds one, the monogram otherwise; decorative. */
export function AvatarCircle({ size = 'md' }: { size?: 'sm' | 'md' }) {
const t = useT()
const avatarUrl = useAuthStore((s) => s.avatarUrl)
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const dims = size === 'sm' ? 'h-8 w-8 text-xs' : 'h-9 w-9 text-sm'
if (avatarUrl) {
return (
<span className={`${CIRCLE} ${dims}`}>
<img src={avatarUrl} alt={t('account.avatar.alt')} className="h-full w-full object-cover" />
</span>
)
}
return (
<span aria-hidden="true" className={`${CIRCLE} ${dims}`}>
{monogram(profile?.display_name, user?.email)}
</span>
)
}
/** Avatar + display name + email, as shown at the bottom of the sidebar. */
export default function UserBadge() {
const profile = useAuthStore((s) => s.profile)
const user = useAuthStore((s) => s.user)
const name = profile?.display_name ?? user?.email ?? ''
return (
<div className="flex items-center gap-3 min-w-0">
<span
aria-hidden="true"
className="grid h-9 w-9 shrink-0 place-items-center rounded-full bg-[var(--color-accent)] text-sm font-semibold text-white"
>
{monogram(profile?.display_name, user?.email)}
</span>
<AvatarCircle />
<span className="flex min-w-0 flex-col leading-tight">
<span className="truncate text-sm font-medium text-[var(--color-ink)]">
{name}
+42
View File
@@ -372,4 +372,46 @@ export const en: Dict = {
'Not available in this version yet. It will be offered, then required for platform roles.',
'account.security.sign_out_everywhere_note':
'Signing out closes the session on this device.',
'account.avatar.title': 'Profile photo',
'account.avatar.alt': 'Profile photo',
'account.avatar.change': 'Change photo',
'account.avatar.save': 'Save photo',
'account.avatar.saving': 'Uploading…',
'account.avatar.cancel': 'Cancel',
'account.avatar.remove': 'Remove photo',
'account.avatar.remove_title': 'Remove the photo?',
'account.avatar.remove_body':
'Your profile photo will be removed. You can add another one at any time.',
'account.avatar.remove_confirm': 'Remove',
'account.avatar.removed': 'Photo removed.',
'account.avatar.processing': 'Processing…',
'account.avatar.processing_hint':
'Your photo is being checked and resized. This takes a few seconds.',
'account.avatar.processing_long':
'Processing is taking longer than expected. Come back in a moment.',
'account.avatar.saved': 'Photo saved.',
'account.avatar.hint': 'JPEG, PNG or WebP, 5 MB at most once reduced.',
'account.avatar.error.too_large': 'Image too large (5 MB at most).',
'account.avatar.error.unsupported_type':
'Unsupported format: choose a JPEG, PNG or WebP image.',
'account.avatar.error.rate_limited':
'Too many attempts. Try again in an hour.',
'account.avatar.error.storage_full':
'Storage is full. Try again later.',
'account.avatar.error.upload_missing':
'The upload did not complete. Try again.',
'account.avatar.error.invalid_state':
'This action no longer matches your current photo. Reload the page.',
'account.avatar.error.not_found': 'Photo not found. Reload the page.',
'account.avatar.error.network':
'Cannot reach the server. Check your connection and try again.',
'account.avatar.error.unknown': 'Something went wrong. Try again.',
'account.avatar.rejected.unsupported_type':
'Photo refused: the file is not a JPEG, PNG or WebP image.',
'account.avatar.rejected.decode_failed':
'Photo refused: the file is corrupt or unreadable.',
'account.avatar.rejected.dimensions':
'Photo refused: between 64 and 4096 pixels per side.',
'account.avatar.rejected.processing_error':
'Photo refused: processing failed. Try another image.',
}
+42
View File
@@ -390,4 +390,46 @@ export const fr: Dict = {
'Pas encore disponible dans cette version. Elle sera proposée, puis exigée pour les rôles plateforme.',
'account.security.sign_out_everywhere_note':
'Se déconnecter ferme la session sur cet appareil.',
'account.avatar.title': 'Photo de profil',
'account.avatar.alt': 'Photo de profil',
'account.avatar.change': 'Modifier la photo',
'account.avatar.save': 'Enregistrer la photo',
'account.avatar.saving': 'Envoi…',
'account.avatar.cancel': 'Annuler',
'account.avatar.remove': 'Supprimer la photo',
'account.avatar.remove_title': 'Supprimer la photo ?',
'account.avatar.remove_body':
'Votre photo de profil sera retirée. Vous pourrez en ajouter une autre à tout moment.',
'account.avatar.remove_confirm': 'Supprimer',
'account.avatar.removed': 'Photo supprimée.',
'account.avatar.processing': 'Traitement en cours…',
'account.avatar.processing_hint':
'Votre photo est vérifiée puis redimensionnée. Cela prend quelques secondes.',
'account.avatar.processing_long':
'Le traitement prend plus de temps que prévu. Revenez dans un instant.',
'account.avatar.saved': 'Photo enregistrée.',
'account.avatar.hint': 'JPEG, PNG ou WebP, 5 Mo maximum une fois réduite.',
'account.avatar.error.too_large': 'Image trop volumineuse (5 Mo maximum).',
'account.avatar.error.unsupported_type':
'Format non pris en charge : choisissez une image JPEG, PNG ou WebP.',
'account.avatar.error.rate_limited':
'Trop de tentatives. Réessayez dans une heure.',
'account.avatar.error.storage_full':
'Espace de stockage saturé. Réessayez plus tard.',
'account.avatar.error.upload_missing':
"L'envoi ne s'est pas terminé. Réessayez.",
'account.avatar.error.invalid_state':
'Cette opération ne correspond plus à votre photo actuelle. Rechargez la page.',
'account.avatar.error.not_found': 'Photo introuvable. Rechargez la page.',
'account.avatar.error.network':
"Impossible de joindre le serveur. Vérifiez votre connexion et réessayez.",
'account.avatar.error.unknown': 'Une erreur est survenue. Réessayez.',
'account.avatar.rejected.unsupported_type':
'Photo refusée : le fichier n’est pas une image JPEG, PNG ou WebP.',
'account.avatar.rejected.decode_failed':
'Photo refusée : le fichier est corrompu ou illisible.',
'account.avatar.rejected.dimensions':
'Photo refusée : entre 64 et 4096 pixels de côté.',
'account.avatar.rejected.processing_error':
'Photo refusée : le traitement a échoué. Réessayez avec une autre image.',
}
+77 -13
View File
@@ -10,6 +10,9 @@ import {
signOut as signOutRequest,
updateOwnProfile,
type AuthFailureKind,
type AvatarState,
downloadAvatar,
fetchOwnAvatarState,
type FaunosteoClient,
type OwnProfilePatch,
type PlatformRole,
@@ -34,22 +37,59 @@ export interface AuthState {
recovery: boolean
/** Failure while reading the URL (expired link, bad code): shown by the landing routes. */
urlFailure: AuthFailureKind | null
/** Own avatar rows reduced to clean / in flight / last rejected; null until loaded. */
avatar: AvatarState | null
/** Blob URL of the clean photo (downloaded under RLS), revoked on change or sign-out. */
avatarUrl: string | null
/** Subscribes to the client; returns the unsubscribe. Safe to call twice. */
start: () => () => void
refreshAccount: () => Promise<void>
updateProfile: (patch: OwnProfilePatch) => Promise<Profile>
/** Re-reads the own rows; downloads the clean photo when it changed. */
refreshAvatar: () => Promise<AvatarState>
/** Forgets the photo locally (after remove_avatar or on sign-out). */
clearAvatar: () => void
signOut: () => Promise<void>
clearRecovery: () => void
}
type Set = (patch: Partial<AuthState>) => void
type Get = () => AuthState
async function loadAccount(client: FaunosteoClient, user: User, set: Set) {
/** Blob URLs are a browser feature; tests under happy-dom may lack them. */
function revokeUrl(url: string | null) {
if (url && typeof URL.revokeObjectURL === 'function') URL.revokeObjectURL(url)
}
/**
* Reads the avatar state and keeps `avatarUrl` in step with the clean row:
* a new clean id downloads once, no clean row drops the URL.
*/
async function syncAvatar(client: FaunosteoClient, user: User, set: Set, get: Get) {
const avatar = await fetchOwnAvatarState(client, user.id)
const previous = get().avatar?.clean?.id ?? null
const current = avatar.clean?.id ?? null
if (current === previous && (current === null || get().avatarUrl)) {
set({ avatar })
return avatar
}
let avatarUrl: string | null = null
if (avatar.clean && typeof URL.createObjectURL === 'function') {
const blob = await downloadAvatar(client, avatar.clean.object_key)
avatarUrl = URL.createObjectURL(blob)
}
revokeUrl(get().avatarUrl)
set({ avatar, avatarUrl })
return avatar
}
async function loadAccount(client: FaunosteoClient, user: User, set: Set, get: Get) {
const [profile, roles] = await Promise.all([
fetchOwnProfile(client, user.id),
fetchOwnPlatformRoles(client, user.id),
])
set({ profile, roles })
await syncAvatar(client, user, set, get).catch(() => set({ avatar: null }))
}
/**
@@ -61,9 +101,11 @@ function applyAuthEvent(
event: AuthChangeEvent,
session: Session | null,
set: Set,
previousUserId: string | undefined,
get: Get,
) {
const previousUserId = get().user?.id
if (event === 'SIGNED_OUT' || session === null) {
revokeUrl(get().avatarUrl)
set({
status: 'signed_out',
session: null,
@@ -71,6 +113,8 @@ function applyAuthEvent(
profile: null,
roles: [],
recovery: false,
avatar: null,
avatarUrl: null,
})
return
}
@@ -83,7 +127,7 @@ function applyAuthEvent(
set(patch)
if (session.user.id !== previousUserId || event === 'USER_UPDATED') {
setTimeout(() => {
void loadAccount(client, session.user, set).catch(() => {
void loadAccount(client, session.user, set, get).catch(() => {
set({ profile: null, roles: [] })
})
}, 0)
@@ -109,6 +153,8 @@ export const useAuthStore = create<AuthState>((set, get) => ({
roles: [],
recovery: false,
urlFailure: null,
avatar: null,
avatarUrl: null,
start: () => {
const client = getSupabaseClient()
@@ -120,13 +166,26 @@ export const useAuthStore = create<AuthState>((set, get) => ({
if (error) set({ urlFailure: classifyAuthError(error) })
})
const { data } = client.auth.onAuthStateChange((event, session) => {
applyAuthEvent(client, event, session, set, get().user?.id)
applyAuthEvent(client, event, session, set, get)
})
return () => data.subscription.unsubscribe()
},
refreshAccount: async () => {
await loadAccount(requireClient(), requireUser(get().user), set)
await loadAccount(requireClient(), requireUser(get().user), set, get)
},
refreshAvatar: async () => {
return syncAvatar(requireClient(), requireUser(get().user), set, get)
},
clearAvatar: () => {
revokeUrl(get().avatarUrl)
const avatar = get().avatar
set({
avatarUrl: null,
avatar: avatar ? { ...avatar, clean: null, inFlight: null } : null,
})
},
updateProfile: async (patch) => {
@@ -137,14 +196,19 @@ export const useAuthStore = create<AuthState>((set, get) => ({
},
signOut: async () => {
const clear = () => set({
status: 'signed_out',
session: null,
user: null,
profile: null,
roles: [],
recovery: false,
})
const clear = () => {
revokeUrl(get().avatarUrl)
set({
status: 'signed_out',
session: null,
user: null,
profile: null,
roles: [],
recovery: false,
avatar: null,
avatarUrl: null,
})
}
try {
// auth-js clears the local session even when the server call fails.
await signOutRequest(requireClient())
+74 -5
View File
@@ -1,10 +1,12 @@
/**
* In-memory stand-in for the parts of the Supabase client the identity step
* uses: auth events, profile select/update, profile_roles select. Tests mock
* In-memory stand-in for the parts of the Supabase client the identity and
* files steps use: auth events, profile select/update, profile_roles and
* assets selects, rpc, storage upload / download. Tests mock
* `@/lib/supabase` to hand this object out.
*/
import { vi } from 'vitest'
import type {
AssetRow,
AuthChangeEvent,
FaunosteoClient,
Profile,
@@ -58,11 +60,42 @@ export function fakeProfile(patch: Partial<Profile> = {}): Profile {
}
}
/** Minimal PostgREST builder: filters, then select / update, awaitable. */
export function fakeAssetRow(patch: Partial<AssetRow> = {}): AssetRow {
return {
id: 'asset-1',
owner_id: 'user-1',
kind: 'profile_avatar',
bucket_id: 'avatars',
object_key: 'profiles/user-1/asset-1/avatar.webp',
status: 'clean',
declared_mime: 'image/jpeg',
declared_size: 1000,
byte_size: 900,
sha256: null,
rejection_reason: null,
created_at: '2026-10-13T10:00:00Z',
updated_at: '2026-10-13T10:00:00Z',
...patch,
}
}
/** Minimal PostgREST builder: filters, order, limit, then select / update, awaitable. */
function builder(tables: Record<string, Row[]>, table: string) {
const filters: Array<(row: Row) => boolean> = []
let patch: Row | null = null
const rows = () => (tables[table] ?? []).filter((r) => filters.every((f) => f(r)))
let order: { col: string; ascending: boolean } | null = null
let max: number | null = null
const rows = () => {
let hit = (tables[table] ?? []).filter((r) => filters.every((f) => f(r)))
if (order) {
const { col, ascending } = order
hit = [...hit].sort((a, b) =>
(String(a[col]) < String(b[col]) ? -1 : String(a[col]) > String(b[col]) ? 1 : 0) *
(ascending ? 1 : -1),
)
}
return max === null ? hit : hit.slice(0, max)
}
const apply = () => {
if (!patch) return rows()
const hit = rows()
@@ -83,6 +116,18 @@ function builder(tables: Record<string, Row[]>, table: string) {
filters.push((r) => r[col] === value)
return api
},
in: (col: string, values: unknown[]) => {
filters.push((r) => values.includes(r[col]))
return api
},
order: (col: string, opts?: { ascending?: boolean }) => {
order = { col, ascending: opts?.ascending ?? true }
return api
},
limit: (n: number) => {
max = n
return api
},
maybeSingle: async () => ({ data: apply()[0] ?? null, error: null }),
single: async () => {
const [first] = apply()
@@ -100,6 +145,13 @@ export interface FakeSupabase {
client: FaunosteoClient
tables: Record<string, Row[]>
emit: (event: AuthChangeEvent, session: Session | null) => void
/** Recorded rpc calls; program results with `rpcResults[name]`. */
rpcCalls: Array<[string, unknown]>
rpcResults: Record<string, { data?: unknown; error?: unknown } | (() => { data?: unknown; error?: unknown })>
storage: {
upload: ReturnType<typeof vi.fn>
download: ReturnType<typeof vi.fn>
}
auth: {
initialize: ReturnType<typeof vi.fn>
signInWithPassword: ReturnType<typeof vi.fn>
@@ -112,9 +164,16 @@ export interface FakeSupabase {
}
export function createFakeSupabase(
tables: Record<string, Row[]> = { profiles: [fakeProfile()], profile_roles: [] },
tables: Record<string, Row[]> = { profiles: [fakeProfile()], profile_roles: [], assets: [] },
): FakeSupabase {
tables.assets ??= []
const listeners: Listener[] = []
const rpcCalls: Array<[string, unknown]> = []
const rpcResults: FakeSupabase['rpcResults'] = {}
const storage = {
upload: vi.fn(async () => ({ data: null, error: null })),
download: vi.fn(async () => ({ data: new Blob([new Uint8Array([82, 73, 70, 70])]), error: null })),
}
const auth = {
initialize: vi.fn(async () => ({ error: null })),
onAuthStateChange: vi.fn((cb: Listener) => {
@@ -131,11 +190,21 @@ export function createFakeSupabase(
const client = {
auth,
from: (table: string) => builder(tables, table),
rpc: async (name: string, args?: unknown) => {
rpcCalls.push([name, args])
const entry = rpcResults[name]
const result = typeof entry === 'function' ? entry() : (entry ?? {})
return { data: result.data ?? null, error: result.error ?? null }
},
storage: { from: () => storage },
} as unknown as FaunosteoClient
return {
client,
tables,
auth,
rpcCalls,
rpcResults,
storage,
emit: (event, session) => listeners.forEach((cb) => cb(event, session)),
}
}
@@ -0,0 +1,120 @@
import { describe, expect, it, vi } from 'vitest'
import { AssetFailure } from '@faunosteo/supabase'
import {
MAX_PICKED_BYTES,
prepareAvatarUpload,
sniffImageType,
transportSize,
type TransportDeps,
} from '@/account/avatarTransport'
const JPEG = new Uint8Array([0xff, 0xd8, 0xff, 0xe0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
const PNG = new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0])
const WEBP = new Uint8Array([...'RIFF'].map((c) => c.charCodeAt(0)).concat([0, 0, 0, 0], [...'WEBP'].map((c) => c.charCodeAt(0)), [0, 0, 0, 0]))
function file(bytes: Uint8Array, name = 'photo.jpg', type = 'image/jpeg', size?: number): File {
const f = new File([bytes as BlobPart], name, { type })
if (size !== undefined) Object.defineProperty(f, 'size', { value: size })
return f
}
function deps(bitmap: { width: number; height: number } | Error, blobSize = 1000): TransportDeps & { canvas: { width: number; height: number }; drawn: unknown[] } {
const canvas = { width: 0, height: 0 }
const drawn: unknown[] = []
return {
canvas,
drawn,
createImageBitmap: async () => {
if (bitmap instanceof Error) throw bitmap
return { ...bitmap, close: vi.fn() }
},
createCanvas: () => ({
get width() {
return canvas.width
},
set width(v: number) {
canvas.width = v
},
get height() {
return canvas.height
},
set height(v: number) {
canvas.height = v
},
getContext: () => ({ drawImage: (...args: unknown[]) => drawn.push(args) }),
toBlob: (cb: (b: Blob | null) => void) => {
const blob = new Blob([new Uint8Array(blobSize)], { type: 'image/jpeg' })
cb(blob)
},
}),
createObjectURL: () => 'blob:preview',
}
}
async function failureKind(p: Promise<unknown>) {
try {
await p
return null
} catch (e) {
return e instanceof AssetFailure ? e.kind : String(e)
}
}
describe('sniffImageType', () => {
it('recognises JPEG, PNG and WebP by magic bytes only', () => {
expect(sniffImageType(JPEG)).toBe('image/jpeg')
expect(sniffImageType(PNG)).toBe('image/png')
expect(sniffImageType(WEBP)).toBe('image/webp')
})
it('refuses SVG, GIF, a RIFF that is not WEBP and empty input', () => {
const svg = new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"/>')
expect(sniffImageType(svg)).toBeNull()
expect(sniffImageType(new TextEncoder().encode('GIF89a......'))).toBeNull()
expect(sniffImageType(new TextEncoder().encode('RIFF....WAVE....'))).toBeNull()
expect(sniffImageType(new Uint8Array())).toBeNull()
})
})
describe('transportSize', () => {
it('caps the longest side at 1024 and never upscales', () => {
expect(transportSize(4000, 3000)).toEqual({ width: 1024, height: 768 })
expect(transportSize(3000, 4000)).toEqual({ width: 768, height: 1024 })
expect(transportSize(500, 500)).toEqual({ width: 500, height: 500 })
expect(transportSize(1024, 10)).toEqual({ width: 1024, height: 10 })
})
})
describe('prepareAvatarUpload', () => {
it('refuses an oversized file before any decode', async () => {
const d = deps({ width: 10, height: 10 })
const spy = vi.spyOn(d, 'createImageBitmap')
expect(await failureKind(prepareAvatarUpload(file(JPEG, 'big.jpg', 'image/jpeg', MAX_PICKED_BYTES + 1), d))).toBe('too_large')
expect(spy).not.toHaveBeenCalled()
})
it('refuses a file whose bytes are not a raster image, whatever its name or type', async () => {
const d = deps({ width: 10, height: 10 })
const svg = new TextEncoder().encode('<svg xmlns="http://www.w3.org/2000/svg"></svg>')
expect(await failureKind(prepareAvatarUpload(file(svg, 'logo.jpg', 'image/jpeg'), d))).toBe('unsupported_type')
})
it('maps a bitmap decode failure to unsupported_type', async () => {
const d = deps(new Error('not decodable'))
expect(await failureKind(prepareAvatarUpload(file(JPEG), d))).toBe('unsupported_type')
})
it('draws a downscaled canvas and returns a JPEG blob with a preview URL', async () => {
const d = deps({ width: 4000, height: 3000 })
const result = await prepareAvatarUpload(file(PNG, 'p.png', 'image/png'), d)
expect(d.canvas).toEqual({ width: 1024, height: 768 })
expect(d.drawn[0]).toEqual([expect.objectContaining({ width: 4000 }), 0, 0, 1024, 768])
expect(result.blob.type).toBe('image/jpeg')
expect(result.previewUrl).toBe('blob:preview')
})
it('keeps a small image at its size', async () => {
const d = deps({ width: 500, height: 500 })
await prepareAvatarUpload(file(JPEG), d)
expect(d.canvas).toEqual({ width: 500, height: 500 })
})
it('refuses a transport blob over 5 MiB', async () => {
const d = deps({ width: 1000, height: 1000 }, 5 * 1024 * 1024 + 1)
expect(await failureKind(prepareAvatarUpload(file(JPEG), d))).toBe('too_large')
})
})
@@ -0,0 +1,172 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { act, cleanup, fireEvent, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { AssetFailure } from '@faunosteo/supabase'
import {
createFakeSupabase,
fakeAssetRow,
fakeProfile,
fakeUser,
type FakeSupabase,
} from '../helpers/fakeSupabase'
const mocks = vi.hoisted(() => ({
fake: null as unknown,
prepare: vi.fn(),
}))
vi.mock('@/lib/supabase', () => ({
getSupabaseClient: () => (mocks.fake as FakeSupabase | null)?.client ?? null,
isSupabaseConfigured: () => mocks.fake !== null,
}))
vi.mock('@/account/avatarTransport', () => ({
prepareAvatarUpload: (...args: unknown[]) => mocks.prepare(...args),
}))
import AvatarSection, { POLL_INTERVAL_MS } from '@/components/account/AvatarSection'
import { useAuthStore } from '@/state/authStore'
import { useLocaleStore } from '@/state/localeStore'
import { fr } from '@/i18n/fr'
let fake: FakeSupabase
const TICKET = {
asset_id: '11111111-1111-4111-8111-111111111111',
bucket_id: 'avatars',
quarantine_key: 'quarantine/user-1/11111111-1111-4111-8111-111111111111/upload',
}
beforeEach(() => {
useLocaleStore.setState({ locale: 'fr' })
fake = createFakeSupabase()
mocks.fake = fake
mocks.prepare.mockReset()
useAuthStore.setState({
status: 'signed_in',
user: fakeUser(),
profile: fakeProfile(),
roles: [],
avatar: { clean: null, inFlight: null, lastRejected: null },
avatarUrl: null,
})
fake.rpcResults.begin_avatar_upload = { data: TICKET }
})
afterEach(() => {
cleanup()
vi.useRealTimers()
})
function pick(file = new File([new Uint8Array([0xff, 0xd8, 0xff])], 'p.jpg', { type: 'image/jpeg' })) {
const input = screen.getByLabelText(fr['account.avatar.change'])
fireEvent.change(input, { target: { files: [file] } })
}
describe('AvatarSection', () => {
it('shows the monogram and no remove button without a photo', () => {
render(<AvatarSection />)
expect(screen.getByText('AL')).toBeInTheDocument()
expect(screen.queryByRole('button', { name: fr['account.avatar.remove'] })).toBeNull()
})
it('refuses a bad pick with the phrased message and makes no request', async () => {
mocks.prepare.mockRejectedValue(new AssetFailure('unsupported_type'))
render(<AvatarSection />)
pick()
expect(await screen.findByText(fr['account.avatar.error.unsupported_type'])).toBeInTheDocument()
expect(fake.rpcCalls).toEqual([])
expect(fake.storage.upload).not.toHaveBeenCalled()
})
it('shows the preview with save / cancel before any request', async () => {
mocks.prepare.mockResolvedValue({ blob: new Blob(['x'], { type: 'image/jpeg' }), previewUrl: 'blob:preview' })
render(<AvatarSection />)
pick()
const img = await screen.findByRole('img', { name: fr['account.avatar.alt'] })
expect(img).toHaveAttribute('src', 'blob:preview')
expect(screen.getByRole('button', { name: fr['account.avatar.save'] })).toBeInTheDocument()
expect(fake.rpcCalls).toEqual([])
await userEvent.click(screen.getByRole('button', { name: fr['account.avatar.cancel'] }))
expect(screen.queryByRole('img')).toBeNull()
})
it('saves in order begin → upload → confirm, polls, then shows the photo', async () => {
const blob = new Blob([new Uint8Array(1234)], { type: 'image/jpeg' })
mocks.prepare.mockResolvedValue({ blob, previewUrl: 'blob:preview' })
render(<AvatarSection />)
pick()
await screen.findByRole('button', { name: fr['account.avatar.save'] })
vi.useFakeTimers({ shouldAdvanceTime: true })
await act(async () => {
fireEvent.click(screen.getByRole('button', { name: fr['account.avatar.save'] }))
})
await waitFor(() => expect(fake.rpcCalls.map((c) => c[0])).toEqual(['begin_avatar_upload', 'confirm_asset_upload']))
expect(fake.rpcCalls[0][1]).toEqual({ declared_mime: 'image/jpeg', declared_size: 1234 })
expect(fake.storage.upload).toHaveBeenCalledWith(TICKET.quarantine_key, blob, { upsert: false, contentType: 'image/jpeg' })
expect(fake.rpcCalls[1][1]).toEqual({ asset_id: TICKET.asset_id })
expect(screen.getByText(fr['account.avatar.processing'])).toBeInTheDocument()
expect(screen.getByRole('button', { name: fr['account.avatar.change'] })).toBeDisabled()
// The worker finishes: the next poll sees the clean row and downloads it.
fake.tables.assets.push(fakeAssetRow({ id: TICKET.asset_id, created_at: '2026-10-13T11:00:00Z' }))
await act(async () => {
await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS + 50)
})
await waitFor(() => expect(screen.getByText(fr['account.avatar.saved'])).toBeInTheDocument())
expect(fake.storage.download).toHaveBeenCalledWith('profiles/user-1/asset-1/avatar.webp')
expect(screen.getByRole('button', { name: fr['account.avatar.remove'] })).toBeInTheDocument()
})
it('phrases a rejection per code once the poll settles', async () => {
mocks.prepare.mockResolvedValue({ blob: new Blob(['x']), previewUrl: 'blob:p' })
render(<AvatarSection />)
pick()
await screen.findByRole('button', { name: fr['account.avatar.save'] })
vi.useFakeTimers({ shouldAdvanceTime: true })
await act(async () => {
fireEvent.click(screen.getByRole('button', { name: fr['account.avatar.save'] }))
})
await waitFor(() => expect(fake.rpcCalls.length).toBe(2))
fake.tables.assets.push(fakeAssetRow({ id: TICKET.asset_id, status: 'rejected', rejection_reason: 'dimensions', created_at: '2026-10-13T11:00:00Z' }))
await act(async () => {
await vi.advanceTimersByTimeAsync(POLL_INTERVAL_MS + 50)
})
await waitFor(() => expect(screen.getByText(fr['account.avatar.rejected.dimensions'])).toBeInTheDocument())
})
it('phrases an RPC refusal and keeps the circle usable', async () => {
mocks.prepare.mockResolvedValue({ blob: new Blob(['x']), previewUrl: 'blob:p' })
fake.rpcResults.begin_avatar_upload = { error: { code: 'P0001', message: 'rate_limited', status: 400 } }
render(<AvatarSection />)
pick()
await userEvent.click(await screen.findByRole('button', { name: fr['account.avatar.save'] }))
expect(await screen.findByText(fr['account.avatar.error.rate_limited'])).toBeInTheDocument()
expect(fake.storage.upload).not.toHaveBeenCalled()
expect(screen.getByRole('button', { name: fr['account.avatar.change'] })).toBeEnabled()
})
it('removes the photo after the dialog confirmation', async () => {
useAuthStore.setState({ avatarUrl: 'blob:current', avatar: { clean: fakeAssetRow(), inFlight: null, lastRejected: null } })
fake.rpcResults.remove_avatar = { data: 1 }
render(<AvatarSection />)
expect(screen.getByRole('img', { name: fr['account.avatar.alt'] })).toHaveAttribute('src', 'blob:current')
await userEvent.click(screen.getByRole('button', { name: fr['account.avatar.remove'] }))
const dialog = await screen.findByRole('dialog', { name: fr['account.avatar.remove_title'] })
expect(dialog).toBeInTheDocument()
await userEvent.click(screen.getByRole('button', { name: fr['account.avatar.remove_confirm'] }))
expect(await screen.findByText(fr['account.avatar.removed'])).toBeInTheDocument()
expect(fake.rpcCalls).toEqual([['remove_avatar', undefined]])
expect(useAuthStore.getState().avatarUrl).toBeNull()
expect(screen.queryByRole('img')).toBeNull()
})
it('resumes the processing state for a row in flight at mount', async () => {
useAuthStore.setState({ avatar: { clean: null, inFlight: fakeAssetRow({ status: 'uploaded' }), lastRejected: null } })
render(<AvatarSection />)
expect(screen.getByText(fr['account.avatar.processing'])).toBeInTheDocument()
expect(screen.getByRole('button', { name: fr['account.avatar.change'] })).toBeDisabled()
})
it('shows the last rejection of a fresh mount', () => {
useAuthStore.setState({ avatar: { clean: null, inFlight: null, lastRejected: fakeAssetRow({ status: 'rejected', rejection_reason: 'decode_failed' }) } })
render(<AvatarSection />)
expect(screen.getByText(fr['account.avatar.rejected.decode_failed'])).toBeInTheDocument()
})
})
+29 -2
View File
@@ -1,5 +1,5 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { cleanup, render, screen } from '@testing-library/react'
import { cleanup, render, screen, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import { createFakeSupabase, fakeProfile, fakeUser, type FakeSupabase } from '../helpers/fakeSupabase'
@@ -27,6 +27,8 @@ beforeEach(() => {
user: fakeUser(),
profile: fakeProfile({ display_name: 'Alice Local' }),
roles: [],
avatar: null,
avatarUrl: null,
})
})
afterEach(cleanup)
@@ -56,9 +58,34 @@ describe('AppLayout', () => {
expect(screen.getByText('dashboard page')).toBeInTheDocument()
})
it('links the top-bar avatar and name to the account page', async () => {
const user = userEvent.setup()
mount()
const link = screen.getByTestId('topbar-account')
expect(link).toHaveAttribute('href', '/app/account')
expect(link).toHaveTextContent('Alice Local')
expect(link).toHaveAttribute('aria-label', fr['shell.nav.account'])
await user.click(link)
expect(await screen.findByText('account page')).toBeInTheDocument()
})
it('shows the photo everywhere once the store holds a blob URL, the monogram otherwise', () => {
mount()
expect(screen.queryAllByRole('img', { name: fr['account.avatar.alt'] })).toHaveLength(0)
expect(screen.getAllByText('AL').length).toBeGreaterThan(0)
cleanup()
useAuthStore.setState({ avatarUrl: 'blob:photo' })
mount()
const imgs = screen.getAllByRole('img', { name: fr['account.avatar.alt'] })
expect(imgs.length).toBeGreaterThanOrEqual(2)
imgs.forEach((img) => expect(img).toHaveAttribute('src', 'blob:photo'))
expect(screen.queryByText('AL')).toBeNull()
})
it('marks the active destination', () => {
mount('/app/account')
const link = screen.getByRole('link', { name: new RegExp(fr['shell.nav.account']) })
const nav = screen.getByRole('navigation', { name: fr['shell.nav_label'] })
const link = within(nav).getByRole('link', { name: new RegExp(fr['shell.nav.account']) })
expect(link).toHaveAttribute('aria-current', 'page')
})
@@ -1,6 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import {
createFakeSupabase,
fakeAssetRow,
fakeSession,
flush,
type FakeSupabase,
@@ -22,6 +23,8 @@ const INITIAL = {
roles: [],
recovery: false,
urlFailure: null,
avatar: null,
avatarUrl: null,
}
let fake: FakeSupabase
@@ -97,6 +100,38 @@ describe('authStore', () => {
expect(fake.tables.profiles[0].first_name).toBe('Alice')
})
it('loads the avatar state and downloads the clean photo once', async () => {
fake.tables.assets.push(fakeAssetRow())
stop = useAuthStore.getState().start()
fake.emit('SIGNED_IN', fakeSession())
await flush()
await flush()
const s = useAuthStore.getState()
expect(s.avatar?.clean?.id).toBe('asset-1')
expect(s.avatarUrl).toMatch(/^blob:/)
expect(fake.storage.download).toHaveBeenCalledTimes(1)
await useAuthStore.getState().refreshAvatar()
expect(fake.storage.download).toHaveBeenCalledTimes(1)
fake.tables.assets.push(fakeAssetRow({ id: 'asset-2', object_key: 'profiles/user-1/asset-2/avatar.webp', created_at: '2026-10-13T12:00:00Z' }))
fake.tables.assets[0].status = 'deleted'
await useAuthStore.getState().refreshAvatar()
expect(fake.storage.download).toHaveBeenLastCalledWith('profiles/user-1/asset-2/avatar.webp')
})
it('clearAvatar drops the URL and the clean row; sign-out clears it too', async () => {
fake.tables.assets.push(fakeAssetRow())
stop = useAuthStore.getState().start()
fake.emit('SIGNED_IN', fakeSession())
await flush()
await flush()
useAuthStore.getState().clearAvatar()
expect(useAuthStore.getState().avatarUrl).toBeNull()
expect(useAuthStore.getState().avatar?.clean).toBeNull()
await useAuthStore.getState().refreshAvatar()
fake.emit('SIGNED_OUT', null)
expect(useAuthStore.getState()).toMatchObject({ avatar: null, avatarUrl: null })
})
it('signOut calls the client and resets the state', async () => {
stop = useAuthStore.getState().start()
fake.emit('SIGNED_IN', fakeSession())
+5 -1
View File
@@ -1,6 +1,6 @@
[package]
name = "faunosteo-worker"
description = "Faunosteo file-processing worker: job loop (R1) and the sandboxed decode child (R0 proof)"
description = "Faunosteo file-processing worker: keyed job loop and sweeper (R1), sandboxed decode child (R0)"
version.workspace = true
edition.workspace = true
license.workspace = true
@@ -26,6 +26,10 @@ libwebp-sys.workspace = true
seccompiler.workspace = true
rlimit.workspace = true
libc.workspace = true
serde.workspace = true
serde_json.workspace = true
ureq.workspace = true
sha2.workspace = true
crc32fast = { version = "1.4", optional = true }
# Already in the tree through png: used by the iCCP-bomb fixture only.
flate2 = { version = "1.1", optional = true }
+107
View File
@@ -0,0 +1,107 @@
//! What the parent needs from Supabase (STORAGE.md §2.2 RPCs and §3.3 Storage calls),
//! behind one trait so the job and sweep logic is tested on an in-memory fake and the HTTP
//! implementation stays thin.
use std::fmt;
use serde::Deserialize;
/// One claimed row of `assets` (`claim_avatar_jobs`).
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Job {
pub asset_id: String,
pub owner_id: String,
pub bucket_id: String,
pub object_key: String,
pub quarantine_key: Option<String>,
pub lock_token: String,
pub attempts: i64,
}
/// One claimed purge (`claim_purges`).
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Purge {
pub purge_id: String,
pub bucket_id: String,
pub object_name: String,
}
/// One quarantine object without a live row (`claim_quarantine_leftovers`).
#[derive(Debug, Clone, Deserialize, PartialEq, Eq)]
pub struct Leftover {
pub bucket_id: String,
pub object_name: String,
pub asset_id: Option<String>,
}
/// Closed failure kinds of a backend call. `Refused` carries the closed code a `P0001`
/// refusal names (`invalid_state`, `invalid_reason`…); every other kind is transport-side.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum BackendError {
Network(String),
Status(u16),
Refused(String),
Decode(String),
/// The downloaded body exceeded the input cap.
Overflow,
Internal(String),
}
impl fmt::Display for BackendError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Network(detail) => write!(f, "network: {detail}"),
Self::Status(status) => write!(f, "http status {status}"),
Self::Refused(code) => write!(f, "refused: {code}"),
Self::Decode(detail) => write!(f, "decode: {detail}"),
Self::Overflow => f.write_str("body over the cap"),
Self::Internal(detail) => write!(f, "internal: {detail}"),
}
}
}
impl std::error::Error for BackendError {}
impl BackendError {
/// Stable name for the `code` log field.
pub fn kind(&self) -> &'static str {
match self {
Self::Network(_) => "network",
Self::Status(_) => "status",
Self::Refused(_) => "refused",
Self::Decode(_) => "decode",
Self::Overflow => "overflow",
Self::Internal(_) => "internal",
}
}
}
pub type BackendResult<T> = Result<T, BackendError>;
/// The eleven operations of STORAGE.md §2.2 and §3.3.
pub trait Backend {
fn claim_jobs(&self, max_jobs: u32) -> BackendResult<Vec<Job>>;
/// `Status(404)` when the object never landed; `Overflow` past the input cap.
fn download_quarantine(&self, bucket: &str, key: &str) -> BackendResult<Vec<u8>>;
/// Upsert of the clean bytes at the final key (idempotent on retry).
fn put_clean(&self, bucket: &str, key: &str, bytes: &[u8]) -> BackendResult<()>;
fn complete(
&self,
asset_id: &str,
lock_token: &str,
byte_size: usize,
sha256: &str,
) -> BackendResult<()>;
fn reject(&self, asset_id: &str, lock_token: &str, reason: &str) -> BackendResult<()>;
/// A missing object counts as deleted.
fn delete_object(&self, bucket: &str, key: &str) -> BackendResult<()>;
fn mark_quarantine_purged(&self, asset_id: &str) -> BackendResult<()>;
fn claim_purges(&self, max_items: u32) -> BackendResult<Vec<Purge>>;
fn ack_purge(&self, purge_id: &str) -> BackendResult<()>;
fn claim_quarantine_leftovers(
&self,
older_than_seconds: u64,
max_items: u32,
) -> BackendResult<Vec<Leftover>>;
fn expire_pending_uploads(&self, max_age_seconds: u64) -> BackendResult<i64>;
}
+332
View File
@@ -0,0 +1,332 @@
//! Configuration of the keyed parent (STORAGE.md §3.2): read from the environment once at
//! start, never from the child. The service key is a `Secret` that no `Debug` or `Display`
//! output can print.
use std::fmt;
use std::time::Duration;
/// A value that must never appear in a log line or an error message.
#[derive(Clone)]
pub struct Secret(String);
impl Secret {
pub fn new(value: String) -> Self {
Self(value)
}
/// The only accessor; call sites are the HTTP headers and the JWT expiry parser.
pub fn expose(&self) -> &str {
&self.0
}
}
impl fmt::Debug for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<redacted>")
}
}
/// Resolved worker configuration.
#[derive(Debug, Clone)]
pub struct Config {
/// Origin of the Supabase stack, without a trailing slash.
pub url: String,
pub service_key: Secret,
pub worker_id: String,
pub poll: Duration,
pub sweep: Duration,
pub quarantine_grace: Duration,
pub pending_max_age: Duration,
/// One claim pass and one sweep, then exit (tests).
pub once: bool,
}
/// Why the configuration could not be built (the variable name, never its value).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ConfigError {
Missing(&'static str),
Invalid(&'static str),
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Missing(name) => write!(f, "{name} is required"),
Self::Invalid(name) => write!(f, "{name} is invalid"),
}
}
}
impl std::error::Error for ConfigError {}
const URL: &str = "SUPABASE_URL";
const KEY: &str = "SUPABASE_SERVICE_ROLE_KEY";
const WORKER_ID: &str = "WORKER_ID";
const POLL: &str = "WORKER_POLL_SECONDS";
const SWEEP: &str = "WORKER_SWEEP_SECONDS";
const QUARANTINE_GRACE: &str = "WORKER_QUARANTINE_GRACE_SECONDS";
const PENDING_MAX_AGE: &str = "WORKER_PENDING_MAX_AGE_SECONDS";
const ONCE: &str = "WORKER_ONCE";
/// Names of the variables the worker reads, for `run --check`.
pub const VARIABLES: &[&str] = &[
URL,
KEY,
WORKER_ID,
POLL,
SWEEP,
QUARANTINE_GRACE,
PENDING_MAX_AGE,
ONCE,
];
impl Config {
/// Builds the configuration from the process environment.
pub fn from_env() -> Result<Self, ConfigError> {
Self::from_vars(|name| std::env::var(name).ok())
}
/// Builds the configuration from any variable source (tests).
pub fn from_vars(var: impl Fn(&str) -> Option<String>) -> Result<Self, ConfigError> {
let url = parse_url(var(URL).ok_or(ConfigError::Missing(URL))?)?;
let key = var(KEY).filter(|k| !k.trim().is_empty());
let service_key = Secret::new(key.ok_or(ConfigError::Missing(KEY))?);
let worker_id = match var(WORKER_ID).filter(|id| !id.trim().is_empty()) {
Some(id)
if id.len() <= 64
&& id
.chars()
.all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_') =>
{
id
}
Some(_) => return Err(ConfigError::Invalid(WORKER_ID)),
None => format!("worker-{}", std::process::id()),
};
Ok(Self {
url,
service_key,
worker_id,
poll: seconds(&var, POLL, 2)?,
sweep: seconds(&var, SWEEP, 600)?,
quarantine_grace: seconds(&var, QUARANTINE_GRACE, 10_800)?,
pending_max_age: seconds(&var, PENDING_MAX_AGE, 86_400)?,
once: var(ONCE).is_some_and(|v| v == "1"),
})
}
}
/// `http(s)://host[:port]`, no path, no whitespace; a trailing slash is dropped.
/// Cleartext `http://` is accepted only where the key cannot leave the machine:
/// loopback, or a single-label host (a Docker service name on the compose network).
fn parse_url(raw: String) -> Result<String, ConfigError> {
let url = raw.trim().trim_end_matches('/').to_owned();
let (secure, rest) = match (url.strip_prefix("https://"), url.strip_prefix("http://")) {
(Some(rest), _) => (true, rest),
(None, Some(rest)) => (false, rest),
(None, None) => return Err(ConfigError::Invalid(URL)),
};
let host_ok = !rest.is_empty()
&& rest
.chars()
.all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '-' | ':' | '_'));
if !host_ok || (!secure && !cleartext_allowed(rest)) {
return Err(ConfigError::Invalid(URL));
}
Ok(url)
}
/// `localhost`, a dotted-quad loopback address, or a single-label name that is not a
/// number (a Docker service name on the compose network) may use plain HTTP. Numeric
/// forms (`167772165`, `0x0a000005`) resolve to arbitrary addresses and are refused.
fn cleartext_allowed(host_port: &str) -> bool {
let host = host_port.rsplit_once(':').map_or(host_port, |(h, _)| h);
if host == "localhost" {
return true;
}
if let Ok(address) = host.parse::<std::net::Ipv4Addr>() {
return address.is_loopback();
}
// A service name starts with a letter: no inet_aton or WHATWG numeric form does
// (`167772165`, `0x0a000005`, `0X0A000005` all resolve to an address).
!host.contains('.') && host.chars().next().is_some_and(|c| c.is_ascii_alphabetic())
}
fn seconds(
var: &impl Fn(&str) -> Option<String>,
name: &'static str,
default: u64,
) -> Result<Duration, ConfigError> {
match var(name) {
None => Ok(Duration::from_secs(default)),
Some(raw) => raw
.trim()
.parse::<u64>()
.map(Duration::from_secs)
.map_err(|_| ConfigError::Invalid(name)),
}
}
/// `exp` claim of a JWT (three base64url parts), when the key is one. A `sb_secret_…`
/// key is not a JWT and yields `None`.
pub fn jwt_exp(token: &str) -> Option<i64> {
let mut parts = token.split('.');
let (_, payload, _) = (parts.next()?, parts.next()?, parts.next()?);
if parts.next().is_some() {
return None;
}
let json = base64url_decode(payload)?;
let value: serde_json::Value = serde_json::from_slice(&json).ok()?;
value.get("exp")?.as_i64()
}
/// Base64url without padding (RFC 7515); padding characters are tolerated.
pub fn base64url_decode(text: &str) -> Option<Vec<u8>> {
let mut out = Vec::with_capacity(text.len() * 3 / 4);
let mut buffer: u32 = 0;
let mut bits = 0;
for byte in text.bytes() {
let value = match byte {
b'A'..=b'Z' => byte - b'A',
b'a'..=b'z' => byte - b'a' + 26,
b'0'..=b'9' => byte - b'0' + 52,
b'-' => 62,
b'_' => 63,
b'=' => break,
_ => return None,
};
buffer = (buffer << 6) | u32::from(value);
bits += 6;
if bits >= 8 {
bits -= 8;
out.push(u8::try_from((buffer >> bits) & 0xff).ok()?);
}
}
Some(out)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
use std::collections::HashMap;
fn vars(pairs: &[(&str, &str)]) -> impl Fn(&str) -> Option<String> {
let map: HashMap<String, String> = pairs
.iter()
.map(|(k, v)| ((*k).to_owned(), (*v).to_owned()))
.collect();
move |name| map.get(name).cloned()
}
#[test]
fn cleartext_http_is_limited_to_loopback_and_service_names() {
for ok in [
"http://127.0.0.1:54321",
"http://localhost:54321",
"http://supabase_kong_faunosteo:8000",
"http://kong",
"https://abc.supabase.co",
] {
assert!(parse_url(ok.to_owned()).is_ok(), "{ok}");
}
for bad in [
"http://abc.supabase.co",
"http://10.0.0.5:8000",
"http://127.evil.example",
"http://167772165:8000",
"http://0x0a000005",
"http://0X0A000005:8000",
"http://127.0.0.1.example",
"ftp://x",
"http://",
] {
assert_eq!(
parse_url(bad.to_owned()).err(),
Some(ConfigError::Invalid(URL)),
"{bad}"
);
}
}
#[test]
fn defaults_apply_when_only_the_required_variables_are_set() {
let cfg = Config::from_vars(vars(&[
("SUPABASE_URL", "http://127.0.0.1:54321/"),
("SUPABASE_SERVICE_ROLE_KEY", "k"),
]))
.unwrap();
assert_eq!(cfg.url, "http://127.0.0.1:54321");
assert_eq!(cfg.poll, Duration::from_secs(2));
assert_eq!(cfg.sweep, Duration::from_secs(600));
assert_eq!(cfg.quarantine_grace, Duration::from_secs(10_800));
assert_eq!(cfg.pending_max_age, Duration::from_secs(86_400));
assert!(!cfg.once);
assert!(cfg.worker_id.starts_with("worker-"));
}
#[test]
fn missing_and_invalid_variables_are_named() {
assert_eq!(
Config::from_vars(vars(&[("SUPABASE_SERVICE_ROLE_KEY", "k")])).err(),
Some(ConfigError::Missing("SUPABASE_URL"))
);
assert_eq!(
Config::from_vars(vars(&[("SUPABASE_URL", "http://x")])).err(),
Some(ConfigError::Missing("SUPABASE_SERVICE_ROLE_KEY"))
);
assert_eq!(
Config::from_vars(vars(&[
("SUPABASE_URL", "ftp://x"),
("SUPABASE_SERVICE_ROLE_KEY", "k")
]))
.err(),
Some(ConfigError::Invalid("SUPABASE_URL"))
);
assert_eq!(
Config::from_vars(vars(&[
("SUPABASE_URL", "http://x"),
("SUPABASE_SERVICE_ROLE_KEY", "k"),
("WORKER_POLL_SECONDS", "two"),
]))
.err(),
Some(ConfigError::Invalid("WORKER_POLL_SECONDS"))
);
assert_eq!(
Config::from_vars(vars(&[
("SUPABASE_URL", "http://x"),
("SUPABASE_SERVICE_ROLE_KEY", "k"),
("WORKER_ID", "bad id"),
]))
.err(),
Some(ConfigError::Invalid("WORKER_ID"))
);
}
#[test]
fn the_key_never_appears_in_debug_output() {
let cfg = Config::from_vars(vars(&[
("SUPABASE_URL", "http://x"),
("SUPABASE_SERVICE_ROLE_KEY", "super-secret-value-9f8e7d"),
("WORKER_ONCE", "1"),
]))
.unwrap();
let printed = format!("{cfg:?}");
assert!(!printed.contains("super-secret-value-9f8e7d"));
assert!(printed.contains("<redacted>"));
assert!(cfg.once);
}
#[test]
fn jwt_exp_reads_the_payload_claim() {
// header {"alg":"HS256"} . payload {"role":"service_role","exp":1983812996} . sig
let token =
"eyJhbGciOiJIUzI1NiJ9.eyJyb2xlIjoic2VydmljZV9yb2xlIiwiZXhwIjoxOTgzODEyOTk2fQ.sig";
assert_eq!(jwt_exp(token), Some(1_983_812_996));
assert_eq!(jwt_exp("sb_secret_abc"), None);
assert_eq!(jwt_exp("a.b"), None);
assert_eq!(base64url_decode("aGk"), Some(b"hi".to_vec()));
assert_eq!(base64url_decode("aGk="), Some(b"hi".to_vec()));
assert_eq!(base64url_decode("a*"), None);
}
}
+29
View File
@@ -0,0 +1,29 @@
//! The decode step as the job sees it: bytes in, a classified child run out. The real
//! implementation spawns this very binary as `decode` (R0 `Spawner`); tests inject outcomes.
use crate::spawn::{ChildRun, Spawner};
pub trait Decoder {
fn decode(&self, bytes: &[u8]) -> ChildRun;
}
/// Spawns `faunosteo-worker decode` from the running executable.
#[derive(Debug, Clone)]
pub struct ChildDecoder {
spawner: Spawner,
}
impl ChildDecoder {
/// Fails only when the executable path cannot be resolved (then nothing can be decoded).
pub fn from_current_exe() -> std::io::Result<Self> {
Ok(Self {
spawner: Spawner::new(std::env::current_exe()?),
})
}
}
impl Decoder for ChildDecoder {
fn decode(&self, bytes: &[u8]) -> ChildRun {
self.spawner.decode(bytes)
}
}
+6
View File
@@ -15,6 +15,10 @@ pub const OUTPUT_FAILED: i32 = 17;
pub const INTERNAL: i32 = 18;
/// The decoders' allocation or complexity budget was exceeded by the content.
pub const RESOURCE_LIMIT: i32 = 19;
/// The parent stopped its loop because a child reported a broken environment (R1).
pub const CHILD_BROKEN: i32 = 20;
/// The parent's watchdog found a stage past its deadline and exited (R1).
pub const WATCHDOG: i32 = 21;
/// Codes that judge the content (the upload is refused). Every other non-zero code means
/// the child's environment is broken (`Broken` outcome: the parent alerts rather than
@@ -42,6 +46,8 @@ pub fn name(code: i32) -> &'static str {
OUTPUT_FAILED => "output_failed",
INTERNAL => "internal",
RESOURCE_LIMIT => "resource_limit",
CHILD_BROKEN => "child_broken",
WATCHDOG => "watchdog",
_ => "unknown",
}
}
+328
View File
@@ -0,0 +1,328 @@
//! `Backend` over PostgREST and the Storage API with the service key (STORAGE.md §3.3).
//! Thin by design: key shapes are checked before any URL is built, every status is
//! classified here, and the closed `P0001` refusals of the RPCs come back as `Refused`.
use std::time::Duration;
use serde::de::DeserializeOwned;
use serde_json::{Value, json};
use ureq::Agent;
use ureq::http::Response;
use crate::backend::{Backend, BackendError, BackendResult, Job, Leftover, Purge};
use crate::config::Config;
use crate::keys;
use crate::limits::MAX_INPUT_BYTES;
/// Largest RPC response the worker reads (a claim of 100 rows is a few KiB).
const MAX_RPC_BODY: u64 = 1024 * 1024;
pub struct HttpBackend {
agent: Agent,
base: String,
key: String,
}
impl HttpBackend {
pub fn new(config: &Config) -> Self {
let agent: Agent = Agent::config_builder()
.timeout_global(Some(Duration::from_secs(30)))
.timeout_connect(Some(Duration::from_secs(5)))
.max_redirects(0)
// Never route the service key through a proxy picked up from the environment.
.proxy(None)
.http_status_as_error(false)
.user_agent("faunosteo-worker")
.build()
.into();
Self {
agent,
base: config.url.clone(),
key: config.service_key.expose().to_owned(),
}
}
fn rpc_url(&self, name: &str) -> String {
format!("{}/rest/v1/rpc/{name}", self.base)
}
fn object_url(&self, bucket: &str, key: &str) -> BackendResult<String> {
validate_bucket(bucket)?;
keys::validate_any_key(key).map_err(|e| BackendError::Internal(e.to_string()))?;
Ok(format!("{}/storage/v1/object/{bucket}/{key}", self.base))
}
/// Calls an RPC and returns the response body text of a 2xx.
fn rpc(&self, name: &str, body: &Value) -> BackendResult<String> {
let mut response = self
.agent
.post(&self.rpc_url(name))
.header("apikey", &self.key)
.header("Authorization", &format!("Bearer {}", self.key))
.header("Prefer", "return=representation")
.send_json(body)
.map_err(network)?;
let status = response.status().as_u16();
let text = read_text(&mut response)?;
if (200..300).contains(&status) {
Ok(text)
} else {
Err(classify(status, &text))
}
}
fn rpc_json<T: DeserializeOwned>(&self, name: &str, body: &Value) -> BackendResult<T> {
let text = self.rpc(name, body)?;
serde_json::from_str(&text).map_err(|e| BackendError::Decode(e.to_string()))
}
fn rpc_void(&self, name: &str, body: &Value) -> BackendResult<()> {
self.rpc(name, body).map(|_| ())
}
}
impl Backend for HttpBackend {
fn claim_jobs(&self, max_jobs: u32) -> BackendResult<Vec<Job>> {
self.rpc_json("claim_avatar_jobs", &json!({ "max_jobs": max_jobs }))
}
fn download_quarantine(&self, bucket: &str, key: &str) -> BackendResult<Vec<u8>> {
let url = self.object_url(bucket, key)?;
let mut response = self
.agent
.get(&url)
.header("apikey", &self.key)
.header("Authorization", &format!("Bearer {}", self.key))
.call()
.map_err(network)?;
let status = response.status().as_u16();
if status != 200 {
let text = read_text(&mut response).unwrap_or_default();
return Err(classify(status, &text));
}
let bytes = response
.body_mut()
.with_config()
.limit(MAX_INPUT_BYTES as u64 + 1)
.read_to_vec()
.map_err(|e| BackendError::Network(e.to_string()))?;
if bytes.len() > MAX_INPUT_BYTES {
return Err(BackendError::Overflow);
}
Ok(bytes)
}
fn put_clean(&self, bucket: &str, key: &str, bytes: &[u8]) -> BackendResult<()> {
let url = self.object_url(bucket, key)?;
let mut response = self
.agent
.post(&url)
.header("apikey", &self.key)
.header("Authorization", &format!("Bearer {}", self.key))
.header("Content-Type", "image/webp")
.header("x-upsert", "true")
.send(bytes)
.map_err(network)?;
expect_success(&mut response)
}
fn complete(
&self,
asset_id: &str,
lock_token: &str,
byte_size: usize,
sha256: &str,
) -> BackendResult<()> {
self.rpc_void(
"complete_avatar_job",
&json!({ "asset_id": asset_id, "lock_token": lock_token, "byte_size": byte_size, "sha256": sha256 }),
)
}
fn reject(&self, asset_id: &str, lock_token: &str, reason: &str) -> BackendResult<()> {
self.rpc_void(
"reject_avatar_job",
&json!({ "asset_id": asset_id, "lock_token": lock_token, "reason": reason }),
)
}
fn delete_object(&self, bucket: &str, key: &str) -> BackendResult<()> {
let url = self.object_url(bucket, key)?;
let mut response = self
.agent
.delete(&url)
.header("apikey", &self.key)
.header("Authorization", &format!("Bearer {}", self.key))
.call()
.map_err(network)?;
// A missing object is a purge already done (HTTP 404, or this Storage API's
// HTTP 400 body with statusCode 404, normalised by `classify`).
match expect_success(&mut response) {
Err(BackendError::Status(404)) => Ok(()),
other => other,
}
}
fn mark_quarantine_purged(&self, asset_id: &str) -> BackendResult<()> {
self.rpc_void("mark_quarantine_purged", &json!({ "asset_id": asset_id }))
}
fn claim_purges(&self, max_items: u32) -> BackendResult<Vec<Purge>> {
self.rpc_json("claim_purges", &json!({ "max_items": max_items }))
}
fn ack_purge(&self, purge_id: &str) -> BackendResult<()> {
self.rpc_void("ack_purge", &json!({ "purge_id": purge_id }))
}
fn claim_quarantine_leftovers(
&self,
older_than_seconds: u64,
max_items: u32,
) -> BackendResult<Vec<Leftover>> {
self.rpc_json(
"claim_quarantine_leftovers",
&json!({ "older_than_seconds": older_than_seconds, "max_items": max_items }),
)
}
fn expire_pending_uploads(&self, max_age_seconds: u64) -> BackendResult<i64> {
self.rpc_json(
"expire_pending_uploads",
&json!({ "max_age_seconds": max_age_seconds }),
)
}
}
fn validate_bucket(bucket: &str) -> BackendResult<()> {
let ok = !bucket.is_empty()
&& bucket
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-');
if ok {
Ok(())
} else {
Err(BackendError::Internal("bucket name refused".to_owned()))
}
}
fn network(error: ureq::Error) -> BackendError {
BackendError::Network(error.to_string())
}
fn read_text(response: &mut Response<ureq::Body>) -> BackendResult<String> {
response
.body_mut()
.with_config()
.limit(MAX_RPC_BODY)
.read_to_string()
.map_err(|e| BackendError::Network(e.to_string()))
}
fn expect_success(response: &mut Response<ureq::Body>) -> BackendResult<()> {
let status = response.status().as_u16();
if (200..300).contains(&status) {
return Ok(());
}
let text = read_text(response).unwrap_or_default();
Err(classify(status, &text))
}
/// A PostgREST `P0001` body names a closed refusal; a Storage API "not found" (HTTP 400
/// with `statusCode` 404 on this image family) is reported as 404; any other status as such.
pub fn classify(status: u16, body: &str) -> BackendError {
let Ok(value) = serde_json::from_str::<Value>(body) else {
return BackendError::Status(status);
};
if value.get("code").and_then(Value::as_str) == Some("P0001")
&& let Some(message) = value.get("message").and_then(Value::as_str)
&& is_closed_code(message)
{
return BackendError::Refused(message.to_owned());
}
if status == 400
&& (value.get("statusCode").and_then(Value::as_str) == Some("404")
|| value.get("error").and_then(Value::as_str) == Some("not_found"))
{
return BackendError::Status(404);
}
BackendError::Status(status)
}
/// Shape of a closed code (`invalid_state`): the server's free text never becomes a code.
fn is_closed_code(message: &str) -> bool {
!message.is_empty()
&& message.len() <= 40
&& message.bytes().all(|b| b.is_ascii_lowercase() || b == b'_')
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn p0001_bodies_become_closed_refusals_and_others_statuses() {
assert_eq!(
classify(
400,
r#"{"code":"P0001","message":"invalid_state","details":null}"#
),
BackendError::Refused("invalid_state".to_owned())
);
assert_eq!(
classify(403, r#"{"code":"42501","message":"denied"}"#),
BackendError::Status(403)
);
assert_eq!(
classify(400, r#"{"code":"P0001","message":"Free text!"}"#),
BackendError::Status(400)
);
assert_eq!(classify(500, "not json"), BackendError::Status(500));
// storage-api v1.79 answers a missing object with HTTP 400 and statusCode "404".
assert_eq!(
classify(
400,
r#"{"statusCode":"404","error":"not_found","message":"Object not found"}"#
),
BackendError::Status(404)
);
assert_eq!(
classify(
400,
r#"{"statusCode":"400","error":"InvalidRequest","message":"x"}"#
),
BackendError::Status(400)
);
}
#[test]
fn object_urls_need_valid_buckets_and_keys() {
let config = Config::from_vars(|name| match name {
"SUPABASE_URL" => Some("http://kong:8000".to_owned()),
"SUPABASE_SERVICE_ROLE_KEY" => Some("k".to_owned()),
_ => None,
})
.ok();
let Some(config) = config else {
panic!("config");
};
let backend = HttpBackend::new(&config);
let key = "profiles/10000000-0000-0000-0000-00000000000a/10000000-0000-0000-0000-00000000000b/avatar.webp";
assert_eq!(
backend.object_url("avatars", key).ok().as_deref(),
Some(
"http://kong:8000/storage/v1/object/avatars/profiles/10000000-0000-0000-0000-00000000000a/10000000-0000-0000-0000-00000000000b/avatar.webp"
)
);
assert!(backend.object_url("avatars/../x", key).is_err());
assert!(
backend
.object_url("avatars", "profiles/../etc/passwd")
.is_err()
);
assert_eq!(
backend.rpc_url("claim_avatar_jobs"),
"http://kong:8000/rest/v1/rpc/claim_avatar_jobs"
);
}
}
+270
View File
@@ -0,0 +1,270 @@
//! One avatar job (STORAGE.md §3.4): download the quarantine bytes, hand them to the
//! sandboxed child, upload the clean output, commit the row, clean the quarantine. Every
//! outcome of the child and of the backend maps to one closed end; nothing is deleted on a
//! refused commit (the purge queue owns every object of a terminal row).
use std::time::Instant;
use sha2::{Digest, Sha256};
use crate::backend::{Backend, BackendError, Job};
use crate::decoder::Decoder;
use crate::spawn::ChildOutcome;
use crate::watchdog::{Watchdog, deadlines};
use crate::{exit_codes, keys, log};
/// How a job ended, as the loop sees it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum JobEnd {
/// The row is `clean`.
Completed,
/// The row was refused with this closed code.
Rejected(&'static str),
/// The commit or the rejection was refused: the row moved on without us.
Superseded,
/// A transport failure: the lease expires and the row is claimed again.
LeftForLease,
/// The child reported a broken environment: the loop must stop.
Stop,
}
/// Closed rejection reasons the row accepts (STORAGE.md §1.2).
pub mod reasons {
pub const UNSUPPORTED_TYPE: &str = "unsupported_type";
pub const DECODE_FAILED: &str = "decode_failed";
pub const DIMENSIONS: &str = "dimensions";
pub const PROCESSING_ERROR: &str = "processing_error";
}
/// Runs one claimed job to its end.
pub fn process_job(
backend: &dyn Backend,
decoder: &dyn Decoder,
watchdog: &Watchdog,
job: &Job,
) -> JobEnd {
let started = Instant::now();
let end = run(backend, decoder, watchdog, job);
let line = match &end {
JobEnd::Completed => log::info("job_completed"),
JobEnd::Rejected(code) => log::info("job_rejected").code(code),
JobEnd::Superseded => log::info("job_superseded"),
JobEnd::LeftForLease => log::warn("job_left_for_lease"),
JobEnd::Stop => log::error("job_stop"),
};
line.asset(&job.asset_id)
.duration_ms(started.elapsed().as_millis())
.emit();
end
}
fn run(backend: &dyn Backend, decoder: &dyn Decoder, watchdog: &Watchdog, job: &Job) -> JobEnd {
if !keys::is_uuid(&job.asset_id) || !keys::is_uuid(&job.lock_token) {
log::error("job_invalid_identity").emit();
return JobEnd::LeftForLease;
}
let Some(quarantine_key) = validated_keys(job) else {
return reject(backend, job, reasons::PROCESSING_ERROR);
};
let bytes = match download(backend, watchdog, job, quarantine_key) {
Ok(bytes) => bytes,
Err(end) => return end,
};
let verdict = {
let _stage = watchdog.enter("decode", deadlines::DECODE);
classify_child(decoder.decode(&bytes), job)
};
match verdict {
ChildVerdict::Clean(webp) => commit(backend, watchdog, job, quarantine_key, &webp),
ChildVerdict::Reject(reason) => reject(backend, job, reason),
ChildVerdict::Stop => JobEnd::Stop,
}
}
/// Both keys and the bucket must have the shapes the RPC builds; `None` = corrupted row.
fn validated_keys(job: &Job) -> Option<&str> {
let quarantine_key = job.quarantine_key.as_deref()?;
let bucket_ok = !job.bucket_id.is_empty()
&& job
.bucket_id
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-');
// Shape, then ownership: both keys must embed this job's owner and asset ids, so a
// mismatched row can never make the worker write or read another account's key.
if !bucket_ok
|| keys::validate_object_key(&job.object_key).is_err()
|| keys::validate_quarantine_key(quarantine_key).is_err()
|| !keys::belongs_to(&job.object_key, &job.owner_id, &job.asset_id)
|| !keys::belongs_to(quarantine_key, &job.owner_id, &job.asset_id)
{
log::error("job_invalid_keys").asset(&job.asset_id).emit();
return None;
}
Some(quarantine_key)
}
fn download(
backend: &dyn Backend,
watchdog: &Watchdog,
job: &Job,
quarantine_key: &str,
) -> Result<Vec<u8>, JobEnd> {
let _stage = watchdog.enter("download", deadlines::DOWNLOAD);
match backend.download_quarantine(&job.bucket_id, quarantine_key) {
Ok(bytes) => Ok(bytes),
Err(BackendError::Status(404)) => {
log::warn("download_missing").asset(&job.asset_id).emit();
Err(reject(backend, job, reasons::DECODE_FAILED))
}
Err(BackendError::Overflow) => {
log::warn("download_overflow").asset(&job.asset_id).emit();
Err(reject(backend, job, reasons::DECODE_FAILED))
}
Err(error) => Err(transport_failure("download", job, &error)),
}
}
/// What the child's end means for the row.
enum ChildVerdict {
Clean(Vec<u8>),
Reject(&'static str),
Stop,
}
/// Maps every way the child can end (R0 `ChildOutcome`) to a verdict on the row.
fn classify_child(run: crate::spawn::ChildRun, job: &Job) -> ChildVerdict {
let stderr = run.stderr;
match run.outcome {
ChildOutcome::Produced(webp) => ChildVerdict::Clean(webp),
ChildOutcome::Rejected { code, name } => {
log::info("child_rejected")
.asset(&job.asset_id)
.code(name)
.child_stderr(&stderr)
.emit();
ChildVerdict::Reject(content_reason(code))
}
ChildOutcome::Broken { name, .. } => {
log::error("child_broken")
.asset(&job.asset_id)
.code(name)
.child_stderr(&stderr)
.emit();
ChildVerdict::Stop
}
ChildOutcome::Signalled { signal } => {
log::warn("child_signalled")
.asset(&job.asset_id)
.signal(signal)
.child_stderr(&stderr)
.emit();
ChildVerdict::Reject(reasons::PROCESSING_ERROR)
}
ChildOutcome::OutputOverflow => {
log::warn("child_output_overflow")
.asset(&job.asset_id)
.emit();
ChildVerdict::Reject(reasons::PROCESSING_ERROR)
}
ChildOutcome::Timeout => {
log::warn("child_timeout").asset(&job.asset_id).emit();
ChildVerdict::Reject(reasons::PROCESSING_ERROR)
}
ChildOutcome::Failed(detail) => {
log::warn("child_failed")
.asset(&job.asset_id)
.detail(&detail)
.emit();
ChildVerdict::Reject(reasons::PROCESSING_ERROR)
}
}
}
/// The row's closed reason for a content exit code of the child.
pub fn content_reason(code: i32) -> &'static str {
match code {
exit_codes::UNSUPPORTED_TYPE => reasons::UNSUPPORTED_TYPE,
exit_codes::DIMENSIONS => reasons::DIMENSIONS,
_ => reasons::DECODE_FAILED,
}
}
fn commit(
backend: &dyn Backend,
watchdog: &Watchdog,
job: &Job,
quarantine_key: &str,
webp: &[u8],
) -> JobEnd {
{
let _stage = watchdog.enter("upload", deadlines::UPLOAD);
if let Err(error) = backend.put_clean(&job.bucket_id, &job.object_key, webp) {
return transport_failure("upload", job, &error);
}
}
let _stage = watchdog.enter("commit", deadlines::COMMIT);
match backend.complete(
&job.asset_id,
&job.lock_token,
webp.len(),
&sha256_hex(webp),
) {
Ok(()) => {}
Err(BackendError::Refused(code)) => {
log::info("complete_refused")
.asset(&job.asset_id)
.code(&code)
.emit();
return JobEnd::Superseded;
}
Err(error) => return transport_failure("commit", job, &error),
}
// Best effort: the sweeper finishes a quarantine the worker could not delete.
if backend
.delete_object(&job.bucket_id, quarantine_key)
.is_ok()
&& backend.mark_quarantine_purged(&job.asset_id).is_err()
{
log::warn("mark_quarantine_purged_failed")
.asset(&job.asset_id)
.emit();
}
JobEnd::Completed
}
/// Rejection of the row with a closed reason; a refusal means the row moved on.
fn reject(backend: &dyn Backend, job: &Job, reason: &'static str) -> JobEnd {
match backend.reject(&job.asset_id, &job.lock_token, reason) {
Ok(()) => JobEnd::Rejected(reason),
Err(BackendError::Refused(code)) => {
log::info("reject_refused")
.asset(&job.asset_id)
.code(&code)
.emit();
JobEnd::Superseded
}
Err(error) => transport_failure("reject", job, &error),
}
}
fn transport_failure(stage: &str, job: &Job, error: &BackendError) -> JobEnd {
let line = log::warn("backend_failure")
.asset(&job.asset_id)
.stage(stage)
.code(error.kind());
// The library's error text (URI fragments, server bodies) stays out of the logs:
// the closed kind and the HTTP status are the record.
match error {
BackendError::Status(status) => line.http_status(*status).emit(),
_ => line.emit(),
}
JobEnd::LeftForLease
}
/// Lowercase hex sha256 of the clean output.
pub fn sha256_hex(bytes: &[u8]) -> String {
Sha256::digest(bytes)
.iter()
.map(|b| format!("{b:02x}"))
.collect()
}
+156
View File
@@ -0,0 +1,156 @@
//! Shape of the object keys the worker accepts from the database (STORAGE.md §1.4). A key
//! is validated before it reaches a URL, so a corrupted or hostile row can never make the
//! worker read or delete outside the two prefixes.
use std::fmt;
/// Final avatar object: `profiles/<owner_id>/<asset_id>/avatar.webp`.
pub const FINAL_PREFIX: &str = "profiles";
/// Upload landing key: `quarantine/<owner_id>/<asset_id>/upload`.
pub const QUARANTINE_PREFIX: &str = "quarantine";
/// Why a key or an id was refused.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyError {
Segments,
Prefix,
Uuid,
FileName,
}
impl fmt::Display for KeyError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let text = match self {
Self::Segments => "key must have four segments",
Self::Prefix => "unknown key prefix",
Self::Uuid => "segment is not a lowercase uuid",
Self::FileName => "unexpected file name",
};
f.write_str(text)
}
}
/// Lowercase hyphenated uuid (36 characters).
pub fn is_uuid(text: &str) -> bool {
let bytes = text.as_bytes();
bytes.len() == 36
&& bytes.iter().enumerate().all(|(i, b)| {
if matches!(i, 8 | 13 | 18 | 23) {
*b == b'-'
} else {
b.is_ascii_digit() || (b'a'..=b'f').contains(b)
}
})
}
fn validate(key: &str, prefix: &str, file_name: &str) -> Result<(), KeyError> {
let segments: Vec<&str> = key.split('/').collect();
let [p, owner, asset, name] = segments.as_slice() else {
return Err(KeyError::Segments);
};
if *p != prefix {
return Err(KeyError::Prefix);
}
if !is_uuid(owner) || !is_uuid(asset) {
return Err(KeyError::Uuid);
}
if *name != file_name {
return Err(KeyError::FileName);
}
Ok(())
}
/// `profiles/<uuid>/<uuid>/avatar.webp`.
pub fn validate_object_key(key: &str) -> Result<(), KeyError> {
validate(key, FINAL_PREFIX, "avatar.webp")
}
/// `quarantine/<uuid>/<uuid>/upload`.
pub fn validate_quarantine_key(key: &str) -> Result<(), KeyError> {
validate(key, QUARANTINE_PREFIX, "upload")
}
/// Either shape: the only keys the worker may delete.
pub fn validate_any_key(key: &str) -> Result<(), KeyError> {
if key.starts_with("profiles/") {
validate_object_key(key)
} else {
validate_quarantine_key(key)
}
}
/// True when a (valid) key's owner and asset segments are exactly these ids.
pub fn belongs_to(key: &str, owner_id: &str, asset_id: &str) -> bool {
let mut segments = key.split('/');
segments.next();
segments.next() == Some(owner_id) && segments.next() == Some(asset_id)
}
/// The asset id embedded in a valid quarantine key.
pub fn asset_id_of_quarantine_key(key: &str) -> Option<&str> {
validate_quarantine_key(key).ok()?;
key.split('/').nth(2)
}
#[cfg(test)]
mod tests {
use super::*;
const U1: &str = "10000000-0000-0000-0000-00000000000a";
const U2: &str = "7f2f0c3e-1c7a-4b2b-9c8d-0123456789ab";
#[test]
fn valid_keys_are_accepted() {
assert_eq!(
validate_object_key(&format!("{FINAL_PREFIX}/{U1}/{U2}/avatar.webp")),
Ok(())
);
assert_eq!(
validate_quarantine_key(&format!("{QUARANTINE_PREFIX}/{U1}/{U2}/upload")),
Ok(())
);
assert_eq!(
asset_id_of_quarantine_key(&format!("{QUARANTINE_PREFIX}/{U1}/{U2}/upload")),
Some(U2)
);
assert_eq!(
validate_any_key(&format!("{FINAL_PREFIX}/{U1}/{U2}/avatar.webp")),
Ok(())
);
}
#[test]
fn deviations_are_refused() {
let cases: &[(&str, KeyError)] = &[
("profiles/x/avatar.webp", KeyError::Segments),
(
&format!("profiles/{U1}/{U2}/avatar.webp/"),
KeyError::Segments,
),
(&format!("avatars/{U1}/{U2}/avatar.webp"), KeyError::Prefix),
(&format!("profiles/{U1}/../avatar.webp"), KeyError::Uuid),
(
&format!("profiles/{}/{U2}/avatar.webp", U1.to_uppercase()),
KeyError::Uuid,
),
(
&format!("profiles/{U1}/{U2}/avatar.png"),
KeyError::FileName,
),
(&format!("profiles/{U1}/{U2}/upload"), KeyError::FileName),
(
&format!("quarantine/{U1}/{U2}/avatar.webp"),
KeyError::FileName,
),
];
for (key, expected) in cases {
assert_eq!(validate_any_key(key).err(), Some(*expected), "{key}");
}
assert_eq!(asset_id_of_quarantine_key("quarantine/a/b/upload"), None);
let key = format!("profiles/{U1}/{U2}/avatar.webp");
assert!(belongs_to(&key, U1, U2));
assert!(!belongs_to(&key, U2, U1));
assert!(!belongs_to(&key, U1, U1));
assert!(!is_uuid("10000000-0000-0000-0000-00000000000"));
}
}
+11 -1
View File
@@ -1,15 +1,25 @@
//! `faunosteo-worker` library: the pieces the binary, the tests and the R1 job loop share.
//! `faunosteo-worker` library: the sandboxed decode child (R0) and the keyed job loop (R1).
//!
//! Process model (review Part IV.1): a keyed parent spawns the same binary as `decode`, a
//! child with an empty environment that confines itself (rlimits + seccomp) before reading
//! one image on stdin and writing one 512 × 512 WebP on stdout. The parent never decodes:
//! it checks the container header of what comes back (`webp_check`).
pub mod backend;
pub mod config;
pub mod decode;
pub mod decoder;
pub mod exit_codes;
#[cfg(feature = "test-hooks")]
pub mod fixtures;
pub mod http;
pub mod job;
pub mod keys;
pub mod limits;
pub mod log;
pub mod run;
pub mod sandbox;
pub mod spawn;
pub mod sweep;
pub mod watchdog;
pub mod webp_check;
+156
View File
@@ -0,0 +1,156 @@
//! JSON-lines logging of the parent (STORAGE.md §3.6): one object per line on stdout, an
//! allowlist of fields, values authored by the worker only. Text a child wrote is escaped
//! by the JSON encoder and truncated; no key, no token, no user content ever reaches a line.
use std::io::Write;
use std::sync::OnceLock;
use std::time::{SystemTime, UNIX_EPOCH};
use serde_json::{Map, Value};
static WORKER_ID: OnceLock<String> = OnceLock::new();
/// Tags every later line with the worker id (first call wins).
pub fn set_worker_id(id: &str) {
let _ = WORKER_ID.set(id.to_owned());
}
/// Longest child stderr excerpt kept in a line.
pub const CHILD_STDERR_MAX: usize = 200;
/// One line under construction.
#[must_use = "a log line is written by emit()"]
pub struct LogLine {
fields: Map<String, Value>,
}
pub fn info(event: &str) -> LogLine {
LogLine::new("info", event)
}
pub fn warn(event: &str) -> LogLine {
LogLine::new("warn", event)
}
pub fn error(event: &str) -> LogLine {
LogLine::new("error", event)
}
impl LogLine {
fn new(level: &str, event: &str) -> Self {
let mut fields = Map::new();
fields.insert("ts".into(), Value::from(unix_seconds()));
fields.insert("level".into(), Value::from(level));
fields.insert("event".into(), Value::from(event));
if let Some(id) = WORKER_ID.get() {
fields.insert("worker_id".into(), Value::from(id.as_str()));
}
Self { fields }
}
fn with(mut self, key: &str, value: impl Into<Value>) -> Self {
self.fields.insert(key.to_owned(), value.into());
self
}
pub fn asset(self, id: &str) -> Self {
self.with("asset_id", id)
}
pub fn stage(self, stage: &str) -> Self {
self.with("stage", stage)
}
pub fn code(self, code: &str) -> Self {
self.with("code", code)
}
pub fn signal(self, signal: i32) -> Self {
self.with("signal", signal)
}
pub fn http_status(self, status: u16) -> Self {
self.with("http_status", status)
}
pub fn duration_ms(self, ms: u128) -> Self {
self.with("duration_ms", u64::try_from(ms).unwrap_or(u64::MAX))
}
pub fn count(self, count: usize) -> Self {
self.with("count", u64::try_from(count).unwrap_or(u64::MAX))
}
/// Worker-authored text only (never a child's or a server's free text).
pub fn detail(self, detail: &str) -> Self {
self.with("detail", detail)
}
/// Child-controlled text: truncated on a character boundary, escaped by the encoder.
pub fn child_stderr(self, text: &str) -> Self {
self.with("child_stderr", truncate(text, CHILD_STDERR_MAX))
}
/// Renders the line (tests read it, `emit` writes it).
pub fn render(&self) -> String {
Value::Object(self.fields.clone()).to_string()
}
pub fn emit(self) {
let stdout = std::io::stdout();
let mut lock = stdout.lock();
let _ = writeln!(lock, "{}", self.render());
let _ = lock.flush();
}
}
fn truncate(text: &str, max: usize) -> &str {
if text.len() <= max {
return text;
}
let mut end = max;
while !text.is_char_boundary(end) {
end -= 1;
}
&text[..end]
}
fn unix_seconds() -> u64 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| d.as_secs())
.unwrap_or(0)
}
#[cfg(test)]
mod tests {
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn lines_are_json_objects_with_the_allowlisted_fields() {
let line = info("job_done")
.asset("a1")
.stage("commit")
.duration_ms(12)
.render();
let value: Value = serde_json::from_str(&line).unwrap();
assert_eq!(value["event"], "job_done");
assert_eq!(value["asset_id"], "a1");
assert_eq!(value["stage"], "commit");
assert_eq!(value["duration_ms"], 12);
assert!(value["ts"].as_u64().is_some());
}
#[test]
fn child_text_is_escaped_and_truncated() {
let hostile = format!("line\n\u{1b}[31mred\"quote{}", "é".repeat(300));
let line = warn("child").child_stderr(&hostile).render();
assert!(!line.contains('\n'));
assert!(!line.contains('\u{1b}'));
let value: Value = serde_json::from_str(&line).unwrap();
let kept = value["child_stderr"].as_str().unwrap();
assert!(kept.len() <= CHILD_STDERR_MAX);
assert!(kept.starts_with("line\n"));
}
}
+4 -38
View File
@@ -1,14 +1,14 @@
//! `faunosteo-worker` entry: a plain `main`, no async runtime, no thread, so the `decode`
//! child installs its sandbox in a single-threaded process (review Part IV.1).
//!
//! R0 subcommands: `decode` (the sandboxed child), `run --check` (prints the resolved
//! configuration, no job loop before R1), `version`.
//! Subcommands: `decode` (the sandboxed child, R0), `run` (the keyed job loop, R1),
//! `run --check` (prints the resolved configuration without secrets), `version`.
#[cfg(feature = "test-hooks")]
use std::io::Write;
use faunosteo_worker::decode;
use faunosteo_worker::exit_codes;
use faunosteo_worker::limits::MIN_LIBWEBP_VERSION;
fn main() {
let args: Vec<String> = std::env::args().collect();
@@ -24,7 +24,7 @@ fn main() {
&mut stderr.lock(),
)
}
Some("run") => run(&args[2..]),
Some("run") => faunosteo_worker::run::main(&args[2..]),
Some("version") => version(),
#[cfg(feature = "test-hooks")]
Some("fixture") => fixture(&args[2..]),
@@ -59,40 +59,6 @@ fn faunosteo_core_version() -> &'static str {
env!("CARGO_PKG_VERSION")
}
/// `run --check`: prints what the worker would start with, without secrets. The job loop
/// arrives at R1; a plain `run` therefore refuses to pretend it works.
fn run(args: &[String]) -> i32 {
let (major, minor, revision) = decode::libwebp_version();
let libwebp_ok = (major, minor, revision) >= MIN_LIBWEBP_VERSION;
println!("faunosteo-worker run: R0 skeleton, the job loop arrives at R1");
println!(
"libwebp {major}.{minor}.{revision} ({})",
if libwebp_ok {
"accepted"
} else {
"below the minimum, refusing to start"
}
);
for name in ["SUPABASE_URL", "SUPABASE_SERVICE_ROLE_KEY", "WORKER_ID"] {
let state = if std::env::var_os(name).is_some() {
"set"
} else {
"unset"
};
println!("{name}: {state}");
}
let _ = std::io::stdout().flush();
if !libwebp_ok {
return exit_codes::INTERNAL;
}
if args.iter().any(|arg| arg == "--check") {
exit_codes::OK
} else {
eprintln!("run: nothing to do before R1 (use --check to print the configuration)");
exit_codes::USAGE
}
}
#[cfg(feature = "test-hooks")]
fn fixture(args: &[String]) -> i32 {
let Some(bytes) = args
+229
View File
@@ -0,0 +1,229 @@
//! `faunosteo-worker run`: the keyed parent's loop (STORAGE.md §3.4, §3.5) and
//! `run --check`, the start-up self-description without any secret value.
use std::io::Write;
use std::time::{Instant, SystemTime, UNIX_EPOCH};
use crate::backend::Backend;
use crate::config::{self, Config, ConfigError};
use crate::decode::libwebp_version;
use crate::decoder::{ChildDecoder, Decoder};
use crate::http::HttpBackend;
use crate::job::{JobEnd, process_job};
use crate::limits::MIN_LIBWEBP_VERSION;
use crate::sweep::{SweepConfig, run_sweep};
use crate::watchdog::{Watchdog, deadlines};
use crate::{exit_codes, log};
const THIRTY_DAYS: i64 = 30 * 24 * 3600;
/// Entry of the `run` subcommand; returns the process exit code.
pub fn main(args: &[String]) -> i32 {
match args.first().map(String::as_str) {
Some("--check") => check(),
Some(other) => {
eprintln!("run: unknown argument {other} (use --check to print the configuration)");
exit_codes::USAGE
}
None => start(),
}
}
fn start() -> i32 {
let config = match Config::from_env() {
Ok(config) => config,
Err(error) => {
eprintln!("run: {error}");
return exit_codes::USAGE;
}
};
if !libwebp_ok() {
eprintln!("run: linked libwebp below the minimum, refusing to start");
return exit_codes::INTERNAL;
}
log::set_worker_id(&config.worker_id);
log_start(&config);
let decoder = match ChildDecoder::from_current_exe() {
Ok(decoder) => decoder,
Err(error) => {
log::error("start_failed")
.detail(&format!("current_exe: {error}"))
.emit();
return exit_codes::INTERNAL;
}
};
let backend = HttpBackend::new(&config);
let watchdog = Watchdog::new();
watchdog.spawn_monitor();
run_loop(&config, &backend, &decoder, &watchdog)
}
/// Sweep at start and every `sweep` interval; claim one job per pass; idle on `poll`.
pub fn run_loop(
config: &Config,
backend: &dyn Backend,
decoder: &dyn Decoder,
watchdog: &Watchdog,
) -> i32 {
let sweep_config = SweepConfig {
pending_max_age: config.pending_max_age,
quarantine_grace: config.quarantine_grace,
};
let mut next_sweep = Instant::now();
loop {
if Instant::now() >= next_sweep {
run_sweep(backend, watchdog, sweep_config);
next_sweep = Instant::now() + config.sweep;
}
let worked = match claim_pass(backend, decoder, watchdog) {
Ok(count) => count > 0,
Err(code) => return code,
};
if config.once {
return exit_codes::OK;
}
if !worked {
std::thread::sleep(config.poll);
}
}
}
/// One claim and the jobs it returned; `Err` carries the exit code of a stop.
fn claim_pass(
backend: &dyn Backend,
decoder: &dyn Decoder,
watchdog: &Watchdog,
) -> Result<usize, i32> {
let jobs = {
let _stage = watchdog.enter("claim", deadlines::CLAIM);
match backend.claim_jobs(1) {
Ok(jobs) => jobs,
Err(error) => {
let line = log::warn("claim_failed").code(error.kind());
match error {
crate::backend::BackendError::Status(status) => line.http_status(status).emit(),
_ => line.emit(),
}
return Ok(0);
}
}
};
for job in &jobs {
if process_job(backend, decoder, watchdog, job) == JobEnd::Stop {
return Err(exit_codes::CHILD_BROKEN);
}
}
Ok(jobs.len())
}
fn log_start(config: &Config) {
let (major, minor, rev) = libwebp_version();
log::info("worker_start")
.detail(&format!(
"libwebp {major}.{minor}.{rev}; poll {}s sweep {}s grace {}s pending {}s once {}",
config.poll.as_secs(),
config.sweep.as_secs(),
config.quarantine_grace.as_secs(),
config.pending_max_age.as_secs(),
config.once
))
.emit();
match token_expiry(config) {
TokenExpiry::NotJwt => log::info("token_no_expiry").emit(),
TokenExpiry::Valid(days) => log::info("token_expiry").count(days).emit(),
TokenExpiry::Soon(days) => log::warn("token_expires_soon").count(days).emit(),
TokenExpiry::Expired => log::error("token_expired").emit(),
}
}
enum TokenExpiry {
NotJwt,
Valid(usize),
Soon(usize),
Expired,
}
fn token_expiry(config: &Config) -> TokenExpiry {
let Some(exp) = config::jwt_exp(config.service_key.expose()) else {
return TokenExpiry::NotJwt;
};
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.map(|d| i64::try_from(d.as_secs()).unwrap_or(i64::MAX))
.unwrap_or(0);
let left = exp - now;
let days = usize::try_from(left / 86_400).unwrap_or(0);
if left <= 0 {
TokenExpiry::Expired
} else if left < THIRTY_DAYS {
TokenExpiry::Soon(days)
} else {
TokenExpiry::Valid(days)
}
}
fn libwebp_ok() -> bool {
libwebp_version() >= MIN_LIBWEBP_VERSION
}
/// Prints the resolved configuration without any value of a secret. Exit 0 when the
/// worker could start with this environment, or when no worker variable is set at all
/// (an unconfigured image is a valid artifact check); a partial or invalid configuration
/// and a too-old libwebp exit non-zero and name the variable.
fn check() -> i32 {
let (major, minor, rev) = libwebp_version();
let libwebp_ok = libwebp_ok();
println!(
"libwebp: {major}.{minor}.{rev} ({})",
if libwebp_ok {
"accepted"
} else {
"below the minimum, refusing to start"
}
);
let mut any_set = false;
for name in config::VARIABLES {
let set = std::env::var_os(name).is_some();
any_set |= set;
println!("{name}: {}", if set { "set" } else { "unset" });
}
let configuration = if any_set {
Some(Config::from_env())
} else {
None
};
print_configuration(configuration.as_ref());
let _ = std::io::stdout().flush();
match (libwebp_ok, configuration) {
(false, _) => exit_codes::INTERNAL,
(true, None | Some(Ok(_))) => exit_codes::OK,
(true, Some(Err(_))) => exit_codes::USAGE,
}
}
fn print_configuration(configuration: Option<&Result<Config, ConfigError>>) {
match configuration {
None => println!("configuration: none (no worker variable set; the loop would not start)"),
Some(Err(error)) => println!("configuration: invalid ({error})"),
Some(Ok(config)) => {
println!(
"configuration: ok (url {}, worker_id {}, poll {}s, sweep {}s, quarantine grace {}s, pending max age {}s, once {})",
config.url,
config.worker_id,
config.poll.as_secs(),
config.sweep.as_secs(),
config.quarantine_grace.as_secs(),
config.pending_max_age.as_secs(),
config.once
);
match token_expiry(config) {
TokenExpiry::NotJwt => println!("token: not a JWT (no expiry to track)"),
TokenExpiry::Valid(days) => println!("token: JWT expiring in {days} days"),
TokenExpiry::Soon(days) => {
println!("token: JWT expiring in {days} days (WARNING: under 30)")
}
TokenExpiry::Expired => println!("token: JWT EXPIRED"),
}
}
}
}
+111
View File
@@ -0,0 +1,111 @@
//! The sweeper (STORAGE.md §3.5): expires never-confirmed uploads, serves the purge queue
//! and deletes quarantine leftovers. These are the only deletions the worker makes, and a
//! key that fails the shape check is never sent to the Storage API.
use std::time::Duration;
use crate::backend::{Backend, BackendError, Leftover, Purge};
use crate::watchdog::{Watchdog, deadlines};
use crate::{keys, log};
/// Ages the sweeper applies.
#[derive(Debug, Clone, Copy)]
pub struct SweepConfig {
pub pending_max_age: Duration,
pub quarantine_grace: Duration,
}
/// What one sweep did.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct SweepReport {
pub expired: i64,
pub purged: usize,
pub leftovers: usize,
pub errors: usize,
}
const BATCH: u32 = 100;
/// One full sweep; every failure is logged and counted, never fatal.
pub fn run_sweep(backend: &dyn Backend, watchdog: &Watchdog, config: SweepConfig) -> SweepReport {
let _stage = watchdog.enter("sweep", deadlines::SWEEP);
let mut report = SweepReport::default();
match backend.expire_pending_uploads(config.pending_max_age.as_secs()) {
Ok(count) => report.expired = count,
Err(error) => failure("expire_pending_uploads", &error, &mut report),
}
match backend.claim_purges(BATCH) {
Ok(purges) => purges
.iter()
.for_each(|p| serve_purge(backend, p, &mut report)),
Err(error) => failure("claim_purges", &error, &mut report),
}
match backend.claim_quarantine_leftovers(config.quarantine_grace.as_secs(), BATCH) {
Ok(items) => items
.iter()
.for_each(|l| serve_leftover(backend, l, &mut report)),
Err(error) => failure("claim_quarantine_leftovers", &error, &mut report),
}
log::info("sweep_done")
.count(report.purged + report.leftovers)
.detail(&format!(
"expired={} errors={}",
report.expired, report.errors
))
.emit();
report
}
fn serve_purge(backend: &dyn Backend, purge: &Purge, report: &mut SweepReport) {
if !deletable(&purge.bucket_id, &purge.object_name) {
log::error("purge_invalid_key")
.detail(&purge.purge_id)
.emit();
report.errors += 1;
return;
}
if let Err(error) = backend.delete_object(&purge.bucket_id, &purge.object_name) {
return failure("delete_object", &error, report);
}
match backend.ack_purge(&purge.purge_id) {
Ok(()) => report.purged += 1,
Err(error) => failure("ack_purge", &error, report),
}
}
fn serve_leftover(backend: &dyn Backend, leftover: &Leftover, report: &mut SweepReport) {
if !deletable(&leftover.bucket_id, &leftover.object_name)
|| keys::validate_quarantine_key(&leftover.object_name).is_err()
{
log::error("leftover_invalid_key").emit();
report.errors += 1;
return;
}
if let Err(error) = backend.delete_object(&leftover.bucket_id, &leftover.object_name) {
return failure("delete_object", &error, report);
}
report.leftovers += 1;
if let Some(asset_id) = leftover.asset_id.as_deref().filter(|id| keys::is_uuid(id))
&& let Err(error) = backend.mark_quarantine_purged(asset_id)
{
failure("mark_quarantine_purged", &error, report);
}
}
/// Bucket literal and one of the two key shapes: the only targets the worker deletes.
fn deletable(bucket: &str, key: &str) -> bool {
let bucket_ok = !bucket.is_empty()
&& bucket
.bytes()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-');
bucket_ok && keys::validate_any_key(key).is_ok()
}
fn failure(call: &str, error: &BackendError, report: &mut SweepReport) {
report.errors += 1;
let line = log::warn("sweep_failure").stage(call).code(error.kind());
match error {
BackendError::Status(status) => line.http_status(*status).emit(),
_ => line.emit(),
}
}
+113
View File
@@ -0,0 +1,113 @@
//! Per-stage deadlines of the parent (STORAGE.md §3.4): the loop announces the stage it
//! enters; a monitor thread exits the process when a stage outlives its deadline, so a hung
//! network call or a wedged child never burns leases silently. Docker restarts the container.
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};
use crate::{exit_codes, log};
/// Deadlines per stage (review Part IV.1).
pub mod deadlines {
use std::time::Duration;
pub const CLAIM: Duration = Duration::from_secs(35);
pub const DOWNLOAD: Duration = Duration::from_secs(35);
pub const DECODE: Duration = Duration::from_secs(25);
pub const UPLOAD: Duration = Duration::from_secs(35);
pub const COMMIT: Duration = Duration::from_secs(35);
pub const SWEEP: Duration = Duration::from_secs(120);
}
#[derive(Debug, Clone)]
struct Stage {
name: &'static str,
started: Instant,
deadline: Duration,
}
/// Shared stage state; cloning shares it.
#[derive(Debug, Clone, Default)]
pub struct Watchdog {
current: Arc<Mutex<Option<Stage>>>,
}
/// Clears the stage when dropped.
pub struct StageGuard {
watchdog: Watchdog,
}
impl Drop for StageGuard {
fn drop(&mut self) {
self.watchdog.set(None);
}
}
impl Watchdog {
pub fn new() -> Self {
Self::default()
}
fn set(&self, stage: Option<Stage>) {
match self.current.lock() {
Ok(mut guard) => *guard = stage,
Err(poisoned) => *poisoned.into_inner() = stage,
}
}
/// Announces a stage; the guard ends it.
pub fn enter(&self, name: &'static str, deadline: Duration) -> StageGuard {
self.set(Some(Stage {
name,
started: Instant::now(),
deadline,
}));
StageGuard {
watchdog: self.clone(),
}
}
/// Name of the stage past its deadline, if any.
pub fn overrun(&self) -> Option<&'static str> {
let guard = match self.current.lock() {
Ok(guard) => guard,
Err(poisoned) => poisoned.into_inner(),
};
guard
.as_ref()
.filter(|stage| stage.started.elapsed() > stage.deadline)
.map(|stage| stage.name)
}
/// Starts the monitor thread: checks every second, exits the process on overrun.
pub fn spawn_monitor(&self) {
let watchdog = self.clone();
std::thread::spawn(move || {
loop {
std::thread::sleep(Duration::from_secs(1));
if let Some(stage) = watchdog.overrun() {
log::error("watchdog_overrun").stage(stage).emit();
std::process::exit(exit_codes::WATCHDOG);
}
}
});
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_stage_overruns_only_past_its_deadline_and_clears_on_drop() {
let watchdog = Watchdog::new();
assert_eq!(watchdog.overrun(), None);
{
let _guard = watchdog.enter("claim", Duration::from_millis(0));
std::thread::sleep(Duration::from_millis(5));
assert_eq!(watchdog.overrun(), Some("claim"));
}
assert_eq!(watchdog.overrun(), None);
let _guard = watchdog.enter("download", Duration::from_secs(60));
assert_eq!(watchdog.overrun(), None);
}
}
+461
View File
@@ -0,0 +1,461 @@
//! R1 proofs of the job and sweep logic on an in-memory backend and an injected decoder
//! (no network, no child): the call order, the mapping of every child outcome, the
//! lease-left rule on transport failures, the no-delete rule on a refused commit, and the
//! sweeper's deletion targets.
#![allow(clippy::unwrap_used, clippy::expect_used)]
use std::cell::RefCell;
use std::collections::HashSet;
use std::time::Duration;
use faunosteo_worker::backend::{Backend, BackendError, BackendResult, Job, Leftover, Purge};
use faunosteo_worker::config::Config;
use faunosteo_worker::decoder::Decoder;
use faunosteo_worker::job::{JobEnd, process_job, reasons, sha256_hex};
use faunosteo_worker::run::run_loop;
use faunosteo_worker::spawn::{ChildOutcome, ChildRun};
use faunosteo_worker::sweep::{SweepConfig, SweepReport, run_sweep};
use faunosteo_worker::watchdog::Watchdog;
use faunosteo_worker::{exit_codes, webp_check};
const OWNER: &str = "10000000-0000-0000-0000-00000000000a";
const ASSET: &str = "20000000-0000-0000-0000-00000000000b";
const TOKEN: &str = "30000000-0000-0000-0000-00000000000c";
fn job() -> Job {
Job {
asset_id: ASSET.to_owned(),
owner_id: OWNER.to_owned(),
bucket_id: "avatars".to_owned(),
object_key: format!("profiles/{OWNER}/{ASSET}/avatar.webp"),
quarantine_key: Some(format!("quarantine/{OWNER}/{ASSET}/upload")),
lock_token: TOKEN.to_owned(),
attempts: 1,
}
}
/// A 512 × 512 lossless WebP container that passes the parent's check.
fn clean_webp() -> Vec<u8> {
let bits: u32 = (512 - 1) | ((512 - 1) << 14);
let mut vp8l = vec![0x2f];
vp8l.extend_from_slice(&bits.to_le_bytes());
let mut body = b"WEBP".to_vec();
body.extend_from_slice(b"VP8L");
body.extend_from_slice(&(vp8l.len() as u32).to_le_bytes());
body.extend_from_slice(&vp8l);
body.push(0);
let mut out = b"RIFF".to_vec();
out.extend_from_slice(&(body.len() as u32).to_le_bytes());
out.extend_from_slice(&body);
assert!(webp_check::expect_square(&out, 512).is_ok());
out
}
#[derive(Default)]
struct Fake {
calls: RefCell<Vec<String>>,
download: RefCell<Option<BackendResult<Vec<u8>>>>,
put: RefCell<Option<BackendResult<()>>>,
complete: RefCell<Option<BackendResult<()>>>,
reject: RefCell<Option<BackendResult<()>>>,
delete: RefCell<Option<BackendResult<()>>>,
purges: RefCell<Vec<Purge>>,
leftovers: RefCell<Vec<Leftover>>,
jobs: RefCell<Vec<Job>>,
}
impl Fake {
fn record(&self, call: String) {
self.calls.borrow_mut().push(call);
}
fn calls(&self) -> Vec<String> {
self.calls.borrow().clone()
}
fn names(&self) -> Vec<String> {
self.calls()
.iter()
.map(|c| c.split(' ').next().unwrap().to_owned())
.collect()
}
}
impl Backend for Fake {
fn claim_jobs(&self, max_jobs: u32) -> BackendResult<Vec<Job>> {
self.record(format!("claim {max_jobs}"));
Ok(self.jobs.borrow_mut().drain(..).collect())
}
fn download_quarantine(&self, bucket: &str, key: &str) -> BackendResult<Vec<u8>> {
self.record(format!("download {bucket}/{key}"));
self.download
.borrow_mut()
.take()
.unwrap_or_else(|| Ok(b"input".to_vec()))
}
fn put_clean(&self, bucket: &str, key: &str, bytes: &[u8]) -> BackendResult<()> {
self.record(format!("put {bucket}/{key} {}", bytes.len()));
self.put.borrow_mut().take().unwrap_or(Ok(()))
}
fn complete(
&self,
asset_id: &str,
lock_token: &str,
byte_size: usize,
sha256: &str,
) -> BackendResult<()> {
self.record(format!(
"complete {asset_id} {lock_token} {byte_size} {sha256}"
));
self.complete.borrow_mut().take().unwrap_or(Ok(()))
}
fn reject(&self, asset_id: &str, lock_token: &str, reason: &str) -> BackendResult<()> {
self.record(format!("reject {asset_id} {lock_token} {reason}"));
self.reject.borrow_mut().take().unwrap_or(Ok(()))
}
fn delete_object(&self, bucket: &str, key: &str) -> BackendResult<()> {
self.record(format!("delete {bucket}/{key}"));
self.delete.borrow_mut().take().unwrap_or(Ok(()))
}
fn mark_quarantine_purged(&self, asset_id: &str) -> BackendResult<()> {
self.record(format!("mark_purged {asset_id}"));
Ok(())
}
fn claim_purges(&self, max_items: u32) -> BackendResult<Vec<Purge>> {
self.record(format!("claim_purges {max_items}"));
Ok(self.purges.borrow_mut().drain(..).collect())
}
fn ack_purge(&self, purge_id: &str) -> BackendResult<()> {
self.record(format!("ack {purge_id}"));
Ok(())
}
fn claim_quarantine_leftovers(
&self,
older: u64,
max_items: u32,
) -> BackendResult<Vec<Leftover>> {
self.record(format!("claim_leftovers {older} {max_items}"));
Ok(self.leftovers.borrow_mut().drain(..).collect())
}
fn expire_pending_uploads(&self, max_age: u64) -> BackendResult<i64> {
self.record(format!("expire {max_age}"));
Ok(2)
}
}
struct FakeDecoder(RefCell<Option<ChildOutcome>>);
impl FakeDecoder {
fn with(outcome: ChildOutcome) -> Self {
Self(RefCell::new(Some(outcome)))
}
}
impl Decoder for FakeDecoder {
fn decode(&self, _bytes: &[u8]) -> ChildRun {
ChildRun {
outcome: self.0.borrow_mut().take().expect("one decode per test"),
stderr: "child says\n\u{1b}[0m".to_owned(),
}
}
}
fn run_job(fake: &Fake, outcome: ChildOutcome) -> JobEnd {
process_job(fake, &FakeDecoder::with(outcome), &Watchdog::new(), &job())
}
#[test]
fn a_produced_avatar_follows_download_decode_put_complete_delete_mark() {
let fake = Fake::default();
let webp = clean_webp();
let end = run_job(&fake, ChildOutcome::Produced(webp.clone()));
assert_eq!(end, JobEnd::Completed);
assert_eq!(
fake.calls(),
vec![
format!("download avatars/quarantine/{OWNER}/{ASSET}/upload"),
format!(
"put avatars/profiles/{OWNER}/{ASSET}/avatar.webp {}",
webp.len()
),
format!(
"complete {ASSET} {TOKEN} {} {}",
webp.len(),
sha256_hex(&webp)
),
format!("delete avatars/quarantine/{OWNER}/{ASSET}/upload"),
format!("mark_purged {ASSET}"),
]
);
}
#[test]
fn a_child_rejection_rejects_the_row_with_the_mapped_code() {
for (code, expected) in [
(exit_codes::UNSUPPORTED_TYPE, reasons::UNSUPPORTED_TYPE),
(exit_codes::DIMENSIONS, reasons::DIMENSIONS),
(exit_codes::DECODE_FAILED, reasons::DECODE_FAILED),
(exit_codes::RESOURCE_LIMIT, reasons::DECODE_FAILED),
(exit_codes::INPUT_TOO_LARGE, reasons::DECODE_FAILED),
] {
let fake = Fake::default();
let end = run_job(
&fake,
ChildOutcome::Rejected {
code,
name: exit_codes::name(code),
},
);
assert_eq!(end, JobEnd::Rejected(expected));
assert_eq!(fake.names(), vec!["download", "reject"]);
assert!(fake.calls()[1].ends_with(expected), "{:?}", fake.calls());
}
}
#[test]
fn every_abnormal_child_end_is_a_processing_error() {
for outcome in [
ChildOutcome::Signalled { signal: 31 },
ChildOutcome::Timeout,
ChildOutcome::OutputOverflow,
ChildOutcome::Failed("output refused".to_owned()),
] {
let fake = Fake::default();
assert_eq!(
run_job(&fake, outcome),
JobEnd::Rejected(reasons::PROCESSING_ERROR)
);
assert_eq!(fake.names(), vec!["download", "reject"]);
}
}
#[test]
fn a_broken_child_stops_the_loop_without_touching_the_row() {
let fake = Fake::default();
let end = run_job(
&fake,
ChildOutcome::Broken {
code: exit_codes::SANDBOX_FAILED,
name: "sandbox_failed",
},
);
assert_eq!(end, JobEnd::Stop);
assert_eq!(fake.names(), vec!["download"]);
}
#[test]
fn a_refused_commit_deletes_nothing() {
let fake = Fake::default();
*fake.complete.borrow_mut() = Some(Err(BackendError::Refused("invalid_state".to_owned())));
let end = run_job(&fake, ChildOutcome::Produced(clean_webp()));
assert_eq!(end, JobEnd::Superseded);
assert_eq!(fake.names(), vec!["download", "put", "complete"]);
}
#[test]
fn a_transport_failure_leaves_the_lease_and_rejects_nothing() {
let fake = Fake::default();
*fake.download.borrow_mut() = Some(Err(BackendError::Network("refused".to_owned())));
let end = process_job(
&fake,
&FakeDecoder(RefCell::new(None)),
&Watchdog::new(),
&job(),
);
assert_eq!(end, JobEnd::LeftForLease);
assert_eq!(fake.names(), vec!["download"]);
let fake = Fake::default();
*fake.put.borrow_mut() = Some(Err(BackendError::Status(503)));
let end = run_job(&fake, ChildOutcome::Produced(clean_webp()));
assert_eq!(end, JobEnd::LeftForLease);
assert_eq!(fake.names(), vec!["download", "put"]);
}
#[test]
fn missing_or_oversized_quarantine_bytes_are_decode_failures() {
for error in [BackendError::Status(404), BackendError::Overflow] {
let fake = Fake::default();
*fake.download.borrow_mut() = Some(Err(error));
let end = process_job(
&fake,
&FakeDecoder(RefCell::new(None)),
&Watchdog::new(),
&job(),
);
assert_eq!(end, JobEnd::Rejected(reasons::DECODE_FAILED));
assert_eq!(fake.names(), vec!["download", "reject"]);
}
}
#[test]
fn a_row_with_hostile_keys_is_rejected_before_any_storage_call() {
let fake = Fake::default();
let mut bad = job();
bad.quarantine_key = Some("quarantine/../../etc/passwd".to_owned());
let end = process_job(
&fake,
&FakeDecoder(RefCell::new(None)),
&Watchdog::new(),
&bad,
);
assert_eq!(end, JobEnd::Rejected(reasons::PROCESSING_ERROR));
assert_eq!(fake.names(), vec!["reject"]);
let fake = Fake::default();
let mut bad = job();
bad.asset_id = "x".to_owned();
let end = process_job(
&fake,
&FakeDecoder(RefCell::new(None)),
&Watchdog::new(),
&bad,
);
assert_eq!(end, JobEnd::LeftForLease);
assert!(fake.calls().is_empty());
}
#[test]
fn the_sweeper_deletes_only_queued_and_leftover_keys_and_acks() {
let fake = Fake::default();
let final_key = format!("profiles/{OWNER}/{ASSET}/avatar.webp");
let quarantine = format!("quarantine/{OWNER}/{ASSET}/upload");
*fake.purges.borrow_mut() = vec![
Purge {
purge_id: "p1".into(),
bucket_id: "avatars".into(),
object_name: final_key.clone(),
},
Purge {
purge_id: "p2".into(),
bucket_id: "avatars".into(),
object_name: "profiles/x/y/avatar.webp".into(),
},
Purge {
purge_id: "p3".into(),
bucket_id: "avatars".into(),
object_name: quarantine.clone(),
},
];
*fake.leftovers.borrow_mut() = vec![
Leftover {
bucket_id: "avatars".into(),
object_name: quarantine.clone(),
asset_id: Some(ASSET.into()),
},
Leftover {
bucket_id: "avatars".into(),
object_name: final_key.clone(),
asset_id: None,
},
];
let report = run_sweep(
&fake,
&Watchdog::new(),
SweepConfig {
pending_max_age: Duration::from_secs(86_400),
quarantine_grace: Duration::from_secs(10_800),
},
);
assert_eq!(
report,
SweepReport {
expired: 2,
purged: 2,
leftovers: 1,
errors: 2
}
);
assert_eq!(
fake.calls(),
vec![
"expire 86400".to_owned(),
"claim_purges 100".to_owned(),
format!("delete avatars/{final_key}"),
"ack p1".to_owned(),
format!("delete avatars/{quarantine}"),
"ack p3".to_owned(),
"claim_leftovers 10800 100".to_owned(),
format!("delete avatars/{quarantine}"),
format!("mark_purged {ASSET}"),
]
);
let deleted: HashSet<String> = fake
.calls()
.into_iter()
.filter(|c| c.starts_with("delete"))
.collect();
assert_eq!(deleted.len(), 2);
}
#[test]
fn a_missing_object_counts_as_purged() {
let fake = Fake::default();
*fake.purges.borrow_mut() = vec![Purge {
purge_id: "p1".into(),
bucket_id: "avatars".into(),
object_name: format!("profiles/{OWNER}/{ASSET}/avatar.webp"),
}];
// The HTTP backend maps a 404 to Ok before the sweeper sees it; a real error is not acked.
*fake.delete.borrow_mut() = Some(Err(BackendError::Status(500)));
let report = run_sweep(
&fake,
&Watchdog::new(),
SweepConfig {
pending_max_age: Duration::from_secs(1),
quarantine_grace: Duration::from_secs(1),
},
);
assert_eq!(report.purged, 0);
assert_eq!(report.errors, 1);
assert!(!fake.names().contains(&"ack".to_owned()));
}
#[test]
fn once_mode_sweeps_then_claims_once_and_exits_zero_or_stops_on_a_broken_child() {
let config = Config::from_vars(|name| match name {
"SUPABASE_URL" => Some("http://kong:8000".to_owned()),
"SUPABASE_SERVICE_ROLE_KEY" => Some("k".to_owned()),
"WORKER_ONCE" => Some("1".to_owned()),
_ => None,
})
.unwrap();
let fake = Fake::default();
*fake.jobs.borrow_mut() = vec![job()];
let decoder = FakeDecoder::with(ChildOutcome::Produced(clean_webp()));
assert_eq!(
run_loop(&config, &fake, &decoder, &Watchdog::new()),
exit_codes::OK
);
assert_eq!(
&fake.names()[..5],
[
"expire",
"claim_purges",
"claim_leftovers",
"claim",
"download"
]
);
let fake = Fake::default();
*fake.jobs.borrow_mut() = vec![job()];
let decoder = FakeDecoder::with(ChildOutcome::Broken {
code: exit_codes::ENV_NOT_EMPTY,
name: "env_not_empty",
});
assert_eq!(
run_loop(&config, &fake, &decoder, &Watchdog::new()),
exit_codes::CHILD_BROKEN
);
}
+3
View File
@@ -27,6 +27,9 @@ allow = [
"CC0-1.0",
"Unlicense",
"BSL-1.0",
# Mozilla root certificate store bundled by webpki-roots (the worker's TLS roots on a
# managed Supabase); data licence, permissive.
"CDLA-Permissive-2.0",
]
confidence-threshold = 0.9
+32
View File
@@ -13,3 +13,35 @@ services:
env_file:
- .env
restart: unless-stopped
# Files worker (plan Step 8 / Rust R1, docs/development/STORAGE.md §3): the keyed
# job loop and the keyless decode child, one static binary on distroless. Started
# only with the `worker` profile: `docker compose --profile worker up -d worker`.
# It joins the Supabase compose network (never a published port) and reads its
# own env file, separate from the app's: SUPABASE_URL, SUPABASE_SERVICE_ROLE_KEY,
# WORKER_ID (chmod 600, excluded from any backup copy of env files).
worker:
container_name: faunosteo_worker
profiles: ["worker"]
build:
context: .
dockerfile: crates/worker/Dockerfile
image: faunosteo_worker:local
env_file:
- .env.worker
command: ["run"]
read_only: true
cap_drop: [ALL]
security_opt:
- no-new-privileges:true
mem_limit: 512m
cpus: 1
pids_limit: 64
networks:
- supabase
restart: unless-stopped
networks:
supabase:
external: true
# Local CLI stack: supabase_network_faunosteo; self-hosted compose: its own name.
name: ${SUPABASE_NETWORK:-supabase_network_faunosteo}
+1 -1
View File
@@ -33,7 +33,7 @@ step has built yet, with the step that owns them:
| Bilans, Clients, Animaux, Agenda, Fiches anatomiques entries and screens | Steps 9 to 16 (clients/animals, cloud reports, agenda) |
| Stat tiles (bilans créés, clients, animaux, rendez-vous) and "Derniers bilans" | once the reports and agenda tables exist; never invented numbers |
| Notification bell | notifications step |
| Avatar photo and "Modifier la photo" | Step 8 (files foundation, avatar bucket) |
| Avatar photo and "Modifier la photo" | built in Step 8 (2026-10-13): circle + "Modifier la photo" in Informations personnelles, photo in the sidebar / drawer badge and in the top bar with the name on desktop (link to Mon compte, no dropdown) |
| Bottom tab bar on mobile | with the first list screens (today only two destinations) |
| Tabs in Mon compte | optional restructuring when "Cabinet et activité" gains fields (activity type, website) |
| Password rule checklist | the mockup says 8 characters; the server policy is 12 with lower, upper and digit (config.toml); the hint text follows the server |
+11 -1
View File
@@ -18,7 +18,7 @@ by side until the plan's Step 14 decides what goes behind authentication.
| `/reset-password` | none; needs the recovery session | new password ×2; "invalid or expired link" otherwise |
| `/auth/callback` | none | landing of OAuth redirects and confirmation links; waits for the session then goes to `/app` (or `?next=`) |
| `/app` | `RequireAuth` | dashboard: greeting, profile completeness card, local tool card, account card |
| `/app/account` | `RequireAuth` | Mon compte: personal, professional, preferences (language, timezone), security (password, MFA note, sign out) |
| `/app/account` | `RequireAuth` | Mon compte: profile photo (Step 8, `STORAGE.md` §4), personal, professional, preferences (language, timezone), security (password, MFA note, sign out) |
`RequireAuth` waits for the client to read the persisted session, then sends
a visitor to `/login?next=<path>`; `next` is honoured only when it is a
@@ -126,6 +126,16 @@ VITE_AUTH_PROVIDERS=google pnpm dev
password → `/app`; sign out and sign in with the new password.
9. REST: anon cannot read profiles (`42501`), alice reads one row only
(SUPABASE.md §8).
10. Profile photo (Step 8, worker container running: `pnpm worker:image`
then `docker compose --profile worker up worker` or the container of
`scripts/avatar-e2e.sh`): Mon compte → Modifier la photo with a phone
JPEG → preview → Enregistrer → "Traitement en cours…" → the photo in the
circle, the sidebar, the drawer (mobile) and the top bar (name beside it
on desktop, alone on mobile) linking to Mon compte; a renamed PNG is
accepted, an HTML or SVG file is refused at the picker; replace, then
Supprimer la photo → confirmation → monogram; sign in as bob: nothing of
alice shows; open the object path of alice's photo in a private window
→ 400.
Automated: `pnpm test:run` (auth store, locale sync, forms, guards, shell,
dashboard, account, pgTAP 009), plus the headless end-to-end run described in
+11
View File
@@ -238,6 +238,17 @@ Definition of Done: all eight buckets exist and are private; architecture §15.1
Risks: direct-to-bucket uploads bypassing the assets row (mitigate: insert policy on storage.objects requires a matching assets row).
```
Step 8 as built (2026-10-13, `STEP8_FILES_ANALYSIS.md` D1–D9 + Rust R1,
`STORAGE.md`): the server-side processing is the Rust worker `crates/worker`
(the plan's `apps/worker` of Step 19 arrives early, in Rust, with one job
family); there is no `issue_signed_url` RPC (the Storage API signs after the
SELECT policy; the owner-only avatar is downloaded under RLS, no signed URL);
the SVG sanitiser and the user SVG upload path move to Step 13 with their
producer (the allowlist sanitiser already exists in `packages/pdf`); the
avatar pointer is derived from the single clean `assets` row (no
`profiles.avatar_asset_id`); the sheet bucket keeps its name
`anatomical-sheet-assets`.
```text
Step 9 (∥ Step 10)
Name: Professional verification
+43 -13
View File
@@ -1,12 +1,13 @@
# Rust in the Faunosteo monorepo
Status: plan R0 (foundation), 2026-10-12, on `feature/rust-r0-foundation`. Rule
and roadmap: `RUST_ARCHITECTURE_REVIEW.md` (rule "Rust by default", Part III
boundary rules, Part VI phases R0–R5, Annex B R0 scope, Annex C owner
amendments). R0 ships the toolchain, the workspace skeleton, the worker's
sandboxed child, the browser module loader and seven experimental proofs. No
application behaviour, interface, archive or database changes; the frontend
does not load the module before R2; the worker's job loop arrives at R1.
Status: plan R0 (foundation, validated 2026-10-13) and R1 (the Step 8 worker,
2026-10-13, `feature/step-08-files-foundation`). Rule and roadmap:
`RUST_ARCHITECTURE_REVIEW.md` (rule "Rust by default", Part III boundary
rules, Part IV the worker, Part VI phases R0–R5, Annex B R0 scope, Annex C
owner amendments). R0 shipped the toolchain, the workspace skeleton, the
worker's sandboxed child, the browser module loader and seven experimental
proofs; R1 ships the worker's job loop and sweeper for the avatar pipeline
(`STORAGE.md` §3). The frontend does not load the browser module before R2.
## 1. Toolchain: native on the development machine, reproducible in Docker, never on the VPS
@@ -141,11 +142,36 @@ under a 10 s CPU limit.
module each time (cache-busting import of the glue), `callCore()` parses
the envelope, `isCoreBuilt()` lets the proofs skip when `pkg/` is absent.
## 3. The worker (R0 skeleton, R1 job loop)
## 3. The worker (R0 sandboxed child, R1 job loop)
`faunosteo-worker` is one static musl binary with subcommands dispatched by
a plain `main` (no async runtime, no thread), so `decode` installs its
sandbox in a single-threaded process:
sandbox in a single-threaded process. R1 (Step 8) adds the keyed parent:
`run` polls `claim_avatar_jobs` through PostgREST every 2 s, downloads the
quarantine object, feeds it to a `decode` child, uploads the 512 × 512 WebP
at the final key, commits the row (`complete_avatar_job` with the lease
token), deletes the quarantine object, and sweeps (expired uploads, the
purge queue, quarantine leftovers) every 10 minutes. Modules: `config` (env,
secret newtype that no Debug output prints, JWT expiry), `log` (JSON lines,
field allowlist, child stderr escaped and cut at 200 bytes), `keys` (strict
four-segment key shapes validated before any URL), `backend` (trait, closed
`BackendError`), `http` (ureq 3 on rustls + ring with the bundled roots, 30 s
global / 5 s connect timeouts, no redirect, `P0001` bodies → closed
refusals, the Storage API's HTTP 400 + `statusCode` 404 → 404), `decoder`,
`job` (the STORAGE.md §3.4 mapping), `sweep` (§3.5), `watchdog` (per-stage
deadlines, exit 21 on overrun; `Broken` child → exit 20), `run`
(`WORKER_ONCE`, `--check`). Library choice recorded against the review's
Part V list: `ureq` instead of `tokio` + `reqwest`, hand-written JSON lines
instead of `tracing`, a 25-line base64url decoder instead of `base64`: the
loop is sequential, nothing needs an async runtime, and the keyed process
keeps a smaller dependency tree (34 → 84 crates, `aws-lc-rs` absent, no
cmake on Alpine). The decode child's traced syscall set did not change with
the parent's TLS code (§5.4 re-run at R1: the same seven). Image 11.6 MB
(8.7 MB at R0: ring + rustls). Tests: `crates/worker/tests/job_loop.rs` (11,
fake backend + fake decoder) and the module unit tests run natively; the
real-stack end-to-end runs through `pnpm e2e:avatar` (`STORAGE.md` §6).
The sandboxed child, unchanged from R0:
- `decode`: refuses a non-empty environment; checks `Threads: 1` in
`/proc/self/status`; disables core dumps (`PR_SET_DUMPABLE 0`,
@@ -181,8 +207,10 @@ sandbox in a single-threaded process:
child's stderr (≤ 4 KiB) is child-controlled text: R1 never logs it raw
(LRN-044).
- `run --check`: prints the resolved configuration without secrets (which
variables are set, the linked libwebp version, refused below 1.6.0). A
plain `run` does nothing before R1 and says so.
variables are set, the linked libwebp version, refused below 1.6.0, the
JWT expiry of the service key); exit 0 when startable or when no worker
variable is set at all, 2 on a partial configuration. `run` starts the loop
(STORAGE.md §3.2 for the variables).
- `version`: binary, workspace and libwebp versions.
- test hooks (`--features test-hooks`, never in a release build): fixtures
generated in memory (no binary in git), `--no-seccomp` (rlimits only,
@@ -406,8 +434,10 @@ pnpm check:all # everything above bu
- The browser-side fresh-instance mechanism (static glue URL with a
cache-busting query under Vite, or a Worker per instance) is designed, not
built: R2 builds and tests it.
- HTTPS from the worker (CA bundle of distroless, `webpki-roots`): no HTTP
client in the R0 binary; R1 adds `reqwest` and the CI HTTPS call.
- HTTPS from the worker: R1 links `ureq` with rustls and the bundled
`webpki-roots` (distroless has no CA bundle; none is needed), but every
run so far targets the local stack over plain HTTP on the Docker network;
the first managed (HTTPS) target proves the roots.
- The forge runner's cache and timings (§5.7), and the workflow files
themselves: no runner exists, none of the three workflows has ever run
(§4).
+1 -1
View File
@@ -174,7 +174,7 @@ new predicates.
| RPC | Role | Effect |
|---|---|---|
| `begin_avatar_upload(declared_mime, declared_size)` | authenticated | locks the caller's `profiles` row (`for update`, serialises a double click); rate: ≤ 10 rows of the caller created in the last hour, all statuses counted → `rate_limited`; global ceiling: sum of `declared_size` of non-deleted rows < `private.storage_ceiling()` → `storage_full`; caps (type in the list, size ≤ 5 MiB); supersedes any `pending_upload` / `uploaded` row of the caller (→ `deleted`, enqueued for purge); inserts the row with `object_key = profiles/<uid>/<id>/avatar.webp`, `quarantine_key = quarantine/<uid>/<id>/upload`; returns `{asset_id, bucket_id, quarantine_key}` |
| `begin_avatar_upload(declared_mime, declared_size)` | authenticated | locks the caller's `profiles` row (`for update`, serialises a double click); rate: ≤ 10 rows of the caller created in the last hour, all statuses counted → `rate_limited`; global ceiling: bytes present in the bucket plus declared sizes of pending rows without bytes (as built, STORAGE.md §2.1) < `private.storage_ceiling()` → `storage_full`; caps (type in the list, size ≤ 5 MiB); supersedes any `pending_upload` / `uploaded` row of the caller (→ `deleted`, enqueued for purge); inserts the row with `object_key = profiles/<uid>/<id>/avatar.webp`, `quarantine_key = quarantine/<uid>/<id>/upload`; returns `{asset_id, bucket_id, quarantine_key}` |
| `confirm_asset_upload(asset_id)` | authenticated | own row, `pending_upload`, a `storage.objects` row exists at `(bucket_id, quarantine_key)` with `metadata->>'size'` ≤ cap → `uploaded`. Kept as an explicit client signal: the worker never has to guess from Storage API internals whether a PUT finished |
| `remove_avatar()` | authenticated | every `profile_avatar` row of the caller not already `deleted` / `rejected` (clean and in flight) → `deleted`; logs `asset.avatar_removed` |
| `claim_avatar_jobs(max_jobs)` | service_role | `uploaded` rows with `locked_at is null or locked_at < now() - 10 min` and `attempts < 3`, `for update skip locked`, oldest first → sets `locked_at`, `attempts + 1`, returns the rows; a row at its 3rd expired lease becomes `rejected (processing_error)` instead. VOLATILE (a STABLE function runs read-only through PostgREST) |
+330
View File
@@ -0,0 +1,330 @@
# Files foundation: Supabase Storage, the `assets` table and the Rust worker
Status: Step 8 / Rust R1, 2026-10-13, branch `feature/step-08-files-foundation`.
Design origin: `STEP8_FILES_ANALYSIS.md` (D1 to D9 validated by the owner, D1
amended to the Rust worker of `RUST_ARCHITECTURE_REVIEW.md` Part IV). This
file is the implementation contract: table, keys, RPCs, policies, worker
behaviour and client flow are what the code and the tests pin.
First consumer: the profile photo (avatar). Every other bucket exists, closed.
---
## 1. Model
### 1.1 Statuses
```text
pending_upload ─(confirm_asset_upload)─► uploaded ─(complete_avatar_job)─► clean
│ │
│ begin supersedes / 24 h expiry ├─(reject_avatar_job, 3rd expired lease)─► rejected
▼ ▼
deleted ◄──────(remove_avatar, replacement by a newer clean row, begin supersedes)
```
`deleted` and `rejected` are terminal: the row stays (history, purge
bookkeeping) and matches no read or upload predicate.
### 1.2 `public.assets`
| Column | Type | Rule |
|---|---|---|
| `id` | uuid pk | |
| `owner_id` | uuid → `profiles.id` on delete cascade | |
| `kind` | text | `'profile_avatar'` only in Step 8 (CHECK) |
| `bucket_id` | text | `'avatars'` for that kind (CHECK) |
| `object_key` | text unique | final key, server-built: `profiles/<owner_id>/<id>/avatar.webp` |
| `quarantine_key` | text | `quarantine/<owner_id>/<id>/upload`; null once the worker confirmed the quarantine object is gone |
| `status` | `public.asset_status` | enum above |
| `declared_mime` | text | `image/jpeg`, `image/png`, `image/webp` (CHECK) |
| `declared_size` | integer | 1 … 5 242 880 (CHECK), the transport size announced by the browser |
| `byte_size`, `sha256` | integer, text | the clean output, set by `complete_avatar_job` |
| `rejection_reason` | text | closed set `unsupported_type`, `decode_failed`, `dimensions`, `processing_error`; non-null iff `rejected` (CHECK) |
| `attempts` | smallint | leases taken; the 3rd expired lease rejects with `processing_error` |
| `locked_at`, `lock_token` | timestamptz, uuid | the lease: set by `claim_avatar_jobs`, required by `complete` / `reject`, 10 min validity |
| `created_at`, `updated_at` | timestamptz | trigger `set_updated_at` |
Indexes: unique partial `assets_one_clean_avatar (owner_id) where kind =
'profile_avatar' and status = 'clean'` ("the" avatar of an account is its
single clean row: no profile column, D5); `assets_claimable (created_at)
where status = 'uploaded'`; `assets_owner_idx (owner_id, created_at desc)`.
Privileges: `revoke all` 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)`
to `authenticated` (the lease columns and `quarantine_key` stay server-side;
a client must list its columns, `select=*` is refused). RLS
`assets_select_own` (`owner_id = auth.uid()`). No INSERT / UPDATE / DELETE
policy: every write is an RPC.
### 1.3 Buckets (all private)
| Bucket | Step 8 state |
|---|---|
| `avatars` | open: SELECT + INSERT policies through `assets`, 5 MiB, `image/jpeg`, `image/png`, `image/webp` |
| `logos`, `professional-documents`, `client-files`, `animal-files`, `report-files`, `imports` | closed: row exists, `public = false`, no policy names it, no cap or MIME list yet (their step sets them) |
| `anatomical-sheet-assets` | untouched (migration `20261009100000`, read through the sheet) |
The migration upserts the rows (`on conflict (id) do update set public =
false`) so it converges on the local stack, the VPS and a managed project.
### 1.4 Keys
- Built by `begin_avatar_upload` from uuids; the user's filename never
reaches the server; one canonical name per kind (`avatar.webp`).
- Policies compare whole strings (`name = quarantine_key`, `name =
object_key`): no prefix match, no `storage.foldername()`, so PGlite and the
real stack evaluate the same expression.
- The worker validates every key it receives from the database before
putting it in a URL: exactly four segments, `profiles|quarantine`, two
uuids, `avatar.webp|upload`.
---
## 2. RPCs
Every function is `security definer`, `set search_path = ''`, authorisation
before lookup, EXECUTE revoked from `public` / `anon` / `authenticated` and
granted to the one role that calls it. Business refusals raise SQLSTATE
`P0001` with the closed code as the message (PostgREST answers 400 with
`{"code":"P0001","message":"<code>"}`); authorisation refusals raise
`42501` (403).
### 2.1 Browser (`authenticated`)
| RPC | Effect | Refusals (message) |
|---|---|---|
| `begin_avatar_upload(declared_mime text, declared_size integer) returns jsonb` | locks the caller's `profiles` row (`for update`); checks the type and the size; rate: at most 10 rows created by the caller in the last hour, every status counted; per-account budget: the caller's bytes present under `profiles/<uid>/` and `quarantine/<uid>/` plus one cap below `private.account_byte_budget()` (64 MiB) → `rate_limited`; global ceiling: the bytes present in the bucket (sum of sized `storage.objects`, whatever each row's state) plus one 5 MiB cap per `pending_upload` row whose bytes have not landed, below `private.storage_ceiling()` (10 GiB): a claim dies with its row, so begin spam pins nothing; residuals: about 100 to 160 colluding confirmed accounts at their budget fill the ceiling (bytes still streaming for a superseded row weigh 0 until they land, bounded by rate × cap), about 2 000 accounts each keeping one bytes-less pending claim alive do the same, and a burst of accounts fills the filesystem quota, the hard bound; supersedes the caller's `pending_upload` / `uploaded` rows (→ `deleted`); inserts the row; returns `{"asset_id", "bucket_id", "quarantine_key"}` | `unsupported_type`, `too_large`, `rate_limited`, `storage_full` |
| `confirm_asset_upload(asset_id uuid) returns void` | active account; own `pending_upload` row with a `storage.objects` row at `(bucket_id, quarantine_key)` whose `metadata->>'size'` is set and within the cap → `uploaded` | `not_found` (unknown or not own), `invalid_state` (not pending), `upload_missing` (no object yet, or an object without a size: fail closed), `too_large` (object over the cap or over the declared size: the row stays pending and expires after 24 h) |
| `remove_avatar() returns integer` | every `profile_avatar` row of the caller in `pending_upload` / `uploaded` / `clean` → `deleted`; returns the count; logs `asset.avatar_removed` | none (0 rows is fine) |
`auth.uid() is null` → `42501` on all three.
### 2.2 Worker (`service_role`)
| RPC | Effect |
|---|---|
| `claim_avatar_jobs(max_jobs integer) returns table (asset_id, owner_id, bucket_id, object_key, quarantine_key, lock_token, attempts)` | first rejects (`processing_error`) every `uploaded` row whose lease expired with `attempts >= 3`; then claims `uploaded` rows with `locked_at is null or locked_at < now() - 10 min` and `attempts < 3`, oldest first, `for update skip locked`, at most `max_jobs` (capped at 10): `locked_at = now()`, `attempts + 1`, fresh `lock_token`. VOLATILE (a STABLE function would run read-only through PostgREST) |
| `complete_avatar_job(asset_id uuid, lock_token uuid, byte_size integer, sha256 text) returns void` | guard: `status = 'uploaded'` and the token matches, else `invalid_state`. In one transaction: the owner's previous `clean` avatar → `deleted` (trigger enqueues its object 48 h later), the row → `clean` with `byte_size` / `sha256`, lease cleared; logs `asset.avatar_set` (actor null = worker, target = owner) |
| `reject_avatar_job(asset_id uuid, lock_token uuid, reason text) returns void` | same guard; `reason` in the closed set else `invalid_reason`; → `rejected`; logs `asset.rejected` with the reason |
| `mark_quarantine_purged(asset_id uuid) returns void` | `clean` row → `quarantine_key = null` (after the worker deleted the quarantine object); other statuses: no-op |
| `claim_purges(max_items integer) returns table (purge_id, bucket_id, object_name)` | queue rows with `not_before <= now()` and no live lease (`locked_at` null or older than 10 min), `for update skip locked`, at most `max_items` (capped at 100); sets `locked_at` |
| `ack_purge(purge_id uuid) returns void` | deletes the queue row (a missing object counted as success by the worker) |
| `claim_quarantine_leftovers(older_than_seconds integer, max_items integer) returns table (bucket_id, object_name, asset_id)` | `storage.objects` under `quarantine/` in `avatars`, created before `now() - older_than`, whose row is not `pending_upload` / `uploaded` (anti-join on `quarantine_key`); `asset_id` is the matching row when one exists (the worker calls `mark_quarantine_purged` for a `clean` one) |
| `expire_pending_uploads(max_age_seconds integer) returns integer` | `pending_upload` rows older than the age → `deleted` (trigger enqueues their keys); returns the count |
The three stale-task guards together: a superseded or removed row is
`deleted` (never `uploaded`, so no `complete`); a `complete` from an expired
lease carries a stale `lock_token` and is refused even while the row is
still `uploaded`; the purge of an old key never touches a newer row's
object (keys embed the asset id).
### 2.3 `storage.objects` policies (bucket `avatars`)
| Policy | Command | Expression |
|---|---|---|
| `avatars_read` | SELECT, `authenticated` | `bucket_id = 'avatars' and private.can_read_avatar_object((select auth.uid()), name)` = a `clean` row of the caller with `object_key = name` |
| `avatars_upload` | INSERT, `authenticated` | `bucket_id = 'avatars' and private.can_upload_avatar_object((select auth.uid()), name)` = a `pending_upload` row of the caller with `quarantine_key = name` |
No UPDATE / DELETE policy exists for any API role. The pgTAP census asserts
that every policy on `storage.objects` pins exactly one bucket literal and
counts them: 2 on `avatars`, 1 on `anatomical-sheet-assets`, 0 elsewhere,
total 3.
### 2.4 Purge queue (`private.object_purge_queue`)
`(id, bucket_id, object_name, not_before, enqueued_at, locked_at, attempts)`,
unique on `(bucket_id, object_name)` (a second enqueue keeps the earliest
`not_before`). Fed by triggers on `assets`:
| Transition | `object_key` | `quarantine_key` |
|---|---|---|
| → `deleted` | `now() + 48 h` | `now() + 1 h` |
| → `rejected` | `now() + 1 h` (a clean object may exist after a PUT before a 3rd expired lease) | `now() + 1 h` |
| row DELETE (account cascade) | `now() + 48 h` | `now() + 1 h` |
A `clean` row's `object_key` is never enqueued (test). The 48 h grace is the
backup invariant of §5.
---
## 3. The worker (`crates/worker`, container `faunosteo_worker`)
### 3.1 Process model
One static musl binary, two roles in one container:
- `faunosteo-worker run`: the keyed parent. Holds `SUPABASE_SERVICE_ROLE_KEY`,
talks to PostgREST and the Storage API over HTTP, never decodes a pixel.
- `faunosteo-worker decode`: the child spawned per job by the parent with an
empty environment and piped stdin/stdout (R0 sandbox unchanged: `Threads:
1`, no core dump, `RLIMIT_AS` 512 MiB, `RLIMIT_CPU` 10 s / 12 s, seccomp
allowlist, 5 MiB input, 512 KiB output, 20 s deadline). It sees no key,
no file, no network.
The parent checks the child's output at container level only
(`webp_check`: RIFF structure, chunk allowlist, 512 × 512 canvas, no
metadata chunk), hashes it and uploads it.
### 3.2 Configuration (environment of the parent only)
| Variable | Required | Default | Meaning |
|---|---|---|---|
| `SUPABASE_URL` | yes | | origin of the stack (`http://supabase_kong_faunosteo:8000` from the Supabase compose network, `http://127.0.0.1:54321` natively). Cleartext `http://` is accepted only for `localhost`, a dotted-quad loopback address, or a single-label host starting with a letter (a Docker service name; inside the musl container Docker's `ndots:0` makes musl skip the search list; glibc, natively, may still try search domains on a mistyped name, so prefer loopback there); any other host must be `https://`, the service key travels in the headers |
| `SUPABASE_SERVICE_ROLE_KEY` | yes | | the service key; never printed; the JWT `exp` claim, when the key is a JWT, is logged at start and warned about 30 days ahead |
| `WORKER_ID` | no | `worker-<pid>` | tag of every log line |
| `WORKER_POLL_SECONDS` | no | `2` | idle interval between claims |
| `WORKER_SWEEP_SECONDS` | no | `600` | interval of the sweeper |
| `WORKER_QUARANTINE_GRACE_SECONDS` | no | `10800` | age before a quarantine leftover is deleted (above the 2 h validity of a signed upload URL) |
| `WORKER_PENDING_MAX_AGE_SECONDS` | no | `86400` | age before a never-confirmed `pending_upload` row is expired |
| `WORKER_ONCE` | no | unset | `1` = one claim pass and one sweep, then exit (tests) |
`faunosteo-worker run --check` prints the resolved configuration without
any secret value. Exit 0 when the worker could start (libwebp ≥ 1.6.0, both
required variables set, URL parseable) or when no worker variable is set at
all (an unconfigured image is a valid artifact check, `scripts/check-all.sh`);
a partial or invalid configuration exits 2 naming the variable, a too-old
libwebp exits 18.
### 3.3 HTTP endpoints used (service key: `apikey` and `Authorization: Bearer`)
| Call | Request |
|---|---|
| RPC | `POST <url>/rest/v1/rpc/<name>`, JSON body, `Content-Type: application/json`, `Prefer: return=representation` |
| download quarantine | `GET <url>/storage/v1/object/<bucket>/<key>`, body read with a 5 MiB + 1 cap; a body over the cap (impossible through the bucket cap and `confirm_asset_upload`, kept as a guard) is a content refusal: the row is rejected `decode_failed` and the worker logs `download_overflow` |
| upload clean | `POST <url>/storage/v1/object/<bucket>/<key>`, `Content-Type: image/webp`, `x-upsert: true` (idempotent on retry) |
| delete object | `DELETE <url>/storage/v1/object/<bucket>/<key>` (a missing object counts as done: HTTP 404, or the HTTP 400 with `statusCode` 404 this Storage API version answers) |
Every call: 30 s global timeout, 5 s connect timeout, no redirect follow, no
proxy (the environment's `HTTP_PROXY` is ignored on purpose),
`http_status_as_error(false)` so each status is classified by the caller.
Log lines carry the closed error kind and the HTTP status, never the library's
error text.
Keys are validated (§1.4) before any URL is built.
### 3.4 One job
```text
claim_avatar_jobs(1) stage claim deadline 35 s
GET quarantine stage download 35 s 404 → reject decode_failed (bytes never landed; the row said uploaded)
spawn decode child, feed bytes stage decode 25 s
Produced(webp) → POST clean (upsert) → complete_avatar_job(asset, token, size, sha256)
stage upload 35 s, stage commit 35 s
→ DELETE quarantine → mark_quarantine_purged (best effort: the sweeper finishes it)
Rejected{code} → reject_avatar_job(asset, token, code) code ∈ unsupported_type | decode_failed | dimensions | resource_limit→decode_failed
Signalled / Timeout / OutputOverflow / Failed
→ reject_avatar_job(asset, token, 'processing_error') (logged with signal number / detail)
Broken{code} → log level=error event=child_broken, leave the lease (the row is retried, 3rd lease rejects), stop the loop with exit code 20
complete refused (invalid_state: superseded, removed, stale lease)
→ log, delete nothing: the purge queue owns every object of a terminal row
```
Every network failure in a stage leaves the row to its lease (re-claimed
after 10 min, rejected `processing_error` on the 3rd expiry). A stage that
exceeds its deadline makes the watchdog thread exit the process (code 21);
Docker's restart policy brings it back.
### 3.5 Sweeper (every `WORKER_SWEEP_SECONDS`, and once at start)
1. `expire_pending_uploads(WORKER_PENDING_MAX_AGE_SECONDS)`.
2. `claim_purges(100)` → DELETE each object (404 = done) → `ack_purge`.
3. `claim_quarantine_leftovers(WORKER_QUARANTINE_GRACE_SECONDS, 100)` →
DELETE each object → `mark_quarantine_purged(asset_id)` when present.
Those are the only deletions the worker ever makes: a key it did not get
from the queue or the leftover query is never deleted.
### 3.6 Logs
One JSON object per line on stdout, fields from an allowlist only: `ts`,
`level`, `event`, `worker_id`, `asset_id`, `stage`, `code`, `signal`,
`http_status`, `duration_ms`, `count`, `detail` (worker-authored text only).
The child's stderr is child-controlled: it is logged under `child_stderr`
JSON-escaped and truncated to 200 bytes, never raw. No key, no URL with a
token, no user content.
---
## 4. Client flow (browser, anon key + user JWT)
1. Pick: `<input type="file" accept="image/jpeg,image/png,image/webp">`.
The first bytes are sniffed (`FF D8 FF`, PNG signature, `RIFF….WEBP`);
anything else, SVG included, is refused with `unsupported_type` before
any decode. `File.size` over 20 MiB → `too_large`.
2. Transport: `createImageBitmap` → canvas with the longest side at 1024 px
→ `toBlob('image/jpeg', 0.9)`; over 5 MiB after that → `too_large`.
The preview shows that blob. Nothing uploaded yet.
3. "Enregistrer la photo": `begin_avatar_upload('image/jpeg', blob.size)` →
`storage.from('avatars').upload(quarantine_key, blob, { upsert: false,
contentType: 'image/jpeg' })` → `confirm_asset_upload(asset_id)`.
4. Poll the own row every 1 s, 30 s at most: `clean` → download
`object_key` (`storage.from('avatars').download`), keep a blob URL in the
auth store; `rejected` → message per code; still `uploaded` after 30 s →
"processing, come back later" (the next mount polls again).
5. "Supprimer la photo": confirmation dialog → `remove_avatar()` → monogram.
Closed failure kinds the UI phrases (`AssetFailure.kind`): `too_large`,
`unsupported_type`, `rate_limited`, `storage_full`, `upload_missing`,
`invalid_state`, `not_found`, `network`, `unknown`; rejection codes of a row:
`unsupported_type`, `decode_failed`, `dimensions`, `processing_error`.
No signed URL, no bearer token: the download runs under the SELECT policy
and stops at the row flip (D7). The production CSP already allows `blob:`
images; `connect-src` must name the Supabase origin (DEPLOY.md).
---
## 5. Operations
### 5.1 Reconciliation (read-only, operator)
```sql
-- objects without a row (any status) and rows whose object is missing
select o.bucket_id, o.name from storage.objects o
where o.bucket_id = 'avatars'
and not exists (select 1 from public.assets a where a.object_key = o.name or a.quarantine_key = o.name);
select a.id, a.object_key from public.assets a
where a.status = 'clean'
and not exists (select 1 from storage.objects o where o.bucket_id = a.bucket_id and o.name = a.object_key);
```
Deletions stay row-driven (queue and leftover sweep); the query only reports.
### 5.2 Backups (self-hosted, file backend)
Order: database dump first (whole instance, `storage` schema included),
files second, within 48 h of each other. With the 48 h purge grace every
`clean` row of the dump still has its object in the file snapshot (the
invariant the restore checks with the second query above). Restore: stop
the worker, restore the database, restore the volume, run the
reconciliation, start the worker. Tooling (restic, retention, drill) is the
deployment runbook's (plan Step 27).
### 5.3 Managed migration
Same migrations (`supabase db push`); rows (`auth.users`, `public`), then the
objects driven by the rows (`clean` rows and confirmed sheet assets:
download, verify sha256, upload at the same key, verify), write freeze while
copying (worker stopped), reconciliation on the destination, switch
`SUPABASE_URL` and the keys of the app and the worker.
---
## 6. Development
```bash
pnpm db:start # local stack (migrations + seed)
pnpm worker:image # faunosteo_worker:dev
docker run --rm --read-only --cap-drop ALL --security-opt no-new-privileges:true \
--network supabase_network_faunosteo \
-e SUPABASE_URL=http://supabase_kong_faunosteo:8000 \
-e SUPABASE_SERVICE_ROLE_KEY="$(pnpm -s supabase status -o env | sed -n 's/^SERVICE_ROLE_KEY="\(.*\)"/\1/p')" \
faunosteo_worker:dev run
pnpm --filter @faunosteo/supabase test:e2e # real-stack end-to-end (needs the worker above)
pnpm e2e:avatar # scripts/avatar-e2e.sh: the three lines above in one, worker started with a 5 s sweep and no quarantine grace
```
`docker compose --profile worker up worker` runs the same container from
`docker-compose.yml` with `.env.worker` (`SUPABASE_URL`,
`SUPABASE_SERVICE_ROLE_KEY`, `WORKER_ID`; `chmod 600`, never in the app
`.env`, never in a backup of env files).
+22 -2
View File
@@ -16,7 +16,9 @@ supabase/
│ ├─ 20261008110000_anatomical_sheets.sql species, sheets, immutable versions, assets, lifecycle RPCs
│ ├─ 20261008110100_private_execute_hardening.sql EXECUTE revoked from PUBLIC on the Step 3 helpers
│ ├─ 20261009100000_sheet_asset_storage.sql bucket anatomical-sheet-assets + read policy through the sheet
│ └─ 20261010100000_sheet_document_schema_v2.sql document schema 2: references, review_status, count_range checks
│ ├─ 20261010100000_sheet_document_schema_v2.sql document schema 2: references, review_status, count_range checks
│ ├─ 20261011100000_profiles_professional.sql practice_name, professional_title, siret
│ └─ 20261013100000_files_foundation.sql assets table, seven private buckets, avatar policies through assets, purge queue, browser + worker RPCs (STORAGE.md)
├─ seed.sql LOCAL dev accounts + first owner (never run in prod)
├─ seed/anatomical-sheets/equine/ official equine sheet as a DRAFT, generated by `pnpm import:equine`
│ ├─ legacy-sources/ the historical Horse.svg, Horse_head.svg, pathologies.json, vertebrae.ts (importer input)
@@ -28,7 +30,9 @@ packages/supabase/
├─ src/client.ts createBrowserClient() (anon key, PKCE)
├─ src/database.types.ts `supabase gen types` output
├─ src/anatomical-sheets.ts publishSheetVersion(): Zod + integrity gate before the RPC
└─ tests/db/ PGlite harness: runs the pgTAP files without Docker
├─ src/assets.ts avatar flow: begin / quarantine upload / confirm / remove / own state / download, closed AssetFailure kinds
├─ tests/db/ PGlite harness: runs the pgTAP files without Docker
└─ tests/e2e/ real-stack end-to-end (avatar flow with the worker container), `pnpm e2e:avatar`
packages/domain/
└─ src/anatomical-sheet/ canonical sheet document (Zod), validateSheetDocument, createEmptySheetDocument
```
@@ -109,6 +113,22 @@ uploading, keeps objects already present, and calls
read policy ignores a row whose upload was never confirmed, so a user who
registers a known hash on their own sheet opens nothing.
### 3.2 Files worker (Storage, Step 8)
User files land in private buckets and are processed by the Rust worker
(`crates/worker`, `docs/development/STORAGE.md`). Against the local stack:
```bash
pnpm worker:image # faunosteo_worker:dev
pnpm e2e:avatar # starts the container on supabase_network_faunosteo, runs tests/e2e, stops it
docker compose --profile worker up worker # the same container from docker-compose.yml, reading .env.worker
```
`.env.worker` (not editable by the assistant): `SUPABASE_URL=http://supabase_kong_faunosteo:8000`,
`SUPABASE_SERVICE_ROLE_KEY=<the service key of pnpm supabase status>`, `WORKER_ID=dev`.
The service key stays in that file and in the shell; it never reaches the
app `.env`, env.js or a commit.
Seed accounts (local only, password `LocalDev-only-password-1`):
| Email | Role |
+3 -1
View File
@@ -19,7 +19,9 @@ Status: Step 0 deliverable, 2026-10-08. Current state: 32 files under
| SQL/TS contract | generated types diff check | CI | every PR touching `supabase/` |
| Rust core and facade | `cargo test` (unit + fixture tests reading the repository JSON in place), `clippy -D warnings`, `cargo deny`; ts-rs types drift check | `crates/*`, `pnpm rust:check` (native, `scripts/rust-check.sh`; same file in the builder image), `pnpm check:all` | before every Rust commit and every merge; CI job `rust` prepared, no runner yet |
| Rust/TS contract | vitest proofs on the built module: byte equality with `JSON.stringify(…, null, 2)` on the real documents, optional/nullable shape read by the current Zod schemas, panic and stack traps, served size budget | `packages/core-wasm/tests` (skipped with a notice when `pkg/` is absent) | `pnpm check:all` (after the native `wasm:build`); CI `check` prepared |
| Worker sandbox | integration tests against the musl binary that ships: fixture corpus through the full sandbox, refusals, SIGSYS on a denied syscall, output cap, `RLIMIT_AS` abort, `RLIMIT_CPU`, deadline; seccomp allowlist derived by `strace` and compared | `crates/worker/tests`, Dockerfile stage `test` (`pnpm worker:test`, Docker only, never native) | every `crates/worker` commit, `pnpm check:all`; CI `docker` job prepared |
| Worker sandbox | integration tests against the musl binary that ships: fixture corpus through the full sandbox, refusals, SIGSYS on a denied syscall, output cap, `RLIMIT_AS` abort, `RLIMIT_CPU`, deadline; seccomp allowlist derived by `strace` and compared | `crates/worker/tests/sandbox.rs`, Dockerfile stage `test` (`pnpm worker:test`, Docker only, never native) | every `crates/worker` commit, `pnpm check:all`; CI `docker` job prepared |
| Worker job loop | native unit tests on a fake backend and a fake decoder: call order claim → download → decode → upload → commit → purge, every child outcome mapped to a closed code, no deletion on a refused commit, lease left on transport failures, sweeper targets and acks, key validation, config and secret redaction | `crates/worker/tests/job_loop.rs`, `crates/worker/src/*` unit tests (`pnpm rust:check`) | every Rust commit |
| Files e2e (real stack) | the worker container against the local Supabase stack: clean upload and owner-only download (Bob, anon, quarantine, direct final-key write refused), replacement and purge after the grace (queue clock shifted through psql), late signed-upload bytes swept, hostile uploads rejected with closed codes, removal and drained queue | `packages/supabase/tests/e2e/avatar-flow.e2e.ts` (`pnpm e2e:avatar`, `scripts/avatar-e2e.sh`) | every Storage, worker or avatar change; not in `check:all` (needs the stack) |
Browser-only code (`svgToPng`, `DOMParser` in the importer) runs under
happy-dom where sufficient and under Playwright when real rendering matters
+1
View File
@@ -94,6 +94,7 @@ works as `corepack pnpm <cmd>`.
| `pnpm build` | typecheck + Vite build in every package |
| `pnpm check` | typecheck, test:run, lint, build in sequence (the Node gate) |
| `pnpm check:all` | the whole pre-merge gate: native Rust check, types drift, wasm build, `pnpm check`, application and worker images (`docs/development/RUST.md` §4; no CI runs it yet) |
| `pnpm e2e:avatar` | real-stack end-to-end of the avatar flow: worker container against the local Supabase stack + `packages/supabase/tests/e2e` (`docs/development/STORAGE.md` §6; needs `pnpm db:start`) |
| `pnpm format` | Prettier on the whole repository |
Package-scoped: `pnpm --filter @faunosteo/app <script>`.
+3 -1
View File
@@ -11,6 +11,7 @@
},
"scripts": {
"dev": "pnpm --filter @faunosteo/app dev",
"dev:lan": "bash scripts/dev-lan.sh",
"preview": "pnpm --filter @faunosteo/app preview",
"build": "pnpm -r build",
"typecheck": "pnpm -r typecheck",
@@ -37,7 +38,8 @@
"wasm:build": "bash scripts/wasm-build.sh",
"wasm:build:docker": "bash docker/rust-run.sh bash scripts/wasm-build.sh",
"worker:test": "docker build -f crates/worker/Dockerfile --target test -t faunosteo_worker:test .",
"worker:image": "docker build -f crates/worker/Dockerfile -t faunosteo_worker:dev ."
"worker:image": "docker build -f crates/worker/Dockerfile -t faunosteo_worker:dev .",
"e2e:avatar": "bash scripts/avatar-e2e.sh"
},
"devDependencies": {
"pnpm": "10.13.1",
+3 -1
View File
@@ -17,11 +17,13 @@
"test:run": "vitest run",
"lint": "eslint . --max-warnings 0",
"build": "tsc -p tsconfig.json --noEmit",
"test:e2e": "vitest run --config vitest.e2e.config.ts",
"assets:upload": "node scripts/upload-sheet-assets.ts"
},
"dependencies": {
"@faunosteo/domain": "workspace:*",
"@supabase/supabase-js": "^2.100.0"
"@supabase/supabase-js": "^2.100.0",
"zod": "^3.24.1"
},
"devDependencies": {
"@electric-sql/pglite": "^0.3.0",
+247
View File
@@ -0,0 +1,247 @@
import { z } from 'zod'
import type { FaunosteoClient } from './client'
import type { Enums, Tables } from './database.types'
/**
* Browser side of the files foundation (docs/development/STORAGE.md §2.1,
* §4): the three avatar RPCs, the quarantine upload under the INSERT policy,
* the own-row read for polling and the download under the SELECT policy.
* Every failure is folded into a closed kind the UI knows how to phrase.
*/
export type AssetStatus = Enums<'asset_status'>
/** Columns the owner may read (column grant); the lease columns stay server-side. */
export const ASSET_OWNER_COLUMNS =
'id, owner_id, kind, bucket_id, object_key, status, declared_mime, declared_size, byte_size, sha256, rejection_reason, created_at, updated_at'
export type AssetRow = Pick<
Tables<'assets'>,
| 'id'
| 'owner_id'
| 'kind'
| 'bucket_id'
| 'object_key'
| 'status'
| 'declared_mime'
| 'declared_size'
| 'byte_size'
| 'sha256'
| 'rejection_reason'
| 'created_at'
| 'updated_at'
>
export const AVATAR_REJECTION_CODES = [
'unsupported_type',
'decode_failed',
'dimensions',
'processing_error',
] as const
export type AvatarRejectionCode = (typeof AVATAR_REJECTION_CODES)[number]
export function isAvatarRejectionCode(
value: string | null | undefined,
): value is AvatarRejectionCode {
return (AVATAR_REJECTION_CODES as readonly string[]).includes(value ?? '')
}
export const ASSET_FAILURE_KINDS = [
'too_large',
'unsupported_type',
'rate_limited',
'storage_full',
'upload_missing',
'invalid_state',
'not_found',
'network',
'unknown',
] as const
export type AssetFailureKind = (typeof ASSET_FAILURE_KINDS)[number]
export class AssetFailure extends Error {
readonly kind: AssetFailureKind
constructor(kind: AssetFailureKind, cause?: unknown) {
super(`asset: ${kind}`, { cause })
this.name = 'AssetFailure'
this.kind = kind
}
}
/** Codes the RPCs raise as SQLSTATE P0001 messages (STORAGE.md §2). */
const RPC_MESSAGE_KINDS: ReadonlySet<string> = new Set([
'too_large',
'unsupported_type',
'rate_limited',
'storage_full',
'upload_missing',
'invalid_state',
'not_found',
])
interface PostgrestLikeError {
code?: string | null
message?: string
status?: number
}
/**
* Folds a PostgREST / storage / fetch error into a closed kind. `42501`
* (authorisation) becomes `unknown` on purpose: the UI never reaches an RPC
* without a session, so it is a bug, not a state to phrase.
*/
export function classifyAssetError(error: unknown): AssetFailureKind {
if (error instanceof AssetFailure) return error.kind
if (error instanceof TypeError) return 'network'
const e = (error ?? {}) as PostgrestLikeError
if (e.code === 'P0001' && e.message && RPC_MESSAGE_KINDS.has(e.message)) {
return e.message as AssetFailureKind
}
if (e.status === undefined || e.status === 0) {
return e.code ? 'unknown' : 'network'
}
return 'unknown'
}
function fail(error: unknown): never {
throw new AssetFailure(classifyAssetError(error), error)
}
const BeginResultSchema = z.object({
asset_id: z.string().uuid(),
bucket_id: z.literal('avatars'),
quarantine_key: z.string().min(1),
})
export interface BeginAvatarUploadInput {
declaredMime: 'image/jpeg' | 'image/png' | 'image/webp'
declaredSize: number
}
export interface AvatarUploadTicket {
assetId: string
bucketId: 'avatars'
quarantineKey: string
}
export async function beginAvatarUpload(
client: FaunosteoClient,
input: BeginAvatarUploadInput,
): Promise<AvatarUploadTicket> {
const { data, error } = await client.rpc('begin_avatar_upload', {
declared_mime: input.declaredMime,
declared_size: input.declaredSize,
})
if (error) fail(error)
const parsed = BeginResultSchema.safeParse(data)
if (!parsed.success) throw new AssetFailure('unknown', parsed.error)
return {
assetId: parsed.data.asset_id,
bucketId: parsed.data.bucket_id,
quarantineKey: parsed.data.quarantine_key,
}
}
export interface QuarantineUploadInput {
bucketId: string
quarantineKey: string
blob: Blob
}
/** Direct upload with the user JWT; only the caller's pending quarantine key passes the INSERT policy. */
export async function uploadToQuarantine(
client: FaunosteoClient,
input: QuarantineUploadInput,
): Promise<void> {
const { error } = await client.storage
.from(input.bucketId)
.upload(input.quarantineKey, input.blob, {
upsert: false,
contentType: 'image/jpeg',
})
if (error) throw new AssetFailure(classifyStorageUploadError(error), error)
}
/**
* storage-js answers with a StorageApiError (status + statusCode) for a
* refused upload; a policy refusal (the row is no longer pending, or the key
* is not ours) is a state problem, not a network one.
*/
function classifyStorageUploadError(error: {
message: string
status?: number
statusCode?: string
}): AssetFailureKind {
if (/already exists|duplicate/i.test(error.message)) return 'invalid_state'
if (error.status === 413 || error.statusCode === '413') return 'too_large'
if (error.status === undefined) return 'network'
return 'invalid_state'
}
export async function confirmAssetUpload(
client: FaunosteoClient,
assetId: string,
): Promise<void> {
const { error } = await client.rpc('confirm_asset_upload', {
asset_id: assetId,
})
if (error) fail(error)
}
/** Every avatar row of the caller still alive → deleted; returns the count. */
export async function removeAvatar(client: FaunosteoClient): Promise<number> {
const { data, error } = await client.rpc('remove_avatar')
if (error) fail(error)
return typeof data === 'number' ? data : 0
}
export interface AvatarState {
/** The single clean avatar, if any. */
clean: AssetRow | null
/** Newest pending / uploaded row newer than the clean one. */
inFlight: AssetRow | null
/** Newest rejected row newer than the clean one (shown once). */
lastRejected: AssetRow | null
}
const IN_FLIGHT: ReadonlySet<AssetStatus> = new Set(['pending_upload', 'uploaded'])
/** Reduces the newest-first rows of an owner to the three states the UI shows. */
export function reduceAvatarRows(rows: readonly AssetRow[]): AvatarState {
const clean = rows.find((r) => r.status === 'clean') ?? null
const newerThanClean = (r: AssetRow) =>
clean === null || r.created_at > clean.created_at
const inFlight =
rows.find((r) => IN_FLIGHT.has(r.status) && newerThanClean(r)) ?? null
const lastRejected =
rows.find((r) => r.status === 'rejected' && newerThanClean(r)) ?? null
return { clean, inFlight, lastRejected }
}
export async function fetchOwnAvatarState(
client: FaunosteoClient,
userId: string,
): Promise<AvatarState> {
const { data, error } = await client
.from('assets')
.select(ASSET_OWNER_COLUMNS)
.eq('owner_id', userId)
.eq('kind', 'profile_avatar')
.in('status', ['clean', 'pending_upload', 'uploaded', 'rejected'])
.order('created_at', { ascending: false })
.limit(5)
if (error) fail(error)
return reduceAvatarRows((data ?? []) as AssetRow[])
}
/** Download under the SELECT policy: only the owner's clean object answers. */
export async function downloadAvatar(
client: FaunosteoClient,
objectKey: string,
): Promise<Blob> {
const { data, error } = await client.storage
.from('avatars')
.download(objectKey)
if (error || !data) fail(error ?? new Error('empty download'))
return data
}
+135
View File
@@ -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'],
+23
View File
@@ -44,6 +44,29 @@ export {
/** owner | admin | moderator | support. Not a professional status. */
export { fetchOwnPlatformRoles, type PlatformRole } from './platform-roles'
export { publishSheetVersion } from './anatomical-sheets'
export {
ASSET_FAILURE_KINDS,
ASSET_OWNER_COLUMNS,
AVATAR_REJECTION_CODES,
AssetFailure,
beginAvatarUpload,
classifyAssetError,
confirmAssetUpload,
downloadAvatar,
fetchOwnAvatarState,
isAvatarRejectionCode,
reduceAvatarRows,
removeAvatar,
uploadToQuarantine,
type AssetFailureKind,
type AssetRow,
type AssetStatus,
type AvatarRejectionCode,
type AvatarState,
type AvatarUploadTicket,
type BeginAvatarUploadInput,
type QuarantineUploadInput,
} from './assets'
/** Row of public.species. */
export type Species = Tables<'species'>
+225
View File
@@ -0,0 +1,225 @@
import {
AssetFailure,
beginAvatarUpload,
classifyAssetError,
confirmAssetUpload,
downloadAvatar,
fetchOwnAvatarState,
reduceAvatarRows,
removeAvatar,
uploadToQuarantine,
type AssetRow,
} from '../src'
import type { FaunosteoClient } from '../src'
/**
* Stub of the client surface the assets module touches: rpc, the assets
* select chain, storage upload / download. The RPC bodies are covered by
* supabase/tests/010_files_foundation.sql; here the point is the call shape
* and the closed failure kinds.
*/
function stubClient(options: {
rpc?: Record<string, { data?: unknown; error?: unknown }>
rows?: AssetRow[]
selectError?: unknown
upload?: { error?: { message: string; status?: number; statusCode?: string } | null }
download?: { data?: Blob | null; error?: unknown }
} = {}) {
const calls: Array<[string, unknown]> = []
const query: Record<string, unknown> = {}
const chain = {
select: (cols: string) => {
query.select = cols
return chain
},
eq: (col: string, value: unknown) => {
query[`eq:${col}`] = value
return chain
},
in: (col: string, values: unknown) => {
query[`in:${col}`] = values
return chain
},
order: (col: string, opts: unknown) => {
query.order = [col, opts]
return chain
},
limit: async (n: number) => {
query.limit = n
return options.selectError
? { data: null, error: options.selectError }
: { data: options.rows ?? [], error: null }
},
}
const storageCalls: Array<[string, unknown[]]> = []
const client = {
rpc: async (name: string, args?: unknown) => {
calls.push([name, args])
const r = options.rpc?.[name] ?? {}
return { data: r.data ?? null, error: r.error ?? null }
},
from: () => chain,
storage: {
from: (bucket: string) => ({
upload: async (...args: unknown[]) => {
storageCalls.push([`upload:${bucket}`, args])
return { data: null, error: options.upload?.error ?? null }
},
download: async (...args: unknown[]) => {
storageCalls.push([`download:${bucket}`, args])
return {
data: options.download?.data ?? null,
error: options.download?.error ?? null,
}
},
}),
},
}
return { client: client as unknown as FaunosteoClient, calls, query, storageCalls }
}
function row(patch: Partial<AssetRow>): AssetRow {
return {
id: 'a1',
owner_id: 'u1',
kind: 'profile_avatar',
bucket_id: 'avatars',
object_key: 'profiles/u1/a1/avatar.webp',
status: 'clean',
declared_mime: 'image/jpeg',
declared_size: 1000,
byte_size: null,
sha256: null,
rejection_reason: null,
created_at: '2026-10-13T10:00:00Z',
updated_at: '2026-10-13T10:00:00Z',
...patch,
}
}
const TICKET = {
asset_id: '11111111-1111-4111-8111-111111111111',
bucket_id: 'avatars',
quarantine_key: 'quarantine/u1/11111111-1111-4111-8111-111111111111/upload',
}
async function kindOf(p: Promise<unknown>) {
try {
await p
return null
} catch (e) {
return e instanceof AssetFailure ? e.kind : 'not-an-asset-failure'
}
}
describe('classifyAssetError', () => {
it('maps P0001 closed messages to their kind', () => {
for (const m of ['too_large', 'unsupported_type', 'rate_limited', 'storage_full', 'upload_missing', 'invalid_state', 'not_found']) {
expect(classifyAssetError({ code: 'P0001', message: m, status: 400 })).toBe(m)
}
})
it('never trusts an unknown message, authorisation is unknown, fetch failures are network', () => {
expect(classifyAssetError({ code: 'P0001', message: 'drop table', status: 400 })).toBe('unknown')
expect(classifyAssetError({ code: '42501', message: 'permission denied', status: 403 })).toBe('unknown')
expect(classifyAssetError(new TypeError('Failed to fetch'))).toBe('network')
expect(classifyAssetError({ message: 'offline' })).toBe('network')
expect(classifyAssetError(new AssetFailure('too_large'))).toBe('too_large')
})
})
describe('beginAvatarUpload', () => {
it('calls the RPC with the declared transport and validates the ticket', async () => {
const stub = stubClient({ rpc: { begin_avatar_upload: { data: TICKET } } })
const ticket = await beginAvatarUpload(stub.client, { declaredMime: 'image/jpeg', declaredSize: 1234 })
expect(stub.calls).toEqual([['begin_avatar_upload', { declared_mime: 'image/jpeg', declared_size: 1234 }]])
expect(ticket).toEqual({ assetId: TICKET.asset_id, bucketId: 'avatars', quarantineKey: TICKET.quarantine_key })
})
it('refuses a malformed ticket and folds RPC errors', async () => {
const bad = stubClient({ rpc: { begin_avatar_upload: { data: { asset_id: 'x' } } } })
expect(await kindOf(beginAvatarUpload(bad.client, { declaredMime: 'image/jpeg', declaredSize: 1 }))).toBe('unknown')
const limited = stubClient({ rpc: { begin_avatar_upload: { error: { code: 'P0001', message: 'rate_limited', status: 400 } } } })
expect(await kindOf(beginAvatarUpload(limited.client, { declaredMime: 'image/jpeg', declaredSize: 1 }))).toBe('rate_limited')
})
})
describe('uploadToQuarantine / confirm / remove', () => {
it('uploads with upsert off and the JPEG content type', async () => {
const stub = stubClient()
const blob = new Blob([new Uint8Array([1, 2, 3])], { type: 'image/jpeg' })
await uploadToQuarantine(stub.client, { bucketId: 'avatars', quarantineKey: TICKET.quarantine_key, blob })
expect(stub.storageCalls).toEqual([
['upload:avatars', [TICKET.quarantine_key, blob, { upsert: false, contentType: 'image/jpeg' }]],
])
})
it('maps a duplicate or a policy refusal to invalid_state, a transport failure to network', async () => {
const blob = new Blob([])
const dup = stubClient({ upload: { error: { message: 'The resource already exists' } } })
expect(await kindOf(uploadToQuarantine(dup.client, { bucketId: 'avatars', quarantineKey: 'k', blob }))).toBe('invalid_state')
const refused = stubClient({
upload: { error: { message: 'new row violates row-level security policy', status: 400, statusCode: '42501' } },
})
expect(await kindOf(uploadToQuarantine(refused.client, { bucketId: 'avatars', quarantineKey: 'k', blob }))).toBe('invalid_state')
const big = stubClient({ upload: { error: { message: 'Payload too large', status: 413, statusCode: '413' } } })
expect(await kindOf(uploadToQuarantine(big.client, { bucketId: 'avatars', quarantineKey: 'k', blob }))).toBe('too_large')
const down = stubClient({ upload: { error: { message: 'fetch failed' } } })
expect(await kindOf(uploadToQuarantine(down.client, { bucketId: 'avatars', quarantineKey: 'k', blob }))).toBe('network')
})
it('confirm and remove call their RPCs', async () => {
const stub = stubClient({ rpc: { remove_avatar: { data: 2 } } })
await confirmAssetUpload(stub.client, TICKET.asset_id)
expect(await removeAvatar(stub.client)).toBe(2)
expect(stub.calls.map((c) => c[0])).toEqual(['confirm_asset_upload', 'remove_avatar'])
expect(stub.calls[0][1]).toEqual({ asset_id: TICKET.asset_id })
const missing = stubClient({ rpc: { confirm_asset_upload: { error: { code: 'P0001', message: 'upload_missing', status: 400 } } } })
expect(await kindOf(confirmAssetUpload(missing.client, TICKET.asset_id))).toBe('upload_missing')
})
})
describe('fetchOwnAvatarState', () => {
it('reads the explicit column list, never *, for the owner', async () => {
const stub = stubClient({ rows: [row({})] })
const state = await fetchOwnAvatarState(stub.client, 'u1')
expect(stub.query.select).not.toContain('*')
expect(stub.query.select).toContain('object_key')
expect(stub.query.select).not.toContain('lock_token')
expect(stub.query['eq:owner_id']).toBe('u1')
expect(stub.query['eq:kind']).toBe('profile_avatar')
expect(stub.query['in:status']).toEqual(['clean', 'pending_upload', 'uploaded', 'rejected'])
expect(stub.query.limit).toBe(5)
expect(state.clean?.id).toBe('a1')
expect(state.inFlight).toBeNull()
})
it('folds a select error', async () => {
const stub = stubClient({ selectError: { code: '42501', message: 'denied', status: 403 } })
expect(await kindOf(fetchOwnAvatarState(stub.client, 'u1'))).toBe('unknown')
})
})
describe('reduceAvatarRows', () => {
const clean = row({ id: 'c', status: 'clean', created_at: '2026-10-13T10:00:00Z' })
it('only rows newer than the clean one count as in flight or rejected', () => {
const older = row({ id: 'o', status: 'rejected', rejection_reason: 'dimensions', created_at: '2026-10-13T09:00:00Z' })
const newer = row({ id: 'n', status: 'uploaded', created_at: '2026-10-13T11:00:00Z' })
const rejected = row({ id: 'r', status: 'rejected', rejection_reason: 'decode_failed', created_at: '2026-10-13T12:00:00Z' })
const state = reduceAvatarRows([rejected, newer, clean, older])
expect(state.clean?.id).toBe('c')
expect(state.inFlight?.id).toBe('n')
expect(state.lastRejected?.id).toBe('r')
})
it('without a clean row every newer row counts', () => {
const state = reduceAvatarRows([row({ id: 'p', status: 'pending_upload' })])
expect(state).toEqual({ clean: null, inFlight: expect.objectContaining({ id: 'p' }), lastRejected: null })
expect(reduceAvatarRows([])).toEqual({ clean: null, inFlight: null, lastRejected: null })
})
})
describe('downloadAvatar', () => {
it('downloads from the avatars bucket and folds a refusal', async () => {
const blob = new Blob([new Uint8Array([82, 73, 70, 70])])
const ok = stubClient({ download: { data: blob } })
expect(await downloadAvatar(ok.client, 'profiles/u1/a1/avatar.webp')).toBe(blob)
expect(ok.storageCalls[0]).toEqual(['download:avatars', ['profiles/u1/a1/avatar.webp']])
const denied = stubClient({ download: { error: { message: 'Object not found', status: 400 } } })
expect(await kindOf(downloadAvatar(denied.client, 'k'))).toBe('unknown')
})
})
@@ -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,332 @@
/**
* Avatar flow against the real local stack and the worker container
* (docs/development/STORAGE.md §4, §3; STEP8_FILES_ANALYSIS IV.2 "integration"
* and "real-stack e2e"). Run through `pnpm e2e:avatar` (scripts/avatar-e2e.sh),
* which starts the worker with a 5 s sweep and a 0 s quarantine grace.
*
* Environment: SUPABASE_URL, SUPABASE_ANON_KEY, SUPABASE_SERVICE_ROLE_KEY
* (the service key is used for inspection only: listing objects and shifting
* the purge clock through psql in the database container).
*/
import { createHash } from 'node:crypto'
import { execFileSync } from 'node:child_process'
import { deflateSync } from 'node:zlib'
import { createClient, type SupabaseClient } from '@supabase/supabase-js'
import {
beginAvatarUpload,
confirmAssetUpload,
downloadAvatar,
fetchOwnAvatarState,
removeAvatar,
uploadToQuarantine,
type AssetRow,
type Database,
} from '../../src'
const URL_ = process.env.SUPABASE_URL
const ANON = process.env.SUPABASE_ANON_KEY
const SERVICE = process.env.SUPABASE_SERVICE_ROLE_KEY
const DB_CONTAINER = process.env.AVATAR_E2E_DB_CONTAINER ?? 'supabase_db_faunosteo'
const PASSWORD = 'LocalDev-only-password-1'
const BOB = 'bob@faunosteo.local'
const WORKER_WAIT_MS = 60_000
type Client = SupabaseClient<Database>
function anonClient(): Client {
return createClient<Database>(URL_ ?? '', ANON ?? '', {
auth: { persistSession: false, autoRefreshToken: false },
})
}
async function signedIn(email: string): Promise<Client> {
const client = anonClient()
const { error } = await client.auth.signInWithPassword({ email, password: PASSWORD })
if (error) throw new Error(`sign-in ${email}: ${error.message}`)
return client
}
function serviceClient(): Client {
return createClient<Database>(URL_ ?? '', SERVICE ?? '', {
auth: { persistSession: false, autoRefreshToken: false },
})
}
// --- fixtures ---------------------------------------------------------------
const CRC_TABLE = new Uint32Array(256).map((_, n) => {
let c = n
for (let k = 0; k < 8; k++) c = c & 1 ? 0xedb88320 ^ (c >>> 1) : c >>> 1
return c >>> 0
})
function crc32(bytes: Uint8Array): number {
let c = 0xffffffff
for (const b of bytes) c = CRC_TABLE[(c ^ b) & 0xff] ^ (c >>> 8)
return (c ^ 0xffffffff) >>> 0
}
function chunk(type: string, payload: Uint8Array): Uint8Array {
const out = new Uint8Array(12 + payload.length)
const view = new DataView(out.buffer)
view.setUint32(0, payload.length)
out.set(Buffer.from(type, 'ascii'), 4)
out.set(payload, 8)
const crc = crc32(out.subarray(4, 8 + payload.length))
view.setUint32(8 + payload.length, crc)
return out
}
/** A valid RGBA PNG gradient of the given size (no dependency, like the worker's fixtures). */
function png(width: number, height: number): Uint8Array {
const raw = new Uint8Array(height * (1 + width * 4))
for (let y = 0; y < height; y++) {
const row = y * (1 + width * 4)
raw[row] = 0
for (let x = 0; x < width; x++) {
const i = row + 1 + x * 4
raw[i] = (x * 255) / width
raw[i + 1] = (y * 255) / height
raw[i + 2] = 128
raw[i + 3] = 255
}
}
const ihdr = new Uint8Array(13)
const view = new DataView(ihdr.buffer)
view.setUint32(0, width)
view.setUint32(4, height)
ihdr.set([8, 6, 0, 0, 0], 8)
return Buffer.concat([
Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
chunk('IHDR', ihdr),
chunk('IDAT', deflateSync(raw)),
chunk('IEND', new Uint8Array(0)),
])
}
const asBlob = (bytes: Uint8Array, type: string) => new Blob([Uint8Array.from(bytes)], { type })
// --- helpers ----------------------------------------------------------------
async function waitFor<T>(
read: () => Promise<T | null>,
timeoutMs: number,
label: string,
): Promise<T> {
const started = Date.now()
for (;;) {
const value = await read()
if (value !== null) return value
if (Date.now() - started > timeoutMs) throw new Error(`timeout: ${label}`)
await new Promise((r) => setTimeout(r, 1000))
}
}
async function ownRow(client: Client, userId: string, assetId: string): Promise<AssetRow | null> {
const { data, error } = await client
.from('assets')
.select('id, owner_id, kind, bucket_id, object_key, status, declared_mime, declared_size, byte_size, sha256, rejection_reason, created_at, updated_at')
.eq('owner_id', userId)
.eq('id', assetId)
.maybeSingle()
if (error) throw new Error(error.message)
return data
}
/** Row once the worker has settled it (clean or rejected). */
function settled(client: Client, userId: string, assetId: string) {
return waitFor(
async () => {
const row = await ownRow(client, userId, assetId)
return row && (row.status === 'clean' || row.status === 'rejected') ? row : null
},
WORKER_WAIT_MS,
`worker verdict on ${assetId}`,
)
}
/** Upload one blob end to end and return the settled row. */
async function uploadAndSettle(
client: Client,
userId: string,
blob: Blob,
declaredMime: 'image/jpeg' | 'image/png' | 'image/webp',
): Promise<AssetRow> {
const ticket = await beginAvatarUpload(client, { declaredMime, declaredSize: blob.size })
await uploadToQuarantine(client, { ...ticket, blob })
await confirmAssetUpload(client, ticket.assetId)
return settled(client, userId, ticket.assetId)
}
/** True when the service role can still download the object (it bypasses the policies). */
async function objectExists(service: Client, bucket: string, key: string): Promise<boolean> {
const { data, error } = await service.storage.from(bucket).download(key)
return !error && data !== null && data.size > 0
}
function psql(sql: string): string {
return execFileSync('docker', [
'exec', DB_CONTAINER, 'psql', '-U', 'postgres', '-d', 'postgres', '-Atc', sql,
]).toString().trim()
}
/** Moves every due date of the purge queue to now (the grace is 48 h / 1 h in production). */
function expeditePurges(): void {
psql('update private.object_purge_queue set not_before = now() where not_before > now()')
}
/** Purge entries still queued for an account (served entries are acked, hence deleted). */
function queuedPurges(userId: string): number {
if (!/^[0-9a-f-]{36}$/.test(userId)) throw new Error('userId is not a uuid')
return Number(psql(`select count(*) from private.object_purge_queue where object_name like '%/${userId}/%'`))
}
async function downloadFails(client: Client, key: string): Promise<boolean> {
const { data, error } = await client.storage.from('avatars').download(key)
return Boolean(error) || data === null
}
function sha256(bytes: Uint8Array): string {
return createHash('sha256').update(bytes).digest('hex')
}
// --- suite ------------------------------------------------------------------
const configured = Boolean(URL_ && ANON && SERVICE)
describe.skipIf(!configured)('avatar flow on the real stack', () => {
const service = serviceClient()
let alice: Client
let aliceId: string
let bob: Client
beforeAll(async () => {
// A fresh account per run keeps the seeded accounts clean and the per-hour rate out of reach.
const email = `e2e-avatar-${Date.now()}@faunosteo.local`
const created = await service.auth.admin.createUser({
email,
password: PASSWORD,
email_confirm: true,
user_metadata: { display_name: 'E2E Avatar', locale: 'fr' },
})
if (created.error || !created.data.user) throw new Error(`createUser: ${created.error?.message}`)
aliceId = created.data.user.id
alice = await signedIn(email)
bob = await signedIn(BOB)
})
afterAll(async () => {
if (aliceId) await service.auth.admin.deleteUser(aliceId)
})
it('cleans a valid upload, and only the owner can download the result', async () => {
const source = png(640, 480)
const row = await uploadAndSettle(alice, aliceId, asBlob(source, 'image/png'), 'image/png')
expect(row.status).toBe('clean')
const blob = await downloadAvatar(alice, row.object_key)
const bytes = new Uint8Array(await blob.arrayBuffer())
expect(Buffer.from(bytes.subarray(0, 4)).toString('ascii')).toBe('RIFF')
expect(Buffer.from(bytes.subarray(8, 12)).toString('ascii')).toBe('WEBP')
expect(row.byte_size).toBe(bytes.length)
expect(row.sha256).toBe(sha256(bytes))
expect(bytes.length).toBeLessThan(512 * 1024)
const quarantineKey = `quarantine/${aliceId}/${row.id}/upload`
expect(await downloadFails(alice, quarantineKey)).toBe(true)
expect(await downloadFails(bob, row.object_key)).toBe(true)
expect(await downloadFails(anonClient(), row.object_key)).toBe(true)
const bobRows = await bob.from('assets').select('id').eq('owner_id', aliceId)
expect(bobRows.data).toEqual([])
const direct = await alice.storage.from('avatars').upload(row.object_key, asBlob(source, 'image/png'), {
upsert: false,
contentType: 'image/png',
})
expect(direct.error).not.toBeNull()
// The worker deleted the quarantine object once the row was clean.
await waitFor(
async () => ((await objectExists(service, 'avatars', quarantineKey)) ? null : true),
30_000,
'quarantine object deleted',
)
})
it('replaces the photo and purges the previous object after its grace', async () => {
const before = await fetchOwnAvatarState(alice, aliceId)
const previous = before.clean
expect(previous).not.toBeNull()
const row = await uploadAndSettle(alice, aliceId, asBlob(png(300, 700), 'image/png'), 'image/png')
expect(row.status).toBe('clean')
const state = await fetchOwnAvatarState(alice, aliceId)
expect(state.clean?.id).toBe(row.id)
expect(await downloadFails(alice, previous!.object_key)).toBe(true)
expect(await objectExists(service, 'avatars', previous!.object_key)).toBe(true)
expeditePurges()
await waitFor(
async () => ((await objectExists(service, 'avatars', previous!.object_key)) ? null : true),
30_000,
'replaced object purged after its grace',
)
})
it('sweeps bytes landed through a still-valid signed upload URL after the row moved on', async () => {
const stale = await beginAvatarUpload(alice, { declaredMime: 'image/png', declaredSize: 1000 })
const signed = await alice.storage.from('avatars').createSignedUploadUrl(stale.quarantineKey)
expect(signed.error).toBeNull()
// A new pick supersedes the row; the signed URL is still valid for two hours.
const fresh = await beginAvatarUpload(alice, { declaredMime: 'image/png', declaredSize: 1000 })
expect(fresh.assetId).not.toBe(stale.assetId)
const late = await alice.storage
.from('avatars')
.uploadToSignedUrl(stale.quarantineKey, signed.data!.token, asBlob(png(100, 100), 'image/png'), {
contentType: 'image/png',
})
// Either the API refused the late upload, or the sweeper removes the leftover.
if (!late.error) {
await waitFor(
async () => ((await objectExists(service, 'avatars', stale.quarantineKey)) ? null : true),
30_000,
'late quarantine bytes swept',
)
}
const staleRow = await ownRow(alice, aliceId, stale.assetId)
expect(staleRow?.status).toBe('deleted')
expect(await downloadFails(alice, stale.quarantineKey)).toBe(true)
})
it('rejects hostile and corrupted uploads with closed codes', async () => {
const html = Buffer.from('<!doctype html><script>alert(1)</script>')
const htmlRow = await uploadAndSettle(alice, aliceId, asBlob(html, 'image/jpeg'), 'image/jpeg')
expect(htmlRow.status).toBe('rejected')
expect(htmlRow.rejection_reason).toBe('unsupported_type')
const truncated = png(200, 200).subarray(0, 80)
const truncatedRow = await uploadAndSettle(alice, aliceId, asBlob(truncated, 'image/png'), 'image/png')
expect(truncatedRow.status).toBe('rejected')
expect(truncatedRow.rejection_reason).toBe('decode_failed')
const tinyRow = await uploadAndSettle(alice, aliceId, asBlob(png(32, 32), 'image/png'), 'image/png')
expect(tinyRow.status).toBe('rejected')
expect(tinyRow.rejection_reason).toBe('dimensions')
})
it('removes the photo, cuts the download at once and empties the objects after the grace', async () => {
const row = await uploadAndSettle(alice, aliceId, asBlob(png(512, 512), 'image/png'), 'image/png')
expect(row.status).toBe('clean')
expect(await removeAvatar(alice)).toBeGreaterThanOrEqual(1)
expect(await downloadFails(alice, row.object_key)).toBe(true)
const state = await fetchOwnAvatarState(alice, aliceId)
expect(state.clean).toBeNull()
expeditePurges()
await waitFor(
async () => {
const listed = await service.storage.from('avatars').list(`profiles/${aliceId}`, { limit: 100 })
const remaining = (listed.data ?? []).length
return remaining === 0 ? true : null
},
30_000,
'every object of the account purged',
)
// Every served entry was acked, a missing object counting as purged.
await waitFor(async () => (queuedPurges(aliceId) === 0 ? true : null), 30_000, 'purge queue drained')
})
})
+17
View File
@@ -0,0 +1,17 @@
import { defineConfig } from 'vitest/config'
/**
* Real-stack end-to-end suite (tests/e2e/*.e2e.ts): needs the local Supabase
* stack and the worker container (scripts/avatar-e2e.sh starts both sides).
* Kept out of the default `test:run` include on purpose.
*/
export default defineConfig({
test: {
environment: 'node',
globals: true,
include: ['tests/e2e/**/*.e2e.ts'],
fileParallelism: false,
testTimeout: 120_000,
hookTimeout: 120_000,
},
})
+3
View File
@@ -246,6 +246,9 @@ importers:
'@supabase/supabase-js':
specifier: ^2.100.0
version: 2.117.3
zod:
specifier: ^3.24.1
version: 3.25.76
devDependencies:
'@electric-sql/pglite':
specifier: ^0.3.0
+57
View File
@@ -0,0 +1,57 @@
#!/usr/bin/env bash
# Real-stack end-to-end run of the avatar flow (docs/development/STORAGE.md §6):
# starts the worker container against the local Supabase stack with a fast sweep
# and no quarantine grace, runs packages/supabase/tests/e2e, stops the container.
# Needs: `pnpm db:start` done, Docker, the worker image (`pnpm worker:image`,
# built here when absent). The keys come from `supabase status`; the service key
# reaches the container through a 600-mode temporary env file, never the command line.
set -euo pipefail
cd "$(dirname "$0")/.."
network="${SUPABASE_NETWORK:-supabase_network_faunosteo}"
kong="${SUPABASE_INTERNAL_URL:-http://supabase_kong_faunosteo:8000}"
image="${WORKER_IMAGE:-faunosteo_worker:dev}"
name="faunosteo_worker_e2e"
status="$(node_modules/.bin/supabase status -o env 2>/dev/null)" \
|| { echo "avatar-e2e: the local stack is not running (pnpm db:start)" >&2; exit 1; }
value() { printf '%s\n' "$status" | sed -n "s/^$1=\"\(.*\)\"$/\1/p"; }
api_url="$(value API_URL)"
anon_key="$(value ANON_KEY)"
service_key="$(value SERVICE_ROLE_KEY)"
[ -n "$api_url" ] && [ -n "$anon_key" ] && [ -n "$service_key" ] \
|| { echo "avatar-e2e: could not read the keys from supabase status" >&2; exit 1; }
if ! docker image inspect "$image" >/dev/null 2>&1; then
echo "== building $image"
docker build -f crates/worker/Dockerfile -t "$image" .
fi
env_file="$(mktemp)"
chmod 600 "$env_file"
cleanup() {
echo "== worker log (last lines)"
docker logs --tail 40 "$name" 2>/dev/null || true
docker rm -f "$name" >/dev/null 2>&1 || true
rm -f "$env_file"
}
trap cleanup EXIT
{
echo "SUPABASE_URL=$kong"
echo "SUPABASE_SERVICE_ROLE_KEY=$service_key"
echo "WORKER_ID=e2e"
echo "WORKER_POLL_SECONDS=1"
echo "WORKER_SWEEP_SECONDS=5"
echo "WORKER_QUARANTINE_GRACE_SECONDS=0"
} > "$env_file"
docker rm -f "$name" >/dev/null 2>&1 || true
echo "== starting $name on $network"
docker run -d --name "$name" --read-only --cap-drop ALL \
--security-opt no-new-privileges:true --memory 512m --cpus 1 --pids-limit 64 \
--network "$network" --env-file "$env_file" "$image" run >/dev/null
echo "== e2e suite"
SUPABASE_URL="$api_url" SUPABASE_ANON_KEY="$anon_key" SUPABASE_SERVICE_ROLE_KEY="$service_key" \
pnpm --filter @faunosteo/supabase test:e2e
+13
View File
@@ -0,0 +1,13 @@
#!/usr/bin/env bash
# Vite dev server reachable from the LAN, wired to the local Supabase stack by the
# host's LAN address (the stack publishes 54321 / 54323 / 54324 on every interface).
# Usage: pnpm dev:lan [host-ip] (default: the first non-loopback IPv4 of this host)
set -euo pipefail
cd "$(dirname "$0")/.."
ip="${1:-$(hostname -I 2>/dev/null | awk '{print $1}')}"
[ -n "$ip" ] || { echo "dev-lan: pass the host IP as the first argument" >&2; exit 1; }
anon="$(node_modules/.bin/supabase status -o env 2>/dev/null | sed -n 's/^ANON_KEY="\(.*\)"$/\1/p')"
[ -n "$anon" ] || { echo "dev-lan: local stack not running (pnpm db:start)" >&2; exit 1; }
echo "== app http://$ip:5173 supabase http://$ip:54321 studio http://$ip:54323 mailpit http://$ip:54324"
VITE_SUPABASE_URL="http://$ip:54321" VITE_SUPABASE_ANON_KEY="$anon" \
exec node_modules/.bin/pnpm --filter @faunosteo/app exec vite --host 0.0.0.0 --port 5173
+9 -1
View File
@@ -6,6 +6,9 @@ project_id = "faunosteo"
[api]
enabled = true
port = 54321
# LAN development: the address written into confirmation and recovery mails
# (GoTrue API_EXTERNAL_URL). Keep 127.0.0.1 when working on the host alone.
external_url = "http://192.168.1.100:54321"
# Only public is exposed through PostgREST. private and audit stay internal
# (docs/development/BACKEND_ARCHITECTURE.md §3).
schemas = ["public"]
@@ -68,12 +71,17 @@ enabled = false
[auth]
enabled = true
# Vite dev server of apps/app
site_url = "http://127.0.0.1:5173"
site_url = "http://192.168.1.100:5173"
# LAN access (pnpm dev:lan): confirmation and recovery links must land on the
# address the browser used, so every private-network host on :5173 is allowed.
additional_redirect_urls = [
"http://127.0.0.1:5173",
"http://localhost:5173",
"http://127.0.0.1:5173/**",
"http://localhost:5173/**",
"http://192.168.*.*:5173/**",
"http://10.*.*.*:5173/**",
"http://172.16.*.*:5173/**",
]
jwt_expiry = 3600
enable_refresh_token_rotation = true
@@ -0,0 +1,729 @@
-- 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';
-- The ceiling's pending term reads only the rows still waiting for bytes.
create index assets_pending_claims on public.assets (quarantine_key) where status = 'pending_upload';
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 the bytes present in the avatars bucket (10 GiB to start),
-- measured on storage.objects, plus one 5 MiB cap per upload still expected.
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;
-- Bytes one account may hold in the bucket at once (its clean photo plus the
-- quarantine objects of its last uploads during their grace): 64 MiB.
create or replace function private.account_byte_budget()
returns bigint
language sql
immutable
set search_path = ''
as $$ select 67108864::bigint; $$;
revoke all on function private.account_byte_budget() 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
join public.profiles p on p.id = a.owner_id and p.active
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_expected bigint;
v_mine bigint;
v_final_prefix text;
v_quarantine_prefix text;
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;
-- Per-account budget on bytes present under the caller's two prefixes
-- (clean photo, quarantine objects within their grace): bounds what one
-- account can hold against the global ceiling.
v_final_prefix := 'profiles/' || v_actor::text || '/%';
v_quarantine_prefix := 'quarantine/' || v_actor::text || '/%';
select coalesce(sum((o.metadata ->> 'size')::bigint), 0) into v_mine
from storage.objects o
where o.bucket_id = 'avatars'
and (o.name like v_final_prefix or o.name like v_quarantine_prefix);
if v_mine + 5242880 > private.account_byte_budget() then
raise exception using message = 'rate_limited', errcode = 'P0001';
end if;
-- Global ceiling: bytes that exist (every sized object of the bucket,
-- whatever its row's state) plus one cap per pending upload whose bytes
-- have not landed yet (the declared size is a claim the upload does not
-- enforce). A claim never outlives its row and a row without bytes weighs
-- nothing once it moved on, so no account can pin the ceiling.
select coalesce(sum((o.metadata ->> 'size')::bigint), 0) into v_used
from storage.objects o where o.bucket_id = 'avatars';
select count(*) * 5242880 into v_expected
from public.assets a
where a.status = 'pending_upload'
and not exists (
select 1 from storage.objects o
where o.bucket_id = a.bucket_id and o.name = a.quarantine_key
and o.metadata ->> 'size' is not null
);
if v_used + v_expected + 5242880 > 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), per-account byte budget, 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_active boolean;
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 p.active into v_active from public.profiles p where p.id = v_actor;
if v_active is distinct from true then
raise exception 'account inactive or unknown' 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, (o.metadata ->> 'size')::bigint into v_found, v_size
from storage.objects o
where o.bucket_id = v_row.bucket_id and o.name = v_row.quarantine_key;
-- No object, or an object the Storage API has not sized: nothing to confirm (fail closed).
if v_found is distinct from true or v_size is null then
raise exception using message = 'upload_missing', errcode = 'P0001';
end if;
-- The object may not exceed the cap nor what the caller declared (the
-- transport blob the browser announced).
if v_size > 5242880 or v_size > v_row.declared_size 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;
revoke all on function public.begin_avatar_upload(text, integer) from service_role;
revoke all on function public.confirm_asset_upload(uuid) from service_role;
revoke all on function public.remove_avatar() from service_role;
grant execute on function public.begin_avatar_upload(text, integer) to authenticated;
grant execute on function public.confirm_asset_upload(uuid) to authenticated;
grant execute on function public.remove_avatar() to authenticated;
-- ---------------------------------------------------------------------------
-- 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 is null 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;
+1 -1
View File
@@ -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'
);
+579
View File
@@ -0,0 +1,579 @@
-- 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(140);
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 = bytes present in the bucket + declared sizes of pending rows without bytes.
-- 2048 rejected rows of 5 MiB (no object) weigh nothing.
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 lives_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'rejected rows without bytes do not count toward the ceiling');
reset role;
-- 2048 pending rows of 5 MiB whose bytes have not landed are expected bytes.
insert into public.assets (owner_id, kind, bucket_id, object_key, quarantine_key, status, declared_mime, declared_size, created_at)
select '40000000-0000-0000-0000-00000000000c', 'profile_avatar', 'avatars',
'profiles/40000000-0000-0000-0000-00000000000c/' || g || '/avatar.webp',
'quarantine/40000000-0000-0000-0000-00000000000c/' || g || '/upload',
'pending_upload', 'image/jpeg', 5242880, 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', 'declared sizes of pending uploads fill the ceiling');
reset role;
-- Superseded or expired without bytes: the claim dies with the row (no pinning by begin spam).
update public.assets set status = 'deleted' where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'pending_upload' and created_at < now() - interval '1 hour';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'scenario: rows that moved on without bytes weigh nothing, even while queued for purge');
reset role;
-- Bytes present count whatever the row says: one 10 GiB object (under a foreign
-- prefix, so the per-account budget stays out of the way) fills the ceiling.
insert into storage.objects (bucket_id, name, metadata) values ('avatars', 'quarantine/40000000-0000-0000-0000-00000000000d/00000000-0000-0000-0000-00000000ffff/upload', '{"size":10737418240}');
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', 'bytes present in the bucket fill the ceiling whatever their row');
reset role;
update storage.objects set metadata = '{"size":1}' where name = 'quarantine/40000000-0000-0000-0000-00000000000d/00000000-0000-0000-0000-00000000ffff/upload';
-- A pending row whose bytes landed is charged once, by its object (no double count).
update public.assets set declared_size = 5242880 where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'pending_upload';
insert into storage.objects (bucket_id, name, metadata)
select 'avatars', a.quarantine_key, '{"size":1024}' from public.assets a
where a.owner_id = '40000000-0000-0000-0000-00000000000c' and a.status = 'pending_upload';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select lives_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, 'a landed upload is counted by its bytes, not by its claim');
reset role;
-- Per-account budget: 64 MiB of the caller's own bytes present → rate_limited.
insert into storage.objects (bucket_id, name, metadata) values ('avatars', 'quarantine/40000000-0000-0000-0000-00000000000c/00000000-0000-0000-0000-00000000fffe/upload', '{"size":67108864}');
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', 'rate_limited', 'scenario: one account cannot hold more than its byte budget in the bucket');
reset role;
update storage.objects set metadata = '{"size":1}' where name = 'quarantine/40000000-0000-0000-0000-00000000000c/00000000-0000-0000-0000-00000000fffe/upload';
-- Carol's pending row (begun above): the object may not exceed the declared size.
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
insert into pg_temp.ids select 'c1', id from public.assets where owner_id = '40000000-0000-0000-0000-00000000000c' and status = 'pending_upload';
insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('c1')).quarantine_key, '{}');
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('c1')) $$, 'P0001', 'upload_missing', 'an object without a size is not confirmable (fail closed)');
reset role;
update storage.objects set metadata = '{"size":4000}' where name = (pg_temp.row('c1')).quarantine_key;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok($$ select public.confirm_asset_upload(pg_temp.id('c1')) $$, 'P0001', 'too_large', 'an object larger than the declared size is refused at confirm');
reset role;
update storage.objects set metadata = '{"size":1000}' where name = (pg_temp.row('c1')).quarantine_key;
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select lives_ok($$ select public.confirm_asset_upload(pg_temp.id('c1')) $$, 'carol confirms within the declared size');
reset role;
set local role service_role;
select set_config('request.jwt.claims', '{"role":"service_role"}', true);
insert into pg_temp.ids select 'tokc1', lock_token from public.claim_avatar_jobs(1);
select throws_ok(
$$ select public.complete_avatar_job(pg_temp.id('c1'), pg_temp.id('tokc1'), 30000, null) $$,
'P0001', 'invalid_output', 'a null sha256 is refused by the output guard'
);
reset role;
-- Deactivated account: begin, confirm and the upload policy refuse it.
update public.profiles set active = false where id = '40000000-0000-0000-0000-00000000000c';
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok($$ select public.confirm_asset_upload(gen_random_uuid()) $$, '42501', null, 'a deactivated account cannot confirm an upload');
select throws_ok($$ select public.begin_avatar_upload('image/jpeg', 1000) $$, '42501', null, 'a deactivated account cannot begin an upload');
reset role;
-- Back to pending under a fresh quarantine key (the real stack forbids deleting storage rows directly).
update public.assets set status = 'pending_upload', locked_at = null, lock_token = null, attempts = 0,
quarantine_key = 'quarantine/40000000-0000-0000-0000-00000000000c/' || gen_random_uuid() || '/upload'
where id = pg_temp.id('c1');
set local role authenticated;
select set_config('request.jwt.claims', '{"sub":"40000000-0000-0000-0000-00000000000c","role":"authenticated"}', true);
select throws_ok(
$$ insert into storage.objects (bucket_id, name, metadata) values ('avatars', (pg_temp.row('c1')).quarantine_key, '{"size":1000}') $$,
'42501', null, 'a deactivated account cannot upload at its pending quarantine key'
);
reset role;
select ok(not has_function_privilege('service_role', 'public.begin_avatar_upload(text, integer)', 'execute'), 'begin_avatar_upload is not granted to service_role');
select ok(not has_function_privilege('service_role', 'public.confirm_asset_upload(uuid)', 'execute'), 'confirm_asset_upload is not granted to service_role');
select ok(not has_function_privilege('service_role', 'public.remove_avatar()', 'execute'), 'remove_avatar is not granted to service_role');
select * from finish();
rollback;