fix(test): reject unsafe HTTP content lengths

This commit is contained in:
Vincent Koc
2026-06-19 19:53:32 +02:00
parent 20d3eccf00
commit 3e89dd6e1e
2 changed files with 16 additions and 1 deletions
+1 -1
View File
@@ -980,7 +980,7 @@ export async function readBoundedResponseText(response, byteLimit, timeoutPromis
const contentLength = response.headers?.get?.("content-length");
if (contentLength && /^\d+$/u.test(contentLength)) {
const parsedContentLength = Number(contentLength);
if (Number.isSafeInteger(parsedContentLength) && parsedContentLength > resolvedByteLimit) {
if (!Number.isSafeInteger(parsedContentLength) || parsedContentLength > resolvedByteLimit) {
await response.body?.cancel?.().catch(() => undefined);
throw createFetchBodyTooLargeError(resolvedByteLimit);
}
@@ -1923,6 +1923,21 @@ describe("kitchen-sink RPC process sampling", () => {
expect(response.text).not.toHaveBeenCalled();
});
it("rejects unsafe decimal HTTP content lengths before reading", async () => {
const response = {
headers: new Headers({
"content-length": "9007199254740992",
}),
text: vi.fn(async () => "not read"),
};
await expect(readBoundedResponseText(response, 1024)).rejects.toMatchObject({
code: "ETOOBIG",
message: "fetch response body exceeded 1024 bytes",
});
expect(response.text).not.toHaveBeenCalled();
});
it("streams HTTP probe responses with non-decimal content-length values", async () => {
let readStarted = false;
let canceled = false;