Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion docs/aidlc/components.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,11 @@ Cloudflare の Service Bindings は採らない。速度で有利だが、
**権限もロールへの割り当ても、すべてデータとして持つ**(2026-09-03 決定)。

権限は行として存在し、ロールとの対応も行として持つ。
コード側に権限の一覧を持たない。
コード側に権限の一覧を持たない。権限を存在させるのはデータベースの行だけである。

ただし、コードが名前で参照する権限は `share/permissions.ts` に書き出す(U-2)。
これは権限の正典ではなく、綴りの誤りをコンパイラに弾かせるための部分集合である。
データベースにあってコードにない権限は許す。逆は下の検査で失敗させる。

**この方式の弱点と対策**

Expand Down Expand Up @@ -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. 書き込みの共通化
Expand Down
17 changes: 16 additions & 1 deletion supabase/tests/004_roles.test.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading