Cloudflare is a widely used edge layer that combines DNS, CDN, WAF and abuse protection. Well configured, it filters malicious traffic before it reaches the origin, improves performance and adds security controls. Poorly configured, it gives a false sense of protection. That happens when the origin remains directly exposed, when TLS runs in a permissive mode or when the rules were never tuned.
In a defensive Cloudflare setup for web security, some points weigh more than others. The first is protecting the origin against bypass, so nobody can go around the edge. Then come WAF tuning, choosing the correct TLS mode and using rate limiting and bot protection with judgment.
Protecting the origin and the TLS mode
The most common mistake is leaving the origin directly reachable by IP, outside Cloudflare. If an attacker discovers the real IP, they go around the entire edge. Restrict the origin to accept connections only from Cloudflare (allowing only the edge's IP ranges in the firewall, for example) and avoid exposing the origin IP in DNS records, emails or headers.
Choose the TLS mode carefully. Permissive modes can accept unencrypted traffic between the edge and the origin, which cancels out part of the protection. Prefer a mode that requires valid end-to-end encryption between Cloudflare and your origin.
Locking the origin so it only accepts the edge
Restricting the origin takes more than one layer. In the origin's firewall or security group, allow only Cloudflare's network ranges and deny the rest. Where the platform offers it, turn on origin authentication with Cloudflare's own certificate, so the origin only responds to requests that provably come from the edge. And review the allowed ranges from time to time, because a single forgotten rule that opens the origin to the whole internet is enough to undo all this effort.
WAF, rules and bots
Turn on the managed rulesets and, as with any WAF, introduce blocking while watching the results: measure the impact first, then start blocking, so you do not drop legitimate traffic. Create rules specific to your context, such as ones that protect administrative routes and sensitive endpoints, and use rate limiting at points prone to abuse. Bot protection helps against automation, but it needs to be dosed carefully so it does not create friction.
- Restrict the origin to accept only Cloudflare traffic
- Do not leak the origin IP in DNS, email or headers
- Use a TLS mode that requires valid encryption all the way to the origin
- Turn on managed rules and tune them by observation
- Apply rate limiting to sensitive and login routes
- Dose bot protection so it does not block real users
Security headers at the edge
The edge is a good place to apply security response headers consistently, without depending on each application remembering to send them. The most important is HSTS, which tells the browser to access the site only over HTTPS on later visits and closes the window in which a clear-text request could be intercepted. The first visit is only covered if the domain is on the browsers' preload list. Add to it the headers that reduce entire classes of client-side attacks, as in the example below.
# Force HTTPS; enable preload only when you are sure of the coverageStrict-Transport-Security: max-age=31536000; includeSubDomains# Prevents the content from being loaded in a third-party frameX-Frame-Options: DENY# Stops the browser from guessing the content typeX-Content-Type-Options: nosniff# Controls how much of the referrer is sent to other sitesReferrer-Policy: strict-origin-when-cross-originA well-built Content Security Policy reduces the impact of injected scripts, but it has to be designed from what the application actually loads; otherwise it breaks features. Start in report-only mode and watch the violations before enforcing the policy, the same way you would with WAF rules. Applying HSTS with includeSubDomains and, later, with preload brings a big gain. Before that, though, confirm that every subdomain really supports HTTPS, because preload is hard to reverse.
Origin authentication and administrative routes
Filtering by network range protects a lot, but whoever discovers the origin IP can still try to talk to it directly. With certificate-based origin authentication, the origin only accepts connections that present the edge's certificate, which makes bypass much harder: even knowing the address, the attacker does not have the certificate the origin requires. To put it simply, the network filter hides the door, and the certificate is the lock that keeps it shut for anyone who finds it.
Administrative routes deserve protection beyond the WAF. Management dashboards, internal areas and operations endpoints are favorite targets and do not need to be reachable from the whole internet. Put an authentication layer at the edge for these routes, one that requires identity before the request even reaches the application, and restrict access to known networks or identities. This shrinks the exposed surface a great deal on the very paths with the highest impact.
- Authenticate the origin with a certificate, in addition to filtering by network range
- Require identity at the edge for dashboards and administrative routes
- Restrict internal routes to known networks or identities
- Do not expose operations endpoints to the internet without need
- Treat administrative access as the highest-impact access to protect
DNS, cache and configuration precautions
In DNS, keep the proxy enabled on the records that should go through the edge, because an unproxied record exposes the origin IP. Be careful with caching sensitive content too. A badly defined cache rule can serve one user content meant for another (one customer's account page delivered to the next customer, for example). Define explicitly what can and cannot be cached.
Like any edge, Cloudflare complements the application's security but does not take its place. A flaw in the application remains exploitable by any request that gets past the rules. Treat the edge as one more layer and keep tuning the configuration over time.
Practical checklist
- Restrict the origin to accept only Cloudflare traffic
- Turn on certificate-based origin authentication, in addition to network filtering
- Avoid leaking the origin IP in DNS, email and headers
- Choose a TLS mode with valid encryption all the way to the origin
- Apply HSTS and confirm HTTPS coverage before includeSubDomains and preload
- Set security headers consistently at the edge
- Design the CSP from what the application loads and start in report-only mode
- Turn on managed WAF rules
- Introduce blocking under observation before enforcing it for good
- Apply rate limiting to sensitive and login routes
- Require identity at the edge for administrative routes
- Dose bot protection so it does not create friction
- Keep the proxy enabled on DNS records that should go through the edge
- Define explicitly what can be cached
- Review rules and allowed ranges continuously
Best practices
- Protect the origin against direct access that bypasses the edge
- Lock down the origin with certificate authentication, in addition to the network filter
- Use end-to-end TLS between Cloudflare and the origin
- Standardize security headers at the edge, HSTS included
- Tune the WAF and the CSP by observation before enforcing
- Protect administrative routes with identity at the edge
- Manage caching so content does not leak between users
- Combine the edge with fixes in the application
- Treat configuration as an ongoing process
Common mistakes
- Origin reachable directly by IP
- The attacker discovers the real IP and bypasses all the edge protection.
- Permissive TLS mode up to the origin
- Traffic can travel unencrypted between the edge and the origin.
- Origin IP leaking in records or emails
- It makes it easier to bypass Cloudflare and attack the origin directly.
- Administrative routes exposed to the internet
- The highest-impact targets become reachable from any source.
- HSTS preload enabled without covering every subdomain
- A subdomain without HTTPS becomes unreachable, and reversing the change is slow.
- Badly defined cache rules
- One user's sensitive content can be served to another.
- Trusting the edge and ignoring the application
- Application flaws remain exploitable when the request gets through.
Authorship and review
Cesar Gargiulo
CEO and founder of GUARDIASEC. Technical and editorial lead for the content published in this library.
Published on . Found a technical error? Write to [email protected] and we will correct it and keep a record of the change.