Skip to content

Commit 3ef3d35

Browse files
ctrimmclaude
andcommitted
Add Supabase security migration: RLS + view security_invoker
Fixes four Supabase Security Advisor warnings: 1. ALTER VIEW recent_unresolved_errors SET (security_invoker = true) — prevents view owner's elevated permissions from bypassing RLS 2. Enable RLS on error_log with no public policies — service_role (cron) bypasses RLS; anon gets no access 3. Enable RLS on monitored_sites + SELECT policy for anon/authenticated — Lambda functions read via anon key; cron writes via service_role 4. Enable RLS on website_emissions + SELECT policy for anon/authenticated — Lambda functions read via anon key; cron writes via service_role Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2457a42 commit 3ef3d35

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
-- Migration: Fix Supabase security advisor warnings
2+
-- Date: 2026-02-21
3+
--
4+
-- Apply in the Supabase SQL Editor (Dashboard → SQL Editor → New query).
5+
-- Safe to run multiple times (uses IF EXISTS / OR REPLACE where needed).
6+
7+
-- ============================================================
8+
-- 1. Fix SECURITY DEFINER view
9+
-- Switch recent_unresolved_errors to SECURITY INVOKER so that
10+
-- RLS policies of the *querying* user are enforced rather than
11+
-- those of the view owner (postgres / supabase_admin).
12+
-- Requires PostgreSQL 15+ — Supabase is on PG15+.
13+
-- ============================================================
14+
ALTER VIEW public.recent_unresolved_errors SET (security_invoker = true);
15+
16+
17+
-- ============================================================
18+
-- 2. error_log — enable RLS, no public access
19+
-- The cron job writes via service_role which bypasses RLS.
20+
-- No anon/authenticated role should read this internal table.
21+
-- ============================================================
22+
ALTER TABLE public.error_log ENABLE ROW LEVEL SECURITY;
23+
24+
25+
-- ============================================================
26+
-- 3. monitored_sites — enable RLS, public read-only
27+
-- Lambda functions query this table with the anon key.
28+
-- All writes come from service_role (bypasses RLS).
29+
-- ============================================================
30+
ALTER TABLE public.monitored_sites ENABLE ROW LEVEL SECURITY;
31+
32+
CREATE POLICY "Allow public read access on monitored_sites"
33+
ON public.monitored_sites
34+
FOR SELECT
35+
TO anon, authenticated
36+
USING (true);
37+
38+
39+
-- ============================================================
40+
-- 4. website_emissions — enable RLS, public read-only
41+
-- Lambda functions query this table with the anon key.
42+
-- All writes come from the cron job via service_role (bypasses RLS).
43+
-- ============================================================
44+
ALTER TABLE public.website_emissions ENABLE ROW LEVEL SECURITY;
45+
46+
CREATE POLICY "Allow public read access on website_emissions"
47+
ON public.website_emissions
48+
FOR SELECT
49+
TO anon, authenticated
50+
USING (true);

0 commit comments

Comments
 (0)