Skip to content

fix(api): review links were unreachable, and the org member list read a table that never existed (#654, #655) - #667

Merged
thinkclay merged 1 commit into
mainfrom
claude/fix-review-links-and-org-members
Aug 27, 2026
Merged

thinkclay merged 1 commit into
mainfrom
claude/fix-review-links-and-org-members

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Closes #654. Closes #655.

#654 — core.review_links

RLS enabled, grants only to postgres, and no policies at all. Every endpoint touching it answered permission denied for table review_links — including GET /v1/reviews/links/by-token/{token}, the reviewer's entry point from a shared link. Review collection was dead end to end.

That route already used the service client because it's anonymous. service_role bypasses RLS but still needs the table GRANT, which is why reaching for the service client didn't save it. Same class as #481 and migrations 332/335.

Migration 347 grants authenticated and service_role, and adds the four missing policies, each scoped to subject_user_id = auth.uid().

Deliberately nothing for anon: a reviewer reaches a link through the service client, which checks expiry, revocation and use count first. Granting anon a direct SELECT would let anyone enumerate the table through PostgREST, and the token is the only secret protecting it.

Two things found while testing it

  • The list had no filter. GET /v1/reviews/links selected with no subject_user_id and leaned entirely on RLS. With no policy that failed closed — but the policy was the only thing between a caller and every share link on the platform. Given an explicit filter too.
  • Revoking someone else's link answered 200 and did nothing. An UPDATE matching no rows isn't an error, and the handler never read back the row. Now 404 — not 403, so the response doesn't confirm the link exists.
check result
list / create ✅ 200 / 201
by-token as an anonymous caller ✅ 200 with subject profile
bad token / revoked token ✅ 404 / 404
another user listing your links ✅ 0
another user revoking your link ✅ 404, is_revoked unchanged
anon hitting /rest/v1/review_links directly ✅ 401

#655 — GET /v1/organizations/{id}/members

Read core.organization_members, which exists in no schema, so it returned 500 for every organization. Membership is a role scoped to the org — core.role_assignments.scope_org_id — which is what lib/org-access.ts, /{id}/location-visibility and /{id}/projects-with-overrides have always used. The members route was the odd one out.

Rewritten against role_assignments, gated by canReadOrgInternals (404 for a non-member, so it doesn't confirm which org ids exist), returning the OrganizationMember shape the SDK declares.

Three things the rewrite had to get right

RLS made the first attempt look fine. role_assignments carries one policy — user_id = auth.uid() — so reading it with the caller's own client returns a one-member organization. The seeded Unicorn org came back as 1 of 15 and looked plausible. The access check above is what makes the service client safe here.

A person can hold several roles, and some hold the same role twice. 15 assignments group to 11 people, deduplicated — matching count(distinct user_id) exactly.

Search filtered on a uuid. The old code ran ilike against user_id, so it could only ever match nothing. Now searches display_name and username, and roleNames — declared in the SDK's ListMembersParams and previously ignored — is honoured.

check result
member list ✅ 11, matching count(distinct user_id)
duplicate admin,admin assignments ✅ collapse to one role
?search=marc ✅ Marc Gigliotti
?roleNames=admin / =member ✅ 11 / 0
non-member ✅ 404

One runtime-only bug this surfaced

firstOf was referenced by the new handler but defined privately in work-logs.ts. That typechecks under the current deno config and throws at runtime — it reached a live probe before anything caught it. Now lib/postgrest.ts, imported by both, with the work-logs public feed re-probed for regression.

Numbers

  • Schema guard baseline: 33 → 32 references
  • API suite: 325 passed / 80 failed against 317 / 80; failure sets diffed by name, zero newly failing
  • 8 new tests, pinned as rules (service-client-after-gate, grouping, no-uuid-search, both grants, every policy owner-scoped, no anon)

Migration 347 is applied locally; it still needs applying to dev/preview/prod.

🤖 Generated with Claude Code

… a table that never existed (#654, #655)

## #654 — core.review_links

RLS enabled, grants only to postgres, and no policies at all. Every endpoint
that touched it answered `permission denied for table review_links`, including
GET /v1/reviews/links/by-token/{token} — the reviewer's entry point from a
shared link, so review collection was dead end to end.

That route already used the service client because it is anonymous. service_role
bypasses RLS but still needs the table GRANT, which is why reaching for the
service client did not save it. Same class as #481 and migrations 332/335.

Migration 347 grants authenticated and service_role, and adds the four policies
the table was missing, each scoped to subject_user_id = auth.uid(). Deliberately
nothing for anon: a reviewer reaches a link through the service client, which
checks expiry, revocation and use count first, and granting anon a direct SELECT
would let anyone enumerate the table through PostgREST when the token is the only
secret protecting it.

Two things found while testing it:

  - GET /v1/reviews/links selected with no subject_user_id filter and leaned
    entirely on RLS to scope the result. With no policy that failed closed, but
    the policy was the only thing between a caller and every share link on the
    platform. Given an explicit filter as well.
  - Revoking another user's link answered 200 and did nothing. An UPDATE that
    matches no rows is not an error, and the handler never read back the row.
    Now 404 — not 403, so the response does not confirm the link exists.

## #655 — GET /v1/organizations/{id}/members

Read core.organization_members, which exists in no schema, so it returned 500
for every organization. Membership is a role scoped to the org —
core.role_assignments.scope_org_id — which is what lib/org-access.ts,
/{id}/location-visibility and /{id}/projects-with-overrides have always used.

Rewritten against role_assignments, gated by canReadOrgInternals (404 for a
non-member, so it does not confirm which org ids exist), returning the
OrganizationMember shape the SDK declares.

Three things the rewrite had to get right, each verified against live data:

  - role_assignments carries one policy, `user_id = auth.uid()`, so reading it
    with the caller's own client returns a one-member organization. The seeded
    Unicorn org came back as 1 of 15. The access check above is what makes the
    service client safe here.
  - A person can hold several roles in one org, and some hold the same role
    twice. 15 assignments group to 11 people, deduplicated.
  - Search ran `ilike` against user_id, a uuid, so it could only ever match
    nothing. It now searches display_name and username, and roleNames — declared
    in the SDK's ListMembersParams and previously ignored — is honoured.

firstOf was referenced by the new handler but defined privately in work-logs.ts.
That typechecks under the current deno config and throws at runtime, which is how
it reached a live probe; it is now lib/postgrest.ts, imported by both.

Verified against a live stack: 11 members matching the database, search and
roleNames filtering correctly, 404 for a non-member; review links list, create,
by-token as an anonymous caller, 404 on a bad or revoked token, another user
seeing none of them, and anon unable to enumerate the table directly.

Schema guard baseline 33 -> 32 references. API suite 325 passed / 80 failed
against 317 / 80; failure sets diffed by name, zero newly failing.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit abc1d42 into main Aug 27, 2026
6 of 15 checks passed
@thinkclay
thinkclay deleted the claude/fix-review-links-and-org-members branch August 27, 2026 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant