Skip to content

docs+test(auth): record and test why grants are readable without request context - #77

Merged
yufoxda merged 3 commits into
feat/permission-checksfrom
claude/epic-wright-9zzc8u
Nov 1, 2026
Merged

yufoxda merged 3 commits into
feat/permission-checksfrom
claude/epic-wright-9zzc8u

Conversation

@yufoxda

@yufoxda yufoxda commented Nov 1, 2026 •

Copy link
Copy Markdown
Contributor

#76 の補足 PR です。マージ先は feat/permission-checks です。

背景

#76 で member_roles の SELECT を using (true) に緩めました。これは 3.4「アプリではなくデータベース側で守る」の例外にあたります。それにもかかわらず、理由は migration のコメントにしか書かれていませんでした。さらに、この変更を守るテストもありませんでした。

変更

1. 設計記録(docs/aidlc/components.md)

3.5 を追加:「ロールの付与情報はデータベースで絞らない」という決定を記録しました。

  • 理由:権限を読む時点では app.current_member_id がまだ設定されていません。また、app_accounts.role は既に無条件で読めるので、絞っても守れるものがほぼありません
  • 緩めないもの:付与と剥奪(INSERT / DELETE)。偽装への対策は U-2.5 で行います
  • 帰結:ロールを返す API は、見せる範囲を API 側で絞る必要があります。U-3 では RLS ポリシーから member_roles を参照してかまいません
  • 採らなかった案:2つのトランザクションに分ける案、SECURITY DEFINER 関数を使う案

3.2 の矛盾を解消:3.2 の「コード側に権限の一覧を持たない」は、share/permissions.ts と矛盾していました。そこで、あの一覧は正典ではなく、コードが名前で参照する部分集合であると追記しました。

2. pgTAP(supabase/tests/004_roles.test.sql)

付与が読めることを確かめる assert は、部員本人の文脈が設定された状態で実行されていました。そのため、本人の行だけを見せるポリシーに戻しても通ってしまいます。ほかに app_rls として文脈なしで member_roles を読むテストもありません(migration のテストはスーパーユーザーで読み、join のテストは自前のテーブルを使っています)。つまり、ポリシーを戻すと全員の権限がゼロになるのに、テストはすべて通る状態でした。

そこで、ミドルウェアと同じく文脈を空にしてから読み、読んだ後に元の文脈に戻すようにしました。assert の数は変わらないため、plan もそのままです。

検証:この環境では Docker が使えないため、pgTAP そのものは CI の database job で確認することになります。代わりに PGlite に migration を流し込み、文脈を設定しない状態で読み取りを試しました。今のポリシーでは付与が 1 行見え、元のポリシーに戻すと 0 行になることを確認しています。pgtap-plan.test.ts を含む member のテストは 23 件すべて成功しました。

CI について

member と application の失敗は、この PR の変更が原因ではありません(依存パッケージの audit が原因です。コメント参照)。修正は #78 で develop に出しています。

🤖 Generated with Claude Code

https://claude.ai/code/session_01Vm1iXpgdwQfcWuyNzX9KF7

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vm1iXpgdwQfcWuyNzX9KF7

yufoxda commented Nov 1, 2026

Copy link
Copy Markdown
Contributor Author

member チェックの失敗は、この PR の変更が原因ではありません。

落ちているのは Audit member runtime dependencies(npm audit --omit=dev --audit-level=high)です。この PR は docs/aidlc/components.md しか変更していません。

  • 原因:feat(auth): resolve what a caller may do, not what they are called #76 の CI が通った 9 月 9 日より後に、脆弱性情報が新たに公開されました
  • develop・main・feat/permission-checks はどれも zod 4.4.3 のままなので、今 CI を回せばどのブランチでも同じ理由で落ちます。手元で確認したところ、community と frontend の audit も同様に失敗します
  • 修正はまだどのブランチにも入っていません

修正案(未適用):develop の member で npm audit fix --omit=dev を実行すると、変更は package-lock.json のみ(zod 4.4.3 → 4.6.7、hono 4.13.5 → 4.13.16)で、audit の脆弱性は 0 件になりました。npm ci で入れ直したうえで、テスト(21 件すべて成功)と型チェックも通っています。community と frontend にも同じ対応が必要です。

docs だけの PR に依存関係の更新を混ぜないよう、修正は develop 向けの別 PR にすることを提案します。


Generated by Claude Code

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 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Vm1iXpgdwQfcWuyNzX9KF7
@yufoxda yufoxda changed the title docs(auth): record why grants are readable without request context docs+test(auth): record and test why grants are readable without request context Nov 1, 2026
@yufoxda
yufoxda merged commit 5ecbb8a into feat/permission-checks Nov 1, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants