Skip to content

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

Closed

Description

@gkhngyk

What

All Postgres aggregate / row-fetch RPCs in `supabase/migrations/` currently use `SECURITY DEFINER` without an internal org-membership check:

`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)

Activity

  1. added
    enhancement
    on May 28, 2026
  2. gkhngyk commented on Jun 8, 2026

    @gkhngyk
    ContributorAuthor

    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 for authenticated (prompt_results, prompts) or no RLS at all (topics), so RLS enforces org isolation with no new policy needed. Implemented as ALTER FUNCTION ... SECURITY INVOKER so 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_aggregates and prompt_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_role baseline for all 7 functions (no change to legitimate numbers), cross-org calls return empty/zeroed, and a contrast check confirmed the pre-fix DEFINER version leaked. The service_role (worker / MCP) path is unaffected. Full matrix in the PR.

  3. added a commit that references this issue on Jun 9, 2026
    e0b5ab6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions