Cloudflare, WAF, and Web Application Security Best Practices

Application-layer security isn't just input validation — the network edge in front of your app is where most attacks are stopped or missed. Here's how I configure Cloudflare and a WAF for production apps.

By Sandaruwan Jayasundara — Senior Software Engineer | Cloud & DevOps

A well-written application is still exposed if the edge in front of it isn't configured. Cloudflare and a properly tuned WAF handle an entire class of attacks before they ever reach your servers.

1. DNS and Proxy Setup

2. WAF Rules That Matter Most

Turn on WAF rules in "log" mode before "block" mode. A misconfigured rule that blocks real users is worse than the attack it was meant to stop.

3. Rate Limiting

4. Bot Management

Not all bots are hostile — legitimate crawlers (Googlebot, Bingbot, and AI crawlers like GPTBot and ClaudeBot) need to reach your content for SEO and discoverability. Configure bot rules to challenge or block credential-stuffing and scraping bots specifically, rather than blanket-blocking all automated traffic.

5. Security Headers at the Application Layer

Strict-Transport-Security: max-age=63072000; includeSubDomains
Content-Security-Policy: default-src 'self'
X-Content-Type-Options: nosniff
Referrer-Policy: strict-origin-when-cross-origin

These headers stop entire attack classes (clickjacking, MIME sniffing, some XSS vectors) for effectively zero runtime cost.

6. DDoS Protection

Cloudflare's network-level DDoS mitigation is largely automatic once traffic is proxied — the engineering work is making sure origin servers aren't directly reachable, and that rate limiting protects expensive endpoints (search, uploads, report generation) that a volumetric attack would target specifically.

Bringing It Together

Edge security and application security are complementary, not interchangeable — a WAF catches what your code shouldn't have to, and your code should never assume the WAF caught everything. I configure both layers together as part of any production deployment.

I'm Sandaruwan Jayasundara — Senior Software Engineer | Cloud & DevOps. See AWS cloud architecture for startups or get in touch about securing your infrastructure.