Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 17 additions & 4 deletions src/app/api/referrals/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { referralInviteEmail, sendEmail } from "@/lib/email";
import { createServiceClient } from "@/lib/supabase/service";

type AnySupabase = any;
const MAX_EMAIL_ENTRIES_PER_REQUEST = 200;
const MAX_INVITES_PER_REQUEST = 20;

// GET /api/referrals - List my referrals
export async function GET(request: NextRequest) {
Expand Down Expand Up @@ -69,9 +71,9 @@ export async function POST(request: NextRequest) {
);
}

if (emails.length > 20) {
if (emails.length > MAX_EMAIL_ENTRIES_PER_REQUEST) {
return NextResponse.json(
{ error: "Maximum 20 invites at a time" },
{ error: `Maximum ${MAX_EMAIL_ENTRIES_PER_REQUEST} email entries at a time` },
{ status: 400 }
);
}
Expand All @@ -80,11 +82,22 @@ export async function POST(request: NextRequest) {
// Only valid emails should count toward throttle limits
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const normalizedEmails = emails.map((e: string) => e.trim().toLowerCase());
const validEmails = normalizedEmails.filter((e: string) => emailRegex.test(e));
const userEmail = user.email?.toLowerCase();
const validEmails = Array.from(
new Set(normalizedEmails.filter((e: string) => emailRegex.test(e) && e !== userEmail))
);
Comment on lines +85 to +88

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 user.email is typed as string | undefined in Supabase Auth — the optional-chain on line 85 confirms the developer knows this. When userEmail resolves to undefined, the filter condition e !== userEmail becomes e !== undefined, which is always true for any email string. That means the entire self-invite guard is silently bypassed for accounts without a stored email (e.g. certain OAuth providers), and the PR's primary goal is not met for those users.

Suggested change
const userEmail = user.email?.toLowerCase();
const validEmails = Array.from(
new Set(normalizedEmails.filter((e: string) => emailRegex.test(e) && e !== userEmail))
);
const userEmail = user.email?.trim().toLowerCase();
if (!userEmail) {
return NextResponse.json(
{ error: "Unable to determine your email address" },
{ status: 400 }
);
}
const validEmails = Array.from(
new Set(normalizedEmails.filter((e: string) => emailRegex.test(e) && e !== userEmail))
);


if (validEmails.length === 0) {
const onlySelf = normalizedEmails.every(e => e === userEmail || !emailRegex.test(e));
return NextResponse.json(
{ error: onlySelf ? "You cannot invite yourself" : "No valid email addresses provided" },
{ status: 400 }
);
}
Comment on lines 90 to +96

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The onlySelf heuristic has a false-positive when userEmail is undefined: the condition e === userEmail becomes e === undefined, which is always false for email strings, so onlySelf falls through entirely to !emailRegex.test(e). Any submission consisting purely of syntactically invalid addresses (e.g. ["notanemail"]) then reports "You cannot invite yourself" even though no self-invite was attempted. Tightening the guard to require at least one entry that matches userEmail avoids the mislead.

Suggested change
if (validEmails.length === 0) {
const onlySelf = normalizedEmails.every(e => e === userEmail || !emailRegex.test(e));
return NextResponse.json(
{ error: onlySelf ? "You cannot invite yourself" : "No valid email addresses provided" },
{ status: 400 }
);
}
if (validEmails.length === 0) {
const hasSelfEntry = userEmail !== undefined && normalizedEmails.some(e => e === userEmail);
const onlySelf = hasSelfEntry && normalizedEmails.every(e => e === userEmail || !emailRegex.test(e));
return NextResponse.json(
{ error: onlySelf ? "You cannot invite yourself" : "No valid email addresses provided" },
{ status: 400 }
);
}


if (validEmails.length > MAX_INVITES_PER_REQUEST) {
return NextResponse.json(
{ error: "No valid email addresses provided" },
{ error: `Maximum ${MAX_INVITES_PER_REQUEST} invites at a time` },
{ status: 400 }
);
}
Expand Down