fix(bot): reject oversized callback bodies

Reject declared oversized bot callbacks before reading their bodies.
This keeps unauthenticated requests from wasting worker resources.
This commit is contained in:
翅膀 committed 2026-10-23 23:21:15 +08:00
1 parent c641a8c1c5
commit 6535126ae0
4 files changed
+19 -3

No files matched your search

+13
View File
@@ -56,3 +56,16 @@ export function headersFrom(event: H3Event): Record<string, string> {
});
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;
}