Skip to content

Lesson 7 of 9 · 24m

Webhook Deployment Behind Reverse Proxies Without Footguns

Terminate TLS cleanly, route webhook traffic, validate Telegram secret headers, preserve source metadata, and deploy changes without dropping updates.

A Telegram webhook endpoint is production internet-facing infrastructure whether you planned it that way or not. It sits behind DNS, TLS termination, reverse proxies, application servers, deploy automation, and incident response. Most outages are boring: a certificate renewal failure, an nginx path mismatch, a container health check that does not cover the webhook route, or a deploy that forgets which port the bot moved to.

Treat ingress as a designed path

A common path looks like this:

Telegram -> HTTPS 443 -> nginx/caddy -> bot webhook service -> queue/database

Keep the path boring. nginx can route only the webhook prefix to the bot service:

location /hooks/telegram {
    proxy_pass http://127.0.0.1:8081;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_read_timeout 10s;
}

Do not expose unrelated application routes on the same public listener merely because they share a process. Smaller attack surface wins.

Validate the Telegram secret token

When registering the webhook, supply secret_token. Telegram sends it back as X-Telegram-Bot-Api-Secret-Token. Validate exact equality before parsing the body or doing expensive work:

expected = settings.telegram_webhook_secret
actual = request.headers.get("X-Telegram-Bot-Api-Secret-Token")
if not hmac.compare_digest(actual or "", expected):
    return Response(status_code=401)

This is not optional because your URL is 'unguessable.' URLs leak through browser history, proxy logs, screenshots, error reports, and certificates transparency adjacent operational mistakes. Header validation is cheap.

Health checks must match the failure domain

A /healthz that only proves the process exists is weak. It should verify the pieces needed for ingress acceptance: configuration loaded, queue writable, database reachable if enqueue depends on it, and worker capacity not exhausted. Do not make it depend on Telegram API reachability unless you want Telegram network issues to restart healthy local services.

Deploys are delivery events

During deployment, old workers may stop while Telegram continues posting updates. A reverse proxy should fail fast and return non-200 if no backend exists, allowing Telegram to retry. A misleading 200 from a maintenance page is data loss. Test that explicitly:

curl -i -X POST https://ops.sudosecurity.org/hooks/telegram   -H 'content-type: application/json'   -H 'X-Telegram-Bot-Api-Secret-Token: wrong'   -d '{"update_id":1}'

You expect 401, not an HTML login page or generic 200. Then test the real secret in a controlled environment with a harmless synthetic update if your ingress supports it.

TLS and hostnames are operational dependencies

Telegram requires a valid public HTTPS endpoint. Certificate renewal failure becomes bot downtime. Monitor certificate expiry, DNS correctness, reverse proxy uptime, and application route status. If you move hostnames, register the new webhook and verify it with getWebhookInfo before decommissioning the old path.

curl -sS "https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/getWebhookInfo" | jq

Pay attention to pending_update_count, last_error_message, and last_error_date. They are operational telemetry Telegram gives you for free.

Field notes

  • Do not put the bot token in webhook paths; it ends up in access logs and is unnecessary.
  • Exact path trailing slashes matter in some proxy configurations. Test /hooks/telegram and /hooks/telegram/ deliberately.
  • Proxy buffering and request-size limits can reject large media metadata updates even when application code is correct.
  • A public maintenance page returning 200 can silently acknowledge failure. Never do that on webhook routes.
  • Keep the webhook secret separate from the bot token so rotation does not require BotFather interaction.

Was this lesson helpful?

Sign in to rate