Skip to main content

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_addr for IP checking (does not rely on X-Forwarded-For for filtering, but logs the real IP if present).

Requirements

  • Python 3.7+
  • Flask
  • requests
  • colorama

Install dependencies with:

pip install flask requests colorama

Usage

  1. 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()
  1. Add restrict_to_cloudflare as a before-request hook to block unauthorized IPs:
@app.before_request
def check_ip():
    restrict_to_cloudflare()
  1. 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-For header 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-For header 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

cloudflare_ip_filter-0.1.3.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cloudflare_ip_filter-0.1.3-py3-none-any.whl (4.2 kB view details)

Uploaded Python 3

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

Hashes for cloudflare_ip_filter-0.1.3.tar.gz
Algorithm Hash digest
SHA256 0b584f1fe854abeb5a3c82aa2d6c23ea9a56c99cc6c09260a1353f73789657b3
MD5 744080eef41b230d145226ec99f68503
BLAKE2b-256 b89ea6eb8f49625d7aa320d02e529deadf02066e555312055c0d517e302f4697

See more details on using hashes here.

File details

Details for the file cloudflare_ip_filter-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for cloudflare_ip_filter-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 2d6a7abcf6668526cf833030c2cac6e451ad68905255e70989d7344a7214ebb6
MD5 39edab45591f36b193188471182d908c
BLAKE2b-256 b7e914f2e838bade52885a8e94320493f5f715fc3af3c321840dd8d0d5e3fbd0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page