Skip to content

chore(security): sweep aggregate/row-fetch RPCs to SECURITY INVOKER for org isolation - #200

Merged
gkhngyk merged 1 commit into
mainfrom
chore/security-invoker-rpcs
Jun 9, 2026
Merged

gkhngyk merged 1 commit into
mainfrom
chore/security-invoker-rpcs

Conversation

@gkhngyk

@gkhngyk gkhngyk commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

What

Flips every aggregate / row-fetch RPC in supabase/migrations/ from SECURITY DEFINER to SECURITY INVOKER, so the database itself enforces org isolation through the existing Row Level Security policies instead of relying solely on the route layer to verify brand-belongs-to-org before calling.

Closes #115.

Affected functions (7):

Scope note vs the original issue: visibility_trend_aggregates and prompt_performance_aggregates were added after #115 was filed and are included here too.

Approach

Uses ALTER FUNCTION ... SECURITY INVOKER rather than CREATE OR REPLACE. This flips only the security attribute and leaves each function's body, search_path, volatility and grants byte-for-byte identical — no risk of body drift. It is a metadata-only change: instant to apply, no table lock or rewrite, and trivially reversible.

The SECURITY INVOKER direction (vs. keeping DEFINER + adding manual membership checks) was chosen because every table these RPCs read is already covered by an org-scoped SELECT policy for the authenticated role, so no new policy is needed:

  • prompt_results → "Users can read own org prompt results"
  • prompts → "prompts: member select"
  • topics → no RLS, GRANT ALL to authenticated

Caller analysis:

  • Dashboard callers (web/src/lib/actions/tracking.ts) use the cookie-based authenticated client, so auth.uid() is always populated and RLS resolves to the caller's own organization.
  • MCP / worker callers (web/src/lib/mcp/data.ts) use the service_role client, which bypasses RLS regardless of INVOKER/DEFINER — unaffected.
  • No session-less anon path calls these RPCs.

Verification

Applied all migrations (00001 → 00014) to a throwaway Postgres on the official Supabase image, seeded two organizations (A and B), each with a brand and prompt results, then exercised every RPC in three contexts. The working dev database was never touched.

RPC service_role / Brand A (baseline) authenticated User A / Brand A authenticated User A / Brand B (cross-org)
insights_aggregates total_results=3, mentions=6, vis=180 identical total_results=0, empty
competitor_aggregates brand_row_count=3, vis=180 identical brand_row_count=0, empty
share_of_voice_aggregates total_brand_mentions=6 identical 0, by_day:[]
visibility_trend_aggregates full series identical []
prompt_performance_aggregates result_count=3, vis=180 identical []
get_latest_prompt_results 2 rows 2 rows (identical) 0 rows

Contrast check on insights_aggregates: with the function temporarily reverted to SECURITY DEFINER, the cross-org call leaked Brand B's 2 results; restored to SECURITY INVOKER, the same call returns 0. This confirms the leak was real and that the change closes it.

Net result:

  • Legitimate dashboard numbers are unchanged (authenticated own-org output is byte-identical to the service_role baseline for all 7 functions).
  • Cross-org calls now return empty / zeroed results — no cross-org data leaks.
  • The MCP / worker service_role path is unaffected.

Out of scope

  • Pure DB-layer hardening; no UI changes, no new endpoints.
  • The route-layer brand verification stays as-is (this is defense-in-depth, not a replacement).

Switch get_latest_prompt_results (x2), insights_aggregates,
competitor_aggregates, share_of_voice_aggregates,
visibility_trend_aggregates and prompt_performance_aggregates from
SECURITY DEFINER to SECURITY INVOKER so the database enforces org
isolation via existing RLS, instead of relying solely on the route
layer. Defense-in-depth; closes the cross-org read surface.

Uses ALTER FUNCTION to flip only the security attribute, leaving each
body, search_path and grants byte-for-byte identical.

Closes #115
@gkhngyk
gkhngyk merged commit e0b5ab6 into main Jun 9, 2026
3 checks passed
@gkhngyk
gkhngyk deleted the chore/security-invoker-rpcs branch June 9, 2026 11:37

This branch was successfully deployed

1 active deployment
Preview — 4c9e4884 Deployed Jun 8, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

chore(security): sweep SECURITY DEFINER RPCs to enforce org membership server-side

1 participant