Skip to main content

Wrapper library to import / export multiple remote rules and easily create, modify and delete rules.

Project description

Welcome to Cloudflare WAF Custom rules library 👋

Check documentation website · View PyPI package

Previously known as Cloudflare Firewall Rules

Website  Donate  Contributions  Tested on Python 3.13  License  Size 

A Cloudflare wrapper to bulk add / edit your WAF custom rules using Cloudflare's API.

This library is a wrapper that aims to easily create, modify, delete security rules. It also provides a way to import & export new rules in your domain's firewall.

If you have several rules that you want to duplicate among your domains, this package is made for you!

Cloudflare's Security Rules are available in a domain in the Security > Security Rules tab, by default, you have a free plan with 5 custom rules available.

The official documentation is available here: https://developers.cloudflare.com/waf/custom-rules/

Domain Security Rules

📥 Installation

pip install cf_rules

# OR

git clone https://github.com/QuentiumYT/Cloudflare-Firewall-Rules.git
cd Cloudflare-Firewall-Rules/
pip install .

🚀 Usage

You have 2 auth methods available:

A Global API Key or a specific API Token generated from here: https://dash.cloudflare.com/profile/api-tokens

Cloudflare Global API Key

Cloudflare Key

Using a Global API Key, you will have access to everything allowed by a Cloudflare account. It can access all domains from every account you have, this might be overpowered...

Cloudflare API Token

An API token is recommended to keep control of specific domains only. You will need to give the correct permissions for Cloudflare's WAF Custom rules to work.

The required permissions are "Zone.Zone, Zone.Firewall Services"

Cloudflare Token

Here is a token creation example:

Cloudflare Token

💨 Quickstart

You can use any example scripts in the examples folder, just create a .env file

I might add more examples in the future, but everything is in the docs :)


Create any Python file in the cloned directory and paste these lines

from cf_rules import Cloudflare

cf = Cloudflare()
cf.auth_key("<your-address@email.com>", "<your-global-api-key>")
# OR
cf.auth_token("<your-specific-bearer-token>")

domains = cf.domains[0].name

print(domains)
# >>> ['example.com']

cf.export_rules("example.com")
# Creates a text file for every rule you have on your domain

cf.create_rule("example.com", "My Bad Bots FW rule", "Bad Bots", "challenge")
# Create a new rule with the content of the "Bad bots.txt" file with the challenge action

cf.update_rule("example.com", "My Bad Bots FW rule", "Bad Bots lib")
# Change the rule's expression to the content of the "Bad bots lib.txt" file

🤝 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change and join your fork with the modifications.
Please make sure to test your suggestions before committing.

If you don't feel comfortable coding, you can submit your idea about what you would like to see implemented.

Any PR with small code examples or better documentation changes is appreciated :)

👤 Author

Quentin L.

Please ✰ this repository if this project helped you!

📖 License

Apache 2.0


Made withby QuentiumYT

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

cf_rules-2.2.0.tar.gz (20.0 kB view details)

Uploaded Source

Built Distribution

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

cf_rules-2.2.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file cf_rules-2.2.0.tar.gz.

File metadata

  • Download URL: cf_rules-2.2.0.tar.gz
  • Upload date:
  • Size: 20.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for cf_rules-2.2.0.tar.gz
Algorithm Hash digest
SHA256 58db6af93ca06aab1921de6b1507186e60d247b5f270c2e5083b99d176bf7917
MD5 21854c71b66c9f99f012645489a0c1f1
BLAKE2b-256 6eed2ef619807994bcdc1a088a29cf5255e99dee8cebfcdf0c3d53fdb55c116e

See more details on using hashes here.

File details

Details for the file cf_rules-2.2.0-py3-none-any.whl.

File metadata

  • Download URL: cf_rules-2.2.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.2

File hashes

Hashes for cf_rules-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d00b9a40aa5155711ca274e79930e95aa8d36bd557fdfdd765a322cc7c989fa
MD5 a7e8079220c0baf22a4d6585c8c7561b
BLAKE2b-256 229287a5ebc9ba7d9fe34aebf247a5461ae33e971e678373a4b6b7ba29258b5c

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