Skip to main content

fastapi-security-telegram-webhook

Plugin for FastAPI which allows you to secure your Telegram Bot API webhook endpoint with IP restriction and an optional secret token.

Telegram provides two ways of getting updates: long polling and webhook. When you use webhook you just register endpoint address and telegram sends JSON to this address. If the bad guy finds out the address of your webhook, then he can send fake "telegram updates" to your bot.

Telegram doesn't provide any security features like signing or authentication mechanisms, so securing webhook is a task for a bot developer.

Thence, for securing your webhook you have only two option:

  • Allow requests only from Telegram subnets. Telegram assures that they won't change.
  • Use secret value in endpoint address, e.g. /telegram-webhook/468e95826f224a60a4e9355ab76e0875. It will complicate the brute force attack and you can easily change it if the value was compromised.

This little plugin allows you to use both ways to secure.

How to use

Use pip or another package management util:

pip install fastapi-security-telegram-webhook

or

poetry add fastapi-security-telegram-webhook

or

pipenv install fastapi-security-telegram-webhook

Package contains two Security objects:

  • OnlyTelegramNetwork allows request only from telegram subnets
  • OnlyTelegramNetworkWithSecret additionally checks secret in path

Example with OnlyTelegramNetworkWithSecret. Pay attention to {secret} in path operation, it's required

from fastapi import FastAPI, Body, Depends
from fastapi_security_telegram_webhook import OnlyTelegramNetworkWithSecret

app = FastAPI()
webhook_security = OnlyTelegramNetworkWithSecret(real_secret="your-secret-from-config-or-env")

# {secret} in path and OnlyTelegramNetworkWithSecret as dependency:
@app.post('/webhook/{secret}', dependencies=[Depends(webhook_security)])
def process_telegram_update(update_raw = Body(...)):
   ...

Use behind proxy

The plugin uses starlette.Request.client.host for extracting IP address of the request, so if your web-app is behind proxy you should pass the real IP to the app.

For uvicorn you can use --proxy-headers as it describes in documentation.

Metadata

Release files for fastapi-security-telegram-webhook 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fastapi-security-telegram-webhook 0.2.0
File Size Uploaded
fastapi-security-telegram-webhook-0.2.0.tar.gz 4.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fastapi-security-telegram-webhook 0.2.0
File Interpreter ABI Platform
fastapi_security_telegram_webhook-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 9.3 kB

Release files / fastapi-security-telegram-webhook-0.2.0.tar.gz

Download URL fastapi-security-telegram-webhook-0.2.0.tar.gz
Size 4.2 kB
Tags Source
SHA-256 checksum
How to use checksums
aa3c908cfa8ba9305fa483d2958aea52bd4605554fea9f429bbf95c2bf2e2d51
BLAKE2b-256 checksum
How to use checksums
48ba5de4d413b4882a4ec423ede8dedf4bfea925456c347db4190c5e071ab846
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.7.1 Linux/4.15.0-1028-gcp

Release files / fastapi_security_telegram_webhook-0.2.0-py3-none-any.whl

Download URL fastapi_security_telegram_webhook-0.2.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
22d1575add1a6e239956ccc8fb8819964393d2e5ee8bef181e6f4e5ee63a8df1
BLAKE2b-256 checksum
How to use checksums
871f6eca16f56b30563dcebcbbf8b151f42317b85e3ef462d2de4f6327c79a38
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.5 CPython/3.7.1 Linux/4.15.0-1028-gcp

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page