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/ddos/patterns.md
1# DDoS Protection Patterns23## Allowlist Trusted IPs45```typescript6const config = {7description: "Allowlist trusted IPs",8rules: [{9expression: "ip.src in { 203.0.113.0/24 192.0.2.1 }",10action: "execute",11action_parameters: {12id: managedRulesetId,13overrides: { sensitivity_level: "eoff" },14},15}],16};1718await client.accounts.rulesets.phases.entrypoint.update("ddos_l7", {19account_id: accountId,20...config,21});22```2324## Route-specific Sensitivity2526```typescript27const config = {28description: "Route-specific protection",29rules: [30{31expression: "not http.request.uri.path matches \"^/api/\"",32action: "execute",33action_parameters: {34id: managedRulesetId,35overrides: { sensitivity_level: "default", action: "block" },36},37},38{39expression: "http.request.uri.path matches \"^/api/\"",40action: "execute",41action_parameters: {42id: managedRulesetId,43overrides: { sensitivity_level: "low", action: "managed_challenge" },44},45},46],47};48```4950## Progressive Enhancement5152```typescript53enum ProtectionLevel { MONITORING = "monitoring", LOW = "low", MEDIUM = "medium", HIGH = "high" }5455const levelConfig = {56[ProtectionLevel.MONITORING]: { action: "log", sensitivity: "eoff" },57[ProtectionLevel.LOW]: { action: "managed_challenge", sensitivity: "low" },58[ProtectionLevel.MEDIUM]: { action: "managed_challenge", sensitivity: "medium" },59[ProtectionLevel.HIGH]: { action: "block", sensitivity: "default" },60} as const;6162async function setProtectionLevel(zoneId: string, level: ProtectionLevel, rulesetId: string, client: Cloudflare) {63const settings = levelConfig[level];64return client.zones.rulesets.phases.entrypoint.update("ddos_l7", {65zone_id: zoneId,66rules: [{67expression: "true",68action: "execute",69action_parameters: { id: rulesetId, overrides: { action: settings.action, sensitivity_level: settings.sensitivity } },70}],71});72}73```7475## Dynamic Response to Attacks7677```typescript78interface Env { CLOUDFLARE_API_TOKEN: string; ZONE_ID: string; KV: KVNamespace; }7980export default {81async fetch(request: Request, env: Env): Promise<Response> {82if (request.url.includes("/attack-detected")) {83const attackData = await request.json();84await env.KV.put(`attack:${Date.now()}`, JSON.stringify(attackData), { expirationTtl: 86400 });85const recentAttacks = await getRecentAttacks(env.KV);86if (recentAttacks.length > 5) {87await setProtectionLevel(env.ZONE_ID, ProtectionLevel.HIGH, managedRulesetId, client);88return new Response("Protection increased");89}90}91return new Response("OK");92},93async scheduled(event: ScheduledEvent, env: Env): Promise<void> {94const recentAttacks = await getRecentAttacks(env.KV);95if (recentAttacks.length === 0) await setProtectionLevel(env.ZONE_ID, ProtectionLevel.MEDIUM, managedRulesetId, client);96},97};98```99100## Multi-rule Tiered Protection (Enterprise Advanced)101102```typescript103const config = {104description: "Multi-tier DDoS protection",105rules: [106{107expression: "not ip.src in $known_ips and not cf.bot_management.score gt 30",108action: "execute",109action_parameters: { id: managedRulesetId, overrides: { sensitivity_level: "default", action: "block" } },110},111{112expression: "cf.bot_management.verified_bot",113action: "execute",114action_parameters: { id: managedRulesetId, overrides: { sensitivity_level: "medium", action: "managed_challenge" } },115},116{117expression: "ip.src in $trusted_ips",118action: "execute",119action_parameters: { id: managedRulesetId, overrides: { sensitivity_level: "low" } },120},121],122};123```124125## Defense in Depth126127Layered security stack: DDoS + WAF + Rate Limiting + Bot Management.128129```typescript130// Layer 1: DDoS (volumetric attacks)131await client.zones.rulesets.phases.entrypoint.update("ddos_l7", {132zone_id: zoneId,133rules: [{ expression: "true", action: "execute", action_parameters: { id: ddosRulesetId, overrides: { sensitivity_level: "medium" } } }],134});135136// Layer 2: WAF (exploit protection)137await client.zones.rulesets.phases.entrypoint.update("http_request_firewall_managed", {138zone_id: zoneId,139rules: [{ expression: "true", action: "execute", action_parameters: { id: wafRulesetId } }],140});141142// Layer 3: Rate Limiting (abuse prevention)143await client.zones.rulesets.phases.entrypoint.update("http_ratelimit", {144zone_id: zoneId,145rules: [{ expression: "http.request.uri.path eq \"/api/login\"", action: "block", ratelimit: { characteristics: ["ip.src"], period: 60, requests_per_period: 5 } }],146});147148// Layer 4: Bot Management (automation detection)149await client.zones.rulesets.phases.entrypoint.update("http_request_sbfm", {150zone_id: zoneId,151rules: [{ expression: "cf.bot_management.score lt 30", action: "managed_challenge" }],152});153```154155## Cache Strategy for DDoS Mitigation156157Exclude query strings from cache key to counter randomized query parameter attacks.158159```typescript160const cacheRule = {161expression: "http.request.uri.path matches \"^/api/\"",162action: "set_cache_settings",163action_parameters: {164cache: true,165cache_key: { ignore_query_strings_order: true, custom_key: { query_string: { exclude: { all: true } } } },166},167};168169await client.zones.rulesets.phases.entrypoint.update("http_request_cache_settings", { zone_id: zoneId, rules: [cacheRule] });170```171172**Rationale**: Attackers randomize query strings (`?random=123456`) to bypass cache. Excluding query params ensures cache hits absorb attack traffic.173174See [configuration.md](./configuration.md) for rule structure details.175