fix(security): enforce bounded webhook body handling

This commit is contained in:
Peter Steinberger
2026-02-13 19:14:36 +01:00
parent 2f9c523bbe
commit 3cbcba10cf
20 changed files with 834 additions and 281 deletions

View File

@@ -4,6 +4,7 @@ import type { OpenClawConfig } from "../config/config.js";
import type { RuntimeEnv } from "../runtime.js";
import { isDiagnosticsEnabled } from "../infra/diagnostic-events.js";
import { formatErrorMessage } from "../infra/errors.js";
import { installRequestBodyLimitGuard } from "../infra/http-body.js";
import {
logWebhookError,
logWebhookProcessed,
@@ -16,6 +17,9 @@ import { resolveTelegramAllowedUpdates } from "./allowed-updates.js";
import { withTelegramApiErrorLogging } from "./api-logging.js";
import { createTelegramBot } from "./bot.js";
const TELEGRAM_WEBHOOK_MAX_BODY_BYTES = 1024 * 1024;
const TELEGRAM_WEBHOOK_BODY_TIMEOUT_MS = 30_000;
export async function startTelegramWebhook(opts: {
token: string;
accountId?: string;
@@ -66,6 +70,14 @@ export async function startTelegramWebhook(opts: {
if (diagnosticsEnabled) {
logWebhookReceived({ channel: "telegram", updateType: "telegram-post" });
}
const guard = installRequestBodyLimitGuard(req, res, {
maxBytes: TELEGRAM_WEBHOOK_MAX_BODY_BYTES,
timeoutMs: TELEGRAM_WEBHOOK_BODY_TIMEOUT_MS,
responseFormat: "text",
});
if (guard.isTripped()) {
return;
}
const handled = handler(req, res);
if (handled && typeof handled.catch === "function") {
void handled
@@ -79,6 +91,9 @@ export async function startTelegramWebhook(opts: {
}
})
.catch((err) => {
if (guard.isTripped()) {
return;
}
const errMsg = formatErrorMessage(err);
if (diagnosticsEnabled) {
logWebhookError({
@@ -92,8 +107,13 @@ export async function startTelegramWebhook(opts: {
res.writeHead(500);
}
res.end();
})
.finally(() => {
guard.dispose();
});
return;
}
guard.dispose();
});
const publicUrl =