177 lines
6.5 KiB
TypeScript
177 lines
6.5 KiB
TypeScript
import { Hono } from "hono";
|
|
import { describe, expect, test, vi } from "vitest";
|
|
import { BODY_LIMIT_HEADER, createBodyLimitMiddleware } from "./body-limit.js";
|
|
import { createRequestIdMiddleware, type ApiRequestIdVariables } from "./request-id.js";
|
|
|
|
type TestEnv = {
|
|
Variables: ApiRequestIdVariables;
|
|
};
|
|
|
|
function createTestApp() {
|
|
const app = new Hono<TestEnv>();
|
|
const writeHandler = vi.fn((c) => c.json({ ok: true, requestId: c.get("requestId") }));
|
|
const readHandler = vi.fn((c) => c.json({ ok: true, requestId: c.get("requestId") }));
|
|
|
|
app.use("*", createRequestIdMiddleware());
|
|
app.use("*", createBodyLimitMiddleware({ jsonLimitBytes: 100, uploadLimitBytes: 1_000 }));
|
|
app.post("/v1/analytics/events", writeHandler);
|
|
app.post("/v1/ai/upload", writeHandler);
|
|
app.post("/v1/widgets/:id", writeHandler);
|
|
app.get("/v1/entities/widgets/rows", readHandler);
|
|
|
|
return { app, readHandler, writeHandler };
|
|
}
|
|
|
|
describe("createBodyLimitMiddleware", () => {
|
|
test("rejects oversized JSON write requests by content-length without echoing body or secrets", async () => {
|
|
const { app, writeHandler } = createTestApp();
|
|
|
|
const response = await app.request("/v1/analytics/events", {
|
|
method: "POST",
|
|
headers: {
|
|
"content-length": "101",
|
|
"content-type": "application/json",
|
|
"x-request-id": "req_body_limit_json",
|
|
},
|
|
body: JSON.stringify({ apiKey: "secret-api-key", token: "secret-token", arbitrary: "body" }),
|
|
});
|
|
const responseText = await response.text();
|
|
|
|
expect(response.status).toBe(413);
|
|
expect(response.headers.get("x-request-id")).toBe("req_body_limit_json");
|
|
expect(response.headers.get(BODY_LIMIT_HEADER)).toBe("100");
|
|
expect(responseText).not.toContain("secret-api-key");
|
|
expect(responseText).not.toContain("secret-token");
|
|
expect(responseText).not.toContain("arbitrary");
|
|
expect(JSON.parse(responseText)).toEqual({
|
|
error: {
|
|
code: "body_too_large",
|
|
message: "Request body is too large.",
|
|
requestId: "req_body_limit_json",
|
|
},
|
|
});
|
|
expect(writeHandler).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("applies the JSON write limit to widget actions", async () => {
|
|
const { app, writeHandler } = createTestApp();
|
|
|
|
const response = await app.request("/v1/widgets/widget_1", {
|
|
method: "POST",
|
|
headers: {
|
|
"content-length": "101",
|
|
"content-type": "application/x-www-form-urlencoded",
|
|
"x-request-id": "req_body_limit_widget",
|
|
},
|
|
body: "action=test-widget&content=secret-widget-content",
|
|
});
|
|
|
|
expect(response.status).toBe(413);
|
|
expect(response.headers.get(BODY_LIMIT_HEADER)).toBe("100");
|
|
await expect(response.json()).resolves.toEqual({
|
|
error: {
|
|
code: "body_too_large",
|
|
message: "Request body is too large.",
|
|
requestId: "req_body_limit_widget",
|
|
},
|
|
});
|
|
expect(writeHandler).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test.each(["/v1/ai/upload", "/v1/ai/upload/tenant_1/asset.png"])(
|
|
"uses the explicit upload limit for AI upload requests at %s before handlers run",
|
|
async (path) => {
|
|
const { app, writeHandler } = createTestApp();
|
|
|
|
const response = await app.request(path, {
|
|
method: "POST",
|
|
headers: {
|
|
"content-length": "1001",
|
|
"content-type": "application/octet-stream",
|
|
"x-request-id": "req_body_limit_upload",
|
|
},
|
|
body: "upload-secret-content",
|
|
});
|
|
|
|
expect(response.status).toBe(413);
|
|
expect(response.headers.get("x-request-id")).toBe("req_body_limit_upload");
|
|
expect(response.headers.get(BODY_LIMIT_HEADER)).toBe("1000");
|
|
await expect(response.json()).resolves.toEqual({
|
|
error: {
|
|
code: "body_too_large",
|
|
message: "Request body is too large.",
|
|
requestId: "req_body_limit_upload",
|
|
},
|
|
});
|
|
expect(writeHandler).not.toHaveBeenCalled();
|
|
}
|
|
);
|
|
|
|
test("allows GET requests even when content-length exceeds write limits", async () => {
|
|
const { app, readHandler } = createTestApp();
|
|
|
|
const response = await app.request("/v1/entities/widgets/rows", {
|
|
method: "GET",
|
|
headers: {
|
|
"content-length": "999999",
|
|
"x-request-id": "req_body_limit_get",
|
|
},
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get("x-request-id")).toBe("req_body_limit_get");
|
|
expect(response.headers.get(BODY_LIMIT_HEADER)).toBeNull();
|
|
await expect(response.json()).resolves.toEqual({ ok: true, requestId: "req_body_limit_get" });
|
|
expect(readHandler).toHaveBeenCalledOnce();
|
|
});
|
|
|
|
test("rejects write requests with bodies when content-length is missing or malformed", async () => {
|
|
const { app, writeHandler } = createTestApp();
|
|
|
|
const missingLengthResponse = await app.request("/v1/analytics/events", {
|
|
method: "POST",
|
|
headers: { "x-request-id": "req_body_limit_missing" },
|
|
body: "{}",
|
|
});
|
|
const malformedLengthResponse = await app.request("/v1/analytics/events", {
|
|
method: "POST",
|
|
headers: { "content-length": "not-a-number", "x-request-id": "req_body_limit_malformed" },
|
|
body: "{}",
|
|
});
|
|
|
|
expect(missingLengthResponse.status).toBe(411);
|
|
expect(malformedLengthResponse.status).toBe(411);
|
|
expect(missingLengthResponse.headers.get(BODY_LIMIT_HEADER)).toBe("100");
|
|
expect(malformedLengthResponse.headers.get(BODY_LIMIT_HEADER)).toBe("100");
|
|
await expect(missingLengthResponse.json()).resolves.toEqual({
|
|
error: {
|
|
code: "content_length_required",
|
|
message: "Write requests with a body require a valid Content-Length header.",
|
|
requestId: "req_body_limit_missing",
|
|
},
|
|
});
|
|
await expect(malformedLengthResponse.json()).resolves.toEqual({
|
|
error: {
|
|
code: "content_length_required",
|
|
message: "Write requests with a body require a valid Content-Length header.",
|
|
requestId: "req_body_limit_malformed",
|
|
},
|
|
});
|
|
expect(writeHandler).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("allows write requests without bodies when content-length is absent", async () => {
|
|
const { app, writeHandler } = createTestApp();
|
|
|
|
const response = await app.request("/v1/analytics/events", {
|
|
method: "POST",
|
|
headers: { "x-request-id": "req_body_limit_no_body" },
|
|
});
|
|
|
|
expect(response.status).toBe(200);
|
|
expect(response.headers.get(BODY_LIMIT_HEADER)).toBe("100");
|
|
await expect(response.json()).resolves.toEqual({ ok: true, requestId: "req_body_limit_no_body" });
|
|
expect(writeHandler).toHaveBeenCalledOnce();
|
|
});
|
|
});
|