Repository navigation
feat(auth): resolve what a caller may do, not what they are called - #76
Merged
Merged
Conversation
Adds the permission side of the role work. Nothing checks permissions yet — U-3 moves the call sites — so this changes no behaviour: it puts the resolved permissions on the caller and gives the code a way to ask about them. Permissions load in a second transaction, after the identity is installed. member_roles is readable only by the member it belongs to, and that policy needs app.current_member_id, which is not set until the caller has been resolved. Reading it in the first transaction returns nothing and would quietly leave everyone with no permissions at all — a failure that looks like a permissions bug rather than an ordering one. The listed keys are not the source of truth; the database is, as Q-2 settled. They are the subset the code names, written down so that `member.readPrivate` fails to compile rather than becoming a string nobody holds and a check that never passes. A test asserts every listed key exists in the database, which is the mitigation the design records for keeping permissions as data. It was confirmed to fail on a deliberately misspelled key rather than assumed to work. Implements U-2 (docs/aidlc/unit-of-work.md) for FR-4.5 and NFR-1.1. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The permission lookup was in its own transaction, which was the wrong fix for a policy that should not have been written that way. member_roles was readable only by the member it belongs to, and satisfying that needs the request context — which does not exist until the caller has been resolved. The ordering hazard was real: read it too early and everyone silently has no permissions. app_accounts is readable to the application unconditionally for exactly this reason, and it already carries the same fact — who is an administrator. A grant is authorization metadata, not a member's personal information; the directory view and the policies on members are what protect that. So member_roles now reads the same way, and writing it stays restricted. With the policy right, the aggregate belongs in the query that was already fetching the account. A caller now resolves in one transaction and two queries: one against the authentication store, one against the domain — the boundary the design cares about, unchanged. The separate loader is gone, and so is the window where the two reads could disagree. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…ribe itself The permission list was built with a hand-written array_agg wrapped in coalesce and a filter clause, embedded in the selection. Drizzle has no array-aggregate helper, so that was the only way to do the aggregation in SQL — but the aggregation itself was not needed. Selecting the joined rows says the same thing in the builder's own vocabulary: account columns, one row per permission the member's roles carry, left joins because a caller may have no member row and a member may hold no roles. The grouping, the coalesce and the null filter all go away, and deduplicating two roles that grant the same permission is a Set. The rows this collapses are few — a member's roles times their permissions — so nothing is gained by making the database do it, and the query is now readable by anyone who knows drizzle rather than anyone who knows postgres aggregate syntax. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The suite declared fifteen assertions and made sixteen, which pgTAP reports as a failed test. That is the second miscount in this branch, and both times it surfaced only in the CI job that needs Docker — not available on the machine editing the file, so the feedback arrived several minutes and one push later. Counting them locally is a file read. The plan sits twenty lines from the assertions it counts, so adding or splitting one will keep getting this wrong; now it fails immediately and says which file and by how much. Confirmed by declaring the wrong number on purpose rather than assuming the check works. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Deduplication here was spelled with a spread over a Set, inline in the object being returned. Everywhere else — collectDiscordUserIds, parseTags, splitProfileTags — it is Array.from(new Set(...)) inside a named function, so the transformation has a name and the call site reads as one thing. filter(Boolean) is the shorter form those use, but it does not narrow away null here; the comparison stays because the column is nullable and the type has to follow. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
The helper existed to hold a hand-written row type that restated the shape of the selection above it, and to run map and filter so the nulls a left join produces could be dropped with the type following along. flatMap with `?? []` drops them in one pass and infers string[] on its own, and the same line already collects Discord user IDs in this codebase. Inline, it needs no invented type and no name — it is one expression that says what it returns. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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
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
…ude/epic-wright-9zzc8u
docs+test(auth): record and test why grants are readable without request context
yufoxda
pushed a commit
that referenced
this pull request
Nov 1, 2026
…comments - unit-of-work.md: the 2026-11-01 revision is approved by the requester; U-2 is done now that #76 is merged. - README.md: status follows. - intent.md 6.3: the code is a teaching material for people a little way into TypeScript who want to step up by reading a real service (revised from "not beginners, successors who can already do some"). Code comments are written in Japanese. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Vm1iXpgdwQfcWuyNzX9KF7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
U-2(
docs/aidlc/unit-of-work.md)の実装です。要件 FR-4.5・NFR-1.1 に対応します。挙動は変わりません。 まだ誰も権限で判定していません(それは U-3)。この PR は「呼び出し元が何をしてよいか」を解決し、それを問い合わせる手段を用意するところまでです。
追加したもの
share/permissions.ts— コードが名前で参照する権限の一覧と、問い合わせ関数。appUser.permissions— 部員が持つロールを辿って解決した権限。member / community の両方に入れました。権限は呼び出し元の解決と同じトランザクションで読みます
member_rolesの SELECT ポリシーはapp.current_member_idを必要としていましたが、この値は呼び出し元を解決するまで設定されません。そのままでは結果が空になり、全員の権限がゼロになります。しかもそれは「権限のバグ」に見えて、実際は読み取りの順序の問題なので気づきにくいです。そこで
member_rolesの SELECT を、app_accountsと同じくusing (true)に緩めました(20260904000000_add_role_tables.sql)。app_rlsは既にapp_accounts.roleを無条件で読めるからです。ロールの付与は認可のための情報で、部員の個人情報ではありません設計記録(
docs/aidlc/components.md3.5)と、この挙動を守る pgTAP は #77 で追加しています。データ方式の弱点を埋める検査
権限はデータベースが正典です(設計 Q-2)。
share/permissions.tsの一覧は正典ではなく、コードが名前で参照する部分集合です。これがないとmember.readPrivateと綴り間違えても素通りし、「誰も持っていない権限」を要求する判定になります。一覧の全キーがデータベースに存在することを検査するテストを追加しました。 これは設計に記録した対策そのものです。
この検査が実際に働くことを確認済みです。 権限名を綴り間違いに書き換えて実行し、狙いどおり失敗することを確かめました。
逆向き(データベースにあってコードにない)は許容します。まだ誰も判定していない権限を先に入れておけるためです。
検証
database・member・applicationすべて green🤖 Generated with Claude Code