This package is a WIP. It currently contains a command to generate the expression for a Cloudflare security rule for your Laravel application routes.
A Laravel application running Laravel 12 or higher. Not running a stable version of Laravel? Upgrade with Shift.
You can install this package by running the following command:
composer require -W nexxai/laravel-cfcacheTo publish the configuration file (only needed when using the --sync argument):
php artisan vendor:publish --tag=cf-waf-rule-configTo use the automatic sync feature, you need to configure your Cloudflare API credentials. Add the following to your .env file:
CF_WAF_API_TOKEN=your-api-token-here
CF_WAF_ZONE_ID=your-zone-id-here-
API Token:
- Go to Cloudflare Dashboard
- Click "Create Token"
- Use the "Custom token" template
- Grant the following permissions:
- Zone -> Firewall Services -> Edit
- Include your specific zone in the Zone Resources
- Create the token and copy it to your
.envfile
-
Zone ID:
- Go to your domain's overview page in Cloudflare
- Find the Zone ID in the right sidebar under "API"
- Copy it to your
.envfile
This command was inspired by Jason McCreary's tweet: https://x.com/gonedark/status/1978458884948775294
Generate the WAF rule expression:
php artisan cloudflare:waf-ruleOnce generated, you can copy and paste the expression into your domain's security rules (Security -> Security Rules -> Create Rule -> Custom Rule -> Edit expression)
Automatically create or update the WAF rule in Cloudflare:
php artisan cloudflare:waf-rule --syncAfter publishing the configuration file, you can customize additional settings in config/cf-waf-rule.php:
return [
'api' => [
'token' => env('CF_WAF_API_TOKEN'),
'zone_id' => env('CF_WAF_ZONE_ID'),
],
'waf' => [
'rule_identifier' => env('CF_WAF_RULE_ID', 'laravel-waf-rule'),
'rule_description' => env('CF_WAF_RULE_DESCRIPTION', 'Valid Laravel Routes'),
'rule_action' => env('CF_WAF_RULE_ACTION', 'block'),
],
'settings' => [
'timeout' => env('CF_WAF_API_TIMEOUT', 30),
'retry_attempts' => env('CF_WAF_API_RETRY_ATTEMPTS', 3),
],
];block- Block the request entirelychallenge- Present a challenge to the visitorjs_challenge- Present a JavaScript challengemanaged_challenge- Use Cloudflare's managed challengeallow- Allow the requestlog- Log the request without taking actionbypass- Bypass all security features
Contributions to this project are welcome. You may open a Pull Request against the main branch. Please ensure you write a clear description (ideally with code samples) and all workflows are passing.