-
Notifications
You must be signed in to change notification settings - Fork 46
Return 400 for malformed referral JSON #467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { NextRequest } from "next/server"; | ||
| import { POST } from "./route"; | ||
|
|
||
| const mocks = vi.hoisted(() => ({ | ||
| mockGetAuthContext: vi.fn(), | ||
| mockCreateServiceClient: vi.fn(), | ||
| mockSendEmail: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock("@/lib/auth/get-user", () => ({ | ||
| getAuthContext: mocks.mockGetAuthContext, | ||
| })); | ||
|
|
||
| vi.mock("@/lib/supabase/service", () => ({ | ||
| createServiceClient: mocks.mockCreateServiceClient, | ||
| })); | ||
|
|
||
| vi.mock("@/lib/email", () => ({ | ||
| referralInviteEmail: vi.fn(), | ||
| sendEmail: mocks.mockSendEmail, | ||
| })); | ||
|
|
||
| function makeRawPostRequest(body: BodyInit) { | ||
| return new NextRequest("http://localhost/api/referrals", { | ||
| method: "POST", | ||
| body, | ||
| headers: { "Content-Type": "application/json" }, | ||
| }); | ||
| } | ||
|
|
||
| describe("POST /api/referrals invalid JSON handling", () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| mocks.mockGetAuthContext.mockResolvedValue({ | ||
| user: { id: "user1" }, | ||
| supabase: { from: vi.fn() }, | ||
| }); | ||
| }); | ||
|
|
||
| it("returns 400 for malformed JSON before referral side effects", async () => { | ||
| const res = await POST(makeRawPostRequest("{not valid json")); | ||
| const body = await res.json(); | ||
|
|
||
| expect(res.status).toBe(400); | ||
| expect(body.error).toBe("Invalid JSON body"); | ||
| expect(mocks.mockCreateServiceClient).not.toHaveBeenCalled(); | ||
| expect(mocks.mockSendEmail).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,18 @@ type AnySupabase = any; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const MAX_EMAIL_ENTRIES_PER_REQUEST = 200; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const MAX_INVITES_PER_REQUEST = 20; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| async function readJsonObject(request: NextRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const body = await request.json(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!body || typeof body !== "object" || Array.isArray(body)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return body as Record<string, unknown>; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+10
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // GET /api/referrals - List my referrals | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| export async function GET(request: NextRequest) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -54,7 +66,13 @@ export async function POST(request: NextRequest) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { user, supabase } = auth; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const body = await request.json(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const body = await readJsonObject(request); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!body) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return NextResponse.json( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { error: "Invalid JSON body" }, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { status: 400 } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| const { emails } = body; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!emails || !Array.isArray(emails) || emails.length === 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readJsonObjecthas two distinct rejection branches — aJSON.parsethrow (malformed input) and a type check (valid JSON that isn't a plain object, e.g. arrays,null, or primitives). The test exercises only the first branch. Sending"[]"or"null"as the body would exercise the second branch and confirm both are mapped to 400 correctly.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!