Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+73
@@ -0,0 +1,73 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { readLimitedRequestBytes, readLimitedRequestFormData, RequestBodyLimitError } from "./readLimitedRequestBody.server";
|
||||
|
||||
describe("readLimitedRequestBytes", () => {
|
||||
it("reads a bounded request body", async () => {
|
||||
const request = new Request("https://app.example.test/upload", { method: "POST", body: new Uint8Array([1, 2, 3]) });
|
||||
await expect(readLimitedRequestBytes(request, 3)).resolves.toEqual(new Uint8Array([1, 2, 3]));
|
||||
});
|
||||
|
||||
it("rejects an oversized declared body before buffering", async () => {
|
||||
const request = new Request("https://app.example.test/upload", {
|
||||
method: "POST",
|
||||
headers: { "content-length": "4" },
|
||||
body: new Uint8Array([1]),
|
||||
});
|
||||
await expect(readLimitedRequestBytes(request, 3)).rejects.toBeInstanceOf(RequestBodyLimitError);
|
||||
});
|
||||
|
||||
it("rejects streamed bytes when content-length is absent or understated", async () => {
|
||||
for (const headers of [{}, { "content-length": "1" }]) {
|
||||
const request = new Request("https://app.example.test/upload", {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: new ReadableStream({
|
||||
start(controller) {
|
||||
controller.enqueue(new Uint8Array([1, 2]));
|
||||
controller.enqueue(new Uint8Array([3, 4]));
|
||||
controller.close();
|
||||
},
|
||||
}),
|
||||
duplex: "half",
|
||||
} as RequestInit & { duplex: "half" });
|
||||
await expect(readLimitedRequestBytes(request, 3)).rejects.toBeInstanceOf(RequestBodyLimitError);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("readLimitedRequestFormData", () => {
|
||||
it("parses multipart form data within the streamed byte limit", async () => {
|
||||
const boundary = "pilot-boundary";
|
||||
const body = [
|
||||
`--${boundary}\r\nContent-Disposition: form-data; name="csrf"\r\n\r\ncsrf-token\r\n`,
|
||||
`--${boundary}\r\nContent-Disposition: form-data; name="message"\r\n\r\nPilot\r\n`,
|
||||
`--${boundary}--\r\n`,
|
||||
].join("");
|
||||
const request = new Request("https://app.example.test/upload", {
|
||||
method: "POST",
|
||||
headers: { "content-type": `multipart/form-data; boundary=${boundary}` },
|
||||
body,
|
||||
});
|
||||
|
||||
const parsed = await readLimitedRequestFormData(request, 1024);
|
||||
|
||||
expect(parsed.get("csrf")).toBe("csrf-token");
|
||||
expect(parsed.get("message")).toBe("Pilot");
|
||||
});
|
||||
|
||||
it.each([undefined, "1"])("rejects actual multipart bytes when content-length is %s", async (contentLength) => {
|
||||
const form = new FormData();
|
||||
form.set("file", new File(["payload that exceeds the route limit"], "pilot.txt", { type: "text/plain" }));
|
||||
const encoded = new Request("https://app.example.test/upload", { method: "POST", body: form });
|
||||
const headers = new Headers({ "content-type": encoded.headers.get("content-type") ?? "" });
|
||||
if (contentLength) headers.set("content-length", contentLength);
|
||||
const request = new Request(encoded.url, {
|
||||
method: "POST",
|
||||
headers,
|
||||
body: encoded.body,
|
||||
duplex: "half",
|
||||
} as RequestInit & { duplex: "half" });
|
||||
|
||||
await expect(readLimitedRequestFormData(request, 16)).rejects.toBeInstanceOf(RequestBodyLimitError);
|
||||
});
|
||||
});
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
export class RequestBodyLimitError extends Error {
|
||||
constructor(readonly limitBytes: number) {
|
||||
super(`Request body exceeds the ${limitBytes}-byte route limit.`);
|
||||
this.name = "RequestBodyLimitError";
|
||||
}
|
||||
}
|
||||
|
||||
export async function readLimitedRequestBytes(request: Request, maxBytes: number): Promise<Uint8Array> {
|
||||
assertValidRequestBodyLimit(maxBytes);
|
||||
|
||||
await rejectOversizedDeclaredBody(request, maxBytes);
|
||||
if (!request.body) return new Uint8Array();
|
||||
|
||||
const reader = request.body.getReader();
|
||||
const chunks: Uint8Array[] = [];
|
||||
let totalBytes = 0;
|
||||
while (true) {
|
||||
const chunk = await reader.read();
|
||||
if (chunk.done) break;
|
||||
totalBytes += chunk.value.byteLength;
|
||||
if (totalBytes > maxBytes) {
|
||||
await reader.cancel().catch(() => undefined);
|
||||
throw new RequestBodyLimitError(maxBytes);
|
||||
}
|
||||
chunks.push(chunk.value);
|
||||
}
|
||||
|
||||
const body = new Uint8Array(totalBytes);
|
||||
let offset = 0;
|
||||
for (const chunk of chunks) {
|
||||
body.set(chunk, offset);
|
||||
offset += chunk.byteLength;
|
||||
}
|
||||
return body;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse multipart or URL-encoded form data while enforcing the bytes actually
|
||||
* consumed by the parser. `Content-Length` remains a fast rejection path, but
|
||||
* missing or understated headers cannot bypass the limit.
|
||||
*/
|
||||
export async function readLimitedRequestFormData(request: Request, maxBytes: number): Promise<FormData> {
|
||||
assertValidRequestBodyLimit(maxBytes);
|
||||
await rejectOversizedDeclaredBody(request, maxBytes);
|
||||
if (!request.body) return request.formData();
|
||||
|
||||
const limitedBody = createLimitedRequestBodyStream(request.body, maxBytes);
|
||||
const contentType = request.headers.get("content-type");
|
||||
return new Response(limitedBody, {
|
||||
headers: contentType ? { "content-type": contentType } : undefined,
|
||||
}).formData();
|
||||
}
|
||||
|
||||
function assertValidRequestBodyLimit(maxBytes: number): void {
|
||||
if (!Number.isSafeInteger(maxBytes) || maxBytes <= 0) {
|
||||
throw new TypeError("Request body limit must be a positive safe integer.");
|
||||
}
|
||||
}
|
||||
|
||||
async function rejectOversizedDeclaredBody(request: Request, maxBytes: number): Promise<void> {
|
||||
const contentLength = request.headers.get("content-length");
|
||||
if (contentLength && /^\d+$/.test(contentLength) && Number(contentLength) > maxBytes) {
|
||||
await request.body?.cancel().catch(() => undefined);
|
||||
throw new RequestBodyLimitError(maxBytes);
|
||||
}
|
||||
}
|
||||
|
||||
function createLimitedRequestBodyStream(body: ReadableStream<Uint8Array>, maxBytes: number): ReadableStream<Uint8Array> {
|
||||
const reader = body.getReader();
|
||||
let totalBytes = 0;
|
||||
return new ReadableStream<Uint8Array>({
|
||||
async pull(controller) {
|
||||
try {
|
||||
const chunk = await reader.read();
|
||||
if (chunk.done) {
|
||||
controller.close();
|
||||
return;
|
||||
}
|
||||
totalBytes += chunk.value.byteLength;
|
||||
if (totalBytes > maxBytes) {
|
||||
await reader.cancel().catch(() => undefined);
|
||||
controller.error(new RequestBodyLimitError(maxBytes));
|
||||
return;
|
||||
}
|
||||
controller.enqueue(chunk.value);
|
||||
} catch (error) {
|
||||
controller.error(error);
|
||||
}
|
||||
},
|
||||
cancel(reason) {
|
||||
return reader.cancel(reason);
|
||||
},
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user