Skip to content

FastAPI Guard

FastAPI Guard Logo

PyPI version Release License: MIT CI CodeQL

pages-build-deployment Docs Update Downloads

fastapi-guard is a comprehensive security library for FastAPI applications, providing middleware to control IPs, log requests, and detect penetration attempts. It integrates seamlessly with FastAPI to offer robust protection against various security threats, ensuring your application remains secure and reliable.

Quick Start

from fastapi import FastAPI
from guard.middleware import SecurityMiddleware
from guard.models import SecurityConfig

app = FastAPI()

config = SecurityConfig(
    ipinfo_token="your_token_here",
    rate_limit=100,
    auto_ban_threshold=5
)

app.add_middleware(SecurityMiddleware, config=config)

Features

  • IP Whitelisting and Blacklisting: Control access based on IP addresses.
  • User Agent Filtering: Block requests from specific user agents.
  • Rate Limiting: Limit the number of requests from a single IP.
  • Automatic IP Banning: Automatically ban IPs after a certain number of suspicious requests.
  • Penetration Attempt Detection: Detect and log potential penetration attempts.
  • Custom Logging: Log security events to a custom file.
  • CORS Configuration: Configure CORS settings for your FastAPI application.
  • Cloud Provider IP Blocking: Block requests from cloud provider IPs (AWS, GCP, Azure).
  • IP Geolocation: Use IPInfo.io API to determine the country of an IP address.

Documentation

📖 Learn More in the Tutorial