Repository navigation
Expand file tree
/
Copy pathsupabase_rls_migration.sql
More file actions
136 lines (110 loc) · 5.75 KB
/
Copy pathsupabase_rls_migration.sql
File metadata and controls
136 lines (110 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
-- ====================================================================
-- InstinctFi — Supabase Row-Level Security (RLS) Migration
-- ====================================================================
-- Run this in your Supabase Dashboard → SQL Editor
-- This enables RLS on all tables and creates policies for secure access.
--
-- IMPORTANT: After running this, anonymous/public reads are still allowed,
-- but writes require the wallet address in the request JWT metadata.
-- ====================================================================
-- ── 1. Enable RLS on all tables ─────────────────────────────────────
ALTER TABLE IF EXISTS polls ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS votes ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS comments ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS users ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS push_subscriptions ENABLE ROW LEVEL SECURITY;
ALTER TABLE IF EXISTS poll_images ENABLE ROW LEVEL SECURITY;
-- ── 2. Polls ────────────────────────────────────────────────────────
-- Anyone can read polls
CREATE POLICY "polls_select_all" ON polls
FOR SELECT USING (true);
-- BUG-14 FIX: Require authenticated JWT with wallet claim for inserts.
-- All production writes go through SECURITY DEFINER RPCs (which bypass RLS),
-- so these policies are defense-in-depth against direct anon table access.
CREATE POLICY "polls_insert_auth" ON polls
FOR INSERT WITH CHECK (
current_setting('request.jwt.claims', true)::json->>'wallet' IS NOT NULL
);
-- Only the poll creator or admin can update their polls
CREATE POLICY "polls_update_owner" ON polls
FOR UPDATE USING (
creator = current_setting('request.jwt.claims', true)::json->>'wallet'
OR current_setting('request.jwt.claims', true)::json->>'wallet' IN (
'62PFLSvnG4Zp8jYS9AFymETvV5e8xBA2JBW2UhjqyNmS'
)
);
-- Only the poll creator or admin can delete their polls
CREATE POLICY "polls_delete_owner" ON polls
FOR DELETE USING (
creator = current_setting('request.jwt.claims', true)::json->>'wallet'
OR current_setting('request.jwt.claims', true)::json->>'wallet' IN (
'62PFLSvnG4Zp8jYS9AFymETvV5e8xBA2JBW2UhjqyNmS'
)
);
-- ── 3. Votes ────────────────────────────────────────────────────────
-- Anyone can read votes (needed for vote counts, leaderboard)
CREATE POLICY "votes_select_all" ON votes
FOR SELECT USING (true);
-- BUG-14 FIX: Require authenticated JWT with wallet claim.
CREATE POLICY "votes_insert_own" ON votes
FOR INSERT WITH CHECK (
current_setting('request.jwt.claims', true)::json->>'wallet' IS NOT NULL
);
-- Only the voter can update their own vote record (for claiming)
CREATE POLICY "votes_update_own" ON votes
FOR UPDATE USING (
voter = current_setting('request.jwt.claims', true)::json->>'wallet'
);
-- ── 4. Comments ─────────────────────────────────────────────────────
-- Anyone can read comments
CREATE POLICY "comments_select_all" ON comments
FOR SELECT USING (true);
-- BUG-14 FIX: Require authenticated JWT with wallet claim.
CREATE POLICY "comments_insert_auth" ON comments
FOR INSERT WITH CHECK (
current_setting('request.jwt.claims', true)::json->>'wallet' IS NOT NULL
);
-- Only the comment author can delete their own comments
CREATE POLICY "comments_delete_own" ON comments
FOR DELETE USING (
wallet = current_setting('request.jwt.claims', true)::json->>'wallet'
);
-- ── 5. Users ────────────────────────────────────────────────────────
-- Anyone can read user profiles (needed for leaderboard)
CREATE POLICY "users_select_all" ON users
FOR SELECT USING (true);
-- BUG-14 FIX: Require authenticated JWT with wallet claim.
CREATE POLICY "users_insert_own" ON users
FOR INSERT WITH CHECK (
current_setting('request.jwt.claims', true)::json->>'wallet' IS NOT NULL
);
-- Only the user can update their own profile
CREATE POLICY "users_update_own" ON users
FOR UPDATE USING (
wallet = current_setting('request.jwt.claims', true)::json->>'wallet'
);
-- ── 6. Push Subscriptions ───────────────────────────────────────────
-- Users can only read their own push subscriptions
CREATE POLICY "push_subs_select_own" ON push_subscriptions
FOR SELECT USING (
wallet = current_setting('request.jwt.claims', true)::json->>'wallet'
);
-- BUG-14 FIX: Require authenticated JWT with wallet claim.
CREATE POLICY "push_subs_insert_own" ON push_subscriptions
FOR INSERT WITH CHECK (
current_setting('request.jwt.claims', true)::json->>'wallet' IS NOT NULL
);
-- Users can delete their own push subscriptions
CREATE POLICY "push_subs_delete_own" ON push_subscriptions
FOR DELETE USING (
wallet = current_setting('request.jwt.claims', true)::json->>'wallet'
);
-- ── 7. Poll Images ─────────────────────────────────────────────────
-- Anyone can read poll images
CREATE POLICY "poll_images_select_all" ON poll_images
FOR SELECT USING (true);
-- BUG-14 FIX: Require authenticated JWT with wallet claim.
CREATE POLICY "poll_images_insert_auth" ON poll_images
FOR INSERT WITH CHECK (
current_setting('request.jwt.claims', true)::json->>'wallet' IS NOT NULL
);