Repository navigation
Closed
Labels
enhancementNew feature or request
Description
What
All Postgres aggregate / row-fetch RPCs in `supabase/migrations/` currently use `SECURITY DEFINER` without an internal org-membership check:
- `public.get_latest_prompt_results(uuid, ...)` (two overloads) — `00001_initial_schema.sql`
- `public.insights_aggregates(...)` — `00006_insights_aggregates.sql`
- `public.competitor_aggregates(...)` — same file
- `public.share_of_voice_aggregates(...)` — same file
`SECURITY DEFINER` runs the function as the postgres owner, bypassing RLS on `prompt_results` and friends. Any authenticated client could in principle call these RPCs with an arbitrary `p_brand_id` (or `p_organization_id` once we add it) and read cross-org data. The route layer currently enforces brand-belongs-to-org before calling, but the database itself doesn't refuse a wrong-org request — defense-in-depth is missing.
Surfaced in PR #114 review by @copilot-pull-request-reviewer; deferred from that PR because the right fix touches every RPC, not just the new ones.
Possible directions
- Switch to `SECURITY INVOKER` and let RLS do the work. Cleanest if RLS is correctly enabled on all referenced tables (it is on `prompt_results`). Caller's role enforces access naturally. Trade-off: any place that intentionally needs to bypass RLS (worker, internal routes) would need a service-role client.
- Keep `SECURITY DEFINER` and add an explicit membership check inside each function. Lookup the caller's `auth.uid()` → `profiles.organization_id` → verify against `brands.organization_id = ... AND brands.id = p_brand_id`. Returns `NULL` / empty result on mismatch. Adds 2–3 extra selects per call, negligible at the row counts involved.
- Hybrid: `SECURITY INVOKER` for the aggregate RPCs that callers always hit as the authenticated user, `SECURITY DEFINER` only where we genuinely need elevated privileges (and then with an explicit check).
Acceptance
- Every `SECURITY DEFINER` RPC in `supabase/migrations/` either drops `SECURITY DEFINER` (preferred where possible) or enforces an internal org-membership check before returning data
- A regression test (or at minimum a SQL-side parity check) confirms wrong-org calls return empty / null
- No change to the displayed dashboard numbers for legitimate callers
Out of scope
- Pure refactor; no UI changes, no new endpoints
- The route-layer brand verification stays as-is (this PR is defense-in-depth at the DB layer, not a replacement)
Metadata
Metadata
Assignees
Labels
enhancementNew feature or request
Activity
feat(mcp): expose get_visibility_trend tool (visibility time-series) …
gkhngyk commentedon Jun 8, 2026
Picked up in #200.
Direction chosen:
SECURITY INVOKER(the preferred option in this issue). All tables the RPCs read already have an org-scoped SELECT policy forauthenticated(prompt_results,prompts) or no RLS at all (topics), so RLS enforces org isolation with no new policy needed. Implemented asALTER FUNCTION ... SECURITY INVOKERso each body /search_path/ grant stays byte-for-byte identical — metadata-only, reversible.Scope grew since this was filed: two RPCs added after May 28 are included as well —
visibility_trend_aggregatesandprompt_performance_aggregates— for a total of 7 functions.Acceptance verified against a throwaway DB seeded with two orgs: authenticated own-org output is byte-identical to the
service_rolebaseline for all 7 functions (no change to legitimate numbers), cross-org calls return empty/zeroed, and a contrast check confirmed the pre-fixDEFINERversion leaked. Theservice_role(worker / MCP) path is unaffected. Full matrix in the PR.chore(security): flip aggregate/row-fetch RPCs to SECURITY INVOKER (#200