Skip to content
This repository was archived by the owner on Aug 26, 2026. It is now read-only.
211 changes: 211 additions & 0 deletions supabase/migrations/20260806034559_rls_policies.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
-- docs/permissions.md の権限マトリクスと docs/data-model.md「RLSポリシー方針」に基づく
-- RLSポリシー。全ポリシーは authenticated ロールのみを対象とする(このアプリはGoogle SSO
-- ログイン前提で、匿名ユーザー向けの公開閲覧機能は無い)。
--
-- profiles.is_admin を参照する条件は一切書かない(MVPスコープ外。docs/permissions.md参照)。

-- 1. profiles (docs/data-model.md「RLSポリシー方針」)
-- SELECTの「全ユーザー(公開カラムは限定)」はRLS(行単位)だけでは表現できないため、
-- テーブル本体は本人のみに限定し、他ユーザーへの公開はpublic_profilesビュー(id/display_nameのみ)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: コメント中の「public_profilesビュー」は実際に作成しているビュー名 profiles_public(58行目)と語順が逆です。後で grep で追えなくなるので直しておくと良さそうです(59行目コメントの「所有者(postgres)としてprofilesを参照するため」の段落は正しい名前になっています)。

-- 経由に限定する。
alter table public.profiles enable row level security;

create policy "profiles_select_self" on public.profiles
for select to authenticated
using (id = auth.uid());

create policy "profiles_insert_self" on public.profiles
for insert to authenticated
with check (id = auth.uid());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[要確認] is_admin の保護がUPDATEのみでINSERTには効いていない

guard_is_admin_immutable_trigger は before update にしか張られていないため、profiles_insert_self の with check は id = auth.uid() だけで is_admin の値を制限していません。理屈上はこのポリシー経由で is_admin = true の行をINSERTできてしまい、35行目付近のコメントで説明されている「自分で is_admin=true にしておく抜け道を塞ぐ」という目的をINSERT側では達成できていません。

現状は handle_new_user()(初期マイグレーション)がサインアップ時に id, email のみで行を自動作成するため、同じ id で profiles_insert_self 経由の追加INSERTはPK重複で失敗し、かつ profiles にはDELETEポリシーが無いため本人がその行を消して作り直すこともできず、実害はほぼ顕在化しません。ただしこれは「サインアップ時に必ず行が先に作られる」という別の実装(auth.usersトリガー)に暗黙に依存した安全性で、このポリシー単体では守れていません。将来profilesの作成経路が変わった場合の静かな抜け穴になり得るので、with check に and is_admin = false を加える(または guard_is_admin_immutable_trigger を before insert or update にする)方が、このガードの意図と一致すると思います。


create policy "profiles_update_self" on public.profiles
for update to authenticated
using (id = auth.uid())
with check (id = auth.uid());
Comment on lines +29 to +32

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

profiles_update_self が is_admin カラムの書き換えを制限していません。

using (id = auth.uid()) / with check (id = auth.uid()) は行の所有者しか
見ていないため、本人が自分の行に対して

update profiles set is_admin = true where id = auth.uid();

を実行すると通ってしまいます。

docs/permissions.md は「is_adminを参照する権限判定はRLSにもアプリ層にも一切書かない」と
明記していますが、これは「参照して分岐を作るな」という話であって、
「本人が自分でis_admin列を書き換えられる状態のまま放置してよい」ことまでは
意味していないはずです。現時点ではどこもis_adminを参照していないので実害はまだ
顕在化しませんが、フェーズ2で管理者判定を実装した瞬間、今のうちに
is_admin=trueを自分でセットしておいたユーザーがそのまま管理者権限を得ます。

eventsテーブルの削除ガードと同様に、is_adminの変更だけは
本人のUPDATEでも弾く(例: BEFORE UPDATEトリガーでold.is_admin <> new.is_adminを拒否)
などの対応が必要に見えます。


-- is_adminは本人のUPDATEでも書き換えられないようにする。参照して分岐を作ってはいないが
-- (docs/permissions.mdの禁止範囲)、書き換え自体を放置すると、フェーズ2で管理者判定を
-- 実装した瞬間に「今のうちに自分でis_admin=trueにしておいたユーザー」がそのまま
-- 管理者権限を得てしまう。service_role(将来の管理者操作用)はauth.role()で除外する。
create function public.guard_is_admin_immutable()
returns trigger
language plpgsql
set search_path = public
as $$
begin
if new.is_admin is distinct from old.is_admin and auth.role() <> 'service_role' then
raise exception 'is_admin cannot be changed by the profile owner';
end if;
return new;
end;
$$;

