Repository navigation
chore(security): sweep aggregate/row-fetch RPCs to SECURITY INVOKER for org isolation - #200
Merged
Merged
Conversation
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
3 tasks
This branch was successfully deployed
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.
What
Flips every aggregate / row-fetch RPC in
supabase/migrations/fromSECURITY DEFINERtoSECURITY 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):
get_latest_prompt_results(uuid, text)andget_latest_prompt_results(uuid, text, text, text, timestamptz, timestamptz)—00001_initial_schema.sqlinsights_aggregates(...),competitor_aggregates(...),share_of_voice_aggregates(...),visibility_trend_aggregates(...)— current definitions in00012_shopping_mode.sqlprompt_performance_aggregates(...)—00013_prompt_performance_aggregates.sqlApproach
Uses
ALTER FUNCTION ... SECURITY INVOKERrather thanCREATE 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 INVOKERdirection (vs. keepingDEFINER+ adding manual membership checks) was chosen because every table these RPCs read is already covered by an org-scoped SELECT policy for theauthenticatedrole, so no new policy is needed:prompt_results→ "Users can read own org prompt results"prompts→ "prompts: member select"topics→ no RLS,GRANT ALLtoauthenticatedCaller analysis:
web/src/lib/actions/tracking.ts) use the cookie-based authenticated client, soauth.uid()is always populated and RLS resolves to the caller's own organization.web/src/lib/mcp/data.ts) use theservice_roleclient, which bypasses RLS regardless ofINVOKER/DEFINER— unaffected.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.
service_role/ Brand A (baseline)insights_aggregatestotal_results=3, mentions=6, vis=180total_results=0, emptycompetitor_aggregatesbrand_row_count=3, vis=180brand_row_count=0, emptyshare_of_voice_aggregatestotal_brand_mentions=60,by_day:[]visibility_trend_aggregates[]prompt_performance_aggregatesresult_count=3, vis=180[]get_latest_prompt_resultsContrast check on
insights_aggregates: with the function temporarily reverted toSECURITY DEFINER, the cross-org call leaked Brand B's 2 results; restored toSECURITY INVOKER, the same call returns 0. This confirms the leak was real and that the change closes it.Net result:
service_rolebaseline for all 7 functions).service_rolepath is unaffected.Out of scope