← Back to PRs

#8067: fix(telegram): use timing-safe comparison for webhook secret

by yubrew open 2026-02-03 14:45 View on GitHub →
channel: telegram stale
## Summary Add timing-safe comparison for Telegram webhook secret validation to prevent timing side-channel attacks (CWE-208). ## The Problem The Telegram webhook secret validation was delegated to grammy's `webhookCallback()` function, which uses standard string comparison instead of `crypto.timingSafeEqual()`. This creates a timing side-channel that allows an attacker to extract the secret token character-by-character via timing analysis. Unlike the LINE webhook implementation (which correctly uses `crypto.timingSafeEqual()` for signature validation), the Telegram webhook relied on grammy's internal validation, which is vulnerable to timing attacks. ## Changes - Added `safeEqualSecret()` helper function using `crypto.timingSafeEqual()` for constant-time string comparison - Implemented manual secret validation before passing request to grammy's handler - Removed `secretToken` option from grammy's `webhookCallback()` since we now validate ourselves - Added regression test to verify timing-safe comparison is used ## Test Plan - [x] Added `src/telegram/webhook.timing-safe.test.ts` with tests for timing-safe comparison helper - [x] Verified all existing tests pass (`pnpm test`) - [x] Verified build succeeds (`pnpm build`) ## Related - CWE-208: Observable Timing Discrepancy - OWASP A02:2021 - Cryptographic Failures --- Internal reference: VULN-026 This PR was generated with the following prompt: > Implement timing-safe webhook secret validation for Telegram webhook to prevent CWE-208 timing side-channel attacks 🤖 Discovered by [bitsec.ai](https://bitsec.ai) <!-- greptile_comment --> <h2>Greptile Overview</h2> <h3>Greptile Summary</h3> This PR moves Telegram webhook secret validation out of grammY's `webhookCallback` and into our own `startTelegramWebhook` server handler, comparing the `x-telegram-bot-api-secret-token` header against the configured secret using `crypto.timingSafeEqual`. It also adds a new Vitest file intended to guard against timing-unsafe comparisons. The core webhook change is small and localized to `src/telegram/webhook.ts`, and it aligns Telegram’s secret checking with the constant-time approach used elsewhere for webhook validation. Main review items are around test effectiveness (the new test currently doesn’t touch production code) and being explicit about how multi-valued headers (`string[]`) should be handled during secret validation. <h3>Confidence Score: 4/5</h3> - This PR is likely safe to merge and improves security, with minor correctness/test gaps to address. - The webhook handler now performs a constant-time compare before invoking grammY, which is the intended mitigation. The main risk is that the added test does not actually prevent regressions in production behavior, and the header parsing currently 401s on multi-valued headers (which can occur with duplicated headers). - src/telegram/webhook.timing-safe.test.ts, src/telegram/webhook.ts <!-- greptile_other_comments_section --> <sub>(3/5) Reply to the agent's comments like "Can you suggest a fix for this @greptileai?" or ask follow-up questions!</sub> <!-- /greptile_comment -->

Most Similar PRs