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/bot-management/patterns.md
1# Bot Management Patterns23## E-commerce Protection45```txt6# High security for checkout7(cf.bot_management.score lt 50 and http.request.uri.path in {"/checkout" "/cart/add"} and not cf.bot_management.verified_bot and not cf.bot_management.corporate_proxy)8Action: Managed Challenge9```1011## API Protection1213```txt14# Protect API with JS detection + score15(http.request.uri.path matches "^/api/" and (cf.bot_management.score lt 30 or not cf.bot_management.js_detection.passed) and not cf.bot_management.verified_bot)16Action: Block17```1819## SEO-Friendly Bot Handling2021```txt22# Allow search engine crawlers23(cf.bot_management.score lt 30 and not cf.verified_bot_category in {"Search Engine Crawler"})24Action: Managed Challenge25```2627## Block AI Scrapers2829```txt30# Block training crawlers only (allow AI assistants/search)31(cf.verified_bot_category eq "AI Crawler")32Action: Block3334# Block all AI-related bots (training + assistants + search)35(cf.verified_bot_category in {"AI Crawler" "AI Assistant" "AI Search"})36Action: Block3738# Allow AI Search, block AI Crawler and AI Assistant39(cf.verified_bot_category in {"AI Crawler" "AI Assistant"})40Action: Block4142# Or use dashboard: Security > Settings > Bot Management > Block AI Bots43```4445## Rate Limiting by Bot Score4647```txt48# Stricter limits for suspicious traffic49(cf.bot_management.score lt 50)50Rate: 10 requests per 10 seconds5152(cf.bot_management.score ge 50)53Rate: 100 requests per 10 seconds54```5556## Mobile App Allowlisting5758```txt59# Identify mobile app by JA3/JA460(cf.bot_management.ja4 in {"fingerprint1" "fingerprint2"})61Action: Skip (all remaining rules)62```6364## Datacenter Detection6566```typescript67import type { IncomingRequestCfProperties } from '@cloudflare/workers-types';6869// Low score + not corporate proxy = likely datacenter bot70export default {71async fetch(request: Request): Promise<Response> {72const cf = request.cf as IncomingRequestCfProperties | undefined;73const botMgmt = cf?.botManagement;7475if (botMgmt?.score && botMgmt.score < 30 &&76!botMgmt.corporateProxy && !botMgmt.verifiedBot) {77return new Response('Datacenter traffic blocked', { status: 403 });78}7980return fetch(request);81}82};83```8485## Conditional Delay (Tarpit)8687```typescript88import type { IncomingRequestCfProperties } from '@cloudflare/workers-types';8990// Add delay proportional to bot suspicion91export default {92async fetch(request: Request): Promise<Response> {93const cf = request.cf as IncomingRequestCfProperties | undefined;94const botMgmt = cf?.botManagement;9596if (botMgmt?.score && botMgmt.score < 50 && !botMgmt.verifiedBot) {97// Delay: 0-2 seconds for scores 50-098const delayMs = Math.max(0, (50 - botMgmt.score) * 40);99await new Promise(r => setTimeout(r, delayMs));100}101102return fetch(request);103}104};105```106107## Layered Defense108109```txt1101. Bot Management (score-based)1112. JavaScript Detections (for JS-capable clients)1123. Rate Limiting (fallback protection)1134. WAF Managed Rules (OWASP, etc.)114```115116## Progressive Enhancement117118```txt119Public content: High threshold (score < 10)120Authenticated: Medium threshold (score < 30)121Sensitive: Low threshold (score < 50) + JSD122```123124## Zero Trust for Bots125126```txt1271. Default deny (all scores < 30)1282. Allowlist verified bots1293. Allowlist mobile apps (JA3/JA4)1304. Allowlist corporate proxies1315. Allowlist static resources132```133134## Workers: Score + JS Detection135136```typescript137import type { IncomingRequestCfProperties } from '@cloudflare/workers-types';138139export default {140async fetch(request: Request): Promise<Response> {141const cf = request.cf as IncomingRequestCfProperties | undefined;142const botMgmt = cf?.botManagement;143const url = new URL(request.url);144145if (botMgmt?.staticResource) return fetch(request); // Skip static146147// API endpoints: require JS detection + good score148if (url.pathname.startsWith('/api/')) {149const jsDetectionPassed = botMgmt?.jsDetection?.passed ?? false;150const score = botMgmt?.score ?? 100;151152if (!jsDetectionPassed || score < 30) {153return new Response('Unauthorized', { status: 401 });154}155}156157return fetch(request);158}159};160```161162## Rate Limiting by JWT Claim + Bot Score163164```txt165# Enterprise: Combine bot score with JWT validation166Rate limiting > Custom rules167- Field: lookup_json_string(http.request.jwt.claims["{config_id}"][0], "sub")168- Matches: user ID claim169- Additional condition: cf.bot_management.score lt 50170```171172## WAF Integration Points173174- **WAF Custom Rules**: Primary enforcement mechanism175- **Rate Limiting Rules**: Bot score as dimension, stricter limits for low scores176- **Transform Rules**: Pass score to origin via custom header177- **Workers**: Programmatic bot logic, custom scoring algorithms178- **Page Rules / Configuration Rules**: Zone-level overrides, path-specific settings179180## See Also181182- [gotchas.md](./gotchas.md) - Common errors, false positives/negatives, limitations183