Repository navigation
Description
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 clauseA 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-sessioncreateClient(). - The MCP/Public-API layer (
web/src/lib/mcp/data.ts) does usesupabaseAdmin, but it never calls these RPCs — it runs its own queries after its ownorganization_idownership check. - Nothing in
server/srcreferences 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.sqlregenerated.
Activity