Summary
A privilege escalation flaw in Capgo's public.apply_usage_overage SECURITY DEFINER function allows sensitive billing actions to run without proper internal authorization checks.
When exposed to authenticated or anonymous roles through Supabase RPC, an attacker can manipulate billing data for organizations they do not control, potentially depleting credits or injecting fraudulent overage events.
Affected context
This vulnerability manifests in Capgo deployments where the public.apply_usage_overage function is defined as SECURITY DEFINER and exposed through Supabase RPC.
It becomes risky when authenticated or anonymous roles have EXECUTE privileges on the function, either directly or via default grants, and when internal checks (such as auth.uid(), organization membership verification, or minimum rights validation) are not enforced.
In such configurations, Row Level Security is effectively bypassed because the function executes with the owner's elevated privileges.
Exploit preconditions
- Attack surface: Database-level function exposed via Supabase RPC or direct SQL access.
- Required access: Ability for an authenticated or anonymous role to EXECUTE the public.apply_usage_overage function.
- Trigger point: Invoking the SECURITY DEFINER function with crafted parameters targeting organizations or accounts the caller does not legitimately control.
- Why exploitation is possible: The function performs sensitive billing operations while running with the owner's privileges, does not validate the caller's identity (auth.uid()), organization membership, or required rights, and thereby bypasses Row Level Security and internal authorization mechanisms.
Why this happens
The core issue is a misuse of elevated-privilege database functions without embedded authorization controls, mapped to CWE-269: Improper Privilege Management.
A SECURITY DEFINER function executes with the privileges of its owner rather than the invoking user, which is appropriate only when the function enforces strict checks on who can perform which actions.
In this case, the function implements sensitive billing logic but omits validation of the caller's identity and permissions.
As a result, any role with EXECUTE access can indirectly inherit higher privileges and operate on billing data belonging to arbitrary organizations, unintentionally bypassing Row Level Security and other access controls.
Potential impact
- An attacker can manipulate billing data for organizations they do not belong to.
- Credits for victim organizations may be depleted without authorization.
- Fraudulent overage events can be inserted, skewing billing metrics and audits.
- Application-level billing integrity and trust in metering data can be undermined.
- Indirect financial loss or service disruption may occur for affected organizations.
General mitigation guidance
Effective mitigation centers on tightening privilege boundaries and embedding authorization into privileged routines.
Common defensive patterns include:
- Restricting EXECUTE privileges on SECURITY DEFINER functions to narrowly scoped, trusted roles instead of generic authenticated or anonymous roles.
- Embedding explicit authorization checks in privileged functions, such as verifying auth.uid(), organization membership, and minimum rights before applying billing changes.
- Preferring SECURITY INVOKER semantics for routines that do not require elevated privileges, so they naturally respect Row Level Security and per-user permissions.
- Segregating billing logic into service layers that enforce business-level authorization before issuing database mutations.
- Periodically reviewing default privileges and RPC exposures to ensure sensitive functions are not unintentionally callable by broad audiences.
How teams detect issues like this
Organizations can detect similar issues by combining schema inspection, policy review, and automated checks:
- Regularly audit database functions for SECURITY DEFINER usage, especially in schemas exposed via APIs or RPC, and flag those lacking internal authorization checks.
- Use static or schema analysis tools to identify functions granted EXECUTE to broad roles such as authenticated, anon, or public.
- During code and migration review, require explicit documentation of why SECURITY DEFINER is necessary and which authorization checks are implemented.
- Implement policy-as-code (e.g., in CI/CD) that fails builds when new privileged functions omit patterns like caller identity verification, organization membership checks, or minimum rights enforcement.
- Correlate billing or metering anomalies with RPC logs to surface suspicious cross-organization operations.

