The Complete Production SOP: How to Deploy and Run Cloudflare WAF Custom Rulesets & Rate Limiting in Autonomous AI Agents
Production SOP to install and run Cloudflare WAF Custom Rulesets in Codex, Claude Code, and Cursor. Generate Wirefilter rules, rate limits, and API configs.
Technical Overview & Architectural Context
Configuring Cloudflare Web Application Firewall (WAF) custom rulesets and rate-limiting policies requires strict adherence to Cloudflare's Wirefilter expression syntax, correct phase designations (such as http_request_firewall_custom or http_ratelimit), and precise JSON schema payloads for the Rulesets API. When autonomous coding agents attempt to generate WAF configurations without deterministic constraints, they frequently hallucinate legacy firewall rule structures (such as deprecated zone filters), mix up bitwise and logical operators, or construct malformed Wirefilter fields (like using URI parameters instead of canonicalized path matching). Furthermore, unguided agents routinely botch rate-limiting definitions by omitting required characteristics (e.g., cf.colo.id, ip.src) or confusing counting expressions with mitigation expressions. This results in 400 Bad Request errors from the Cloudflare API or, worse, active outages caused by over-permissive challenges or unintentional wholesale IP blocks across legitimate production traffic. By injecting the cloudflare-waf-custom-rulesets skill, autonomous runtimes (Codex, Claude Code, Cursor, and Windsurf) inherit deterministic procedural standard operating procedures (SOPs). The skill forces the agent to validate Wirefilter syntax locally, respect zone-level phase boundaries, parameterize threat scores appropriately, and output production-ready Terraform or Cloudflare API v4 payloads with zero sandbox leaks.
Prerequisites & Environment Requirements
Verified Search Intents & Long-Tail Queries Solved
This operational tutorial is engineered to provide deterministic solutions for these developer search queries:
Step-by-Step Execution Playbook
Local Environment Setup & SKILL.md Mounting
Create the local skill manifest directory and inject verified procedural rules into the agent runtime.
Autonomous runtimes discover capabilities through standardized directory paths such as ~/.agents/skills or project-level .agents/skills directories. Mounting this skill exposes frontmatter triggers ('write cloudflare waf rule', 'configure cloudflare rate limiting', 'block bad bots cloudflare') to the agent's intent parser. When matched, the agent enforces Wirefilter syntax guidelines and proper Rulesets API phase sequencing before emitting any configuration files.
mkdir -p .agents/skills/cloudflare-waf-custom-rulesets && curl -fsSL https://askills.dev/api/skills/cloudflare-waf-custom-rulesets/raw -o .agents/skills/cloudflare-waf-custom-rulesets/SKILL.md---
name: "cloudflare-waf-custom-rulesets"
description: "Protect web applications against DDoS, bots, and brute force using Cloudflare WAF Rulesets."
metadata:
author: "Cloudflare"
version: "1.0.0"
runtime_compatibility:
- "codex"
- "claude-code"
- "cursor"
- "windsurf"
triggers:
- "write cloudflare waf rule"
- "configure cloudflare rate limiting"
- "block bad bots cloudflare"
permissions:
fs_read: true
fs_write: "workspace"
shell_exec: false
net_access: false
---
# Cloudflare WAF Custom Rulesets & Rate Limiting SOP
- Target Phase: http_request_firewall_custom or http_ratelimit
- Expression Syntax: Strict Wirefilter standard (e.g., http.request.uri.path eq "/login")
- Verification: Validate JSON payloads against Cloudflare Rulesets API v4 specificationsKeep shell_exec and net_access set to false in the skill manifest. Forcing the agent to work purely with workspace filesystem outputs prevents credential leakage and reduces token usage from unnecessary network roundtrips.
Claude Code and Cursor scan different directories by default. Ensure your project root contains a symlink from .claude/skills or .cursor/rules pointing to .agents/skills to prevent missed trigger activations.
Environment Secrets & Dependency Validation
Inject Cloudflare credentials and configure environment parameters without exposing secrets to LLM context.
To deploy or validate rules generated by the skill, local tooling requires a Cloudflare Zone ID and API Token. Rather than piping these secrets directly into the LLM conversation history—which leaks tokens into context buffers and prompt caching layers—store them strictly in a local untracked .env file and ensure gitignore policies are enforced.
echo 'CLOUDFLARE_ZONE_ID=023e105f4ecef8ad9ca31a8372d0c353
CLOUDFLARE_API_TOKEN=cf_pat_abc123EXAMPLETOKEN456' > .env.local && echo '.env.local' >> .gitignoreInstruct the agent to reference process.env.CLOUDFLARE_API_TOKEN or os.environ['CLOUDFLARE_API_TOKEN'] in generated automation scripts instead of hardcoding placeholders like 'INSERT_API_KEY_HERE'.
Ensure .env.local is explicitly listed in your .gitignore. Sandboxed agents running commit tasks might inadvertently stage untracked files containing live production security tokens.
Intent Matching & Trigger Invocation
Invoke deterministic rule synthesis using precise natural language triggers aligned with SKILL.md.
Agent runtimes monitor user inputs for semantic proximity to the defined frontmatter triggers. Initiating your request with exact phrases such as 'write cloudflare waf rule' or 'configure cloudflare rate limiting' guarantees that the runtime selects the Cloudflare WAF SOP over generic web server routing rules.
State your security intent, target endpoint, matching criteria (URI, ASN, or Threat Score), and required action (block, managed_challenge, log) in a single unambiguous prompt.
Avoid mixing edge-worker routing instructions with WAF security instructions in the same prompt; this causes the agent to hallucinate Cloudflare Workers code instead of native declarative WAF rulesets.
Verification, Linting & Output Quality Gate
Execute local syntactic verification on the generated ruleset JSON or Terraform HCL before deployment.
The skill mandates that all generated Wirefilter expressions and ruleset configurations undergo structural validation. You can run a lightweight schema check using Node.js or Terraform CLI to verify that rule expressions are syntactically sound, balanced in quotes/parentheses, and mapped to legitimate Cloudflare Ruleset phases.
node -e 'const fs=require("fs"); const r=JSON.parse(fs.readFileSync("waf-rule.json")); if(!r.rules[0].expression || !r.rules[0].action) process.exit(1); console.log("Ruleset expression valid:", r.rules[0].expression);'Always inspect the generated git diff or output JSON for proper escaping of quotes within complex Wirefilter expressions (e.g., http.request.headers["user-agent"]).
Rate limiting rules must use phase 'http_ratelimit' and include a 'ratelimit' object block with 'characteristics'. Placing rate limiting logic inside 'http_request_firewall_custom' will cause API rejection.
Real-World Scenario: Automated Defense Against Credential Stuffing on /api/v1/auth/login
“write cloudflare waf rule to protect /api/v1/auth/login from brute force attacks: managed_challenge any request with ip.threat_score greater than 20, and configure cloudflare rate limiting to block clients exceeding 10 POST requests per minute.”
Runtime Troubleshooting & Failure Modes
Root Cause: The agent hallucinated the Wirefilter field name, using 'http.request.url.path' instead of the canonical Cloudflare field 'http.request.uri.path'.
sed -i 's/http.request.url.path/http.request.uri.path/g' waf-rule.json && node -e 'const r=JSON.parse(require("fs").readFileSync("waf-rule.json")); console.log("Fixed expression:", r.rules[0].expression);'Root Cause: The agent attempted to append a 'ratelimit' block to a rule deployed in the 'http_request_firewall_custom' phase rather than target phase 'http_ratelimit'.
node -e 'const fs=require("fs"); const f="waf-rule.json"; const data=JSON.parse(fs.readFileSync(f)); data.phase="http_ratelimit"; fs.writeFileSync(f, JSON.stringify(data, null, 2)); console.log("Updated phase to http_ratelimit");'Frequently Asked Questions (Cloudflare WAF Custom Rulesets & Rate Limiting SOP)
Q:Can I use Cloudflare WAF Custom Rulesets & Rate Limiting across Codex, Claude Code, and Cursor simultaneously?
Yes. The skill conforms to the universal SKILL.md specification. Symlinking the skill manifest into each runtime's expected config path (.agents/skills, .claude/skills, or .cursor/rules) allows all supported engines to activate the same deterministic SOP.
Q:How does Cloudflare WAF Custom Rulesets & Rate Limiting prevent context window overflow during long agent sessions?
The skill provides concise, deterministic procedural boundaries and explicit Wirefilter templates directly at the point of trigger. It eliminates lengthy back-and-forth LLM correction loops and discards redundant API documentation from the context window.
Q:Does this skill require escalated root or container permissions?
No. The skill is audited with a security score of 98/100. It requires zero shell execution permissions (shell_exec: false) and zero outbound network access (net_access: false), operating solely within workspace filesystem boundaries.
Ready to equip your autonomous agent with Cloudflare WAF Custom Rulesets & Rate Limiting?
Copy the complete audited SKILL.md specification or download the 1-click zip bundle directly from our verified registry.