Skip to content

fix(db): grant the roles six core policies already name (#755) - #758

Merged
thinkclay merged 1 commit into
mainfrom
claude/fix-review-pins-grants
Sep 14, 2026
Merged

thinkclay merged 1 commit into
mainfrom
claude/fix-review-pins-grants

Conversation

@thinkclay

Copy link
Copy Markdown
Contributor

Closes #755.

A policy decides which rows a role may see. A GRANT decides whether it may touch the table at all. Six core tables carry policies naming anon or authenticated and issue no grant to those roles, so the policies are unreachable:

permission denied for table review_pins
HINT: Grant the required privileges to the current role with:
      GRANT SELECT ON core.review_pins TO authenticated;

Postgres names the fix — but only for whoever reads the raw error. Through the API it's a 401 or a 500.

Found smoke-testing production: a public profile reads core.review_pins directly and it failed for anonymous and signed-in visitors. Unlike the service_role drift in #751 this isn't an environment difference — local has the same hole, so pinned reviews have never worked anywhere. The writes go through pin_review/unpin_review, which are SECURITY DEFINER and therefore worked, so a user could pin a review and never see it pinned.

The sweep found five more

table role policy commands
generic_invitations authenticated INSERT, SELECT, UPDATE
inquiry_audit_log authenticated INSERT
inquiry_reminders authenticated SELECT
invitation_rules authenticated SELECT
review_pins anon SELECT
review_pins authenticated DELETE, INSERT, SELECT, UPDATE
user_relationships authenticated INSERT, SELECT, UPDATE

Each grant is exactly the commands that table's policies already cover, so nothing is opened that a policy author hadn't already decided to open. Same class as #481/#654 and migrations 332/335/347/352/355 — each of which fixed one table, as each was found by something failing.

core.user_skills is deliberately left alone

Same shape — user_skills_select_public names anon with no grant — but a grant is the wrong fix. The predicate reads core.preferences to check profile_visibility -> skills, and anon can't read that either, so granting user_skills alone only moves the error:

permission denied for table preferences
HINT: GRANT SELECT ON core.preferences TO anon;

Taking that hint would expose every user's preferences to anonymous readers. It needs a SECURITY DEFINER helper for the visibility lookup — a design change, not a missing grant. I granted it, watched the error move, and backed it out. The API path is unaffected: the public skills widget reads through the service client behind an explicit visibility check (#732).

The guard

Asks the database the question a user's request asks — can this role actually read this table — rather than reading the catalogue. That also catches the user_skills case, where the grant would exist and the predicate still couldn't be evaluated. user_skills is pinned as a known gap, so fixing the predicate fails the test and the exception gets removed rather than rotting.

Verified against a live stack

check result
review_pins read, anonymous ✅ 200 (was 401)
review_pins read, authenticated ✅ 200 (was 403)
pin_review → read the pin back anonymously ✅ [{"review_id":"…","position":1}]
guard non-vacuous ✅ revoking the grant fails 2 of 3

API suite 349 passed / 79 failed — the known pre-existing set; nothing in pins, grants or skills.

🤖 Generated with Claude Code

A policy decides which rows a role may see. A GRANT decides whether it may touch
the table at all. Six core tables carry policies naming `anon` or
`authenticated` and issue no grant to those roles, so the policies are
unreachable and the tables answer:

    permission denied for table review_pins
    HINT: Grant the required privileges to the current role with:
          GRANT SELECT ON core.review_pins TO authenticated;

Postgres names the fix, but only for whoever reads the raw error. Through the
API it is a 401 or a 500.

Found smoke-testing production: a public profile reads core.review_pins
directly and it failed for anonymous *and* signed-in visitors. Unlike the
service_role drift in #751 this is not an environment difference — local has the
same hole, so pinned reviews have never worked anywhere. The writes go through
the pin_review / unpin_review RPCs, which are SECURITY DEFINER and therefore
worked, so a user could pin a review and never see it pinned.

The sweep that found it found five more:

    generic_invitations  authenticated  INSERT, SELECT, UPDATE
    inquiry_audit_log    authenticated  INSERT
    inquiry_reminders    authenticated  SELECT
    invitation_rules     authenticated  SELECT
    review_pins          anon           SELECT
    review_pins          authenticated  DELETE, INSERT, SELECT, UPDATE
    user_relationships   authenticated  INSERT, SELECT, UPDATE

Each grant is exactly the commands that table's policies already cover, so
nothing is opened that a policy author had not already decided to open. Same
class as #481 and #654, and migrations 332 / 335 / 347 / 352 / 355 — each of
which fixed one table, as each was found by something failing.

## core.user_skills is deliberately left alone

It has the same shape — user_skills_select_public names `anon` with no grant —
but a grant is the wrong fix. The predicate reads core.preferences to check
profile_visibility -> skills, and `anon` cannot read that either, so granting
user_skills alone only moves the error:

    permission denied for table preferences
    HINT: GRANT SELECT ON core.preferences TO anon;

and taking that hint would expose every user's preferences to anonymous
readers. It needs a SECURITY DEFINER helper for the visibility lookup — a design
change rather than a missing grant. I granted it, saw the error move, and backed
it out. The API path is unaffected: the public skills widget reads through the
service client behind an explicit visibility check (#732).

## The guard

The test asks the database the question a user's request asks — can this role
actually read this table — rather than reading the catalogue. That also catches
the user_skills case, where the grant would exist and the predicate still could
not be evaluated. user_skills is pinned as a known gap, so fixing the predicate
fails the test and the exception gets removed rather than rotting.

Verified against a live stack: anonymous and authenticated reads of review_pins
both 200; pin_review then reading the pin back anonymously returns it. Confirmed
non-vacuous — revoking the grant fails two of the three tests.

API suite 349 passed / 79 failed, the known pre-existing set; nothing in pins,
grants or skills.

Co-Authored-By: Claude Opus 5 <[email protected]>
@thinkclay
thinkclay merged commit d2f52ff into main Sep 14, 2026
9 of 10 checks passed
@thinkclay
thinkclay deleted the claude/fix-review-pins-grants branch September 14, 2026 22:43
@github-actions

Copy link
Copy Markdown
Contributor

📊 Performance Benchmark Results


thinkclay added a commit that referenced this pull request Sep 15, 2026
…green again (#763)

`Lint REST API Code` has been failing on `main` since #758 merged
(d2f52ff, 2026-09-14T22:43Z). `deno fmt --check` in
packages/supabase/functions/api finds one unformatted file — a three-line
import that fits on one — and fails the whole job, so every PR opened since
inherits a red check it did not cause.

    error: Found 1 not formatted file in 146 files

`deno lint` is clean, and this is `deno fmt`'s own output, so there is nothing
to decide about it.

Co-authored-by: Claude Opus 5 <[email protected]>
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.

core.review_pins has four policies and no grants, so pinned reviews are unreadable for everyone

1 participant