Skip to content

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

Closed

Description

@gkhngyk

Problem

Five citation RPCs are security definer (necessarily — citation_urls is a cross-tenant dictionary with no select policy of its own) and are granted to authenticated, but none of them verifies that the caller belongs to the brand's organization:

  • citations_domains (00061)
  • citations_urls (00061)
  • citations_window_stats (00061)
  • citation_url_candidates (00067)
  • citation_url_occurrences (00067)

Each filters strictly by p_brand_id, so the tenant boundary currently rests on the web tier sending the right brand id rather than on the database enforcing membership. Practical exposure is limited — brand ids are unguessable UUIDs and the functions return aggregates — but it is defense-in-depth we already apply elsewhere and should apply uniformly. (citation_url_ids shares the pattern but has no authenticated grant, so it is not reachable.)

Fix

One migration adding the allowed CTE that report_citation_evidence (00063) and the new gap functions (00068) already use:

with allowed as (
  select 1 from brands b
  join profiles pf on pf.organization_id = b.organization_id
  where b.id = p_brand_id and pf.id = auth.uid()
)
-- ... and `exists (select 1 from allowed)` in the base scan's where clause

A non-member gets empty results instead of data.

Caller audit (done — safe to guard)

The guard makes auth.uid() is null callers (service role) see empty results, so every caller must run under a user session. Verified:

  • The only caller of all five is web/src/lib/actions/citations.ts, and every call site uses the user-session createClient().
  • The MCP/Public-API layer (web/src/lib/mcp/data.ts) does use supabaseAdmin, but it never calls these RPCs — it runs its own queries after its own organization_id ownership check.
  • Nothing in server/src references them.

Any future service-role caller must either pass through a user context or be added knowingly; the function comments should say so.

Acceptance

  • All five functions carry the guard; behavior for legitimate members is byte-identical (same-user before/after diff on a real brand).
  • A signed-in member of a different organization gets empty results for a foreign brand id.
  • supabase/schema.sql regenerated.

Activity

  1. added
    citations
    on Aug 23, 2026
  2. self-assigned this
    on Aug 23, 2026
  3. added
    needs-migration
    on Aug 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

citationsneeds-migration

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions