fix(telegram): always verify webhook secrets

Webhook verification must not silently disable itself when the
optional secret is absent. Derive a stable secret from the bot
token and reject callbacks when verification is unavailable.
This commit is contained in:
翅膀 committed 2026-10-23 23:22:57 +08:00
1 parent 706737ca6b
commit 0bf18fe2ca
3 files changed
+72 -6

No files matched your search

+12 -6
View File
@@ -1,5 +1,7 @@
import type { Env } from "../../types";
import { log } from "../../lib/log";
import { handleTelegramUpdate } from "./commands";
import { telegramWebhookSecret } from "./secret";
const MAX_BODY_SIZE = 1024 * 1024;
@@ -19,12 +21,16 @@ export async function handleTelegramWebhookRequest(request: Request, env: Env):
return new Response("Request too large", { status: 413 });
}
const secret = env.TELEGRAM_WEBHOOK_SECRET;
if (secret) {
const token = request.headers.get("X-Telegram-Bot-Api-Secret-Token");
if (!token || !timingSafeEqual(token, secret)) {
return new Response("Invalid secret", { status: 401 });
}
// Every call must carry the webhook secret. Without a bot token there is
// no secret to check and nothing to act with, so the endpoint stays closed.
const secret = await telegramWebhookSecret(env);
if (!secret) {
log.warn("Telegram webhook called but TELEGRAM_TOKEN is not configured");
return new Response("Telegram is not configured", { status: 403 });
}
const token = request.headers.get("X-Telegram-Bot-Api-Secret-Token");
if (!token || !timingSafeEqual(token, secret)) {
return new Response("Invalid secret", { status: 401 });
}
const rawBody = await request.text();