Skip to content
Merged
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
23 changes: 21 additions & 2 deletions apps/commandboard-api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,13 @@ async function route(request: IncomingMessage, response: ServerResponse) {
}

if (request.method === "POST" && url.pathname === "/api/plugins/c0mpute/jobs/dispatch") {
const body = await readJson(request);
let body: unknown;
try {
body = await readJson(request);
} catch {
json(response, 400, { error: "Invalid JSON body" });
return;
}
if (!isRecord(body) || typeof body.job_id !== "string") {
json(response, 422, { error: "Expected job_id" });
return;
Expand All @@ -277,7 +283,13 @@ async function route(request: IncomingMessage, response: ServerResponse) {
}

if (request.method === "POST" && url.pathname === "/api/plugins/c0mpute/quotes") {
const body = await readJson(request);
let body: unknown;
try {
body = await readJson(request);
} catch {
json(response, 400, { error: "Invalid JSON body" });
return;
}
if (!isRecord(body) || typeof body.workload !== "string") {
json(response, 422, { error: "Expected workload" });
return;
Expand Down Expand Up @@ -311,9 +323,16 @@ function text(response: ServerResponse, status: number, contentType: string, bod
response.end(body);
}

const MAX_BODY_BYTES = 1_048_576; // 1 MB

async function readJson(request: IncomingMessage) {
const chunks: Buffer[] = [];
let total = 0;
for await (const chunk of request) {
total += chunk.length;
if (total > MAX_BODY_BYTES) {
throw new Error("Request body too large");
}
chunks.push(Buffer.from(chunk));
}

Expand Down
Loading