Repository navigation
Expand file tree
/
Copy pathsupabase-security.sql
More file actions
108 lines (94 loc) · 4.31 KB
/
Copy pathsupabase-security.sql
File metadata and controls
108 lines (94 loc) · 4.31 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
-- ═══════════════════════════════════════════════════════════════
-- QUANTRO · Güvenlik Sertleştirme (RLS)
-- Supabase Dashboard → SQL Editor'a yapıştırıp RUN deyin.
--
-- NE YAPAR?
-- • blog_posts: anon (yayınlanabilir anahtar) YALNIZCA yayınlanmış
-- yazıları okuyabilir. Yazma/güncelleme/silme HİÇBİR tarayıcı
-- tarafından yapılamaz; yalnız sunucu (service_role) yapabilir.
-- • blog_comments: anon yalnızca ONAYLI yorumları okuyabilir.
-- Yorum eklemek /api/comments üzerinden sunucu ile yapılır;
-- tarayıcıdan doğrudan INSERT yapılamaz. Onaylama/silme yalnız
-- admin (service_role) içindir.
-- • contact_messages: anon yalnızca form gönderebilir (INSERT).
-- Okuma/güncelleme/silme yalnız sunucuya açıktır — ziyaretçi
-- mesajları artık tarayıcıdan okunamaz.
-- ═══════════════════════════════════════════════════════════════
-- ── BLOG YAZILARI ──
alter table public.blog_posts enable row level security;
drop policy if exists "anon_blog_read_published" on public.blog_posts;
create policy "anon_blog_read_published"
on public.blog_posts
for select
to anon
using (published = true);
-- ── BLOG YORUMLARI ──
create table if not exists public.blog_comments (
id uuid primary key default gen_random_uuid(),
post_slug text not null,
name text not null,
email text,
content text not null,
is_approved boolean not null default false,
created_at timestamptz not null default now()
);
alter table public.blog_comments enable row level security;
drop policy if exists "anon_comment_read_approved" on public.blog_comments;
create policy "anon_comment_read_approved"
on public.blog_comments
for select
to anon
using (is_approved = true);
-- NOT: blog_comments için anon INSERT yetkisi VERİLMEDİ. Yorumlar
-- yalnızca /api/comments (service_role) üzerinden eklenir; böylece
-- spam doğrulaması sunucuda yapılır.
-- ── İLETİŞİM MESAJLARI ──
alter table public.contact_messages enable row level security;
drop policy if exists "anon_msg_insert" on public.contact_messages;
create policy "anon_msg_insert"
on public.contact_messages
for insert
to anon
with check (true);
-- ── BLOG BEĞENİLERİ ──
create table if not exists public.post_likes (
post_slug text primary key,
likes integer not null default 0,
updated_at timestamptz not null default now()
);
alter table public.post_likes enable row level security;
drop policy if exists "anon_like_read" on public.post_likes;
create policy "anon_like_read"
on public.post_likes
for select
to anon
using (true);
-- Beğeni sayacı: /api/likes (service_role) çağırır; RLS'yi atlar.
-- Çakışma (race) güvenli artırma; ikinci çağrıda yeni satır değil +1 yapar.
create or replace function public.increment_post_like(p_slug text)
returns integer
language sql
security definer
set search_path = public
as $$
insert into public.post_likes(post_slug, likes, updated_at)
values (p_slug, 1, now())
on conflict (post_slug)
do update set likes = public.post_likes.likes + 1, updated_at = now()
returning likes
$$;
-- RPC'yi yalnızca sunucu (service_role) çağırabilir; anon doğrudan
-- beğeni artıramaz (browser bu fonksiyonu göremez/çalıştıramaz).
revoke execute on function public.increment_post_like(text) from public;
revoke execute on function public.increment_post_like(text) from anon, authenticated;
grant execute on function public.increment_post_like(text) to service_role;
-- ── DOĞRULAMA ──
-- Aşağıdaki sorgular 0 satır döndürmeli (anon yazamıyor/okuyamıyor olmalı):
--
-- select count(*) from public.blog_posts where published = false; -- anon olarak
-- select * from public.contact_messages; -- anon olarak
-- select * from public.blog_comments where is_approved = false; -- anon olarak
-- insert into public.blog_comments (post_slug,name,content) values ('x','y','z'); -- anon olarak HATA vermeli
--
-- NOT: service_role anahtarı RLS'yi OTOMATİK olarak atlar. Sitenin
-- /api/* uçları bu anahtarı kullandığı için admin işlemleri etkilenmez.