Flask middleware to restrict access to Cloudflare IP ranges
Project description
Cloudflare IP Restriction Middleware for Flask
This is a simple Flask middleware module that restricts access to your Flask application only to requests coming from Cloudflare's official IPv4 ranges.
Features
- Automatically fetches the current list of Cloudflare IPv4 ranges from Cloudflare's API.
- Checks incoming request IPs against the Cloudflare IP ranges.
- Blocks any request from IPs outside Cloudflare's ranges with HTTP 403 Forbidden.
- Logs allowed and blocked requests in color-coded terminal output using
colorama. - Uses only the standard
request.remote_addrfor IP checking (does not rely onX-Forwarded-Forfor filtering, but logs the real IP if present).
Requirements
- Python 3.7+
- Flask
- requests
- colorama
Install dependencies with:
pip install flask requests colorama
Usage
- Import and call
fetch_cloudflare_ips()once at app startup to load IP ranges:
from utils.ip_filter import fetch_cloudflare_ips, restrict_to_cloudflare
app = Flask(__name__)
fetch_cloudflare_ips()
- Add
restrict_to_cloudflareas a before-request hook to block unauthorized IPs:
@app.before_request
def check_ip():
restrict_to_cloudflare()
- Your app will now only accept requests coming through Cloudflare's IPv4 proxies.
How it works
- The module fetches Cloudflare's current IPv4 CIDR blocks from their API.
- Every incoming request's IP (
request.remote_addr) is checked if it belongs to any of those CIDRs. - If yes, access is allowed and the real client IP (from
X-Forwarded-Forheader if present) is logged in green. - Otherwise, the request is aborted with HTTP 403, and the blocked IP is logged in red.
Notes
- This module does not handle IPv6 addresses.
- It assumes your Flask app is running behind Cloudflare proxy.
- The
X-Forwarded-Forheader is only used for logging purposes, not for access control. - You may want to run
fetch_cloudflare_ips()periodically (e.g. daily) to keep IP ranges up to date.
Example output
Cloudflare IP ranges loaded successfully.
✅ Allowed access. Real IP: 203.0.113.45
⛔️ Blocked IP access: 198.51.100.23
License
MIT License
Feel free to contribute or ask questions!
Project details
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 cloudflare_ip_filter-0.1.3.tar.gz.
File metadata
- Download URL: cloudflare_ip_filter-0.1.3.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b584f1fe854abeb5a3c82aa2d6c23ea9a56c99cc6c09260a1353f73789657b3
|
|
| MD5 |
744080eef41b230d145226ec99f68503
|
|
| BLAKE2b-256 |
b89ea6eb8f49625d7aa320d02e529deadf02066e555312055c0d517e302f4697
|
File details
Details for the file cloudflare_ip_filter-0.1.3-py3-none-any.whl.
File metadata
- Download URL: cloudflare_ip_filter-0.1.3-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d6a7abcf6668526cf833030c2cac6e451ad68905255e70989d7344a7214ebb6
|
|
| MD5 |
39edab45591f36b193188471182d908c
|
|
| BLAKE2b-256 |
b7e914f2e838bade52885a8e94320493f5f715fc3af3c321840dd8d0d5e3fbd0
|