Skip to content

fix(citations): enforce org membership in the security-definer citation RPCs - #780

Merged
gkhngyk merged 1 commit into
mainfrom
fix/citations-rpc-org-guard
Aug 23, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
fix/citations-rpc-org-guard

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

Summary

Five citation RPCs are security definer and granted to authenticated, and none of them checked who was asking. They cannot stop being definer — they read citation_urls, a cross-tenant dictionary with no select policy of its own, so an invoker-rights version would return nothing to anyone. Definer rights mean RLS is never consulted, which left the tenant boundary resting on the web tier sending the right p_brand_id rather than on the database enforcing membership.

citations_domains, citations_urls, citations_window_stats, citation_url_candidates and citation_url_occurrences now carry the allowed CTE that report_citation_evidence (00063) and the gap functions (00068) already use: the base scan filters to nothing unless the caller's profile shares the brand's organization. A non-member reads empty rather than hitting an error, so the page falls to its own empty state. citation_url_ids has the same shape but no authenticated grant, so it is unreachable and left alone.

Bodies are otherwise unchanged — the guard is the only edit.

Verified against the live database

Compared on a brand with 190,620 citation rows, each guarded body run as a member of the brand's organization and diffed against the function it replaces. Row counts equal and the set difference empty in both directions:

Function Rows Difference
citations_domains 10,884 0 / 0
citations_urls 2,000 0 / 0
citations_window_stats 14,505 answers regions array equal
citation_url_candidates 2,065 0 / 0
citation_url_occurrences 341 0 / 0

A profile belonging to a different organization resolves the guard to zero rows, so every scan filters to nothing.

One consequence, stated in the function comments

auth.uid() is null under the service role, so a service-role caller now reads empty. Nothing calls these that way today: all five have exactly one caller, web/src/lib/actions/citations.ts, and every call site uses the user-session createClient(). The MCP layer runs its own queries behind its own organization_id ownership check, and server/src never references them. A future service-role caller has to come through a user context or change this deliberately.

Related issue

Closes #779

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

Requires the migration to be applied.

  1. Open Citations on a brand you belong to: domains, URLs, source types, the per-URL detail page and its occurrence table all render exactly as before, over the same numbers.
  2. Sign in as a member of another organization and call any of the five with the first brand's id — the result is empty, not an error and not data.
  3. The Competitor Gaps tab is untouched; it already had this guard from 00068.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • supabase/schema.sql regenerated from the migrations
  • Changes are focused — one concern per PR

…on RPCs

Five citation RPCs are security definer and granted to authenticated, and
none of them checked who was asking. They cannot stop being definer — they
read citation_urls, a cross-tenant dictionary with no select policy of its
own, so an invoker-rights version returns nothing to anyone. Definer rights
mean RLS is never consulted, which left the tenant boundary resting on the
web tier passing the right p_brand_id rather than on the database. Brand ids
are unguessable uuids and the payloads are aggregates, so exposure was small,
but this is the check report_citation_evidence (00063) and the gap functions
(00068) already carry.

Add their `allowed` CTE to citations_domains, citations_urls,
citations_window_stats, citation_url_candidates and citation_url_occurrences:
the base scan filters to nothing unless the caller's profile shares the
brand's organization. A non-member reads empty rather than hitting an error,
so the page falls to its empty state. citation_url_ids has the same shape but
no authenticated grant, so it is unreachable and untouched.

Bodies are otherwise unchanged. Verified on a brand with 190,620 citation
rows: all five compared against the live functions for a member of the org,
row counts equal and set difference zero in both directions — 10,884 domains,
2,000 urls, the window-stats row, 2,065 url candidates and 341 occurrences.
A profile from another organization resolves the guard to zero rows.

One consequence, stated in the function comments: auth.uid() is null under
the service role, so a service-role caller now reads empty. Nothing calls
these that way — every call site is in web/src/lib/actions/citations.ts on
the user-session client, the MCP layer runs its own queries behind its own
ownership check, and server/src never touches them.
@github-actions github-actions Bot added the needs-migration Adds or edits a migration — apply it to the hosted database label Aug 23, 2026
@gkhngyk
gkhngyk merged commit 42d2142 into main Aug 23, 2026
5 checks passed
@gkhngyk
gkhngyk deleted the fix/citations-rpc-org-guard branch August 23, 2026 13:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add the org-membership guard to the five security-definer citation RPCs

1 participant