Repository navigation
fix(citations): enforce org membership in the security-definer citation RPCs - #780
Merged
Merged
Conversation
…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.
This was referenced Aug 24, 2026
Merged
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.
Summary
Five citation RPCs are
security definerand granted toauthenticated, and none of them checked who was asking. They cannot stop being definer — they readcitation_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 rightp_brand_idrather than on the database enforcing membership.citations_domains,citations_urls,citations_window_stats,citation_url_candidatesandcitation_url_occurrencesnow carry theallowedCTE thatreport_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_idshas the same shape but noauthenticatedgrant, 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:
citations_domainscitations_urlscitations_window_statscitation_url_candidatescitation_url_occurrencesA 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-sessioncreateClient(). The MCP layer runs its own queries behind its ownorganization_idownership check, andserver/srcnever 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 featurefix— Bug fixchore— Maintenance / dependenciesdocs— Documentation onlyrefactor— Code change that neither fixes a bug nor adds a featuretest— Adding or updating testsHow to test
Requires the migration to be applied.
Checklist
feature/,fix/,chore/,docs/) — see CONTRIBUTING.mdsupabase/schema.sqlregenerated from the migrations