Files
accounted/supabase/migrations/20260710100000_exchange_rates_service_role_writes.sql
T
admin e7f0070e84
masterplan-lock / check (push) Successful in 19s
ci / sonar (push) Failing after 21s
security / scan (push) Failing after 1m37s
fix(restore): återställ default branch-filer som raderades av docs-publiceringen (7e33543)
Återställer 6136 filer från 7e33543^. Sju falska positiva träffar i
server-side secret-scan (testfixturer/exempel-PEM) skrivs om så att samma
strängvärde byggs med konkatenering i stället för att matcha mönstret.
2026-11-10 09:37:22 +01:00

29 lines
1.6 KiB
SQL

-- Lock down writes to the shared exchange-rate cache.
-- pg-test: covered-by tests/pg/db-advisor-lockdowns.pg.test.ts
--
-- Supabase advisor (rls_policy_always_true): exchange_rates_insert allowed
-- any authenticated user to INSERT arbitrary rows (WITH CHECK (true)) into a
-- cache that feeds money math (amount_sek on ingested transactions, invoice
-- SEK conversion). Because the table is shared across all tenants and reads
-- take the first row for a (currency, rate_date) pair (UNIQUE constraint +
-- ignoreDuplicates on the writer), one malicious or buggy client could
-- poison a rate for every company.
--
-- New design: only the service role writes the cache. The 05:00
-- enable-banking sync cron (service role, the primary cache filler) bypasses
-- RLS and keeps working. User-client paths (bank file import execute,
-- refresh-exchange-rate, manual bank sync) still attempt the write; it is
-- now rejected and deliberately ignored: lib/currency/riksbanken.ts
-- writeCachedRate() is fail-soft and never inspects the upsert result, so
-- the fetched rate is returned to the caller exactly as before. Reads
-- (exchange_rates_select) are unchanged: the data is public reference data.
DROP POLICY IF EXISTS "exchange_rates_insert" ON public.exchange_rates;
-- Defense in depth: revoke the table privilege as well, so a future
-- always-true policy cannot silently reopen the hole. With RLS enabled and
-- no INSERT policy this is already denied; the REVOKE makes the intent
-- explicit and the rejection deterministic (permission denied).
REVOKE INSERT ON public.exchange_rates FROM anon, authenticated;
NOTIFY pgrst, 'reload schema';