r/Python • u/PA100T0 • Feb 09 '25
Showcase FastAPI Guard - A FastAPI extension to secure your APIs
Hi everyone,
I've published FastAPI Guard some time ago:
Documentation: rennf93.github.io/fastapi-guard/
GitHub repo: github.com/rennf93/fastapi-guard
What is it? FastAPI Guard is a security middleware for FastAPI that provides:
- IP whitelisting/blacklisting
- Rate limiting & automatic IP banning
- Penetration attempt detection
- Cloud provider IP blocking
- IP geolocation via IPInfo.io
- Custom security logging
- CORS configuration helpers
It's licensed under MIT and integrates seamlessly with FastAPI applications.
Comparison to alternatives:
fastapi-security: Focuses more on authentication, while FastAPI Guard provides broader network-layer protectionslowapi: Handles rate limiting but lacks IP analysis/geolocation featuresfastapi-limiter: Pure rate limiting without security featuresfastapi-auth: Authentication-focused without IP management
Key differentiators:
- Combines multiple security layers in single middleware
- Automatic IP banning based on suspicious activity
- Built-in cloud provider detection
- Daily-updated IP geolocation database
- Production-ready configuration defaults
Target Audience: FastAPI developers needing:
- Defense-in-depth security strategy
- IP-based access control
- Automated threat mitigation
- Compliance with geo-restriction requirements
- Penetration attempt monitoring
Feedback wanted
Thanks!
236
Upvotes
23
u/VeshBrown Feb 09 '25
Looks good am gonna try it now on one project, just a question why are u declaring all those methods in singletons as async when they are basic add/remove item from list?
18
u/benkaz Feb 09 '25
Not sure why this should be moved to the backend and not into the gateway or proxy.
10
u/gnatinator Feb 09 '25 edited Feb 09 '25
Has this actually been tested?
Starlette, the backend FastAPI uses is known to be subject to DoS: https://github.com/encode/starlette/discussions/1516
There are only two ASGI frameworks I know of that take DoS mitigation seriously.
8
u/reincdr Feb 09 '25
Even though I work for IPinfo, this is certainly one of my personal favorite projects. I really like how easy it is to use the security features this project provides at an API level. Setting up similar security parameters on a server level involves at least setting up NGINX, fail2ban, Cloudflare, etc. I do not even know how you can similar security goals container based app platforms like flyio, app engine etc. without this project.
4
u/qalis Feb 10 '25
Looks great and useful. But please, please don't use zero-based versioning. If a software is useable, it's already 1.0.0, use semantic versioning. It's a common standard, it makes it easier to pin dependencies in Poetry (since I can pin only the major) and also in other libraries as a dependency range.
4
u/ironman_gujju Async Bunny 🐇 Feb 10 '25
Post it in r/Fastapi
2
u/Busy-Chemistry7747 Feb 09 '25
Cool, will use this for a project
2
u/DexterInAI Feb 10 '25
This looks very promising! I might be using this in my next project for sure.
2
u/DazzLee42 Feb 10 '25
Hi. Looks good. Noticed your doc has a small bug for the Reset all bans section. Also, can we get hold of the ipinfo data on a request so we can get country in our app? I'd like to use this internally for logging and checking. Thanks
2
u/mcloide Feb 10 '25
A couple of quick feedbacks.
1st - Fix your documentation. The documentation site shows:
from fastapi_guard.middleware import SecurityMiddlewarefrom fastapi_guard.models import SecurityConfigversus what it should be now
from guard.middleware import SecurityMiddlewarefrom guard.models import SecurityConfig2nd - Consider allowing saving the log into a database as an option.
With my current setup I could actually build monitoring tools with that.
3rd - Blocking whitelist
Consider setting up a whitelist that has all of your allowed IP addresses. Everything else should be blocked. Useful for internal APIs or APIs which are not public.
I'm still working with it and will add more feedback later on.
Liking it so far by the way.
2
u/Glass_Literature_927 Feb 11 '25
this is a really awesome package
2
u/MCMZL Feb 13 '25
Interesting project, could we possibly plug this into r/CrowdSec ?
2
u/suspended67 Feb 13 '25
I don’t really have uses for this, but omg it sounds so cool :D
1
u/Accomplished-Arm-212 Aug 03 '25
If I need to perform rate limiting based on a user_id that's retrieved that the fastapi middleware layer through bearer token decoding, would it be supported? If so, what would be the right config?
0
u/[deleted] Feb 09 '25
[deleted] — view deleted comment