Skip to content

Cloudflare for web security: WAF, DNS, cache and edge protection

Cloudflare's edge protects you when it is well configured. See how to use the WAF, DNS and TLS without leaving gaps.

Reading time
10 min
Published

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.

Common security headers applied to the response
# 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-origin

A 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.

Frequently asked questions

Does Cloudflare improve web security and performance at the same time?
Yes, and that is exactly its advantage. Because it sits at the edge, Cloudflare improves security when it filters malicious traffic with the WAF, mitigates abuse and hides the origin. And it improves performance when it serves cached content from a distributed network close to the user, which reduces latency and the load on the origin. Both gains only show up with the correct cache, TLS and rule configuration; poorly configured, it delivers less on both fronts.
Does using Cloudflare already protect my site?
Only if it is well configured. If the origin is still reachable by direct IP, if the TLS mode is permissive or if the rules were never tuned, the protection is partial and can even give a false sense of security. The real gain comes from protecting the origin, tuning the WAF and taking care of TLS and caching.
How do I stop people from discovering my origin IP?
Restrict the origin in the firewall to accept connections only from Cloudflare's ranges, and avoid leaking the real IP in unproxied DNS records, email headers and application responses. If the origin IP leaks, the attacker can go straight to it and bypass the entire edge.
Which TLS mode should I use on Cloudflare?
Avoid permissive modes, which accept unencrypted traffic between the edge and the origin. Prefer a mode that requires valid end-to-end encryption, with a trusted certificate on the origin. The traffic is then protected along the entire path, not just between the user and the edge.
Does Cloudflare replace pentesting and fixing the application?
No. The edge filters malicious traffic and reduces abuse, but a flaw in the application remains exploitable when a request gets past the rules. Cloudflare is a complementary layer, and pentesting, vulnerability remediation and good practices in the application are still necessary.
Should I configure HSTS and security headers at the edge?
Yes, and the edge is a good place for it, because it applies the headers consistently, without depending on each application to send them. HSTS tells the browser to use only HTTPS, and headers such as those that prevent framing by third-party sites and content type guessing reduce entire classes of client-side attacks. The biggest caution is with HSTS preload: only enable it after confirming that every subdomain supports HTTPS, because reversing it is slow.
How do I block direct access to the origin even if someone finds the IP?
Filtering by network range helps, but whoever discovers the IP can still try to talk to the origin. The strongest layer is certificate-based origin authentication: the origin only responds to connections that present the edge's certificate, and knowing the address is no longer enough to get around Cloudflare. Combine this with network restriction and periodic rule reviews, so no forgotten exception opens the origin to the internet.
How do I protect the admin dashboard with Cloudflare?
Administrative routes do not need to be reachable from the entire internet. Put an authentication layer at the edge that requires identity before the request reaches the application, and restrict access to known networks or identities. Even if the WAF lets a request through, the administrative path then stays closed to anyone who is not authenticated. The exposed surface shrinks right where the impact of a compromise is greatest.

When to bring in specialist help

Configuring Cloudflare so the origin stays protected and the rules do not block users is part of GUARDIASEC's WAF and edge protection service, which assesses exposure, TLS, rules and cache and tunes the setup under observation.