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.0.tar.gz (3.3 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.0-py3-none-any.whl (3.4 kB view details)

Uploaded Python 3

File details

Details for the file cloudflare_ip_filter-0.1.0.tar.gz.

File metadata

  • Download URL: cloudflare_ip_filter-0.1.0.tar.gz
  • Upload date:
  • Size: 3.3 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.0.tar.gz
Algorithm Hash digest
SHA256 5c9c18db4c845cecd72cb521bd86164bb8e503a5dc0a523ee0c0a5f3dd48fd0c
MD5 584f151660ede10185a7cc295bb67a41
BLAKE2b-256 3948d1b977c77fd5daa3cd62583114f8dd59971573808f65ff77c30b9151815a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for cloudflare_ip_filter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 024a2f61dd4448ff78a0b9b1fb2fc29de01a11ef34baa7c92dffe8205aa9758e
MD5 cf200f6f46ff4eedaf7ed2259972ac72
BLAKE2b-256 ae39d9f85f3485df999f1ad14d32dd384d4c2e034fcf7690ed4a300c098c0962

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