diff --git a/server/lib/cf.ts b/server/lib/cf.ts index bb1de19..4c48143 100644 --- a/server/lib/cf.ts +++ b/server/lib/cf.ts @@ -56,3 +56,16 @@ export function headersFrom(event: H3Event): Record { }); return out; } + +/** Largest body the bot callback routes read (each handler checks it again). */ +export const MAX_CALLBACK_BODY = 1024 * 1024; + +/** + * A body declared larger than `max` bytes: answered 413 before the route + * reads (and copies) it. Bodies without a declared length are still checked + * by the handler once read. + */ +export function declaresBodyOver(event: H3Event, max = MAX_CALLBACK_BODY): boolean { + const length = Number(event.headers.get("content-length")); + return Number.isFinite(length) && length > max; +} diff --git a/server/routes/discord/interactions.post.ts b/server/routes/discord/interactions.post.ts index 11a1401..e105d74 100644 --- a/server/routes/discord/interactions.post.ts +++ b/server/routes/discord/interactions.post.ts @@ -1,12 +1,13 @@ import { readRawBody } from "h3"; import { handleInteractionRequest } from "../../lib/drivers/discord/interactions"; -import { cfEnv, rawRequest } from "../../lib/cf"; +import { cfEnv, declaresBodyOver, rawRequest } from "../../lib/cf"; export default defineEventHandler(async (event) => { // The raw Request is provided for headers/URL, but nitro consumes its body // while buffering the payload — rebuild a fresh Request with the body from // h3's internal buffers so the handler can read it (signature verification // needs the exact raw bytes). + if (declaresBodyOver(event)) return new Response("Request too large", { status: 413 }); const source = rawRequest(event); const body = await readRawBody(event, "utf8"); const request = new Request(source.url, { diff --git a/server/routes/feishu/webhook.post.ts b/server/routes/feishu/webhook.post.ts index 05e84f2..2c08776 100644 --- a/server/routes/feishu/webhook.post.ts +++ b/server/routes/feishu/webhook.post.ts @@ -1,8 +1,9 @@ import { readRawBody } from "h3"; import { handleFeishuWebhookRequest } from "../../lib/drivers/feishu/updates"; -import { cfEnv, rawRequest } from "../../lib/cf"; +import { cfEnv, declaresBodyOver, rawRequest } from "../../lib/cf"; export default defineEventHandler(async (event) => { + if (declaresBodyOver(event)) return new Response("Request too large", { status: 413 }); const source = rawRequest(event); const body = await readRawBody(event, "utf8"); const request = new Request(source.url, { diff --git a/server/routes/telegram/webhook.post.ts b/server/routes/telegram/webhook.post.ts index 8d0e93e..5fcbd37 100644 --- a/server/routes/telegram/webhook.post.ts +++ b/server/routes/telegram/webhook.post.ts @@ -1,8 +1,9 @@ import { readRawBody } from "h3"; import { handleTelegramWebhookRequest } from "../../lib/drivers/telegram/updates"; -import { cfEnv, rawRequest } from "../../lib/cf"; +import { cfEnv, declaresBodyOver, rawRequest } from "../../lib/cf"; export default defineEventHandler(async (event) => { + if (declaresBodyOver(event)) return new Response("Request too large", { status: 413 }); const source = rawRequest(event); const body = await readRawBody(event, "utf8"); const request = new Request(source.url, {