FastAPI Middleware for IP Address Safelisting
Project description
ip-safelist-middleware
FastAPI Middleware for IP Address Safelisting. This middleware allows you to restrict access to your FastAPI application based on client IP addresses.
📖 Documentation | 🚀 Quick Start | 📋 Examples
Features
- IP address filtering based on exact match or network ranges (CIDR notation)
- Support for AWS IP ranges from specified regions
- Path-based access control using regex patterns
- Unrestricted access for public endpoints using
allowtype - Environment variable configuration with pydantic-settings
- Customizable HTTP status code and message for blocked requests
Installation
pip install ip-safelist-middleware
Usage
Basic Usage with Environment Variables
from fastapi import FastAPI
from ip_safelist_middleware import IPSafeListMiddleware, ListItem, ListType
app = FastAPI()
# Configure the middleware to use environment-based IP safe list
app.add_middleware(
IPSafeListMiddleware,
list_items=[
ListItem(path=r'^/api/.*$', type=ListType.env),
]
)
Set the environment variable to specify allowed IPs:
export IP_SAFELIST_NETWORKS="127.0.0.1,192.168.0.0/24,10.0.0.0/8"
Using AWS IP Ranges
from fastapi import FastAPI
from ip_safelist_middleware import IPSafeListMiddleware, ListItem, ListType
app = FastAPI()
# Configure the middleware to use AWS IP ranges for specific paths
app.add_middleware(
IPSafeListMiddleware,
list_items=[
ListItem(path=r'^/admin/.*$', type=ListType.aws),
]
)
Configure AWS regions through environment variables:
export IP_SAFELIST_AWS_REGIONS="us-east-1,us-west-2"
Combined Configuration
You can combine different allowlist types for different paths:
from fastapi import FastAPI
from ip_safelist_middleware import IPSafeListMiddleware, ListItem, ListType
app = FastAPI()
app.add_middleware(
IPSafeListMiddleware,
list_items=[
ListItem(path=r'^/admin/.*$', type=ListType.aws), # AWS IPs for admin
ListItem(path=r'^/api/internal/.*$', type=ListType.env), # Environment IPs for internal API
ListItem(path=r'^/public/.*$', type=ListType.allow), # Unrestricted access for public endpoints
ListItem(path=r'^/(?!health|public).*$', type=ListType.env), # Environment IPs for everything except health/public
]
)
Unrestricted Access for Public Endpoints
Use ListType.allow to create public endpoints that bypass IP restrictions entirely:
from fastapi import FastAPI
from ip_safelist_middleware import IPSafeListMiddleware, ListItem, ListType
app = FastAPI()
app.add_middleware(
IPSafeListMiddleware,
list_items=[
ListItem(path=r'^/api/.*$', type=ListType.env), # Protected API endpoints
ListItem(path=r'^/public/.*$', type=ListType.allow), # Public endpoints - no IP restrictions
ListItem(path=r'^/docs.*$', type=ListType.allow), # Public documentation
]
)
The allow type is useful for:
- Public API endpoints
- Documentation pages
- Health checks accessible from anywhere
- Static assets or public content
Note: When allow is combined with other types in a list (e.g., [ListType.allow, ListType.env]), the allow type takes precedence and grants unrestricted access.
Excluding Paths
To exclude paths (e.g., health checks), use negative lookahead in your regex pattern:
ListItem(path=r'^/(?!health).*$', type=ListType.env) # All paths except /health
Custom Error Response
You can customize the HTTP status code and message returned to blocked requests:
app.add_middleware(
IPSafeListMiddleware,
list_items=[
ListItem(path=r'^/api/.*$', type=ListType.env),
],
status_code=401,
status_message="Unauthorized access"
)
Default Behavior
When added to your FastAPI application without any parameters:
- The middleware blocks all paths by default (matches pattern
^/.*$) - It uses the environment-based allowlist type
- If no IP addresses are specified in the
IP_SAFELIST_NETWORKSenvironment variable, all requests will be blocked - Blocked requests receive a
403 Forbiddenresponse - AWS IP ranges are disabled by default
Example of default behavior:
app.add_middleware(IPSafeListMiddleware) # No parameters - blocks all requests by default
To allow any traffic, you must explicitly define allowed IP addresses through environment variables or custom configurations.
Custom Rules
If you do not use the default path of ^/.*$ and add your own path rules, requests to a path without a defined rule will be refused.
Configuration
The middleware can be configured using environment variables:
| Environment Variable | Description | Default |
|---|---|---|
| IP_SAFELIST_NETWORKS | Comma-separated list of IP addresses or CIDR blocks | None |
| IP_SAFELIST_AWS_ENABLED | Enable/disable AWS IP ranges | False |
| IP_SAFELIST_AWS_REGIONS | Comma-separated list of AWS regions | us-east-1,us-east-2 |
| IP_SAFELIST_STATUS_CODE | HTTP status code for blocked requests | 403 |
| IP_SAFELIST_STATUS_MESSAGE | Message returned for blocked requests | Forbidden |
For detailed configuration options and deployment examples, see the Configuration Guide.
Documentation
Complete documentation is available at gmr.github.io/ip-safelist-middleware including:
License
This project is licensed under the BSD License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ip_safelist_middleware-1.2.0.tar.gz.
File metadata
- Download URL: ip_safelist_middleware-1.2.0.tar.gz
- Upload date:
- Size: 109.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6c0ab58a249073f8a22dd2130b0412f9e097c44b9896c1cc5385167d68f095c6
|
|
| MD5 |
e972fc1d97525f69aa80bfa20fd559b4
|
|
| BLAKE2b-256 |
3995073d63652a69d2bfa87f8ee54710e6a1e5b65c6c27bd7cf8043f31a74527
|
File details
Details for the file ip_safelist_middleware-1.2.0-py3-none-any.whl.
File metadata
- Download URL: ip_safelist_middleware-1.2.0-py3-none-any.whl
- Upload date:
- Size: 8.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a435b234d5ba8b2267bca3ec7e52f6ceac4b8a8c76e9148c528883c1f082db49
|
|
| MD5 |
82ab608d53b8bcfcce262c33502fa479
|
|
| BLAKE2b-256 |
015c48673056db9dcb1dc310c7af25fda026624b20b62a11aaecff17161c839d
|