create trigger guard_is_admin_immutable_trigger
before update on public.profiles
for each row execute function public.guard_is_admin_immutable();
Comment on lines +34 to +53

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このguard_is_admin_immutableはnew.is_admin is distinct from old.is_adminという条件でis_adminを直接参照しています。意図(本人による書き換え防止)は理にかなっていて、権限判定にis_adminを使っているわけではないので実質的にはdocs/permissions.mdの禁止事項(「これを参照する権限判定は一切実装しない」)には抵触しないと考えますが、ファイル冒頭5行目の「profiles.is_adminを参照する条件は一切書かない」というコメント自体とは文字通り矛盾しています。

  • このガード自体はdocs/data-model.md/docs/permissions.mdのどちらにも記載がなく、権限マトリクスにもRLS方針表にも無いロジックです。フェーズ2の「同じPRで両層+テストを変更する」ルールの対象外だとしても、少なくともdocs/data-model.mdのis_adminの注記に一行追記しておかないと、次にこのカラムを触る人が「参照する条件は無いはず」という前提で読んでしまいそうです。
  • このトリガーの otherwise/service_role分岐(auth.role() <> 'service_role')も含めて、test/db/側のテストが本PRには無いので(#26で対応予定とのことですが)、少なくとも「本人がis_adminをtrueに書き換えようとして失敗する」ケースは他のRLS検証と合わせて#26でカバーされるよう明記しておくと安心です。


-- 他ユーザーに公開してよい列(id, display_name)のみを持つビュー。
-- 所有者(postgres)としてprofilesを参照するため、上記の自分のみSELECT可のRLSに関わらず
-- 全行を返す(profilesテーブル自体への直接アクセスは自分の行のみに制限したまま)。
create view public.profiles_public
with (security_invoker = false)
as
select id, display_name
from public.profiles;

grant select on public.profiles_public to authenticated;

-- 2. events (docs/data-model.md「RLSポリシー方針」、削除ガードはdocs/data-model.md 2章)
alter table public.events enable row level security;

-- 未削除の全イベントに加えて、削除後もオーナー自身と、その支出(expenses)を持つ
-- ユーザーは引き続き参照できるようにする(docs/data-model.md 5章「eventsは論理削除なので、
-- 支出から辿ってイベント名などは常に参照できる」)。
create policy "events_select_not_deleted_or_referenced" on public.events
for select to authenticated
using (
deleted_at is null
or owner_id = auth.uid()
or exists (
select 1
from public.expenses e
where e.event_id = events.id
and e.user_id = auth.uid()
)
);
Comment on lines +69 to +83

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このSELECTポリシーはdocs/data-model.md「RLSポリシー方針」表のevents行(「全ユーザー(deleted_at IS NULL)」)より広く、owner_id = auth.uid()と「自分のexpensesが参照しているイベント」の場合も削除後に見えるようにしています。根拠のコメント(5章の「支出から辿ってイベント名などは常に参照できる」)は妥当で、実装自体に異論はないのですが、この例外はdocs/data-model.mdのRLSポリシー方針表にもdocs/permissions.mdの権限マトリクスにも明文化されていません。

ドキュメント駆動の運用(CLAUDE.md)である以上、この表を「正」として読む次の実装者・レビュアーが「削除済みイベントは誰からも見えないはず」と誤解しないよう、docs/data-model.mdの方針表に一行反映しておくことを推奨します。


create policy "events_insert_as_owner" on public.events
for insert to authenticated
with check (owner_id = auth.uid());

create policy "events_update_owner_only" on public.events
for update to authenticated
using (owner_id = auth.uid())
with check (owner_id = auth.uid());

-- イベント削除(論理削除)のガード。オーナー本人以外の参加者が1人でもいる場合、
-- deleted_at をNULLから設定する更新を拒否する。MVPでは例外を設けない(管理者もバイパス不可)。
-- security definerが無いと、内部のEXISTS検査が呼び出し元(削除しようとしているオーナー)の
-- RLS越しに実行され、private参加者の行が見えず「参加者なし」と誤判定してガードが
-- すり抜けてしまう(handle_new_user()と同じ理由でsecurity definerが必要)。
create function public.guard_event_deletion()
returns trigger
language plpgsql
security definer set search_path = public
as $$
Comment on lines +99 to +103
begin
if new.deleted_at is not null and old.deleted_at is null then
if exists (
select 1
from public.event_participants ep
where ep.event_id = new.id
and ep.user_id <> new.owner_id
) then
raise exception 'event % has participants other than the owner and cannot be deleted', new.id;
end if;
end if;
return new;
end;
$$;
Comment on lines +99 to +117

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

削除ガードがRLS越しに沈黙してすり抜ける可能性があります。

guard_event_deletion() は security definer を付けていないため、内部の
exists (select 1 from public.event_participants ep where ...) は
呼び出し元(イベントを削除しようとしているオーナー)の権限、つまり
event_participants_select_own_or_public ポリシーの下で実行されます。

このポリシーは user_id = auth.uid() or visibility = 'public' の行しか見せません。
つまり、オーナー以外の参加者が visibility = 'private' で参加登録している場合、
このexists検査からはその行が見えず「参加者はいない」と判定されてしまい、
docs/data-model.md 2章・docs/permissions.md が要求する
「オーナー本人以外の参加者が1人でもいる場合は削除不可(例外なし)」の
ガードを静かにすり抜けて論理削除が成功してしまいます。

同じマイグレーション内の handle_new_user() (20260806003241_initial_schema.sql)は
まさに同じ理由(呼び出し元のRLSに関わらず内部で判定する必要がある)で
security definer を付けています。こちらの関数にも同様に
security definer が必要ではないでしょうか。

docs/permissions.md の「最小の検証セット」に "参加者がいるイベントをオーナーが削除 → 失敗" が
挙げられていますが、参加者のvisibilityがprivateのケースでこのテストを書けば
今のロジックのままでは赤くなるはずです。


create trigger guard_event_deletion_trigger
before update on public.events
for each row execute function public.guard_event_deletion();

-- 3. event_participants (docs/data-model.md「RLSポリシー方針」、招待の仕様は3章)
alter table public.event_participants enable row level security;

create policy "event_participants_select_own_or_public" on public.event_participants
for select to authenticated
using (user_id = auth.uid() or visibility = 'public');

-- 自分で登録する経路(invited_byなし) と、参加登録済みユーザーによる招待経路
-- (invited_by=自分。招待できるのは参加登録している任意のユーザー)の2通りのみ許可する。
-- 招待経路で作成する他人の行は、招待者が公開設定を勝手に指定できないよう
-- visibility='private'(既定値どおり)、participation_state='joined'
-- (docs/data-model.md 3章「MVPでの挙動」)に固定する。
create policy "event_participants_insert_self_or_invite" on public.event_participants
for insert to authenticated
with check (
(user_id = auth.uid() and invited_by is null)
or (
invited_by = auth.uid()
and visibility = 'private'
and participation_state = 'joined'
and exists (
select 1
from public.event_participants ep
where ep.event_id = event_participants.event_id
and ep.user_id = auth.uid()
)
)
);
Comment on lines +135 to +150
Comment on lines +135 to +150

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

event_participants_insert_self_or_invite の招待経路は「招待者自身が event_participants に行を持つこと」を必須にしています。一方 docs/data-model.md 2章は owner_id を「参加者ではない」役割として明示的に定義しており(「登録だけして自分は参加しない、というケースも表現できる」)、docs/permissions.md の権限マトリクスは「他ユーザーの招待」で「オーナー(登録者)」列を無条件で ○ としています。

このポリシーのままだと、自分自身は参加登録していないオーナーが誰かを招待しようとするとRLSに弾かれ、マトリクスの「オーナー ○」と食い違うケースが生まれます(オーナー = 参加登録済みの前提が常に成り立つなら問題ありませんが、data-model.md はその前提を明示的に否定しています)。

  • owner_id = auth.uid() を許可条件に追加してマトリクス通りにするか
  • あるいは「オーナーも招待するには自分で参加登録している必要がある」という制約を docs/permissions.md / docs/data-model.md 側に明記するか

どちらかで揃えたほうがいいと思います。


create policy "event_participants_update_self" on public.event_participants
for update to authenticated
using (user_id = auth.uid())
with check (user_id = auth.uid());

create policy "event_participants_delete_self" on public.event_participants
for delete to authenticated
using (user_id = auth.uid());

-- 4. ticket_entries (docs/data-model.md「RLSポリシー方針」。個人スコープ)
alter table public.ticket_entries enable row level security;

create policy "ticket_entries_select_own" on public.ticket_entries
for select to authenticated
using (user_id = auth.uid());

create policy "ticket_entries_insert_own" on public.ticket_entries
for insert to authenticated
with check (user_id = auth.uid());

create policy "ticket_entries_update_own" on public.ticket_entries
for update to authenticated
using (user_id = auth.uid())
with check (user_id = auth.uid());

create policy "ticket_entries_delete_own" on public.ticket_entries
for delete to authenticated
using (user_id = auth.uid());

-- 5. expenses (docs/data-model.md「RLSポリシー方針」。個人スコープ)
alter table public.expenses enable row level security;

create policy "expenses_select_own" on public.expenses
for select to authenticated
using (user_id = auth.uid());

create policy "expenses_insert_own" on public.expenses
for insert to authenticated
with check (user_id = auth.uid());

create policy "expenses_update_own" on public.expenses
for update to authenticated
using (user_id = auth.uid())
with check (user_id = auth.uid());

create policy "expenses_delete_own" on public.expenses
for delete to authenticated
using (user_id = auth.uid());

-- 6. budgets (docs/data-model.md「RLSポリシー方針」。個人スコープ)
alter table public.budgets enable row level security;

create policy "budgets_select_own" on public.budgets
for select to authenticated
using (user_id = auth.uid());

create policy "budgets_insert_own" on public.budgets
for insert to authenticated
with check (user_id = auth.uid());

create policy "budgets_update_own" on public.budgets
for update to authenticated
using (user_id = auth.uid())
with check (user_id = auth.uid());

create policy "budgets_delete_own" on public.budgets
for delete to authenticated
using (user_id = auth.uid());
Comment on lines +217 to +219
Loading