From 51cdc769939a4137469033e1cf663a990ec88890 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 1 Nov 2026 10:03:22 +0000 Subject: [PATCH 1/2] docs(auth): record why grants are readable without request context U-2 opens SELECT on member_roles to app_rls unconditionally, an exception to "enforce in the database" (3.4). Record the reason, what stays restricted, what it obliges the API to do, and the alternatives that were rejected, so the decision is findable outside the migration comment. Also reconcile 3.2 with share/permissions.ts: the list in the code is the subset it refers to by name, not a second source of truth. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vm1iXpgdwQfcWuyNzX9KF7 --- docs/aidlc/components.md | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/docs/aidlc/components.md b/docs/aidlc/components.md index 14c44ba..6c30fdf 100644 --- a/docs/aidlc/components.md +++ b/docs/aidlc/components.md @@ -89,7 +89,11 @@ Cloudflare の Service Bindings は採らない。速度で有利だが、 **権限もロールへの割り当ても、すべてデータとして持つ**(2026-09-03 決定)。 権限は行として存在し、ロールとの対応も行として持つ。 -コード側に権限の一覧を持たない。 +コード側に権限の一覧を持たない。権限を存在させるのはデータベースの行だけである。 + +ただし、コードが名前で参照する権限は `share/permissions.ts` に書き出す(U-2)。 +これは権限の正典ではなく、綴りの誤りをコンパイラに弾かせるための部分集合である。 +データベースにあってコードにない権限は許す。逆は下の検査で失敗させる。 **この方式の弱点と対策** @@ -150,6 +154,38 @@ RLS ポリシーの4箇所も同様に、渡す識別子をロール名から権 最後の2つは、既存の `member_status_history` と同じ方式(トリガーが書き、アプリは書けない)にする。 +### 3.5 ロール付与の読み取りはデータベースで絞らない(U-2 で決定) + +`member_roles` の SELECT は、`app_accounts` と同じく `app_rls` に無条件で許す(`using (true)`)。 +3.4 の「データベース側で守る」の例外として、範囲と理由をここに残す。 + +**理由** + +- 権限は呼び出し元を解決する処理の中で読む。その時点ではまだ `app.current_member_id` が設定されていない。 + このため、本人の行に絞るポリシーでは結果が空になり、**全員の権限がゼロになる**。 + しかも見た目は権限のバグで、実際の原因である読み取り順序にはたどり着きにくい +- 絞っても守れるものがほぼない。`app_rls` は `app_accounts.role` を既に無条件で読めるので、 + アプリ層が侵害された時点で誰が管理者かは分かってしまう。 + また、ロールの付与は認可のための情報であり、部員の個人情報ではない。 + 個人情報を守っているのは `members` のポリシーと名簿ビュー + +**緩めないもの** + +- 付与と剥奪(INSERT / DELETE)は今まで通り制限する。権限昇格につながるのはこちら。 + 申告されたロールを実値と突き合わせる対策は U-2.5 で行う(`unit-of-work.md`) + +**この決定の帰結** + +- 部員のロールを画面に出す API は、**誰に何を見せるかを API 側で絞る**。データベースは絞ってくれない +- U-3 で RLS ポリシーを権限ベースへ変えるとき、ポリシーの中から `member_roles` を参照してよい + +**採らなかった案** + +- 呼び出し元の解決を2つのトランザクションに分け、2つ目で `app.current_member_id` を設定してから読む。 + 読み取りの順序という見えにくい前提に正しさを頼ることになる。 + 順序を誤ったときの症状(全員の権限がゼロ)も原因にたどり着きにくい +- SECURITY DEFINER 関数で権限を返す。仕組みが増えるわりに、上の理由で守れるものがない + --- ## 4. 書き込みの共通化 From 261c1c369e551948c9cf5bdb3d5b2a1d99061e73 Mon Sep 17 00:00:00 2001 From: Claude Date: Sun, 1 Nov 2026 10:50:49 +0000 Subject: [PATCH 2/2] test(db): read grants with no request context, as the middleware does The assertion that grants are readable ran with the member's own context set, so a policy scoped to the member's own rows passed it too. Narrowing the policy back would have left every caller with no permissions while the suite stayed green: nothing else reads member_roles as app_rls without context (the migration test reads as superuser, the join test uses its own tables). Clear the context before the read and restore it after. The assertion count is unchanged. Verified against PGlite with the migrations replayed: with no context set, the current policy returns the grant and the old self-only policy returns nothing. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01Vm1iXpgdwQfcWuyNzX9KF7 --- supabase/tests/004_roles.test.sql | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/supabase/tests/004_roles.test.sql b/supabase/tests/004_roles.test.sql index 653fe67..aa4924f 100644 --- a/supabase/tests/004_roles.test.sql +++ b/supabase/tests/004_roles.test.sql @@ -143,12 +143,27 @@ select throws_ok( -- Grants are readable to the application, like app_accounts: resolving who is -- calling happens before any request context exists. Scoping what a person is -- shown is the API's job; RLS here is about who may change a grant. +-- +-- So read them the way the auth middleware does, with no context set. Read with +-- the member's context instead and a policy scoped to the member's own rows +-- would pass too, and narrowing it back would leave every caller with no +-- permissions while this suite stayed green. +select + set_config('app.current_user_id', '', true), + set_config('app.current_member_id', '', true), + set_config('app.current_user_role', '', true); + select results_eq( $$select role_key from public.member_roles$$, $$values ('member'::text)$$, - 'grants are readable so a caller can be resolved at all' + 'grants are readable before the caller is resolved' ); +select + set_config('app.current_user_id', 'roles-member', true), + set_config('app.current_member_id', '00000000-0000-4000-8000-00000000030a', true), + set_config('app.current_user_role', 'user', true); + -- A DELETE that no policy admits is filtered, not rejected: the statement -- succeeds having removed nothing. Only the surviving row proves the point, so -- asserting an exception here would have passed for the wrong reason.