Skip to content

Commit fcbc275

Browse files
committed
The content-length header is required. Deal with an empty body
1 parent 344dfde commit fcbc275

File tree

1 file changed

+7
-35
lines changed

1 file changed

+7
-35
lines changed

apps/webapp/app/routes/api.v1.waitpoints.http-callback.$waitpointFriendlyId.callback.$hash.ts

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ export async function action({ request, params }: ActionFunctionArgs) {
2323
}
2424

2525
const contentLength = request.headers.get("content-length");
26-
if (contentLength && parseInt(contentLength) > env.TASK_PAYLOAD_MAXIMUM_SIZE) {
26+
if (!contentLength) {
27+
return json({ error: "Content-Length header is required" }, { status: 411 });
28+
}
29+
30+
if (parseInt(contentLength) > env.TASK_PAYLOAD_MAXIMUM_SIZE) {
2731
return json({ error: "Request body too large" }, { status: 413 });
2832
}
2933

@@ -58,16 +62,8 @@ export async function action({ request, params }: ActionFunctionArgs) {
5862
});
5963
}
6064

61-
let body;
62-
try {
63-
body = await readJsonWithLimit(request, env.TASK_PAYLOAD_MAXIMUM_SIZE);
64-
} catch (e) {
65-
return json({ error: "Request body too large" }, { status: 413 });
66-
}
67-
68-
if (!body) {
69-
body = {};
70-
}
65+
// If the request body is not valid JSON, return an empty object
66+
const body = await request.json().catch(() => ({}));
7167

7268
const stringifiedData = await stringifyIO(body);
7369
const finalData = await conditionallyExportPacket(
@@ -93,27 +89,3 @@ export async function action({ request, params }: ActionFunctionArgs) {
9389
throw json({ error: "Failed to complete HTTP callback" }, { status: 500 });
9490
}
9591
}
96-
97-
async function readJsonWithLimit(request: Request, maxSize: number) {
98-
const reader = request.body?.getReader();
99-
if (!reader) throw new Error("No body");
100-
let received = 0;
101-
let chunks: Uint8Array[] = [];
102-
while (true) {
103-
const { done, value } = await reader.read();
104-
if (done) break;
105-
received += value.length;
106-
if (received > maxSize) {
107-
throw new Error("Request body too large");
108-
}
109-
chunks.push(value);
110-
}
111-
const full = new Uint8Array(received);
112-
let offset = 0;
113-
for (const chunk of chunks) {
114-
full.set(chunk, offset);
115-
offset += chunk.length;
116-
}
117-
const text = new TextDecoder().decode(full);
118-
return JSON.parse(text);
119-
}

0 commit comments

Comments
 (0)