Coder Social home page Coder Social logo

fastapi-security-telegram-webhook's Introduction

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. The are fixed in documentation, but may change in future.
  • 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.

fastapi-security-telegram-webhook's People

Contributors

b0g3r avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.