Skip to content

Commit e5bbbf1

Browse files
committed
fix(webapp): return 400 (not 500) on malformed JSON body in management API routes
1 parent ef114ac commit e5bbbf1

5 files changed

Lines changed: 40 additions & 5 deletions

File tree

apps/webapp/app/routes/api.v1.orgs.$orgParam.invites.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
5555
});
5656
if (denied) return denied;
5757

58-
const body = InviteRequestBody.safeParse(await request.json());
58+
let rawBody: unknown;
59+
try {
60+
rawBody = await request.json();
61+
} catch {
62+
return json({ error: "Invalid request body" }, { status: 400 });
63+
}
64+
65+
const body = InviteRequestBody.safeParse(rawBody);
5966

6067
if (!body.success) {
6168
return json({ error: "Invalid request body" }, { status: 400 });

apps/webapp/app/routes/api.v1.orgs.$orgParam.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
6464
return json({ id: organization.id });
6565
}
6666

67-
const body = RenameOrgRequestBody.safeParse(await request.json());
67+
let rawBody: unknown;
68+
try {
69+
rawBody = await request.json();
70+
} catch {
71+
return json({ error: "Invalid request body" }, { status: 400 });
72+
}
73+
74+
const body = RenameOrgRequestBody.safeParse(rawBody);
6875

6976
if (!body.success) {
7077
return json({ error: "Invalid request body" }, { status: 400 });

apps/webapp/app/routes/api.v1.orgs.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,14 @@ export async function action({ request }: ActionFunctionArgs) {
6262
return json({ error: "Invalid or Missing Access Token" }, { status: 401 });
6363
}
6464

65-
const body = CreateOrgRequestBody.safeParse(await request.json());
65+
let rawBody: unknown;
66+
try {
67+
rawBody = await request.json();
68+
} catch {
69+
return json({ error: "Invalid request body" }, { status: 400 });
70+
}
71+
72+
const body = CreateOrgRequestBody.safeParse(rawBody);
6673

6774
if (!body.success) {
6875
return json({ error: "Invalid request body" }, { status: 400 });

apps/webapp/app/routes/api.v1.projects.$projectRef.default-region.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
5454
return json({ error: "Project not found" }, { status: 404 });
5555
}
5656

57-
const body = SetDefaultRegionRequestBody.safeParse(await request.json());
57+
let rawBody: unknown;
58+
try {
59+
rawBody = await request.json();
60+
} catch {
61+
return json({ error: "Invalid request body" }, { status: 400 });
62+
}
63+
64+
const body = SetDefaultRegionRequestBody.safeParse(rawBody);
5865

5966
if (!body.success) {
6067
return json({ error: "Invalid request body" }, { status: 400 });

apps/webapp/app/routes/api.v1.projects.$projectRef.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,14 @@ export async function action({ request, params }: ActionFunctionArgs) {
123123
return json({ id: project.id });
124124
}
125125

126-
const body = RenameProjectRequestBody.safeParse(await request.json());
126+
let rawBody: unknown;
127+
try {
128+
rawBody = await request.json();
129+
} catch {
130+
return json({ error: "Invalid request body" }, { status: 400 });
131+
}
132+
133+
const body = RenameProjectRequestBody.safeParse(rawBody);
127134

128135
if (!body.success) {
129136
return json({ error: "Invalid request body" }, { status: 400 });

0 commit comments

Comments
 (0)