Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Comprehensive Cloudflare platform skill covering Workers, D1, R2, KV, AI, Durable Objects, and security.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/waf/configuration.md
1# Configuration23## Prerequisites45**API Token**: Create at https://dash.cloudflare.com/profile/api-tokens6- Permission: `Zone.WAF Edit` or `Zone.Firewall Services Edit`7- Zone Resources: Include specific zones or all zones89**Zone ID**: Found in dashboard > Overview > API section (right sidebar)1011```bash12# Set environment variables13export CF_API_TOKEN="your_api_token_here"14export ZONE_ID="your_zone_id_here"15```1617## TypeScript SDK Usage1819```bash20npm install cloudflare21```2223```typescript24import Cloudflare from 'cloudflare';2526const client = new Cloudflare({ apiToken: process.env.CF_API_TOKEN });2728// Custom rules29await client.rulesets.create({30zone_id: process.env.ZONE_ID,31kind: 'zone',32phase: 'http_request_firewall_custom',33name: 'Custom WAF',34rules: [35{ action: 'block', expression: 'cf.waf.score gt 50', enabled: true },36{ action: 'challenge', expression: 'http.request.uri.path eq "/admin"', enabled: true },37],38});3940// Managed ruleset41await client.rulesets.create({42zone_id: process.env.ZONE_ID,43phase: 'http_request_firewall_managed',44rules: [{45action: 'execute',46action_parameters: { id: 'efb7b8c949ac4650a09736fc376e9aee' },47expression: 'true',48}],49});5051// Rate limiting52await client.rulesets.create({53zone_id: process.env.ZONE_ID,54phase: 'http_ratelimit',55rules: [{56action: 'block',57expression: 'http.request.uri.path starts_with "/api"',58action_parameters: {59ratelimit: {60characteristics: ['cf.colo.id', 'ip.src'],61period: 60,62requests_per_period: 100,63mitigation_timeout: 600,64},65},66}],67});68```6970## Terraform Configuration7172```hcl73provider "cloudflare" {74api_token = var.cloudflare_api_token75}7677resource "cloudflare_ruleset" "waf_custom" {78zone_id = var.zone_id79kind = "zone"80phase = "http_request_firewall_custom"8182rules {83action = "block"84expression = "cf.waf.score gt 50"85}86}87```8889**Managed Ruleset & Rate Limiting**:90```hcl91resource "cloudflare_ruleset" "waf_managed" {92zone_id = var.zone_id93name = "Managed Ruleset"94kind = "zone"95phase = "http_request_firewall_managed"9697rules {98action = "execute"99action_parameters {100id = "efb7b8c949ac4650a09736fc376e9aee"101overrides {102rules {103id = "5de7edfa648c4d6891dc3e7f84534ffa"104action = "log"105}106}107}108expression = "true"109}110}111112resource "cloudflare_ruleset" "rate_limiting" {113zone_id = var.zone_id114phase = "http_ratelimit"115116rules {117action = "block"118expression = "http.request.uri.path starts_with \"/api\""119ratelimit {120characteristics = ["cf.colo.id", "ip.src"]121period = 60122requests_per_period = 100123mitigation_timeout = 600124}125}126}127```128129## Pulumi Configuration130131```typescript132import * as cloudflare from '@pulumi/cloudflare';133134const zoneId = 'zone_id';135136// Custom rules137const wafCustom = new cloudflare.Ruleset('waf-custom', {138zoneId,139phase: 'http_request_firewall_custom',140rules: [141{ action: 'block', expression: 'cf.waf.score gt 50', enabled: true },142{ action: 'challenge', expression: 'http.request.uri.path eq "/admin"', enabled: true },143],144});145146// Managed ruleset147const wafManaged = new cloudflare.Ruleset('waf-managed', {148zoneId,149phase: 'http_request_firewall_managed',150rules: [{151action: 'execute',152actionParameters: { id: 'efb7b8c949ac4650a09736fc376e9aee' },153expression: 'true',154}],155});156157// Rate limiting158const rateLimiting = new cloudflare.Ruleset('rate-limiting', {159zoneId,160phase: 'http_ratelimit',161rules: [{162action: 'block',163expression: 'http.request.uri.path starts_with "/api"',164ratelimit: {165characteristics: ['cf.colo.id', 'ip.src'],166period: 60,167requestsPerPeriod: 100,168mitigationTimeout: 600,169},170}],171});172```173174## Dashboard Configuration1751761. Navigate to: **Security** > **WAF**1772. Select tab:178- **Managed rules** - Deploy/configure managed rulesets179- **Custom rules** - Create custom rules180- **Rate limiting rules** - Configure rate limits1813. Click **Deploy** or **Create rule**182183**Testing**: Use Security Events to test expressions before deploying.184185## Wrangler Integration186187WAF configuration is zone-level (not Worker-specific). Configuration methods:188- Dashboard UI189- Cloudflare API via SDK190- Terraform/Pulumi (IaC)191192**Workers benefit from WAF automatically** - no Worker code changes needed.193194**Example: Query WAF API from Worker**:195```typescript196export default {197async fetch(request: Request, env: Env): Promise<Response> {198return fetch(`https://api.cloudflare.com/client/v4/zones/${env.ZONE_ID}/rulesets`, {199headers: { 'Authorization': `Bearer ${env.CF_API_TOKEN}` },200});201},202};203```