Skip to content

Commit 14bdb37

Browse files
tomusdrwclaude
andcommitted
fix: address CodeRabbit review — auth check in RPC, consistent UX
- Security: add auth.uid() validation to increment_usage RPC function to prevent users from incrementing other users' quotas - UX: add "Please log in first" alert to useManageSubscription for consistency with useCheckout - Update docs (Supabase.mdx) with the secured RPC function Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 7175512 commit 14bdb37

4 files changed

Lines changed: 38 additions & 1 deletion

File tree

demo/src/App.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,10 @@ function useManageSubscription() {
236236
const { session } = useSession();
237237

238238
return async () => {
239-
if (!session?.access_token) return;
239+
if (!session?.access_token) {
240+
alert("Please log in first");
241+
return;
242+
}
240243
try {
241244
const res = await fetch(`${SUPABASE_URL}/functions/v1/create-portal`, {
242245
method: "POST",

lib/supabase/Supabase.mdx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ create or replace function increment_usage(
9090
declare
9191
new_count int;
9292
begin
93+
-- Ensure caller can only increment their own usage
94+
if p_user_id != auth.uid() then
95+
raise exception 'Access denied: cannot modify another user''s usage';
96+
end if;
97+
9398
insert into usage (user_id, app_id, action, period, count)
9499
values (p_user_id, p_app_id, p_action, p_period, 1)
95100
on conflict (user_id, app_id, action, period)

supabase/migrations/20260331000000_init.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ create or replace function increment_usage(
6060
declare
6161
new_count int;
6262
begin
63+
-- Ensure caller can only increment their own usage
64+
if p_user_id != auth.uid() then
65+
raise exception 'Access denied: cannot modify another user''s usage';
66+
end if;
67+
6368
insert into usage (user_id, app_id, action, period, count)
6469
values (p_user_id, p_app_id, p_action, p_period, 1)
6570
on conflict (user_id, app_id, action, period)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
-- Fix: validate caller identity in increment_usage to prevent
2+
-- one user from incrementing another user's quota.
3+
create or replace function increment_usage(
4+
p_user_id uuid,
5+
p_app_id text,
6+
p_action text,
7+
p_period text
8+
) returns int as $$
9+
declare
10+
new_count int;
11+
begin
12+
-- Ensure caller can only increment their own usage
13+
if p_user_id != auth.uid() then
14+
raise exception 'Access denied: cannot modify another user''s usage';
15+
end if;
16+
17+
insert into usage (user_id, app_id, action, period, count)
18+
values (p_user_id, p_app_id, p_action, p_period, 1)
19+
on conflict (user_id, app_id, action, period)
20+
do update set count = usage.count + 1
21+
returning count into new_count;
22+
return new_count;
23+
end;
24+
$$ language plpgsql security definer;

0 commit comments

Comments
 (0)